~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

Fix merge issues with 1.0 CC fix.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
/**
17
17
  @file
18
18
 
19
19
  @brief
20
 
  mysql_select and join optimization
21
 
 
 
20
  select_query and join optimization
22
21
 
23
22
  @defgroup Query_Optimizer  Query Optimizer
24
23
  @{
25
24
*/
26
 
#include <drizzled/server_includes.h>
27
 
#include <drizzled/sql_select.h>
28
 
#include <drizzled/sj_tmp_table.h>
29
 
#include <drizzled/table_map_iterator.h>
30
 
 
31
 
#include <mysys/my_bit.h>
 
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
 
32
34
#include <drizzled/error.h>
33
35
#include <drizzled/gettext.h>
34
36
#include <drizzled/util/test.h>
46
48
#include <drizzled/check_stack_overrun.h>
47
49
#include <drizzled/lock.h>
48
50
#include <drizzled/item/outer_ref.h>
49
 
 
50
 
 
51
 
#include CMATH_H
52
 
 
53
 
#if defined(CMATH_NAMESPACE)
54
 
using namespace CMATH_NAMESPACE;
55
 
#endif
56
 
 
57
 
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
58
 
                              "MAYBE_REF","ALL","range","index",
59
 
                              "ref_or_null","unique_subquery","index_subquery",
60
 
                              "index_merge"
61
 
};
62
 
 
63
 
struct st_sargable_param;
64
 
 
65
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
66
 
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
67
 
                                 DYNAMIC_ARRAY *keyuse);
68
 
static bool update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,
69
 
                                JOIN_TAB *join_tab,
70
 
                                uint32_t tables, COND *conds,
71
 
                                COND_EQUAL *cond_equal,
72
 
                                table_map table_map, SELECT_LEX *select_lex,
73
 
                                st_sargable_param **sargables);
74
 
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
75
 
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
76
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
77
 
                               table_map used_tables);
78
 
static bool choose_plan(JOIN *join,table_map join_tables);
79
 
 
80
 
static void best_access_path(JOIN *join, JOIN_TAB *s, Session *session,
81
 
                             table_map remaining_tables, uint32_t idx,
82
 
                             double record_count, double read_time);
83
 
static void optimize_straight_join(JOIN *join, table_map join_tables);
84
 
static bool greedy_search(JOIN *join, table_map remaining_tables,
85
 
                             uint32_t depth, uint32_t prune_level);
86
 
static bool best_extension_by_limited_search(JOIN *join,
87
 
                                             table_map remaining_tables,
88
 
                                             uint32_t idx, double record_count,
89
 
                                             double read_time, uint32_t depth,
90
 
                                             uint32_t prune_level);
91
 
static uint32_t determine_search_depth(JOIN* join);
92
 
static int join_tab_cmp(const void* ptr1, const void* ptr2);
93
 
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
94
 
/*
95
 
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
96
 
  tested and approved.
97
 
*/
98
 
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
99
 
                      double record_count,double read_time);
100
 
static uint32_t cache_record_length(JOIN *join,uint32_t index);
101
 
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
102
 
static bool get_best_combination(JOIN *join);
103
 
static store_key *get_store_key(Session *session,
104
 
                                KEYUSE *keyuse, table_map used_tables,
105
 
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
106
 
                                uint32_t maybe_null);
107
 
static bool make_simple_join(JOIN *join,Table *tmp_table);
108
 
static void make_outerjoin_info(JOIN *join);
109
 
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
110
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
111
 
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
112
 
static void update_depend_map(JOIN *join);
113
 
static void update_depend_map(JOIN *join, order_st *order);
114
 
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
115
 
                           bool change_list, bool *simple_order);
116
 
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
117
 
                            List<Item> &fields, bool send_row,
118
 
                            uint64_t select_options, const char *info,
119
 
                            Item *having);
 
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
#include <drizzled/sql_union.h>
 
57
#include <drizzled/optimizer/key_field.h>
 
58
#include <drizzled/optimizer/position.h>
 
59
#include <drizzled/optimizer/sargable_param.h>
 
60
#include <drizzled/optimizer/key_use.h>
 
61
#include <drizzled/optimizer/range.h>
 
62
#include <drizzled/optimizer/quick_range_select.h>
 
63
#include <drizzled/optimizer/quick_ror_intersect_select.h>
 
64
#include <drizzled/filesort.h>
 
65
#include <drizzled/sql_lex.h>
 
66
#include <drizzled/session.h>
 
67
#include <drizzled/sort_field.h>
 
68
#include <drizzled/select_result.h>
 
69
#include <drizzled/key.h>
 
70
#include <drizzled/my_hash.h>
 
71
 
 
72
using namespace std;
 
73
 
 
74
namespace drizzled {
 
75
 
 
76
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
120
77
static COND *build_equal_items(Session *session, COND *cond,
121
78
                               COND_EQUAL *inherited,
122
79
                               List<TableList> *join_list,
123
80
                               COND_EQUAL **cond_equal_ref);
124
 
static COND* substitute_for_best_equal_field(COND *cond,
125
 
                                             COND_EQUAL *cond_equal,
126
 
                                             void *table_join_idx);
127
 
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
128
 
                            COND *conds, bool top, bool in_sj);
129
 
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
130
 
static void restore_prev_nj_state(JOIN_TAB *last);
131
 
static void reset_nj_counters(List<TableList> *join_list);
132
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
133
 
                                          uint32_t first_unused);
134
 
 
135
 
static
136
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
137
 
static void restore_prev_sj_state(const table_map remaining_tables,
138
 
                                  const JOIN_TAB *tab);
139
 
 
140
 
static COND *optimize_cond(JOIN *join, COND *conds,
141
 
                           List<TableList> *join_list,
142
 
                           Item::cond_result *cond_value);
143
 
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
144
 
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
145
 
 
146
 
static enum_nested_loop_state
147
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
148
 
                     int error);
149
 
static enum_nested_loop_state
150
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
151
 
static enum_nested_loop_state
152
 
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
153
 
static enum_nested_loop_state
154
 
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
155
 
static enum_nested_loop_state
156
 
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
157
 
static enum_nested_loop_state
158
 
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
159
 
static enum_nested_loop_state
160
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
161
 
 
162
 
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
163
 
static int join_read_system(JOIN_TAB *tab);
164
 
static int join_read_const(JOIN_TAB *tab);
165
 
static int join_read_key(JOIN_TAB *tab);
166
 
static int join_read_always_key(JOIN_TAB *tab);
167
 
static int join_read_last_key(JOIN_TAB *tab);
168
 
static int join_no_more_records(READ_RECORD *info);
169
 
static int join_read_next(READ_RECORD *info);
170
 
static int join_read_next_different(READ_RECORD *info);
171
 
static int join_init_quick_read_record(JOIN_TAB *tab);
172
 
static int test_if_quick_select(JOIN_TAB *tab);
173
 
static int join_init_read_record(JOIN_TAB *tab);
174
 
static int join_read_first(JOIN_TAB *tab);
175
 
static int join_read_next_same(READ_RECORD *info);
176
 
static int join_read_next_same_diff(READ_RECORD *info);
177
 
static int join_read_last(JOIN_TAB *tab);
178
 
static int join_read_prev_same(READ_RECORD *info);
179
 
static int join_read_prev(READ_RECORD *info);
180
 
int join_read_always_key_or_null(JOIN_TAB *tab);
181
 
int join_read_next_same_or_null(READ_RECORD *info);
182
 
static COND *make_cond_for_table(COND *cond,table_map table,
183
 
                                 table_map used_table,
184
 
                                 bool exclude_expensive_cond);
 
81
 
185
82
static Item* part_of_refkey(Table *form,Field *field);
186
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
187
 
                                    ha_rows select_limit, bool no_changes,
188
 
                                    const key_map *map);
189
 
static bool list_contains_unique_index(Table *table,
190
 
                          bool (*find_func) (Field *, void *), void *data);
191
 
static bool find_field_in_item_list (Field *field, void *data);
192
 
static bool find_field_in_order_list (Field *field, void *data);
193
 
static int create_sort_index(Session *session, JOIN *join, order_st *order,
194
 
                             ha_rows filesort_limit, ha_rows select_limit,
195
 
                             bool is_order_by);
196
 
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
197
 
                             Item *having);
198
 
static int remove_dup_with_compare(Session *session, Table *entry, Field **field,
199
 
                                   uint32_t offset, Item *having);
200
 
static int remove_dup_with_hash_index(Session *session,Table *table,
201
 
                                      uint32_t field_count, Field **first_field,
202
 
                                      uint32_t key_length, Item *having);
203
 
static int join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count);
204
 
static uint32_t used_blob_length(CACHE_FIELD **ptr);
205
 
static bool store_record_in_cache(JOIN_CACHE *cache);
206
 
static void reset_cache_read(JOIN_CACHE *cache);
207
 
static void reset_cache_write(JOIN_CACHE *cache);
208
 
static void read_cached_record(JOIN_TAB *tab);
209
 
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
210
 
static order_st *create_distinct_group(Session *session, Item **ref_pointer_array,
211
 
                                    order_st *order, List<Item> &fields,
212
 
                                    List<Item> &all_fields,
213
 
                                    bool *all_order_by_fields_used);
214
 
static bool test_if_subpart(order_st *a,order_st *b);
215
 
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
216
 
static void calc_group_buffer(JOIN *join,order_st *group);
217
 
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
218
 
static bool alloc_group_fields(JOIN *join,order_st *group);
219
 
// Create list for using with tempory table
220
 
static bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
221
 
                                     List<Item> &new_list1,
222
 
                                     List<Item> &new_list2,
223
 
                                     uint32_t elements, List<Item> &items);
224
 
// Create list for using with tempory table
225
 
static bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
226
 
                                      List<Item> &new_list1,
227
 
                                      List<Item> &new_list2,
228
 
                                      uint32_t elements, List<Item> &items);
229
 
static void init_tmptable_sum_functions(Item_sum **func);
230
 
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
231
 
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
232
 
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
233
 
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
234
 
static bool init_sum_functions(Item_sum **func, Item_sum **end);
235
 
static bool update_sum_func(Item_sum **func);
236
 
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
237
 
                            bool distinct, const char *message=NULL);
238
 
static Item *remove_additional_cond(Item* conds);
239
 
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
240
 
static bool test_if_ref(Item_field *left_item,Item *right_item);
241
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
242
 
                                       Item *new_cond, bool fix_fields);
 
83
static bool cmp_buffer_with_ref(JoinTable *tab);
 
84
static void change_cond_ref_to_const(Session *session,
 
85
                                     list<COND_CMP>& save_list,
 
86
                                     Item *and_father,
 
87
                                     Item *cond,
 
88
                                     Item *field,
 
89
                                     Item *value);
 
90
static void copy_blobs(Field **ptr);
243
91
 
244
92
static bool eval_const_cond(COND *cond)
245
93
{
246
 
    return ((Item_func*) cond)->val_int() ? true : false;
 
94
  return ((Item_func*) cond)->val_int() ? true : false;
247
95
}
248
96
 
249
 
 
250
97
/*
251
98
  This is used to mark equalities that were made from i-th IN-equality.
252
99
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
253
100
  The following variable occupies 64 addresses.
254
101
*/
255
 
const char *subq_sj_cond_name=
256
 
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
 
102
const char *subq_sj_cond_name= "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
257
103
 
258
 
static bool bitmap_covers(const table_map x, const table_map y)
 
104
static void copy_blobs(Field **ptr)
259
105
{
260
 
  return !test(y & ~x);
 
106
  for (; *ptr ; ptr++)
 
107
  {
 
108
    if ((*ptr)->flags & BLOB_FLAG)
 
109
    {
 
110
      ((Field_blob *) (*ptr))->copy();
 
111
    }
 
112
  }
261
113
}
262
114
 
263
115
/**
264
116
  This handles SELECT with and without UNION.
265
117
*/
266
 
 
267
118
bool handle_select(Session *session, LEX *lex, select_result *result,
268
 
                   ulong setup_tables_done_option)
 
119
                   uint64_t setup_tables_done_option)
269
120
{
270
121
  bool res;
271
 
  register SELECT_LEX *select_lex = &lex->select_lex;
272
 
  DRIZZLE_SELECT_START();
 
122
  Select_Lex *select_lex= &lex->select_lex;
 
123
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
273
124
 
274
 
  if (select_lex->master_unit()->is_union() ||
 
125
  if (select_lex->master_unit()->is_union() or
275
126
      select_lex->master_unit()->fake_select_lex)
276
 
    res= mysql_union(session, lex, result, &lex->unit, setup_tables_done_option);
 
127
  {
 
128
    res= drizzle_union(session, lex, result, &lex->unit,
 
129
                       setup_tables_done_option);
 
130
  }
277
131
  else
278
132
  {
279
 
    SELECT_LEX_UNIT *unit= &lex->unit;
 
133
    Select_Lex_Unit *unit= &lex->unit;
280
134
    unit->set_limit(unit->global_parameters);
281
135
    session->session_marker= 0;
 
136
 
282
137
    /*
283
 
      'options' of mysql_select will be set in JOIN, as far as JOIN for
 
138
      'options' of select_query will be set in JOIN, as far as JOIN for
284
139
      every PS/SP execution new, we will not need reset this flag if
285
140
      setup_tables_done_option changed for next rexecution
286
141
    */
287
 
    res= mysql_select(session, &select_lex->ref_pointer_array,
 
142
    res= select_query(session,
 
143
                      &select_lex->ref_pointer_array,
288
144
                      (TableList*) select_lex->table_list.first,
289
 
                      select_lex->with_wild, select_lex->item_list,
 
145
                      select_lex->with_wild,
 
146
                      select_lex->item_list,
290
147
                      select_lex->where,
291
 
                      select_lex->order_list.elements +
292
 
                      select_lex->group_list.elements,
293
 
                      (order_st*) select_lex->order_list.first,
294
 
                      (order_st*) select_lex->group_list.first,
 
148
                      select_lex->order_list.size() +
 
149
                      select_lex->group_list.size(),
 
150
                      (Order*) select_lex->order_list.first,
 
151
                      (Order*) select_lex->group_list.first,
295
152
                      select_lex->having,
296
 
                      (order_st*) lex->proc_list.first,
297
153
                      select_lex->options | session->options |
298
154
                      setup_tables_done_option,
299
155
                      result, unit, select_lex);
300
156
  }
301
157
  res|= session->is_error();
 
158
 
302
159
  if (unlikely(res))
 
160
  {
303
161
    result->abort();
 
162
  }
304
163
 
305
 
  DRIZZLE_SELECT_END();
306
 
  return(res);
 
164
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
 
165
  return res;
307
166
}
308
167
 
309
 
 
310
168
/*
311
169
  Fix fields referenced from inner selects.
312
170
 
347
205
    true  an error occured
348
206
    false ok
349
207
*/
350
 
 
351
 
bool
352
 
fix_inner_refs(Session *session, List<Item> &all_fields, SELECT_LEX *select,
353
 
                 Item **ref_pointer_array)
 
208
bool fix_inner_refs(Session *session,
 
209
                    List<Item> &all_fields,
 
210
                    Select_Lex *select,
 
211
                    Item **ref_pointer_array)
354
212
{
355
213
  Item_outer_ref *ref;
356
214
  bool res= false;
357
215
  bool direct_ref= false;
358
216
 
359
 
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
 
217
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
360
218
  while ((ref= ref_it++))
361
219
  {
362
220
    Item *item= ref->outer_ref;
363
221
    Item **item_ref= ref->ref;
364
222
    Item_ref *new_ref;
 
223
 
365
224
    /*
366
 
      TODO: this field item already might be present in the select list.
 
225
      @todo this field item already might be present in the select list.
367
226
      In this case instead of adding new field item we could use an
368
227
      existing one. The change will lead to less operations for copying fields,
369
228
      smaller temporary tables and less data passed through filesort.
370
229
    */
371
230
    if (ref_pointer_array && !ref->found_in_select_list)
372
231
    {
373
 
      int el= all_fields.elements;
 
232
      int el= all_fields.size();
374
233
      ref_pointer_array[el]= item;
375
234
      /* Add the field item to the select list of the current select. */
376
235
      all_fields.push_front(item);
385
244
    {
386
245
      Item_sum *sum_func;
387
246
      if (ref->in_sum_func->nest_level > select->nest_level)
 
247
      {
388
248
        direct_ref= true;
 
249
      }
389
250
      else
390
251
      {
391
252
        for (sum_func= ref->in_sum_func; sum_func &&
400
261
        }
401
262
      }
402
263
    }
 
264
 
403
265
    new_ref= direct_ref ?
404
266
              new Item_direct_ref(ref->context, item_ref, ref->table_name,
405
267
                          ref->field_name, ref->alias_name_used) :
406
268
              new Item_ref(ref->context, item_ref, ref->table_name,
407
269
                          ref->field_name, ref->alias_name_used);
408
 
    if (!new_ref)
409
 
      return true;
 
270
 
410
271
    ref->outer_ref= new_ref;
411
272
    ref->ref= &ref->outer_ref;
412
273
 
413
274
    if (!ref->fixed && ref->fix_fields(session, 0))
 
275
    {
414
276
      return true;
 
277
    }
415
278
    session->used_tables|= item->used_tables();
416
279
  }
417
280
  return res;
418
281
}
419
282
 
420
 
/**
421
 
  Function to setup clauses without sum functions.
422
 
*/
423
 
inline int setup_without_group(Session *session, Item **ref_pointer_array,
424
 
                               TableList *tables,
425
 
                               TableList *leaves,
426
 
                               List<Item> &fields,
427
 
                               List<Item> &all_fields,
428
 
                               COND **conds,
429
 
                               order_st *order,
430
 
                               order_st *group, bool *hidden_group_fields)
431
 
{
432
 
  int res;
433
 
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
434
 
 
435
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
436
 
  res= setup_conds(session, tables, leaves, conds);
437
 
 
438
 
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
439
 
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
440
 
                          order);
441
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
442
 
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
443
 
                          group, hidden_group_fields);
444
 
  session->lex->allow_sum_func= save_allow_sum_func;
445
 
  return(res);
446
 
}
447
 
 
448
283
/*****************************************************************************
449
284
  Check fields, find best join, do the select and output fields.
450
 
  mysql_select assumes that all tables are already opened
 
285
  select_query assumes that all tables are already opened
451
286
*****************************************************************************/
452
287
 
453
 
/**
454
 
  Prepare of whole select (including sub queries in future).
455
 
 
456
 
  @todo
457
 
    Add check of calculation of GROUP functions and fields:
458
 
    SELECT COUNT(*)+table.col1 from table1;
459
 
 
460
 
  @retval
461
 
    -1   on error
462
 
  @retval
463
 
    0   on success
464
 
*/
465
 
int
466
 
JOIN::prepare(Item ***rref_pointer_array,
467
 
              TableList *tables_init,
468
 
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
469
 
              order_st *order_init, order_st *group_init,
470
 
              Item *having_init,
471
 
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
472
 
              SELECT_LEX_UNIT *unit_arg)
473
 
{
474
 
  // to prevent double initialization on EXPLAIN
475
 
  if (optimized)
476
 
    return(0);
477
 
 
478
 
  conds= conds_init;
479
 
  order= order_init;
480
 
  group_list= group_init;
481
 
  having= having_init;
482
 
  proc_param= proc_param_init;
483
 
  tables_list= tables_init;
484
 
  select_lex= select_lex_arg;
485
 
  select_lex->join= this;
486
 
  join_list= &select_lex->top_join_list;
487
 
  union_part= unit_arg->is_union();
488
 
 
489
 
  session->lex->current_select->is_item_list_lookup= 1;
490
 
  /*
491
 
    If we have already executed SELECT, then it have not sense to prevent
492
 
    its table from update (see unique_table())
493
 
  */
494
 
  if (session->derived_tables_processing)
495
 
    select_lex->exclude_from_table_unique_test= true;
496
 
 
497
 
  /* Check that all tables, fields, conds and order are ok */
498
 
 
499
 
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
500
 
      setup_tables_and_check_access(session, &select_lex->context, join_list,
501
 
                                    tables_list, &select_lex->leaf_tables,
502
 
                                    false))
503
 
      return(-1);
504
 
 
505
 
  TableList *table_ptr;
506
 
  for (table_ptr= select_lex->leaf_tables;
507
 
       table_ptr;
508
 
       table_ptr= table_ptr->next_leaf)
509
 
    tables++;
510
 
 
511
 
  if (setup_wild(session, tables_list, fields_list, &all_fields, wild_num) ||
512
 
      select_lex->setup_ref_array(session, og_num) ||
513
 
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
514
 
                   &all_fields, 1) ||
515
 
      setup_without_group(session, (*rref_pointer_array), tables_list,
516
 
                          select_lex->leaf_tables, fields_list,
517
 
                          all_fields, &conds, order, group_list,
518
 
                          &hidden_group_fields))
519
 
    return(-1);                         /* purecov: inspected */
520
 
 
521
 
  ref_pointer_array= *rref_pointer_array;
522
 
 
523
 
  if (having)
524
 
  {
525
 
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
526
 
    session->where="having clause";
527
 
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
528
 
    select_lex->having_fix_field= 1;
529
 
    bool having_fix_rc= (!having->fixed &&
530
 
                         (having->fix_fields(session, &having) ||
531
 
                          having->check_cols(1)));
532
 
    select_lex->having_fix_field= 0;
533
 
    if (having_fix_rc || session->is_error())
534
 
      return(-1);                               /* purecov: inspected */
535
 
    session->lex->allow_sum_func= save_allow_sum_func;
536
 
  }
537
 
 
538
 
  {
539
 
    Item_subselect *subselect;
540
 
    Item_in_subselect *in_subs= NULL;
541
 
    /*
542
 
      Are we in a subquery predicate?
543
 
      TODO: the block below will be executed for every PS execution without need.
544
 
    */
545
 
    if ((subselect= select_lex->master_unit()->item))
546
 
    {
547
 
      bool do_semijoin= !test(session->variables.optimizer_switch &
548
 
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
549
 
      if (subselect->substype() == Item_subselect::IN_SUBS)
550
 
        in_subs= (Item_in_subselect*)subselect;
551
 
 
552
 
      /*
553
 
        Check if we're in subquery that is a candidate for flattening into a
554
 
        semi-join (which is done done in flatten_subqueries()). The
555
 
        requirements are:
556
 
          1. Subquery predicate is an IN/=ANY subq predicate
557
 
          2. Subquery is a single SELECT (not a UNION)
558
 
          3. Subquery does not have GROUP BY or order_st BY
559
 
          4. Subquery does not use aggregate functions or HAVING
560
 
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
561
 
          6. No execution method was already chosen (by a prepared statement).
562
 
 
563
 
          (*). We are not in a subquery of a single table UPDATE/DELETE that
564
 
               doesn't have a JOIN (TODO: We should handle this at some
565
 
               point by switching to multi-table UPDATE/DELETE)
566
 
 
567
 
          (**). We're not in a confluent table-less subquery, like
568
 
                "SELECT 1".
569
 
      */
570
 
      if (in_subs &&                                                    // 1
571
 
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
572
 
          !select_lex->group_list.elements && !order &&                 // 3
573
 
          !having && !select_lex->with_sum_func &&                      // 4
574
 
          session->session_marker &&                                            // 5
575
 
          select_lex->outer_select()->join &&                           // (*)
576
 
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
577
 
          do_semijoin &&
578
 
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
579
 
      {
580
 
        {
581
 
          if (!in_subs->left_expr->fixed &&
582
 
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
583
 
          {
584
 
            return(-1);
585
 
          }
586
 
          /*
587
 
            Check that the right part of the subselect contains no more than one
588
 
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
589
 
          */
590
 
          if (subselect->substype() == Item_subselect::IN_SUBS &&
591
 
             (select_lex->item_list.elements !=
592
 
              ((Item_in_subselect*)subselect)->left_expr->cols()))
593
 
          {
594
 
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
595
 
            return(-1);
596
 
          }
597
 
        }
598
 
 
599
 
        /* Register the subquery for further processing */
600
 
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
601
 
        in_subs->expr_join_nest= (TableList*)session->session_marker;
602
 
      }
603
 
      else
604
 
      {
605
 
        bool do_materialize= !test(session->variables.optimizer_switch &
606
 
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
607
 
        /*
608
 
          Check if the subquery predicate can be executed via materialization.
609
 
          The required conditions are:
610
 
          1. Subquery predicate is an IN/=ANY subq predicate
611
 
          2. Subquery is a single SELECT (not a UNION)
612
 
          3. Subquery is not a table-less query. In this case there is no
613
 
             point in materializing.
614
 
          4. Subquery predicate is a top-level predicate
615
 
             (this implies it is not negated)
616
 
             TODO: this is a limitation that should be lifeted once we
617
 
             implement correct NULL semantics (WL#3830)
618
 
          5. Subquery is non-correlated
619
 
             TODO:
620
 
             This is an overly restrictive condition. It can be extended to:
621
 
             (Subquery is non-correlated ||
622
 
              Subquery is correlated to any query outer to IN predicate ||
623
 
              (Subquery is correlated to the immediate outer query &&
624
 
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
625
 
               aggregate functions) && subquery predicate is not under "NOT IN"))
626
 
          6. No execution method was already chosen (by a prepared statement).
627
 
 
628
 
          (*) The subquery must be part of a SELECT statement. The current
629
 
               condition also excludes multi-table update statements.
630
 
 
631
 
          We have to determine whether we will perform subquery materialization
632
 
          before calling the IN=>EXISTS transformation, so that we know whether to
633
 
          perform the whole transformation or only that part of it which wraps
634
 
          Item_in_subselect in an Item_in_optimizer.
635
 
        */
636
 
        if (do_materialize &&
637
 
            in_subs  &&                                                   // 1
638
 
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
639
 
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
640
 
            session->lex->sql_command == SQLCOM_SELECT)                       // *
641
 
        {
642
 
          if (in_subs->is_top_level_item() &&                             // 4
643
 
              !in_subs->is_correlated &&                                  // 5
644
 
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
645
 
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
646
 
        }
647
 
 
648
 
        Item_subselect::trans_res trans_res;
649
 
        if ((trans_res= subselect->select_transformer(this)) !=
650
 
            Item_subselect::RES_OK)
651
 
        {
652
 
          return((trans_res == Item_subselect::RES_ERROR));
653
 
        }
654
 
      }
655
 
    }
656
 
  }
657
 
 
658
 
  if (order)
659
 
  {
660
 
    order_st *ord;
661
 
    for (ord= order; ord; ord= ord->next)
662
 
    {
663
 
      Item *item= *ord->item;
664
 
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
665
 
        item->split_sum_func(session, ref_pointer_array, all_fields);
666
 
    }
667
 
  }
668
 
 
669
 
  if (having && having->with_sum_func)
670
 
    having->split_sum_func(session, ref_pointer_array, all_fields,
671
 
                           &having, true);
672
 
  if (select_lex->inner_sum_func_list)
673
 
  {
674
 
    Item_sum *end=select_lex->inner_sum_func_list;
675
 
    Item_sum *item_sum= end;
676
 
    do
677
 
    {
678
 
      item_sum= item_sum->next;
679
 
      item_sum->split_sum_func(session, ref_pointer_array,
680
 
                               all_fields, item_sum->ref_by, false);
681
 
    } while (item_sum != end);
682
 
  }
683
 
 
684
 
  if (select_lex->inner_refs_list.elements &&
685
 
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
686
 
    return(-1);
687
 
 
688
 
  /*
689
 
    Check if there are references to un-aggregated columns when computing
690
 
    aggregate functions with implicit grouping (there is no GROUP BY).
691
 
 
692
 
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
693
 
  */
694
 
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
695
 
  {
696
 
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
697
 
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
698
 
    return(-1);
699
 
  }
700
 
  {
701
 
    /* Caclulate the number of groups */
702
 
    send_group_parts= 0;
703
 
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
704
 
      send_group_parts++;
705
 
  }
706
 
 
707
 
  if (error)
708
 
    goto err;                                   /* purecov: inspected */
709
 
 
710
 
  if (result && result->prepare(fields_list, unit_arg))
711
 
    goto err;                                   /* purecov: inspected */
712
 
 
713
 
  /* Init join struct */
714
 
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
715
 
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
716
 
  this->group= group_list != 0;
717
 
  unit= unit_arg;
718
 
 
719
 
#ifdef RESTRICTED_GROUP
720
 
  if (sum_func_count && !group_list && (func_count || field_count))
721
 
  {
722
 
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
723
 
    goto err;
724
 
  }
725
 
#endif
726
 
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
727
 
    goto err;
728
 
  if (alloc_func_list())
729
 
    goto err;
730
 
 
731
 
  return(0); // All OK
732
 
 
733
 
err:
734
 
  return(-1);                           /* purecov: inspected */
735
 
}
736
 
 
737
 
 
738
 
/*
739
 
  Remove the predicates pushed down into the subquery
740
 
 
741
 
  SYNOPSIS
742
 
    JOIN::remove_subq_pushed_predicates()
743
 
      where   IN  Must be NULL
744
 
              OUT The remaining WHERE condition, or NULL
745
 
 
746
 
  DESCRIPTION
747
 
    Given that this join will be executed using (unique|index)_subquery,
748
 
    without "checking NULL", remove the predicates that were pushed down
749
 
    into the subquery.
750
 
 
751
 
    If the subquery compares scalar values, we can remove the condition that
752
 
    was wrapped into trig_cond (it will be checked when needed by the subquery
753
 
    engine)
754
 
 
755
 
    If the subquery compares row values, we need to keep the wrapped
756
 
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
757
 
    and non-NULL values, we'll do a full table scan and will rely on the
758
 
    equalities corresponding to non-NULL parts of left tuple to filter out
759
 
    non-matching records.
760
 
 
761
 
    TODO: We can remove the equalities that will be guaranteed to be true by the
762
 
    fact that subquery engine will be using index lookup. This must be done only
763
 
    for cases where there are no conversion errors of significance, e.g. 257
764
 
    that is searched in a byte. But this requires homogenization of the return
765
 
    codes of all Field*::store() methods.
766
 
*/
767
 
 
768
 
void JOIN::remove_subq_pushed_predicates(Item **where)
769
 
{
770
 
  if (conds->type() == Item::FUNC_ITEM &&
771
 
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
772
 
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
773
 
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
774
 
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
775
 
                   ((Item_func *)conds)->arguments()[0]))
776
 
  {
777
 
    *where= 0;
778
 
    return;
779
 
  }
780
 
}
781
 
 
782
 
 
783
288
/*
784
289
  Index lookup-based subquery: save some flags for EXPLAIN output
785
290
 
798
303
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
799
304
    and set appropriate flags in join_tab->packed_info.
800
305
*/
801
 
 
802
 
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
 
306
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
803
307
{
804
308
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
805
 
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
 
309
 
 
310
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
806
311
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
 
312
 
807
313
  if (where)
808
314
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
 
315
 
809
316
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
810
317
  {
811
318
    if (join_tab->ref.cond_guards[i])
816
323
  }
817
324
}
818
325
 
819
 
 
820
 
 
821
 
 
822
 
/*
823
 
  Check if the table's rowid is included in the temptable
824
 
 
825
 
  SYNOPSIS
826
 
    sj_table_is_included()
827
 
      join      The join
828
 
      join_tab  The table to be checked
829
 
 
830
 
  DESCRIPTION
831
 
    SemiJoinDuplicateElimination: check the table's rowid should be included
832
 
    in the temptable. This is so if
833
 
 
834
 
    1. The table is not embedded within some semi-join nest
835
 
    2. The has been pulled out of a semi-join nest, or
836
 
 
837
 
    3. The table is functionally dependent on some previous table
838
 
 
839
 
    [4. This is also true for constant tables that can't be
840
 
        NULL-complemented but this function is not called for such tables]
841
 
 
842
 
  RETURN
843
 
    true  - Include table's rowid
844
 
    false - Don't
845
 
*/
846
 
 
847
 
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
848
 
{
849
 
  if (join_tab->emb_sj_nest)
850
 
    return false;
851
 
 
852
 
  /* Check if this table is functionally dependent on the tables that
853
 
     are within the same outer join nest
854
 
  */
855
 
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
856
 
  if (join_tab->type == JT_EQ_REF)
857
 
  {
858
 
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
859
 
    uint32_t idx;
860
 
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
861
 
    {
862
 
      JOIN_TAB *ref_tab= join->join_tab + idx;
863
 
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
864
 
        return true;
865
 
    }
866
 
    /* Ok, functionally dependent */
867
 
    return false;
868
 
  }
869
 
  /* Not functionally dependent => need to include*/
870
 
  return true;
871
 
}
872
 
 
873
 
 
874
 
/*
875
 
  Setup the strategies to eliminate semi-join duplicates.
876
 
 
877
 
  SYNOPSIS
878
 
    setup_semijoin_dups_elimination()
879
 
      join           Join to process
880
 
      options        Join options (needed to see if join buffering will be
881
 
                     used or not)
882
 
      no_jbuf_after  Another bit of information re where join buffering will
883
 
                     be used.
884
 
 
885
 
  DESCRIPTION
886
 
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
887
 
    strategies:
888
 
 
889
 
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
890
 
                         of row combinations)
891
 
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
892
 
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
893
 
                  together and picking the 1st one)
894
 
 
895
 
    The join order has "duplicate-generating ranges", and every range is
896
 
    served by one strategy or a combination of FirstMatch with with some
897
 
    other strategy.
898
 
 
899
 
    "Duplicate-generating range" is defined as a range within the join order
900
 
    that contains all of the inner tables of a semi-join. All ranges must be
901
 
    disjoint, if tables of several semi-joins are interleaved, then the ranges
902
 
    are joined together, which is equivalent to converting
903
 
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
904
 
    to
905
 
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
906
 
    .
907
 
 
908
 
    Applicability conditions are as follows:
909
 
 
910
 
    DuplicateWeedout strategy
911
 
    ~~~~~~~~~~~~~~~~~~~~~~~~~
912
 
 
913
 
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
914
 
      +------+  +=========================+  +---+
915
 
        (1)                 (2)               (3)
916
 
 
917
 
       (1) - Prefix of OuterTables (those that participate in
918
 
             IN-equality and/or are correlated with subquery) and outer
919
 
             Noncorrelated Tables.
920
 
       (2) - The handled range. The range starts with the first sj-inner
921
 
             table, and covers all sj-inner and outer tables
922
 
             Within the range,  Inner, Outer, outer Noncorrelated tables
923
 
             may follow in any order.
924
 
       (3) - The suffix of outer Noncorrelated tables.
925
 
 
926
 
    FirstMatch strategy
927
 
    ~~~~~~~~~~~~~~~~~~~
928
 
 
929
 
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
930
 
      +------+  +==================+  +---+
931
 
        (1)             (2)          (3)
932
 
 
933
 
      (1) - Prefix of outer and non-correlated tables
934
 
      (2) - The handled range, which may contain only inner and
935
 
            non-correlated tables.
936
 
      (3) - The suffix of outer Noncorrelated tables.
937
 
 
938
 
    InsideOut strategy
939
 
    ~~~~~~~~~~~~~~~~~~
940
 
 
941
 
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
942
 
     +--------+   +===========+ +=============+   +------+
943
 
        (1)           (2)          (3)              (4)
944
 
 
945
 
      (1) - Prefix that may contain any outer tables. The prefix must contain
946
 
            all the non-trivially correlated outer tables. (non-trivially means
947
 
            that the correlation is not just through the IN-equality).
948
 
 
949
 
      (2) - Inner table for which the InsideOut scan is performed.
950
 
 
951
 
      (3) - The remainder of the duplicate-generating range. It is served by
952
 
            application of FirstMatch strategy, with the exception that
953
 
            outer IN-correlated tables are considered to be non-correlated.
954
 
 
955
 
      (4) - THe suffix of outer and outer non-correlated tables.
956
 
 
957
 
    If several strategies are applicable, their relative priorities are:
958
 
      1. InsideOut
959
 
      2. FirstMatch
960
 
      3. DuplicateWeedout
961
 
 
962
 
    This function walks over the join order and sets up the strategies by
963
 
    setting appropriate members in join_tab structures.
964
 
 
965
 
  RETURN
966
 
    false  OK
967
 
    true   Out of memory error
968
 
*/
969
 
 
970
 
static
971
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
972
 
{
973
 
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
974
 
  struct {
975
 
    /*
976
 
      0 - invalid (EOF marker),
977
 
      1 - InsideOut,
978
 
      2 - Temptable (maybe confluent),
979
 
      3 - Temptable with join buffering
980
 
    */
981
 
    uint32_t strategy;
982
 
    uint32_t start_idx; /* Left range bound */
983
 
    uint32_t end_idx;   /* Right range bound */
984
 
    /*
985
 
      For Temptable strategy: Bitmap of all outer and correlated tables from
986
 
      all involved join nests.
987
 
    */
988
 
    table_map outer_tables;
989
 
  } dups_ranges [MAX_TABLES];
990
 
 
991
 
  TableList *emb_insideout_nest= NULL;
992
 
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
993
 
                               tables) whose ranges we're in */
994
 
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
995
 
  table_map range_start_map= 0; /* table_map at current range start */
996
 
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
997
 
  int cur_range= 0;
998
 
  uint32_t i;
999
 
 
1000
 
  /*
1001
 
    First pass: locate the duplicate-generating ranges and pick the strategies.
1002
 
  */
1003
 
  for (i=join->const_tables ; i < join->tables ; i++)
1004
 
  {
1005
 
    JOIN_TAB *tab=join->join_tab+i;
1006
 
    Table *table=tab->table;
1007
 
    cur_map |= table->map;
1008
 
 
1009
 
    if (tab->emb_sj_nest) // Encountered an sj-inner table
1010
 
    {
1011
 
      if (!emb_sj_map)
1012
 
      {
1013
 
        dups_ranges[cur_range].start_idx= i;
1014
 
        range_start_map= cur_map & ~table->map;
1015
 
        /*
1016
 
          Remember if this is a possible start of range that is covered by
1017
 
          the InsideOut strategy (the reason that it is not covered could
1018
 
          be that it overlaps with anther semi-join's range. we don't
1019
 
          support InsideOut for joined ranges)
1020
 
        */
1021
 
        if (join->best_positions[i].use_insideout_scan)
1022
 
          emb_insideout_nest= tab->emb_sj_nest;
1023
 
      }
1024
 
 
1025
 
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1026
 
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1027
 
 
1028
 
      if (tab->emb_sj_nest != emb_insideout_nest)
1029
 
      {
1030
 
        /*
1031
 
          Two different semi-joins interleave. This cannot be handled by
1032
 
          InsideOut strategy.
1033
 
        */
1034
 
        emb_insideout_nest= NULL;
1035
 
      }
1036
 
    }
1037
 
 
1038
 
    if (emb_sj_map) /* We're in duplicate-generating range */
1039
 
    {
1040
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1041
 
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1042
 
          i <= no_jbuf_after && !dealing_with_jbuf)
1043
 
      {
1044
 
        /*
1045
 
          This table uses join buffering, which makes use of FirstMatch or
1046
 
          InsideOut strategies impossible for the current and (we assume)
1047
 
          preceding duplicate-producing ranges.
1048
 
          That is, for the join order:
1049
 
 
1050
 
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
1051
 
                  |     |     |     |          | \
1052
 
                  +-----+     +-----+          |  join buffering use
1053
 
                     r1          r2         we're here
1054
 
 
1055
 
          we'll have to remove r1 and r2 and use duplicate-elimination
1056
 
          strategy that spans all the tables, starting from the very 1st
1057
 
          one.
1058
 
        */
1059
 
        dealing_with_jbuf= true;
1060
 
        emb_insideout_nest= false;
1061
 
 
1062
 
        /*
1063
 
          Absorb all preceding duplicate-eliminating ranges. Their strategies
1064
 
          do not matter:
1065
 
        */
1066
 
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
1067
 
        {
1068
 
          dups_ranges[cur_range].outer_tables |=
1069
 
            dups_ranges[prev_range].outer_tables;
1070
 
        }
1071
 
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1072
 
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1073
 
        cur_range=  0;
1074
 
      }
1075
 
 
1076
 
      /*
1077
 
        Check if we are at the end of duplicate-producing range. We are if
1078
 
 
1079
 
        1. It's an InsideOut range (which presumes all correlated tables are
1080
 
           in the prefix), and all inner tables are in the join order prefix,
1081
 
           or
1082
 
        2. It's a DuplicateElimination range (possibly covering several
1083
 
           SJ-nests), and all inner, outer, and correlated tables of all
1084
 
           sj-nests are in the join order prefix.
1085
 
      */
1086
 
      bool end_of_range= false;
1087
 
      if (emb_insideout_nest &&
1088
 
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1089
 
      {
1090
 
        /* Save that this range is handled with InsideOut: */
1091
 
        dups_ranges[cur_range].strategy= 1;
1092
 
        end_of_range= true;
1093
 
      }
1094
 
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1095
 
      {
1096
 
        /*
1097
 
          This is a complete range to be handled with either DuplicateWeedout
1098
 
          or FirstMatch
1099
 
        */
1100
 
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1101
 
        /*
1102
 
          This will hold tables from within the range that need to be put
1103
 
          into the join buffer before we can use the FirstMatch on its tail.
1104
 
        */
1105
 
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
1106
 
                                             ~range_start_map;
1107
 
        end_of_range= true;
1108
 
      }
1109
 
 
1110
 
      if (end_of_range)
1111
 
      {
1112
 
        dups_ranges[cur_range].end_idx= i+1;
1113
 
        emb_sj_map= emb_outer_tables= 0;
1114
 
        emb_insideout_nest= NULL;
1115
 
        dealing_with_jbuf= false;
1116
 
        dups_ranges[++cur_range].strategy= 0;
1117
 
      }
1118
 
    }
1119
 
  }
1120
 
 
1121
 
  Session *session= join->session;
1122
 
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1123
 
  /*
1124
 
    Second pass: setup the chosen strategies
1125
 
  */
1126
 
  for (int j= 0; j < cur_range; j++)
1127
 
  {
1128
 
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1129
 
    JOIN_TAB *jump_to;
1130
 
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
1131
 
    {
1132
 
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1133
 
      jump_to= tab++;
1134
 
    }
1135
 
    else // DuplicateWeedout strategy
1136
 
    {
1137
 
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1138
 
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1139
 
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1140
 
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
1141
 
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1142
 
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1143
 
      JOIN_TAB *last_outer_tab= tab - 1;
1144
 
      /*
1145
 
        Walk through the range and remember
1146
 
         - tables that need their rowids to be put into temptable
1147
 
         - the last outer table
1148
 
      */
1149
 
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1150
 
      {
1151
 
        if (sj_table_is_included(join, tab))
1152
 
        {
1153
 
          last_tab->join_tab= tab;
1154
 
          last_tab->rowid_offset= jt_rowid_offset;
1155
 
          jt_rowid_offset += tab->table->file->ref_length;
1156
 
          if (tab->table->maybe_null)
1157
 
          {
1158
 
            last_tab->null_byte= jt_null_bits / 8;
1159
 
            last_tab->null_bit= jt_null_bits++;
1160
 
          }
1161
 
          last_tab++;
1162
 
          tab->table->prepare_for_position();
1163
 
          tab->rowid_keep_flags= rowid_keep_flags;
1164
 
        }
1165
 
        cur_map |= tab->table->map;
1166
 
        if (!tab->emb_sj_nest && bitmap_covers(cur_map,
1167
 
                                               dups_ranges[j].outer_tables))
1168
 
          last_outer_tab= tab;
1169
 
      }
1170
 
 
1171
 
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1172
 
      {
1173
 
        SJ_TMP_TABLE *sjtbl;
1174
 
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1175
 
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
1176
 
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
1177
 
          return(true);
1178
 
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1179
 
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1180
 
        sjtbl->rowid_len= jt_rowid_offset;
1181
 
        sjtbl->null_bits= jt_null_bits;
1182
 
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
1183
 
 
1184
 
        *next_sjtbl_ptr= sjtbl;
1185
 
        next_sjtbl_ptr= &(sjtbl->next);
1186
 
        sjtbl->next= NULL;
1187
 
 
1188
 
        sjtbl->tmp_table=
1189
 
          create_duplicate_weedout_tmp_table(session,
1190
 
                                             sjtbl->rowid_len +
1191
 
                                             sjtbl->null_bytes,
1192
 
                                             sjtbl);
1193
 
 
1194
 
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1195
 
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1196
 
      }
1197
 
      tab= last_outer_tab + 1;
1198
 
      jump_to= last_outer_tab;
1199
 
    }
1200
 
 
1201
 
    /* Create the FirstMatch tail */
1202
 
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1203
 
    {
1204
 
      if (tab->emb_sj_nest)
1205
 
        tab->do_firstmatch= jump_to;
1206
 
      else
1207
 
        jump_to= tab;
1208
 
    }
1209
 
  }
1210
 
  return(false);
1211
 
}
1212
 
 
1213
 
 
1214
 
static void cleanup_sj_tmp_tables(JOIN *join)
1215
 
{
1216
 
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1217
 
       sj_tbl= sj_tbl->next)
1218
 
  {
1219
 
    if (sj_tbl->tmp_table)
1220
 
    {
1221
 
      sj_tbl->tmp_table->free_tmp_table(join->session);
1222
 
    }
1223
 
  }
1224
 
  join->sj_tmp_tables= NULL;
1225
 
}
1226
 
 
1227
 
uint32_t make_join_orderinfo(JOIN *join);
1228
 
 
1229
 
/**
1230
 
  global select optimisation.
1231
 
 
1232
 
  @note
1233
 
    error code saved in field 'error'
1234
 
 
1235
 
  @retval
1236
 
    0   success
1237
 
  @retval
1238
 
    1   error
1239
 
*/
1240
 
 
1241
 
int
1242
 
JOIN::optimize()
1243
 
{
1244
 
  // to prevent double initialization on EXPLAIN
1245
 
  if (optimized)
1246
 
    return(0);
1247
 
  optimized= 1;
1248
 
 
1249
 
  session->set_proc_info("optimizing");
1250
 
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1251
 
              unit->select_limit_cnt);
1252
 
  /* select_limit is used to decide if we are likely to scan the whole table */
1253
 
  select_limit= unit->select_limit_cnt;
1254
 
  if (having || (select_options & OPTION_FOUND_ROWS))
1255
 
    select_limit= HA_POS_ERROR;
1256
 
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1257
 
  // Ignore errors of execution if option IGNORE present
1258
 
  if (session->lex->ignore)
1259
 
    session->lex->current_select->no_error= 1;
1260
 
 
1261
 
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
1262
 
  /* Add HAVING to WHERE if possible */
1263
 
  if (having && !group_list && !sum_func_count)
1264
 
  {
1265
 
    if (!conds)
1266
 
    {
1267
 
      conds= having;
1268
 
      having= 0;
1269
 
    }
1270
 
    else if ((conds=new Item_cond_and(conds,having)))
1271
 
    {
1272
 
      /*
1273
 
        Item_cond_and can't be fixed after creation, so we do not check
1274
 
        conds->fixed
1275
 
      */
1276
 
      conds->fix_fields(session, &conds);
1277
 
      conds->change_ref_to_fields(session, tables_list);
1278
 
      conds->top_level_item();
1279
 
      having= 0;
1280
 
    }
1281
 
  }
1282
 
#endif
1283
 
 
1284
 
  /* Convert all outer joins to inner joins if possible */
1285
 
  conds= simplify_joins(this, join_list, conds, true, false);
1286
 
  build_bitmap_for_nested_joins(join_list, 0);
1287
 
 
1288
 
  conds= optimize_cond(this, conds, join_list, &cond_value);
1289
 
  if (session->is_error())
1290
 
  {
1291
 
    error= 1;
1292
 
    return(1);
1293
 
  }
1294
 
 
1295
 
  {
1296
 
    having= optimize_cond(this, having, join_list, &having_value);
1297
 
    if (session->is_error())
1298
 
    {
1299
 
      error= 1;
1300
 
      return(1);
1301
 
    }
1302
 
    if (select_lex->where)
1303
 
      select_lex->cond_value= cond_value;
1304
 
    if (select_lex->having)
1305
 
      select_lex->having_value= having_value;
1306
 
 
1307
 
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1308
 
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1309
 
    {                                           /* Impossible cond */
1310
 
      zero_result_cause=  having_value == Item::COND_FALSE ?
1311
 
                           "Impossible HAVING" : "Impossible WHERE";
1312
 
      error= 0;
1313
 
      return(0);
1314
 
    }
1315
 
  }
1316
 
 
1317
 
  /* Optimize count(*), cmin() and cmax() */
1318
 
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1319
 
  {
1320
 
    int res;
1321
 
    /*
1322
 
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1323
 
      to the WHERE conditions,
1324
 
      or 1 if all items were resolved,
1325
 
      or 0, or an error number HA_ERR_...
1326
 
    */
1327
 
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1328
 
    {
1329
 
      if (res == HA_ERR_KEY_NOT_FOUND)
1330
 
      {
1331
 
        zero_result_cause= "No matching min/max row";
1332
 
        error=0;
1333
 
        return(0);
1334
 
      }
1335
 
      if (res > 1)
1336
 
      {
1337
 
        error= res;
1338
 
        return(1);
1339
 
      }
1340
 
      if (res < 0)
1341
 
      {
1342
 
        zero_result_cause= "No matching min/max row";
1343
 
        error=0;
1344
 
        return(0);
1345
 
      }
1346
 
      zero_result_cause= "Select tables optimized away";
1347
 
      tables_list= 0;                           // All tables resolved
1348
 
      /*
1349
 
        Extract all table-independent conditions and replace the WHERE
1350
 
        clause with them. All other conditions were computed by opt_sum_query
1351
 
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
1352
 
        so there is no need to compute the whole WHERE clause again.
1353
 
        Notice that make_cond_for_table() will always succeed to remove all
1354
 
        computed conditions, because opt_sum_query() is applicable only to
1355
 
        conjunctions.
1356
 
        Preserve conditions for EXPLAIN.
1357
 
      */
1358
 
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
1359
 
      {
1360
 
        COND *table_independent_conds=
1361
 
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1362
 
        conds= table_independent_conds;
1363
 
      }
1364
 
    }
1365
 
  }
1366
 
  if (!tables_list)
1367
 
  {
1368
 
    error= 0;
1369
 
    return(0);
1370
 
  }
1371
 
  error= -1;                                    // Error is sent to client
1372
 
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1373
 
 
1374
 
  /* Calculate how to do the join */
1375
 
  session->set_proc_info("statistics");
1376
 
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1377
 
      session->is_fatal_error)
1378
 
  {
1379
 
    return(1);
1380
 
  }
1381
 
 
1382
 
  /* Remove distinct if only const tables */
1383
 
  select_distinct= select_distinct && (const_tables != tables);
1384
 
  session->set_proc_info("preparing");
1385
 
  if (result->initialize_tables(this))
1386
 
  {
1387
 
    return(1);                          // error == -1
1388
 
  }
1389
 
  if (const_table_map != found_const_table_map &&
1390
 
      !(select_options & SELECT_DESCRIBE) &&
1391
 
      (!conds ||
1392
 
       !(conds->used_tables() & RAND_TABLE_BIT) ||
1393
 
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
1394
 
  {
1395
 
    zero_result_cause= "no matching row in const table";
1396
 
    error= 0;
1397
 
    return(0);
1398
 
  }
1399
 
  if (!(session->options & OPTION_BIG_SELECTS) &&
1400
 
      best_read > (double) session->variables.max_join_size &&
1401
 
      !(select_options & SELECT_DESCRIBE))
1402
 
  {                                             /* purecov: inspected */
1403
 
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1404
 
    error= -1;
1405
 
    return(1);
1406
 
  }
1407
 
  if (const_tables && !session->locked_tables &&
1408
 
      !(select_options & SELECT_NO_UNLOCK))
1409
 
    mysql_unlock_some_tables(session, table, const_tables);
1410
 
  if (!conds && outer_join)
1411
 
  {
1412
 
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1413
 
    conds=new Item_int((int64_t) 1,1);  // Always true
1414
 
  }
1415
 
  select= make_select(*table, const_table_map,
1416
 
                      const_table_map, conds, 1, &error);
1417
 
  if (error)
1418
 
  {                                             /* purecov: inspected */
1419
 
    error= -1;                                  /* purecov: inspected */
1420
 
    return(1);
1421
 
  }
1422
 
 
1423
 
  reset_nj_counters(join_list);
1424
 
  make_outerjoin_info(this);
1425
 
 
1426
 
  /*
1427
 
    Among the equal fields belonging to the same multiple equality
1428
 
    choose the one that is to be retrieved first and substitute
1429
 
    all references to these in where condition for a reference for
1430
 
    the selected field.
1431
 
  */
1432
 
  if (conds)
1433
 
  {
1434
 
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1435
 
    conds->update_used_tables();
1436
 
  }
1437
 
 
1438
 
  /*
1439
 
    Permorm the the optimization on fields evaluation mentioned above
1440
 
    for all on expressions.
1441
 
  */
1442
 
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1443
 
  {
1444
 
    if (*tab->on_expr_ref)
1445
 
    {
1446
 
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1447
 
                                                         tab->cond_equal,
1448
 
                                                         map2table);
1449
 
      (*tab->on_expr_ref)->update_used_tables();
1450
 
    }
1451
 
  }
1452
 
 
1453
 
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
1454
 
      (select_options & SELECT_DESCRIBE) &&
1455
 
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
1456
 
  {
1457
 
    conds=new Item_int((int64_t) 0,1);  // Always false
1458
 
  }
1459
 
  if (make_join_select(this, select, conds))
1460
 
  {
1461
 
    zero_result_cause=
1462
 
      "Impossible WHERE noticed after reading const tables";
1463
 
    return(0);                          // error == 0
1464
 
  }
1465
 
 
1466
 
  error= -1;                                    /* if goto err */
1467
 
 
1468
 
  /* Optimize distinct away if possible */
1469
 
  {
1470
 
    order_st *org_order= order;
1471
 
    order=remove_const(this, order,conds,1, &simple_order);
1472
 
    if (session->is_error())
1473
 
    {
1474
 
      error= 1;
1475
 
      return(1);
1476
 
    }
1477
 
 
1478
 
    /*
1479
 
      If we are using order_st BY NULL or order_st BY const_expression,
1480
 
      return result in any order (even if we are using a GROUP BY)
1481
 
    */
1482
 
    if (!order && org_order)
1483
 
      skip_sort_order= 1;
1484
 
  }
1485
 
  /*
1486
 
     Check if we can optimize away GROUP BY/DISTINCT.
1487
 
     We can do that if there are no aggregate functions, the
1488
 
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
1489
 
     (if present) contain direct references to all key parts of
1490
 
     an unique index (in whatever order) and if the key parts of the
1491
 
     unique index cannot contain NULLs.
1492
 
     Note that the unique keys for DISTINCT and GROUP BY should not
1493
 
     be the same (as long as they are unique).
1494
 
 
1495
 
     The FROM clause must contain a single non-constant table.
1496
 
  */
1497
 
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
1498
 
      !tmp_table_param.sum_func_count &&
1499
 
      (!join_tab[const_tables].select ||
1500
 
       !join_tab[const_tables].select->quick ||
1501
 
       join_tab[const_tables].select->quick->get_type() !=
1502
 
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1503
 
  {
1504
 
    if (group_list &&
1505
 
       list_contains_unique_index(join_tab[const_tables].table,
1506
 
                                 find_field_in_order_list,
1507
 
                                 (void *) group_list))
1508
 
    {
1509
 
      /*
1510
 
        We have found that grouping can be removed since groups correspond to
1511
 
        only one row anyway, but we still have to guarantee correct result
1512
 
        order. The line below effectively rewrites the query from GROUP BY
1513
 
        <fields> to order_st BY <fields>. There are two exceptions:
1514
 
        - if skip_sort_order is set (see above), then we can simply skip
1515
 
          GROUP BY;
1516
 
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
1517
 
          with the GROUP BY ones, i.e. either one is a prefix of another.
1518
 
          We only check if the order_st BY is a prefix of GROUP BY. In this case
1519
 
          test_if_subpart() copies the ASC/DESC attributes from the original
1520
 
          order_st BY fields.
1521
 
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
1522
 
          'order' as is.
1523
 
       */
1524
 
      if (!order || test_if_subpart(group_list, order))
1525
 
          order= skip_sort_order ? 0 : group_list;
1526
 
      /*
1527
 
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1528
 
        rewritten to IGNORE INDEX FOR order_st BY(fields).
1529
 
      */
1530
 
      join_tab->table->keys_in_use_for_order_by=
1531
 
        join_tab->table->keys_in_use_for_group_by;
1532
 
      group_list= 0;
1533
 
      group= 0;
1534
 
    }
1535
 
    if (select_distinct &&
1536
 
       list_contains_unique_index(join_tab[const_tables].table,
1537
 
                                 find_field_in_item_list,
1538
 
                                 (void *) &fields_list))
1539
 
    {
1540
 
      select_distinct= 0;
1541
 
    }
1542
 
  }
1543
 
  if (group_list || tmp_table_param.sum_func_count)
1544
 
  {
1545
 
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1546
 
      select_distinct=0;
1547
 
  }
1548
 
  else if (select_distinct && tables - const_tables == 1)
1549
 
  {
1550
 
    /*
1551
 
      We are only using one table. In this case we change DISTINCT to a
1552
 
      GROUP BY query if:
1553
 
      - The GROUP BY can be done through indexes (no sort) and the order_st
1554
 
        BY only uses selected fields.
1555
 
        (In this case we can later optimize away GROUP BY and order_st BY)
1556
 
      - We are scanning the whole table without LIMIT
1557
 
        This can happen if:
1558
 
        - We are using CALC_FOUND_ROWS
1559
 
        - We are using an order_st BY that can't be optimized away.
1560
 
 
1561
 
      We don't want to use this optimization when we are using LIMIT
1562
 
      because in this case we can just create a temporary table that
1563
 
      holds LIMIT rows and stop when this table is full.
1564
 
    */
1565
 
    JOIN_TAB *tab= &join_tab[const_tables];
1566
 
    bool all_order_fields_used;
1567
 
    if (order)
1568
 
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1569
 
        &tab->table->keys_in_use_for_order_by);
1570
 
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
1571
 
                                          order, fields_list, all_fields,
1572
 
                                          &all_order_fields_used)))
1573
 
    {
1574
 
      bool skip_group= (skip_sort_order &&
1575
 
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
1576
 
                                &tab->table->keys_in_use_for_group_by) != 0);
1577
 
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1578
 
      if ((skip_group && all_order_fields_used) ||
1579
 
          select_limit == HA_POS_ERROR ||
1580
 
          (order && !skip_sort_order))
1581
 
      {
1582
 
        /*  Change DISTINCT to GROUP BY */
1583
 
        select_distinct= 0;
1584
 
        no_order= !order;
1585
 
        if (all_order_fields_used)
1586
 
        {
1587
 
          if (order && skip_sort_order)
1588
 
          {
1589
 
            /*
1590
 
              Force MySQL to read the table in sorted order to get result in
1591
 
              order_st BY order.
1592
 
            */
1593
 
            tmp_table_param.quick_group=0;
1594
 
          }
1595
 
          order=0;
1596
 
        }
1597
 
        group=1;                                // For end_write_group
1598
 
      }
1599
 
      else
1600
 
        group_list= 0;
1601
 
    }
1602
 
    else if (session->is_fatal_error)                   // End of memory
1603
 
      return(1);
1604
 
  }
1605
 
  simple_group= 0;
1606
 
  {
1607
 
    order_st *old_group_list;
1608
 
    group_list= remove_const(this, (old_group_list= group_list), conds,
1609
 
                             rollup.state == ROLLUP::STATE_NONE,
1610
 
                             &simple_group);
1611
 
    if (session->is_error())
1612
 
    {
1613
 
      error= 1;
1614
 
      return(1);
1615
 
    }
1616
 
    if (old_group_list && !group_list)
1617
 
      select_distinct= 0;
1618
 
  }
1619
 
  if (!group_list && group)
1620
 
  {
1621
 
    order=0;                                    // The output has only one row
1622
 
    simple_order=1;
1623
 
    select_distinct= 0;                       // No need in distinct for 1 row
1624
 
    group_optimized_away= 1;
1625
 
  }
1626
 
 
1627
 
  calc_group_buffer(this, group_list);
1628
 
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1629
 
 
1630
 
  if (test_if_subpart(group_list, order) ||
1631
 
      (!group_list && tmp_table_param.sum_func_count))
1632
 
    order=0;
1633
 
 
1634
 
  // Can't use sort on head table if using row cache
1635
 
  if (full_join)
1636
 
  {
1637
 
    if (group_list)
1638
 
      simple_group=0;
1639
 
    if (order)
1640
 
      simple_order=0;
1641
 
  }
1642
 
 
1643
 
  /*
1644
 
    Check if we need to create a temporary table.
1645
 
    This has to be done if all tables are not already read (const tables)
1646
 
    and one of the following conditions holds:
1647
 
    - We are using DISTINCT (simple distinct's are already optimized away)
1648
 
    - We are using an order_st BY or GROUP BY on fields not in the first table
1649
 
    - We are using different order_st BY and GROUP BY orders
1650
 
    - The user wants us to buffer the result.
1651
 
  */
1652
 
  need_tmp= (const_tables != tables &&
1653
 
             ((select_distinct || !simple_order || !simple_group) ||
1654
 
              (group_list && order) ||
1655
 
              test(select_options & OPTION_BUFFER_RESULT)));
1656
 
 
1657
 
  uint32_t no_jbuf_after= make_join_orderinfo(this);
1658
 
  uint64_t select_opts_for_readinfo=
1659
 
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1660
 
 
1661
 
  sj_tmp_tables= NULL;
1662
 
  if (!select_lex->sj_nests.is_empty())
1663
 
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1664
 
                                    no_jbuf_after);
1665
 
 
1666
 
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1667
 
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1668
 
    return(1);
1669
 
 
1670
 
  /* Create all structures needed for materialized subquery execution. */
1671
 
  if (setup_subquery_materialization())
1672
 
    return(1);
1673
 
 
1674
 
  /*
1675
 
    is this simple IN subquery?
1676
 
  */
1677
 
  if (!group_list && !order &&
1678
 
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1679
 
      tables == 1 && conds &&
1680
 
      !unit->is_union())
1681
 
  {
1682
 
    if (!having)
1683
 
    {
1684
 
      Item *where= conds;
1685
 
      if (join_tab[0].type == JT_EQ_REF &&
1686
 
          join_tab[0].ref.items[0]->name == in_left_expr_name)
1687
 
      {
1688
 
        remove_subq_pushed_predicates(&where);
1689
 
        save_index_subquery_explain_info(join_tab, where);
1690
 
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1691
 
        error= 0;
1692
 
        return(unit->item->
1693
 
                    change_engine(new
1694
 
                                  subselect_uniquesubquery_engine(session,
1695
 
                                                                  join_tab,
1696
 
                                                                  unit->item,
1697
 
                                                                  where)));
1698
 
      }
1699
 
      else if (join_tab[0].type == JT_REF &&
1700
 
               join_tab[0].ref.items[0]->name == in_left_expr_name)
1701
 
      {
1702
 
        remove_subq_pushed_predicates(&where);
1703
 
        save_index_subquery_explain_info(join_tab, where);
1704
 
        join_tab[0].type= JT_INDEX_SUBQUERY;
1705
 
        error= 0;
1706
 
        return(unit->item->
1707
 
                    change_engine(new
1708
 
                                  subselect_indexsubquery_engine(session,
1709
 
                                                                 join_tab,
1710
 
                                                                 unit->item,
1711
 
                                                                 where,
1712
 
                                                                 NULL,
1713
 
                                                                 0)));
1714
 
      }
1715
 
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
1716
 
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
1717
 
               having->name == in_having_cond)
1718
 
    {
1719
 
      join_tab[0].type= JT_INDEX_SUBQUERY;
1720
 
      error= 0;
1721
 
      conds= remove_additional_cond(conds);
1722
 
      save_index_subquery_explain_info(join_tab, conds);
1723
 
      return(unit->item->
1724
 
                  change_engine(new subselect_indexsubquery_engine(session,
1725
 
                                                                   join_tab,
1726
 
                                                                   unit->item,
1727
 
                                                                   conds,
1728
 
                                                                   having,
1729
 
                                                                   1)));
1730
 
    }
1731
 
 
1732
 
  }
1733
 
  /*
1734
 
    Need to tell handlers that to play it safe, it should fetch all
1735
 
    columns of the primary key of the tables: this is because MySQL may
1736
 
    build row pointers for the rows, and for all columns of the primary key
1737
 
    the read set has not necessarily been set by the server code.
1738
 
  */
1739
 
  if (need_tmp || select_distinct || group_list || order)
1740
 
  {
1741
 
    for (uint32_t i = const_tables; i < tables; i++)
1742
 
      join_tab[i].table->prepare_for_position();
1743
 
  }
1744
 
 
1745
 
  if (const_tables != tables)
1746
 
  {
1747
 
    /*
1748
 
      Because filesort always does a full table scan or a quick range scan
1749
 
      we must add the removed reference to the select for the table.
1750
 
      We only need to do this when we have a simple_order or simple_group
1751
 
      as in other cases the join is done before the sort.
1752
 
    */
1753
 
    if ((order || group_list) &&
1754
 
        (join_tab[const_tables].type != JT_ALL) &&
1755
 
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
1756
 
        ((order && simple_order) || (group_list && simple_group)))
1757
 
    {
1758
 
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
1759
 
        return(1);
1760
 
      }
1761
 
    }
1762
 
 
1763
 
    if (!(select_options & SELECT_BIG_RESULT) &&
1764
 
        ((group_list &&
1765
 
          (!simple_group ||
1766
 
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
1767
 
                                    unit->select_limit_cnt, 0,
1768
 
                                    &join_tab[const_tables].table->
1769
 
                                    keys_in_use_for_group_by))) ||
1770
 
         select_distinct) &&
1771
 
        tmp_table_param.quick_group)
1772
 
    {
1773
 
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
1774
 
    }
1775
 
    if (order)
1776
 
    {
1777
 
      /*
1778
 
        Force using of tmp table if sorting by a SP or UDF function due to
1779
 
        their expensive and probably non-deterministic nature.
1780
 
      */
1781
 
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1782
 
      {
1783
 
        Item *item= *tmp_order->item;
1784
 
        if (item->is_expensive())
1785
 
        {
1786
 
          /* Force tmp table without sort */
1787
 
          need_tmp=1; simple_order=simple_group=0;
1788
 
          break;
1789
 
        }
1790
 
      }
1791
 
    }
1792
 
  }
1793
 
 
1794
 
  tmp_having= having;
1795
 
  if (select_options & SELECT_DESCRIBE)
1796
 
  {
1797
 
    error= 0;
1798
 
    return(0);
1799
 
  }
1800
 
  having= 0;
1801
 
 
1802
 
  /*
1803
 
    The loose index scan access method guarantees that all grouping or
1804
 
    duplicate row elimination (for distinct) is already performed
1805
 
    during data retrieval, and that all MIN/MAX functions are already
1806
 
    computed for each group. Thus all MIN/MAX functions should be
1807
 
    treated as regular functions, and there is no need to perform
1808
 
    grouping in the main execution loop.
1809
 
    Notice that currently loose index scan is applicable only for
1810
 
    single table queries, thus it is sufficient to test only the first
1811
 
    join_tab element of the plan for its access method.
1812
 
  */
1813
 
  if (join_tab->is_using_loose_index_scan())
1814
 
    tmp_table_param.precomputed_group_by= true;
1815
 
 
1816
 
  /* Create a tmp table if distinct or if the sort is too complicated */
1817
 
  if (need_tmp)
1818
 
  {
1819
 
    session->set_proc_info("Creating tmp table");
1820
 
 
1821
 
    init_items_ref_array();
1822
 
 
1823
 
    tmp_table_param.hidden_field_count= (all_fields.elements -
1824
 
                                         fields_list.elements);
1825
 
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1826
 
                                                             (order_st*) 0);
1827
 
    /*
1828
 
      Pushing LIMIT to the temporary table creation is not applicable
1829
 
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
1830
 
      there are aggregate functions, because in all these cases we need
1831
 
      all result rows.
1832
 
    */
1833
 
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1834
 
                             !tmp_group &&
1835
 
                             !session->lex->current_select->with_sum_func) ?
1836
 
                            select_limit : HA_POS_ERROR;
1837
 
 
1838
 
    if (!(exec_tmp_table1=
1839
 
          create_tmp_table(session, &tmp_table_param, all_fields,
1840
 
                           tmp_group,
1841
 
                           group_list ? 0 : select_distinct,
1842
 
                           group_list && simple_group,
1843
 
                           select_options,
1844
 
                           tmp_rows_limit,
1845
 
                           (char *) "")))
1846
 
                {
1847
 
      return(1);
1848
 
    }
1849
 
 
1850
 
    /*
1851
 
      We don't have to store rows in temp table that doesn't match HAVING if:
1852
 
      - we are sorting the table and writing complete group rows to the
1853
 
        temp table.
1854
 
      - We are using DISTINCT without resolving the distinct as a GROUP BY
1855
 
        on all columns.
1856
 
 
1857
 
      If having is not handled here, it will be checked before the row
1858
 
      is sent to the client.
1859
 
    */
1860
 
    if (tmp_having &&
1861
 
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1862
 
      having= tmp_having;
1863
 
 
1864
 
    /* if group or order on first table, sort first */
1865
 
    if (group_list && simple_group)
1866
 
    {
1867
 
      session->set_proc_info("Sorting for group");
1868
 
      if (create_sort_index(session, this, group_list,
1869
 
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1870
 
          alloc_group_fields(this, group_list) ||
1871
 
          make_sum_func_list(all_fields, fields_list, 1) ||
1872
 
          setup_sum_funcs(session, sum_funcs))
1873
 
      {
1874
 
        return(1);
1875
 
      }
1876
 
      group_list=0;
1877
 
    }
1878
 
    else
1879
 
    {
1880
 
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1881
 
          setup_sum_funcs(session, sum_funcs))
1882
 
      {
1883
 
        return(1);
1884
 
      }
1885
 
 
1886
 
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1887
 
      {
1888
 
        session->set_proc_info("Sorting for order");
1889
 
        if (create_sort_index(session, this, order,
1890
 
                              HA_POS_ERROR, HA_POS_ERROR, true))
1891
 
        {
1892
 
          return(1);
1893
 
        }
1894
 
        order=0;
1895
 
      }
1896
 
    }
1897
 
 
1898
 
    /*
1899
 
      Optimize distinct when used on some of the tables
1900
 
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1901
 
      In this case we can stop scanning t2 when we have found one t1.a
1902
 
    */
1903
 
 
1904
 
    if (exec_tmp_table1->distinct)
1905
 
    {
1906
 
      table_map used_tables= session->used_tables;
1907
 
      JOIN_TAB *last_join_tab= join_tab+tables-1;
1908
 
      do
1909
 
      {
1910
 
        if (used_tables & last_join_tab->table->map)
1911
 
          break;
1912
 
        last_join_tab->not_used_in_distinct=1;
1913
 
      } while (last_join_tab-- != join_tab);
1914
 
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1915
 
      if (order && skip_sort_order)
1916
 
      {
1917
 
        /* Should always succeed */
1918
 
        if (test_if_skip_sort_order(&join_tab[const_tables],
1919
 
                                    order, unit->select_limit_cnt, 0,
1920
 
                                    &join_tab[const_tables].table->
1921
 
                                      keys_in_use_for_order_by))
1922
 
          order=0;
1923
 
      }
1924
 
    }
1925
 
 
1926
 
    /*
1927
 
      If this join belongs to an uncacheable subquery save
1928
 
      the original join
1929
 
    */
1930
 
    if (select_lex->uncacheable && !is_top_level_join() &&
1931
 
        init_save_join_tab())
1932
 
      return(-1);                         /* purecov: inspected */
1933
 
  }
1934
 
 
1935
 
  error= 0;
1936
 
  return(0);
1937
 
}
1938
 
 
1939
 
 
1940
 
/**
1941
 
  Restore values in temporary join.
1942
 
*/
1943
 
void JOIN::restore_tmp()
1944
 
{
1945
 
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1946
 
}
1947
 
 
1948
 
 
1949
 
int
1950
 
JOIN::reinit()
1951
 
{
1952
 
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1953
 
                                    select_lex->offset_limit->val_uint() :
1954
 
                                    0UL);
1955
 
 
1956
 
  first_record= 0;
1957
 
 
1958
 
  if (exec_tmp_table1)
1959
 
  {
1960
 
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1961
 
    exec_tmp_table1->file->ha_delete_all_rows();
1962
 
    free_io_cache(exec_tmp_table1);
1963
 
    filesort_free_buffers(exec_tmp_table1,0);
1964
 
  }
1965
 
  if (exec_tmp_table2)
1966
 
  {
1967
 
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1968
 
    exec_tmp_table2->file->ha_delete_all_rows();
1969
 
    free_io_cache(exec_tmp_table2);
1970
 
    filesort_free_buffers(exec_tmp_table2,0);
1971
 
  }
1972
 
  if (items0)
1973
 
    set_items_ref_array(items0);
1974
 
 
1975
 
  if (join_tab_save)
1976
 
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1977
 
 
1978
 
  if (tmp_join)
1979
 
    restore_tmp();
1980
 
 
1981
 
  /* Reset of sum functions */
1982
 
  if (sum_funcs)
1983
 
  {
1984
 
    Item_sum *func, **func_ptr= sum_funcs;
1985
 
    while ((func= *(func_ptr++)))
1986
 
      func->clear();
1987
 
  }
1988
 
 
1989
 
  return(0);
1990
 
}
1991
 
 
1992
 
/**
1993
 
   @brief Save the original join layout
1994
 
 
1995
 
   @details Saves the original join layout so it can be reused in
1996
 
   re-execution and for EXPLAIN.
1997
 
 
1998
 
   @return Operation status
1999
 
   @retval 0      success.
2000
 
   @retval 1      error occurred.
2001
 
*/
2002
 
 
2003
 
bool
2004
 
JOIN::init_save_join_tab()
2005
 
{
2006
 
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
2007
 
    return 1;                                  /* purecov: inspected */
2008
 
  error= 0;                                    // Ensure that tmp_join.error= 0
2009
 
  restore_tmp();
2010
 
  return 0;
2011
 
}
2012
 
 
2013
 
 
2014
 
bool
2015
 
JOIN::save_join_tab()
2016
 
{
2017
 
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
2018
 
  {
2019
 
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
2020
 
                                                sizeof(JOIN_TAB) * tables)))
2021
 
      return 1;
2022
 
  }
2023
 
  return 0;
2024
 
}
2025
 
 
2026
 
 
2027
 
/**
2028
 
  Exec select.
2029
 
 
2030
 
  @todo
2031
 
    Note, that create_sort_index calls test_if_skip_sort_order and may
2032
 
    finally replace sorting with index scan if there is a LIMIT clause in
2033
 
    the query.  It's never shown in EXPLAIN!
2034
 
 
2035
 
  @todo
2036
 
    When can we have here session->net.report_error not zero?
2037
 
*/
2038
 
void
2039
 
JOIN::exec()
2040
 
{
2041
 
  List<Item> *columns_list= &fields_list;
2042
 
  int      tmp_error;
2043
 
 
2044
 
  session->set_proc_info("executing");
2045
 
  error= 0;
2046
 
  (void) result->prepare2(); // Currently, this cannot fail.
2047
 
 
2048
 
  if (!tables_list && (tables || !select_lex->with_sum_func))
2049
 
  {                                           // Only test of functions
2050
 
    if (select_options & SELECT_DESCRIBE)
2051
 
      select_describe(this, false, false, false,
2052
 
                      (zero_result_cause?zero_result_cause:"No tables used"));
2053
 
    else
2054
 
    {
2055
 
      result->send_fields(*columns_list,
2056
 
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2057
 
      /*
2058
 
        We have to test for 'conds' here as the WHERE may not be constant
2059
 
        even if we don't have any tables for prepared statements or if
2060
 
        conds uses something like 'rand()'.
2061
 
      */
2062
 
      if (cond_value != Item::COND_FALSE &&
2063
 
          (!conds || conds->val_int()) &&
2064
 
          (!having || having->val_int()))
2065
 
      {
2066
 
        if (do_send_rows && result->send_data(fields_list))
2067
 
          error= 1;
2068
 
        else
2069
 
        {
2070
 
          error= (int) result->send_eof();
2071
 
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2072
 
                         session->sent_row_count);
2073
 
        }
2074
 
      }
2075
 
      else
2076
 
      {
2077
 
        error=(int) result->send_eof();
2078
 
        send_records= 0;
2079
 
      }
2080
 
    }
2081
 
    /* Single select (without union) always returns 0 or 1 row */
2082
 
    session->limit_found_rows= send_records;
2083
 
    session->examined_row_count= 0;
2084
 
    return;
2085
 
  }
2086
 
  /*
2087
 
    Don't reset the found rows count if there're no tables as
2088
 
    FOUND_ROWS() may be called. Never reset the examined row count here.
2089
 
    It must be accumulated from all join iterations of all join parts.
2090
 
  */
2091
 
  if (tables)
2092
 
    session->limit_found_rows= 0;
2093
 
 
2094
 
  if (zero_result_cause)
2095
 
  {
2096
 
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
2097
 
                            *columns_list,
2098
 
                            send_row_on_empty_set(),
2099
 
                            select_options,
2100
 
                            zero_result_cause,
2101
 
                            having);
2102
 
    return;
2103
 
  }
2104
 
 
2105
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2106
 
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2107
 
    return;
2108
 
 
2109
 
  if (select_options & SELECT_DESCRIBE)
2110
 
  {
2111
 
    /*
2112
 
      Check if we managed to optimize order_st BY away and don't use temporary
2113
 
      table to resolve order_st BY: in that case, we only may need to do
2114
 
      filesort for GROUP BY.
2115
 
    */
2116
 
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2117
 
    {
2118
 
      /*
2119
 
        Reset 'order' to 'group_list' and reinit variables describing
2120
 
        'order'
2121
 
      */
2122
 
      order= group_list;
2123
 
      simple_order= simple_group;
2124
 
      skip_sort_order= 0;
2125
 
    }
2126
 
    if (order &&
2127
 
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2128
 
        (const_tables == tables ||
2129
 
         ((simple_order || skip_sort_order) &&
2130
 
          test_if_skip_sort_order(&join_tab[const_tables], order,
2131
 
                                  select_limit, 0,
2132
 
                                  &join_tab[const_tables].table->
2133
 
                                    keys_in_use_for_query))))
2134
 
      order=0;
2135
 
    having= tmp_having;
2136
 
    select_describe(this, need_tmp,
2137
 
                    order != 0 && !skip_sort_order,
2138
 
                    select_distinct,
2139
 
                    !tables ? "No tables used" : NULL);
2140
 
    return;
2141
 
  }
2142
 
 
2143
 
  JOIN *curr_join= this;
2144
 
  List<Item> *curr_all_fields= &all_fields;
2145
 
  List<Item> *curr_fields_list= &fields_list;
2146
 
  Table *curr_tmp_table= 0;
2147
 
  /*
2148
 
    Initialize examined rows here because the values from all join parts
2149
 
    must be accumulated in examined_row_count. Hence every join
2150
 
    iteration must count from zero.
2151
 
  */
2152
 
  curr_join->examined_rows= 0;
2153
 
 
2154
 
  /* Create a tmp table if distinct or if the sort is too complicated */
2155
 
  if (need_tmp)
2156
 
  {
2157
 
    if (tmp_join)
2158
 
    {
2159
 
      /*
2160
 
        We are in a non cacheable sub query. Get the saved join structure
2161
 
        after optimization.
2162
 
        (curr_join may have been modified during last exection and we need
2163
 
        to reset it)
2164
 
      */
2165
 
      curr_join= tmp_join;
2166
 
    }
2167
 
    curr_tmp_table= exec_tmp_table1;
2168
 
 
2169
 
    /* Copy data to the temporary table */
2170
 
    session->set_proc_info("Copying to tmp table");
2171
 
    if (!curr_join->sort_and_group &&
2172
 
        curr_join->const_tables != curr_join->tables)
2173
 
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2174
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2175
 
    {
2176
 
      error= tmp_error;
2177
 
      return;
2178
 
    }
2179
 
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2180
 
 
2181
 
    if (curr_join->having)
2182
 
      curr_join->having= curr_join->tmp_having= 0; // Allready done
2183
 
 
2184
 
    /* Change sum_fields reference to calculated fields in tmp_table */
2185
 
    curr_join->all_fields= *curr_all_fields;
2186
 
    if (!items1)
2187
 
    {
2188
 
      items1= items0 + all_fields.elements;
2189
 
      if (sort_and_group || curr_tmp_table->group)
2190
 
      {
2191
 
        if (change_to_use_tmp_fields(session, items1,
2192
 
                                     tmp_fields_list1, tmp_all_fields1,
2193
 
                                     fields_list.elements, all_fields))
2194
 
          return;
2195
 
      }
2196
 
      else
2197
 
      {
2198
 
        if (change_refs_to_tmp_fields(session, items1,
2199
 
                                      tmp_fields_list1, tmp_all_fields1,
2200
 
                                      fields_list.elements, all_fields))
2201
 
          return;
2202
 
      }
2203
 
      curr_join->tmp_all_fields1= tmp_all_fields1;
2204
 
      curr_join->tmp_fields_list1= tmp_fields_list1;
2205
 
      curr_join->items1= items1;
2206
 
    }
2207
 
    curr_all_fields= &tmp_all_fields1;
2208
 
    curr_fields_list= &tmp_fields_list1;
2209
 
    curr_join->set_items_ref_array(items1);
2210
 
 
2211
 
    if (sort_and_group || curr_tmp_table->group)
2212
 
    {
2213
 
      curr_join->tmp_table_param.field_count+=
2214
 
        curr_join->tmp_table_param.sum_func_count+
2215
 
        curr_join->tmp_table_param.func_count;
2216
 
      curr_join->tmp_table_param.sum_func_count=
2217
 
        curr_join->tmp_table_param.func_count= 0;
2218
 
    }
2219
 
    else
2220
 
    {
2221
 
      curr_join->tmp_table_param.field_count+=
2222
 
        curr_join->tmp_table_param.func_count;
2223
 
      curr_join->tmp_table_param.func_count= 0;
2224
 
    }
2225
 
 
2226
 
    if (curr_tmp_table->group)
2227
 
    {                                           // Already grouped
2228
 
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2229
 
        curr_join->order= curr_join->group_list;  /* order by group */
2230
 
      curr_join->group_list= 0;
2231
 
    }
2232
 
 
2233
 
    /*
2234
 
      If we have different sort & group then we must sort the data by group
2235
 
      and copy it to another tmp table
2236
 
      This code is also used if we are using distinct something
2237
 
      we haven't been able to store in the temporary table yet
2238
 
      like SEC_TO_TIME(SUM(...)).
2239
 
    */
2240
 
 
2241
 
    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))
2242
 
    {                                   /* Must copy to another table */
2243
 
      /* Free first data from old join */
2244
 
      curr_join->join_free();
2245
 
      if (make_simple_join(curr_join, curr_tmp_table))
2246
 
        return;
2247
 
      calc_group_buffer(curr_join, group_list);
2248
 
      count_field_types(select_lex, &curr_join->tmp_table_param,
2249
 
                        curr_join->tmp_all_fields1,
2250
 
                        curr_join->select_distinct && !curr_join->group_list);
2251
 
      curr_join->tmp_table_param.hidden_field_count=
2252
 
        (curr_join->tmp_all_fields1.elements-
2253
 
         curr_join->tmp_fields_list1.elements);
2254
 
 
2255
 
 
2256
 
      if (exec_tmp_table2)
2257
 
        curr_tmp_table= exec_tmp_table2;
2258
 
      else
2259
 
      {
2260
 
        /* group data to new table */
2261
 
 
2262
 
        /*
2263
 
          If the access method is loose index scan then all MIN/MAX
2264
 
          functions are precomputed, and should be treated as regular
2265
 
          functions. See extended comment in JOIN::exec.
2266
 
        */
2267
 
        if (curr_join->join_tab->is_using_loose_index_scan())
2268
 
          curr_join->tmp_table_param.precomputed_group_by= true;
2269
 
 
2270
 
        if (!(curr_tmp_table=
2271
 
              exec_tmp_table2= create_tmp_table(session,
2272
 
                                                &curr_join->tmp_table_param,
2273
 
                                                *curr_all_fields,
2274
 
                                                (order_st*) 0,
2275
 
                                                curr_join->select_distinct &&
2276
 
                                                !curr_join->group_list,
2277
 
                                                1, curr_join->select_options,
2278
 
                                                HA_POS_ERROR,
2279
 
                                                (char *) "")))
2280
 
          return;
2281
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
2282
 
      }
2283
 
      if (curr_join->group_list)
2284
 
      {
2285
 
        session->set_proc_info("Creating sort index");
2286
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2287
 
        {
2288
 
          return;
2289
 
        }
2290
 
        if (create_sort_index(session, curr_join, curr_join->group_list,
2291
 
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2292
 
            make_group_fields(this, curr_join))
2293
 
        {
2294
 
          return;
2295
 
        }
2296
 
        sortorder= curr_join->sortorder;
2297
 
      }
2298
 
 
2299
 
      session->set_proc_info("Copying to group table");
2300
 
      tmp_error= -1;
2301
 
      if (curr_join != this)
2302
 
      {
2303
 
        if (sum_funcs2)
2304
 
        {
2305
 
          curr_join->sum_funcs= sum_funcs2;
2306
 
          curr_join->sum_funcs_end= sum_funcs_end2;
2307
 
        }
2308
 
        else
2309
 
        {
2310
 
          curr_join->alloc_func_list();
2311
 
          sum_funcs2= curr_join->sum_funcs;
2312
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2313
 
        }
2314
 
      }
2315
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2316
 
                                        1, true))
2317
 
        return;
2318
 
      curr_join->group_list= 0;
2319
 
      if (!curr_join->sort_and_group &&
2320
 
          curr_join->const_tables != curr_join->tables)
2321
 
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2322
 
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
2323
 
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2324
 
      {
2325
 
        error= tmp_error;
2326
 
        return;
2327
 
      }
2328
 
      end_read_record(&curr_join->join_tab->read_record);
2329
 
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2330
 
      curr_join->join_tab[0].table= 0;           // Table is freed
2331
 
 
2332
 
      // No sum funcs anymore
2333
 
      if (!items2)
2334
 
      {
2335
 
        items2= items1 + all_fields.elements;
2336
 
        if (change_to_use_tmp_fields(session, items2,
2337
 
                                     tmp_fields_list2, tmp_all_fields2,
2338
 
                                     fields_list.elements, tmp_all_fields1))
2339
 
          return;
2340
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2341
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
2342
 
      }
2343
 
      curr_fields_list= &curr_join->tmp_fields_list2;
2344
 
      curr_all_fields= &curr_join->tmp_all_fields2;
2345
 
      curr_join->set_items_ref_array(items2);
2346
 
      curr_join->tmp_table_param.field_count+=
2347
 
        curr_join->tmp_table_param.sum_func_count;
2348
 
      curr_join->tmp_table_param.sum_func_count= 0;
2349
 
    }
2350
 
    if (curr_tmp_table->distinct)
2351
 
      curr_join->select_distinct=0;             /* Each row is unique */
2352
 
 
2353
 
    curr_join->join_free();                     /* Free quick selects */
2354
 
    if (curr_join->select_distinct && ! curr_join->group_list)
2355
 
    {
2356
 
      session->set_proc_info("Removing duplicates");
2357
 
      if (curr_join->tmp_having)
2358
 
        curr_join->tmp_having->update_used_tables();
2359
 
      if (remove_duplicates(curr_join, curr_tmp_table,
2360
 
                            *curr_fields_list, curr_join->tmp_having))
2361
 
        return;
2362
 
      curr_join->tmp_having=0;
2363
 
      curr_join->select_distinct=0;
2364
 
    }
2365
 
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2366
 
    if (make_simple_join(curr_join, curr_tmp_table))
2367
 
      return;
2368
 
    calc_group_buffer(curr_join, curr_join->group_list);
2369
 
    count_field_types(select_lex, &curr_join->tmp_table_param,
2370
 
                      *curr_all_fields, 0);
2371
 
 
2372
 
  }
2373
 
 
2374
 
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2375
 
  {
2376
 
    if (make_group_fields(this, curr_join))
2377
 
    {
2378
 
      return;
2379
 
    }
2380
 
    if (!items3)
2381
 
    {
2382
 
      if (!items0)
2383
 
        init_items_ref_array();
2384
 
      items3= ref_pointer_array + (all_fields.elements*4);
2385
 
      setup_copy_fields(session, &curr_join->tmp_table_param,
2386
 
                        items3, tmp_fields_list3, tmp_all_fields3,
2387
 
                        curr_fields_list->elements, *curr_all_fields);
2388
 
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2389
 
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2390
 
      tmp_table_param.save_copy_field_end=
2391
 
        curr_join->tmp_table_param.copy_field_end;
2392
 
      curr_join->tmp_all_fields3= tmp_all_fields3;
2393
 
      curr_join->tmp_fields_list3= tmp_fields_list3;
2394
 
    }
2395
 
    else
2396
 
    {
2397
 
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2398
 
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2399
 
      curr_join->tmp_table_param.copy_field_end=
2400
 
        tmp_table_param.save_copy_field_end;
2401
 
    }
2402
 
    curr_fields_list= &tmp_fields_list3;
2403
 
    curr_all_fields= &tmp_all_fields3;
2404
 
    curr_join->set_items_ref_array(items3);
2405
 
 
2406
 
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2407
 
                                      1, true) ||
2408
 
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
2409
 
        session->is_fatal_error)
2410
 
      return;
2411
 
  }
2412
 
  if (curr_join->group_list || curr_join->order)
2413
 
  {
2414
 
    session->set_proc_info("Sorting result");
2415
 
    /* If we have already done the group, add HAVING to sorted table */
2416
 
    if (curr_join->tmp_having && ! curr_join->group_list &&
2417
 
        ! curr_join->sort_and_group)
2418
 
    {
2419
 
      // Some tables may have been const
2420
 
      curr_join->tmp_having->update_used_tables();
2421
 
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2422
 
      table_map used_tables= (curr_join->const_table_map |
2423
 
                              curr_table->table->map);
2424
 
 
2425
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2426
 
                                                 used_tables,
2427
 
                                                 used_tables, 0);
2428
 
      if (sort_table_cond)
2429
 
      {
2430
 
        if (!curr_table->select)
2431
 
          if (!(curr_table->select= new SQL_SELECT))
2432
 
            return;
2433
 
        if (!curr_table->select->cond)
2434
 
          curr_table->select->cond= sort_table_cond;
2435
 
        else                                    // This should never happen
2436
 
        {
2437
 
          if (!(curr_table->select->cond=
2438
 
                new Item_cond_and(curr_table->select->cond,
2439
 
                                  sort_table_cond)))
2440
 
            return;
2441
 
          /*
2442
 
            Item_cond_and do not need fix_fields for execution, its parameters
2443
 
            are fixed or do not need fix_fields, too
2444
 
          */
2445
 
          curr_table->select->cond->quick_fix_field();
2446
 
        }
2447
 
        curr_table->select_cond= curr_table->select->cond;
2448
 
        curr_table->select_cond->top_level_item();
2449
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2450
 
                                                   ~ (table_map) 0,
2451
 
                                                   ~used_tables, 0);
2452
 
      }
2453
 
    }
2454
 
    {
2455
 
      if (group)
2456
 
        curr_join->select_limit= HA_POS_ERROR;
2457
 
      else
2458
 
      {
2459
 
        /*
2460
 
          We can abort sorting after session->select_limit rows if we there is no
2461
 
          WHERE clause for any tables after the sorted one.
2462
 
        */
2463
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2464
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2465
 
        for (; curr_table < end_table ; curr_table++)
2466
 
        {
2467
 
          /*
2468
 
            table->keyuse is set in the case there was an original WHERE clause
2469
 
            on the table that was optimized away.
2470
 
          */
2471
 
          if (curr_table->select_cond ||
2472
 
              (curr_table->keyuse && !curr_table->first_inner))
2473
 
          {
2474
 
            /* We have to sort all rows */
2475
 
            curr_join->select_limit= HA_POS_ERROR;
2476
 
            break;
2477
 
          }
2478
 
        }
2479
 
      }
2480
 
      if (curr_join->join_tab == join_tab && save_join_tab())
2481
 
      {
2482
 
        return;
2483
 
      }
2484
 
      /*
2485
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
2486
 
        chose FILESORT to be faster than INDEX SCAN or there is no
2487
 
        suitable index present.
2488
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2489
 
        finally replace sorting with index scan if there is a LIMIT clause in
2490
 
        the query. XXX: it's never shown in EXPLAIN!
2491
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2492
 
      */
2493
 
      if (create_sort_index(session, curr_join,
2494
 
                            curr_join->group_list ?
2495
 
                            curr_join->group_list : curr_join->order,
2496
 
                            curr_join->select_limit,
2497
 
                            (select_options & OPTION_FOUND_ROWS ?
2498
 
                             HA_POS_ERROR : unit->select_limit_cnt),
2499
 
                            curr_join->group_list ? true : false))
2500
 
        return;
2501
 
      sortorder= curr_join->sortorder;
2502
 
      if (curr_join->const_tables != curr_join->tables &&
2503
 
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2504
 
      {
2505
 
        /*
2506
 
          If no IO cache exists for the first table then we are using an
2507
 
          INDEX SCAN and no filesort. Thus we should not remove the sorted
2508
 
          attribute on the INDEX SCAN.
2509
 
        */
2510
 
        skip_sort_order= 1;
2511
 
      }
2512
 
    }
2513
 
  }
2514
 
  /* XXX: When can we have here session->is_error() not zero? */
2515
 
  if (session->is_error())
2516
 
  {
2517
 
    error= session->is_error();
2518
 
    return;
2519
 
  }
2520
 
  curr_join->having= curr_join->tmp_having;
2521
 
  curr_join->fields= curr_fields_list;
2522
 
 
2523
 
  {
2524
 
    session->set_proc_info("Sending data");
2525
 
    result->send_fields(*curr_fields_list,
2526
 
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2527
 
    error= do_select(curr_join, curr_fields_list, NULL);
2528
 
    session->limit_found_rows= curr_join->send_records;
2529
 
  }
2530
 
 
2531
 
  /* Accumulate the counts from all join iterations of all join parts. */
2532
 
  session->examined_row_count+= curr_join->examined_rows;
2533
 
 
2534
 
  /*
2535
 
    With EXPLAIN EXTENDED we have to restore original ref_array
2536
 
    for a derived table which is always materialized.
2537
 
    Otherwise we would not be able to print the query  correctly.
2538
 
  */
2539
 
  if (items0 &&
2540
 
      (session->lex->describe & DESCRIBE_EXTENDED) &&
2541
 
      select_lex->linkage == DERIVED_TABLE_TYPE)
2542
 
    set_items_ref_array(items0);
2543
 
 
2544
 
  return;
2545
 
}
2546
 
 
2547
 
 
2548
 
/**
2549
 
  Clean up join.
2550
 
 
2551
 
  @return
2552
 
    Return error that hold JOIN.
2553
 
*/
2554
 
 
2555
 
int
2556
 
JOIN::destroy()
2557
 
{
2558
 
  select_lex->join= 0;
2559
 
 
2560
 
  if (tmp_join)
2561
 
  {
2562
 
    if (join_tab != tmp_join->join_tab)
2563
 
    {
2564
 
      JOIN_TAB *tab, *end;
2565
 
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2566
 
        tab->cleanup();
2567
 
    }
2568
 
    tmp_join->tmp_join= 0;
2569
 
    tmp_table_param.copy_field=0;
2570
 
    return(tmp_join->destroy());
2571
 
  }
2572
 
  cond_equal= 0;
2573
 
 
2574
 
  cleanup(1);
2575
 
  if (exec_tmp_table1)
2576
 
    exec_tmp_table1->free_tmp_table(session);
2577
 
  if (exec_tmp_table2)
2578
 
    exec_tmp_table2->free_tmp_table(session);
2579
 
  delete select;
2580
 
  delete_dynamic(&keyuse);
2581
 
  return(error);
2582
 
}
2583
 
 
2584
 
 
2585
 
 
2586
326
/**
2587
327
  An entry point to single-unit select (a select without UNION).
2588
328
 
2589
 
  @param session                  thread handler
 
329
  @param session                  thread Cursor
2590
330
  @param rref_pointer_array   a reference to ref_pointer_array of
2591
331
                              the top-level select_lex for this query
2592
332
  @param tables               list of all tables used in this query.
2602
342
                              for a, b and c in this list.
2603
343
  @param conds                top level item of an expression representing
2604
344
                              WHERE clause of the top level select
2605
 
  @param og_num               total number of order_st BY and GROUP BY clauses
 
345
  @param og_num               total number of ORDER BY and GROUP BY clauses
2606
346
                              arguments
2607
 
  @param order                linked list of order_st BY agruments
 
347
  @param order                linked list of ORDER BY agruments
2608
348
  @param group                linked list of GROUP BY arguments
2609
349
  @param having               top level item of HAVING expression
2610
 
  @param proc_param           list of PROCEDUREs
2611
350
  @param select_options       select options (BIG_RESULT, etc)
2612
351
  @param result               an instance of result set handling class.
2613
352
                              This object is responsible for send result
2614
353
                              set rows to the client or inserting them
2615
354
                              into a table.
2616
 
  @param select_lex           the only SELECT_LEX of this query
 
355
  @param select_lex           the only Select_Lex of this query
2617
356
  @param unit                 top-level UNIT of this query
2618
357
                              UNIT is an artificial object created by the
2619
358
                              parser for every SELECT clause.
2626
365
  @retval
2627
366
    true   an error
2628
367
*/
2629
 
 
2630
 
bool
2631
 
mysql_select(Session *session, Item ***rref_pointer_array,
2632
 
             TableList *tables, uint32_t wild_num, List<Item> &fields,
2633
 
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
2634
 
             Item *having, order_st *proc_param, uint64_t select_options,
2635
 
             select_result *result, SELECT_LEX_UNIT *unit,
2636
 
             SELECT_LEX *select_lex)
 
368
bool select_query(Session *session,
 
369
                  Item ***rref_pointer_array,
 
370
                  TableList *tables,
 
371
                  uint32_t wild_num,
 
372
                  List<Item> &fields,
 
373
                  COND *conds,
 
374
                  uint32_t og_num,
 
375
                  Order *order,
 
376
                  Order *group,
 
377
                  Item *having,
 
378
                  uint64_t select_options,
 
379
                  select_result *result,
 
380
                  Select_Lex_Unit *unit,
 
381
                  Select_Lex *select_lex)
2637
382
{
2638
383
  bool err;
2639
384
  bool free_join= 1;
2640
385
 
2641
386
  select_lex->context.resolve_in_select_list= true;
2642
 
  JOIN *join;
 
387
  Join *join;
2643
388
  if (select_lex->join != 0)
2644
389
  {
2645
390
    join= select_lex->join;
2648
393
      creation
2649
394
    */
2650
395
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2651
 
        (select_options & SELECT_DESCRIBE))
 
396
        (select_options & SELECT_DESCRIBE))
2652
397
    {
2653
398
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2654
399
      {
2655
 
        //here is EXPLAIN of subselect or derived table
2656
 
        if (join->change_result(result))
2657
 
        {
2658
 
          return(true);
2659
 
        }
 
400
        //here is EXPLAIN of subselect or derived table
 
401
        if (join->change_result(result))
 
402
        {
 
403
          return true;
 
404
        }
2660
405
      }
2661
406
      else
2662
407
      {
2663
408
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2664
 
                               conds, og_num, order, group, having, proc_param,
2665
 
                               select_lex, unit)))
2666
 
        {
2667
 
          goto err;
2668
 
        }
 
409
                               conds, og_num, order, group, having, select_lex, unit)))
 
410
        {
 
411
          goto err;
 
412
        }
2669
413
      }
2670
414
    }
2671
415
    free_join= 0;
2673
417
  }
2674
418
  else
2675
419
  {
2676
 
    if (!(join= new JOIN(session, fields, select_options, result)))
2677
 
        return(true);
 
420
    join= new Join(session, fields, select_options, result);
2678
421
    session->set_proc_info("init");
2679
422
    session->used_tables=0;                         // Updated by setup_fields
2680
 
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2681
 
                           conds, og_num, order, group, having, proc_param,
2682
 
                           select_lex, unit)) == true)
 
423
    if ((err= join->prepare(rref_pointer_array, tables, wild_num, conds, og_num, order, group, having, select_lex, unit)))
2683
424
    {
2684
425
      goto err;
2685
426
    }
2686
427
  }
2687
428
 
2688
 
  if (join->flatten_subqueries())
2689
 
  {
2690
 
    err= 1;
2691
 
    goto err;
2692
 
  }
2693
 
 
2694
 
  if ((err= join->optimize()))
2695
 
  {
2696
 
    goto err;                                   // 1
2697
 
  }
2698
 
 
2699
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
429
  err= join->optimize();
 
430
  if (err)
 
431
  {
 
432
    goto err; // 1
 
433
  }
 
434
 
 
435
  if (session->lex().describe & DESCRIBE_EXTENDED)
2700
436
  {
2701
437
    join->conds_history= join->conds;
2702
438
    join->having_history= (join->having?join->having:join->tmp_having);
2703
439
  }
2704
440
 
2705
441
  if (session->is_error())
 
442
  {
2706
443
    goto err;
 
444
  }
2707
445
 
2708
446
  join->exec();
2709
447
 
2710
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
448
  if (session->lex().describe & DESCRIBE_EXTENDED)
2711
449
  {
2712
450
    select_lex->where= join->conds_history;
2713
451
    select_lex->having= join->having_history;
2723
461
  return(join->error);
2724
462
}
2725
463
 
2726
 
 
2727
 
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
2728
 
                          Item_in_subselect* const *el2)
2729
 
{
2730
 
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
2731
 
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2732
 
}
2733
 
 
2734
 
 
2735
 
inline Item * and_items(Item* cond, Item *item)
 
464
inline Item *and_items(Item* cond, Item *item)
2736
465
{
2737
466
  return (cond? (new Item_cond_and(cond, item)) : item);
2738
467
}
2739
468
 
2740
 
 
2741
 
static TableList *alloc_join_nest(Session *session)
2742
 
{
2743
 
  TableList *tbl;
2744
 
  if (!(tbl= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
2745
 
                                       sizeof(nested_join_st))))
2746
 
    return NULL;
2747
 
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl +
2748
 
                                    ALIGN_SIZE(sizeof(TableList)));
2749
 
  return tbl;
2750
 
}
2751
 
 
2752
 
 
2753
 
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
2754
 
{
2755
 
  List_iterator<TableList> it(*tlist);
2756
 
  TableList *table;
2757
 
  while ((table= it++))
2758
 
  {
2759
 
    if (table->on_expr)
2760
 
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
2761
 
    if (table->nested_join)
2762
 
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
2763
 
  }
2764
 
}
2765
 
 
2766
 
 
2767
 
/*
2768
 
  Convert a subquery predicate into a TableList semi-join nest
2769
 
 
2770
 
  SYNOPSIS
2771
 
    convert_subq_to_sj()
2772
 
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON
2773
 
                    clause
2774
 
       subq_pred    Subquery predicate to be converted
2775
 
 
2776
 
  DESCRIPTION
2777
 
    Convert a subquery predicate into a TableList semi-join nest. All the
2778
 
    prerequisites are already checked, so the conversion is always successfull.
2779
 
 
2780
 
    Prepared Statements: the transformation is permanent:
2781
 
     - Changes in TableList structures are naturally permanent
2782
 
     - Item tree changes are performed on statement MEM_ROOT:
2783
 
        = we activate statement MEM_ROOT
2784
 
        = this function is called before the first fix_prepare_information
2785
 
          call.
2786
 
 
2787
 
    This is intended because the criteria for subquery-to-sj conversion remain
2788
 
    constant for the lifetime of the Prepared Statement.
2789
 
 
2790
 
  RETURN
2791
 
    false  OK
2792
 
    true   Out of memory error
2793
 
*/
2794
 
 
2795
 
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2796
 
{
2797
 
  SELECT_LEX *parent_lex= parent_join->select_lex;
2798
 
  TableList *emb_tbl_nest= NULL;
2799
 
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
2800
 
  Session *session= parent_join->session;
2801
 
 
2802
 
  /*
2803
 
    1. Find out where to put the predicate into.
2804
 
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2805
 
  */
2806
 
  if ((void*)subq_pred->expr_join_nest != (void*)1)
2807
 
  {
2808
 
    if (subq_pred->expr_join_nest->nested_join)
2809
 
    {
2810
 
      /*
2811
 
        We're dealing with
2812
 
 
2813
 
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
2814
 
 
2815
 
        The sj-nest will be inserted into the brackets nest.
2816
 
      */
2817
 
      emb_tbl_nest=  subq_pred->expr_join_nest;
2818
 
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
2819
 
    }
2820
 
    else if (!subq_pred->expr_join_nest->outer_join)
2821
 
    {
2822
 
      /*
2823
 
        We're dealing with
2824
 
 
2825
 
          ... INNER JOIN tblX ON (subquery AND whatever) ...
2826
 
 
2827
 
        The sj-nest will be tblX's "sibling", i.e. another child of its
2828
 
        parent. This is ok because tblX is joined as an inner join.
2829
 
      */
2830
 
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2831
 
      if (emb_tbl_nest)
2832
 
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
2833
 
    }
2834
 
    else if (!subq_pred->expr_join_nest->nested_join)
2835
 
    {
2836
 
      TableList *outer_tbl= subq_pred->expr_join_nest;
2837
 
      TableList *wrap_nest;
2838
 
      /*
2839
 
        We're dealing with
2840
 
 
2841
 
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2842
 
 
2843
 
        we'll need to convert it into:
2844
 
 
2845
 
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2846
 
                        |                      |
2847
 
                        |<----- wrap_nest ---->|
2848
 
 
2849
 
        Q:  other subqueries may be pointing to this element. What to do?
2850
 
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2851
 
            But we'll need to fix other pointers.
2852
 
        A2: Another way: have TableList::next_ptr so the following
2853
 
            subqueries know the table has been nested.
2854
 
        A3: changes in the TableList::outer_join will make everything work
2855
 
            automatically.
2856
 
      */
2857
 
      if (!(wrap_nest= alloc_join_nest(parent_join->session)))
2858
 
      {
2859
 
        return(true);
2860
 
      }
2861
 
      wrap_nest->embedding= outer_tbl->embedding;
2862
 
      wrap_nest->join_list= outer_tbl->join_list;
2863
 
      wrap_nest->alias= (char*) "(sj-wrap)";
2864
 
 
2865
 
      wrap_nest->nested_join->join_list.empty();
2866
 
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
2867
 
 
2868
 
      outer_tbl->embedding= wrap_nest;
2869
 
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2870
 
 
2871
 
      /*
2872
 
        wrap_nest will take place of outer_tbl, so move the outer join flag
2873
 
        and on_expr
2874
 
      */
2875
 
      wrap_nest->outer_join= outer_tbl->outer_join;
2876
 
      outer_tbl->outer_join= 0;
2877
 
 
2878
 
      wrap_nest->on_expr= outer_tbl->on_expr;
2879
 
      outer_tbl->on_expr= NULL;
2880
 
 
2881
 
      List_iterator<TableList> li(*wrap_nest->join_list);
2882
 
      TableList *tbl;
2883
 
      while ((tbl= li++))
2884
 
      {
2885
 
        if (tbl == outer_tbl)
2886
 
        {
2887
 
          li.replace(wrap_nest);
2888
 
          break;
2889
 
        }
2890
 
      }
2891
 
      /*
2892
 
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
2893
 
        semi-join nest into it
2894
 
      */
2895
 
      emb_join_list= &wrap_nest->nested_join->join_list;
2896
 
      emb_tbl_nest=  wrap_nest;
2897
 
    }
2898
 
  }
2899
 
 
2900
 
  TableList *sj_nest;
2901
 
  nested_join_st *nested_join;
2902
 
  if (!(sj_nest= alloc_join_nest(parent_join->session)))
2903
 
  {
2904
 
    return(true);
2905
 
  }
2906
 
  nested_join= sj_nest->nested_join;
2907
 
 
2908
 
  sj_nest->join_list= emb_join_list;
2909
 
  sj_nest->embedding= emb_tbl_nest;
2910
 
  sj_nest->alias= (char*) "(sj-nest)";
2911
 
  /* Nests do not participate in those 'chains', so: */
2912
 
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2913
 
  emb_join_list->push_back(sj_nest);
2914
 
 
2915
 
  /*
2916
 
    nested_join->used_tables and nested_join->not_null_tables are
2917
 
    initialized in simplify_joins().
2918
 
  */
2919
 
 
2920
 
  /*
2921
 
    2. Walk through subquery's top list and set 'embedding' to point to the
2922
 
       sj-nest.
2923
 
  */
2924
 
  st_select_lex *subq_lex= subq_pred->unit->first_select();
2925
 
  nested_join->join_list.empty();
2926
 
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
2927
 
  TableList *tl, *last_leaf;
2928
 
  while ((tl= li++))
2929
 
  {
2930
 
    tl->embedding= sj_nest;
2931
 
    tl->join_list= &nested_join->join_list;
2932
 
    nested_join->join_list.push_back(tl);
2933
 
  }
2934
 
 
2935
 
  /*
2936
 
    Reconnect the next_leaf chain.
2937
 
    TODO: Do we have to put subquery's tables at the end of the chain?
2938
 
          Inserting them at the beginning would be a bit faster.
2939
 
    NOTE: We actually insert them at the front! That's because the order is
2940
 
          reversed in this list.
2941
 
  */
2942
 
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2943
 
  tl->next_leaf= subq_lex->leaf_tables;
2944
 
  last_leaf= tl;
2945
 
 
2946
 
  /*
2947
 
    Same as above for next_local chain
2948
 
    (a theory: a next_local chain always starts with ::leaf_tables
2949
 
     because view's tables are inserted after the view)
2950
 
  */
2951
 
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2952
 
  tl->next_local= subq_lex->leaf_tables;
2953
 
 
2954
 
  /* A theory: no need to re-connect the next_global chain */
2955
 
 
2956
 
  /* 3. Remove the original subquery predicate from the WHERE/ON */
2957
 
 
2958
 
  // The subqueries were replaced for Item_int(1) earlier
2959
 
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2960
 
  /*TODO: also reset the 'with_subselect' there. */
2961
 
 
2962
 
  /* n. Adjust the parent_join->tables counter */
2963
 
  uint32_t table_no= parent_join->tables;
2964
 
  /* n. Walk through child's tables and adjust table->map */
2965
 
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2966
 
  {
2967
 
    tl->table->tablenr= table_no;
2968
 
    tl->table->map= ((table_map)1) << table_no;
2969
 
    SELECT_LEX *old_sl= tl->select_lex;
2970
 
    tl->select_lex= parent_join->select_lex;
2971
 
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2972
 
      emb->select_lex= parent_join->select_lex;
2973
 
  }
2974
 
  parent_join->tables += subq_lex->join->tables;
2975
 
 
2976
 
  /*
2977
 
    Put the subquery's WHERE into semi-join's sj_on_expr
2978
 
    Add the subquery-induced equalities too.
2979
 
  */
2980
 
  SELECT_LEX *save_lex= session->lex->current_select;
2981
 
  session->lex->current_select=subq_lex;
2982
 
  if (!subq_pred->left_expr->fixed &&
2983
 
       subq_pred->left_expr->fix_fields(session, &subq_pred->left_expr))
2984
 
    return(true);
2985
 
  session->lex->current_select=save_lex;
2986
 
 
2987
 
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2988
 
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
2989
 
                                        subq_pred->left_expr->used_tables();
2990
 
  sj_nest->sj_on_expr= subq_lex->where;
2991
 
 
2992
 
  /*
2993
 
    Create the IN-equalities and inject them into semi-join's ON expression.
2994
 
    Additionally, for InsideOut strategy
2995
 
     - Record the number of IN-equalities.
2996
 
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
2997
 
       see which of the expressions are bound and which are not (for those
2998
 
       we'll produce a distinct stream of (ie_i1,...ie_ik).
2999
 
 
3000
 
       (TODO: can we just create a list of pointers and hope the expressions
3001
 
       will not substitute themselves on fix_fields()? or we need to wrap
3002
 
       them into Item_direct_view_refs and store pointers to those. The
3003
 
       pointers to Item_direct_view_refs are guaranteed to be stable as
3004
 
       Item_direct_view_refs doesn't substitute itself with anything in
3005
 
       Item_direct_view_ref::fix_fields.
3006
 
  */
3007
 
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3008
 
  sj_nest->nested_join->sj_outer_expr_list.empty();
3009
 
 
3010
 
  if (subq_pred->left_expr->cols() == 1)
3011
 
  {
3012
 
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3013
 
 
3014
 
    Item *item_eq= new Item_func_eq(subq_pred->left_expr,
3015
 
                                    subq_lex->ref_pointer_array[0]);
3016
 
    item_eq->name= (char*)subq_sj_cond_name;
3017
 
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3018
 
  }
3019
 
  else
3020
 
  {
3021
 
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
3022
 
    {
3023
 
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3024
 
                                                element_index(i));
3025
 
      Item *item_eq=
3026
 
        new Item_func_eq(subq_pred->left_expr->element_index(i),
3027
 
                         subq_lex->ref_pointer_array[i]);
3028
 
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3029
 
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3030
 
    }
3031
 
  }
3032
 
  /* Fix the created equality and AND */
3033
 
  sj_nest->sj_on_expr->fix_fields(parent_join->session, &sj_nest->sj_on_expr);
3034
 
 
3035
 
  /*
3036
 
    Walk through sj nest's WHERE and ON expressions and call
3037
 
    item->fix_table_changes() for all items.
3038
 
  */
3039
 
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3040
 
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3041
 
 
3042
 
 
3043
 
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3044
 
  subq_lex->master_unit()->exclude_level();
3045
 
 
3046
 
  /* Inject sj_on_expr into the parent's WHERE or ON */
3047
 
  if (emb_tbl_nest)
3048
 
  {
3049
 
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
3050
 
                                     sj_nest->sj_on_expr);
3051
 
    emb_tbl_nest->on_expr->fix_fields(parent_join->session, &emb_tbl_nest->on_expr);
3052
 
  }
3053
 
  else
3054
 
  {
3055
 
    /* Inject into the WHERE */
3056
 
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3057
 
    parent_join->conds->fix_fields(parent_join->session, &parent_join->conds);
3058
 
    parent_join->select_lex->where= parent_join->conds;
3059
 
  }
3060
 
 
3061
 
  return(false);
3062
 
}
3063
 
 
3064
 
 
3065
 
/*
3066
 
  Convert candidate subquery predicates to semi-joins
3067
 
 
3068
 
  SYNOPSIS
3069
 
    JOIN::flatten_subqueries()
3070
 
 
3071
 
  DESCRIPTION
3072
 
    Convert candidate subquery predicates to semi-joins.
3073
 
 
3074
 
  RETURN
3075
 
    false  OK
3076
 
    true   Error
3077
 
*/
3078
 
 
3079
 
bool JOIN::flatten_subqueries()
3080
 
{
3081
 
  Item_in_subselect **in_subq;
3082
 
  Item_in_subselect **in_subq_end;
3083
 
 
3084
 
  if (sj_subselects.elements() == 0)
3085
 
    return(false);
3086
 
 
3087
 
  /* 1. Fix children subqueries */
3088
 
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3089
 
       in_subq != in_subq_end; in_subq++)
3090
 
  {
3091
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3092
 
    child_join->outer_tables = child_join->tables;
3093
 
    if (child_join->flatten_subqueries())
3094
 
      return(true);
3095
 
    (*in_subq)->sj_convert_priority=
3096
 
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3097
 
  }
3098
 
 
3099
 
  /*
3100
 
    2. Pick which subqueries to convert:
3101
 
      sort the subquery array
3102
 
      - prefer correlated subqueries over uncorrelated;
3103
 
      - prefer subqueries that have greater number of outer tables;
3104
 
  */
3105
 
  sj_subselects.sort(subq_sj_candidate_cmp);
3106
 
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3107
 
  /* Replace all subqueries to be flattened with Item_int(1) */
3108
 
  for (in_subq= sj_subselects.front();
3109
 
       in_subq != in_subq_end &&
3110
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3111
 
       in_subq++)
3112
 
  {
3113
 
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3114
 
      return(true);
3115
 
  }
3116
 
 
3117
 
  for (in_subq= sj_subselects.front();
3118
 
       in_subq != in_subq_end &&
3119
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3120
 
       in_subq++)
3121
 
  {
3122
 
    if (convert_subq_to_sj(this, *in_subq))
3123
 
      return(true);
3124
 
  }
3125
 
 
3126
 
  /* 3. Finalize those we didn't convert */
3127
 
  for (; in_subq!= in_subq_end; in_subq++)
3128
 
  {
3129
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3130
 
    Item_subselect::trans_res res;
3131
 
    (*in_subq)->changed= 0;
3132
 
    (*in_subq)->fixed= 0;
3133
 
    res= (*in_subq)->select_transformer(child_join);
3134
 
    if (res == Item_subselect::RES_ERROR)
3135
 
      return(true);
3136
 
 
3137
 
    (*in_subq)->changed= 1;
3138
 
    (*in_subq)->fixed= 1;
3139
 
 
3140
 
    Item *substitute= (*in_subq)->substitution;
3141
 
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3142
 
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3143
 
      return(true);
3144
 
 
3145
 
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
3146
 
    //  return(true);
3147
 
  }
3148
 
  sj_subselects.clear();
3149
 
  return(false);
3150
 
}
3151
 
 
3152
 
 
3153
 
/**
3154
 
  Setup for execution all subqueries of a query, for which the optimizer
3155
 
  chose hash semi-join.
3156
 
 
3157
 
  @details Iterate over all subqueries of the query, and if they are under an
3158
 
  IN predicate, and the optimizer chose to compute it via hash semi-join:
3159
 
  - try to initialize all data structures needed for the materialized execution
3160
 
    of the IN predicate,
3161
 
  - if this fails, then perform the IN=>EXISTS transformation which was
3162
 
    previously blocked during JOIN::prepare.
3163
 
 
3164
 
  This method is part of the "code generation" query processing phase.
3165
 
 
3166
 
  This phase must be called after substitute_for_best_equal_field() because
3167
 
  that function may replace items with other items from a multiple equality,
3168
 
  and we need to reference the correct items in the index access method of the
3169
 
  IN predicate.
3170
 
 
3171
 
  @return Operation status
3172
 
  @retval false     success.
3173
 
  @retval true      error occurred.
3174
 
*/
3175
 
 
3176
 
bool JOIN::setup_subquery_materialization()
3177
 
{
3178
 
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3179
 
       un= un->next_unit())
3180
 
  {
3181
 
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3182
 
    {
3183
 
      Item_subselect *subquery_predicate= sl->master_unit()->item;
3184
 
      if (subquery_predicate &&
3185
 
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
3186
 
      {
3187
 
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3188
 
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3189
 
            in_subs->setup_engine())
3190
 
          return true;
3191
 
      }
3192
 
    }
3193
 
  }
3194
 
  return false;
3195
 
}
3196
 
 
3197
 
 
3198
 
/*
3199
 
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3200
 
 
3201
 
  SYNOPSIS
3202
 
    find_eq_ref_candidate()
3203
 
      table             Table to be checked
3204
 
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
3205
 
                        count.
3206
 
 
3207
 
  DESCRIPTION
3208
 
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3209
 
 
3210
 
  TODO
3211
 
    Check again if it is feasible to factor common parts with constant table
3212
 
    search
3213
 
 
3214
 
  RETURN
3215
 
    true  - There exists an eq_ref(outer-tables) candidate
3216
 
    false - Otherwise
3217
 
*/
3218
 
 
3219
 
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
3220
 
{
3221
 
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3222
 
  uint32_t key;
3223
 
 
3224
 
  if (keyuse)
3225
 
  {
3226
 
    while (1) /* For each key */
3227
 
    {
3228
 
      key= keyuse->key;
3229
 
      KEY *keyinfo= table->key_info + key;
3230
 
      key_part_map bound_parts= 0;
3231
 
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3232
 
      {
3233
 
        do  /* For all equalities on all key parts */
3234
 
        {
3235
 
          /* Check if this is "t.keypart = expr(outer_tables) */
3236
 
          if (!(keyuse->used_tables & sj_inner_tables) &&
3237
 
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3238
 
          {
3239
 
            bound_parts |= 1 << keyuse->keypart;
3240
 
          }
3241
 
          keyuse++;
3242
 
        } while (keyuse->key == key && keyuse->table == table);
3243
 
 
3244
 
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3245
 
          return true;
3246
 
        if (keyuse->table != table)
3247
 
          return false;
3248
 
      }
3249
 
      else
3250
 
      {
3251
 
        do
3252
 
        {
3253
 
          keyuse++;
3254
 
          if (keyuse->table != table)
3255
 
            return false;
3256
 
        }
3257
 
        while (keyuse->key == key);
3258
 
      }
3259
 
    }
3260
 
  }
3261
 
  return false;
3262
 
}
3263
 
 
3264
 
 
3265
 
/*
3266
 
  Pull tables out of semi-join nests, if possible
3267
 
 
3268
 
  SYNOPSIS
3269
 
    pull_out_semijoin_tables()
3270
 
      join  The join where to do the semi-join flattening
3271
 
 
3272
 
  DESCRIPTION
3273
 
    Try to pull tables out of semi-join nests.
3274
 
 
3275
 
    PRECONDITIONS
3276
 
    When this function is called, the join may have several semi-join nests
3277
 
    (possibly within different semi-join nests), but it is guaranteed that
3278
 
    one semi-join nest does not contain another.
3279
 
 
3280
 
    ACTION
3281
 
    A table can be pulled out of the semi-join nest if
3282
 
     - It is a constant table
3283
 
     - It is accessed
3284
 
 
3285
 
    POSTCONDITIONS
3286
 
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3287
 
       tables)
3288
 
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3289
 
     * Semi-join nests TableList::sj_inner_tables
3290
 
 
3291
 
    This operation is (and should be) performed at each PS execution since
3292
 
    tables may become/cease to be constant across PS reexecutions.
3293
 
 
3294
 
  RETURN
3295
 
    0 - OK
3296
 
    1 - Out of memory error
3297
 
*/
3298
 
 
3299
 
int pull_out_semijoin_tables(JOIN *join)
3300
 
{
3301
 
  TableList *sj_nest;
3302
 
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
3303
 
 
3304
 
  /* Try pulling out of the each of the semi-joins */
3305
 
  while ((sj_nest= sj_list_it++))
3306
 
  {
3307
 
    /* Action #1: Mark the constant tables to be pulled out */
3308
 
    table_map pulled_tables= 0;
3309
 
 
3310
 
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
3311
 
    TableList *tbl;
3312
 
    while ((tbl= child_li++))
3313
 
    {
3314
 
      if (tbl->table)
3315
 
      {
3316
 
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3317
 
        if (tbl->table->map & join->const_table_map)
3318
 
        {
3319
 
          pulled_tables |= tbl->table->map;
3320
 
        }
3321
 
      }
3322
 
    }
3323
 
 
3324
 
    /*
3325
 
      Action #2: Find which tables we can pull out based on
3326
 
      update_ref_and_keys() data. Note that pulling one table out can allow
3327
 
      us to pull out some other tables too.
3328
 
    */
3329
 
    bool pulled_a_table;
3330
 
    do
3331
 
    {
3332
 
      pulled_a_table= false;
3333
 
      child_li.rewind();
3334
 
      while ((tbl= child_li++))
3335
 
      {
3336
 
        if (tbl->table && !(pulled_tables & tbl->table->map))
3337
 
        {
3338
 
          if (find_eq_ref_candidate(tbl->table,
3339
 
                                    sj_nest->nested_join->used_tables &
3340
 
                                    ~pulled_tables))
3341
 
          {
3342
 
            pulled_a_table= true;
3343
 
            pulled_tables |= tbl->table->map;
3344
 
          }
3345
 
        }
3346
 
      }
3347
 
    } while (pulled_a_table);
3348
 
 
3349
 
    child_li.rewind();
3350
 
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3351
 
    {
3352
 
      (sj_nest)->sj_inner_tables= 0;
3353
 
      while ((tbl= child_li++))
3354
 
      {
3355
 
        if (tbl->table)
3356
 
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3357
 
      }
3358
 
    }
3359
 
    else
3360
 
    {
3361
 
      /* Record the bitmap of inner tables, mark the inner tables */
3362
 
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
3363
 
                             ~pulled_tables;
3364
 
      (sj_nest)->sj_inner_tables= inner_tables;
3365
 
      while ((tbl= child_li++))
3366
 
      {
3367
 
        if (tbl->table)
3368
 
        {
3369
 
          if (inner_tables & tbl->table->map)
3370
 
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3371
 
          else
3372
 
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3373
 
        }
3374
 
      }
3375
 
    }
3376
 
  }
3377
 
  return(0);
3378
 
}
3379
 
 
3380
469
/*****************************************************************************
3381
 
  Create JOIN_TABS, make a guess about the table types,
 
470
  Create JoinTableS, make a guess about the table types,
3382
471
  Approximate how many records will be used in each table
3383
472
*****************************************************************************/
3384
 
 
3385
 
 
3386
 
static ha_rows get_quick_record_count(Session *session, SQL_SELECT *select,
3387
 
                                      Table *table,
3388
 
                                      const key_map *keys,ha_rows limit)
 
473
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
3389
474
{
3390
475
  int error;
3391
476
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3392
 
    return(0);                           // Fatal error flag is set
 
477
  {
 
478
    return 0;                           // Fatal error flag is set
 
479
  }
 
480
 
3393
481
  if (select)
3394
482
  {
3395
483
    select->head=table;
3396
484
    table->reginfo.impossible_range=0;
3397
485
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
3398
486
                                          limit, 0, false)) == 1)
 
487
    {
3399
488
      return(select->quick->records);
 
489
    }
 
490
 
3400
491
    if (error == -1)
3401
492
    {
3402
493
      table->reginfo.impossible_range=1;
3403
 
      return(0);
 
494
      return 0;
3404
495
    }
3405
496
  }
 
497
 
3406
498
  return(HA_POS_ERROR);                 /* This shouldn't happend */
3407
499
}
3408
500
 
3409
 
/*
3410
 
   This structure is used to collect info on potentially sargable
3411
 
   predicates in order to check whether they become sargable after
3412
 
   reading const tables.
3413
 
   We form a bitmap of indexes that can be used for sargable predicates.
3414
 
   Only such indexes are involved in range analysis.
3415
 
*/
3416
 
typedef struct st_sargable_param
3417
 
{
3418
 
  Field *field;              /* field against which to check sargability */
3419
 
  Item **arg_value;          /* values of potential keys for lookups     */
3420
 
  uint32_t num_values;           /* number of values in the above array      */
3421
 
} SARGABLE_PARAM;
3422
 
 
3423
 
/**
3424
 
  Calculate the best possible join and initialize the join structure.
3425
 
 
3426
 
  @retval
3427
 
    0   ok
3428
 
  @retval
3429
 
    1   Fatal error
3430
 
*/
3431
 
 
3432
 
static bool
3433
 
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
3434
 
                     DYNAMIC_ARRAY *keyuse_array)
3435
 
{
3436
 
  int error;
3437
 
  Table *table;
3438
 
  uint32_t i,table_count,const_count,key;
3439
 
  table_map found_const_table_map, all_table_map, found_ref, refs;
3440
 
  key_map const_ref, eq_part;
3441
 
  Table **table_vector;
3442
 
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3443
 
  KEYUSE *keyuse,*start_keyuse;
3444
 
  table_map outer_join=0;
3445
 
  SARGABLE_PARAM *sargables= 0;
3446
 
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3447
 
 
3448
 
  table_count=join->tables;
3449
 
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
3450
 
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3451
 
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
3452
 
  if (!stat || !stat_ref || !table_vector)
3453
 
    return(1);                          // Eom /* purecov: inspected */
3454
 
 
3455
 
  join->best_ref=stat_vector;
3456
 
 
3457
 
  stat_end=stat+table_count;
3458
 
  found_const_table_map= all_table_map=0;
3459
 
  const_count=0;
3460
 
 
3461
 
  for (s= stat, i= 0;
3462
 
       tables;
3463
 
       s++, tables= tables->next_leaf, i++)
3464
 
  {
3465
 
    TableList *embedding= tables->embedding;
3466
 
    stat_vector[i]=s;
3467
 
    s->keys.init();
3468
 
    s->const_keys.init();
3469
 
    s->checked_keys.init();
3470
 
    s->needed_reg.init();
3471
 
    table_vector[i]=s->table=table=tables->table;
3472
 
    table->pos_in_table_list= tables;
3473
 
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3474
 
    if(error)
3475
 
    {
3476
 
        table->file->print_error(error, MYF(0));
3477
 
        return(1);
3478
 
    }
3479
 
    table->quick_keys.clear_all();
3480
 
    table->reginfo.join_tab=s;
3481
 
    table->reginfo.not_exists_optimize=0;
3482
 
    memset(table->const_key_parts, 0,
3483
 
           sizeof(key_part_map)*table->s->keys);
3484
 
    all_table_map|= table->map;
3485
 
    s->join=join;
3486
 
    s->info=0;                                  // For describe
3487
 
 
3488
 
    s->dependent= tables->dep_tables;
3489
 
    s->key_dependent= 0;
3490
 
    if (tables->schema_table)
3491
 
      table->file->stats.records= 2;
3492
 
    table->quick_condition_rows= table->file->stats.records;
3493
 
 
3494
 
    s->on_expr_ref= &tables->on_expr;
3495
 
    if (*s->on_expr_ref)
3496
 
    {
3497
 
      /* s is the only inner table of an outer join */
3498
 
      if (!table->file->stats.records && !embedding)
3499
 
      {                                         // Empty table
3500
 
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
3501
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3502
 
        continue;
3503
 
      }
3504
 
      outer_join|= table->map;
3505
 
      s->embedding_map= 0;
3506
 
      for (;embedding; embedding= embedding->embedding)
3507
 
        s->embedding_map|= embedding->nested_join->nj_map;
3508
 
      continue;
3509
 
    }
3510
 
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3511
 
    {
3512
 
      /* s belongs to a nested join, maybe to several embedded joins */
3513
 
      s->embedding_map= 0;
3514
 
      do
3515
 
      {
3516
 
        nested_join_st *nested_join= embedding->nested_join;
3517
 
        s->embedding_map|=nested_join->nj_map;
3518
 
        s->dependent|= embedding->dep_tables;
3519
 
        embedding= embedding->embedding;
3520
 
        outer_join|= nested_join->used_tables;
3521
 
      }
3522
 
      while (embedding);
3523
 
      continue;
3524
 
    }
3525
 
    if ((table->file->stats.records <= 1) &&
3526
 
        !s->dependent &&
3527
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3528
 
    {
3529
 
      set_position(join,const_count++,s,(KEYUSE*) 0);
3530
 
    }
3531
 
  }
3532
 
  stat_vector[i]=0;
3533
 
  join->outer_join=outer_join;
3534
 
 
3535
 
  if (join->outer_join)
3536
 
  {
3537
 
    /*
3538
 
       Build transitive closure for relation 'to be dependent on'.
3539
 
       This will speed up the plan search for many cases with outer joins,
3540
 
       as well as allow us to catch illegal cross references/
3541
 
       Warshall's algorithm is used to build the transitive closure.
3542
 
       As we use bitmaps to represent the relation the complexity
3543
 
       of the algorithm is O((number of tables)^2).
3544
 
    */
3545
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3546
 
    {
3547
 
      for (uint32_t j= 0 ; j < table_count ; j++)
3548
 
      {
3549
 
        table= stat[j].table;
3550
 
        if (s->dependent & table->map)
3551
 
          s->dependent |= table->reginfo.join_tab->dependent;
3552
 
      }
3553
 
      if (s->dependent)
3554
 
        s->table->maybe_null= 1;
3555
 
    }
3556
 
    /* Catch illegal cross references for outer joins */
3557
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3558
 
    {
3559
 
      if (s->dependent & s->table->map)
3560
 
      {
3561
 
        join->tables=0;                 // Don't use join->table
3562
 
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3563
 
        return(1);
3564
 
      }
3565
 
      s->key_dependent= s->dependent;
3566
 
    }
3567
 
  }
3568
 
 
3569
 
  if (conds || outer_join)
3570
 
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
3571
 
                            conds, join->cond_equal,
3572
 
                            ~outer_join, join->select_lex, &sargables))
3573
 
      return(1);
3574
 
 
3575
 
  /* Read tables with 0 or 1 rows (system tables) */
3576
 
  join->const_table_map= 0;
3577
 
 
3578
 
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3579
 
       p_pos < p_end ;
3580
 
       p_pos++)
3581
 
  {
3582
 
    int tmp;
3583
 
    s= p_pos->table;
3584
 
    s->type=JT_SYSTEM;
3585
 
    join->const_table_map|=s->table->map;
3586
 
    if ((tmp=join_read_const_table(s, p_pos)))
3587
 
    {
3588
 
      if (tmp > 0)
3589
 
        return(1);                      // Fatal error
3590
 
    }
3591
 
    else
3592
 
      found_const_table_map|= s->table->map;
3593
 
  }
3594
 
 
3595
 
  /* loop until no more const tables are found */
3596
 
  int ref_changed;
3597
 
  do
3598
 
  {
3599
 
  more_const_tables_found:
3600
 
    ref_changed = 0;
3601
 
    found_ref=0;
3602
 
 
3603
 
    /*
3604
 
      We only have to loop from stat_vector + const_count as
3605
 
      set_position() will move all const_tables first in stat_vector
3606
 
    */
3607
 
 
3608
 
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3609
 
    {
3610
 
      table=s->table;
3611
 
 
3612
 
      /*
3613
 
        If equi-join condition by a key is null rejecting and after a
3614
 
        substitution of a const table the key value happens to be null
3615
 
        then we can state that there are no matches for this equi-join.
3616
 
      */
3617
 
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3618
 
      {
3619
 
        /*
3620
 
          When performing an outer join operation if there are no matching rows
3621
 
          for the single row of the outer table all the inner tables are to be
3622
 
          null complemented and thus considered as constant tables.
3623
 
          Here we apply this consideration to the case of outer join operations
3624
 
          with a single inner table only because the case with nested tables
3625
 
          would require a more thorough analysis.
3626
 
          TODO. Apply single row substitution to null complemented inner tables
3627
 
          for nested outer join operations.
3628
 
        */
3629
 
        while (keyuse->table == table)
3630
 
        {
3631
 
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3632
 
              keyuse->val->is_null() && keyuse->null_rejecting)
3633
 
          {
3634
 
            s->type= JT_CONST;
3635
 
            mark_as_null_row(table);
3636
 
            found_const_table_map|= table->map;
3637
 
            join->const_table_map|= table->map;
3638
 
            set_position(join,const_count++,s,(KEYUSE*) 0);
3639
 
            goto more_const_tables_found;
3640
 
           }
3641
 
          keyuse++;
3642
 
        }
3643
 
      }
3644
 
 
3645
 
      if (s->dependent)                         // If dependent on some table
3646
 
      {
3647
 
        // All dep. must be constants
3648
 
        if (s->dependent & ~(found_const_table_map))
3649
 
          continue;
3650
 
        if (table->file->stats.records <= 1L &&
3651
 
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3652
 
            !table->pos_in_table_list->embedding)
3653
 
        {                                       // system table
3654
 
          int tmp= 0;
3655
 
          s->type=JT_SYSTEM;
3656
 
          join->const_table_map|=table->map;
3657
 
          set_position(join,const_count++,s,(KEYUSE*) 0);
3658
 
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3659
 
          {
3660
 
            if (tmp > 0)
3661
 
              return(1);                        // Fatal error
3662
 
          }
3663
 
          else
3664
 
            found_const_table_map|= table->map;
3665
 
          continue;
3666
 
        }
3667
 
      }
3668
 
      /* check if table can be read by key or table only uses const refs */
3669
 
      if ((keyuse=s->keyuse))
3670
 
      {
3671
 
        s->type= JT_REF;
3672
 
        while (keyuse->table == table)
3673
 
        {
3674
 
          start_keyuse=keyuse;
3675
 
          key=keyuse->key;
3676
 
          s->keys.set_bit(key);               // QQ: remove this ?
3677
 
 
3678
 
          refs=0;
3679
 
          const_ref.clear_all();
3680
 
          eq_part.clear_all();
3681
 
          do
3682
 
          {
3683
 
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3684
 
            {
3685
 
              if (!((~found_const_table_map) & keyuse->used_tables))
3686
 
                const_ref.set_bit(keyuse->keypart);
3687
 
              else
3688
 
                refs|=keyuse->used_tables;
3689
 
              eq_part.set_bit(keyuse->keypart);
3690
 
            }
3691
 
            keyuse++;
3692
 
          } while (keyuse->table == table && keyuse->key == key);
3693
 
 
3694
 
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3695
 
              !table->pos_in_table_list->embedding)
3696
 
          {
3697
 
            if ((table->key_info[key].flags & (HA_NOSAME))
3698
 
                 == HA_NOSAME)
3699
 
            {
3700
 
              if (const_ref == eq_part)
3701
 
              {                                 // Found everything for ref.
3702
 
                int tmp;
3703
 
                ref_changed = 1;
3704
 
                s->type= JT_CONST;
3705
 
                join->const_table_map|=table->map;
3706
 
                set_position(join,const_count++,s,start_keyuse);
3707
 
                if (create_ref_for_key(join, s, start_keyuse,
3708
 
                                       found_const_table_map))
3709
 
                  return(1);
3710
 
                if ((tmp=join_read_const_table(s,
3711
 
                                               join->positions+const_count-1)))
3712
 
                {
3713
 
                  if (tmp > 0)
3714
 
                    return(1);                  // Fatal error
3715
 
                }
3716
 
                else
3717
 
                  found_const_table_map|= table->map;
3718
 
                break;
3719
 
              }
3720
 
              else
3721
 
                found_ref|= refs;      // Table is const if all refs are const
3722
 
            }
3723
 
            else if (const_ref == eq_part)
3724
 
              s->const_keys.set_bit(key);
3725
 
          }
3726
 
        }
3727
 
      }
3728
 
    }
3729
 
  } while (join->const_table_map & found_ref && ref_changed);
3730
 
 
3731
 
  /*
3732
 
    Update info on indexes that can be used for search lookups as
3733
 
    reading const tables may has added new sargable predicates.
3734
 
  */
3735
 
  if (const_count && sargables)
3736
 
  {
3737
 
    for( ; sargables->field ; sargables++)
3738
 
    {
3739
 
      Field *field= sargables->field;
3740
 
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3741
 
      key_map possible_keys= field->key_start;
3742
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
3743
 
      bool is_const= 1;
3744
 
      for (uint32_t j=0; j < sargables->num_values; j++)
3745
 
        is_const&= sargables->arg_value[j]->const_item();
3746
 
      if (is_const)
3747
 
        join_tab[0].const_keys.merge(possible_keys);
3748
 
    }
3749
 
  }
3750
 
 
3751
 
  if (pull_out_semijoin_tables(join))
3752
 
    return(true);
3753
 
 
3754
 
  /* Calc how many (possible) matched records in each table */
3755
 
 
3756
 
  for (s=stat ; s < stat_end ; s++)
3757
 
  {
3758
 
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
3759
 
    {
3760
 
      /* Only one matching row */
3761
 
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3762
 
      continue;
3763
 
    }
3764
 
    /* Approximate found rows and time to read them */
3765
 
    s->found_records=s->records=s->table->file->stats.records;
3766
 
    s->read_time=(ha_rows) s->table->file->scan_time();
3767
 
 
3768
 
    /*
3769
 
      Set a max range of how many seeks we can expect when using keys
3770
 
      This is can't be to high as otherwise we are likely to use
3771
 
      table scan.
3772
 
    */
3773
 
    s->worst_seeks= cmin((double) s->found_records / 10,
3774
 
                        (double) s->read_time*3);
3775
 
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3776
 
      s->worst_seeks=2.0;
3777
 
 
3778
 
    /*
3779
 
      Add to stat->const_keys those indexes for which all group fields or
3780
 
      all select distinct fields participate in one index.
3781
 
    */
3782
 
    add_group_and_distinct_keys(join, s);
3783
 
 
3784
 
    if (!s->const_keys.is_clear_all() &&
3785
 
        !s->table->pos_in_table_list->embedding)
3786
 
    {
3787
 
      ha_rows records;
3788
 
      SQL_SELECT *select;
3789
 
      select= make_select(s->table, found_const_table_map,
3790
 
                          found_const_table_map,
3791
 
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3792
 
                          1, &error);
3793
 
      if (!select)
3794
 
        return(1);
3795
 
      records= get_quick_record_count(join->session, select, s->table,
3796
 
                                      &s->const_keys, join->row_limit);
3797
 
      s->quick=select->quick;
3798
 
      s->needed_reg=select->needed_reg;
3799
 
      select->quick=0;
3800
 
      if (records == 0 && s->table->reginfo.impossible_range)
3801
 
      {
3802
 
        /*
3803
 
          Impossible WHERE or ON expression
3804
 
          In case of ON, we mark that the we match one empty NULL row.
3805
 
          In case of WHERE, don't set found_const_table_map to get the
3806
 
          caller to abort with a zero row result.
3807
 
        */
3808
 
        join->const_table_map|= s->table->map;
3809
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3810
 
        s->type= JT_CONST;
3811
 
        if (*s->on_expr_ref)
3812
 
        {
3813
 
          /* Generate empty row */
3814
 
          s->info= "Impossible ON condition";
3815
 
          found_const_table_map|= s->table->map;
3816
 
          s->type= JT_CONST;
3817
 
          mark_as_null_row(s->table);           // All fields are NULL
3818
 
        }
3819
 
      }
3820
 
      if (records != HA_POS_ERROR)
3821
 
      {
3822
 
        s->found_records=records;
3823
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3824
 
      }
3825
 
      delete select;
3826
 
    }
3827
 
  }
3828
 
 
3829
 
  join->join_tab=stat;
3830
 
  join->map2table=stat_ref;
3831
 
  join->table= join->all_tables=table_vector;
3832
 
  join->const_tables=const_count;
3833
 
  join->found_const_table_map=found_const_table_map;
3834
 
 
3835
 
  /* Find an optimal join order of the non-constant tables. */
3836
 
  if (join->const_tables != join->tables)
3837
 
  {
3838
 
    optimize_keyuse(join, keyuse_array);
3839
 
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3840
 
      return(true);
3841
 
  }
3842
 
  else
3843
 
  {
3844
 
    memcpy(join->best_positions, join->positions,
3845
 
           sizeof(POSITION)*join->const_tables);
3846
 
    join->best_read=1.0;
3847
 
  }
3848
 
  /* Generate an execution plan from the found optimal join order. */
3849
 
  return(join->session->killed || get_best_combination(join));
3850
 
}
3851
 
 
3852
 
 
3853
501
/*****************************************************************************
3854
502
  Check with keys are used and with tables references with tables
3855
503
  Updates in stat:
3858
506
          keyuse     Pointer to possible keys
3859
507
*****************************************************************************/
3860
508
 
3861
 
/// Used when finding key fields
3862
 
typedef struct key_field_t {
3863
 
  Field         *field;
3864
 
  Item          *val;                   ///< May be empty if diff constant
3865
 
  uint          level;
3866
 
  uint          optimize; // KEY_OPTIMIZE_*
3867
 
  bool          eq_func;
3868
 
  /**
3869
 
    If true, the condition this struct represents will not be satisfied
3870
 
    when val IS NULL.
3871
 
  */
3872
 
  bool          null_rejecting;
3873
 
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3874
 
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
3875
 
} KEY_FIELD;
3876
 
 
3877
 
/**
3878
 
  Merge new key definitions to old ones, remove those not used in both.
3879
 
 
3880
 
  This is called for OR between different levels.
3881
 
 
3882
 
  To be able to do 'ref_or_null' we merge a comparison of a column
3883
 
  and 'column IS NULL' to one test.  This is useful for sub select queries
3884
 
  that are internally transformed to something like:.
3885
 
 
3886
 
  @code
3887
 
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
3888
 
  @endcode
3889
 
 
3890
 
  KEY_FIELD::null_rejecting is processed as follows: @n
3891
 
  result has null_rejecting=true if it is set for both ORed references.
3892
 
  for example:
3893
 
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
3894
 
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3895
 
 
3896
 
  @todo
3897
 
    The result of this is that we're missing some 'ref' accesses.
3898
 
    OptimizerTeam: Fix this
3899
 
*/
3900
 
 
3901
 
static KEY_FIELD *
3902
 
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3903
 
                 uint32_t and_level)
3904
 
{
3905
 
  if (start == new_fields)
3906
 
    return start;                               // Impossible or
3907
 
  if (new_fields == end)
3908
 
    return start;                               // No new fields, skip all
3909
 
 
3910
 
  KEY_FIELD *first_free=new_fields;
3911
 
 
3912
 
  /* Mark all found fields in old array */
3913
 
  for (; new_fields != end ; new_fields++)
3914
 
  {
3915
 
    for (KEY_FIELD *old=start ; old != first_free ; old++)
3916
 
    {
3917
 
      if (old->field == new_fields->field)
3918
 
      {
3919
 
        /*
3920
 
          NOTE: below const_item() call really works as "!used_tables()", i.e.
3921
 
          it can return false where it is feasible to make it return true.
3922
 
 
3923
 
          The cause is as follows: Some of the tables are already known to be
3924
 
          const tables (the detection code is in make_join_statistics(),
3925
 
          above the update_ref_and_keys() call), but we didn't propagate
3926
 
          information about this: Table::const_table is not set to true, and
3927
 
          Item::update_used_tables() hasn't been called for each item.
3928
 
          The result of this is that we're missing some 'ref' accesses.
3929
 
          TODO: OptimizerTeam: Fix this
3930
 
        */
3931
 
        if (!new_fields->val->const_item())
3932
 
        {
3933
 
          /*
3934
 
            If the value matches, we can use the key reference.
3935
 
            If not, we keep it until we have examined all new values
3936
 
          */
3937
 
          if (old->val->eq(new_fields->val, old->field->binary()))
3938
 
          {
3939
 
            old->level= and_level;
3940
 
            old->optimize= ((old->optimize & new_fields->optimize &
3941
 
                             KEY_OPTIMIZE_EXISTS) |
3942
 
                            ((old->optimize | new_fields->optimize) &
3943
 
                             KEY_OPTIMIZE_REF_OR_NULL));
3944
 
            old->null_rejecting= (old->null_rejecting &&
3945
 
                                  new_fields->null_rejecting);
3946
 
          }
3947
 
        }
3948
 
        else if (old->eq_func && new_fields->eq_func &&
3949
 
                 old->val->eq_by_collation(new_fields->val,
3950
 
                                           old->field->binary(),
3951
 
                                           old->field->charset()))
3952
 
 
3953
 
        {
3954
 
          old->level= and_level;
3955
 
          old->optimize= ((old->optimize & new_fields->optimize &
3956
 
                           KEY_OPTIMIZE_EXISTS) |
3957
 
                          ((old->optimize | new_fields->optimize) &
3958
 
                           KEY_OPTIMIZE_REF_OR_NULL));
3959
 
          old->null_rejecting= (old->null_rejecting &&
3960
 
                                new_fields->null_rejecting);
3961
 
        }
3962
 
        else if (old->eq_func && new_fields->eq_func &&
3963
 
                 ((old->val->const_item() && old->val->is_null()) ||
3964
 
                  new_fields->val->is_null()))
3965
 
        {
3966
 
          /* field = expression OR field IS NULL */
3967
 
          old->level= and_level;
3968
 
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3969
 
          /*
3970
 
            Remember the NOT NULL value unless the value does not depend
3971
 
            on other tables.
3972
 
          */
3973
 
          if (!old->val->used_tables() && old->val->is_null())
3974
 
            old->val= new_fields->val;
3975
 
          /* The referred expression can be NULL: */
3976
 
          old->null_rejecting= 0;
3977
 
        }
3978
 
        else
3979
 
        {
3980
 
          /*
3981
 
            We are comparing two different const.  In this case we can't
3982
 
            use a key-lookup on this so it's better to remove the value
3983
 
            and let the range optimzier handle it
3984
 
          */
3985
 
          if (old == --first_free)              // If last item
3986
 
            break;
3987
 
          *old= *first_free;                    // Remove old value
3988
 
          old--;                                // Retry this value
3989
 
        }
3990
 
      }
3991
 
    }
3992
 
  }
3993
 
  /* Remove all not used items */
3994
 
  for (KEY_FIELD *old=start ; old != first_free ;)
3995
 
  {
3996
 
    if (old->level != and_level)
3997
 
    {                                           // Not used in all levels
3998
 
      if (old == --first_free)
3999
 
        break;
4000
 
      *old= *first_free;                        // Remove old value
4001
 
      continue;
4002
 
    }
4003
 
    old++;
4004
 
  }
4005
 
  return first_free;
4006
 
}
4007
 
 
4008
 
 
4009
 
/**
4010
 
  Add a possible key to array of possible keys if it's usable as a key
4011
 
 
4012
 
    @param key_fields      Pointer to add key, if usable
4013
 
    @param and_level       And level, to be stored in KEY_FIELD
4014
 
    @param cond            Condition predicate
4015
 
    @param field           Field used in comparision
4016
 
    @param eq_func         True if we used =, <=> or IS NULL
4017
 
    @param value           Value used for comparison with field
4018
 
    @param usable_tables   Tables which can be used for key optimization
4019
 
    @param sargables       IN/OUT Array of found sargable candidates
4020
 
 
4021
 
  @note
4022
 
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4023
 
    table, we store this to be able to do not exists optimization later.
4024
 
 
4025
 
  @returns
4026
 
    *key_fields is incremented if we stored a key in the array
4027
 
*/
4028
 
 
4029
 
static void
4030
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4031
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
4032
 
              table_map usable_tables, SARGABLE_PARAM **sargables)
4033
 
{
4034
 
  uint32_t exists_optimize= 0;
4035
 
  if (!(field->flags & PART_KEY_FLAG))
4036
 
  {
4037
 
    // Don't remove column IS NULL on a LEFT JOIN table
4038
 
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4039
 
        !field->table->maybe_null || field->null_ptr)
4040
 
      return;                                   // Not a key. Skip it
4041
 
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4042
 
    assert(num_values == 1);
4043
 
  }
4044
 
  else
4045
 
  {
4046
 
    table_map used_tables=0;
4047
 
    bool optimizable=0;
4048
 
    for (uint32_t i=0; i<num_values; i++)
4049
 
    {
4050
 
      used_tables|=(value[i])->used_tables();
4051
 
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4052
 
        optimizable=1;
4053
 
    }
4054
 
    if (!optimizable)
4055
 
      return;
4056
 
    if (!(usable_tables & field->table->map))
4057
 
    {
4058
 
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4059
 
          !field->table->maybe_null || field->null_ptr)
4060
 
        return;                                 // Can't use left join optimize
4061
 
      exists_optimize= KEY_OPTIMIZE_EXISTS;
4062
 
    }
4063
 
    else
4064
 
    {
4065
 
      JOIN_TAB *stat=field->table->reginfo.join_tab;
4066
 
      key_map possible_keys=field->key_start;
4067
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
4068
 
      stat[0].keys.merge(possible_keys);             // Add possible keys
4069
 
 
4070
 
      /*
4071
 
        Save the following cases:
4072
 
        Field op constant
4073
 
        Field LIKE constant where constant doesn't start with a wildcard
4074
 
        Field = field2 where field2 is in a different table
4075
 
        Field op formula
4076
 
        Field IS NULL
4077
 
        Field IS NOT NULL
4078
 
         Field BETWEEN ...
4079
 
         Field IN ...
4080
 
      */
4081
 
      stat[0].key_dependent|=used_tables;
4082
 
 
4083
 
      bool is_const=1;
4084
 
      for (uint32_t i=0; i<num_values; i++)
4085
 
      {
4086
 
        if (!(is_const&= value[i]->const_item()))
4087
 
          break;
4088
 
      }
4089
 
      if (is_const)
4090
 
        stat[0].const_keys.merge(possible_keys);
4091
 
      else if (!eq_func)
4092
 
      {
4093
 
        /*
4094
 
          Save info to be able check whether this predicate can be
4095
 
          considered as sargable for range analisis after reading const tables.
4096
 
          We do not save info about equalities as update_const_equal_items
4097
 
          will take care of updating info on keys from sargable equalities.
4098
 
        */
4099
 
        (*sargables)--;
4100
 
        (*sargables)->field= field;
4101
 
        (*sargables)->arg_value= value;
4102
 
        (*sargables)->num_values= num_values;
4103
 
      }
4104
 
      /*
4105
 
        We can't always use indexes when comparing a string index to a
4106
 
        number. cmp_type() is checked to allow compare of dates to numbers.
4107
 
        eq_func is NEVER true when num_values > 1
4108
 
       */
4109
 
      if (!eq_func)
4110
 
      {
4111
 
        /*
4112
 
          Additional optimization: if we're processing
4113
 
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
4114
 
          "t.key = c1".
4115
 
          TODO: This is a very limited fix. A more generic fix is possible.
4116
 
          There are 2 options:
4117
 
          A) Make equality propagation code be able to handle BETWEEN
4118
 
             (including cases like t1.key BETWEEN t2.key AND t3.key)
4119
 
          B) Make range optimizer to infer additional "t.key = c" equalities
4120
 
             and use them in equality propagation process (see details in
4121
 
             OptimizerKBAndTodo)
4122
 
        */
4123
 
        if ((cond->functype() != Item_func::BETWEEN) ||
4124
 
            ((Item_func_between*) cond)->negated ||
4125
 
            !value[0]->eq(value[1], field->binary()))
4126
 
          return;
4127
 
        eq_func= true;
4128
 
      }
4129
 
 
4130
 
      if (field->result_type() == STRING_RESULT)
4131
 
      {
4132
 
        if ((*value)->result_type() != STRING_RESULT)
4133
 
        {
4134
 
          if (field->cmp_type() != (*value)->result_type())
4135
 
            return;
4136
 
        }
4137
 
        else
4138
 
        {
4139
 
          /*
4140
 
            We can't use indexes if the effective collation
4141
 
            of the operation differ from the field collation.
4142
 
          */
4143
 
          if (field->cmp_type() == STRING_RESULT &&
4144
 
              ((Field_str*)field)->charset() != cond->compare_collation())
4145
 
            return;
4146
 
        }
4147
 
      }
4148
 
    }
4149
 
  }
4150
 
  /*
4151
 
    For the moment eq_func is always true. This slot is reserved for future
4152
 
    extensions where we want to remembers other things than just eq comparisons
4153
 
  */
4154
 
  assert(eq_func);
4155
 
  /* Store possible eq field */
4156
 
  (*key_fields)->field=         field;
4157
 
  (*key_fields)->eq_func=       eq_func;
4158
 
  (*key_fields)->val=           *value;
4159
 
  (*key_fields)->level=         and_level;
4160
 
  (*key_fields)->optimize=      exists_optimize;
4161
 
  /*
4162
 
    If the condition has form "tbl.keypart = othertbl.field" and
4163
 
    othertbl.field can be NULL, there will be no matches if othertbl.field
4164
 
    has NULL value.
4165
 
    We use null_rejecting in add_not_null_conds() to add
4166
 
    'othertbl.field IS NOT NULL' to tab->select_cond.
4167
 
  */
4168
 
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4169
 
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4170
 
                                  ((*value)->type() == Item::FIELD_ITEM) &&
4171
 
                                  ((Item_field*)*value)->field->maybe_null());
4172
 
  (*key_fields)->cond_guard= NULL;
4173
 
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
4174
 
                              cond->name < subq_sj_cond_name + 64)?
4175
 
                              cond->name - subq_sj_cond_name: UINT_MAX;
4176
 
  (*key_fields)++;
4177
 
}
4178
 
 
4179
 
/**
4180
 
  Add possible keys to array of possible keys originated from a simple
4181
 
  predicate.
4182
 
 
4183
 
    @param  key_fields     Pointer to add key, if usable
4184
 
    @param  and_level      And level, to be stored in KEY_FIELD
4185
 
    @param  cond           Condition predicate
4186
 
    @param  field          Field used in comparision
4187
 
    @param  eq_func        True if we used =, <=> or IS NULL
4188
 
    @param  value          Value used for comparison with field
4189
 
                           Is NULL for BETWEEN and IN
4190
 
    @param  usable_tables  Tables which can be used for key optimization
4191
 
    @param  sargables      IN/OUT Array of found sargable candidates
4192
 
 
4193
 
  @note
4194
 
    If field items f1 and f2 belong to the same multiple equality and
4195
 
    a key is added for f1, the the same key is added for f2.
4196
 
 
4197
 
  @returns
4198
 
    *key_fields is incremented if we stored a key in the array
4199
 
*/
4200
 
 
4201
 
static void
4202
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
4203
 
                     Item_func *cond, Item_field *field_item,
4204
 
                     bool eq_func, Item **val,
4205
 
                     uint32_t num_values, table_map usable_tables,
4206
 
                     SARGABLE_PARAM **sargables)
4207
 
{
4208
 
  Field *field= field_item->field;
4209
 
  add_key_field(key_fields, and_level, cond, field,
4210
 
                eq_func, val, num_values, usable_tables, sargables);
4211
 
  Item_equal *item_equal= field_item->item_equal;
4212
 
  if (item_equal)
4213
 
  {
4214
 
    /*
4215
 
      Add to the set of possible key values every substitution of
4216
 
      the field for an equal field included into item_equal
4217
 
    */
4218
 
    Item_equal_iterator it(*item_equal);
4219
 
    Item_field *item;
4220
 
    while ((item= it++))
4221
 
    {
4222
 
      if (!field->eq(item->field))
4223
 
      {
4224
 
        add_key_field(key_fields, and_level, cond, item->field,
4225
 
                      eq_func, val, num_values, usable_tables,
4226
 
                      sargables);
4227
 
      }
4228
 
    }
4229
 
  }
4230
 
}
4231
 
 
4232
 
static void
4233
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
4234
 
               COND *cond, table_map usable_tables,
4235
 
               SARGABLE_PARAM **sargables)
4236
 
{
4237
 
  if (cond->type() == Item_func::COND_ITEM)
4238
 
  {
4239
 
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4240
 
    KEY_FIELD *org_key_fields= *key_fields;
4241
 
 
4242
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4243
 
    {
4244
 
      Item *item;
4245
 
      while ((item=li++))
4246
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4247
 
                       sargables);
4248
 
      for (; org_key_fields != *key_fields ; org_key_fields++)
4249
 
        org_key_fields->level= *and_level;
4250
 
    }
4251
 
    else
4252
 
    {
4253
 
      (*and_level)++;
4254
 
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
4255
 
                     sargables);
4256
 
      Item *item;
4257
 
      while ((item=li++))
4258
 
      {
4259
 
        KEY_FIELD *start_key_fields= *key_fields;
4260
 
        (*and_level)++;
4261
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4262
 
                       sargables);
4263
 
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
4264
 
                                     *key_fields,++(*and_level));
4265
 
      }
4266
 
    }
4267
 
    return;
4268
 
  }
4269
 
 
4270
 
  /*
4271
 
    Subquery optimization: Conditions that are pushed down into subqueries
4272
 
    are wrapped into Item_func_trig_cond. We process the wrapped condition
4273
 
    but need to set cond_guard for KEYUSE elements generated from it.
4274
 
  */
4275
 
  {
4276
 
    if (cond->type() == Item::FUNC_ITEM &&
4277
 
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4278
 
    {
4279
 
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4280
 
      if (!join->group_list && !join->order &&
4281
 
          join->unit->item &&
4282
 
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
4283
 
          !join->unit->is_union())
4284
 
      {
4285
 
        KEY_FIELD *save= *key_fields;
4286
 
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4287
 
                       sargables);
4288
 
        // Indicate that this ref access candidate is for subquery lookup:
4289
 
        for (; save != *key_fields; save++)
4290
 
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4291
 
      }
4292
 
      return;
4293
 
    }
4294
 
  }
4295
 
 
4296
 
  /* If item is of type 'field op field/constant' add it to key_fields */
4297
 
  if (cond->type() != Item::FUNC_ITEM)
4298
 
    return;
4299
 
  Item_func *cond_func= (Item_func*) cond;
4300
 
  switch (cond_func->select_optimize()) {
4301
 
  case Item_func::OPTIMIZE_NONE:
4302
 
    break;
4303
 
  case Item_func::OPTIMIZE_KEY:
4304
 
  {
4305
 
    Item **values;
4306
 
    // BETWEEN, IN, NE
4307
 
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4308
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4309
 
    {
4310
 
      values= cond_func->arguments()+1;
4311
 
      if (cond_func->functype() == Item_func::NE_FUNC &&
4312
 
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4313
 
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4314
 
        values--;
4315
 
      assert(cond_func->functype() != Item_func::IN_FUNC ||
4316
 
                  cond_func->argument_count() != 2);
4317
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4318
 
                           (Item_field*) (cond_func->key_item()->real_item()),
4319
 
                           0, values,
4320
 
                           cond_func->argument_count()-1,
4321
 
                           usable_tables, sargables);
4322
 
    }
4323
 
    if (cond_func->functype() == Item_func::BETWEEN)
4324
 
    {
4325
 
      values= cond_func->arguments();
4326
 
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
4327
 
      {
4328
 
        Item_field *field_item;
4329
 
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4330
 
            &&
4331
 
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4332
 
        {
4333
 
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4334
 
          add_key_equal_fields(key_fields, *and_level, cond_func,
4335
 
                               field_item, 0, values, 1, usable_tables,
4336
 
                               sargables);
4337
 
        }
4338
 
      }
4339
 
    }
4340
 
    break;
4341
 
  }
4342
 
  case Item_func::OPTIMIZE_OP:
4343
 
  {
4344
 
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4345
 
                     cond_func->functype() == Item_func::EQUAL_FUNC);
4346
 
 
4347
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4348
 
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4349
 
    {
4350
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4351
 
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
4352
 
                           equal_func,
4353
 
                           cond_func->arguments()+1, 1, usable_tables,
4354
 
                           sargables);
4355
 
    }
4356
 
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4357
 
        cond_func->functype() != Item_func::LIKE_FUNC &&
4358
 
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4359
 
    {
4360
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4361
 
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
4362
 
                           equal_func,
4363
 
                           cond_func->arguments(),1,usable_tables,
4364
 
                           sargables);
4365
 
    }
4366
 
    break;
4367
 
  }
4368
 
  case Item_func::OPTIMIZE_NULL:
4369
 
    /* column_name IS [NOT] NULL */
4370
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4371
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4372
 
    {
4373
 
      Item *tmp=new Item_null;
4374
 
      if (unlikely(!tmp))                       // Should never be true
4375
 
        return;
4376
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4377
 
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
4378
 
                    cond_func->functype() == Item_func::ISNULL_FUNC,
4379
 
                           &tmp, 1, usable_tables, sargables);
4380
 
    }
4381
 
    break;
4382
 
  case Item_func::OPTIMIZE_EQUAL:
4383
 
    Item_equal *item_equal= (Item_equal *) cond;
4384
 
    Item *const_item= item_equal->get_const();
4385
 
    Item_equal_iterator it(*item_equal);
4386
 
    Item_field *item;
4387
 
    if (const_item)
4388
 
    {
4389
 
      /*
4390
 
        For each field field1 from item_equal consider the equality
4391
 
        field1=const_item as a condition allowing an index access of the table
4392
 
        with field1 by the keys value of field1.
4393
 
      */
4394
 
      while ((item= it++))
4395
 
      {
4396
 
        add_key_field(key_fields, *and_level, cond_func, item->field,
4397
 
                      true, &const_item, 1, usable_tables, sargables);
4398
 
      }
4399
 
    }
4400
 
    else
4401
 
    {
4402
 
      /*
4403
 
        Consider all pairs of different fields included into item_equal.
4404
 
        For each of them (field1, field1) consider the equality
4405
 
        field1=field2 as a condition allowing an index access of the table
4406
 
        with field1 by the keys value of field2.
4407
 
      */
4408
 
      Item_equal_iterator fi(*item_equal);
4409
 
      while ((item= fi++))
4410
 
      {
4411
 
        Field *field= item->field;
4412
 
        while ((item= it++))
4413
 
        {
4414
 
          if (!field->eq(item->field))
4415
 
          {
4416
 
            add_key_field(key_fields, *and_level, cond_func, field,
4417
 
                          true, (Item **) &item, 1, usable_tables,
4418
 
                          sargables);
4419
 
          }
4420
 
        }
4421
 
        it.rewind();
4422
 
      }
4423
 
    }
4424
 
    break;
4425
 
  }
4426
 
}
4427
509
 
4428
510
/**
4429
511
  Add all keys with uses 'field' for some keypart.
4430
512
 
4431
513
  If field->and_level != and_level then only mark key_part as const_part.
4432
514
*/
4433
 
 
4434
 
static uint
4435
 
max_part_bit(key_part_map bits)
 
515
uint32_t max_part_bit(key_part_map bits)
4436
516
{
4437
517
  uint32_t found;
4438
518
  for (found=0; bits & 1 ; found++,bits>>=1) ;
 
519
 
4439
520
  return found;
4440
521
}
4441
522
 
4442
 
static void
4443
 
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4444
 
{
4445
 
  Field *field=key_field->field;
4446
 
  Table *form= field->table;
4447
 
  KEYUSE keyuse;
4448
 
 
4449
 
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4450
 
  {
4451
 
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
4452
 
    {
4453
 
      if (!(form->keys_in_use_for_query.is_set(key)))
4454
 
        continue;
4455
 
 
4456
 
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
4457
 
      for (uint32_t part=0 ; part <  key_parts ; part++)
4458
 
      {
4459
 
        if (field->eq(form->key_info[key].key_part[part].field))
4460
 
        {
4461
 
          keyuse.table= field->table;
4462
 
          keyuse.val =  key_field->val;
4463
 
          keyuse.key =  key;
4464
 
          keyuse.keypart=part;
4465
 
          keyuse.keypart_map= (key_part_map) 1 << part;
4466
 
          keyuse.used_tables=key_field->val->used_tables();
4467
 
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4468
 
          keyuse.null_rejecting= key_field->null_rejecting;
4469
 
          keyuse.cond_guard= key_field->cond_guard;
4470
 
          keyuse.sj_pred_no= key_field->sj_pred_no;
4471
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
4472
 
        }
4473
 
      }
4474
 
    }
4475
 
  }
4476
 
}
4477
 
 
4478
 
static int
4479
 
sort_keyuse(KEYUSE *a,KEYUSE *b)
 
523
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4480
524
{
4481
525
  int res;
4482
 
  if (a->table->tablenr != b->table->tablenr)
4483
 
    return (int) (a->table->tablenr - b->table->tablenr);
4484
 
  if (a->key != b->key)
4485
 
    return (int) (a->key - b->key);
4486
 
  if (a->keypart != b->keypart)
4487
 
    return (int) (a->keypart - b->keypart);
 
526
  if (a->getTable()->tablenr != b->getTable()->tablenr)
 
527
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
 
528
 
 
529
  if (a->getKey() != b->getKey())
 
530
    return static_cast<int>((a->getKey() - b->getKey()));
 
531
 
 
532
  if (a->getKeypart() != b->getKeypart())
 
533
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
534
 
4488
535
  // Place const values before other ones
4489
 
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4490
 
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
 
536
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
 
537
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
4491
538
    return res;
 
539
 
4492
540
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4493
 
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4494
 
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4495
 
}
4496
 
 
4497
 
 
4498
 
/*
4499
 
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
4500
 
 
4501
 
    This function populates KEY_FIELD array with entries generated from the
4502
 
    ON condition of the given nested join, and does the same for nested joins
4503
 
    contained within this nested join.
4504
 
 
4505
 
  @param[in]      nested_join_table   Nested join pseudo-table to process
4506
 
  @param[in,out]  end                 End of the key field array
4507
 
  @param[in,out]  and_level           And-level
4508
 
  @param[in,out]  sargables           Array of found sargable candidates
4509
 
 
4510
 
 
4511
 
  @note
4512
 
    We can add accesses to the tables that are direct children of this nested
4513
 
    join (1), and are not inner tables w.r.t their neighbours (2).
4514
 
 
4515
 
    Example for #1 (outer brackets pair denotes nested join this function is
4516
 
    invoked for):
4517
 
    @code
4518
 
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4519
 
    @endcode
4520
 
    Example for #2:
4521
 
    @code
4522
 
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4523
 
    @endcode
4524
 
    In examples 1-2 for condition cond, we can add 'ref' access candidates to
4525
 
    t1 only.
4526
 
    Example #3:
4527
 
    @code
4528
 
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4529
 
    @endcode
4530
 
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4531
 
*/
4532
 
 
4533
 
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4534
 
                                  KEY_FIELD **end, uint32_t *and_level,
4535
 
                                  SARGABLE_PARAM **sargables)
4536
 
{
4537
 
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4538
 
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
4539
 
  bool have_another = false;
4540
 
  table_map tables= 0;
4541
 
  TableList *table;
4542
 
  assert(nested_join_table->nested_join);
4543
 
 
4544
 
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4545
 
                                            (table= li++))))
4546
 
  {
4547
 
    if (table->nested_join)
4548
 
    {
4549
 
      if (!table->on_expr)
4550
 
      {
4551
 
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4552
 
        have_another= true;
4553
 
        li2= li;
4554
 
        li= List_iterator<TableList>(table->nested_join->join_list);
4555
 
      }
4556
 
      else
4557
 
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4558
 
    }
4559
 
    else
4560
 
      if (!table->on_expr)
4561
 
        tables |= table->table->map;
4562
 
  }
4563
 
  if (nested_join_table->on_expr)
4564
 
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
4565
 
                   sargables);
 
541
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
 
542
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4566
543
}
4567
544
 
4568
545
 
4570
547
  Update keyuse array with all possible keys we can use to fetch rows.
4571
548
 
4572
549
  @param       session
4573
 
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
 
550
  @param[out]  keyuse         Put here ordered array of KeyUse structures
4574
551
  @param       join_tab       Array in tablenr_order
4575
552
  @param       tables         Number of tables in join
4576
553
  @param       cond           WHERE condition (note that the function analyzes
4579
556
                              for which we can make ref access based the WHERE
4580
557
                              clause)
4581
558
  @param       select_lex     current SELECT
4582
 
  @param[out]  sargables      Array of found sargable candidates
 
559
  @param[out]  sargables      std::vector of found sargable candidates
4583
560
 
4584
561
   @retval
4585
562
     0  OK
4586
563
   @retval
4587
564
     1  Out of memory.
4588
565
*/
4589
 
 
4590
 
static bool
4591
 
update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4592
 
                    uint32_t tables, COND *cond, COND_EQUAL *,
4593
 
                    table_map normal_tables, SELECT_LEX *select_lex,
4594
 
                    SARGABLE_PARAM **sargables)
 
566
void update_ref_and_keys(Session *session,
 
567
                         DYNAMIC_ARRAY *keyuse,
 
568
                         JoinTable *join_tab,
 
569
                         uint32_t tables,
 
570
                         COND *cond,
 
571
                         COND_EQUAL *,
 
572
                         table_map normal_tables,
 
573
                         Select_Lex *select_lex,
 
574
                         vector<optimizer::SargableParam> &sargables)
4595
575
{
4596
 
  uint  and_level,i,found_eq_constant;
4597
 
  KEY_FIELD *key_fields, *end, *field;
4598
 
  uint32_t sz;
4599
 
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
576
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
4600
577
 
4601
578
  /*
4602
 
    We use the same piece of memory to store both  KEY_FIELD
4603
 
    and SARGABLE_PARAM structure.
4604
 
    KEY_FIELD values are placed at the beginning this memory
4605
 
    while  SARGABLE_PARAM values are put at the end.
4606
 
    All predicates that are used to fill arrays of KEY_FIELD
4607
 
    and SARGABLE_PARAM structures have at most 2 arguments
 
579
    All predicates that are used to fill arrays of KeyField
 
580
    and SargableParam classes have at most 2 arguments
4608
581
    except BETWEEN predicates that have 3 arguments and
4609
582
    IN predicates.
4610
583
    This any predicate if it's not BETWEEN/IN can be used
4611
 
    directly to fill at most 2 array elements, either of KEY_FIELD
4612
 
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
 
584
    directly to fill at most 2 array elements, either of KeyField
 
585
    or SargableParam type. For a BETWEEN predicate 3 elements
4613
586
    can be filled as this predicate is considered as
4614
587
    saragable with respect to each of its argument.
4615
588
    An IN predicate can require at most 1 element as currently
4619
592
    can be not more than select_lex->max_equal_elems such
4620
593
    substitutions.
4621
594
  */
4622
 
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4623
 
      (((session->lex->current_select->cond_count+1)*2 +
4624
 
        session->lex->current_select->between_count)*m+1);
4625
 
  if (!(key_fields=(KEY_FIELD*) session->alloc(sz)))
4626
 
    return true; /* purecov: inspected */
4627
 
  and_level= 0;
 
595
  optimizer::KeyField* key_fields= new (session->mem) optimizer::KeyField[((session->lex().current_select->cond_count+1)*2 + session->lex().current_select->between_count)*m+1];
 
596
  uint and_level= 0;
 
597
  optimizer::KeyField* end, *field;
4628
598
  field= end= key_fields;
4629
 
  *sargables= (SARGABLE_PARAM *) key_fields +
4630
 
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
4631
 
  /* set a barrier for the array of SARGABLE_PARAM */
4632
 
  (*sargables)[0].field= 0;
4633
599
 
4634
 
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
4635
 
    return true;
 
600
  keyuse->init(sizeof(optimizer::KeyUse), 20, 64);
4636
601
  if (cond)
4637
602
  {
4638
603
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
4639
604
                   sargables);
4640
 
    for (; field != end ; field++)
 
605
    for (; field != end; field++)
4641
606
    {
4642
 
      add_key_part(keyuse,field);
 
607
      add_key_part(keyuse, field);
4643
608
      /* Mark that we can optimize LEFT JOIN */
4644
 
      if (field->val->type() == Item::NULL_ITEM &&
4645
 
          !field->field->real_maybe_null())
4646
 
        field->field->table->reginfo.not_exists_optimize=1;
 
609
      if (field->getValue()->type() == Item::NULL_ITEM &&
 
610
          ! field->getField()->real_maybe_null())
 
611
      {
 
612
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
 
613
      }
4647
614
    }
4648
615
  }
4649
 
  for (i=0 ; i < tables ; i++)
 
616
  for (uint32_t i= 0; i < tables; i++)
4650
617
  {
4651
618
    /*
4652
619
      Block the creation of keys for inner tables of outer joins.
4665
632
 
4666
633
  /* Process ON conditions for the nested joins */
4667
634
  {
4668
 
    List_iterator<TableList> li(*join_tab->join->join_list);
 
635
    List<TableList>::iterator li(join_tab->join->join_list->begin());
4669
636
    TableList *table;
4670
637
    while ((table= li++))
4671
638
    {
4672
 
      if (table->nested_join)
 
639
      if (table->getNestedJoin())
4673
640
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
4674
641
                              sargables);
4675
642
    }
4688
655
      (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
4689
656
      used in the query, we drop the partial key parts from consideration).
4690
657
  */
4691
 
  if (keyuse->elements)
 
658
  if (keyuse->size())
4692
659
  {
4693
 
    KEYUSE key_end,*prev,*save_pos,*use;
 
660
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
4694
661
 
4695
 
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
4696
 
          (qsort_cmp) sort_keyuse);
 
662
    internal::my_qsort(keyuse->buffer,keyuse->size(),sizeof(optimizer::KeyUse),
 
663
                       (qsort_cmp) sort_keyuse);
4697
664
 
4698
665
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
4699
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
666
    keyuse->push_back(&key_end);
4700
667
 
4701
 
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
 
668
    use= save_pos= (optimizer::KeyUse*)keyuse->buffer;
4702
669
    prev= &key_end;
4703
 
    found_eq_constant=0;
4704
 
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
 
670
    uint found_eq_constant= 0;
4705
671
    {
4706
 
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4707
 
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
672
      for (uint32_t i= 0; i < keyuse->size()-1; i++, use++)
4708
673
      {
4709
 
        if (use->key == prev->key && use->table == prev->table)
4710
 
        {
4711
 
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4712
 
            continue;                           /* remove */
4713
 
        }
4714
 
        else if (use->keypart != 0)             // First found must be 0
4715
 
          continue;
 
674
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
 
675
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
676
 
 
677
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
 
678
        {
 
679
          if (prev->getKeypart() + 1 < use->getKeypart() ||
 
680
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
 
681
          {
 
682
            continue;                           /* remove */
 
683
          }
 
684
        }
 
685
        else if (use->getKeypart() != 0)                // First found must be 0
 
686
        {
 
687
          continue;
 
688
        }
 
689
 
 
690
#ifdef HAVE_VALGRIND
 
691
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
692
        if (save_pos != use)
 
693
#endif
 
694
          *save_pos= *use;
 
695
        prev=use;
 
696
        found_eq_constant= ! use->getUsedTables();
 
697
        /* Save ptr to first use */
 
698
        if (! use->getTable()->reginfo.join_tab->keyuse)
 
699
        {
 
700
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
 
701
        }
 
702
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
 
703
        save_pos++;
4716
704
      }
4717
705
 
4718
 
#ifdef HAVE_purify
4719
 
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
4720
 
      if (save_pos != use)
4721
 
#endif
4722
 
        *save_pos= *use;
4723
 
      prev=use;
4724
 
      found_eq_constant= !use->used_tables;
4725
 
      /* Save ptr to first use */
4726
 
      if (!use->table->reginfo.join_tab->keyuse)
4727
 
        use->table->reginfo.join_tab->keyuse=save_pos;
4728
 
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
4729
 
      save_pos++;
 
706
      uint32_t i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
 
707
      reinterpret_cast<optimizer::KeyUse*>(keyuse->buffer)[i] = key_end;
 
708
      keyuse->set_size(i);
4730
709
    }
4731
 
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4732
 
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
4733
 
    keyuse->elements=i;
4734
710
  }
4735
 
  return false;
4736
711
}
4737
712
 
4738
713
/**
4739
714
  Update some values in keyuse for faster choose_plan() loop.
4740
715
*/
4741
 
 
4742
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
716
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
4743
717
{
4744
 
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
4745
 
 
4746
 
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
 
718
  optimizer::KeyUse* keyuse= (optimizer::KeyUse*)keyuse_array->buffer;
 
719
  for (optimizer::KeyUse* end= keyuse+ keyuse_array->size() ; keyuse < end ; keyuse++)
4747
720
  {
4748
721
    table_map map;
4749
722
    /*
4754
727
      Constant tables are ignored.
4755
728
      To avoid bad matches, we don't make ref_table_rows less than 100.
4756
729
    */
4757
 
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
4758
 
    if (keyuse->used_tables &
4759
 
        (map= (keyuse->used_tables & ~join->const_table_map &
4760
 
               ~OUTER_REF_TABLE_BIT)))
 
730
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
 
731
 
 
732
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
4761
733
    {
4762
734
      uint32_t tablenr;
4763
735
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4764
736
      if (map == 1)                     // Only one table
4765
737
      {
4766
 
        Table *tmp_table=join->all_tables[tablenr];
4767
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
738
        Table *tmp_table=join->all_tables[tablenr];
 
739
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
4768
740
      }
4769
741
    }
 
742
 
4770
743
    /*
4771
744
      Outer reference (external field) is constant for single executing
4772
745
      of subquery
4773
746
    */
4774
 
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
4775
 
      keyuse->ref_table_rows= 1;
 
747
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
 
748
    {
 
749
      keyuse->setTableRows(1);
 
750
    }
4776
751
  }
4777
752
}
4778
753
 
4794
769
  @return
4795
770
    None
4796
771
*/
4797
 
 
4798
 
static void
4799
 
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
 
772
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
4800
773
{
4801
774
  List<Item_field> indexed_fields;
4802
 
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4803
 
  order_st      *cur_group;
 
775
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
 
776
  Order      *cur_group;
4804
777
  Item_field *cur_item;
4805
778
  key_map possible_keys(0);
4806
779
 
4807
780
  if (join->group_list)
4808
781
  { /* Collect all query fields referenced in the GROUP clause. */
4809
782
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4810
 
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4811
 
                               (unsigned char*) &indexed_fields);
 
783
    {
 
784
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
 
785
    }
4812
786
  }
4813
787
  else if (join->select_distinct)
4814
788
  { /* Collect all query fields referenced in the SELECT clause. */
4815
789
    List<Item> &select_items= join->fields_list;
4816
 
    List_iterator<Item> select_items_it(select_items);
 
790
    List<Item>::iterator select_items_it(select_items.begin());
4817
791
    Item *item;
4818
792
    while ((item= select_items_it++))
4819
 
      item->walk(&Item::collect_item_field_processor, 0,
4820
 
                 (unsigned char*) &indexed_fields);
 
793
    {
 
794
      item->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
 
795
    }
4821
796
  }
4822
797
  else
 
798
  {
4823
799
    return;
 
800
  }
4824
801
 
4825
 
  if (indexed_fields.elements == 0)
 
802
  if (indexed_fields.size() == 0)
 
803
  {
4826
804
    return;
 
805
  }
4827
806
 
4828
807
  /* Intersect the keys of all group fields. */
4829
808
  cur_item= indexed_fields_it++;
4830
 
  possible_keys.merge(cur_item->field->part_of_key);
 
809
  possible_keys|= cur_item->field->part_of_key;
4831
810
  while ((cur_item= indexed_fields_it++))
4832
811
  {
4833
 
    possible_keys.intersect(cur_item->field->part_of_key);
4834
 
  }
4835
 
 
4836
 
  if (!possible_keys.is_clear_all())
4837
 
    join_tab->const_keys.merge(possible_keys);
4838
 
}
4839
 
 
4840
 
 
4841
 
/*****************************************************************************
4842
 
  Go through all combinations of not marked tables and find the one
4843
 
  which uses least records
4844
 
*****************************************************************************/
4845
 
 
4846
 
/** Save const tables first as used tables. */
4847
 
 
4848
 
static void
4849
 
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
4850
 
{
4851
 
  join->positions[idx].table= table;
4852
 
  join->positions[idx].key=key;
4853
 
  join->positions[idx].records_read=1.0;        /* This is a const table */
4854
 
  join->positions[idx].ref_depend_map= 0;
4855
 
 
4856
 
  /* Move the const table as down as possible in best_ref */
4857
 
  JOIN_TAB **pos=join->best_ref+idx+1;
4858
 
  JOIN_TAB *next=join->best_ref[idx];
4859
 
  for (;next != table ; pos++)
4860
 
  {
4861
 
    JOIN_TAB *tmp=pos[0];
4862
 
    pos[0]=next;
4863
 
    next=tmp;
4864
 
  }
4865
 
  join->best_ref[idx]=table;
4866
 
}
4867
 
 
4868
 
 
4869
 
/*
4870
 
  Given a semi-join nest, find out which of the IN-equalities are bound
4871
 
 
4872
 
  SYNOPSIS
4873
 
    get_bound_sj_equalities()
4874
 
      sj_nest           Semi-join nest
4875
 
      remaining_tables  Tables that are not yet bound
4876
 
 
4877
 
  DESCRIPTION
4878
 
    Given a semi-join nest, find out which of the IN-equalities have their
4879
 
    left part expression bound (i.e. the said expression doesn't refer to
4880
 
    any of remaining_tables and can be evaluated).
4881
 
 
4882
 
  RETURN
4883
 
    Bitmap of bound IN-equalities.
4884
 
*/
4885
 
 
4886
 
uint64_t get_bound_sj_equalities(TableList *sj_nest,
4887
 
                                  table_map remaining_tables)
4888
 
{
4889
 
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4890
 
  Item *item;
4891
 
  uint32_t i= 0;
4892
 
  uint64_t res= 0;
4893
 
  while ((item= li++))
4894
 
  {
4895
 
    /*
4896
 
      Q: should this take into account equality propagation and how?
4897
 
      A: If e->outer_side is an Item_field, walk over the equality
4898
 
         class and see if there is an element that is bound?
4899
 
      (this is an optional feature)
4900
 
    */
4901
 
    if (!(item->used_tables() & remaining_tables))
4902
 
    {
4903
 
      res |= 1UL < i;
4904
 
    }
4905
 
  }
4906
 
  return res;
4907
 
}
4908
 
 
4909
 
 
4910
 
/**
4911
 
  Find the best access path for an extension of a partial execution
4912
 
  plan and add this path to the plan.
4913
 
 
4914
 
  The function finds the best access path to table 's' from the passed
4915
 
  partial plan where an access path is the general term for any means to
4916
 
  access the data in 's'. An access path may use either an index or a scan,
4917
 
  whichever is cheaper. The input partial plan is passed via the array
4918
 
  'join->positions' of length 'idx'. The chosen access method for 's' and its
4919
 
  cost are stored in 'join->positions[idx]'.
4920
 
 
4921
 
  @param join             pointer to the structure providing all context info
4922
 
                          for the query
4923
 
  @param s                the table to be joined by the function
4924
 
  @param session              thread for the connection that submitted the query
4925
 
  @param remaining_tables set of tables not included into the partial plan yet
4926
 
  @param idx              the length of the partial plan
4927
 
  @param record_count     estimate for the number of records returned by the
4928
 
                          partial plan
4929
 
  @param read_time        the cost of the partial plan
4930
 
 
4931
 
  @return
4932
 
    None
4933
 
*/
4934
 
 
4935
 
static void
4936
 
best_access_path(JOIN      *join,
4937
 
                 JOIN_TAB  *s,
4938
 
                 Session       *session,
4939
 
                 table_map remaining_tables,
4940
 
                 uint32_t      idx,
4941
 
                 double    record_count,
4942
 
                 double)
4943
 
{
4944
 
  KEYUSE *best_key=         0;
4945
 
  uint32_t best_max_key_part=   0;
4946
 
  bool found_constraint= 0;
4947
 
  double best=              DBL_MAX;
4948
 
  double best_time=         DBL_MAX;
4949
 
  double records=           DBL_MAX;
4950
 
  table_map best_ref_depends_map= 0;
4951
 
  double tmp;
4952
 
  ha_rows rec;
4953
 
  uint32_t best_is_sj_inside_out=    0;
4954
 
 
4955
 
  if (s->keyuse)
4956
 
  {                                            /* Use key if possible */
4957
 
    Table *table= s->table;
4958
 
    KEYUSE *keyuse,*start_key=0;
4959
 
    double best_records= DBL_MAX;
4960
 
    uint32_t max_key_part=0;
4961
 
    uint64_t bound_sj_equalities= 0;
4962
 
    bool try_sj_inside_out= false;
4963
 
    /*
4964
 
      Discover the bound equalites. We need to do this, if
4965
 
        1. The next table is an SJ-inner table, and
4966
 
        2. It is the first table from that semijoin, and
4967
 
        3. We're not within a semi-join range (i.e. all semi-joins either have
4968
 
           all or none of their tables in join_table_map), except
4969
 
           s->emb_sj_nest (which we've just entered).
4970
 
        3. All correlation references from this sj-nest are bound
4971
 
    */
4972
 
    if (s->emb_sj_nest &&                                                 // (1)
4973
 
        s->emb_sj_nest->sj_in_exprs < 64 &&
4974
 
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
4975
 
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
4976
 
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
4977
 
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4978
 
    {
4979
 
      /* This table is an InsideOut scan candidate */
4980
 
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
4981
 
                                                   remaining_tables);
4982
 
      try_sj_inside_out= true;
4983
 
    }
4984
 
 
4985
 
    /* Test how we can use keys */
4986
 
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
4987
 
    for (keyuse=s->keyuse ; keyuse->table == table ;)
4988
 
    {
4989
 
      key_part_map found_part= 0;
4990
 
      table_map found_ref= 0;
4991
 
      uint32_t key= keyuse->key;
4992
 
      KEY *keyinfo= table->key_info+key;
4993
 
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4994
 
      key_part_map const_part= 0;
4995
 
      /* The or-null keypart in ref-or-null access: */
4996
 
      key_part_map ref_or_null_part= 0;
4997
 
 
4998
 
      /* Calculate how many key segments of the current key we can use */
4999
 
      start_key= keyuse;
5000
 
      uint64_t handled_sj_equalities=0;
5001
 
      key_part_map sj_insideout_map= 0;
5002
 
 
5003
 
      do /* For each keypart */
5004
 
      {
5005
 
        uint32_t keypart= keyuse->keypart;
5006
 
        table_map best_part_found_ref= 0;
5007
 
        double best_prev_record_reads= DBL_MAX;
5008
 
 
5009
 
        do /* For each way to access the keypart */
5010
 
        {
5011
 
 
5012
 
          /*
5013
 
            if 1. expression doesn't refer to forward tables
5014
 
               2. we won't get two ref-or-null's
5015
 
          */
5016
 
          if (!(remaining_tables & keyuse->used_tables) &&
5017
 
              !(ref_or_null_part && (keyuse->optimize &
5018
 
                                     KEY_OPTIMIZE_REF_OR_NULL)))
5019
 
          {
5020
 
            found_part|= keyuse->keypart_map;
5021
 
            if (!(keyuse->used_tables & ~join->const_table_map))
5022
 
              const_part|= keyuse->keypart_map;
5023
 
 
5024
 
            double tmp2= prev_record_reads(join, idx, (found_ref |
5025
 
                                                      keyuse->used_tables));
5026
 
            if (tmp2 < best_prev_record_reads)
5027
 
            {
5028
 
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5029
 
              best_prev_record_reads= tmp2;
5030
 
            }
5031
 
            if (rec > keyuse->ref_table_rows)
5032
 
              rec= keyuse->ref_table_rows;
5033
 
            /*
5034
 
              If there is one 'key_column IS NULL' expression, we can
5035
 
              use this ref_or_null optimisation of this field
5036
 
            */
5037
 
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5038
 
              ref_or_null_part |= keyuse->keypart_map;
5039
 
          }
5040
 
 
5041
 
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5042
 
          {
5043
 
            if (!(remaining_tables & keyuse->used_tables))
5044
 
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
5045
 
            else
5046
 
            {
5047
 
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
5048
 
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5049
 
            }
5050
 
          }
5051
 
 
5052
 
          keyuse++;
5053
 
        } while (keyuse->table == table && keyuse->key == key &&
5054
 
                 keyuse->keypart == keypart);
5055
 
        found_ref|= best_part_found_ref;
5056
 
      } while (keyuse->table == table && keyuse->key == key);
5057
 
 
5058
 
      /*
5059
 
        Assume that that each key matches a proportional part of table.
5060
 
      */
5061
 
      if (!found_part && !handled_sj_equalities)
5062
 
        continue;                               // Nothing usable found
5063
 
 
5064
 
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5065
 
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
5066
 
 
5067
 
      bool sj_inside_out_scan= false;
5068
 
      {
5069
 
        found_constraint= 1;
5070
 
        /*
5071
 
          Check if InsideOut scan is applicable:
5072
 
          1. All IN-equalities are either "bound" or "handled"
5073
 
          2. Index keyparts are
5074
 
             ...
5075
 
        */
5076
 
        if (try_sj_inside_out &&
5077
 
            table->covering_keys.is_set(key) &&
5078
 
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5079
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5080
 
        {
5081
 
          uint32_t n_fixed_parts= max_part_bit(found_part);
5082
 
          if (n_fixed_parts != keyinfo->key_parts &&
5083
 
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5084
 
               PREV_BITS(uint, keyinfo->key_parts))
5085
 
          {
5086
 
            /*
5087
 
              Not all parts are fixed. Produce bitmap of remaining bits and
5088
 
              check if all of them are covered.
5089
 
            */
5090
 
            sj_inside_out_scan= true;
5091
 
            if (!n_fixed_parts)
5092
 
            {
5093
 
              /*
5094
 
                It's a confluent ref scan.
5095
 
 
5096
 
                That is, all found KEYUSE elements refer to IN-equalities,
5097
 
                and there is really no ref access because there is no
5098
 
                  t.keypart0 = {bound expression}
5099
 
 
5100
 
                Calculate the cost of complete loose index scan.
5101
 
              */
5102
 
              records= (double)s->table->file->stats.records;
5103
 
 
5104
 
              /* The cost is entire index scan cost (divided by 2) */
5105
 
              best_time= s->table->file->index_only_read_time(key, records);
5106
 
 
5107
 
              /* Now figure how many different keys we will get */
5108
 
              ulong rpc;
5109
 
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5110
 
                records= records / rpc;
5111
 
              start_key= NULL;
5112
 
            }
5113
 
          }
5114
 
        }
5115
 
 
5116
 
        /*
5117
 
          Check if we found full key
5118
 
        */
5119
 
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5120
 
            !ref_or_null_part)
5121
 
        {                                         /* use eq key */
5122
 
          max_key_part= UINT32_MAX;
5123
 
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5124
 
          {
5125
 
            tmp = prev_record_reads(join, idx, found_ref);
5126
 
            records=1.0;
5127
 
          }
5128
 
          else
5129
 
          {
5130
 
            if (!found_ref)
5131
 
            {                                     /* We found a const key */
5132
 
              /*
5133
 
                ReuseRangeEstimateForRef-1:
5134
 
                We get here if we've found a ref(const) (c_i are constants):
5135
 
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
5136
 
 
5137
 
                If range optimizer was able to construct a "range"
5138
 
                access on this index, then its condition "quick_cond" was
5139
 
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5140
 
                from the range optimizer.
5141
 
 
5142
 
                Proof of (*): By properties of range and ref optimizers
5143
 
                quick_cond will be equal or tighther than ref_const_cond.
5144
 
                ref_const_cond already covers "smallest" possible interval -
5145
 
                a singlepoint interval over all keyparts. Therefore,
5146
 
                quick_cond is equivalent to ref_const_cond (if it was an
5147
 
                empty interval we wouldn't have got here).
5148
 
              */
5149
 
              if (table->quick_keys.is_set(key))
5150
 
                records= (double) table->quick_rows[key];
5151
 
              else
5152
 
              {
5153
 
                /* quick_range couldn't use key! */
5154
 
                records= (double) s->records/rec;
5155
 
              }
5156
 
            }
5157
 
            else
5158
 
            {
5159
 
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5160
 
              {                                   /* Prefer longer keys */
5161
 
                records=
5162
 
                  ((double) s->records / (double) rec *
5163
 
                   (1.0 +
5164
 
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
5165
 
                     (double) table->s->max_key_length)));
5166
 
                if (records < 2.0)
5167
 
                  records=2.0;               /* Can't be as good as a unique */
5168
 
              }
5169
 
              /*
5170
 
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
5171
 
                E(#rows) from range optimizer. Make another try:
5172
 
 
5173
 
                If range optimizer produced E(#rows) for a prefix of the ref
5174
 
                access we're considering, and that E(#rows) is lower then our
5175
 
                current estimate, make an adjustment. The criteria of when we
5176
 
                can make an adjustment is a special case of the criteria used
5177
 
                in ReuseRangeEstimateForRef-3.
5178
 
              */
5179
 
              if (table->quick_keys.is_set(key) &&
5180
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5181
 
                  table->quick_n_ranges[key] == 1 &&
5182
 
                  records > (double) table->quick_rows[key])
5183
 
              {
5184
 
                records= (double) table->quick_rows[key];
5185
 
              }
5186
 
            }
5187
 
            /* Limit the number of matched rows */
5188
 
            tmp= records;
5189
 
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5190
 
            if (table->covering_keys.is_set(key))
5191
 
            {
5192
 
              /* we can use only index tree */
5193
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5194
 
            }
5195
 
            else
5196
 
              tmp= record_count*cmin(tmp,s->worst_seeks);
5197
 
          }
5198
 
        }
5199
 
        else
5200
 
        {
5201
 
          /*
5202
 
            Use as much key-parts as possible and a uniq key is better
5203
 
            than a not unique key
5204
 
            Set tmp to (previous record count) * (records / combination)
5205
 
          */
5206
 
          if ((found_part & 1) &&
5207
 
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5208
 
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
5209
 
          {
5210
 
            max_key_part= max_part_bit(found_part);
5211
 
            /*
5212
 
              ReuseRangeEstimateForRef-3:
5213
 
              We're now considering a ref[or_null] access via
5214
 
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5215
 
              (same-as-above but with one cond replaced
5216
 
               with "t.keypart_i IS NULL")]  (**)
5217
 
 
5218
 
              Try re-using E(#rows) from "range" optimizer:
5219
 
              We can do so if "range" optimizer used the same intervals as
5220
 
              in (**). The intervals used by range optimizer may be not
5221
 
              available at this point (as "range" access might have choosen to
5222
 
              create quick select over another index), so we can't compare
5223
 
              them to (**). We'll make indirect judgements instead.
5224
 
              The sufficient conditions for re-use are:
5225
 
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5226
 
                   this is not satisfied we have no way to know which ranges
5227
 
                   will be actually scanned by 'ref' until we execute the
5228
 
                   join)
5229
 
              (C2) max #key parts in 'range' access == K == max_key_part (this
5230
 
                   is apparently a necessary requirement)
5231
 
 
5232
 
              We also have a property that "range optimizer produces equal or
5233
 
              tighter set of scan intervals than ref(const) optimizer". Each
5234
 
              of the intervals in (**) are "tightest possible" intervals when
5235
 
              one limits itself to using keyparts 1..K (which we do in #2).
5236
 
              From here it follows that range access used either one, or
5237
 
              both of the (I1) and (I2) intervals:
5238
 
 
5239
 
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
5240
 
               (same-as-above but with one cond replaced
5241
 
                with "t.keypart_i IS NULL")               (I2)
5242
 
 
5243
 
              The remaining part is to exclude the situation where range
5244
 
              optimizer used one interval while we're considering
5245
 
              ref-or-null and looking for estimate for two intervals. This
5246
 
              is done by last limitation:
5247
 
 
5248
 
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5249
 
            */
5250
 
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5251
 
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5252
 
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
5253
 
            {
5254
 
              tmp= records= (double) table->quick_rows[key];
5255
 
            }
5256
 
            else
5257
 
            {
5258
 
              /* Check if we have statistic about the distribution */
5259
 
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
5260
 
              {
5261
 
                /*
5262
 
                  Fix for the case where the index statistics is too
5263
 
                  optimistic: If
5264
 
                  (1) We're considering ref(const) and there is quick select
5265
 
                      on the same index,
5266
 
                  (2) and that quick select uses more keyparts (i.e. it will
5267
 
                      scan equal/smaller interval then this ref(const))
5268
 
                  (3) and E(#rows) for quick select is higher then our
5269
 
                      estimate,
5270
 
                  Then
5271
 
                    We'll use E(#rows) from quick select.
5272
 
 
5273
 
                  Q: Why do we choose to use 'ref'? Won't quick select be
5274
 
                  cheaper in some cases ?
5275
 
                  TODO: figure this out and adjust the plan choice if needed.
5276
 
                */
5277
 
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
5278
 
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5279
 
                    records < (double)table->quick_rows[key])         // (3)
5280
 
                  records= (double)table->quick_rows[key];
5281
 
 
5282
 
                tmp= records;
5283
 
              }
5284
 
              else
5285
 
              {
5286
 
                /*
5287
 
                  Assume that the first key part matches 1% of the file
5288
 
                  and that the whole key matches 10 (duplicates) or 1
5289
 
                  (unique) records.
5290
 
                  Assume also that more key matches proportionally more
5291
 
                  records
5292
 
                  This gives the formula:
5293
 
                  records = (x * (b-a) + a*c-b)/(c-1)
5294
 
 
5295
 
                  b = records matched by whole key
5296
 
                  a = records matched by first key part (1% of all records?)
5297
 
                  c = number of key parts in key
5298
 
                  x = used key parts (1 <= x <= c)
5299
 
                */
5300
 
                double rec_per_key;
5301
 
                if (!(rec_per_key=(double)
5302
 
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
5303
 
                  rec_per_key=(double) s->records/rec+1;
5304
 
 
5305
 
                if (!s->records)
5306
 
                  tmp = 0;
5307
 
                else if (rec_per_key/(double) s->records >= 0.01)
5308
 
                  tmp = rec_per_key;
5309
 
                else
5310
 
                {
5311
 
                  double a=s->records*0.01;
5312
 
                  if (keyinfo->key_parts > 1)
5313
 
                    tmp= (max_key_part * (rec_per_key - a) +
5314
 
                          a*keyinfo->key_parts - rec_per_key)/
5315
 
                         (keyinfo->key_parts-1);
5316
 
                  else
5317
 
                    tmp= a;
5318
 
                  set_if_bigger(tmp,1.0);
5319
 
                }
5320
 
                records = (uint32_t) tmp;
5321
 
              }
5322
 
 
5323
 
              if (ref_or_null_part)
5324
 
              {
5325
 
                /* We need to do two key searches to find key */
5326
 
                tmp *= 2.0;
5327
 
                records *= 2.0;
5328
 
              }
5329
 
 
5330
 
              /*
5331
 
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
5332
 
                E(#rows) from range optimizer. Make another try:
5333
 
 
5334
 
                If range optimizer produced E(#rows) for a prefix of the ref
5335
 
                access we're considering, and that E(#rows) is lower then our
5336
 
                current estimate, make the adjustment.
5337
 
 
5338
 
                The decision whether we can re-use the estimate from the range
5339
 
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5340
 
                applied to first table->quick_key_parts[key] key parts.
5341
 
              */
5342
 
              if (table->quick_keys.is_set(key) &&
5343
 
                  table->quick_key_parts[key] <= max_key_part &&
5344
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5345
 
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5346
 
                                                     const_part) ? 1 : 0) &&
5347
 
                  records > (double) table->quick_rows[key])
5348
 
              {
5349
 
                tmp= records= (double) table->quick_rows[key];
5350
 
              }
5351
 
            }
5352
 
 
5353
 
            /* Limit the number of matched rows */
5354
 
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5355
 
            if (table->covering_keys.is_set(key))
5356
 
            {
5357
 
              /* we can use only index tree */
5358
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5359
 
            }
5360
 
            else
5361
 
              tmp= record_count * cmin(tmp,s->worst_seeks);
5362
 
          }
5363
 
          else
5364
 
            tmp= best_time;                    // Do nothing
5365
 
        }
5366
 
 
5367
 
        if (sj_inside_out_scan && !start_key)
5368
 
        {
5369
 
          tmp= tmp/2;
5370
 
          if (records)
5371
 
            records= records/2;
5372
 
        }
5373
 
 
5374
 
      }
5375
 
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5376
 
      {
5377
 
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
5378
 
        best= tmp;
5379
 
        best_records= records;
5380
 
        best_key= start_key;
5381
 
        best_max_key_part= max_key_part;
5382
 
        best_ref_depends_map= found_ref;
5383
 
        best_is_sj_inside_out= sj_inside_out_scan;
5384
 
      }
5385
 
    }
5386
 
    records= best_records;
5387
 
  }
5388
 
 
5389
 
  /*
5390
 
    Don't test table scan if it can't be better.
5391
 
    Prefer key lookup if we would use the same key for scanning.
5392
 
 
5393
 
    Don't do a table scan on InnoDB tables, if we can read the used
5394
 
    parts of the row from any of the used index.
5395
 
    This is because table scans uses index and we would not win
5396
 
    anything by using a table scan.
5397
 
 
5398
 
    A word for word translation of the below if-statement in sergefp's
5399
 
    understanding: we check if we should use table scan if:
5400
 
    (1) The found 'ref' access produces more records than a table scan
5401
 
        (or index scan, or quick select), or 'ref' is more expensive than
5402
 
        any of them.
5403
 
    (2) This doesn't hold: the best way to perform table scan is to to perform
5404
 
        'range' access using index IDX, and the best way to perform 'ref'
5405
 
        access is to use the same index IDX, with the same or more key parts.
5406
 
        (note: it is not clear how this rule is/should be extended to
5407
 
        index_merge quick selects)
5408
 
    (3) See above note about InnoDB.
5409
 
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5410
 
             path, but there is no quick select)
5411
 
        If the condition in the above brackets holds, then the only possible
5412
 
        "table scan" access method is ALL/index (there is no quick select).
5413
 
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
5414
 
        choose it over ALL/index, there is no need to consider a full table
5415
 
        scan.
5416
 
  */
5417
 
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
5418
 
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5419
 
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5420
 
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5421
 
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5422
 
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5423
 
  {                                             // Check full join
5424
 
    ha_rows rnd_records= s->found_records;
5425
 
    /*
5426
 
      If there is a filtering condition on the table (i.e. ref analyzer found
5427
 
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5428
 
      preceding this table in the join order we're now considering), then
5429
 
      assume that 25% of the rows will be filtered out by this condition.
5430
 
 
5431
 
      This heuristic is supposed to force tables used in exprZ to be before
5432
 
      this table in join order.
5433
 
    */
5434
 
    if (found_constraint)
5435
 
      rnd_records-= rnd_records/4;
5436
 
 
5437
 
    /*
5438
 
      If applicable, get a more accurate estimate. Don't use the two
5439
 
      heuristics at once.
5440
 
    */
5441
 
    if (s->table->quick_condition_rows != s->found_records)
5442
 
      rnd_records= s->table->quick_condition_rows;
5443
 
 
5444
 
    /*
5445
 
      Range optimizer never proposes a RANGE if it isn't better
5446
 
      than FULL: so if RANGE is present, it's always preferred to FULL.
5447
 
      Here we estimate its cost.
5448
 
    */
5449
 
    if (s->quick)
5450
 
    {
5451
 
      /*
5452
 
        For each record we:
5453
 
        - read record range through 'quick'
5454
 
        - skip rows which does not satisfy WHERE constraints
5455
 
        TODO:
5456
 
        We take into account possible use of join cache for ALL/index
5457
 
        access (see first else-branch below), but we don't take it into
5458
 
        account here for range/index_merge access. Find out why this is so.
5459
 
      */
5460
 
      tmp= record_count *
5461
 
        (s->quick->read_time +
5462
 
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5463
 
    }
5464
 
    else
5465
 
    {
5466
 
      /* Estimate cost of reading table. */
5467
 
      tmp= s->table->file->scan_time();
5468
 
      if (s->table->map & join->outer_join)     // Can't use join cache
5469
 
      {
5470
 
        /*
5471
 
          For each record we have to:
5472
 
          - read the whole table record
5473
 
          - skip rows which does not satisfy join condition
5474
 
        */
5475
 
        tmp= record_count *
5476
 
          (tmp +
5477
 
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5478
 
      }
5479
 
      else
5480
 
      {
5481
 
        /* We read the table as many times as join buffer becomes full. */
5482
 
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5483
 
                           record_count /
5484
 
                           (double) session->variables.join_buff_size));
5485
 
        /*
5486
 
            We don't make full cartesian product between rows in the scanned
5487
 
           table and existing records because we skip all rows from the
5488
 
           scanned table, which does not satisfy join condition when
5489
 
           we read the table (see flush_cached_records for details). Here we
5490
 
           take into account cost to read and skip these records.
5491
 
        */
5492
 
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5493
 
      }
5494
 
    }
5495
 
 
5496
 
    /*
5497
 
      We estimate the cost of evaluating WHERE clause for found records
5498
 
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5499
 
      tmp give us total cost of using Table SCAN
5500
 
    */
5501
 
    if (best == DBL_MAX ||
5502
 
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5503
 
         best + record_count/(double) TIME_FOR_COMPARE*records))
5504
 
    {
5505
 
      /*
5506
 
        If the table has a range (s->quick is set) make_join_select()
5507
 
        will ensure that this will be used
5508
 
      */
5509
 
      best= tmp;
5510
 
      records= rows2double(rnd_records);
5511
 
      best_key= 0;
5512
 
      /* range/index_merge/ALL/index access method are "independent", so: */
5513
 
      best_ref_depends_map= 0;
5514
 
      best_is_sj_inside_out= false;
5515
 
    }
5516
 
  }
5517
 
 
5518
 
  /* Update the cost information for the current partial plan */
5519
 
  join->positions[idx].records_read= records;
5520
 
  join->positions[idx].read_time=    best;
5521
 
  join->positions[idx].key=          best_key;
5522
 
  join->positions[idx].table=        s;
5523
 
  join->positions[idx].ref_depend_map= best_ref_depends_map;
5524
 
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5525
 
 
5526
 
  if (!best_key &&
5527
 
      idx == join->const_tables &&
5528
 
      s->table == join->sort_by_table &&
5529
 
      join->unit->select_limit_cnt >= records)
5530
 
    join->sort_by_table= (Table*) 1;  // Must use temporary table
5531
 
 
5532
 
  return;
5533
 
}
5534
 
 
5535
 
 
5536
 
/**
5537
 
  Selects and invokes a search strategy for an optimal query plan.
5538
 
 
5539
 
  The function checks user-configurable parameters that control the search
5540
 
  strategy for an optimal plan, selects the search method and then invokes
5541
 
  it. Each specific optimization procedure stores the final optimal plan in
5542
 
  the array 'join->best_positions', and the cost of the plan in
5543
 
  'join->best_read'.
5544
 
 
5545
 
  @param join         pointer to the structure providing all context info for
5546
 
                      the query
5547
 
  @param join_tables  set of the tables in the query
5548
 
 
5549
 
  @todo
5550
 
    'MAX_TABLES+2' denotes the old implementation of find_best before
5551
 
    the greedy version. Will be removed when greedy_search is approved.
5552
 
 
5553
 
  @retval
5554
 
    false       ok
5555
 
  @retval
5556
 
    true        Fatal error
5557
 
*/
5558
 
 
5559
 
static bool
5560
 
choose_plan(JOIN *join, table_map join_tables)
5561
 
{
5562
 
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
5563
 
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
5564
 
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5565
 
 
5566
 
  join->cur_embedding_map= 0;
5567
 
  reset_nj_counters(join->join_list);
5568
 
  /*
5569
 
    if (SELECT_STRAIGHT_JOIN option is set)
5570
 
      reorder tables so dependent tables come after tables they depend
5571
 
      on, otherwise keep tables in the order they were specified in the query
5572
 
    else
5573
 
      Apply heuristic: pre-sort all access plans with respect to the number of
5574
 
      records accessed.
5575
 
  */
5576
 
  my_qsort(join->best_ref + join->const_tables,
5577
 
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
5578
 
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
5579
 
  join->cur_emb_sj_nests= 0;
5580
 
  if (straight_join)
5581
 
  {
5582
 
    optimize_straight_join(join, join_tables);
5583
 
  }
5584
 
  else
5585
 
  {
5586
 
    if (search_depth == MAX_TABLES+2)
5587
 
    { /*
5588
 
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5589
 
        the greedy version. Will be removed when greedy_search is approved.
5590
 
      */
5591
 
      join->best_read= DBL_MAX;
5592
 
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5593
 
        return(true);
5594
 
    }
5595
 
    else
5596
 
    {
5597
 
      if (search_depth == 0)
5598
 
        /* Automatically determine a reasonable value for 'search_depth' */
5599
 
        search_depth= determine_search_depth(join);
5600
 
      if (greedy_search(join, join_tables, search_depth, prune_level))
5601
 
        return(true);
5602
 
    }
5603
 
  }
5604
 
 
5605
 
  /*
5606
 
    Store the cost of this query into a user variable
5607
 
    Don't update last_query_cost for statements that are not "flat joins" :
5608
 
    i.e. they have subqueries, unions or call stored procedures.
5609
 
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
5610
 
  */
5611
 
  if (join->session->lex->is_single_level_stmt())
5612
 
    join->session->status_var.last_query_cost= join->best_read;
5613
 
  return(false);
5614
 
}
5615
 
 
5616
 
 
5617
 
/**
5618
 
  Compare two JOIN_TAB objects based on the number of accessed records.
5619
 
 
5620
 
  @param ptr1 pointer to first JOIN_TAB object
5621
 
  @param ptr2 pointer to second JOIN_TAB object
 
812
    possible_keys&= cur_item->field->part_of_key;
 
813
  }
 
814
 
 
815
  if (possible_keys.any())
 
816
    join_tab->const_keys|= possible_keys;
 
817
}
 
818
 
 
819
/**
 
820
  Compare two JoinTable objects based on the number of accessed records.
 
821
 
 
822
  @param ptr1 pointer to first JoinTable object
 
823
  @param ptr2 pointer to second JoinTable object
5622
824
 
5623
825
  NOTES
5624
826
    The order relation implemented by join_tab_cmp() is not transitive,
5638
840
  @retval
5639
841
    0  if equal
5640
842
*/
5641
 
 
5642
 
static int
5643
 
join_tab_cmp(const void* ptr1, const void* ptr2)
 
843
int join_tab_cmp(const void* ptr1, const void* ptr2)
5644
844
{
5645
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5646
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
845
  JoinTable *jt1= *(JoinTable**) ptr1;
 
846
  JoinTable *jt2= *(JoinTable**) ptr2;
5647
847
 
5648
848
  if (jt1->dependent & jt2->table->map)
5649
849
    return 1;
 
850
 
5650
851
  if (jt2->dependent & jt1->table->map)
5651
852
    return -1;
 
853
 
5652
854
  if (jt1->found_records > jt2->found_records)
5653
855
    return 1;
 
856
 
5654
857
  if (jt1->found_records < jt2->found_records)
5655
858
    return -1;
 
859
 
5656
860
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5657
861
}
5658
862
 
5659
 
 
5660
863
/**
5661
864
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5662
865
*/
5663
 
 
5664
 
static int
5665
 
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
866
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5666
867
{
5667
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5668
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
868
  JoinTable *jt1= *(JoinTable**) ptr1;
 
869
  JoinTable *jt2= *(JoinTable**) ptr2;
5669
870
 
5670
871
  if (jt1->dependent & jt2->table->map)
5671
872
    return 1;
 
873
 
5672
874
  if (jt2->dependent & jt1->table->map)
5673
875
    return -1;
 
876
 
5674
877
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5675
878
}
5676
879
 
5677
880
/**
5678
 
  Heuristic procedure to automatically guess a reasonable degree of
5679
 
  exhaustiveness for the greedy search procedure.
5680
 
 
5681
 
  The procedure estimates the optimization time and selects a search depth
5682
 
  big enough to result in a near-optimal QEP, that doesn't take too long to
5683
 
  find. If the number of tables in the query exceeds some constant, then
5684
 
  search_depth is set to this constant.
5685
 
 
5686
 
  @param join   pointer to the structure providing all context info for
5687
 
                the query
5688
 
 
5689
 
  @note
5690
 
    This is an extremely simplistic implementation that serves as a stub for a
5691
 
    more advanced analysis of the join. Ideally the search depth should be
5692
 
    determined by learning from previous query optimizations, because it will
5693
 
    depend on the CPU power (and other factors).
5694
 
 
5695
 
  @todo
5696
 
    this value should be determined dynamically, based on statistics:
5697
 
    uint32_t max_tables_for_exhaustive_opt= 7;
5698
 
 
5699
 
  @todo
5700
 
    this value could be determined by some mapping of the form:
5701
 
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5702
 
 
5703
 
  @return
5704
 
    A positive integer that specifies the search depth (and thus the
5705
 
    exhaustiveness) of the depth-first search algorithm used by
5706
 
    'greedy_search'.
5707
 
*/
5708
 
 
5709
 
static uint
5710
 
determine_search_depth(JOIN *join)
5711
 
{
5712
 
  uint32_t table_count=  join->tables - join->const_tables;
5713
 
  uint32_t search_depth;
5714
 
  /* TODO: this value should be determined dynamically, based on statistics: */
5715
 
  uint32_t max_tables_for_exhaustive_opt= 7;
5716
 
 
5717
 
  if (table_count <= max_tables_for_exhaustive_opt)
5718
 
    search_depth= table_count+1; // use exhaustive for small number of tables
5719
 
  else
5720
 
    /*
5721
 
      TODO: this value could be determined by some mapping of the form:
5722
 
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5723
 
    */
5724
 
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
5725
 
 
5726
 
  return search_depth;
5727
 
}
5728
 
 
5729
 
 
5730
 
/**
5731
 
  Select the best ways to access the tables in a query without reordering them.
5732
 
 
5733
 
    Find the best access paths for each query table and compute their costs
5734
 
    according to their order in the array 'join->best_ref' (thus without
5735
 
    reordering the join tables). The function calls sequentially
5736
 
    'best_access_path' for each table in the query to select the best table
5737
 
    access method. The final optimal plan is stored in the array
5738
 
    'join->best_positions', and the corresponding cost in 'join->best_read'.
5739
 
 
5740
 
  @param join          pointer to the structure providing all context info for
5741
 
                       the query
5742
 
  @param join_tables   set of the tables in the query
5743
 
 
5744
 
  @note
5745
 
    This function can be applied to:
5746
 
    - queries with STRAIGHT_JOIN
5747
 
    - internally to compute the cost of an arbitrary QEP
5748
 
  @par
5749
 
    Thus 'optimize_straight_join' can be used at any stage of the query
5750
 
    optimization process to finalize a QEP as it is.
5751
 
*/
5752
 
 
5753
 
static void
5754
 
optimize_straight_join(JOIN *join, table_map join_tables)
5755
 
{
5756
 
  JOIN_TAB *s;
5757
 
  uint32_t idx= join->const_tables;
5758
 
  double    record_count= 1.0;
5759
 
  double    read_time=    0.0;
5760
 
 
5761
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5762
 
  {
5763
 
    /* Find the best access method from 's' to the current partial plan */
5764
 
    advance_sj_state(join_tables, s);
5765
 
    best_access_path(join, s, join->session, join_tables, idx,
5766
 
                     record_count, read_time);
5767
 
    /* compute the cost of the new plan extended with 's' */
5768
 
    record_count*= join->positions[idx].records_read;
5769
 
    read_time+=    join->positions[idx].read_time;
5770
 
    join_tables&= ~(s->table->map);
5771
 
    ++idx;
5772
 
  }
5773
 
 
5774
 
  read_time+= record_count / (double) TIME_FOR_COMPARE;
5775
 
  if (join->sort_by_table &&
5776
 
      join->sort_by_table != join->positions[join->const_tables].table->table)
5777
 
    read_time+= record_count;  // We have to make a temp table
5778
 
  memcpy(join->best_positions, join->positions, 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
 
              uint32_t      search_depth,
5868
 
              uint32_t      prune_level)
5869
 
{
5870
 
  double    record_count= 1.0;
5871
 
  double    read_time=    0.0;
5872
 
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
5873
 
  uint32_t      best_idx;
5874
 
  uint32_t      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
 
    std::swap(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
 
                                 uint32_t      idx,
6048
 
                                 double    record_count,
6049
 
                                 double    read_time,
6050
 
                                 uint32_t      search_depth,
6051
 
                                 uint32_t      prune_level)
6052
 
{
6053
 
  Session *session= join->session;
6054
 
  if (session->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, session, 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
 
        std::swap(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
 
        std::swap(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(join->best_positions, 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,uint32_t idx,double record_count,
6176
 
          double read_time)
6177
 
{
6178
 
  Session *session= join->session;
6179
 
  if (session->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(join->best_positions, join->positions, sizeof(POSITION)*idx);
6191
 
      join->best_read= read_time - 0.001;
6192
 
    }
6193
 
    return(false);
6194
 
  }
6195
 
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6196
 
    return(false);                                      /* Found better before */
6197
 
 
6198
 
  JOIN_TAB *s;
6199
 
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6200
 
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6201
 
  {
6202
 
    table_map real_table_bit=s->table->map;
6203
 
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6204
 
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6205
 
    {
6206
 
      double records, best;
6207
 
      advance_sj_state(rest_tables, s);
6208
 
      best_access_path(join, s, session, rest_tables, idx, record_count,
6209
 
                       read_time);
6210
 
      records= join->positions[idx].records_read;
6211
 
      best= join->positions[idx].read_time;
6212
 
      /*
6213
 
        Go to the next level only if there hasn't been a better key on
6214
 
        this level! This will cut down the search for a lot simple cases!
6215
 
      */
6216
 
      double current_record_count=record_count*records;
6217
 
      double current_read_time=read_time+best;
6218
 
      if (best_record_count > current_record_count ||
6219
 
          best_read_time > current_read_time ||
6220
 
          (idx == join->const_tables && s->table == join->sort_by_table))
6221
 
      {
6222
 
        if (best_record_count >= current_record_count &&
6223
 
            best_read_time >= current_read_time &&
6224
 
            (!(s->key_dependent & rest_tables) || records < 2.0))
6225
 
        {
6226
 
          best_record_count=current_record_count;
6227
 
          best_read_time=current_read_time;
6228
 
        }
6229
 
        std::swap(join->best_ref[idx], *pos);
6230
 
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6231
 
                      current_record_count,current_read_time))
6232
 
          return(true);
6233
 
        std::swap(join->best_ref[idx], *pos);
6234
 
      }
6235
 
      restore_prev_nj_state(s);
6236
 
      restore_prev_sj_state(rest_tables, s);
6237
 
      if (join->select_options & SELECT_STRAIGHT_JOIN)
6238
 
        break;                          // Don't test all combinations
6239
 
    }
6240
 
  }
6241
 
  return(false);
6242
 
}
6243
 
 
6244
 
 
6245
 
/**
6246
881
  Find how much space the prevous read not const tables takes in cache.
6247
882
*/
6248
 
 
6249
 
static void calc_used_field_length(Session *, JOIN_TAB *join_tab)
 
883
void calc_used_field_length(Session *, JoinTable *join_tab)
6250
884
{
6251
885
  uint32_t null_fields,blobs,fields,rec_length;
6252
886
  Field **f_ptr,*field;
6253
 
  MY_BITMAP *read_set= join_tab->table->read_set;;
6254
887
 
6255
888
  null_fields= blobs= fields= rec_length=0;
6256
 
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
 
889
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
6257
890
  {
6258
 
    if (bitmap_is_set(read_set, field->field_index))
 
891
    if (field->isReadSet())
6259
892
    {
6260
893
      uint32_t flags=field->flags;
6261
894
      fields++;
6262
895
      rec_length+=field->pack_length();
 
896
 
6263
897
      if (flags & BLOB_FLAG)
6264
 
        blobs++;
 
898
      {
 
899
        blobs++;
 
900
      }
 
901
 
6265
902
      if (!(flags & NOT_NULL_FLAG))
6266
 
        null_fields++;
 
903
      {
 
904
        null_fields++;
 
905
      }
6267
906
    }
6268
907
  }
 
908
 
6269
909
  if (null_fields)
 
910
  {
6270
911
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
912
  }
 
913
 
6271
914
  if (join_tab->table->maybe_null)
 
915
  {
6272
916
    rec_length+=sizeof(bool);
 
917
  }
 
918
 
6273
919
  if (blobs)
6274
920
  {
6275
 
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6276
 
                             (join_tab->table->getRecordLength()- rec_length));
6277
 
    rec_length+=(uint) cmax((uint)4,blob_length);
6278
 
  }
6279
 
  join_tab->used_fields=fields;
6280
 
  join_tab->used_fieldlength=rec_length;
6281
 
  join_tab->used_blobs=blobs;
6282
 
}
6283
 
 
6284
 
 
6285
 
static uint
6286
 
cache_record_length(JOIN *join,uint32_t idx)
6287
 
{
6288
 
  uint32_t length=0;
6289
 
  JOIN_TAB **pos,**end;
6290
 
  Session *session=join->session;
6291
 
 
6292
 
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6293
 
       pos != end ;
6294
 
       pos++)
6295
 
  {
6296
 
    JOIN_TAB *join_tab= *pos;
6297
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
6298
 
      calc_used_field_length(session, join_tab);
6299
 
    length+=join_tab->used_fieldlength;
6300
 
  }
6301
 
  return length;
6302
 
}
6303
 
 
6304
 
 
6305
 
/*
6306
 
  Get the number of different row combinations for subset of partial join
6307
 
 
6308
 
  SYNOPSIS
6309
 
    prev_record_reads()
6310
 
      join       The join structure
6311
 
      idx        Number of tables in the partial join order (i.e. the
6312
 
                 partial join order is in join->positions[0..idx-1])
6313
 
      found_ref  Bitmap of tables for which we need to find # of distinct
6314
 
                 row combinations.
6315
 
 
6316
 
  DESCRIPTION
6317
 
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
6318
 
    tables within that join order (specified in found_ref), find out how many
6319
 
    distinct row combinations of subset tables will be in the result of the
6320
 
    partial join order.
6321
 
 
6322
 
    This is used as follows: Suppose we have a table accessed with a ref-based
6323
 
    method. The ref access depends on current rows of tables in found_ref.
6324
 
    We want to count # of different ref accesses. We assume two ref accesses
6325
 
    will be different if at least one of access parameters is different.
6326
 
    Example: consider a query
6327
 
 
6328
 
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6329
 
 
6330
 
    and a join order:
6331
 
      t1,  ref access on t1.key=c1
6332
 
      t2,  ref access on t2.key=c2
6333
 
      t3,  ref access on t3.key=t1.field
6334
 
 
6335
 
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6336
 
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6337
 
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
6338
 
            n_distinct_ref_scans = #records_read(t1)
6339
 
 
6340
 
    The reason for having this function (at least the latest version of it)
6341
 
    is that we need to account for buffering in join execution.
6342
 
 
6343
 
    An edge-case example: if we have a non-first table in join accessed via
6344
 
    ref(const) or ref(param) where there is a small number of different
6345
 
    values of param, then the access will likely hit the disk cache and will
6346
 
    not require any disk seeks.
6347
 
 
6348
 
    The proper solution would be to assume an LRU disk cache of some size,
6349
 
    calculate probability of cache hits, etc. For now we just count
6350
 
    identical ref accesses as one.
6351
 
 
6352
 
  RETURN
6353
 
    Expected number of row combinations
6354
 
*/
6355
 
 
6356
 
static double
6357
 
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
6358
 
{
6359
 
  double found=1.0;
6360
 
  POSITION *pos_end= join->positions - 1;
6361
 
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6362
 
  {
6363
 
    if (pos->table->table->map & found_ref)
6364
 
    {
6365
 
      found_ref|= pos->ref_depend_map;
6366
 
      /*
6367
 
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6368
 
        with no matching row we will get position[t2].records_read==0.
6369
 
        Actually the size of output is one null-complemented row, therefore
6370
 
        we will use value of 1 whenever we get records_read==0.
6371
 
 
6372
 
        Note
6373
 
        - the above case can't occur if inner part of outer join has more
6374
 
          than one table: table with no matches will not be marked as const.
6375
 
 
6376
 
        - Ideally we should add 1 to records_read for every possible null-
6377
 
          complemented row. We're not doing it because: 1. it will require
6378
 
          non-trivial code and add overhead. 2. The value of records_read
6379
 
          is an inprecise estimate and adding 1 (or, in the worst case,
6380
 
          #max_nested_outer_joins=64-1) will not make it any more precise.
6381
 
      */
6382
 
      if (pos->records_read > DBL_EPSILON)
6383
 
        found*= pos->records_read;
6384
 
    }
6385
 
  }
6386
 
  return found;
6387
 
}
6388
 
 
 
921
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
 
922
                                     (join_tab->table->getRecordLength()- rec_length));
 
923
    rec_length+= max((uint32_t)4,blob_length);
 
924
  }
 
925
  join_tab->used_fields= fields;
 
926
  join_tab->used_fieldlength= rec_length;
 
927
  join_tab->used_blobs= blobs;
 
928
}
 
929
 
 
930
StoredKey *get_store_key(Session *session,
 
931
                         optimizer::KeyUse *keyuse,
 
932
                         table_map used_tables,
 
933
                         KeyPartInfo *key_part,
 
934
                         unsigned char *key_buff,
 
935
                         uint32_t maybe_null)
 
936
{
 
937
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
 
938
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
 
939
  {
 
940
    return new store_key_const_item(session,
 
941
                                    key_part->field,
 
942
                                    key_buff + maybe_null,
 
943
                                    maybe_null ? key_buff : 0,
 
944
                                    key_part->length,
 
945
                                    key_use_val);
 
946
  }
 
947
  else if (key_use_val->type() == Item::FIELD_ITEM ||
 
948
           (key_use_val->type() == Item::REF_ITEM &&
 
949
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
 
950
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
 
951
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
 
952
  {
 
953
    return new store_key_field(session,
 
954
                               key_part->field,
 
955
                               key_buff + maybe_null,
 
956
                               maybe_null ? key_buff : 0,
 
957
                               key_part->length,
 
958
                               ((Item_field*) key_use_val->real_item())->field,
 
959
                               key_use_val->full_name());
 
960
  }
 
961
  return new store_key_item(session,
 
962
                            key_part->field,
 
963
                            key_buff + maybe_null,
 
964
                            maybe_null ? key_buff : 0,
 
965
                            key_part->length,
 
966
                            key_use_val);
 
967
}
6389
968
 
6390
969
/**
6391
 
  Set up join struct according to best position.
 
970
  This function is only called for const items on fields which are keys.
 
971
 
 
972
  @return
 
973
    returns 1 if there was some conversion made when the field was stored.
6392
974
*/
6393
 
 
6394
 
static bool
6395
 
get_best_combination(JOIN *join)
6396
 
{
6397
 
  uint32_t i,tablenr;
6398
 
  table_map used_tables;
6399
 
  JOIN_TAB *join_tab,*j;
6400
 
  KEYUSE *keyuse;
6401
 
  uint32_t table_count;
6402
 
  Session *session=join->session;
6403
 
 
6404
 
  table_count=join->tables;
6405
 
  if (!(join->join_tab=join_tab=
6406
 
        (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
6407
 
    return(true);
6408
 
 
6409
 
  join->full_join=0;
6410
 
 
6411
 
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6412
 
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
6413
 
  {
6414
 
    Table *form;
6415
 
    *j= *join->best_positions[tablenr].table;
6416
 
    form=join->table[tablenr]=j->table;
6417
 
    used_tables|= form->map;
6418
 
    form->reginfo.join_tab=j;
6419
 
    if (!*j->on_expr_ref)
6420
 
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
6421
 
    if (j->type == JT_CONST)
6422
 
      continue;                                 // Handled in make_join_stat..
6423
 
 
6424
 
    j->ref.key = -1;
6425
 
    j->ref.key_parts=0;
6426
 
 
6427
 
    if (j->type == JT_SYSTEM)
6428
 
      continue;
6429
 
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
6430
 
    {
6431
 
      j->type=JT_ALL;
6432
 
      if (tablenr != join->const_tables)
6433
 
        join->full_join=1;
6434
 
    }
6435
 
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6436
 
      return(true);                        // Something went wrong
6437
 
  }
6438
 
 
6439
 
  for (i=0 ; i < table_count ; i++)
6440
 
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6441
 
  update_depend_map(join);
6442
 
  return(0);
6443
 
}
6444
 
 
6445
 
 
6446
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6447
 
                               table_map used_tables)
6448
 
{
6449
 
  KEYUSE *keyuse=org_keyuse;
 
975
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
976
{
 
977
  bool error;
 
978
  Table *table= field->getTable();
 
979
  Session *session= table->in_use;
 
980
  ha_rows cuted_fields=session->cuted_fields;
 
981
 
 
982
  /*
 
983
    we should restore old value of count_cuted_fields because
 
984
    store_val_in_field can be called from insert_query
 
985
    with select_insert, which make count_cuted_fields= 1
 
986
   */
 
987
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
 
988
  session->count_cuted_fields= check_flag;
 
989
  error= item->save_in_field(field, 1);
 
990
  session->count_cuted_fields= old_count_cuted_fields;
 
991
  return error || cuted_fields != session->cuted_fields;
 
992
}
 
993
 
 
994
inline void add_cond_and_fix(Item **e1, Item *e2)
 
995
{
 
996
  if (*e1)
 
997
  {
 
998
    Item* res= new Item_cond_and(*e1, e2);
 
999
    *e1= res;
 
1000
    res->quick_fix_field();
 
1001
  }
 
1002
  else
 
1003
  {
 
1004
    *e1= e2;
 
1005
  }
 
1006
}
 
1007
 
 
1008
bool create_ref_for_key(Join *join,
 
1009
                        JoinTable *j,
 
1010
                        optimizer::KeyUse *org_keyuse,
 
1011
                        table_map used_tables)
 
1012
{
 
1013
  optimizer::KeyUse *keyuse= org_keyuse;
6450
1014
  Session  *session= join->session;
6451
 
  uint32_t keyparts,length,key;
6452
 
  Table *table;
6453
 
  KEY *keyinfo;
 
1015
  uint32_t keyparts;
 
1016
  uint32_t length;
 
1017
  uint32_t key;
 
1018
  Table *table= NULL;
 
1019
  KeyInfo *keyinfo= NULL;
6454
1020
 
6455
1021
  /*  Use best key from find_best */
6456
 
  table=j->table;
6457
 
  key=keyuse->key;
6458
 
  keyinfo=table->key_info+key;
 
1022
  table= j->table;
 
1023
  key= keyuse->getKey();
 
1024
  keyinfo= table->key_info + key;
6459
1025
 
6460
1026
  {
6461
 
    keyparts=length=0;
 
1027
    keyparts= length= 0;
6462
1028
    uint32_t found_part_ref_or_null= 0;
6463
1029
    /*
6464
1030
      Calculate length for the used key
6467
1033
    */
6468
1034
    do
6469
1035
    {
6470
 
      if (!(~used_tables & keyuse->used_tables))
 
1036
      if (! (~used_tables & keyuse->getUsedTables()))
6471
1037
      {
6472
 
        if (keyparts == keyuse->keypart &&
6473
 
            !(found_part_ref_or_null & keyuse->optimize))
6474
 
        {
6475
 
          keyparts++;
6476
 
          length+= keyinfo->key_part[keyuse->keypart].store_length;
6477
 
          found_part_ref_or_null|= keyuse->optimize;
6478
 
        }
 
1038
        if (keyparts == keyuse->getKeypart() &&
 
1039
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
 
1040
        {
 
1041
          keyparts++;
 
1042
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
 
1043
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
 
1044
        }
6479
1045
      }
6480
1046
      keyuse++;
6481
 
    } while (keyuse->table == table && keyuse->key == key);
 
1047
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
6482
1048
  }
6483
1049
 
6484
1050
  /* set up fieldref */
6486
1052
  j->ref.key_parts=keyparts;
6487
1053
  j->ref.key_length=length;
6488
1054
  j->ref.key=(int) key;
6489
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
6490
 
      !(j->ref.key_copy= (store_key**) session->alloc((sizeof(store_key*) *
6491
 
                                                   (keyparts+1)))) ||
6492
 
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
6493
 
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
6494
 
  {
6495
 
    return(true);
6496
 
  }
 
1055
  j->ref.key_buff= (unsigned char*) session->mem.calloc(ALIGN_SIZE(length)*2);
 
1056
  j->ref.key_copy= new (session->mem) StoredKey*[keyparts + 1];
 
1057
  j->ref.items= new (session->mem) Item*[keyparts];
 
1058
  j->ref.cond_guards= new (session->mem) bool*[keyparts];
6497
1059
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
6498
1060
  j->ref.key_err=1;
6499
1061
  j->ref.null_rejecting= 0;
6500
1062
  j->ref.disable_cache= false;
6501
1063
  keyuse=org_keyuse;
6502
1064
 
6503
 
  store_key **ref_key= j->ref.key_copy;
6504
 
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
1065
  StoredKey **ref_key= j->ref.key_copy;
 
1066
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
6505
1067
  bool keyuse_uses_no_tables= true;
6506
1068
  {
6507
 
    uint32_t i;
6508
 
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
1069
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
6509
1070
    {
6510
 
      while (keyuse->keypart != i ||
6511
 
             ((~used_tables) & keyuse->used_tables))
6512
 
        keyuse++;                               /* Skip other parts */
 
1071
      while (keyuse->getKeypart() != i or ((~used_tables) & keyuse->getUsedTables()))
 
1072
      {
 
1073
        keyuse++;       /* Skip other parts */
 
1074
      }
6513
1075
 
6514
1076
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
6515
 
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6516
 
      j->ref.cond_guards[i]= keyuse->cond_guard;
6517
 
      if (keyuse->null_rejecting)
 
1077
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
 
1078
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
 
1079
      if (keyuse->isNullRejected())
 
1080
      {
6518
1081
        j->ref.null_rejecting |= 1 << i;
6519
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6520
 
      if (!keyuse->used_tables &&
6521
 
          !(join->select_options & SELECT_DESCRIBE))
6522
 
      {                                 // Compare against constant
6523
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
1082
      }
 
1083
 
 
1084
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
 
1085
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
 
1086
      {         // Compare against constant
 
1087
        store_key_item tmp(session, keyinfo->key_part[i].field,
6524
1088
                           key_buff + maybe_null,
6525
1089
                           maybe_null ?  key_buff : 0,
6526
 
                           keyinfo->key_part[i].length, keyuse->val);
6527
 
        if (session->is_fatal_error)
6528
 
          return(true);
6529
 
        tmp.copy();
 
1090
                           keyinfo->key_part[i].length, keyuse->getVal());
 
1091
        if (session->is_fatal_error)
 
1092
        {
 
1093
          return true;
 
1094
        }
 
1095
        tmp.copy();
6530
1096
      }
6531
1097
      else
6532
 
        *ref_key++= get_store_key(session,
6533
 
                                  keyuse,join->const_table_map,
6534
 
                                  &keyinfo->key_part[i],
6535
 
                                  key_buff, maybe_null);
 
1098
      {
 
1099
        *ref_key++= get_store_key(session,
 
1100
                                  keyuse,join->const_table_map,
 
1101
                                  &keyinfo->key_part[i],
 
1102
                                  key_buff, maybe_null);
 
1103
      }
 
1104
 
6536
1105
      /*
6537
 
        Remember if we are going to use REF_OR_NULL
6538
 
        But only if field _really_ can be null i.e. we force JT_REF
6539
 
        instead of JT_REF_OR_NULL in case if field can't be null
 
1106
        Remember if we are going to use REF_OR_NULL
 
1107
        But only if field _really_ can be null i.e. we force AM_REF
 
1108
        instead of AM_REF_OR_NULL in case if field can't be null
6540
1109
      */
6541
 
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6542
 
        null_ref_key= key_buff;
 
1110
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
1111
        null_ref_key= key_buff;
6543
1112
      key_buff+=keyinfo->key_part[i].store_length;
6544
1113
    }
6545
1114
  }
6546
 
  *ref_key=0;                           // end_marker
6547
 
  if (j->type == JT_CONST)
 
1115
  *ref_key= 0;       // end_marker
 
1116
  if (j->type == AM_CONST)
 
1117
  {
6548
1118
    j->table->const_table= 1;
6549
 
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
6550
 
           keyparts != keyinfo->key_parts || null_ref_key)
 
1119
  }
 
1120
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) || keyparts != keyinfo->key_parts || null_ref_key)
6551
1121
  {
6552
1122
    /* Must read with repeat */
6553
 
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
1123
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
6554
1124
    j->ref.null_ref_key= null_ref_key;
6555
1125
  }
6556
1126
  else if (keyuse_uses_no_tables)
6562
1132
      Here we should not mark the table as a 'const' as a field may
6563
1133
      have a 'normal' value or a NULL value.
6564
1134
    */
6565
 
    j->type=JT_CONST;
6566
 
  }
6567
 
  else
6568
 
    j->type=JT_EQ_REF;
6569
 
  return(0);
6570
 
}
6571
 
 
6572
 
 
6573
 
 
6574
 
static store_key *
6575
 
get_store_key(Session *session, KEYUSE *keyuse, table_map used_tables,
6576
 
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
6577
 
{
6578
 
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6579
 
  {
6580
 
    return new store_key_const_item(session,
6581
 
                                    key_part->field,
6582
 
                                    key_buff + maybe_null,
6583
 
                                    maybe_null ? key_buff : 0,
6584
 
                                    key_part->length,
6585
 
                                    keyuse->val);
6586
 
  }
6587
 
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
6588
 
           (keyuse->val->type() == Item::REF_ITEM &&
6589
 
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6590
 
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6591
 
             Item_ref::DIRECT_REF &&
6592
 
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6593
 
    return new store_key_field(session,
6594
 
                               key_part->field,
6595
 
                               key_buff + maybe_null,
6596
 
                               maybe_null ? key_buff : 0,
6597
 
                               key_part->length,
6598
 
                               ((Item_field*) keyuse->val->real_item())->field,
6599
 
                               keyuse->val->full_name());
6600
 
  return new store_key_item(session,
6601
 
                            key_part->field,
6602
 
                            key_buff + maybe_null,
6603
 
                            maybe_null ? key_buff : 0,
6604
 
                            key_part->length,
6605
 
                            keyuse->val);
6606
 
}
6607
 
 
6608
 
/**
6609
 
  This function is only called for const items on fields which are keys.
6610
 
 
6611
 
  @return
6612
 
    returns 1 if there was some conversion made when the field was stored.
6613
 
*/
6614
 
 
6615
 
bool
6616
 
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6617
 
{
6618
 
  bool error;
6619
 
  Table *table= field->table;
6620
 
  Session *session= table->in_use;
6621
 
  ha_rows cuted_fields=session->cuted_fields;
6622
 
 
6623
 
  /*
6624
 
    we should restore old value of count_cuted_fields because
6625
 
    store_val_in_field can be called from mysql_insert
6626
 
    with select_insert, which make count_cuted_fields= 1
6627
 
   */
6628
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
6629
 
  session->count_cuted_fields= check_flag;
6630
 
  error= item->save_in_field(field, 1);
6631
 
  session->count_cuted_fields= old_count_cuted_fields;
6632
 
  return error || cuted_fields != session->cuted_fields;
6633
 
}
6634
 
 
6635
 
 
6636
 
static bool
6637
 
make_simple_join(JOIN *join,Table *tmp_table)
6638
 
{
6639
 
  Table **tableptr;
6640
 
  JOIN_TAB *join_tab;
6641
 
 
6642
 
  /*
6643
 
    Reuse Table * and JOIN_TAB if already allocated by a previous call
6644
 
    to this function through JOIN::exec (may happen for sub-queries).
6645
 
  */
6646
 
  if (!join->table_reexec)
6647
 
  {
6648
 
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
6649
 
      return(true);                        /* purecov: inspected */
6650
 
    if (join->tmp_join)
6651
 
      join->tmp_join->table_reexec= join->table_reexec;
6652
 
  }
6653
 
  if (!join->join_tab_reexec)
6654
 
  {
6655
 
    if (!(join->join_tab_reexec=
6656
 
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
6657
 
      return(true);                        /* purecov: inspected */
6658
 
    if (join->tmp_join)
6659
 
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6660
 
  }
6661
 
  tableptr= join->table_reexec;
6662
 
  join_tab= join->join_tab_reexec;
6663
 
 
6664
 
  join->join_tab=join_tab;
6665
 
  join->table=tableptr; tableptr[0]=tmp_table;
6666
 
  join->tables=1;
6667
 
  join->const_tables=0;
6668
 
  join->const_table_map=0;
6669
 
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6670
 
    join->tmp_table_param.func_count=0;
6671
 
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6672
 
  join->first_record=join->sort_and_group=0;
6673
 
  join->send_records=(ha_rows) 0;
6674
 
  join->group=0;
6675
 
  join->row_limit=join->unit->select_limit_cnt;
6676
 
  join->do_send_rows = (join->row_limit) ? 1 : 0;
6677
 
 
6678
 
  join_tab->cache.buff=0;                       /* No caching */
6679
 
  join_tab->table=tmp_table;
6680
 
  join_tab->select=0;
6681
 
  join_tab->select_cond=0;
6682
 
  join_tab->quick=0;
6683
 
  join_tab->type= JT_ALL;                       /* Map through all records */
6684
 
  join_tab->keys.init();
6685
 
  join_tab->keys.set_all();                     /* test everything in quick */
6686
 
  join_tab->info=0;
6687
 
  join_tab->on_expr_ref=0;
6688
 
  join_tab->last_inner= 0;
6689
 
  join_tab->first_unmatched= 0;
6690
 
  join_tab->ref.key = -1;
6691
 
  join_tab->not_used_in_distinct=0;
6692
 
  join_tab->read_first_record= join_init_read_record;
6693
 
  join_tab->join=join;
6694
 
  join_tab->ref.key_parts= 0;
6695
 
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6696
 
  join_tab->do_firstmatch= NULL;
6697
 
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
6698
 
  tmp_table->status=0;
6699
 
  tmp_table->null_row=0;
6700
 
  return(false);
6701
 
}
6702
 
 
6703
 
 
6704
 
inline void add_cond_and_fix(Item **e1, Item *e2)
6705
 
{
6706
 
  if (*e1)
6707
 
  {
6708
 
    Item *res;
6709
 
    if ((res= new Item_cond_and(*e1, e2)))
6710
 
    {
6711
 
      *e1= res;
6712
 
      res->quick_fix_field();
6713
 
    }
6714
 
  }
6715
 
  else
6716
 
    *e1= e2;
6717
 
}
6718
 
 
 
1135
    j->type= AM_CONST;
 
1136
  }
 
1137
  else
 
1138
  {
 
1139
    j->type= AM_EQ_REF;
 
1140
  }
 
1141
 
 
1142
  return 0;
 
1143
}
6719
1144
 
6720
1145
/**
6721
1146
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6761
1186
 
6762
1187
    Implementation overview
6763
1188
      1. update_ref_and_keys() accumulates info about null-rejecting
6764
 
         predicates in in KEY_FIELD::null_rejecting
6765
 
      1.1 add_key_part saves these to KEYUSE.
6766
 
      2. create_ref_for_key copies them to TABLE_REF.
 
1189
         predicates in in KeyField::null_rejecting
 
1190
      1.1 add_key_part saves these to KeyUse.
 
1191
      2. create_ref_for_key copies them to table_reference_st.
6767
1192
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6768
 
         appropiate JOIN_TAB members.
 
1193
         appropiate JoinTable members.
6769
1194
*/
6770
 
 
6771
 
static void add_not_null_conds(JOIN *join)
 
1195
void add_not_null_conds(Join *join)
6772
1196
{
6773
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
1197
  for (uint32_t i= join->const_tables; i < join->tables; i++)
6774
1198
  {
6775
 
    JOIN_TAB *tab=join->join_tab+i;
6776
 
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
6777
 
         tab->type == JT_REF_OR_NULL) &&
 
1199
    JoinTable *tab=join->join_tab+i;
 
1200
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
 
1201
         tab->type == AM_REF_OR_NULL) &&
6778
1202
        !tab->table->maybe_null)
6779
1203
    {
6780
1204
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
6785
1209
          Item *notnull;
6786
1210
          assert(item->type() == Item::FIELD_ITEM);
6787
1211
          Item_field *not_null_item= (Item_field*)item;
6788
 
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
1212
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
6789
1213
          /*
6790
1214
            For UPDATE queries such as:
6791
1215
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
6792
1216
            not_null_item is the t1.f1, but it's referred_tab is 0.
6793
1217
          */
6794
1218
          if (!referred_tab || referred_tab->join != join)
 
1219
          {
6795
1220
            continue;
6796
 
          if (!(notnull= new Item_func_isnotnull(not_null_item)))
6797
 
            return;
 
1221
          }
 
1222
          notnull= new Item_func_isnotnull(not_null_item);
 
1223
 
6798
1224
          /*
6799
1225
            We need to do full fix_fields() call here in order to have correct
6800
1226
            notnull->const_item(). This is needed e.g. by test_quick_select
6802
1228
            called.
6803
1229
          */
6804
1230
          if (notnull->fix_fields(join->session, &notnull))
 
1231
          {
6805
1232
            return;
 
1233
          }
 
1234
 
6806
1235
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6807
1236
        }
6808
1237
      }
6809
1238
    }
6810
1239
  }
6811
 
  return;
6812
1240
}
6813
1241
 
6814
1242
/**
6826
1254
    -  pointer to the guarded predicate, if success
6827
1255
    -  0, otherwise
6828
1256
*/
6829
 
 
6830
 
static COND*
6831
 
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
 
1257
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
6832
1258
{
6833
1259
  COND *tmp;
6834
1260
  assert(cond != 0);
6844
1270
  return tmp;
6845
1271
}
6846
1272
 
6847
 
 
6848
 
/**
6849
 
  Fill in outer join related info for the execution plan structure.
6850
 
 
6851
 
    For each outer join operation left after simplification of the
6852
 
    original query the function set up the following pointers in the linear
6853
 
    structure join->join_tab representing the selected execution plan.
6854
 
    The first inner table t0 for the operation is set to refer to the last
6855
 
    inner table tk through the field t0->last_inner.
6856
 
    Any inner table ti for the operation are set to refer to the first
6857
 
    inner table ti->first_inner.
6858
 
    The first inner table t0 for the operation is set to refer to the
6859
 
    first inner table of the embedding outer join operation, if there is any,
6860
 
    through the field t0->first_upper.
6861
 
    The on expression for the outer join operation is attached to the
6862
 
    corresponding first inner table through the field t0->on_expr_ref.
6863
 
    Here ti are structures of the JOIN_TAB type.
6864
 
 
6865
 
  EXAMPLE. For the query:
6866
 
  @code
6867
 
        SELECT * FROM t1
6868
 
                      LEFT JOIN
6869
 
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6870
 
                      ON (t1.a=t2.a AND t1.b=t3.b)
6871
 
          WHERE t1.c > 5,
6872
 
  @endcode
6873
 
 
6874
 
    given the execution plan with the table order t1,t2,t3,t4
6875
 
    is selected, the following references will be set;
6876
 
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6877
 
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6878
 
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
6879
 
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6880
 
 
6881
 
  @param join   reference to the info fully describing the query
6882
 
 
6883
 
  @note
6884
 
    The function assumes that the simplification procedure has been
6885
 
    already applied to the join query (see simplify_joins).
6886
 
    This function can be called only after the execution plan
6887
 
    has been chosen.
6888
 
*/
6889
 
 
6890
 
static void
6891
 
make_outerjoin_info(JOIN *join)
6892
 
{
6893
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
6894
 
  {
6895
 
    JOIN_TAB *tab=join->join_tab+i;
6896
 
    Table *table=tab->table;
6897
 
    TableList *tbl= table->pos_in_table_list;
6898
 
    TableList *embedding= tbl->embedding;
6899
 
 
6900
 
    if (tbl->outer_join)
6901
 
    {
6902
 
      /*
6903
 
        Table tab is the only one inner table for outer join.
6904
 
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6905
 
        is in the query above.)
6906
 
      */
6907
 
      tab->last_inner= tab->first_inner= tab;
6908
 
      tab->on_expr_ref= &tbl->on_expr;
6909
 
      tab->cond_equal= tbl->cond_equal;
6910
 
      if (embedding)
6911
 
        tab->first_upper= embedding->nested_join->first_nested;
6912
 
    }
6913
 
    for ( ; embedding ; embedding= embedding->embedding)
6914
 
    {
6915
 
      /* Ignore sj-nests: */
6916
 
      if (!embedding->on_expr)
6917
 
        continue;
6918
 
      nested_join_st *nested_join= embedding->nested_join;
6919
 
      if (!nested_join->counter_)
6920
 
      {
6921
 
        /*
6922
 
          Table tab is the first inner table for nested_join.
6923
 
          Save reference to it in the nested join structure.
6924
 
        */
6925
 
        nested_join->first_nested= tab;
6926
 
        tab->on_expr_ref= &embedding->on_expr;
6927
 
        tab->cond_equal= tbl->cond_equal;
6928
 
        if (embedding->embedding)
6929
 
          tab->first_upper= embedding->embedding->nested_join->first_nested;
6930
 
      }
6931
 
      if (!tab->first_inner)
6932
 
        tab->first_inner= nested_join->first_nested;
6933
 
      if (++nested_join->counter_ < nested_join->join_list.elements)
6934
 
        break;
6935
 
      /* Table tab is the last inner table for nested join. */
6936
 
      nested_join->first_nested->last_inner= tab;
6937
 
    }
6938
 
  }
6939
 
  return;
6940
 
}
6941
 
 
6942
 
 
6943
 
static bool
6944
 
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6945
 
{
6946
 
  Session *session= join->session;
6947
 
  if (select)
6948
 
  {
6949
 
    add_not_null_conds(join);
6950
 
    table_map used_tables;
6951
 
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6952
 
    {                        /* there may be a select without a cond. */
6953
 
      if (join->tables > 1)
6954
 
        cond->update_used_tables();             // Tablenr may have changed
6955
 
      if (join->const_tables == join->tables &&
6956
 
          session->lex->current_select->master_unit() ==
6957
 
          &session->lex->unit)          // not upper level SELECT
6958
 
        join->const_table_map|=RAND_TABLE_BIT;
6959
 
      {                                         // Check const tables
6960
 
        COND *const_cond=
6961
 
          make_cond_for_table(cond,
6962
 
                              join->const_table_map,
6963
 
                              (table_map) 0, 1);
6964
 
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6965
 
             tab < join->join_tab+join->tables ; tab++)
6966
 
        {
6967
 
          if (*tab->on_expr_ref)
6968
 
          {
6969
 
            JOIN_TAB *cond_tab= tab->first_inner;
6970
 
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6971
 
                                           join->const_table_map,
6972
 
                                           (  table_map) 0, 0);
6973
 
            if (!tmp)
6974
 
              continue;
6975
 
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6976
 
            if (!tmp)
6977
 
              return(1);
6978
 
            tmp->quick_fix_field();
6979
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6980
 
                                    new Item_cond_and(cond_tab->select_cond,
6981
 
                                                      tmp);
6982
 
            if (!cond_tab->select_cond)
6983
 
              return(1);
6984
 
            cond_tab->select_cond->quick_fix_field();
6985
 
          }
6986
 
        }
6987
 
        if (const_cond && !const_cond->val_int())
6988
 
        {
6989
 
          return(1);     // Impossible const condition
6990
 
        }
6991
 
      }
6992
 
    }
6993
 
    used_tables=((select->const_tables=join->const_table_map) |
6994
 
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6995
 
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
6996
 
    {
6997
 
      JOIN_TAB *tab=join->join_tab+i;
6998
 
      /*
6999
 
        first_inner is the X in queries like:
7000
 
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7001
 
      */
7002
 
      JOIN_TAB *first_inner_tab= tab->first_inner;
7003
 
      table_map current_map= tab->table->map;
7004
 
      bool use_quick_range=0;
7005
 
      COND *tmp;
7006
 
 
7007
 
      /*
7008
 
        Following force including random expression in last table condition.
7009
 
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7010
 
      */
7011
 
      if (i == join->tables-1)
7012
 
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7013
 
      used_tables|=current_map;
7014
 
 
7015
 
      if (tab->type == JT_REF && tab->quick &&
7016
 
          (uint) tab->ref.key == tab->quick->index &&
7017
 
          tab->ref.key_length < tab->quick->max_used_key_length)
7018
 
      {
7019
 
        /* Range uses longer key;  Use this instead of ref on key */
7020
 
        tab->type=JT_ALL;
7021
 
        use_quick_range=1;
7022
 
        tab->use_quick=1;
7023
 
        tab->ref.key= -1;
7024
 
        tab->ref.key_parts=0;           // Don't use ref key.
7025
 
        join->best_positions[i].records_read= rows2double(tab->quick->records);
7026
 
        /*
7027
 
          We will use join cache here : prevent sorting of the first
7028
 
          table only and sort at the end.
7029
 
        */
7030
 
        if (i != join->const_tables && join->tables > join->const_tables + 1)
7031
 
          join->full_join= 1;
7032
 
      }
7033
 
 
7034
 
      tmp= NULL;
7035
 
      if (cond)
7036
 
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7037
 
      if (cond && !tmp && tab->quick)
7038
 
      {                                         // Outer join
7039
 
        if (tab->type != JT_ALL)
7040
 
        {
7041
 
          /*
7042
 
            Don't use the quick method
7043
 
            We come here in the case where we have 'key=constant' and
7044
 
            the test is removed by make_cond_for_table()
7045
 
          */
7046
 
          delete tab->quick;
7047
 
          tab->quick= 0;
7048
 
        }
7049
 
        else
7050
 
        {
7051
 
          /*
7052
 
            Hack to handle the case where we only refer to a table
7053
 
            in the ON part of an OUTER JOIN. In this case we want the code
7054
 
            below to check if we should use 'quick' instead.
7055
 
          */
7056
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
7057
 
        }
7058
 
 
7059
 
      }
7060
 
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7061
 
          tab->type == JT_EQ_REF)
7062
 
      {
7063
 
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7064
 
                                       session->memdup((unsigned char*) select,
7065
 
                                                   sizeof(*select)));
7066
 
        if (!sel)
7067
 
          return(1);                    // End of memory
7068
 
        /*
7069
 
          If tab is an inner table of an outer join operation,
7070
 
          add a match guard to the pushed down predicate.
7071
 
          The guard will turn the predicate on only after
7072
 
          the first match for outer tables is encountered.
7073
 
        */
7074
 
        if (cond && tmp)
7075
 
        {
7076
 
          /*
7077
 
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7078
 
            a cond, so neutralize the hack above.
7079
 
          */
7080
 
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7081
 
            return(1);
7082
 
          tab->select_cond=sel->cond=tmp;
7083
 
          /* Push condition to storage engine if this is enabled
7084
 
             and the condition is not guarded */
7085
 
          tab->table->file->pushed_cond= NULL;
7086
 
          if (session->variables.engine_condition_pushdown)
7087
 
          {
7088
 
            COND *push_cond=
7089
 
              make_cond_for_table(tmp, current_map, current_map, 0);
7090
 
            if (push_cond)
7091
 
            {
7092
 
              /* Push condition to handler */
7093
 
              if (!tab->table->file->cond_push(push_cond))
7094
 
                tab->table->file->pushed_cond= push_cond;
7095
 
            }
7096
 
          }
7097
 
        }
7098
 
        else
7099
 
          tab->select_cond= sel->cond= NULL;
7100
 
 
7101
 
        sel->head=tab->table;
7102
 
        if (tab->quick)
7103
 
        {
7104
 
          /* Use quick key read if it's a constant and it's not used
7105
 
             with key reading */
7106
 
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7107
 
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7108
 
          {
7109
 
            sel->quick=tab->quick;              // Use value from get_quick_...
7110
 
            sel->quick_keys.clear_all();
7111
 
            sel->needed_reg.clear_all();
7112
 
          }
7113
 
          else
7114
 
          {
7115
 
            delete tab->quick;
7116
 
          }
7117
 
          tab->quick=0;
7118
 
        }
7119
 
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7120
 
        if (i == join->const_tables && ref_key)
7121
 
        {
7122
 
          if (!tab->const_keys.is_clear_all() &&
7123
 
              tab->table->reginfo.impossible_range)
7124
 
            return(1);
7125
 
        }
7126
 
        else if (tab->type == JT_ALL && ! use_quick_range)
7127
 
        {
7128
 
          if (!tab->const_keys.is_clear_all() &&
7129
 
              tab->table->reginfo.impossible_range)
7130
 
            return(1);                          // Impossible range
7131
 
          /*
7132
 
            We plan to scan all rows.
7133
 
            Check again if we should use an index.
7134
 
            We could have used an column from a previous table in
7135
 
            the index if we are using limit and this is the first table
7136
 
          */
7137
 
 
7138
 
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7139
 
              (!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)))
7140
 
          {
7141
 
            /* Join with outer join condition */
7142
 
            COND *orig_cond=sel->cond;
7143
 
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7144
 
 
7145
 
            /*
7146
 
              We can't call sel->cond->fix_fields,
7147
 
              as it will break tab->on_expr if it's AND condition
7148
 
              (fix_fields currently removes extra AND/OR levels).
7149
 
              Yet attributes of the just built condition are not needed.
7150
 
              Thus we call sel->cond->quick_fix_field for safety.
7151
 
            */
7152
 
            if (sel->cond && !sel->cond->fixed)
7153
 
              sel->cond->quick_fix_field();
7154
 
 
7155
 
            if (sel->test_quick_select(session, tab->keys,
7156
 
                                       used_tables & ~ current_map,
7157
 
                                       (join->select_options &
7158
 
                                        OPTION_FOUND_ROWS ?
7159
 
                                        HA_POS_ERROR :
7160
 
                                        join->unit->select_limit_cnt), 0,
7161
 
                                        false) < 0)
7162
 
            {
7163
 
              /*
7164
 
                Before reporting "Impossible WHERE" for the whole query
7165
 
                we have to check isn't it only "impossible ON" instead
7166
 
              */
7167
 
              sel->cond=orig_cond;
7168
 
              if (!*tab->on_expr_ref ||
7169
 
                  sel->test_quick_select(session, tab->keys,
7170
 
                                         used_tables & ~ current_map,
7171
 
                                         (join->select_options &
7172
 
                                          OPTION_FOUND_ROWS ?
7173
 
                                          HA_POS_ERROR :
7174
 
                                          join->unit->select_limit_cnt),0,
7175
 
                                          false) < 0)
7176
 
                return(1);                      // Impossible WHERE
7177
 
            }
7178
 
            else
7179
 
              sel->cond=orig_cond;
7180
 
 
7181
 
            /* Fix for EXPLAIN */
7182
 
            if (sel->quick)
7183
 
              join->best_positions[i].records_read= (double)sel->quick->records;
7184
 
          }
7185
 
          else
7186
 
          {
7187
 
            sel->needed_reg=tab->needed_reg;
7188
 
            sel->quick_keys.clear_all();
7189
 
          }
7190
 
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7191
 
              !sel->needed_reg.is_subset(tab->checked_keys))
7192
 
          {
7193
 
            tab->keys=sel->quick_keys;
7194
 
            tab->keys.merge(sel->needed_reg);
7195
 
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7196
 
                             (select->quick_keys.is_clear_all() ||
7197
 
                              (select->quick &&
7198
 
                               (select->quick->records >= 100L)))) ?
7199
 
              2 : 1;
7200
 
            sel->read_tables= used_tables & ~current_map;
7201
 
          }
7202
 
          if (i != join->const_tables && tab->use_quick != 2)
7203
 
          {                                     /* Read with cache */
7204
 
            if (cond &&
7205
 
                (tmp=make_cond_for_table(cond,
7206
 
                                         join->const_table_map |
7207
 
                                         current_map,
7208
 
                                         current_map, 0)))
7209
 
            {
7210
 
              tab->cache.select=(SQL_SELECT*)
7211
 
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
7212
 
              tab->cache.select->cond=tmp;
7213
 
              tab->cache.select->read_tables=join->const_table_map;
7214
 
            }
7215
 
          }
7216
 
        }
7217
 
      }
7218
 
 
7219
 
      /*
7220
 
        Push down conditions from all on expressions.
7221
 
        Each of these conditions are guarded by a variable
7222
 
        that turns if off just before null complemented row for
7223
 
        outer joins is formed. Thus, the condition from an
7224
 
        'on expression' are guaranteed not to be checked for
7225
 
        the null complemented row.
7226
 
      */
7227
 
 
7228
 
      /* First push down constant conditions from on expressions */
7229
 
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7230
 
           join_tab < join->join_tab+join->tables ; join_tab++)
7231
 
      {
7232
 
        if (*join_tab->on_expr_ref)
7233
 
        {
7234
 
          JOIN_TAB *cond_tab= join_tab->first_inner;
7235
 
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7236
 
                                         join->const_table_map,
7237
 
                                         (table_map) 0, 0);
7238
 
          if (!tmp)
7239
 
            continue;
7240
 
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7241
 
          if (!tmp)
7242
 
            return(1);
7243
 
          tmp->quick_fix_field();
7244
 
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7245
 
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7246
 
          if (!cond_tab->select_cond)
7247
 
            return(1);
7248
 
          cond_tab->select_cond->quick_fix_field();
7249
 
        }
7250
 
      }
7251
 
 
7252
 
      /* Push down non-constant conditions from on expressions */
7253
 
      JOIN_TAB *last_tab= tab;
7254
 
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7255
 
      {
7256
 
        /*
7257
 
          Table tab is the last inner table of an outer join.
7258
 
          An on expression is always attached to it.
7259
 
        */
7260
 
        COND *on_expr= *first_inner_tab->on_expr_ref;
7261
 
 
7262
 
        table_map used_tables2= (join->const_table_map |
7263
 
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7264
 
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7265
 
        {
7266
 
          current_map= tab->table->map;
7267
 
          used_tables2|= current_map;
7268
 
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7269
 
                                              current_map, 0);
7270
 
          if (tmp_cond)
7271
 
          {
7272
 
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7273
 
            /*
7274
 
              First add the guards for match variables of
7275
 
              all embedding outer join operations.
7276
 
            */
7277
 
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7278
 
                                                     tmp_cond,
7279
 
                                                     first_inner_tab)))
7280
 
              return(1);
7281
 
            /*
7282
 
              Now add the guard turning the predicate off for
7283
 
              the null complemented row.
7284
 
            */
7285
 
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7286
 
                                              &first_inner_tab->
7287
 
                                              not_null_compl);
7288
 
            if (tmp_cond)
7289
 
              tmp_cond->quick_fix_field();
7290
 
            /* Add the predicate to other pushed down predicates */
7291
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7292
 
                                  new Item_cond_and(cond_tab->select_cond,
7293
 
                                                    tmp_cond);
7294
 
            if (!cond_tab->select_cond)
7295
 
              return(1);
7296
 
            cond_tab->select_cond->quick_fix_field();
7297
 
          }
7298
 
        }
7299
 
        first_inner_tab= first_inner_tab->first_upper;
7300
 
      }
7301
 
    }
7302
 
  }
7303
 
  return(0);
7304
 
}
7305
 
 
7306
 
 
7307
 
/*
7308
 
  Check if given expression uses only table fields covered by the given index
7309
 
 
7310
 
  SYNOPSIS
7311
 
    uses_index_fields_only()
7312
 
      item           Expression to check
7313
 
      tbl            The table having the index
7314
 
      keyno          The index number
7315
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7316
 
 
7317
 
  DESCRIPTION
7318
 
    Check if given expression only uses fields covered by index #keyno in the
7319
 
    table tbl. The expression can use any fields in any other tables.
7320
 
 
7321
 
    The expression is guaranteed not to be AND or OR - those constructs are
7322
 
    handled outside of this function.
7323
 
 
7324
 
  RETURN
7325
 
    true   Yes
7326
 
    false  No
7327
 
*/
7328
 
 
7329
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
7330
 
                            bool other_tbls_ok)
7331
 
{
7332
 
  if (item->const_item())
7333
 
    return true;
7334
 
 
7335
 
  /*
7336
 
    Don't push down the triggered conditions. Nested outer joins execution
7337
 
    code may need to evaluate a condition several times (both triggered and
7338
 
    untriggered), and there is no way to put thi
7339
 
    TODO: Consider cloning the triggered condition and using the copies for:
7340
 
      1. push the first copy down, to have most restrictive index condition
7341
 
         possible
7342
 
      2. Put the second copy into tab->select_cond.
7343
 
  */
7344
 
  if (item->type() == Item::FUNC_ITEM &&
7345
 
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7346
 
    return false;
7347
 
 
7348
 
  if (!(item->used_tables() & tbl->map))
7349
 
    return other_tbls_ok;
7350
 
 
7351
 
  Item::Type item_type= item->type();
7352
 
  switch (item_type) {
7353
 
  case Item::FUNC_ITEM:
7354
 
    {
7355
 
      /* This is a function, apply condition recursively to arguments */
7356
 
      Item_func *item_func= (Item_func*)item;
7357
 
      Item **child;
7358
 
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
7359
 
      for (child= item_func->arguments(); child != item_end; child++)
7360
 
      {
7361
 
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
7362
 
          return false;
7363
 
      }
7364
 
      return true;
7365
 
    }
7366
 
  case Item::COND_ITEM:
7367
 
    {
7368
 
      /* This is a function, apply condition recursively to arguments */
7369
 
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7370
 
      Item *item;
7371
 
      while ((item=li++))
7372
 
      {
7373
 
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7374
 
          return false;
7375
 
      }
7376
 
      return true;
7377
 
    }
7378
 
  case Item::FIELD_ITEM:
7379
 
    {
7380
 
      Item_field *item_field= (Item_field*)item;
7381
 
      if (item_field->field->table != tbl)
7382
 
        return true;
7383
 
      return item_field->field->part_of_key.is_set(keyno);
7384
 
    }
7385
 
  case Item::REF_ITEM:
7386
 
    return uses_index_fields_only(item->real_item(), tbl, keyno,
7387
 
                                  other_tbls_ok);
7388
 
  default:
7389
 
    return false; /* Play it safe, don't push unknown non-const items */
7390
 
  }
7391
 
}
7392
 
 
7393
 
 
7394
 
#define ICP_COND_USES_INDEX_ONLY 10
7395
 
 
7396
 
/*
7397
 
  Get a part of the condition that can be checked using only index fields
7398
 
 
7399
 
  SYNOPSIS
7400
 
    make_cond_for_index()
7401
 
      cond           The source condition
7402
 
      table          The table that is partially available
7403
 
      keyno          The index in the above table. Only fields covered by the index
7404
 
                     are available
7405
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7406
 
 
7407
 
  DESCRIPTION
7408
 
    Get a part of the condition that can be checked when for the given table
7409
 
    we have values only of fields covered by some index. The condition may
7410
 
    refer to other tables, it is assumed that we have values of all of their
7411
 
    fields.
7412
 
 
7413
 
    Example:
7414
 
      make_cond_for_index(
7415
 
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7416
 
          t2, keyno(t2.key1))
7417
 
      will return
7418
 
        "cond(t1.field) AND cond(t2.key2)"
7419
 
 
7420
 
  RETURN
7421
 
    Index condition, or NULL if no condition could be inferred.
7422
 
*/
7423
 
 
7424
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
7425
 
                          bool other_tbls_ok)
7426
 
{
7427
 
  if (!cond)
7428
 
    return NULL;
7429
 
  if (cond->type() == Item::COND_ITEM)
7430
 
  {
7431
 
    uint32_t n_marked= 0;
7432
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7433
 
    {
7434
 
      Item_cond_and *new_cond=new Item_cond_and;
7435
 
      if (!new_cond)
7436
 
        return (COND*) 0;
7437
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7438
 
      Item *item;
7439
 
      while ((item=li++))
7440
 
      {
7441
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7442
 
        if (fix)
7443
 
          new_cond->argument_list()->push_back(fix);
7444
 
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7445
 
      }
7446
 
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7447
 
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7448
 
      switch (new_cond->argument_list()->elements) {
7449
 
      case 0:
7450
 
        return (COND*) 0;
7451
 
      case 1:
7452
 
        return new_cond->argument_list()->head();
7453
 
      default:
7454
 
        new_cond->quick_fix_field();
7455
 
        return new_cond;
7456
 
      }
7457
 
    }
7458
 
    else /* It's OR */
7459
 
    {
7460
 
      Item_cond_or *new_cond=new Item_cond_or;
7461
 
      if (!new_cond)
7462
 
        return (COND*) 0;
7463
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7464
 
      Item *item;
7465
 
      while ((item=li++))
7466
 
      {
7467
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7468
 
        if (!fix)
7469
 
          return (COND*) 0;
7470
 
        new_cond->argument_list()->push_back(fix);
7471
 
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7472
 
      }
7473
 
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7474
 
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7475
 
      new_cond->quick_fix_field();
7476
 
      new_cond->top_level_item();
7477
 
      return new_cond;
7478
 
    }
7479
 
  }
7480
 
 
7481
 
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
7482
 
    return (COND*) 0;
7483
 
  cond->marker= ICP_COND_USES_INDEX_ONLY;
7484
 
  return cond;
7485
 
}
7486
 
 
7487
 
 
7488
 
Item *make_cond_remainder(Item *cond, bool exclude_index)
7489
 
{
7490
 
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7491
 
    return 0; /* Already checked */
7492
 
 
7493
 
  if (cond->type() == Item::COND_ITEM)
7494
 
  {
7495
 
    table_map tbl_map= 0;
7496
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7497
 
    {
7498
 
      /* Create new top level AND item */
7499
 
      Item_cond_and *new_cond=new Item_cond_and;
7500
 
      if (!new_cond)
7501
 
        return (COND*) 0;
7502
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7503
 
      Item *item;
7504
 
      while ((item=li++))
7505
 
      {
7506
 
        Item *fix= make_cond_remainder(item, exclude_index);
7507
 
        if (fix)
7508
 
        {
7509
 
          new_cond->argument_list()->push_back(fix);
7510
 
          tbl_map |= fix->used_tables();
7511
 
        }
7512
 
      }
7513
 
      switch (new_cond->argument_list()->elements) {
7514
 
      case 0:
7515
 
        return (COND*) 0;
7516
 
      case 1:
7517
 
        return new_cond->argument_list()->head();
7518
 
      default:
7519
 
        new_cond->quick_fix_field();
7520
 
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7521
 
        return new_cond;
7522
 
      }
7523
 
    }
7524
 
    else /* It's OR */
7525
 
    {
7526
 
      Item_cond_or *new_cond=new Item_cond_or;
7527
 
      if (!new_cond)
7528
 
        return (COND*) 0;
7529
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7530
 
      Item *item;
7531
 
      while ((item=li++))
7532
 
      {
7533
 
        Item *fix= make_cond_remainder(item, false);
7534
 
        if (!fix)
7535
 
          return (COND*) 0;
7536
 
        new_cond->argument_list()->push_back(fix);
7537
 
        tbl_map |= fix->used_tables();
7538
 
      }
7539
 
      new_cond->quick_fix_field();
7540
 
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7541
 
      new_cond->top_level_item();
7542
 
      return new_cond;
7543
 
    }
7544
 
  }
7545
 
  return cond;
7546
 
}
7547
 
 
7548
 
 
7549
 
/*
7550
 
  Try to extract and push the index condition
7551
 
 
7552
 
  SYNOPSIS
7553
 
    push_index_cond()
7554
 
      tab            A join tab that has tab->table->file and its condition
7555
 
                     in tab->select_cond
7556
 
      keyno          Index for which extract and push the condition
7557
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7558
 
 
7559
 
  DESCRIPTION
7560
 
    Try to extract and push the index condition down to table handler
7561
 
*/
7562
 
 
7563
 
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
7564
 
{
7565
 
  Item *idx_cond;
7566
 
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7567
 
      tab->join->session->variables.engine_condition_pushdown)
7568
 
  {
7569
 
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7570
 
                                  other_tbls_ok);
7571
 
 
7572
 
    if (idx_cond)
7573
 
    {
7574
 
      tab->pre_idx_push_select_cond= tab->select_cond;
7575
 
      Item *idx_remainder_cond=
7576
 
        tab->table->file->idx_cond_push(keyno, idx_cond);
7577
 
 
7578
 
      /*
7579
 
        Disable eq_ref's "lookup cache" if we've pushed down an index
7580
 
        condition.
7581
 
        TODO: This check happens to work on current ICP implementations, but
7582
 
        there may exist a compliant implementation that will not work
7583
 
        correctly with it. Sort this out when we stabilize the condition
7584
 
        pushdown APIs.
7585
 
      */
7586
 
      if (idx_remainder_cond != idx_cond)
7587
 
        tab->ref.disable_cache= true;
7588
 
 
7589
 
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
7590
 
 
7591
 
      if (row_cond)
7592
 
      {
7593
 
        if (!idx_remainder_cond)
7594
 
          tab->select_cond= row_cond;
7595
 
        else
7596
 
        {
7597
 
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7598
 
          tab->select_cond->quick_fix_field();
7599
 
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
7600
 
            row_cond->used_tables() | idx_remainder_cond->used_tables();
7601
 
        }
7602
 
      }
7603
 
      else
7604
 
        tab->select_cond= idx_remainder_cond;
7605
 
      if (tab->select)
7606
 
      {
7607
 
        tab->select->cond= tab->select_cond;
7608
 
      }
7609
 
    }
7610
 
  }
7611
 
  return;
7612
 
}
7613
 
 
7614
 
 
7615
 
 
7616
 
    /*
7617
 
      Determine if the set is already ordered for order_st BY, so it can
7618
 
      disable join cache because it will change the ordering of the results.
7619
 
      Code handles sort table that is at any location (not only first after
7620
 
      the const tables) despite the fact that it's currently prohibited.
7621
 
      We must disable join cache if the first non-const table alone is
7622
 
      ordered. If there is a temp table the ordering is done as a last
7623
 
      operation and doesn't prevent join cache usage.
7624
 
    */
7625
 
uint32_t make_join_orderinfo(JOIN *join)
7626
 
{
7627
 
  uint32_t i;
7628
 
  if (join->need_tmp)
7629
 
    return join->tables;
7630
 
 
7631
 
  for (i=join->const_tables ; i < join->tables ; i++)
7632
 
  {
7633
 
    JOIN_TAB *tab=join->join_tab+i;
7634
 
    Table *table=tab->table;
7635
 
    if ((table == join->sort_by_table &&
7636
 
         (!join->order || join->skip_sort_order)) ||
7637
 
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
7638
 
    {
7639
 
      break;
7640
 
    }
7641
 
  }
7642
 
  return i;
7643
 
}
7644
 
 
7645
 
 
7646
 
/*
7647
 
  Plan refinement stage: do various set ups for the executioner
7648
 
 
7649
 
  SYNOPSIS
7650
 
    make_join_readinfo()
7651
 
      join           Join being processed
7652
 
      options        Join's options (checking for SELECT_DESCRIBE,
7653
 
                     SELECT_NO_JOIN_CACHE)
7654
 
      no_jbuf_after  Don't use join buffering after table with this number.
7655
 
 
7656
 
  DESCRIPTION
7657
 
    Plan refinement stage: do various set ups for the executioner
7658
 
      - set up use of join buffering
7659
 
      - push index conditions
7660
 
      - increment counters
7661
 
      - etc
7662
 
 
7663
 
  RETURN
7664
 
    false - OK
7665
 
    true  - Out of memory
7666
 
*/
7667
 
 
7668
 
static bool
7669
 
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
7670
 
{
7671
 
  uint32_t i;
7672
 
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7673
 
  bool sorted= 1;
7674
 
 
7675
 
  for (i=join->const_tables ; i < join->tables ; i++)
7676
 
  {
7677
 
    JOIN_TAB *tab=join->join_tab+i;
7678
 
    Table *table=tab->table;
7679
 
    bool using_join_cache;
7680
 
    tab->read_record.table= table;
7681
 
    tab->read_record.file=table->file;
7682
 
    tab->next_select=sub_select;                /* normal select */
7683
 
    /*
7684
 
      TODO: don't always instruct first table's ref/range access method to
7685
 
      produce sorted output.
7686
 
    */
7687
 
    tab->sorted= sorted;
7688
 
    sorted= 0;                                  // only first must be sorted
7689
 
    if (tab->insideout_match_tab)
7690
 
    {
7691
 
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
7692
 
                                                         [tab->index].
7693
 
                                                         key_length)))
7694
 
        return true;
7695
 
    }
7696
 
    switch (tab->type) {
7697
 
    case JT_SYSTEM:                             // Only happens with left join
7698
 
      table->status=STATUS_NO_RECORD;
7699
 
      tab->read_first_record= join_read_system;
7700
 
      tab->read_record.read_record= join_no_more_records;
7701
 
      break;
7702
 
    case JT_CONST:                              // Only happens with left join
7703
 
      table->status=STATUS_NO_RECORD;
7704
 
      tab->read_first_record= join_read_const;
7705
 
      tab->read_record.read_record= join_no_more_records;
7706
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7707
 
          !table->no_keyread)
7708
 
      {
7709
 
        table->key_read=1;
7710
 
        table->file->extra(HA_EXTRA_KEYREAD);
7711
 
      }
7712
 
      break;
7713
 
    case JT_EQ_REF:
7714
 
      table->status=STATUS_NO_RECORD;
7715
 
      if (tab->select)
7716
 
      {
7717
 
        delete tab->select->quick;
7718
 
        tab->select->quick=0;
7719
 
      }
7720
 
      delete tab->quick;
7721
 
      tab->quick=0;
7722
 
      tab->read_first_record= join_read_key;
7723
 
      tab->read_record.read_record= join_no_more_records;
7724
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7725
 
          !table->no_keyread)
7726
 
      {
7727
 
        table->key_read=1;
7728
 
        table->file->extra(HA_EXTRA_KEYREAD);
7729
 
      }
7730
 
      else
7731
 
        push_index_cond(tab, tab->ref.key, true);
7732
 
      break;
7733
 
    case JT_REF_OR_NULL:
7734
 
    case JT_REF:
7735
 
      table->status=STATUS_NO_RECORD;
7736
 
      if (tab->select)
7737
 
      {
7738
 
        delete tab->select->quick;
7739
 
        tab->select->quick=0;
7740
 
      }
7741
 
      delete tab->quick;
7742
 
      tab->quick=0;
7743
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7744
 
          !table->no_keyread)
7745
 
      {
7746
 
        table->key_read=1;
7747
 
        table->file->extra(HA_EXTRA_KEYREAD);
7748
 
      }
7749
 
      else
7750
 
        push_index_cond(tab, tab->ref.key, true);
7751
 
      if (tab->type == JT_REF)
7752
 
      {
7753
 
        tab->read_first_record= join_read_always_key;
7754
 
        tab->read_record.read_record= tab->insideout_match_tab?
7755
 
           join_read_next_same_diff : join_read_next_same;
7756
 
      }
7757
 
      else
7758
 
      {
7759
 
        tab->read_first_record= join_read_always_key_or_null;
7760
 
        tab->read_record.read_record= join_read_next_same_or_null;
7761
 
      }
7762
 
      break;
7763
 
    case JT_ALL:
7764
 
      /*
7765
 
        If previous table use cache
7766
 
        If the incoming data set is already sorted don't use cache.
7767
 
      */
7768
 
      table->status=STATUS_NO_RECORD;
7769
 
      using_join_cache= false;
7770
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7771
 
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7772
 
          !tab->insideout_match_tab)
7773
 
      {
7774
 
        if ((options & SELECT_DESCRIBE) ||
7775
 
            !join_init_cache(join->session,join->join_tab+join->const_tables,
7776
 
                             i-join->const_tables))
7777
 
        {
7778
 
          using_join_cache= true;
7779
 
          tab[-1].next_select=sub_select_cache; /* Patch previous */
7780
 
        }
7781
 
      }
7782
 
      /* These init changes read_record */
7783
 
      if (tab->use_quick == 2)
7784
 
      {
7785
 
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7786
 
        tab->read_first_record= join_init_quick_read_record;
7787
 
        if (statistics)
7788
 
          status_var_increment(join->session->status_var.select_range_check_count);
7789
 
      }
7790
 
      else
7791
 
      {
7792
 
        tab->read_first_record= join_init_read_record;
7793
 
        if (i == join->const_tables)
7794
 
        {
7795
 
          if (tab->select && tab->select->quick)
7796
 
          {
7797
 
            if (statistics)
7798
 
              status_var_increment(join->session->status_var.select_range_count);
7799
 
          }
7800
 
          else
7801
 
          {
7802
 
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
7803
 
            if (statistics)
7804
 
              status_var_increment(join->session->status_var.select_scan_count);
7805
 
          }
7806
 
        }
7807
 
        else
7808
 
        {
7809
 
          if (tab->select && tab->select->quick)
7810
 
          {
7811
 
            if (statistics)
7812
 
              status_var_increment(join->session->status_var.select_full_range_join_count);
7813
 
          }
7814
 
          else
7815
 
          {
7816
 
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
7817
 
            if (statistics)
7818
 
              status_var_increment(join->session->status_var.select_full_join_count);
7819
 
          }
7820
 
        }
7821
 
        if (!table->no_keyread)
7822
 
        {
7823
 
          if (tab->select && tab->select->quick &&
7824
 
              tab->select->quick->index != MAX_KEY && //not index_merge
7825
 
              table->covering_keys.is_set(tab->select->quick->index))
7826
 
          {
7827
 
            table->key_read=1;
7828
 
            table->file->extra(HA_EXTRA_KEYREAD);
7829
 
          }
7830
 
          else if (!table->covering_keys.is_clear_all() &&
7831
 
                   !(tab->select && tab->select->quick))
7832
 
          {                                     // Only read index tree
7833
 
            if (!tab->insideout_match_tab)
7834
 
            {
7835
 
              /*
7836
 
                See bug #26447: "Using the clustered index for a table scan
7837
 
                is always faster than using a secondary index".
7838
 
              */
7839
 
              if (table->s->primary_key != MAX_KEY &&
7840
 
                  table->file->primary_key_is_clustered())
7841
 
                tab->index= table->s->primary_key;
7842
 
              else
7843
 
                tab->index= table->find_shortest_key(&table->covering_keys);
7844
 
            }
7845
 
            tab->read_first_record= join_read_first;
7846
 
            tab->type=JT_NEXT;          // Read with index_first / index_next
7847
 
          }
7848
 
        }
7849
 
        if (tab->select && tab->select->quick &&
7850
 
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7851
 
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7852
 
      }
7853
 
      break;
7854
 
    default:
7855
 
      break;                                    /* purecov: deadcode */
7856
 
    case JT_UNKNOWN:
7857
 
    case JT_MAYBE_REF:
7858
 
      abort();                                  /* purecov: deadcode */
7859
 
    }
7860
 
  }
7861
 
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7862
 
  return(false);
7863
 
}
7864
 
 
7865
 
 
7866
 
/**
7867
 
  Give error if we some tables are done with a full join.
7868
 
 
7869
 
  This is used by multi_table_update and multi_table_delete when running
7870
 
  in safe mode.
7871
 
 
7872
 
  @param join           Join condition
7873
 
 
7874
 
  @retval
7875
 
    0   ok
7876
 
  @retval
7877
 
    1   Error (full join used)
7878
 
*/
7879
 
 
7880
 
bool error_if_full_join(JOIN *join)
7881
 
{
7882
 
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7883
 
       tab < end;
7884
 
       tab++)
7885
 
  {
7886
 
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7887
 
    {
7888
 
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7889
 
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7890
 
      return(1);
7891
 
    }
7892
 
  }
7893
 
  return(0);
7894
 
}
7895
 
 
7896
 
 
7897
 
/**
7898
 
  cleanup JOIN_TAB.
7899
 
*/
7900
 
 
7901
 
void JOIN_TAB::cleanup()
7902
 
{
7903
 
  delete select;
7904
 
  select= 0;
7905
 
  delete quick;
7906
 
  quick= 0;
 
1273
/**
 
1274
  cleanup JoinTable.
 
1275
*/
 
1276
void JoinTable::cleanup()
 
1277
{
 
1278
  safe_delete(select);
 
1279
  safe_delete(quick);
 
1280
 
7907
1281
  if (cache.buff)
 
1282
  {
 
1283
    size_t size= cache.end - cache.buff;
 
1284
    global_join_buffer.sub(size);
7908
1285
    free(cache.buff);
 
1286
  }
7909
1287
  cache.buff= 0;
7910
1288
  limit= 0;
7911
1289
  if (table)
7913
1291
    if (table->key_read)
7914
1292
    {
7915
1293
      table->key_read= 0;
7916
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
1294
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
7917
1295
    }
7918
 
    table->file->ha_index_or_rnd_end();
 
1296
    table->cursor->ha_index_or_rnd_end();
7919
1297
    /*
7920
1298
      We need to reset this for next select
7921
1299
      (Tested in part_of_refkey)
7922
1300
    */
7923
1301
    table->reginfo.join_tab= 0;
7924
1302
  }
7925
 
  end_read_record(&read_record);
7926
 
}
7927
 
 
7928
 
 
7929
 
/**
7930
 
  Partially cleanup JOIN after it has executed: close index or rnd read
7931
 
  (table cursors), free quick selects.
7932
 
 
7933
 
    This function is called in the end of execution of a JOIN, before the used
7934
 
    tables are unlocked and closed.
7935
 
 
7936
 
    For a join that is resolved using a temporary table, the first sweep is
7937
 
    performed against actual tables and an intermediate result is inserted
7938
 
    into the temprorary table.
7939
 
    The last sweep is performed against the temporary table. Therefore,
7940
 
    the base tables and associated buffers used to fill the temporary table
7941
 
    are no longer needed, and this function is called to free them.
7942
 
 
7943
 
    For a join that is performed without a temporary table, this function
7944
 
    is called after all rows are sent, but before EOF packet is sent.
7945
 
 
7946
 
    For a simple SELECT with no subqueries this function performs a full
7947
 
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
7948
 
    tables.
7949
 
 
7950
 
    If a JOIN is executed for a subquery or if it has a subquery, we can't
7951
 
    do the full cleanup and need to do a partial cleanup only.
7952
 
    - If a JOIN is not the top level join, we must not unlock the tables
7953
 
    because the outer select may not have been evaluated yet, and we
7954
 
    can't unlock only selected tables of a query.
7955
 
    - Additionally, if this JOIN corresponds to a correlated subquery, we
7956
 
    should not free quick selects and join buffers because they will be
7957
 
    needed for the next execution of the correlated subquery.
7958
 
    - However, if this is a JOIN for a [sub]select, which is not
7959
 
    a correlated subquery itself, but has subqueries, we can free it
7960
 
    fully and also free JOINs of all its subqueries. The exception
7961
 
    is a subquery in SELECT list, e.g: @n
7962
 
    SELECT a, (select cmax(b) from t1) group by c @n
7963
 
    This subquery will not be evaluated at first sweep and its value will
7964
 
    not be inserted into the temporary table. Instead, it's evaluated
7965
 
    when selecting from the temporary table. Therefore, it can't be freed
7966
 
    here even though it's not correlated.
7967
 
 
7968
 
  @todo
7969
 
    Unlock tables even if the join isn't top level select in the tree
7970
 
*/
7971
 
 
7972
 
void JOIN::join_free()
7973
 
{
7974
 
  SELECT_LEX_UNIT *tmp_unit;
7975
 
  SELECT_LEX *sl;
7976
 
  /*
7977
 
    Optimization: if not EXPLAIN and we are done with the JOIN,
7978
 
    free all tables.
7979
 
  */
7980
 
  bool full= (!select_lex->uncacheable && !session->lex->describe);
7981
 
  bool can_unlock= full;
7982
 
 
7983
 
  cleanup(full);
7984
 
 
7985
 
  for (tmp_unit= select_lex->first_inner_unit();
7986
 
       tmp_unit;
7987
 
       tmp_unit= tmp_unit->next_unit())
7988
 
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
7989
 
    {
7990
 
      Item_subselect *subselect= sl->master_unit()->item;
7991
 
      bool full_local= full && (!subselect || subselect->is_evaluated());
7992
 
      /*
7993
 
        If this join is evaluated, we can fully clean it up and clean up all
7994
 
        its underlying joins even if they are correlated -- they will not be
7995
 
        used any more anyway.
7996
 
        If this join is not yet evaluated, we still must clean it up to
7997
 
        close its table cursors -- it may never get evaluated, as in case of
7998
 
        ... HAVING false OR a IN (SELECT ...))
7999
 
        but all table cursors must be closed before the unlock.
8000
 
      */
8001
 
      sl->cleanup_all_joins(full_local);
8002
 
      /* Can't unlock if at least one JOIN is still needed */
8003
 
      can_unlock= can_unlock && full_local;
8004
 
    }
8005
 
 
8006
 
  /*
8007
 
    We are not using tables anymore
8008
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8009
 
  */
8010
 
  if (can_unlock && lock && session->lock &&
8011
 
      !(select_options & SELECT_NO_UNLOCK) &&
8012
 
      !select_lex->subquery_in_having &&
8013
 
      (select_lex == (session->lex->unit.fake_select_lex ?
8014
 
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
8015
 
  {
8016
 
    /*
8017
 
      TODO: unlock tables even if the join isn't top level select in the
8018
 
      tree.
8019
 
    */
8020
 
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
8021
 
    lock= 0;
8022
 
  }
8023
 
 
8024
 
  return;
8025
 
}
8026
 
 
8027
 
 
8028
 
/**
8029
 
  Free resources of given join.
8030
 
 
8031
 
  @param fill   true if we should free all resources, call with full==1
8032
 
                should be last, before it this function can be called with
8033
 
                full==0
8034
 
 
8035
 
  @note
8036
 
    With subquery this function definitely will be called several times,
8037
 
    but even for simple query it can be called several times.
8038
 
*/
8039
 
 
8040
 
void JOIN::cleanup(bool full)
8041
 
{
8042
 
  if (table)
8043
 
  {
8044
 
    JOIN_TAB *tab,*end;
8045
 
    /*
8046
 
      Only a sorted table may be cached.  This sorted table is always the
8047
 
      first non const table in join->table
8048
 
    */
8049
 
    if (tables > const_tables) // Test for not-const tables
8050
 
    {
8051
 
      free_io_cache(table[const_tables]);
8052
 
      filesort_free_buffers(table[const_tables],full);
8053
 
    }
8054
 
 
8055
 
    if (full)
8056
 
    {
8057
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8058
 
        tab->cleanup();
8059
 
      table= 0;
8060
 
    }
8061
 
    else
8062
 
    {
8063
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8064
 
      {
8065
 
        if (tab->table)
8066
 
          tab->table->file->ha_index_or_rnd_end();
8067
 
      }
8068
 
    }
8069
 
    cleanup_sj_tmp_tables(this);//
8070
 
  }
8071
 
  /*
8072
 
    We are not using tables anymore
8073
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8074
 
  */
8075
 
  if (full)
8076
 
  {
8077
 
    if (tmp_join)
8078
 
      tmp_table_param.copy_field= 0;
8079
 
    group_fields.delete_elements();
8080
 
    /*
8081
 
      We can't call delete_elements() on copy_funcs as this will cause
8082
 
      problems in free_elements() as some of the elements are then deleted.
8083
 
    */
8084
 
    tmp_table_param.copy_funcs.empty();
8085
 
    /*
8086
 
      If we have tmp_join and 'this' JOIN is not tmp_join and
8087
 
      tmp_table_param.copy_field's  of them are equal then we have to remove
8088
 
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
8089
 
      be removed in tmp_table_param.cleanup().
8090
 
    */
8091
 
    if (tmp_join &&
8092
 
        tmp_join != this &&
8093
 
        tmp_join->tmp_table_param.copy_field ==
8094
 
        tmp_table_param.copy_field)
8095
 
    {
8096
 
      tmp_join->tmp_table_param.copy_field=
8097
 
        tmp_join->tmp_table_param.save_copy_field= 0;
8098
 
    }
8099
 
    tmp_table_param.cleanup();
8100
 
  }
8101
 
  return;
8102
 
}
8103
 
 
8104
 
 
8105
 
/**
8106
 
  Remove the following expressions from order_st BY and GROUP BY:
 
1303
  read_record.end_read_record();
 
1304
}
 
1305
 
 
1306
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
 
1307
{
 
1308
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
 
1309
  {
 
1310
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
1311
    {
 
1312
      return 0;
 
1313
    }
 
1314
  }
 
1315
  return 1;
 
1316
}
 
1317
 
 
1318
/**
 
1319
  Remove the following expressions from ORDER BY and GROUP BY:
8107
1320
  Constant expressions @n
8108
1321
  Expression that only uses tables that are of type EQ_REF and the reference
8109
1322
  is in the order_st list or if all refereed tables are of the above type.
8110
1323
 
8111
1324
  In the following, the X field can be removed:
8112
1325
  @code
8113
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
8114
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
 
1326
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
 
1327
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8115
1328
  @endcode
8116
1329
 
8117
1330
  These can't be optimized:
8118
1331
  @code
8119
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
8120
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
8121
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
 
1332
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
 
1333
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
 
1334
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8122
1335
  @endcode
8123
1336
*/
8124
 
 
8125
 
static bool
8126
 
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
 
1337
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
8127
1338
{
8128
1339
  if (tab->cached_eq_ref_table)                 // If cached
 
1340
  {
8129
1341
    return tab->eq_ref_table;
 
1342
  }
 
1343
 
8130
1344
  tab->cached_eq_ref_table=1;
 
1345
 
8131
1346
  /* We can skip const tables only if not an outer table */
8132
 
  if (tab->type == JT_CONST && !tab->first_inner)
8133
 
    return (tab->eq_ref_table=1);               /* purecov: inspected */
8134
 
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
 
1347
  if (tab->type == AM_CONST && !tab->first_inner)
 
1348
  {
 
1349
    return (tab->eq_ref_table=1);
 
1350
  }
 
1351
 
 
1352
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
1353
  {
8135
1354
    return (tab->eq_ref_table=0);               // We must use this
 
1355
  }
 
1356
 
8136
1357
  Item **ref_item=tab->ref.items;
8137
1358
  Item **end=ref_item+tab->ref.key_parts;
8138
1359
  uint32_t found=0;
8142
1363
  {
8143
1364
    if (! (*ref_item)->const_item())
8144
1365
    {                                           // Not a const ref
8145
 
      order_st *order;
 
1366
      Order *order;
8146
1367
      for (order=start_order ; order ; order=order->next)
8147
1368
      {
8148
 
        if ((*ref_item)->eq(order->item[0],0))
8149
 
          break;
 
1369
        if ((*ref_item)->eq(order->item[0],0))
 
1370
          break;
8150
1371
      }
 
1372
 
8151
1373
      if (order)
8152
1374
      {
8153
 
        found++;
8154
 
        assert(!(order->used & map));
8155
 
        order->used|=map;
8156
 
        continue;                               // Used in order_st BY
 
1375
        found++;
 
1376
        assert(!(order->used & map));
 
1377
        order->used|=map;
 
1378
        continue;                               // Used in order_st BY
8157
1379
      }
 
1380
 
8158
1381
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8159
 
        return (tab->eq_ref_table=0);
 
1382
      {
 
1383
        return (tab->eq_ref_table= 0);
 
1384
      }
8160
1385
    }
8161
1386
  }
8162
1387
  /* Check that there was no reference to table before sort order */
8168
1393
      continue;
8169
1394
    }
8170
1395
    if (start_order->depend_map & map)
8171
 
      return (tab->eq_ref_table=0);
8172
 
  }
8173
 
  return tab->eq_ref_table=1;
8174
 
}
8175
 
 
8176
 
 
8177
 
static bool
8178
 
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
8179
 
{
8180
 
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8181
 
  {
8182
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8183
 
      return 0;
8184
 
  }
8185
 
  return 1;
8186
 
}
8187
 
 
8188
 
 
8189
 
/** Update the dependency map for the tables. */
8190
 
 
8191
 
static void update_depend_map(JOIN *join)
8192
 
{
8193
 
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8194
 
 
8195
 
  for (; join_tab != end ; join_tab++)
8196
 
  {
8197
 
    TABLE_REF *ref= &join_tab->ref;
8198
 
    table_map depend_map=0;
8199
 
    Item **item=ref->items;
8200
 
    uint32_t i;
8201
 
    for (i=0 ; i < ref->key_parts ; i++,item++)
8202
 
      depend_map|=(*item)->used_tables();
8203
 
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8204
 
    depend_map&= ~OUTER_REF_TABLE_BIT;
8205
 
    for (JOIN_TAB **tab=join->map2table;
8206
 
         depend_map ;
8207
 
         tab++,depend_map>>=1 )
8208
 
    {
8209
 
      if (depend_map & 1)
8210
 
        ref->depend_map|=(*tab)->ref.depend_map;
8211
 
    }
8212
 
  }
8213
 
}
8214
 
 
8215
 
 
8216
 
/** Update the dependency map for the sort order. */
8217
 
 
8218
 
static void update_depend_map(JOIN *join, order_st *order)
8219
 
{
8220
 
  for (; order ; order=order->next)
8221
 
  {
8222
 
    table_map depend_map;
8223
 
    order->item[0]->update_used_tables();
8224
 
    order->depend_map=depend_map=order->item[0]->used_tables();
8225
 
    // Not item_sum(), RAND() and no reference to table outside of sub select
8226
 
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8227
 
        && !order->item[0]->with_sum_func)
8228
 
    {
8229
 
      for (JOIN_TAB **tab=join->map2table;
8230
 
           depend_map ;
8231
 
           tab++, depend_map>>=1)
8232
 
      {
8233
 
        if (depend_map & 1)
8234
 
          order->depend_map|=(*tab)->ref.depend_map;
8235
 
      }
8236
 
    }
8237
 
  }
8238
 
}
8239
 
 
8240
 
 
8241
 
/**
8242
 
  Remove all constants and check if order_st only contains simple
8243
 
  expressions.
8244
 
 
8245
 
  simple_order is set to 1 if sort_order only uses fields from head table
8246
 
  and the head table is not a LEFT JOIN table.
8247
 
 
8248
 
  @param join                   Join handler
8249
 
  @param first_order            List of SORT or GROUP order
8250
 
  @param cond                   WHERE statement
8251
 
  @param change_list            Set to 1 if we should remove things from list.
8252
 
                               If this is not set, then only simple_order is
8253
 
                               calculated.
8254
 
  @param simple_order           Set to 1 if we are only using simple expressions
8255
 
 
8256
 
  @return
8257
 
    Returns new sort order
8258
 
*/
8259
 
 
8260
 
static order_st *
8261
 
remove_const(JOIN *join,order_st *first_order, COND *cond,
8262
 
             bool change_list, bool *simple_order)
8263
 
{
8264
 
  if (join->tables == join->const_tables)
8265
 
    return change_list ? 0 : first_order;               // No need to sort
8266
 
 
8267
 
  order_st *order,**prev_ptr;
8268
 
  table_map first_table= join->join_tab[join->const_tables].table->map;
8269
 
  table_map not_const_tables= ~join->const_table_map;
8270
 
  table_map ref;
8271
 
 
8272
 
  prev_ptr= &first_order;
8273
 
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8274
 
 
8275
 
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8276
 
 
8277
 
  update_depend_map(join, first_order);
8278
 
  for (order=first_order; order ; order=order->next)
8279
 
  {
8280
 
    table_map order_tables=order->item[0]->used_tables();
8281
 
    if (order->item[0]->with_sum_func)
8282
 
      *simple_order=0;                          // Must do a temp table to sort
8283
 
    else if (!(order_tables & not_const_tables))
8284
 
    {
8285
 
      if (order->item[0]->with_subselect)
8286
 
        order->item[0]->val_str(&order->item[0]->str_value);
8287
 
      continue;                                 // skip const item
8288
 
    }
8289
 
    else
8290
 
    {
8291
 
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8292
 
        *simple_order=0;
8293
 
      else
8294
 
      {
8295
 
        Item *comp_item=0;
8296
 
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8297
 
        {
8298
 
          continue;
8299
 
        }
8300
 
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8301
 
        {
8302
 
          if (!(order_tables & first_table) &&
8303
 
              only_eq_ref_tables(join,first_order, ref))
8304
 
          {
8305
 
            continue;
8306
 
          }
8307
 
          *simple_order=0;                      // Must do a temp table to sort
8308
 
        }
8309
 
      }
8310
 
    }
8311
 
    if (change_list)
8312
 
      *prev_ptr= order;                         // use this entry
8313
 
    prev_ptr= &order->next;
8314
 
  }
8315
 
  if (change_list)
8316
 
    *prev_ptr=0;
8317
 
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8318
 
    *simple_order=1;
8319
 
  return(first_order);
8320
 
}
8321
 
 
8322
 
 
8323
 
static int
8324
 
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
8325
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
8326
 
                 const char *info, Item *having)
8327
 
{
8328
 
  if (select_options & SELECT_DESCRIBE)
8329
 
  {
8330
 
    select_describe(join, false, false, false, info);
8331
 
    return(0);
8332
 
  }
8333
 
 
8334
 
  join->join_free();
8335
 
 
8336
 
  if (send_row)
8337
 
  {
8338
 
    for (TableList *table= tables; table; table= table->next_leaf)
8339
 
      mark_as_null_row(table->table);           // All fields are NULL
8340
 
    if (having && having->val_int() == 0)
8341
 
      send_row=0;
8342
 
  }
8343
 
  if (!(result->send_fields(fields,
8344
 
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8345
 
  {
8346
 
    if (send_row)
8347
 
    {
8348
 
      List_iterator_fast<Item> it(fields);
8349
 
      Item *item;
8350
 
      while ((item= it++))
8351
 
        item->no_rows_in_result();
8352
 
      result->send_data(fields);
8353
 
    }
8354
 
    result->send_eof();                         // Should be safe
8355
 
  }
8356
 
  /* Update results for FOUND_ROWS */
8357
 
  join->session->limit_found_rows= join->session->examined_row_count= 0;
8358
 
  return(0);
8359
 
}
8360
 
 
8361
 
/*
8362
 
  used only in JOIN::clear
8363
 
*/
8364
 
static void clear_tables(JOIN *join)
8365
 
{
8366
 
  /*
8367
 
    must clear only the non-const tables, as const tables
8368
 
    are not re-calculated.
8369
 
  */
8370
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
8371
 
    mark_as_null_row(join->table[i]);           // All fields are NULL
8372
 
}
8373
 
 
8374
 
/*****************************************************************************
8375
 
  Make som simple condition optimization:
8376
 
  If there is a test 'field = const' change all refs to 'field' to 'const'
8377
 
  Remove all dummy tests 'item = item', 'const op const'.
8378
 
  Remove all 'item is NULL', when item can never be null!
8379
 
  item->marker should be 0 for all items on entry
8380
 
  Return in cond_value false if condition is impossible (1 = 2)
8381
 
*****************************************************************************/
8382
 
 
8383
 
class COND_CMP :public ilink {
8384
 
public:
8385
 
  static void *operator new(size_t size)
8386
 
  {
8387
 
    return (void*) sql_alloc((uint) size);
8388
 
  }
8389
 
  static void operator delete(void *, size_t)
8390
 
  { TRASH(ptr, size); }
8391
 
 
8392
 
  Item *and_level;
8393
 
  Item_func *cmp_func;
8394
 
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8395
 
};
8396
 
 
8397
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8398
 
template class I_List<COND_CMP>;
8399
 
template class I_List_iterator<COND_CMP>;
8400
 
#endif
8401
 
 
 
1396
      return (tab->eq_ref_table= 0);
 
1397
  }
 
1398
  return tab->eq_ref_table= 1;
 
1399
}
8402
1400
 
8403
1401
/**
8404
1402
  Find the multiple equality predicate containing a field.
8418
1416
    - Item_equal for the found multiple equality predicate if a success;
8419
1417
    - NULL otherwise.
8420
1418
*/
8421
 
 
8422
 
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8423
 
                            bool *inherited_fl)
 
1419
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
8424
1420
{
8425
1421
  Item_equal *item= 0;
8426
1422
  bool in_upper_level= false;
8427
1423
  while (cond_equal)
8428
1424
  {
8429
 
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
 
1425
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
8430
1426
    while ((item= li++))
8431
1427
    {
8432
1428
      if (item->contains(field))
 
1429
      {
8433
1430
        goto finish;
 
1431
      }
8434
1432
    }
8435
1433
    in_upper_level= true;
8436
1434
    cond_equal= cond_equal->upper_levels;
8437
1435
  }
8438
1436
  in_upper_level= false;
 
1437
 
8439
1438
finish:
8440
1439
  *inherited_fl= in_upper_level;
8441
1440
  return item;
8442
1441
}
8443
1442
 
8444
 
 
8445
1443
/**
8446
1444
  Check whether an equality can be used to build multiple equalities.
8447
1445
 
8523
1521
  @retval
8524
1522
    false   otherwise
8525
1523
*/
8526
 
 
8527
 
static bool check_simple_equality(Item *left_item, Item *right_item,
8528
 
                                  Item *item, COND_EQUAL *cond_equal)
 
1524
static bool check_simple_equality(Item *left_item,
 
1525
                                  Item *right_item,
 
1526
                                  Item *item,
 
1527
                                  COND_EQUAL *cond_equal)
8529
1528
{
8530
1529
  if (left_item->type() == Item::FIELD_ITEM &&
8531
1530
      right_item->type() == Item::FIELD_ITEM &&
8570
1569
      left_item_equal= new Item_equal(left_item_equal);
8571
1570
      cond_equal->current_level.push_back(left_item_equal);
8572
1571
      if (copy_item_name)
 
1572
      {
8573
1573
        left_item_equal->name = item->name;
 
1574
      }
8574
1575
    }
8575
1576
    if (right_copyfl)
8576
1577
    {
8578
1579
      right_item_equal= new Item_equal(right_item_equal);
8579
1580
      cond_equal->current_level.push_back(right_item_equal);
8580
1581
      if (copy_item_name)
 
1582
      {
8581
1583
        right_item_equal->name = item->name;
 
1584
      }
8582
1585
    }
8583
1586
 
8584
1587
    if (left_item_equal)
8591
1594
        /* Merge two multiple equalities forming a new one */
8592
1595
        left_item_equal->merge(right_item_equal);
8593
1596
        /* Remove the merged multiple equality from the list */
8594
 
        List_iterator<Item_equal> li(cond_equal->current_level);
 
1597
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
8595
1598
        while ((li++) != right_item_equal) {};
8596
1599
        li.remove();
8597
1600
      }
8603
1606
      {
8604
1607
        right_item_equal->add((Item_field *) left_item);
8605
1608
        if (copy_item_name)
 
1609
        {
8606
1610
          right_item_equal->name = item->name;
 
1611
        }
8607
1612
      }
8608
1613
      else
8609
1614
      {
8612
1617
                                               (Item_field *) right_item);
8613
1618
        cond_equal->current_level.push_back(item_equal);
8614
1619
        if (copy_item_name)
 
1620
        {
8615
1621
          item_equal->name = item->name;
 
1622
        }
8616
1623
      }
8617
1624
    }
8618
1625
    return true;
8644
1651
 
8645
1652
      if (field_item->result_type() == STRING_RESULT)
8646
1653
      {
8647
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
1654
        const charset_info_st * const cs= ((Field_str*) field_item->field)->charset();
8648
1655
        if (!item)
8649
1656
        {
8650
1657
          Item_func_eq *eq_item;
8651
 
          if ((eq_item= new Item_func_eq(left_item, right_item)))
8652
 
            return false;
 
1658
          eq_item= new Item_func_eq(left_item, right_item);
8653
1659
          eq_item->set_cmp_func();
8654
1660
          eq_item->quick_fix_field();
8655
1661
          item= eq_item;
8656
1662
        }
8657
 
        if ((cs != ((Item_func *) item)->compare_collation()) ||
8658
 
            !cs->coll->propagate(cs, 0, 0))
 
1663
 
 
1664
        if ((cs != ((Item_func *) item)->compare_collation()) || !cs->coll->propagate())
 
1665
        {
8659
1666
          return false;
 
1667
        }
8660
1668
      }
8661
1669
 
8662
1670
      Item_equal *item_equal = find_item_equal(cond_equal,
8683
1691
      return true;
8684
1692
    }
8685
1693
  }
 
1694
 
8686
1695
  return false;
8687
1696
}
8688
1697
 
8689
 
 
8690
1698
/**
8691
1699
  Convert row equalities into a conjunction of regular equalities.
8692
1700
 
8712
1720
  @retval
8713
1721
    false   otherwise
8714
1722
*/
8715
 
 
8716
 
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
8717
 
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
1723
static bool check_row_equality(Session *session,
 
1724
                               Item *left_row,
 
1725
                               Item_row *right_row,
 
1726
                               COND_EQUAL *cond_equal,
 
1727
                               List<Item>* eq_list)
8718
1728
{
8719
1729
  uint32_t n= left_row->cols();
8720
1730
  for (uint32_t i= 0 ; i < n; i++)
8730
1740
                                       (Item_row *) right_item,
8731
1741
                                       cond_equal, eq_list);
8732
1742
      if (!is_converted)
8733
 
        session->lex->current_select->cond_count++;
 
1743
      {
 
1744
        session->lex().current_select->cond_count++;
 
1745
      }
8734
1746
    }
8735
1747
    else
8736
1748
    {
8737
1749
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
8738
 
      session->lex->current_select->cond_count++;
 
1750
      session->lex().current_select->cond_count++;
8739
1751
    }
8740
1752
 
8741
1753
    if (!is_converted)
8742
1754
    {
8743
1755
      Item_func_eq *eq_item;
8744
 
      if (!(eq_item= new Item_func_eq(left_item, right_item)))
8745
 
        return false;
 
1756
      eq_item= new Item_func_eq(left_item, right_item);
8746
1757
      eq_item->set_cmp_func();
8747
1758
      eq_item->quick_fix_field();
8748
1759
      eq_list->push_back(eq_item);
8751
1762
  return true;
8752
1763
}
8753
1764
 
8754
 
 
8755
1765
/**
8756
1766
  Eliminate row equalities and form multiple equalities predicates.
8757
1767
 
8781
1791
           or, if the equality is neither a simple one nor a row equality,
8782
1792
           or, if the procedure fails by a fatal error.
8783
1793
*/
8784
 
 
8785
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
8786
 
                           List<Item> *eq_list)
 
1794
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
8787
1795
{
8788
1796
  if (item->type() == Item::FUNC_ITEM &&
8789
1797
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
8794
1802
    if (left_item->type() == Item::ROW_ITEM &&
8795
1803
        right_item->type() == Item::ROW_ITEM)
8796
1804
    {
8797
 
      session->lex->current_select->cond_count--;
 
1805
      session->lex().current_select->cond_count--;
8798
1806
      return check_row_equality(session,
8799
1807
                                (Item_row *) left_item,
8800
1808
                                (Item_row *) right_item,
8801
1809
                                cond_equal, eq_list);
8802
1810
    }
8803
1811
    else
 
1812
    {
8804
1813
      return check_simple_equality(left_item, right_item, item, cond_equal);
 
1814
    }
8805
1815
  }
8806
1816
  return false;
8807
1817
}
8808
1818
 
8809
 
 
8810
1819
/**
8811
1820
  Replace all equality predicates in a condition by multiple equality items.
8812
1821
 
8826
1835
    just an argument of a comparison predicate.
8827
1836
    The function also determines the maximum number of members in
8828
1837
    equality lists of each Item_cond_and object assigning it to
8829
 
    session->lex->current_select->max_equal_elems.
 
1838
    session->lex().current_select->max_equal_elems.
8830
1839
 
8831
1840
  @note
8832
1841
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
8870
1879
  @return
8871
1880
    pointer to the transformed condition
8872
1881
*/
8873
 
 
8874
 
static COND *build_equal_items_for_cond(Session *session, COND *cond,
8875
 
                                        COND_EQUAL *inherited)
 
1882
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
8876
1883
{
8877
1884
  Item_equal *item_equal;
8878
1885
  COND_EQUAL cond_equal;
8885
1892
      Item_func::COND_AND_FUNC;
8886
1893
    List<Item> *args= ((Item_cond*) cond)->argument_list();
8887
1894
 
8888
 
    List_iterator<Item> li(*args);
 
1895
    List<Item>::iterator li(args->begin());
8889
1896
    Item *item;
8890
1897
 
8891
1898
    if (and_level)
8904
1911
          re-execution of any prepared statement/stored procedure.
8905
1912
        */
8906
1913
        if (check_equality(session, item, &cond_equal, &eq_list))
 
1914
        {
8907
1915
          li.remove();
 
1916
        }
8908
1917
      }
8909
1918
 
8910
 
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
 
1919
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
8911
1920
      while ((item_equal= it++))
8912
1921
      {
8913
1922
        item_equal->fix_length_and_dec();
8914
1923
        item_equal->update_used_tables();
8915
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
1924
        set_if_bigger(session->lex().current_select->max_equal_elems,
8916
1925
                      item_equal->members());
8917
1926
      }
8918
1927
 
8923
1932
       Make replacement of equality predicates for lower levels
8924
1933
       of the condition expression.
8925
1934
    */
8926
 
    li.rewind();
 
1935
    li= args->begin();
8927
1936
    while ((item= li++))
8928
1937
    {
8929
1938
      Item *new_item;
8959
1968
     */
8960
1969
    if (check_equality(session, cond, &cond_equal, &eq_list))
8961
1970
    {
8962
 
      int n= cond_equal.current_level.elements + eq_list.elements;
 
1971
      int n= cond_equal.current_level.size() + eq_list.size();
 
1972
 
8963
1973
      if (n == 0)
 
1974
      {
8964
1975
        return new Item_int((int64_t) 1,1);
 
1976
      }
8965
1977
      else if (n == 1)
8966
1978
      {
8967
1979
        if ((item_equal= cond_equal.current_level.pop()))
8968
1980
        {
8969
1981
          item_equal->fix_length_and_dec();
8970
1982
          item_equal->update_used_tables();
8971
 
        }
 
1983
        }
8972
1984
        else
 
1985
        {
8973
1986
          item_equal= (Item_equal *) eq_list.pop();
8974
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
1987
        }
 
1988
        set_if_bigger(session->lex().current_select->max_equal_elems,
8975
1989
                      item_equal->members());
8976
1990
        return item_equal;
8977
1991
      }
8980
1994
        /*
8981
1995
          Here a new AND level must be created. It can happen only
8982
1996
          when a row equality is processed as a standalone predicate.
8983
 
        */
 
1997
        */
8984
1998
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
8985
1999
        and_cond->quick_fix_field();
8986
2000
        List<Item> *args= and_cond->argument_list();
8987
 
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
 
2001
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
8988
2002
        while ((item_equal= it++))
8989
2003
        {
8990
2004
          item_equal->fix_length_and_dec();
8991
2005
          item_equal->update_used_tables();
8992
 
          set_if_bigger(session->lex->current_select->max_equal_elems,
 
2006
          set_if_bigger(session->lex().current_select->max_equal_elems,
8993
2007
                        item_equal->members());
8994
2008
        }
8995
2009
        and_cond->cond_equal= cond_equal;
9014
2028
  return cond;
9015
2029
}
9016
2030
 
9017
 
 
9018
2031
/**
9019
2032
  Build multiple equalities for a condition and all on expressions that
9020
2033
  inherit these multiple equalities.
9069
2082
    can get more freedom in performing join operations.
9070
2083
    Althogh we don't use this property now, it probably makes sense to use
9071
2084
    it in the future.
9072
 
  @param session                      Thread handler
 
2085
  @param session                      Thread Cursor
9073
2086
  @param cond                condition to build the multiple equalities for
9074
2087
  @param inherited           path to all inherited multiple equality items
9075
2088
  @param join_list           list of join tables to which the condition
9080
2093
  @return
9081
2094
    pointer to the transformed condition containing multiple equalities
9082
2095
*/
9083
 
 
9084
2096
static COND *build_equal_items(Session *session, COND *cond,
9085
2097
                               COND_EQUAL *inherited,
9086
2098
                               List<TableList> *join_list,
9092
2104
  {
9093
2105
    cond= build_equal_items_for_cond(session, cond, inherited);
9094
2106
    cond->update_used_tables();
9095
 
    if (cond->type() == Item::COND_ITEM &&
9096
 
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
2107
 
 
2108
    if (cond->type() == Item::COND_ITEM && ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
2109
    {
9097
2110
      cond_equal= &((Item_cond_and*) cond)->cond_equal;
 
2111
    }
9098
2112
    else if (cond->type() == Item::FUNC_ITEM &&
9099
2113
             ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9100
2114
    {
9112
2126
  if (join_list)
9113
2127
  {
9114
2128
    TableList *table;
9115
 
    List_iterator<TableList> li(*join_list);
 
2129
    List<TableList>::iterator li(join_list->begin());
9116
2130
 
9117
2131
    while ((table= li++))
9118
2132
    {
9119
2133
      if (table->on_expr)
9120
2134
      {
9121
 
        List<TableList> *nested_join_list= table->nested_join ?
9122
 
          &table->nested_join->join_list : NULL;
 
2135
        List<TableList> *nested_join_list= table->getNestedJoin() ?
 
2136
          &table->getNestedJoin()->join_list : NULL;
9123
2137
        /*
9124
2138
          We can modify table->on_expr because its old value will
9125
2139
          be restored before re-execution of PS/SP.
9134
2148
  return cond;
9135
2149
}
9136
2150
 
9137
 
 
9138
2151
/**
9139
2152
  Compare field items by table order in the execution plan.
9140
2153
 
9154
2167
  @retval
9155
2168
    0  otherwise
9156
2169
*/
9157
 
 
9158
2170
static int compare_fields_by_table_order(Item_field *field1,
9159
 
                                  Item_field *field2,
9160
 
                                  void *table_join_idx)
 
2171
                                         Item_field *field2,
 
2172
                                         void *table_join_idx)
9161
2173
{
9162
2174
  int cmp= 0;
9163
2175
  bool outer_ref= 0;
9166
2178
    outer_ref= 1;
9167
2179
    cmp= -1;
9168
2180
  }
 
2181
 
9169
2182
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9170
2183
  {
9171
2184
    outer_ref= 1;
9172
2185
    cmp++;
9173
2186
  }
 
2187
 
9174
2188
  if (outer_ref)
 
2189
  {
9175
2190
    return cmp;
9176
 
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
9177
 
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
 
2191
  }
 
2192
 
 
2193
  JoinTable **idx= (JoinTable **) table_join_idx;
 
2194
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
2195
 
9178
2196
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
9179
2197
}
9180
2198
 
9181
 
 
9182
2199
/**
9183
2200
  Generate minimal set of simple equalities equivalent to a multiple equality.
9184
2201
 
9218
2235
    a pointer to the simple generated equality, if success.
9219
2236
    - 0, otherwise.
9220
2237
*/
9221
 
 
9222
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9223
 
                                  Item_equal *item_equal)
 
2238
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
9224
2239
{
9225
2240
  List<Item> eq_list;
9226
2241
  Item_func_eq *eq_item= 0;
9227
2242
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9228
2243
    return new Item_int((int64_t) 0,1);
9229
2244
  Item *item_const= item_equal->get_const();
9230
 
  Item_equal_iterator it(*item_equal);
 
2245
  Item_equal_iterator it(item_equal->begin());
9231
2246
  Item *head;
9232
2247
  if (item_const)
 
2248
  {
9233
2249
    head= item_const;
 
2250
  }
9234
2251
  else
9235
2252
  {
9236
2253
    head= item_equal->get_first();
9244
2261
    if (upper)
9245
2262
    {
9246
2263
      if (item_const && upper->get_const())
 
2264
      {
9247
2265
        item= 0;
 
2266
      }
9248
2267
      else
9249
2268
      {
9250
 
        Item_equal_iterator li(*item_equal);
 
2269
        Item_equal_iterator li(item_equal->begin());
9251
2270
        while ((item= li++) != item_field)
9252
2271
        {
9253
2272
          if (item->find_item_equal(upper_levels) == upper)
 
2273
          {
9254
2274
            break;
 
2275
          }
9255
2276
        }
9256
2277
      }
9257
2278
    }
9258
2279
    if (item == item_field)
9259
2280
    {
9260
2281
      if (eq_item)
 
2282
      {
9261
2283
        eq_list.push_back(eq_item);
 
2284
      }
 
2285
 
9262
2286
      eq_item= new Item_func_eq(item_field, head);
 
2287
 
9263
2288
      if (!eq_item)
 
2289
      {
9264
2290
        return 0;
 
2291
      }
9265
2292
      eq_item->set_cmp_func();
9266
2293
      eq_item->quick_fix_field();
9267
2294
   }
9268
2295
  }
9269
2296
 
9270
 
  if (!cond && !eq_list.head())
 
2297
  if (!cond && !&eq_list.front())
9271
2298
  {
9272
2299
    if (!eq_item)
 
2300
    {
9273
2301
      return new Item_int((int64_t) 1,1);
 
2302
    }
9274
2303
    return eq_item;
9275
2304
  }
9276
2305
 
9277
2306
  if (eq_item)
 
2307
  {
9278
2308
    eq_list.push_back(eq_item);
 
2309
  }
 
2310
 
9279
2311
  if (!cond)
 
2312
  {
9280
2313
    cond= new Item_cond_and(eq_list);
 
2314
  }
9281
2315
  else
9282
2316
  {
9283
2317
    assert(cond->type() == Item::COND_ITEM);
9290
2324
  return cond;
9291
2325
}
9292
2326
 
9293
 
 
9294
2327
/**
9295
2328
  Substitute every field reference in a condition by the best equal field
9296
2329
  and eliminate all multiple equality predicates.
9318
2351
  @return
9319
2352
    The transformed condition
9320
2353
*/
9321
 
 
9322
 
static COND* substitute_for_best_equal_field(COND *cond,
9323
 
                                             COND_EQUAL *cond_equal,
9324
 
                                             void *table_join_idx)
 
2354
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
9325
2355
{
9326
2356
  Item_equal *item_equal;
9327
2357
 
9336
2366
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
9337
2367
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
9338
2368
 
9339
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
2369
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
9340
2370
      while ((item_equal= it++))
9341
2371
      {
9342
2372
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9343
2373
      }
9344
2374
    }
9345
2375
 
9346
 
    List_iterator<Item> li(*cond_list);
 
2376
    List<Item>::iterator li(cond_list->begin());
9347
2377
    Item *item;
9348
2378
    while ((item= li++))
9349
2379
    {
9359
2389
 
9360
2390
    if (and_level)
9361
2391
    {
9362
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
2392
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
9363
2393
      while ((item_equal= it++))
9364
2394
      {
9365
2395
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
9370
2400
          break;
9371
2401
      }
9372
2402
    }
9373
 
    if (cond->type() == Item::COND_ITEM &&
9374
 
        !((Item_cond*)cond)->argument_list()->elements)
 
2403
 
 
2404
    if (cond->type() == Item::COND_ITEM && !((Item_cond*)cond)->argument_list()->size())
 
2405
    {
9375
2406
      cond= new Item_int((int32_t)cond->val_bool());
 
2407
    }
9376
2408
 
9377
2409
  }
9378
2410
  else if (cond->type() == Item::FUNC_ITEM &&
9380
2412
  {
9381
2413
    item_equal= (Item_equal *) cond;
9382
2414
    item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9383
 
    if (cond_equal && cond_equal->current_level.head() == item_equal)
 
2415
    if (cond_equal && &cond_equal->current_level.front() == item_equal)
 
2416
    {
9384
2417
      cond_equal= 0;
 
2418
    }
 
2419
 
9385
2420
    return eliminate_item_equal(0, cond_equal, item_equal);
9386
2421
  }
9387
2422
  else
 
2423
  {
9388
2424
    cond->transform(&Item::replace_equal_field, 0);
 
2425
  }
 
2426
 
9389
2427
  return cond;
9390
2428
}
9391
2429
 
9392
 
 
9393
2430
/**
9394
2431
  Check appearance of new constant items in multiple equalities
9395
2432
  of a condition after reading a constant table.
9402
2439
  @param cond       condition whose multiple equalities are to be checked
9403
2440
  @param table      constant table that has been read
9404
2441
*/
9405
 
 
9406
 
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
 
2442
void update_const_equal_items(COND *cond, JoinTable *tab)
9407
2443
{
9408
2444
  if (!(cond->used_tables() & tab->table->map))
9409
2445
    return;
9411
2447
  if (cond->type() == Item::COND_ITEM)
9412
2448
  {
9413
2449
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9414
 
    List_iterator_fast<Item> li(*cond_list);
 
2450
    List<Item>::iterator li(cond_list->begin());
9415
2451
    Item *item;
9416
2452
    while ((item= li++))
9417
2453
      update_const_equal_items(item, tab);
9425
2461
    if (!contained_const && item_equal->get_const())
9426
2462
    {
9427
2463
      /* Update keys for range analysis */
9428
 
      Item_equal_iterator it(*item_equal);
 
2464
      Item_equal_iterator it(item_equal->begin());
9429
2465
      Item_field *item_field;
9430
2466
      while ((item_field= it++))
9431
2467
      {
9432
2468
        Field *field= item_field->field;
9433
 
        JOIN_TAB *stat= field->table->reginfo.join_tab;
 
2469
        JoinTable *stat= field->getTable()->reginfo.join_tab;
9434
2470
        key_map possible_keys= field->key_start;
9435
 
        possible_keys.intersect(field->table->keys_in_use_for_query);
9436
 
        stat[0].const_keys.merge(possible_keys);
 
2471
        possible_keys&= field->getTable()->keys_in_use_for_query;
 
2472
        stat[0].const_keys|= possible_keys;
9437
2473
 
9438
2474
        /*
9439
2475
          For each field in the multiple equality (for which we know that it
9440
2476
          is a constant) we have to find its corresponding key part, and set
9441
2477
          that key part in const_key_parts.
9442
2478
        */
9443
 
        if (!possible_keys.is_clear_all())
 
2479
        if (possible_keys.any())
9444
2480
        {
9445
 
          Table *tab= field->table;
9446
 
          KEYUSE *use;
9447
 
          for (use= stat->keyuse; use && use->table == tab; use++)
9448
 
            if (possible_keys.is_set(use->key) &&
9449
 
                tab->key_info[use->key].key_part[use->keypart].field ==
 
2481
          Table *field_tab= field->getTable();
 
2482
          optimizer::KeyUse *use;
 
2483
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
 
2484
            if (possible_keys.test(use->getKey()) &&
 
2485
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
9450
2486
                field)
9451
 
              tab->const_key_parts[use->key]|= use->keypart_map;
 
2487
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
9452
2488
        }
9453
2489
      }
9454
2490
    }
9455
2491
  }
9456
2492
}
9457
2493
 
9458
 
 
9459
2494
/*
9460
2495
  change field = field to field = const for each found field = const in the
9461
2496
  and_level
9462
2497
*/
9463
 
 
9464
 
static void
9465
 
change_cond_ref_to_const(Session *session, I_List<COND_CMP> *save_list,
9466
 
                         Item *and_father, Item *cond,
9467
 
                         Item *field, Item *value)
 
2498
static void change_cond_ref_to_const(Session *session,
 
2499
                                     list<COND_CMP>& save_list,
 
2500
                                     Item *and_father,
 
2501
                                     Item *cond,
 
2502
                                     Item *field,
 
2503
                                     Item *value)
9468
2504
{
9469
2505
  if (cond->type() == Item::COND_ITEM)
9470
2506
  {
9471
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9472
 
      Item_func::COND_AND_FUNC;
9473
 
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
2507
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
2508
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
9474
2509
    Item *item;
9475
2510
    while ((item=li++))
9476
 
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
9477
 
                               field, value);
 
2511
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
 
2512
 
9478
2513
    return;
9479
2514
  }
 
2515
 
9480
2516
  if (cond->eq_cmp_result() == Item::COND_OK)
 
2517
  {
9481
2518
    return;                                     // Not a boolean function
 
2519
  }
9482
2520
 
9483
2521
  Item_bool_func2 *func=  (Item_bool_func2*) cond;
9484
2522
  Item **args= func->arguments();
9493
2531
       left_item->collation.collation == value->collation.collation))
9494
2532
  {
9495
2533
    Item *tmp=value->clone_item();
9496
 
    tmp->collation.set(right_item->collation);
9497
 
 
9498
2534
    if (tmp)
9499
2535
    {
9500
 
      session->change_item_tree(args + 1, tmp);
 
2536
      tmp->collation.set(right_item->collation);
 
2537
      args[1]= tmp;
9501
2538
      func->update_used_tables();
9502
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9503
 
          && and_father != cond && !left_item->const_item())
 
2539
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
2540
                and_father != cond &&
 
2541
          ! left_item->const_item())
9504
2542
      {
9505
 
        cond->marker=1;
9506
 
        COND_CMP *tmp2;
9507
 
        if ((tmp2=new COND_CMP(and_father,func)))
9508
 
          save_list->push_back(tmp2);
 
2543
        cond->marker=1;
 
2544
        save_list.push_back( COND_CMP(and_father, func) );
9509
2545
      }
9510
2546
      func->set_cmp_func();
9511
2547
    }
9517
2553
            right_item->collation.collation == value->collation.collation))
9518
2554
  {
9519
2555
    Item *tmp= value->clone_item();
9520
 
    tmp->collation.set(left_item->collation);
9521
 
 
9522
2556
    if (tmp)
9523
2557
    {
9524
 
      session->change_item_tree(args, tmp);
 
2558
      tmp->collation.set(left_item->collation);
 
2559
      *args= tmp;
9525
2560
      value= tmp;
9526
2561
      func->update_used_tables();
9527
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9528
 
          && and_father != cond && !right_item->const_item())
 
2562
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
2563
          and_father != cond &&
 
2564
          ! right_item->const_item())
9529
2565
      {
9530
2566
        args[0]= args[1];                       // For easy check
9531
 
        session->change_item_tree(args + 1, value);
9532
 
        cond->marker=1;
9533
 
        COND_CMP *tmp2;
9534
 
        if ((tmp2=new COND_CMP(and_father,func)))
9535
 
          save_list->push_back(tmp2);
 
2567
        args[1]= value;
 
2568
        cond->marker=1;
 
2569
        save_list.push_back( COND_CMP(and_father, func) );
9536
2570
      }
9537
2571
      func->set_cmp_func();
9538
2572
    }
9547
2581
  @return
9548
2582
    new conditions
9549
2583
*/
9550
 
 
9551
 
static Item *remove_additional_cond(Item* conds)
 
2584
Item *remove_additional_cond(Item* conds)
9552
2585
{
9553
2586
  if (conds->name == in_additional_cond)
 
2587
  {
9554
2588
    return 0;
 
2589
  }
 
2590
 
9555
2591
  if (conds->type() == Item::COND_ITEM)
9556
2592
  {
9557
2593
    Item_cond *cnd= (Item_cond*) conds;
9558
 
    List_iterator<Item> li(*(cnd->argument_list()));
 
2594
    List<Item>::iterator li(cnd->argument_list()->begin());
9559
2595
    Item *item;
9560
2596
    while ((item= li++))
9561
2597
    {
9562
2598
      if (item->name == in_additional_cond)
9563
2599
      {
9564
2600
        li.remove();
9565
 
        if (cnd->argument_list()->elements == 1)
9566
 
          return cnd->argument_list()->head();
 
2601
        if (cnd->argument_list()->size() == 1)
 
2602
        {
 
2603
          return &cnd->argument_list()->front();
 
2604
        }
 
2605
 
9567
2606
        return conds;
9568
2607
      }
9569
2608
    }
9571
2610
  return conds;
9572
2611
}
9573
2612
 
9574
 
static void
9575
 
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
9576
 
                         COND *and_father, COND *cond)
 
2613
static void propagate_cond_constants(Session *session,
 
2614
                                     list<COND_CMP>& save_list,
 
2615
                                     COND *and_father,
 
2616
                                     COND *cond)
9577
2617
{
9578
2618
  if (cond->type() == Item::COND_ITEM)
9579
2619
  {
9580
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9581
 
      Item_func::COND_AND_FUNC;
9582
 
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
2620
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
2621
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
9583
2622
    Item *item;
9584
 
    I_List<COND_CMP> save;
 
2623
    list<COND_CMP> save;
9585
2624
    while ((item=li++))
9586
2625
    {
9587
 
      propagate_cond_constants(session, &save,and_level ? cond : item, item);
 
2626
      propagate_cond_constants(session, save, and_level ? cond : item, item);
9588
2627
    }
9589
2628
    if (and_level)
9590
 
    {                                           // Handle other found items
9591
 
      I_List_iterator<COND_CMP> cond_itr(save);
9592
 
      COND_CMP *cond_cmp;
9593
 
      while ((cond_cmp=cond_itr++))
 
2629
    {
 
2630
      // Handle other found items
 
2631
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
9594
2632
      {
9595
 
        Item **args= cond_cmp->cmp_func->arguments();
9596
 
        if (!args[0]->const_item())
9597
 
          change_cond_ref_to_const(session, &save,cond_cmp->and_level,
9598
 
                                   cond_cmp->and_level, args[0], args[1]);
 
2633
        Item **args= iter->second->arguments();
 
2634
        if (not args[0]->const_item())
 
2635
        {
 
2636
          change_cond_ref_to_const(session, save, iter->first,
 
2637
                                   iter->first, args[0], args[1] );
 
2638
        }
9599
2639
      }
9600
2640
    }
9601
2641
  }
9602
2642
  else if (and_father != cond && !cond->marker)         // In a AND group
9603
2643
  {
9604
2644
    if (cond->type() == Item::FUNC_ITEM &&
9605
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9606
 
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
2645
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
2646
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
9607
2647
    {
9608
2648
      Item_func_eq *func=(Item_func_eq*) cond;
9609
2649
      Item **args= func->arguments();
9610
2650
      bool left_const= args[0]->const_item();
9611
2651
      bool right_const= args[1]->const_item();
9612
 
      if (!(left_const && right_const) &&
9613
 
          args[0]->result_type() == args[1]->result_type())
 
2652
      if (!(left_const && right_const) && args[0]->result_type() == args[1]->result_type())
9614
2653
      {
9615
 
        if (right_const)
9616
 
        {
 
2654
        if (right_const)
 
2655
        {
9617
2656
          resolve_const_item(session, &args[1], args[0]);
9618
 
          func->update_used_tables();
 
2657
          func->update_used_tables();
9619
2658
          change_cond_ref_to_const(session, save_list, and_father, and_father,
9620
2659
                                   args[0], args[1]);
9621
 
        }
9622
 
        else if (left_const)
9623
 
        {
 
2660
        }
 
2661
        else if (left_const)
 
2662
        {
9624
2663
          resolve_const_item(session, &args[0], args[1]);
9625
 
          func->update_used_tables();
 
2664
          func->update_used_tables();
9626
2665
          change_cond_ref_to_const(session, save_list, and_father, and_father,
9627
2666
                                   args[1], args[0]);
9628
 
        }
9629
 
      }
9630
 
    }
9631
 
  }
9632
 
}
9633
 
 
9634
 
 
9635
 
/**
9636
 
  Simplify joins replacing outer joins by inner joins whenever it's
9637
 
  possible.
9638
 
 
9639
 
    The function, during a retrieval of join_list,  eliminates those
9640
 
    outer joins that can be converted into inner join, possibly nested.
9641
 
    It also moves the on expressions for the converted outer joins
9642
 
    and from inner joins to conds.
9643
 
    The function also calculates some attributes for nested joins:
9644
 
    - used_tables
9645
 
    - not_null_tables
9646
 
    - dep_tables.
9647
 
    - on_expr_dep_tables
9648
 
    The first two attributes are used to test whether an outer join can
9649
 
    be substituted for an inner join. The third attribute represents the
9650
 
    relation 'to be dependent on' for tables. If table t2 is dependent
9651
 
    on table t1, then in any evaluated execution plan table access to
9652
 
    table t2 must precede access to table t2. This relation is used also
9653
 
    to check whether the query contains  invalid cross-references.
9654
 
    The forth attribute is an auxiliary one and is used to calculate
9655
 
    dep_tables.
9656
 
    As the attribute dep_tables qualifies possibles orders of tables in the
9657
 
    execution plan, the dependencies required by the straight join
9658
 
    modifiers are reflected in this attribute as well.
9659
 
    The function also removes all braces that can be removed from the join
9660
 
    expression without changing its meaning.
9661
 
 
9662
 
  @note
9663
 
    An outer join can be replaced by an inner join if the where condition
9664
 
    or the on expression for an embedding nested join contains a conjunctive
9665
 
    predicate rejecting null values for some attribute of the inner tables.
9666
 
 
9667
 
    E.g. in the query:
9668
 
    @code
9669
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9670
 
    @endcode
9671
 
    the predicate t2.b < 5 rejects nulls.
9672
 
    The query is converted first to:
9673
 
    @code
9674
 
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9675
 
    @endcode
9676
 
    then to the equivalent form:
9677
 
    @code
9678
 
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9679
 
    @endcode
9680
 
 
9681
 
 
9682
 
    Similarly the following query:
9683
 
    @code
9684
 
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9685
 
        WHERE t2.c < 5
9686
 
    @endcode
9687
 
    is converted to:
9688
 
    @code
9689
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9690
 
 
9691
 
    @endcode
9692
 
 
9693
 
    One conversion might trigger another:
9694
 
    @code
9695
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9696
 
                       LEFT JOIN t3 ON t3.b=t2.b
9697
 
        WHERE t3 IS NOT NULL =>
9698
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9699
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9700
 
      SELECT * FROM t1, t2, t3
9701
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9702
 
  @endcode
9703
 
 
9704
 
    The function removes all unnecessary braces from the expression
9705
 
    produced by the conversions.
9706
 
    E.g.
9707
 
    @code
9708
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9709
 
    @endcode
9710
 
    finally is converted to:
9711
 
    @code
9712
 
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9713
 
 
9714
 
    @endcode
9715
 
 
9716
 
 
9717
 
    It also will remove braces from the following queries:
9718
 
    @code
9719
 
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9720
 
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9721
 
    @endcode
9722
 
 
9723
 
    The benefit of this simplification procedure is that it might return
9724
 
    a query for which the optimizer can evaluate execution plan with more
9725
 
    join orders. With a left join operation the optimizer does not
9726
 
    consider any plan where one of the inner tables is before some of outer
9727
 
    tables.
9728
 
 
9729
 
  IMPLEMENTATION
9730
 
    The function is implemented by a recursive procedure.  On the recursive
9731
 
    ascent all attributes are calculated, all outer joins that can be
9732
 
    converted are replaced and then all unnecessary braces are removed.
9733
 
    As join list contains join tables in the reverse order sequential
9734
 
    elimination of outer joins does not require extra recursive calls.
9735
 
 
9736
 
  SEMI-JOIN NOTES
9737
 
    Remove all semi-joins that have are within another semi-join (i.e. have
9738
 
    an "ancestor" semi-join nest)
9739
 
 
9740
 
  EXAMPLES
9741
 
    Here is an example of a join query with invalid cross references:
9742
 
    @code
9743
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9744
 
    @endcode
9745
 
 
9746
 
  @param join        reference to the query info
9747
 
  @param join_list   list representation of the join to be converted
9748
 
  @param conds       conditions to add on expressions for converted joins
9749
 
  @param top         true <=> conds is the where condition
9750
 
 
9751
 
  @return
9752
 
    - The new condition, if success
9753
 
    - 0, otherwise
9754
 
*/
9755
 
 
9756
 
static COND *
9757
 
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
9758
 
               bool in_sj)
9759
 
{
9760
 
  TableList *table;
9761
 
  nested_join_st *nested_join;
9762
 
  TableList *prev_table= 0;
9763
 
  List_iterator<TableList> li(*join_list);
9764
 
 
9765
 
  /*
9766
 
    Try to simplify join operations from join_list.
9767
 
    The most outer join operation is checked for conversion first.
9768
 
  */
9769
 
  while ((table= li++))
9770
 
  {
9771
 
    table_map used_tables;
9772
 
    table_map not_null_tables= (table_map) 0;
9773
 
 
9774
 
    if ((nested_join= table->nested_join))
9775
 
    {
9776
 
      /*
9777
 
         If the element of join_list is a nested join apply
9778
 
         the procedure to its nested join list first.
9779
 
      */
9780
 
      if (table->on_expr)
9781
 
      {
9782
 
        Item *expr= table->on_expr;
9783
 
        /*
9784
 
           If an on expression E is attached to the table,
9785
 
           check all null rejected predicates in this expression.
9786
 
           If such a predicate over an attribute belonging to
9787
 
           an inner table of an embedded outer join is found,
9788
 
           the outer join is converted to an inner join and
9789
 
           the corresponding on expression is added to E.
9790
 
        */
9791
 
        expr= simplify_joins(join, &nested_join->join_list,
9792
 
                             expr, false, in_sj || table->sj_on_expr);
9793
 
 
9794
 
        if (!table->prep_on_expr || expr != table->on_expr)
9795
 
        {
9796
 
          assert(expr);
9797
 
 
9798
 
          table->on_expr= expr;
9799
 
          table->prep_on_expr= expr->copy_andor_structure(join->session);
9800
 
        }
9801
 
      }
9802
 
      nested_join->used_tables= (table_map) 0;
9803
 
      nested_join->not_null_tables=(table_map) 0;
9804
 
      conds= simplify_joins(join, &nested_join->join_list, conds, top,
9805
 
                            in_sj || table->sj_on_expr);
9806
 
      used_tables= nested_join->used_tables;
9807
 
      not_null_tables= nested_join->not_null_tables;
9808
 
    }
9809
 
    else
9810
 
    {
9811
 
      if (!table->prep_on_expr)
9812
 
        table->prep_on_expr= table->on_expr;
9813
 
      used_tables= table->table->map;
9814
 
      if (conds)
9815
 
        not_null_tables= conds->not_null_tables();
9816
 
    }
9817
 
 
9818
 
    if (table->embedding)
9819
 
    {
9820
 
      table->embedding->nested_join->used_tables|= used_tables;
9821
 
      table->embedding->nested_join->not_null_tables|= not_null_tables;
9822
 
    }
9823
 
 
9824
 
    if (!table->outer_join || (used_tables & not_null_tables))
9825
 
    {
9826
 
      /*
9827
 
        For some of the inner tables there are conjunctive predicates
9828
 
        that reject nulls => the outer join can be replaced by an inner join.
9829
 
      */
9830
 
      table->outer_join= 0;
9831
 
      if (table->on_expr)
9832
 
      {
9833
 
        /* Add ON expression to the WHERE or upper-level ON condition. */
9834
 
        if (conds)
9835
 
        {
9836
 
          conds= and_conds(conds, table->on_expr);
9837
 
          conds->top_level_item();
9838
 
          /* conds is always a new item as both cond and on_expr existed */
9839
 
          assert(!conds->fixed);
9840
 
          conds->fix_fields(join->session, &conds);
9841
 
        }
9842
 
        else
9843
 
          conds= table->on_expr;
9844
 
        table->prep_on_expr= table->on_expr= 0;
9845
 
      }
9846
 
    }
9847
 
 
9848
 
    if (!top)
9849
 
      continue;
9850
 
 
9851
 
    /*
9852
 
      Only inner tables of non-convertible outer joins
9853
 
      remain with on_expr.
9854
 
    */
9855
 
    if (table->on_expr)
9856
 
    {
9857
 
      table->dep_tables|= table->on_expr->used_tables();
9858
 
      if (table->embedding)
9859
 
      {
9860
 
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
9861
 
        /*
9862
 
           Embedding table depends on tables used
9863
 
           in embedded on expressions.
9864
 
        */
9865
 
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9866
 
      }
9867
 
      else
9868
 
        table->dep_tables&= ~table->table->map;
9869
 
    }
9870
 
 
9871
 
    if (prev_table)
9872
 
    {
9873
 
      /* The order of tables is reverse: prev_table follows table */
9874
 
      if (prev_table->straight)
9875
 
        prev_table->dep_tables|= used_tables;
9876
 
      if (prev_table->on_expr)
9877
 
      {
9878
 
        prev_table->dep_tables|= table->on_expr_dep_tables;
9879
 
        table_map prev_used_tables= prev_table->nested_join ?
9880
 
                                    prev_table->nested_join->used_tables :
9881
 
                                    prev_table->table->map;
9882
 
        /*
9883
 
          If on expression contains only references to inner tables
9884
 
          we still make the inner tables dependent on the outer tables.
9885
 
          It would be enough to set dependency only on one outer table
9886
 
          for them. Yet this is really a rare case.
9887
 
        */
9888
 
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9889
 
          prev_table->dep_tables|= used_tables;
9890
 
      }
9891
 
    }
9892
 
    prev_table= table;
9893
 
  }
9894
 
 
9895
 
  /*
9896
 
    Flatten nested joins that can be flattened.
9897
 
    no ON expression and not a semi-join => can be flattened.
9898
 
  */
9899
 
  li.rewind();
9900
 
  while ((table= li++))
9901
 
  {
9902
 
    nested_join= table->nested_join;
9903
 
    if (table->sj_on_expr && !in_sj)
9904
 
    {
9905
 
       /*
9906
 
         If this is a semi-join that is not contained within another semi-join,
9907
 
         leave it intact (otherwise it is flattened)
9908
 
       */
9909
 
      join->select_lex->sj_nests.push_back(table);
9910
 
    }
9911
 
    else if (nested_join && !table->on_expr)
9912
 
    {
9913
 
      TableList *tbl;
9914
 
      List_iterator<TableList> it(nested_join->join_list);
9915
 
      while ((tbl= it++))
9916
 
      {
9917
 
        tbl->embedding= table->embedding;
9918
 
        tbl->join_list= table->join_list;
9919
 
      }
9920
 
      li.replace(nested_join->join_list);
9921
 
    }
9922
 
  }
9923
 
  return(conds);
9924
 
}
9925
 
 
9926
 
 
9927
 
/**
9928
 
  Assign each nested join structure a bit in nested_join_map.
9929
 
 
9930
 
    Assign each nested join structure (except "confluent" ones - those that
9931
 
    embed only one element) a bit in nested_join_map.
9932
 
 
9933
 
  @param join          Join being processed
9934
 
  @param join_list     List of tables
9935
 
  @param first_unused  Number of first unused bit in nested_join_map before the
9936
 
                       call
9937
 
 
9938
 
  @note
9939
 
    This function is called after simplify_joins(), when there are no
9940
 
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9941
 
    we will not run out of bits in nested_join_map.
9942
 
 
9943
 
  @return
9944
 
    First unused bit in nested_join_map after the call.
9945
 
*/
9946
 
 
9947
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
9948
 
                                          uint32_t first_unused)
9949
 
{
9950
 
  List_iterator<TableList> li(*join_list);
9951
 
  TableList *table;
9952
 
  while ((table= li++))
9953
 
  {
9954
 
    nested_join_st *nested_join;
9955
 
    if ((nested_join= table->nested_join))
9956
 
    {
9957
 
      /*
9958
 
        It is guaranteed by simplify_joins() function that a nested join
9959
 
        that has only one child is either
9960
 
         - a single-table view (the child is the underlying table), or
9961
 
         - a single-table semi-join nest
9962
 
 
9963
 
        We don't assign bits to such sj-nests because
9964
 
        1. it is redundant (a "sequence" of one table cannot be interleaved
9965
 
            with anything)
9966
 
        2. we could run out bits in nested_join_map otherwise.
9967
 
      */
9968
 
      if (nested_join->join_list.elements != 1)
9969
 
      {
9970
 
        /* Don't assign bits to sj-nests */
9971
 
        if (table->on_expr)
9972
 
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9973
 
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9974
 
                                                    first_unused);
9975
 
      }
9976
 
    }
9977
 
  }
9978
 
  return(first_unused);
9979
 
}
9980
 
 
9981
 
 
9982
 
/**
9983
 
  Set nested_join_st::counter=0 in all nested joins in passed list.
9984
 
 
9985
 
    Recursively set nested_join_st::counter=0 for all nested joins contained in
9986
 
    the passed join_list.
9987
 
 
9988
 
  @param join_list  List of nested joins to process. It may also contain base
9989
 
                    tables which will be ignored.
9990
 
*/
9991
 
 
9992
 
static void reset_nj_counters(List<TableList> *join_list)
9993
 
{
9994
 
  List_iterator<TableList> li(*join_list);
9995
 
  TableList *table;
9996
 
  while ((table= li++))
9997
 
  {
9998
 
    nested_join_st *nested_join;
9999
 
    if ((nested_join= table->nested_join))
10000
 
    {
10001
 
      nested_join->counter_= 0;
10002
 
      reset_nj_counters(&nested_join->join_list);
10003
 
    }
10004
 
  }
10005
 
  return;
10006
 
}
10007
 
 
 
2667
        }
 
2668
      }
 
2669
    }
 
2670
  }
 
2671
}
10008
2672
 
10009
2673
/**
10010
2674
  Check interleaving with an inner tables of an outer join for
10082
2746
         position:
10083
2747
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10084
2748
             joins) we've opened but didn't close.
10085
 
          2. {each nested_join_st structure not simplified away}->counter - number
 
2749
          2. {each NestedJoin class not simplified away}->counter - number
10086
2750
             of this nested join's children that have already been added to to
10087
2751
             the partial join order.
10088
2752
  @endverbatim
10089
2753
 
10090
2754
  @param join       Join being processed
10091
 
  @param last_tab   Last table in current partial join order (this function is
10092
 
                    not called for empty partial join orders)
10093
2755
  @param next_tab   Table we're going to extend the current partial join with
10094
2756
 
10095
2757
  @retval
10098
2760
  @retval
10099
2761
    true   Requested join order extension not allowed.
10100
2762
*/
10101
 
 
10102
 
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
 
2763
bool check_interleaving_with_nj(JoinTable *next_tab)
10103
2764
{
10104
 
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
10105
 
  JOIN *join= last_tab->join;
 
2765
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
 
2766
  Join *join= next_tab->join;
10106
2767
 
10107
 
  if (join->cur_embedding_map & ~next_tab->embedding_map)
 
2768
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
10108
2769
  {
10109
2770
    /*
10110
2771
      next_tab is outside of the "pair of brackets" we're currently in.
10117
2778
    Do update counters for "pairs of brackets" that we've left (marked as
10118
2779
    X,Y,Z in the above picture)
10119
2780
  */
10120
 
  for (;next_emb; next_emb= next_emb->embedding)
 
2781
  for (;next_emb; next_emb= next_emb->getEmbedding())
10121
2782
  {
10122
 
    next_emb->nested_join->counter_++;
10123
 
    if (next_emb->nested_join->counter_ == 1)
 
2783
    next_emb->getNestedJoin()->counter_++;
 
2784
    if (next_emb->getNestedJoin()->counter_ == 1)
10124
2785
    {
10125
2786
      /*
10126
2787
        next_emb is the first table inside a nested join we've "entered". In
10127
2788
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
10128
2789
        X bracket might have Y pair bracket.
10129
2790
      */
10130
 
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
 
2791
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
10131
2792
    }
10132
2793
 
10133
 
    if (next_emb->nested_join->join_list.elements !=
10134
 
        next_emb->nested_join->counter_)
 
2794
    if (next_emb->getNestedJoin()->join_list.size() != next_emb->getNestedJoin()->counter_)
 
2795
    {
10135
2796
      break;
 
2797
    }
10136
2798
 
10137
2799
    /*
10138
2800
      We're currently at Y or Z-bracket as depicted in the above picture.
10139
2801
      Mark that we've left it and continue walking up the brackets hierarchy.
10140
2802
    */
10141
 
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
 
2803
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
10142
2804
  }
10143
2805
  return false;
10144
2806
}
10145
2807
 
10146
 
 
10147
 
/**
10148
 
  Nested joins perspective: Remove the last table from the join order.
10149
 
 
10150
 
    Remove the last table from the partial join order and update the nested
10151
 
    joins counters and join->cur_embedding_map. It is ok to call this
10152
 
    function for the first table in join order (for which
10153
 
    check_interleaving_with_nj has not been called)
10154
 
 
10155
 
  @param last  join table to remove, it is assumed to be the last in current
10156
 
               partial join order.
10157
 
*/
10158
 
 
10159
 
static void restore_prev_nj_state(JOIN_TAB *last)
10160
 
{
10161
 
  TableList *last_emb= last->table->pos_in_table_list->embedding;
10162
 
  JOIN *join= last->join;
10163
 
  while (last_emb)
10164
 
  {
10165
 
    if (last_emb->on_expr)
10166
 
    {
10167
 
      if (!(--last_emb->nested_join->counter_))
10168
 
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10169
 
      else if (last_emb->nested_join->join_list.elements-1 ==
10170
 
               last_emb->nested_join->counter_)
10171
 
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
10172
 
      else
10173
 
        break;
10174
 
    }
10175
 
    last_emb= last_emb->embedding;
10176
 
  }
10177
 
}
10178
 
 
10179
 
 
10180
 
 
10181
 
static
10182
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10183
 
{
10184
 
  TableList *emb_sj_nest;
10185
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10186
 
  {
10187
 
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10188
 
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
10189
 
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
10190
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10191
 
  }
10192
 
}
10193
 
 
10194
 
 
10195
 
/*
10196
 
  we assume remaining_tables doesnt contain @tab.
10197
 
*/
10198
 
 
10199
 
static void restore_prev_sj_state(const table_map remaining_tables,
10200
 
                                  const JOIN_TAB *tab)
10201
 
{
10202
 
  TableList *emb_sj_nest;
10203
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10204
 
  {
10205
 
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10206
 
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) ==
10207
 
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
10208
 
    {
10209
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10210
 
    }
10211
 
  }
10212
 
}
10213
 
 
10214
 
 
10215
 
static COND *
10216
 
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
10217
 
              Item::cond_result *cond_value)
 
2808
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
10218
2809
{
10219
2810
  Session *session= join->session;
10220
2811
 
10221
 
  if (!conds)
 
2812
  if (conds == NULL)
 
2813
  {
10222
2814
    *cond_value= Item::COND_TRUE;
 
2815
  }
10223
2816
  else
10224
2817
  {
10225
2818
    /*
10234
2827
                             &join->cond_equal);
10235
2828
 
10236
2829
    /* change field = field to field = const for each found field = const */
10237
 
    propagate_cond_constants(session, (I_List<COND_CMP> *) 0, conds, conds);
 
2830
    list<COND_CMP> temp;
 
2831
    propagate_cond_constants(session, temp, conds, conds);
10238
2832
    /*
10239
2833
      Remove all instances of item == item
10240
2834
      Remove all and-levels where CONST item != CONST item
10244
2838
  return(conds);
10245
2839
}
10246
2840
 
10247
 
 
10248
2841
/**
10249
2842
  Remove const and eq items.
10250
2843
 
10255
2848
    - COND_TRUE   : always true ( 1 = 1 )
10256
2849
    - COND_FALSE  : always false        ( 1 = 2 )
10257
2850
*/
10258
 
 
10259
 
COND *
10260
 
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
2851
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
10261
2852
{
10262
2853
  if (cond->type() == Item::COND_ITEM)
10263
2854
  {
10264
 
    bool and_level= ((Item_cond*) cond)->functype()
10265
 
      == Item_func::COND_AND_FUNC;
10266
 
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
2855
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
 
2856
 
 
2857
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
10267
2858
    Item::cond_result tmp_cond_value;
10268
 
    bool should_fix_fields=0;
 
2859
    bool should_fix_fields= false;
10269
2860
 
10270
 
    *cond_value=Item::COND_UNDEF;
 
2861
    *cond_value= Item::COND_UNDEF;
10271
2862
    Item *item;
10272
 
    while ((item=li++))
 
2863
    while ((item= li++))
10273
2864
    {
10274
 
      Item *new_item=remove_eq_conds(session, item, &tmp_cond_value);
10275
 
      if (!new_item)
10276
 
        li.remove();
 
2865
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
 
2866
      if (! new_item)
 
2867
      {
 
2868
        li.remove();
 
2869
      }
10277
2870
      else if (item != new_item)
10278
2871
      {
10279
 
        li.replace(new_item);
10280
 
        should_fix_fields=1;
 
2872
        li.replace(new_item);
 
2873
        should_fix_fields= true;
10281
2874
      }
 
2875
 
10282
2876
      if (*cond_value == Item::COND_UNDEF)
10283
 
        *cond_value=tmp_cond_value;
10284
 
      switch (tmp_cond_value) {
10285
 
      case Item::COND_OK:                       // Not true or false
10286
 
        if (and_level || *cond_value == Item::COND_FALSE)
10287
 
          *cond_value=tmp_cond_value;
10288
 
        break;
10289
 
      case Item::COND_FALSE:
10290
 
        if (and_level)
10291
 
        {
10292
 
          *cond_value=tmp_cond_value;
10293
 
          return (COND*) 0;                     // Always false
10294
 
        }
10295
 
        break;
10296
 
      case Item::COND_TRUE:
10297
 
        if (!and_level)
10298
 
        {
10299
 
          *cond_value= tmp_cond_value;
10300
 
          return (COND*) 0;                     // Always true
10301
 
        }
10302
 
        break;
10303
 
      case Item::COND_UNDEF:                    // Impossible
10304
 
        break; /* purecov: deadcode */
 
2877
      {
 
2878
        *cond_value= tmp_cond_value;
 
2879
      }
 
2880
 
 
2881
      switch (tmp_cond_value)
 
2882
      {
 
2883
        case Item::COND_OK:                     /* Not true or false */
 
2884
          if (and_level || (*cond_value == Item::COND_FALSE))
 
2885
            *cond_value= tmp_cond_value;
 
2886
          break;
 
2887
        case Item::COND_FALSE:
 
2888
          if (and_level)
 
2889
          {
 
2890
            *cond_value= tmp_cond_value;
 
2891
            return (COND *) NULL;                       /* Always false */
 
2892
          }
 
2893
          break;
 
2894
        case Item::COND_TRUE:
 
2895
          if (! and_level)
 
2896
          {
 
2897
            *cond_value= tmp_cond_value;
 
2898
            return (COND *) NULL;                       /* Always true */
 
2899
          }
 
2900
          break;
 
2901
        case Item::COND_UNDEF:                  /* Impossible */
 
2902
          break;
10305
2903
      }
10306
2904
    }
 
2905
 
10307
2906
    if (should_fix_fields)
 
2907
    {
10308
2908
      cond->update_used_tables();
10309
 
 
10310
 
    if (!((Item_cond*) cond)->argument_list()->elements ||
10311
 
        *cond_value != Item::COND_OK)
10312
 
      return (COND*) 0;
10313
 
    if (((Item_cond*) cond)->argument_list()->elements == 1)
10314
 
    {                                           // Remove list
10315
 
      item= ((Item_cond*) cond)->argument_list()->head();
10316
 
      ((Item_cond*) cond)->argument_list()->empty();
 
2909
    }
 
2910
 
 
2911
    if (! ((Item_cond*) cond)->argument_list()->size() || *cond_value != Item::COND_OK)
 
2912
    {
 
2913
      return (COND*) NULL;
 
2914
    }
 
2915
 
 
2916
    if (((Item_cond*) cond)->argument_list()->size() == 1)
 
2917
    {
 
2918
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
2919
      item= &((Item_cond*) cond)->argument_list()->front();
 
2920
      ((Item_cond*) cond)->argument_list()->clear();
 
2921
 
10317
2922
      return item;
10318
2923
    }
10319
2924
  }
10320
 
  else if (cond->type() == Item::FUNC_ITEM &&
10321
 
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
2925
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
10322
2926
  {
10323
2927
    /*
10324
2928
      Handles this special case for some ODBC applications:
10330
2934
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
10331
2935
    */
10332
2936
 
10333
 
    Item_func_isnull *func=(Item_func_isnull*) cond;
 
2937
    Item_func_isnull *func= (Item_func_isnull*) cond;
10334
2938
    Item **args= func->arguments();
10335
2939
    if (args[0]->type() == Item::FIELD_ITEM)
10336
2940
    {
10337
 
      Field *field=((Item_field*) args[0])->field;
10338
 
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
10339
 
          (session->options & OPTION_AUTO_IS_NULL) &&
10340
 
          (session->first_successful_insert_id_in_prev_stmt > 0 &&
10341
 
           session->substitute_null_with_insert_id))
 
2941
      Field *field= ((Item_field*) args[0])->field;
 
2942
      if (field->flags & AUTO_INCREMENT_FLAG
 
2943
          && ! field->getTable()->maybe_null
 
2944
          && session->options & OPTION_AUTO_IS_NULL
 
2945
          && (
 
2946
            session->first_successful_insert_id_in_prev_stmt > 0
 
2947
            && session->substitute_null_with_insert_id
 
2948
            )
 
2949
          )
10342
2950
      {
10343
 
        COND *new_cond;
10344
 
        if ((new_cond= new Item_func_eq(args[0],
10345
 
                                        new Item_int("last_insert_id()",
10346
 
                                                     session->read_first_successful_insert_id_in_prev_stmt(),
10347
 
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
10348
 
        {
10349
 
          cond=new_cond;
10350
 
          /*
10351
 
            Item_func_eq can't be fixed after creation so we do not check
10352
 
            cond->fixed, also it do not need tables so we use 0 as second
10353
 
            argument.
10354
 
          */
10355
 
          cond->fix_fields(session, &cond);
10356
 
        }
 
2951
        COND *new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()", 
 
2952
          session->read_first_successful_insert_id_in_prev_stmt(), MY_INT64_NUM_DECIMAL_DIGITS));
 
2953
        cond= new_cond;
 
2954
        /*
 
2955
        Item_func_eq can't be fixed after creation so we do not check
 
2956
        cond->fixed, also it do not need tables so we use 0 as second
 
2957
        argument.
 
2958
        */
 
2959
        cond->fix_fields(session, &cond);
10357
2960
        /*
10358
2961
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
10359
2962
          clear for next row
10360
2963
        */
10361
2964
        session->substitute_null_with_insert_id= false;
10362
2965
      }
 
2966
#ifdef NOTDEFINED
10363
2967
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10364
 
      else if (((field->type() == DRIZZLE_TYPE_DATE) ||
10365
 
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
10366
 
                (field->flags & NOT_NULL_FLAG) &&
10367
 
               !field->table->maybe_null)
 
2968
      else if (
 
2969
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME))
 
2970
          && (field->flags & NOT_NULL_FLAG)
 
2971
          && ! field->table->maybe_null)
10368
2972
      {
10369
 
        COND *new_cond;
10370
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
10371
 
        {
10372
 
          cond=new_cond;
10373
 
          /*
10374
 
            Item_func_eq can't be fixed after creation so we do not check
10375
 
            cond->fixed, also it do not need tables so we use 0 as second
10376
 
            argument.
10377
 
          */
10378
 
          cond->fix_fields(session, &cond);
10379
 
        }
 
2973
        COND* new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2));
 
2974
        cond= new_cond;
 
2975
        /*
 
2976
        Item_func_eq can't be fixed after creation so we do not check
 
2977
        cond->fixed, also it do not need tables so we use 0 as second
 
2978
        argument.
 
2979
        */
 
2980
        cond->fix_fields(session, &cond);
10380
2981
      }
 
2982
#endif /* NOTDEFINED */
10381
2983
    }
10382
2984
    if (cond->const_item())
10383
2985
    {
10384
2986
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10385
 
      return (COND*) 0;
 
2987
      return (COND *) NULL;
10386
2988
    }
10387
2989
  }
10388
2990
  else if (cond->const_item() && !cond->is_expensive())
10389
2991
  /*
10390
 
    TODO:
 
2992
    @todo
10391
2993
    Excluding all expensive functions is too restritive we should exclude only
10392
 
    materialized IN because it is created later than this phase, and cannot be
10393
 
    evaluated at this point.
10394
 
    The condition should be something as (need to fix member access):
10395
 
      !(cond->type() == Item::FUNC_ITEM &&
10396
 
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
10397
 
        ((Item_in_optimizer*)cond)->is_expensive()))
 
2994
    materialized IN subquery predicates because they can't yet be evaluated
 
2995
    here (they need additional initialization that is done later on).
 
2996
 
 
2997
    The proper way to exclude the subqueries would be to walk the cond tree and
 
2998
    check for materialized subqueries there.
 
2999
 
10398
3000
  */
10399
3001
  {
10400
3002
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10401
 
    return (COND*) 0;
 
3003
    return (COND *) NULL;
10402
3004
  }
10403
3005
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
10404
 
  {                                             // boolan compare function
 
3006
  {
 
3007
    /* boolan compare function */
10405
3008
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10406
3009
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10407
3010
    if (left_item->eq(right_item,1))
10408
3011
    {
10409
 
      if (!left_item->maybe_null ||
10410
 
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
10411
 
        return (COND*) 0;                       // Compare of identical items
 
3012
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
3013
      {
 
3014
        return (COND*) NULL;                    /* Comparison of identical items */
 
3015
      }
10412
3016
    }
10413
3017
  }
10414
 
  *cond_value=Item::COND_OK;
10415
 
  return cond;                                  // Point at next and level
 
3018
  *cond_value= Item::COND_OK;
 
3019
  return cond;                                  /* Point at next and return into recursion */
10416
3020
}
10417
3021
 
10418
3022
/*
10438
3042
    true    can be used
10439
3043
    false   cannot be used
10440
3044
*/
10441
 
static bool
10442
 
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
3045
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10443
3046
{
10444
3047
  return r->const_item() &&
10445
3048
    /* elements must be compared as dates */
10454
3057
/**
10455
3058
  Return true if the item is a const value in all the WHERE clause.
10456
3059
*/
10457
 
 
10458
 
static bool
10459
 
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
3060
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
10460
3061
{
10461
3062
  if (cond->type() == Item::COND_ITEM)
10462
3063
  {
10463
 
    bool and_level= (((Item_cond*) cond)->functype()
10464
 
                     == Item_func::COND_AND_FUNC);
10465
 
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
3064
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
 
3065
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
3066
 
10466
3067
    Item *item;
10467
3068
    while ((item=li++))
10468
3069
    {
10469
3070
      bool res=const_expression_in_where(item, comp_item, const_item);
10470
3071
      if (res)                                  // Is a const value
10471
3072
      {
10472
 
        if (and_level)
10473
 
          return 1;
10474
 
      }
10475
 
      else if (!and_level)
10476
 
        return 0;
 
3073
        if (and_level)
 
3074
        {
 
3075
          return true;
 
3076
        }
 
3077
      }
 
3078
      else if (and_level == false)
 
3079
      {
 
3080
        return false;
 
3081
      }
10477
3082
    }
10478
 
    return and_level ? 0 : 1;
 
3083
    return and_level ? false : true;
10479
3084
  }
10480
3085
  else if (cond->eq_cmp_result() != Item::COND_OK)
10481
3086
  {                                             // boolan compare function
10482
3087
    Item_func* func= (Item_func*) cond;
10483
3088
    if (func->functype() != Item_func::EQUAL_FUNC &&
10484
 
        func->functype() != Item_func::EQ_FUNC)
10485
 
      return 0;
 
3089
              func->functype() != Item_func::EQ_FUNC)
 
3090
    {
 
3091
      return false;
 
3092
    }
 
3093
 
10486
3094
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10487
3095
    Item *right_item= ((Item_func*) cond)->arguments()[1];
 
3096
 
10488
3097
    if (left_item->eq(comp_item,1))
10489
3098
    {
10490
3099
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10491
3100
      {
10492
 
        if (*const_item)
10493
 
          return right_item->eq(*const_item, 1);
10494
 
        *const_item=right_item;
10495
 
        return 1;
 
3101
        if (*const_item)
 
3102
        {
 
3103
          return right_item->eq(*const_item, 1);
 
3104
        }
 
3105
        *const_item=right_item;
 
3106
        return true;
10496
3107
      }
10497
3108
    }
10498
3109
    else if (right_item->eq(comp_item,1))
10499
3110
    {
10500
3111
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10501
3112
      {
10502
 
        if (*const_item)
10503
 
          return left_item->eq(*const_item, 1);
10504
 
        *const_item=left_item;
10505
 
        return 1;
 
3113
        if (*const_item)
 
3114
        {
 
3115
          return left_item->eq(*const_item, 1);
 
3116
        }
 
3117
        *const_item=left_item;
 
3118
        return true;
10506
3119
      }
10507
3120
    }
10508
3121
  }
10509
 
  return 0;
 
3122
 
 
3123
  return false;
10510
3124
}
10511
3125
 
10512
 
 
10513
3126
/**
10514
3127
  @details
10515
3128
  Rows produced by a join sweep may end up in a temporary table or be sent
10521
3134
  @return
10522
3135
    end_select function to use. This function can't fail.
10523
3136
*/
10524
 
 
10525
 
Next_select_func setup_end_select_func(JOIN *join)
 
3137
Next_select_func setup_end_select_func(Join *join)
10526
3138
{
10527
3139
  Table *table= join->tmp_table;
10528
 
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
 
3140
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
10529
3141
  Next_select_func end_select;
10530
3142
 
10531
3143
  /* Set up select_end */
10534
3146
    if (table->group && tmp_tbl->sum_func_count &&
10535
3147
        !tmp_tbl->precomputed_group_by)
10536
3148
    {
10537
 
      if (table->s->keys)
 
3149
      if (table->getShare()->sizeKeys())
10538
3150
      {
10539
 
        end_select=end_update;
 
3151
        end_select= end_update;
10540
3152
      }
10541
3153
      else
10542
3154
      {
10543
 
        end_select=end_unique_update;
 
3155
        end_select= end_unique_update;
10544
3156
      }
10545
3157
    }
10546
3158
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
10547
3159
    {
10548
 
      end_select=end_write_group;
 
3160
      end_select= end_write_group;
10549
3161
    }
10550
3162
    else
10551
3163
    {
10552
 
      end_select=end_write;
 
3164
      end_select= end_write;
10553
3165
      if (tmp_tbl->precomputed_group_by)
10554
3166
      {
10555
3167
        /*
10556
3168
          A preceding call to create_tmp_table in the case when loose
10557
3169
          index scan is used guarantees that
10558
 
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
 
3170
          Tmp_Table_Param::items_to_copy has enough space for the group
10559
3171
          by functions. It is OK here to use memcpy since we copy
10560
3172
          Item_sum pointers into an array of Item pointers.
10561
3173
        */
10568
3180
  }
10569
3181
  else
10570
3182
  {
10571
 
    if ((join->sort_and_group) &&
10572
 
        !tmp_tbl->precomputed_group_by)
 
3183
    if ((join->sort_and_group) && !tmp_tbl->precomputed_group_by)
 
3184
    {
10573
3185
      end_select= end_send_group;
 
3186
    }
10574
3187
    else
 
3188
    {
10575
3189
      end_select= end_send;
 
3190
    }
10576
3191
  }
 
3192
 
10577
3193
  return end_select;
10578
3194
}
10579
3195
 
10580
 
 
10581
3196
/**
10582
3197
  Make a join of all tables and write it on socket or to table.
10583
3198
 
10588
3203
  @retval
10589
3204
    -1  if error should be sent
10590
3205
*/
10591
 
 
10592
 
static int
10593
 
do_select(JOIN *join,List<Item> *fields,Table *table)
 
3206
int do_select(Join *join, List<Item> *fields, Table *table)
10594
3207
{
10595
3208
  int rc= 0;
10596
3209
  enum_nested_loop_state error= NESTED_LOOP_OK;
10597
 
  JOIN_TAB *join_tab= NULL;
 
3210
  JoinTable *join_tab= NULL;
10598
3211
 
10599
3212
  join->tmp_table= table;                       /* Save for easy recursion */
10600
3213
  join->fields= fields;
10601
3214
 
10602
3215
  if (table)
10603
3216
  {
10604
 
    table->file->extra(HA_EXTRA_WRITE_CACHE);
10605
 
    empty_record(table);
 
3217
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
3218
    table->emptyRecord();
10606
3219
    if (table->group && join->tmp_table_param.sum_func_count &&
10607
 
        table->s->keys && !table->file->inited)
10608
 
      table->file->ha_index_init(0, 0);
 
3220
        table->getShare()->sizeKeys() && !table->cursor->inited)
 
3221
    {
 
3222
      int tmp_error;
 
3223
      tmp_error= table->cursor->startIndexScan(0, 0);
 
3224
      if (tmp_error != 0)
 
3225
      {
 
3226
        table->print_error(tmp_error, MYF(0));
 
3227
        return -1;
 
3228
      }
 
3229
    }
10609
3230
  }
 
3231
 
10610
3232
  /* Set up select_end */
10611
3233
  Next_select_func end_select= setup_end_select_func(join);
10612
3234
  if (join->tables)
10615
3237
 
10616
3238
    join_tab=join->join_tab+join->const_tables;
10617
3239
  }
 
3240
 
10618
3241
  join->send_records=0;
10619
3242
  if (join->tables == join->const_tables)
10620
3243
  {
10626
3249
    {
10627
3250
      error= (*end_select)(join, 0, 0);
10628
3251
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
10629
 
        error= (*end_select)(join, 0, 1);
 
3252
              error= (*end_select)(join, 0, 1);
10630
3253
 
10631
3254
      /*
10632
3255
        If we don't go through evaluate_join_record(), do the counting
10648
3271
    assert(join->tables);
10649
3272
    error= sub_select(join,join_tab,0);
10650
3273
    if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
 
3274
    {
10651
3275
      error= sub_select(join,join_tab,1);
 
3276
    }
 
3277
 
10652
3278
    if (error == NESTED_LOOP_QUERY_LIMIT)
 
3279
    {
10653
3280
      error= NESTED_LOOP_OK;                    /* select_limit used */
 
3281
    }
10654
3282
  }
 
3283
 
10655
3284
  if (error == NESTED_LOOP_NO_MORE_ROWS)
 
3285
  {
10656
3286
    error= NESTED_LOOP_OK;
 
3287
  }
10657
3288
 
10658
3289
  if (error == NESTED_LOOP_OK)
10659
3290
  {
10664
3295
    if (!table)                                 // If sending data to client
10665
3296
    {
10666
3297
      /*
10667
 
        The following will unlock all cursors if the command wasn't an
10668
 
        update command
 
3298
        The following will unlock all cursors if the command wasn't an
 
3299
        update command
10669
3300
      */
10670
3301
      join->join_free();                        // Unlock all cursors
10671
3302
      if (join->result->send_eof())
10672
 
        rc= 1;                                  // Don't send error
 
3303
      {
 
3304
        rc= 1;                                  // Don't send error
 
3305
      }
10673
3306
    }
10674
3307
  }
10675
3308
  else
 
3309
  {
10676
3310
    rc= -1;
 
3311
  }
 
3312
 
10677
3313
  if (table)
10678
3314
  {
10679
3315
    int tmp, new_errno= 0;
10680
 
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
10681
 
    {
10682
 
      new_errno= tmp;
10683
 
    }
10684
 
    if ((tmp=table->file->ha_index_or_rnd_end()))
10685
 
    {
10686
 
      new_errno= tmp;
10687
 
    }
 
3316
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
3317
    {
 
3318
      new_errno= tmp;
 
3319
    }
 
3320
 
 
3321
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
3322
    {
 
3323
      new_errno= tmp;
 
3324
    }
 
3325
 
10688
3326
    if (new_errno)
10689
 
      table->file->print_error(new_errno,MYF(0));
 
3327
    {
 
3328
      table->print_error(new_errno,MYF(0));
 
3329
    }
10690
3330
  }
10691
3331
  return(join->session->is_error() ? -1 : rc);
10692
3332
}
10693
3333
 
10694
 
 
10695
 
enum_nested_loop_state
10696
 
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
3334
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
10697
3335
{
10698
3336
  enum_nested_loop_state rc;
10699
3337
 
10704
3342
      rc= sub_select(join,join_tab,end_of_records);
10705
3343
    return rc;
10706
3344
  }
10707
 
  if (join->session->killed)            // If aborted by user
 
3345
 
 
3346
  if (join->session->getKilled())               // If aborted by user
10708
3347
  {
10709
3348
    join->session->send_kill_message();
10710
 
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
 
3349
    return NESTED_LOOP_KILLED;
10711
3350
  }
 
3351
 
10712
3352
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
10713
3353
  {
10714
 
    if (!store_record_in_cache(&join_tab->cache))
 
3354
    if (! join_tab->cache.store_record_in_cache())
10715
3355
      return NESTED_LOOP_OK;                     // There is more room in cache
10716
3356
    return flush_cached_records(join,join_tab,false);
10717
3357
  }
10718
3358
  rc= flush_cached_records(join, join_tab, true);
10719
3359
  if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
 
3360
  {
10720
3361
    rc= sub_select(join, join_tab, end_of_records);
 
3362
  }
 
3363
 
10721
3364
  return rc;
10722
3365
}
10723
3366
 
10840
3483
  @return
10841
3484
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
10842
3485
*/
10843
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
10844
 
 
10845
 
enum_nested_loop_state
10846
 
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
3486
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
10847
3487
{
10848
3488
  join_tab->table->null_row=0;
10849
3489
  if (end_of_records)
 
3490
  {
10850
3491
    return (*join_tab->next_select)(join,join_tab+1,end_of_records);
 
3492
  }
10851
3493
 
10852
3494
  int error;
10853
3495
  enum_nested_loop_state rc;
10854
 
  READ_RECORD *info= &join_tab->read_record;
10855
 
 
10856
 
  if (join_tab->flush_weedout_table)
10857
 
  {
10858
 
    do_sj_reset(join_tab->flush_weedout_table);
10859
 
  }
 
3496
  ReadRecord *info= &join_tab->read_record;
10860
3497
 
10861
3498
  if (join->resume_nested_loop)
10862
3499
  {
10863
3500
    /* If not the last table, plunge down the nested loop */
10864
3501
    if (join_tab < join->join_tab + join->tables - 1)
 
3502
    {
10865
3503
      rc= (*join_tab->next_select)(join, join_tab + 1, 0);
 
3504
    }
10866
3505
    else
10867
3506
    {
10868
3507
      join->resume_nested_loop= false;
10900
3539
    rc= evaluate_join_record(join, join_tab, error);
10901
3540
  }
10902
3541
 
10903
 
  if (rc == NESTED_LOOP_NO_MORE_ROWS &&
10904
 
      join_tab->last_inner && !join_tab->found)
 
3542
  if (rc == NESTED_LOOP_NO_MORE_ROWS and join_tab->last_inner && !join_tab->found)
 
3543
  {
10905
3544
    rc= evaluate_null_complemented_join_record(join, join_tab);
 
3545
  }
10906
3546
 
10907
3547
  if (rc == NESTED_LOOP_NO_MORE_ROWS)
 
3548
  {
10908
3549
    rc= NESTED_LOOP_OK;
 
3550
  }
 
3551
 
10909
3552
  return rc;
10910
3553
}
10911
3554
 
10912
 
 
10913
 
 
10914
 
 
10915
 
/*
10916
 
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
10917
 
 
10918
 
  SYNPOSIS
10919
 
    do_sj_dups_weedout()
10920
 
 
10921
 
  RETURN
10922
 
    -1  Error
10923
 
    1   The row combination is a duplicate (discard it)
10924
 
    0   The row combination is not a duplicate (continue)
10925
 
*/
10926
 
 
10927
 
int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
10928
 
{
10929
 
  int error;
10930
 
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
10931
 
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
10932
 
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
10933
 
  unsigned char *nulls_ptr= ptr;
10934
 
 
10935
 
  /* Put the the rowids tuple into table->record[0]: */
10936
 
 
10937
 
  // 1. Store the length
10938
 
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
10939
 
  {
10940
 
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
10941
 
    ptr++;
10942
 
  }
10943
 
  else
10944
 
  {
10945
 
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
10946
 
    ptr += 2;
10947
 
  }
10948
 
 
10949
 
  // 2. Zero the null bytes
10950
 
  if (sjtbl->null_bytes)
10951
 
  {
10952
 
    memset(ptr, 0, sjtbl->null_bytes);
10953
 
    ptr += sjtbl->null_bytes;
10954
 
  }
10955
 
 
10956
 
  // 3. Put the rowids
10957
 
  for (uint32_t i=0; tab != tab_end; tab++, i++)
10958
 
  {
10959
 
    handler *h= tab->join_tab->table->file;
10960
 
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
10961
 
    {
10962
 
      /* It's a NULL-complemented row */
10963
 
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
10964
 
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
10965
 
    }
10966
 
    else
10967
 
    {
10968
 
      /* Copy the rowid value */
10969
 
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
10970
 
        h->position(tab->join_tab->table->record[0]);
10971
 
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
10972
 
    }
10973
 
  }
10974
 
 
10975
 
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
10976
 
  if (error)
10977
 
  {
10978
 
    /* create_myisam_from_heap will generate error if needed */
10979
 
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
10980
 
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
10981
 
                                &sjtbl->recinfo, error, 1))
10982
 
      return -1;
10983
 
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
10984
 
    return 1;
10985
 
  }
10986
 
  return 0;
10987
 
}
10988
 
 
10989
 
 
10990
 
/*
10991
 
  SemiJoinDuplicateElimination: Reset the temporary table
10992
 
*/
10993
 
 
10994
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
10995
 
{
10996
 
  if (sj_tbl->tmp_table)
10997
 
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
10998
 
  return 0;
10999
 
}
11000
 
 
11001
 
/*
11002
 
  Process one record of the nested loop join.
11003
 
 
11004
 
    This function will evaluate parts of WHERE/ON clauses that are
11005
 
    applicable to the partial record on hand and in case of success
11006
 
    submit this record to the next level of the nested loop.
11007
 
*/
11008
 
 
11009
 
static enum_nested_loop_state
11010
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
11011
 
                     int error)
11012
 
{
11013
 
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
11014
 
  ha_rows found_records=join->found_records;
11015
 
  COND *select_cond= join_tab->select_cond;
11016
 
 
11017
 
  if (error > 0 || (join->session->is_error()))     // Fatal error
11018
 
    return NESTED_LOOP_ERROR;
11019
 
  if (error < 0)
11020
 
    return NESTED_LOOP_NO_MORE_ROWS;
11021
 
  if (join->session->killed)                    // Aborted by user
11022
 
  {
11023
 
    join->session->send_kill_message();
11024
 
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
11025
 
  }
11026
 
  if (!select_cond || select_cond->val_int())
11027
 
  {
11028
 
    /*
11029
 
      There is no select condition or the attached pushed down
11030
 
      condition is true => a match is found.
11031
 
    */
11032
 
    bool found= 1;
11033
 
    while (join_tab->first_unmatched && found)
11034
 
    {
11035
 
      /*
11036
 
        The while condition is always false if join_tab is not
11037
 
        the last inner join table of an outer join operation.
11038
 
      */
11039
 
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
11040
 
      /*
11041
 
        Mark that a match for current outer table is found.
11042
 
        This activates push down conditional predicates attached
11043
 
        to the all inner tables of the outer join.
11044
 
      */
11045
 
      first_unmatched->found= 1;
11046
 
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
11047
 
      {
11048
 
        if (tab->table->reginfo.not_exists_optimize)
11049
 
          return NESTED_LOOP_NO_MORE_ROWS;
11050
 
        /* Check all predicates that has just been activated. */
11051
 
        /*
11052
 
          Actually all predicates non-guarded by first_unmatched->found
11053
 
          will be re-evaluated again. It could be fixed, but, probably,
11054
 
          it's not worth doing now.
11055
 
        */
11056
 
        if (tab->select_cond && !tab->select_cond->val_int())
11057
 
        {
11058
 
          /* The condition attached to table tab is false */
11059
 
          if (tab == join_tab)
11060
 
            found= 0;
11061
 
          else
11062
 
          {
11063
 
            /*
11064
 
              Set a return point if rejected predicate is attached
11065
 
              not to the last table of the current nest level.
11066
 
            */
11067
 
            join->return_tab= tab;
11068
 
            return NESTED_LOOP_OK;
11069
 
          }
11070
 
        }
11071
 
      }
11072
 
      /*
11073
 
        Check whether join_tab is not the last inner table
11074
 
        for another embedding outer join.
11075
 
      */
11076
 
      if ((first_unmatched= first_unmatched->first_upper) &&
11077
 
          first_unmatched->last_inner != join_tab)
11078
 
        first_unmatched= 0;
11079
 
      join_tab->first_unmatched= first_unmatched;
11080
 
    }
11081
 
 
11082
 
    JOIN_TAB *return_tab= join->return_tab;
11083
 
    join_tab->found_match= true;
11084
 
    if (join_tab->check_weed_out_table)
11085
 
    {
11086
 
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
11087
 
      if (res == -1)
11088
 
        return NESTED_LOOP_ERROR;
11089
 
      if (res == 1)
11090
 
        return NESTED_LOOP_OK;
11091
 
    }
11092
 
    else if (join_tab->do_firstmatch)
11093
 
    {
11094
 
      /*
11095
 
        We should return to the join_tab->do_firstmatch after we have
11096
 
        enumerated all the suffixes for current prefix row combination
11097
 
      */
11098
 
      return_tab= join_tab->do_firstmatch;
11099
 
    }
11100
 
 
11101
 
    /*
11102
 
      It was not just a return to lower loop level when one
11103
 
      of the newly activated predicates is evaluated as false
11104
 
      (See above join->return_tab= tab).
11105
 
    */
11106
 
    join->examined_rows++;
11107
 
    join->session->row_count++;
11108
 
 
11109
 
    if (found)
11110
 
    {
11111
 
      enum enum_nested_loop_state rc;
11112
 
      /* A match from join_tab is found for the current partial join. */
11113
 
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
11114
 
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
11115
 
        return rc;
11116
 
      if (return_tab < join->return_tab)
11117
 
        join->return_tab= return_tab;
11118
 
 
11119
 
      if (join->return_tab < join_tab)
11120
 
        return NESTED_LOOP_OK;
11121
 
      /*
11122
 
        Test if this was a SELECT DISTINCT query on a table that
11123
 
        was not in the field list;  In this case we can abort if
11124
 
        we found a row, as no new rows can be added to the result.
11125
 
      */
11126
 
      if (not_used_in_distinct && found_records != join->found_records)
11127
 
        return NESTED_LOOP_NO_MORE_ROWS;
11128
 
    }
11129
 
    else
11130
 
      join_tab->read_record.file->unlock_row();
11131
 
  }
11132
 
  else
11133
 
  {
11134
 
    /*
11135
 
      The condition pushed down to the table join_tab rejects all rows
11136
 
      with the beginning coinciding with the current partial join.
11137
 
    */
11138
 
    join->examined_rows++;
11139
 
    join->session->row_count++;
11140
 
    join_tab->read_record.file->unlock_row();
11141
 
  }
11142
 
  return NESTED_LOOP_OK;
11143
 
}
11144
 
 
11145
 
 
11146
 
/**
11147
 
 
11148
 
  @details
11149
 
    Construct a NULL complimented partial join record and feed it to the next
11150
 
    level of the nested loop. This function is used in case we have
11151
 
    an OUTER join and no matching record was found.
11152
 
*/
11153
 
 
11154
 
static enum_nested_loop_state
11155
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
11156
 
{
11157
 
  /*
11158
 
    The table join_tab is the first inner table of a outer join operation
11159
 
    and no matches has been found for the current outer row.
11160
 
  */
11161
 
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
11162
 
  /* Cache variables for faster loop */
11163
 
  COND *select_cond;
11164
 
  for ( ; join_tab <= last_inner_tab ; join_tab++)
11165
 
  {
11166
 
    /* Change the the values of guard predicate variables. */
11167
 
    join_tab->found= 1;
11168
 
    join_tab->not_null_compl= 0;
11169
 
    /* The outer row is complemented by nulls for each inner tables */
11170
 
    restore_record(join_tab->table,s->default_values);  // Make empty record
11171
 
    mark_as_null_row(join_tab->table);       // For group by without error
11172
 
    select_cond= join_tab->select_cond;
11173
 
    /* Check all attached conditions for inner table rows. */
11174
 
    if (select_cond && !select_cond->val_int())
11175
 
      return NESTED_LOOP_OK;
11176
 
  }
11177
 
  join_tab--;
11178
 
  /*
11179
 
    The row complemented by nulls might be the first row
11180
 
    of embedding outer joins.
11181
 
    If so, perform the same actions as in the code
11182
 
    for the first regular outer join row above.
11183
 
  */
11184
 
  for ( ; ; )
11185
 
  {
11186
 
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
11187
 
    if ((first_unmatched= first_unmatched->first_upper) &&
11188
 
        first_unmatched->last_inner != join_tab)
11189
 
      first_unmatched= 0;
11190
 
    join_tab->first_unmatched= first_unmatched;
11191
 
    if (!first_unmatched)
11192
 
      break;
11193
 
    first_unmatched->found= 1;
11194
 
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
11195
 
    {
11196
 
      if (tab->select_cond && !tab->select_cond->val_int())
11197
 
      {
11198
 
        join->return_tab= tab;
11199
 
        return NESTED_LOOP_OK;
11200
 
      }
11201
 
    }
11202
 
  }
11203
 
  /*
11204
 
    The row complemented by nulls satisfies all conditions
11205
 
    attached to inner tables.
11206
 
    Send the row complemented by nulls to be joined with the
11207
 
    remaining tables.
11208
 
  */
11209
 
  return (*join_tab->next_select)(join, join_tab+1, 0);
11210
 
}
11211
 
 
11212
 
 
11213
 
static enum_nested_loop_state
11214
 
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
11215
 
{
11216
 
  enum_nested_loop_state rc= NESTED_LOOP_OK;
11217
 
  int error;
11218
 
  READ_RECORD *info;
11219
 
 
11220
 
  join_tab->table->null_row= 0;
11221
 
  if (!join_tab->cache.records)
11222
 
    return NESTED_LOOP_OK;                      /* Nothing to do */
11223
 
  if (skip_last)
11224
 
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
11225
 
  if (join_tab->use_quick == 2)
11226
 
  {
11227
 
    if (join_tab->select->quick)
11228
 
    {                                   /* Used quick select last. reset it */
11229
 
      delete join_tab->select->quick;
11230
 
      join_tab->select->quick=0;
11231
 
    }
11232
 
  }
11233
 
 /* read through all records */
11234
 
  if ((error=join_init_read_record(join_tab)))
11235
 
  {
11236
 
    reset_cache_write(&join_tab->cache);
11237
 
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
11238
 
  }
11239
 
 
11240
 
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
11241
 
  {
11242
 
    tmp->status=tmp->table->status;
11243
 
    tmp->table->status=0;
11244
 
  }
11245
 
 
11246
 
  info= &join_tab->read_record;
11247
 
  do
11248
 
  {
11249
 
    if (join->session->killed)
11250
 
    {
11251
 
      join->session->send_kill_message();
11252
 
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
11253
 
    }
11254
 
    SQL_SELECT *select=join_tab->select;
11255
 
    if (rc == NESTED_LOOP_OK &&
11256
 
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
11257
 
    {
11258
 
      uint32_t i;
11259
 
      reset_cache_read(&join_tab->cache);
11260
 
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
11261
 
      {
11262
 
        read_cached_record(join_tab);
11263
 
        if (!select || !select->skip_record())
11264
 
        {
11265
 
          int res= 0;
11266
 
          if (!join_tab->check_weed_out_table ||
11267
 
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
11268
 
          {
11269
 
            rc= (join_tab->next_select)(join,join_tab+1,0);
11270
 
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
11271
 
            {
11272
 
              reset_cache_write(&join_tab->cache);
11273
 
              return rc;
11274
 
            }
11275
 
          }
11276
 
          if (res == -1)
11277
 
            return NESTED_LOOP_ERROR;
11278
 
        }
11279
 
      }
11280
 
    }
11281
 
  } while (!(error=info->read_record(info)));
11282
 
 
11283
 
  if (skip_last)
11284
 
    read_cached_record(join_tab);               // Restore current record
11285
 
  reset_cache_write(&join_tab->cache);
11286
 
  if (error > 0)                                // Fatal error
11287
 
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
11288
 
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
11289
 
    tmp2->table->status=tmp2->status;
11290
 
  return NESTED_LOOP_OK;
11291
 
}
11292
 
 
11293
 
int safe_index_read(JOIN_TAB *tab)
 
3555
int safe_index_read(JoinTable *tab)
11294
3556
{
11295
3557
  int error;
11296
3558
  Table *table= tab->table;
11297
 
  if ((error=table->file->index_read_map(table->record[0],
 
3559
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
11298
3560
                                         tab->ref.key_buff,
11299
3561
                                         make_prev_keypart_map(tab->ref.key_parts),
11300
3562
                                         HA_READ_KEY_EXACT)))
11302
3564
  return 0;
11303
3565
}
11304
3566
 
11305
 
 
11306
 
static int
11307
 
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
11308
 
{
11309
 
  int error;
11310
 
  Table *table=tab->table;
11311
 
  table->const_table=1;
11312
 
  table->null_row=0;
11313
 
  table->status=STATUS_NO_RECORD;
11314
 
 
11315
 
  if (tab->type == JT_SYSTEM)
11316
 
  {
11317
 
    if ((error=join_read_system(tab)))
11318
 
    {                                           // Info for DESCRIBE
11319
 
      tab->info="const row not found";
11320
 
      /* Mark for EXPLAIN that the row was not found */
11321
 
      pos->records_read=0.0;
11322
 
      pos->ref_depend_map= 0;
11323
 
      if (!table->maybe_null || error > 0)
11324
 
        return(error);
11325
 
    }
11326
 
  }
11327
 
  else
11328
 
  {
11329
 
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
11330
 
        !table->no_keyread &&
11331
 
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
11332
 
    {
11333
 
      table->key_read=1;
11334
 
      table->file->extra(HA_EXTRA_KEYREAD);
11335
 
      tab->index= tab->ref.key;
11336
 
    }
11337
 
    error=join_read_const(tab);
11338
 
    if (table->key_read)
11339
 
    {
11340
 
      table->key_read=0;
11341
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
11342
 
    }
11343
 
    if (error)
11344
 
    {
11345
 
      tab->info="unique row not found";
11346
 
      /* Mark for EXPLAIN that the row was not found */
11347
 
      pos->records_read=0.0;
11348
 
      pos->ref_depend_map= 0;
11349
 
      if (!table->maybe_null || error > 0)
11350
 
        return(error);
11351
 
    }
11352
 
  }
11353
 
  if (*tab->on_expr_ref && !table->null_row)
11354
 
  {
11355
 
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
11356
 
      mark_as_null_row(table);
11357
 
  }
11358
 
  if (!table->null_row)
11359
 
    table->maybe_null=0;
11360
 
 
11361
 
  /* Check appearance of new constant items in Item_equal objects */
11362
 
  JOIN *join= tab->join;
11363
 
  if (join->conds)
11364
 
    update_const_equal_items(join->conds, tab);
11365
 
  TableList *tbl;
11366
 
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
11367
 
  {
11368
 
    TableList *embedded;
11369
 
    TableList *embedding= tbl;
11370
 
    do
11371
 
    {
11372
 
      embedded= embedding;
11373
 
      if (embedded->on_expr)
11374
 
         update_const_equal_items(embedded->on_expr, tab);
11375
 
      embedding= embedded->embedding;
11376
 
    }
11377
 
    while (embedding &&
11378
 
           embedding->nested_join->join_list.head() == embedded);
11379
 
  }
11380
 
 
11381
 
  return(0);
11382
 
}
11383
 
 
11384
 
 
11385
 
static int
11386
 
join_read_system(JOIN_TAB *tab)
11387
 
{
11388
 
  Table *table= tab->table;
11389
 
  int error;
11390
 
  if (table->status & STATUS_GARBAGE)           // If first read
11391
 
  {
11392
 
    if ((error=table->file->read_first_row(table->record[0],
11393
 
                                           table->s->primary_key)))
11394
 
    {
11395
 
      if (error != HA_ERR_END_OF_FILE)
11396
 
        return table->report_error(error);
11397
 
      mark_as_null_row(tab->table);
11398
 
      empty_record(table);                      // Make empty record
11399
 
      return -1;
11400
 
    }
11401
 
    update_virtual_fields_marked_for_write(table);
11402
 
    store_record(table,record[1]);
11403
 
  }
11404
 
  else if (!table->status)                      // Only happens with left join
11405
 
    restore_record(table,record[1]);                    // restore old record
11406
 
  table->null_row=0;
11407
 
  return table->status ? -1 : 0;
11408
 
}
11409
 
 
11410
 
 
11411
3567
/**
11412
3568
  Read a (constant) table when there is at most one matching row.
11413
3569
 
11420
3576
  @retval
11421
3577
    1   Got an error (other than row not found) during read
11422
3578
*/
11423
 
 
11424
 
static int
11425
 
join_read_const(JOIN_TAB *tab)
 
3579
int join_read_const(JoinTable *tab)
11426
3580
{
11427
3581
  int error;
11428
3582
  Table *table= tab->table;
11430
3584
  {
11431
3585
    table->status= 0;
11432
3586
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
11433
 
      error=HA_ERR_KEY_NOT_FOUND;
 
3587
    {
 
3588
      error= HA_ERR_KEY_NOT_FOUND;
 
3589
    }
11434
3590
    else
11435
3591
    {
11436
 
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
3592
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
11437
3593
                                            (unsigned char*) tab->ref.key_buff,
11438
3594
                                            make_prev_keypart_map(tab->ref.key_parts),
11439
3595
                                            HA_READ_KEY_EXACT);
11441
3597
    if (error)
11442
3598
    {
11443
3599
      table->status= STATUS_NOT_FOUND;
11444
 
      mark_as_null_row(tab->table);
11445
 
      empty_record(table);
 
3600
      tab->table->mark_as_null_row();
 
3601
      table->emptyRecord();
11446
3602
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11447
 
        return table->report_error(error);
 
3603
      {
 
3604
        return table->report_error(error);
 
3605
      }
11448
3606
      return -1;
11449
3607
    }
11450
 
    update_virtual_fields_marked_for_write(table);
11451
 
    store_record(table,record[1]);
 
3608
    table->storeRecord();
11452
3609
  }
11453
3610
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
11454
3611
  {
11455
3612
    table->status=0;
11456
 
    restore_record(table,record[1]);                    // restore old record
 
3613
    table->restoreRecord();                     // restore old record
11457
3614
  }
11458
3615
  table->null_row=0;
11459
3616
  return table->status ? -1 : 0;
11460
3617
}
11461
3618
 
11462
 
 
11463
3619
/*
11464
3620
  eq_ref access method implementation: "read_first" function
11465
3621
 
11466
3622
  SYNOPSIS
11467
3623
    join_read_key()
11468
 
      tab  JOIN_TAB of the accessed table
 
3624
      tab  JoinTable of the accessed table
11469
3625
 
11470
3626
  DESCRIPTION
11471
3627
    This is "read_fist" function for the "ref" access method. The difference
11476
3632
   -1  - Row not found
11477
3633
    1  - Error
11478
3634
*/
11479
 
 
11480
 
static int
11481
 
join_read_key(JOIN_TAB *tab)
 
3635
int join_read_key(JoinTable *tab)
11482
3636
{
11483
3637
  int error;
11484
3638
  Table *table= tab->table;
11485
3639
 
11486
 
  if (!table->file->inited)
 
3640
  if (!table->cursor->inited)
11487
3641
  {
11488
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
3642
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3643
    if (error != 0)
 
3644
    {
 
3645
      table->print_error(error, MYF(0));
 
3646
    }
11489
3647
  }
11490
3648
 
11491
 
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
 
3649
  /* @todo Why don't we do "Late NULLs Filtering" here? */
11492
3650
  if (cmp_buffer_with_ref(tab) ||
11493
3651
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
11494
3652
  {
11497
3655
      table->status=STATUS_NOT_FOUND;
11498
3656
      return -1;
11499
3657
    }
11500
 
    error=table->file->index_read_map(table->record[0],
 
3658
    error=table->cursor->index_read_map(table->getInsertRecord(),
11501
3659
                                      tab->ref.key_buff,
11502
3660
                                      make_prev_keypart_map(tab->ref.key_parts),
11503
3661
                                      HA_READ_KEY_EXACT);
11504
3662
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
 
3663
    {
11505
3664
      return table->report_error(error);
 
3665
    }
11506
3666
  }
11507
3667
  table->null_row=0;
11508
3668
  return table->status ? -1 : 0;
11509
3669
}
11510
3670
 
11511
 
 
11512
3671
/*
11513
3672
  ref access method implementation: "read_first" function
11514
3673
 
11515
3674
  SYNOPSIS
11516
3675
    join_read_always_key()
11517
 
      tab  JOIN_TAB of the accessed table
 
3676
      tab  JoinTable of the accessed table
11518
3677
 
11519
3678
  DESCRIPTION
11520
 
    This is "read_fist" function for the "ref" access method.
 
3679
    This is "read_first" function for the "ref" access method.
11521
3680
 
11522
3681
    The functon must leave the index initialized when it returns.
11523
3682
    ref_or_null access implementation depends on that.
11527
3686
   -1  - Row not found
11528
3687
    1  - Error
11529
3688
*/
11530
 
 
11531
 
static int
11532
 
join_read_always_key(JOIN_TAB *tab)
 
3689
int join_read_always_key(JoinTable *tab)
11533
3690
{
11534
3691
  int error;
11535
3692
  Table *table= tab->table;
11536
3693
 
11537
3694
  /* Initialize the index first */
11538
 
  if (!table->file->inited)
11539
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
3695
  if (!table->cursor->inited)
 
3696
  {
 
3697
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3698
    if (error != 0)
 
3699
    {
 
3700
      return table->report_error(error);
 
3701
    }
 
3702
  }
11540
3703
 
11541
3704
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
11542
3705
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
11543
3706
  {
11544
3707
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
11545
 
        return -1;
 
3708
    {
 
3709
      return -1;
 
3710
    }
11546
3711
  }
11547
3712
 
11548
3713
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
3714
  {
11549
3715
    return -1;
11550
 
  if ((error=table->file->index_read_map(table->record[0],
 
3716
  }
 
3717
 
 
3718
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
11551
3719
                                         tab->ref.key_buff,
11552
3720
                                         make_prev_keypart_map(tab->ref.key_parts),
11553
3721
                                         HA_READ_KEY_EXACT)))
11554
3722
  {
11555
3723
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
 
3724
    {
11556
3725
      return table->report_error(error);
11557
 
    return -1; /* purecov: inspected */
 
3726
    }
 
3727
    return -1;
11558
3728
  }
11559
 
  update_virtual_fields_marked_for_write(table);
 
3729
 
11560
3730
  return 0;
11561
3731
}
11562
3732
 
11563
 
 
11564
3733
/**
11565
 
  This function is used when optimizing away order_st BY in
11566
 
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
 
3734
  This function is used when optimizing away ORDER BY in
 
3735
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
11567
3736
*/
11568
 
 
11569
 
static int
11570
 
join_read_last_key(JOIN_TAB *tab)
 
3737
int join_read_last_key(JoinTable *tab)
11571
3738
{
11572
3739
  int error;
11573
3740
  Table *table= tab->table;
11574
3741
 
11575
 
  if (!table->file->inited)
11576
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
3742
  if (!table->cursor->inited)
 
3743
  {
 
3744
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3745
    if (error != 0)
 
3746
      return table->report_error(error);
 
3747
  }
 
3748
 
11577
3749
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
3750
  {
11578
3751
    return -1;
11579
 
  if ((error=table->file->index_read_last_map(table->record[0],
 
3752
  }
 
3753
 
 
3754
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
11580
3755
                                              tab->ref.key_buff,
11581
3756
                                              make_prev_keypart_map(tab->ref.key_parts))))
11582
3757
  {
11583
3758
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
 
3759
    {
11584
3760
      return table->report_error(error);
11585
 
    return -1; /* purecov: inspected */
 
3761
    }
 
3762
    return -1;
11586
3763
  }
 
3764
 
11587
3765
  return 0;
11588
3766
}
11589
3767
 
11590
 
 
11591
 
        /* ARGSUSED */
11592
 
static int
11593
 
join_no_more_records(READ_RECORD *)
 
3768
int join_no_more_records(ReadRecord *)
11594
3769
{
11595
3770
  return -1;
11596
3771
}
11597
3772
 
11598
 
static int
11599
 
join_read_next_same_diff(READ_RECORD *info)
 
3773
int join_read_next_same_diff(ReadRecord *info)
11600
3774
{
11601
3775
  Table *table= info->table;
11602
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3776
  JoinTable *tab=table->reginfo.join_tab;
11603
3777
  if (tab->insideout_match_tab->found_match)
11604
3778
  {
11605
 
    KEY *key= tab->table->key_info + tab->index;
 
3779
    KeyInfo *key= tab->table->key_info + tab->index;
11606
3780
    do
11607
3781
    {
11608
3782
      int error;
11609
3783
      /* Save index tuple from record to the buffer */
11610
3784
      key_copy(tab->insideout_buf, info->record, key, 0);
11611
3785
 
11612
 
      if ((error=table->file->index_next_same(table->record[0],
 
3786
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
11613
3787
                                              tab->ref.key_buff,
11614
3788
                                              tab->ref.key_length)))
11615
3789
      {
11624
3798
    return 0;
11625
3799
  }
11626
3800
  else
 
3801
  {
11627
3802
    return join_read_next_same(info);
 
3803
  }
11628
3804
}
11629
3805
 
11630
 
static int
11631
 
join_read_next_same(READ_RECORD *info)
 
3806
int join_read_next_same(ReadRecord *info)
11632
3807
{
11633
3808
  int error;
11634
3809
  Table *table= info->table;
11635
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3810
  JoinTable *tab=table->reginfo.join_tab;
11636
3811
 
11637
 
  if ((error=table->file->index_next_same(table->record[0],
 
3812
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
11638
3813
                                          tab->ref.key_buff,
11639
3814
                                          tab->ref.key_length)))
11640
3815
  {
11643
3818
    table->status= STATUS_GARBAGE;
11644
3819
    return -1;
11645
3820
  }
11646
 
  update_virtual_fields_marked_for_write(table);
 
3821
 
11647
3822
  return 0;
11648
3823
}
11649
3824
 
11650
 
 
11651
 
static int
11652
 
join_read_prev_same(READ_RECORD *info)
 
3825
int join_read_prev_same(ReadRecord *info)
11653
3826
{
11654
3827
  int error;
11655
3828
  Table *table= info->table;
11656
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3829
  JoinTable *tab=table->reginfo.join_tab;
11657
3830
 
11658
 
  if ((error=table->file->index_prev(table->record[0])))
 
3831
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
3832
  {
11659
3833
    return table->report_error(error);
 
3834
  }
 
3835
 
11660
3836
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
11661
3837
                      tab->ref.key_length))
11662
3838
  {
11663
3839
    table->status=STATUS_NOT_FOUND;
11664
3840
    error= -1;
11665
3841
  }
11666
 
  update_virtual_fields_marked_for_write(table);
 
3842
 
11667
3843
  return error;
11668
3844
}
11669
3845
 
11670
 
 
11671
 
static int
11672
 
join_init_quick_read_record(JOIN_TAB *tab)
 
3846
int join_init_quick_read_record(JoinTable *tab)
11673
3847
{
11674
3848
  if (test_if_quick_select(tab) == -1)
 
3849
  {
11675
3850
    return -1;                                  /* No possible records */
 
3851
  }
 
3852
 
11676
3853
  return join_init_read_record(tab);
11677
3854
}
11678
3855
 
11679
 
 
11680
 
int rr_sequential(READ_RECORD *info);
11681
 
int init_read_record_seq(JOIN_TAB *tab)
 
3856
int init_read_record_seq(JoinTable *tab)
11682
3857
{
11683
 
  tab->read_record.read_record= rr_sequential;
11684
 
  if (tab->read_record.file->ha_rnd_init(1))
 
3858
  tab->read_record.init_reard_record_sequential();
 
3859
 
 
3860
  if (tab->read_record.cursor->startTableScan(1))
 
3861
  {
11685
3862
    return 1;
 
3863
  }
11686
3864
  return (*tab->read_record.read_record)(&tab->read_record);
11687
3865
}
11688
3866
 
11689
 
static int
11690
 
test_if_quick_select(JOIN_TAB *tab)
 
3867
int test_if_quick_select(JoinTable *tab)
11691
3868
{
11692
 
  delete tab->select->quick;
11693
 
  tab->select->quick=0;
 
3869
  safe_delete(tab->select->quick);
 
3870
 
11694
3871
  return tab->select->test_quick_select(tab->join->session, tab->keys,
11695
 
                                        (table_map) 0, HA_POS_ERROR, 0,
11696
 
                                        false);
 
3872
                                        (table_map) 0, HA_POS_ERROR, 0, false);
11697
3873
}
11698
3874
 
11699
 
 
11700
 
static int
11701
 
join_init_read_record(JOIN_TAB *tab)
 
3875
int join_init_read_record(JoinTable *tab)
11702
3876
{
11703
3877
  if (tab->select && tab->select->quick && tab->select->quick->reset())
11704
 
    return 1;
11705
 
  init_read_record(&tab->read_record, tab->join->session, tab->table,
11706
 
                   tab->select,1,1);
 
3878
  {
 
3879
    return 1;
 
3880
  }
 
3881
 
 
3882
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
 
3883
  {
 
3884
    return 1;
 
3885
  }
 
3886
 
11707
3887
  return (*tab->read_record.read_record)(&tab->read_record);
11708
3888
}
11709
3889
 
11710
 
 
11711
 
static int
11712
 
join_read_first(JOIN_TAB *tab)
 
3890
int join_read_first(JoinTable *tab)
11713
3891
{
11714
3892
  int error;
11715
3893
  Table *table=tab->table;
11716
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
3894
  if (!table->key_read && table->covering_keys.test(tab->index) &&
11717
3895
      !table->no_keyread)
11718
3896
  {
11719
 
    table->key_read=1;
11720
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
3897
    table->key_read= 1;
 
3898
    table->cursor->extra(HA_EXTRA_KEYREAD);
11721
3899
  }
11722
 
  tab->table->status=0;
 
3900
  tab->table->status= 0;
11723
3901
  tab->read_record.table=table;
11724
 
  tab->read_record.file=table->file;
 
3902
  tab->read_record.cursor=table->cursor;
11725
3903
  tab->read_record.index=tab->index;
11726
 
  tab->read_record.record=table->record[0];
 
3904
  tab->read_record.record=table->getInsertRecord();
11727
3905
  if (tab->insideout_match_tab)
11728
3906
  {
11729
3907
    tab->read_record.do_insideout_scan= tab;
11736
3914
    tab->read_record.do_insideout_scan= 0;
11737
3915
  }
11738
3916
 
11739
 
  if (!table->file->inited)
11740
 
    table->file->ha_index_init(tab->index, tab->sorted);
11741
 
  if ((error=tab->table->file->index_first(tab->table->record[0])))
 
3917
  if (!table->cursor->inited)
 
3918
  {
 
3919
    error= table->cursor->startIndexScan(tab->index, tab->sorted);
 
3920
    if (error != 0)
 
3921
    {
 
3922
      table->report_error(error);
 
3923
      return -1;
 
3924
    }
 
3925
  }
 
3926
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
11742
3927
  {
11743
3928
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11744
3929
      table->report_error(error);
11745
3930
    return -1;
11746
3931
  }
11747
 
  if (not error)
11748
 
    update_virtual_fields_marked_for_write(tab->table);
 
3932
 
11749
3933
  return 0;
11750
3934
}
11751
3935
 
11752
 
 
11753
 
static int
11754
 
join_read_next_different(READ_RECORD *info)
 
3936
int join_read_next_different(ReadRecord *info)
11755
3937
{
11756
 
  JOIN_TAB *tab= info->do_insideout_scan;
 
3938
  JoinTable *tab= info->do_insideout_scan;
11757
3939
  if (tab->insideout_match_tab->found_match)
11758
3940
  {
11759
 
    KEY *key= tab->table->key_info + tab->index;
 
3941
    KeyInfo *key= tab->table->key_info + tab->index;
11760
3942
    do
11761
3943
    {
11762
3944
      int error;
11763
3945
      /* Save index tuple from record to the buffer */
11764
3946
      key_copy(tab->insideout_buf, info->record, key, 0);
11765
3947
 
11766
 
      if ((error=info->file->index_next(info->record)))
 
3948
      if ((error=info->cursor->index_next(info->record)))
 
3949
      {
11767
3950
        return info->table->report_error(error);
11768
 
      if (not error)
11769
 
        update_virtual_fields_marked_for_write(tab->table);
 
3951
      }
11770
3952
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
11771
3953
                      tab->insideout_buf, key->key_length));
11772
3954
    tab->insideout_match_tab->found_match= 0;
11773
3955
    return 0;
11774
3956
  }
11775
3957
  else
 
3958
  {
11776
3959
    return join_read_next(info);
 
3960
  }
11777
3961
}
11778
3962
 
11779
 
 
11780
 
static int
11781
 
join_read_next(READ_RECORD *info)
 
3963
int join_read_next(ReadRecord *info)
11782
3964
{
11783
3965
  int error;
11784
 
  if ((error=info->file->index_next(info->record)))
 
3966
  if ((error=info->cursor->index_next(info->record)))
 
3967
  {
11785
3968
    return info->table->report_error(error);
11786
 
  if (not error)
11787
 
    update_virtual_fields_marked_for_write(info->table);
 
3969
  }
11788
3970
  return 0;
11789
3971
}
11790
3972
 
11791
 
 
11792
 
static int
11793
 
join_read_last(JOIN_TAB *tab)
 
3973
int join_read_last(JoinTable *tab)
11794
3974
{
11795
3975
  Table *table=tab->table;
11796
3976
  int error;
11797
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
3977
  if (!table->key_read && table->covering_keys.test(tab->index) &&
11798
3978
      !table->no_keyread)
11799
3979
  {
11800
3980
    table->key_read=1;
11801
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
3981
    table->cursor->extra(HA_EXTRA_KEYREAD);
11802
3982
  }
11803
3983
  tab->table->status=0;
11804
3984
  tab->read_record.read_record=join_read_prev;
11805
3985
  tab->read_record.table=table;
11806
 
  tab->read_record.file=table->file;
 
3986
  tab->read_record.cursor=table->cursor;
11807
3987
  tab->read_record.index=tab->index;
11808
 
  tab->read_record.record=table->record[0];
11809
 
  if (!table->file->inited)
11810
 
    table->file->ha_index_init(tab->index, 1);
11811
 
  if ((error= tab->table->file->index_last(tab->table->record[0])))
 
3988
  tab->read_record.record=table->getInsertRecord();
 
3989
 
 
3990
  if (!table->cursor->inited)
 
3991
  {
 
3992
    error= table->cursor->startIndexScan(tab->index, 1);
 
3993
    if (error != 0)
 
3994
    {
 
3995
      return table->report_error(error);
 
3996
    }
 
3997
  }
 
3998
 
 
3999
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
 
4000
  {
11812
4001
    return table->report_error(error);
11813
 
  if (not error)
11814
 
    update_virtual_fields_marked_for_write(tab->table);
 
4002
  }
 
4003
 
11815
4004
  return 0;
11816
4005
}
11817
4006
 
11818
 
 
11819
 
static int
11820
 
join_read_prev(READ_RECORD *info)
 
4007
int join_read_prev(ReadRecord *info)
11821
4008
{
11822
4009
  int error;
11823
 
  if ((error= info->file->index_prev(info->record)))
 
4010
  if ((error= info->cursor->index_prev(info->record)))
 
4011
  {
11824
4012
    return info->table->report_error(error);
11825
 
  if (not error)
11826
 
    update_virtual_fields_marked_for_write(info->table);
 
4013
  }
 
4014
 
11827
4015
  return 0;
11828
4016
}
11829
4017
 
11830
4018
/**
11831
4019
  Reading of key with key reference and one part that may be NULL.
11832
4020
*/
11833
 
 
11834
 
int
11835
 
join_read_always_key_or_null(JOIN_TAB *tab)
 
4021
int join_read_always_key_or_null(JoinTable *tab)
11836
4022
{
11837
4023
  int res;
11838
4024
 
11839
4025
  /* First read according to key which is NOT NULL */
11840
4026
  *tab->ref.null_ref_key= 0;                    // Clear null byte
11841
4027
  if ((res= join_read_always_key(tab)) >= 0)
 
4028
  {
11842
4029
    return res;
 
4030
  }
11843
4031
 
11844
4032
  /* Then read key with null value */
11845
4033
  *tab->ref.null_ref_key= 1;                    // Set null byte
11846
4034
  return safe_index_read(tab);
11847
4035
}
11848
4036
 
11849
 
 
11850
 
int
11851
 
join_read_next_same_or_null(READ_RECORD *info)
 
4037
int join_read_next_same_or_null(ReadRecord *info)
11852
4038
{
11853
4039
  int error;
11854
4040
  if ((error= join_read_next_same(info)) >= 0)
 
4041
  {
11855
4042
    return error;
11856
 
  JOIN_TAB *tab= info->table->reginfo.join_tab;
 
4043
  }
 
4044
  JoinTable *tab= info->table->reginfo.join_tab;
11857
4045
 
11858
4046
  /* Test if we have already done a read after null key */
11859
4047
  if (*tab->ref.null_ref_key)
 
4048
  {
11860
4049
    return -1;                                  // All keys read
 
4050
  }
 
4051
 
11861
4052
  *tab->ref.null_ref_key= 1;                    // Set null byte
 
4053
 
11862
4054
  return safe_index_read(tab);                  // then read null keys
11863
4055
}
11864
4056
 
11865
 
 
11866
 
/*****************************************************************************
11867
 
  DESCRIPTION
11868
 
    Functions that end one nested loop iteration. Different functions
11869
 
    are used to support GROUP BY clause and to redirect records
11870
 
    to a table (e.g. in case of SELECT into a temporary table) or to the
11871
 
    network client.
11872
 
 
11873
 
  RETURN VALUES
11874
 
    NESTED_LOOP_OK           - the record has been successfully handled
11875
 
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
11876
 
                               was detected
11877
 
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
11878
 
                               the record
11879
 
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
11880
 
                               additionally, the nested loop produced the
11881
 
                               number of rows specified in the LIMIT clause
11882
 
                               for the query
11883
 
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
11884
 
                               additionally, there is a cursor and the nested
11885
 
                               loop algorithm produced the number of rows
11886
 
                               that is specified for current cursor fetch
11887
 
                               operation.
11888
 
   All return values except NESTED_LOOP_OK abort the nested loop.
11889
 
*****************************************************************************/
11890
 
 
11891
 
/* ARGSUSED */
11892
 
static enum_nested_loop_state
11893
 
end_send(JOIN *join, JOIN_TAB *,
11894
 
         bool end_of_records)
11895
 
{
11896
 
  if (!end_of_records)
11897
 
  {
11898
 
    int error;
11899
 
    if (join->having && join->having->val_int() == 0)
11900
 
      return(NESTED_LOOP_OK);               // Didn't match having
11901
 
    error=0;
11902
 
    if (join->do_send_rows)
11903
 
      error=join->result->send_data(*join->fields);
11904
 
    if (error)
11905
 
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
11906
 
    if (++join->send_records >= join->unit->select_limit_cnt &&
11907
 
        join->do_send_rows)
11908
 
    {
11909
 
      if (join->select_options & OPTION_FOUND_ROWS)
11910
 
      {
11911
 
        JOIN_TAB *jt=join->join_tab;
11912
 
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
11913
 
            && !join->send_group_parts && !join->having && !jt->select_cond &&
11914
 
            !(jt->select && jt->select->quick) &&
11915
 
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
11916
 
            (jt->ref.key < 0))
11917
 
        {
11918
 
          /* Join over all rows in table;  Return number of found rows */
11919
 
          Table *table=jt->table;
11920
 
 
11921
 
          join->select_options ^= OPTION_FOUND_ROWS;
11922
 
          if (table->sort.record_pointers ||
11923
 
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
11924
 
          {
11925
 
            /* Using filesort */
11926
 
            join->send_records= table->sort.found_records;
11927
 
          }
11928
 
          else
11929
 
          {
11930
 
            table->file->info(HA_STATUS_VARIABLE);
11931
 
            join->send_records= table->file->stats.records;
11932
 
          }
11933
 
        }
11934
 
        else
11935
 
        {
11936
 
          join->do_send_rows= 0;
11937
 
          if (join->unit->fake_select_lex)
11938
 
            join->unit->fake_select_lex->select_limit= 0;
11939
 
          return(NESTED_LOOP_OK);
11940
 
        }
11941
 
      }
11942
 
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
11943
 
    }
11944
 
    else if (join->send_records >= join->fetch_limit)
11945
 
    {
11946
 
      /*
11947
 
        There is a server side cursor and all rows for
11948
 
        this fetch request are sent.
11949
 
      */
11950
 
      return(NESTED_LOOP_CURSOR_LIMIT);
11951
 
    }
11952
 
  }
11953
 
 
11954
 
  return(NESTED_LOOP_OK);
11955
 
}
11956
 
 
11957
 
 
11958
 
/* ARGSUSED */
11959
 
enum_nested_loop_state
11960
 
end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
 
4057
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
11961
4058
{
11962
4059
  int idx= -1;
11963
4060
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
11964
4061
 
11965
 
  if (!join->first_record || end_of_records ||
11966
 
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
 
4062
  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
11967
4063
  {
11968
 
    if (join->first_record ||
 
4064
    if (join->first_record or
11969
4065
        (end_of_records && !join->group && !join->group_optimized_away))
11970
4066
    {
11971
4067
      if (idx < (int) join->send_group_parts)
11972
4068
      {
11973
 
        int error=0;
11974
 
        {
11975
 
          if (!join->first_record)
11976
 
          {
11977
 
            List_iterator_fast<Item> it(*join->fields);
11978
 
            Item *item;
11979
 
            /* No matching rows for group function */
11980
 
            join->clear();
 
4069
        int error=0;
 
4070
        {
 
4071
          if (!join->first_record)
 
4072
          {
 
4073
                  List<Item>::iterator it(join->fields->begin());
 
4074
                  Item *item;
 
4075
            /* No matching rows for group function */
 
4076
            join->clear();
11981
4077
 
11982
4078
            while ((item= it++))
 
4079
            {
11983
4080
              item->no_rows_in_result();
11984
 
          }
11985
 
          if (join->having && join->having->val_int() == 0)
11986
 
            error= -1;                          // Didn't satisfy having
11987
 
          else
11988
 
          {
11989
 
            if (join->do_send_rows)
11990
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
11991
 
            join->send_records++;
11992
 
          }
11993
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
11994
 
          {
11995
 
            if (join->rollup_send_data((uint) (idx+1)))
11996
 
              error= 1;
11997
 
          }
11998
 
        }
11999
 
        if (error > 0)
12000
 
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
12001
 
        if (end_of_records)
12002
 
          return(NESTED_LOOP_OK);
12003
 
        if (join->send_records >= join->unit->select_limit_cnt &&
12004
 
            join->do_send_rows)
12005
 
        {
12006
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
12007
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
12008
 
          join->do_send_rows=0;
12009
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
4081
            }
 
4082
          }
 
4083
          if (join->having && join->having->val_int() == 0)
 
4084
          {
 
4085
            error= -1;                          // Didn't satisfy having
 
4086
          }
 
4087
          else
 
4088
          {
 
4089
            if (join->do_send_rows)
 
4090
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
4091
            join->send_records++;
 
4092
          }
 
4093
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
 
4094
          {
 
4095
            if (join->rollup_send_data((uint32_t) (idx+1)))
 
4096
              error= 1;
 
4097
          }
 
4098
        }
 
4099
 
 
4100
        if (error > 0)
 
4101
        {
 
4102
          return(NESTED_LOOP_ERROR);
 
4103
        }
 
4104
 
 
4105
        if (end_of_records)
 
4106
        {
 
4107
          return(NESTED_LOOP_OK);
 
4108
        }
 
4109
 
 
4110
        if (join->send_records >= join->unit->select_limit_cnt &&
 
4111
            join->do_send_rows)
 
4112
        {
 
4113
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
4114
          {
 
4115
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
4116
          }
 
4117
          join->do_send_rows=0;
 
4118
          join->unit->select_limit_cnt = HA_POS_ERROR;
12010
4119
        }
12011
4120
        else if (join->send_records >= join->fetch_limit)
12012
4121
        {
12025
4134
    else
12026
4135
    {
12027
4136
      if (end_of_records)
12028
 
        return(NESTED_LOOP_OK);
 
4137
      {
 
4138
        return(NESTED_LOOP_OK);
 
4139
      }
12029
4140
      join->first_record=1;
12030
4141
      test_if_item_cache_changed(join->group_fields);
12031
4142
    }
12037
4148
      */
12038
4149
      copy_fields(&join->tmp_table_param);
12039
4150
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12040
 
        return(NESTED_LOOP_ERROR);
 
4151
      {
 
4152
        return(NESTED_LOOP_ERROR);
 
4153
      }
12041
4154
      return(ok_code);
12042
4155
    }
12043
4156
  }
12046
4159
  return(NESTED_LOOP_OK);
12047
4160
}
12048
4161
 
12049
 
 
12050
 
/* ARGSUSED */
12051
 
enum_nested_loop_state
12052
 
end_write(JOIN *join, JOIN_TAB *,
12053
 
          bool end_of_records)
12054
 
{
12055
 
  Table *table=join->tmp_table;
12056
 
 
12057
 
  if (join->session->killed)                    // Aborted by user
12058
 
  {
12059
 
    join->session->send_kill_message();
12060
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12061
 
  }
12062
 
  if (!end_of_records)
12063
 
  {
12064
 
    copy_fields(&join->tmp_table_param);
12065
 
    copy_funcs(join->tmp_table_param.items_to_copy);
12066
 
    if (!join->having || join->having->val_int())
12067
 
    {
12068
 
      int error;
12069
 
      join->found_records++;
12070
 
      if ((error=table->file->ha_write_row(table->record[0])))
12071
 
      {
12072
 
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
12073
 
          goto end;
12074
 
        if (create_myisam_from_heap(join->session, table,
12075
 
                                    join->tmp_table_param.start_recinfo,
12076
 
                                    &join->tmp_table_param.recinfo,
12077
 
                                    error, 1))
12078
 
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
12079
 
        table->s->uniques=0;                    // To ensure rows are the same
12080
 
      }
12081
 
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
12082
 
          join->do_send_rows)
12083
 
      {
12084
 
        if (!(join->select_options & OPTION_FOUND_ROWS))
12085
 
          return(NESTED_LOOP_QUERY_LIMIT);
12086
 
        join->do_send_rows=0;
12087
 
        join->unit->select_limit_cnt = HA_POS_ERROR;
12088
 
        return(NESTED_LOOP_OK);
12089
 
      }
12090
 
    }
12091
 
  }
12092
 
end:
12093
 
  return(NESTED_LOOP_OK);
12094
 
}
12095
 
 
12096
 
/* ARGSUSED */
12097
 
/** Group by searching after group record and updating it if possible. */
12098
 
 
12099
 
static enum_nested_loop_state
12100
 
end_update(JOIN *join, JOIN_TAB *,
12101
 
           bool end_of_records)
12102
 
{
12103
 
  Table *table=join->tmp_table;
12104
 
  order_st   *group;
12105
 
  int     error;
12106
 
 
12107
 
  if (end_of_records)
12108
 
    return(NESTED_LOOP_OK);
12109
 
  if (join->session->killed)                    // Aborted by user
12110
 
  {
12111
 
    join->session->send_kill_message();
12112
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12113
 
  }
12114
 
 
12115
 
  join->found_records++;
12116
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
12117
 
  /* Make a key of group index */
12118
 
  for (group=table->group ; group ; group=group->next)
12119
 
  {
12120
 
    Item *item= *group->item;
12121
 
    item->save_org_in_field(group->field);
12122
 
    /* Store in the used key if the field was 0 */
12123
 
    if (item->maybe_null)
12124
 
      group->buff[-1]= (char) group->field->is_null();
12125
 
  }
12126
 
  if (!table->file->index_read_map(table->record[1],
12127
 
                                   join->tmp_table_param.group_buff,
12128
 
                                   HA_WHOLE_KEY,
12129
 
                                   HA_READ_KEY_EXACT))
12130
 
  {                                             /* Update old record */
12131
 
    restore_record(table,record[1]);
12132
 
    update_tmptable_sum_func(join->sum_funcs,table);
12133
 
    if ((error=table->file->ha_update_row(table->record[1],
12134
 
                                          table->record[0])))
12135
 
    {
12136
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12137
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12138
 
    }
12139
 
    return(NESTED_LOOP_OK);
12140
 
  }
12141
 
 
12142
 
  /*
12143
 
    Copy null bits from group key to table
12144
 
    We can't copy all data as the key may have different format
12145
 
    as the row data (for example as with VARCHAR keys)
12146
 
  */
12147
 
  KEY_PART_INFO *key_part;
12148
 
  for (group=table->group,key_part=table->key_info[0].key_part;
12149
 
       group ;
12150
 
       group=group->next,key_part++)
12151
 
  {
12152
 
    if (key_part->null_bit)
12153
 
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
12154
 
  }
12155
 
  init_tmptable_sum_functions(join->sum_funcs);
12156
 
  copy_funcs(join->tmp_table_param.items_to_copy);
12157
 
  if ((error=table->file->ha_write_row(table->record[0])))
12158
 
  {
12159
 
    if (create_myisam_from_heap(join->session, table,
12160
 
                                join->tmp_table_param.start_recinfo,
12161
 
                                &join->tmp_table_param.recinfo,
12162
 
                                error, 0))
12163
 
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
12164
 
    /* Change method to update rows */
12165
 
    table->file->ha_index_init(0, 0);
12166
 
    join->join_tab[join->tables-1].next_select=end_unique_update;
12167
 
  }
12168
 
  join->send_records++;
12169
 
  return(NESTED_LOOP_OK);
12170
 
}
12171
 
 
12172
 
 
12173
 
/** Like end_update, but this is done with unique constraints instead of keys.  */
12174
 
 
12175
 
static enum_nested_loop_state
12176
 
end_unique_update(JOIN *join, JOIN_TAB *,
12177
 
                  bool end_of_records)
12178
 
{
12179
 
  Table *table=join->tmp_table;
12180
 
  int     error;
12181
 
 
12182
 
  if (end_of_records)
12183
 
    return(NESTED_LOOP_OK);
12184
 
  if (join->session->killed)                    // Aborted by user
12185
 
  {
12186
 
    join->session->send_kill_message();
12187
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12188
 
  }
12189
 
 
12190
 
  init_tmptable_sum_functions(join->sum_funcs);
12191
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
12192
 
  copy_funcs(join->tmp_table_param.items_to_copy);
12193
 
 
12194
 
  if (!(error=table->file->ha_write_row(table->record[0])))
12195
 
    join->send_records++;                       // New group
12196
 
  else
12197
 
  {
12198
 
    if ((int) table->file->get_dup_key(error) < 0)
12199
 
    {
12200
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12201
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12202
 
    }
12203
 
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
12204
 
    {
12205
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12206
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12207
 
    }
12208
 
    restore_record(table,record[1]);
12209
 
    update_tmptable_sum_func(join->sum_funcs,table);
12210
 
    if ((error=table->file->ha_update_row(table->record[1],
12211
 
                                          table->record[0])))
12212
 
    {
12213
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12214
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12215
 
    }
12216
 
  }
12217
 
  return(NESTED_LOOP_OK);
12218
 
}
12219
 
 
12220
 
 
12221
 
/* ARGSUSED */
12222
 
enum_nested_loop_state
12223
 
end_write_group(JOIN *join, JOIN_TAB *,
12224
 
                bool end_of_records)
 
4162
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
12225
4163
{
12226
4164
  Table *table=join->tmp_table;
12227
4165
  int     idx= -1;
12228
4166
 
12229
 
  if (join->session->killed)
 
4167
  if (join->session->getKilled())
12230
4168
  {                                             // Aborted by user
12231
4169
    join->session->send_kill_message();
12232
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
4170
    return NESTED_LOOP_KILLED;
12233
4171
  }
12234
 
  if (!join->first_record || end_of_records ||
12235
 
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
 
4172
 
 
4173
  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
12236
4174
  {
12237
 
    if (join->first_record || (end_of_records && !join->group))
 
4175
    if (join->first_record or (end_of_records && !join->group))
12238
4176
    {
12239
4177
      int send_group_parts= join->send_group_parts;
12240
4178
      if (idx < send_group_parts)
12241
4179
      {
12242
 
        if (!join->first_record)
12243
 
        {
12244
 
          /* No matching rows for group function */
12245
 
          join->clear();
12246
 
        }
12247
 
        copy_sum_funcs(join->sum_funcs,
12248
 
                       join->sum_funcs_end[send_group_parts]);
12249
 
        if (!join->having || join->having->val_int())
12250
 
        {
12251
 
          int error= table->file->ha_write_row(table->record[0]);
12252
 
          if (error && create_myisam_from_heap(join->session, table,
12253
 
                                               join->tmp_table_param.start_recinfo,
12254
 
                                                &join->tmp_table_param.recinfo,
12255
 
                                               error, 0))
12256
 
            return(NESTED_LOOP_ERROR);
12257
 
        }
12258
 
        if (join->rollup.state != ROLLUP::STATE_NONE)
12259
 
        {
12260
 
          if (join->rollup_write_data((uint) (idx+1), table))
12261
 
            return(NESTED_LOOP_ERROR);
12262
 
        }
12263
 
        if (end_of_records)
12264
 
          return(NESTED_LOOP_OK);
 
4180
        if (!join->first_record)
 
4181
        {
 
4182
          /* No matching rows for group function */
 
4183
          join->clear();
 
4184
        }
 
4185
 
 
4186
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
 
4187
 
 
4188
        if (!join->having || join->having->val_int())
 
4189
        {
 
4190
          int error= table->cursor->insertRecord(table->getInsertRecord());
 
4191
 
 
4192
          if (error)
 
4193
          {
 
4194
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
 
4195
            return NESTED_LOOP_ERROR;
 
4196
          }
 
4197
        }
 
4198
 
 
4199
        if (join->rollup.getState() != Rollup::STATE_NONE)
 
4200
        {
 
4201
          if (join->rollup_write_data((uint32_t) (idx+1), table))
 
4202
          {
 
4203
            return NESTED_LOOP_ERROR;
 
4204
          }
 
4205
        }
 
4206
 
 
4207
        if (end_of_records)
 
4208
        {
 
4209
          return NESTED_LOOP_OK;
 
4210
        }
12265
4211
      }
12266
4212
    }
12267
4213
    else
12268
4214
    {
12269
4215
      if (end_of_records)
12270
 
        return(NESTED_LOOP_OK);
 
4216
      {
 
4217
        return NESTED_LOOP_OK;
 
4218
      }
12271
4219
      join->first_record=1;
12272
4220
      test_if_item_cache_changed(join->group_fields);
12273
4221
    }
12274
4222
    if (idx < (int) join->send_group_parts)
12275
4223
    {
12276
4224
      copy_fields(&join->tmp_table_param);
12277
 
      copy_funcs(join->tmp_table_param.items_to_copy);
 
4225
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
 
4226
      {
 
4227
        return NESTED_LOOP_ERROR;
 
4228
      }
 
4229
 
12278
4230
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12279
 
        return(NESTED_LOOP_ERROR);
12280
 
      return(NESTED_LOOP_OK);
 
4231
      {
 
4232
        return NESTED_LOOP_ERROR;
 
4233
      }
 
4234
 
 
4235
      return NESTED_LOOP_OK;
12281
4236
    }
12282
4237
  }
 
4238
 
12283
4239
  if (update_sum_func(join->sum_funcs))
12284
 
    return(NESTED_LOOP_ERROR);
12285
 
  return(NESTED_LOOP_OK);
 
4240
  {
 
4241
    return NESTED_LOOP_ERROR;
 
4242
  }
 
4243
 
 
4244
  return NESTED_LOOP_OK;
12286
4245
}
12287
4246
 
12288
 
 
12289
4247
/*****************************************************************************
12290
4248
  Remove calculation with tables that aren't yet read. Remove also tests
12291
4249
  against fields that are read through key where the table is not a
12292
4250
  outer join table.
12293
4251
  We can't remove tests that are made against columns which are stored
12294
4252
  in sorted order.
12295
 
*****************************************************************************/
12296
 
 
12297
 
/**
12298
4253
  @return
12299
 
    1 if right_item is used removable reference key on left_item
12300
 
*/
12301
 
 
12302
 
static bool test_if_ref(Item_field *left_item,Item *right_item)
 
4254
    1 if right_item used is a removable reference key on left_item
 
4255
    0 otherwise.
 
4256
****************************************************************************/
 
4257
bool test_if_ref(Item_field *left_item,Item *right_item)
12303
4258
{
12304
4259
  Field *field=left_item->field;
12305
4260
  // No need to change const test. We also have to keep tests on LEFT JOIN
12306
 
  if (!field->table->const_table && !field->table->maybe_null)
 
4261
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
12307
4262
  {
12308
 
    Item *ref_item=part_of_refkey(field->table,field);
 
4263
    Item *ref_item=part_of_refkey(field->getTable(),field);
12309
4264
    if (ref_item && ref_item->eq(right_item,1))
12310
4265
    {
12311
4266
      right_item= right_item->real_item();
12312
4267
      if (right_item->type() == Item::FIELD_ITEM)
12313
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
4268
        return (field->eq_def(((Item_field *) right_item)->field));
12314
4269
      /* remove equalities injected by IN->EXISTS transformation */
12315
4270
      else if (right_item->type() == Item::CACHE_ITEM)
12316
4271
        return ((Item_cache *)right_item)->eq_def (field);
12317
4272
      if (right_item->const_item() && !(right_item->is_null()))
12318
4273
      {
12319
 
        /*
12320
 
          We can remove binary fields and numerical fields except float,
12321
 
          as float comparison isn't 100 % secure
12322
 
          We have to keep normal strings to be able to check for end spaces
 
4274
        /*
 
4275
          We can remove binary fields and numerical fields except float,
 
4276
          as float comparison isn't 100 % secure
 
4277
          We have to keep normal strings to be able to check for end spaces
12323
4278
 
12324
 
          sergefp: the above seems to be too restrictive. Counterexample:
12325
 
            create table t100 (v varchar(10), key(v)) default charset=latin1;
12326
 
            insert into t100 values ('a'),('a ');
12327
 
            explain select * from t100 where v='a';
12328
 
          The EXPLAIN shows 'using Where'. Running the query returns both
12329
 
          rows, so it seems there are no problems with endspace in the most
12330
 
          frequent case?
12331
 
        */
12332
 
        if (field->binary() &&
12333
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
12334
 
            field->decimals() == 0)
12335
 
        {
12336
 
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
12337
 
        }
 
4279
                sergefp: the above seems to be too restrictive. Counterexample:
 
4280
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
 
4281
                  insert into t100 values ('a'),('a ');
 
4282
                  explain select * from t100 where v='a';
 
4283
                The EXPLAIN shows 'using Where'. Running the query returns both
 
4284
                rows, so it seems there are no problems with endspace in the most
 
4285
                frequent case?
 
4286
        */
 
4287
        if (field->binary() &&
 
4288
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
4289
            field->decimals() == 0)
 
4290
        {
 
4291
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
4292
        }
12338
4293
      }
12339
4294
    }
12340
4295
  }
12341
 
  return 0;                                     // keep test
12342
 
}
12343
 
 
12344
 
/**
12345
 
   @brief Replaces an expression destructively inside the expression tree of
12346
 
   the WHERE clase.
12347
 
 
12348
 
   @note Because of current requirements for semijoin flattening, we do not
12349
 
   need to recurse here, hence this function will only examine the top-level
12350
 
   AND conditions. (see JOIN::prepare, comment above the line
12351
 
   'if (do_materialize)'
12352
 
 
12353
 
   @param join The top-level query.
12354
 
   @param old_cond The expression to be replaced.
12355
 
   @param new_cond The expression to be substituted.
12356
 
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
12357
 
   the new expression.
12358
 
   @return <code>true</code> if there was an error, <code>false</code> if
12359
 
   successful.
12360
 
*/
12361
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
12362
 
                                       Item *new_cond, bool do_fix_fields)
12363
 
{
12364
 
  if (join->conds == old_cond) {
12365
 
    join->conds= new_cond;
12366
 
    if (do_fix_fields)
12367
 
      new_cond->fix_fields(join->session, &join->conds);
12368
 
    return false;
12369
 
  }
12370
 
 
12371
 
  if (join->conds->type() == Item::COND_ITEM) {
12372
 
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
12373
 
    Item *item;
12374
 
    while ((item= li++))
12375
 
      if (item == old_cond)
12376
 
      {
12377
 
        li.replace(new_cond);
12378
 
        if (do_fix_fields)
12379
 
          new_cond->fix_fields(join->session, li.ref());
12380
 
        return false;
12381
 
      }
12382
 
  }
12383
 
 
12384
 
  return true;
 
4296
  return 0;
12385
4297
}
12386
4298
 
12387
4299
/*
12415
4327
  RETURN
12416
4328
    Extracted condition
12417
4329
*/
12418
 
 
12419
 
static COND *
12420
 
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
12421
 
                    bool exclude_expensive_cond)
 
4330
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
12422
4331
{
12423
4332
  if (used_table && !(cond->used_tables() & used_table) &&
12424
 
      /*
12425
 
        Exclude constant conditions not checked at optimization time if
12426
 
        the table we are pushing conditions to is the first one.
12427
 
        As a result, such conditions are not considered as already checked
12428
 
        and will be checked at execution time, attached to the first table.
12429
 
      */
12430
 
      !((used_table & 1) && cond->is_expensive()))
 
4333
    /*
 
4334
      Exclude constant conditions not checked at optimization time if
 
4335
      the table we are pushing conditions to is the first one.
 
4336
      As a result, such conditions are not considered as already checked
 
4337
      and will be checked at execution time, attached to the first table.
 
4338
    */
 
4339
    !((used_table & 1) && cond->is_expensive()))
 
4340
  {
12431
4341
    return (COND*) 0;                           // Already checked
 
4342
  }
 
4343
 
12432
4344
  if (cond->type() == Item::COND_ITEM)
12433
4345
  {
12434
4346
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
12435
4347
    {
12436
4348
      /* Create new top level AND item */
12437
4349
      Item_cond_and *new_cond=new Item_cond_and;
12438
 
      if (!new_cond)
12439
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
12440
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
4350
      if (new_cond == NULL)
 
4351
      {
 
4352
        return (COND*) 0;
 
4353
      }
 
4354
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
12441
4355
      Item *item;
12442
4356
      while ((item=li++))
12443
4357
      {
12444
 
        Item *fix=make_cond_for_table(item,tables,used_table,
12445
 
                                      exclude_expensive_cond);
12446
 
        if (fix)
12447
 
          new_cond->argument_list()->push_back(fix);
 
4358
        Item *fix= make_cond_for_table(item,tables,used_table,
 
4359
                                            exclude_expensive_cond);
 
4360
        if (fix)
 
4361
        {
 
4362
          new_cond->argument_list()->push_back(fix);
 
4363
        }
12448
4364
      }
12449
 
      switch (new_cond->argument_list()->elements) {
12450
 
      case 0:
12451
 
        return (COND*) 0;                       // Always true
12452
 
      case 1:
12453
 
        return new_cond->argument_list()->head();
12454
 
      default:
12455
 
        /*
12456
 
          Item_cond_and do not need fix_fields for execution, its parameters
12457
 
          are fixed or do not need fix_fields, too
12458
 
        */
12459
 
        new_cond->quick_fix_field();
12460
 
        new_cond->used_tables_cache=
12461
 
          ((Item_cond_and*) cond)->used_tables_cache &
12462
 
          tables;
12463
 
        return new_cond;
 
4365
      switch (new_cond->argument_list()->size())
 
4366
      {
 
4367
        case 0:
 
4368
          return (COND*) 0;                     // Always true
 
4369
 
 
4370
        case 1:
 
4371
          return &new_cond->argument_list()->front();
 
4372
 
 
4373
        default:
 
4374
          /*
 
4375
            Item_cond_and do not need fix_fields for execution, its parameters
 
4376
            are fixed or do not need fix_fields, too
 
4377
          */
 
4378
          new_cond->quick_fix_field();
 
4379
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
 
4380
          return new_cond;
12464
4381
      }
12465
4382
    }
12466
4383
    else
12467
4384
    {                                           // Or list
12468
4385
      Item_cond_or *new_cond=new Item_cond_or;
12469
 
      if (!new_cond)
12470
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
12471
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
4386
      if (new_cond == NULL)
 
4387
      {
 
4388
        return (COND*) 0;
 
4389
      }
 
4390
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
12472
4391
      Item *item;
12473
4392
      while ((item=li++))
12474
4393
      {
12475
 
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
12476
 
        if (!fix)
12477
 
          return (COND*) 0;                     // Always true
12478
 
        new_cond->argument_list()->push_back(fix);
 
4394
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
4395
        if (!fix)
 
4396
        {
 
4397
          return (COND*) 0;                     // Always true
 
4398
        }
 
4399
        new_cond->argument_list()->push_back(fix);
12479
4400
      }
12480
4401
      /*
12481
 
        Item_cond_and do not need fix_fields for execution, its parameters
12482
 
        are fixed or do not need fix_fields, too
 
4402
        Item_cond_and do not need fix_fields for execution, its parameters
 
4403
        are fixed or do not need fix_fields, too
12483
4404
      */
12484
4405
      new_cond->quick_fix_field();
12485
4406
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
12486
4407
      new_cond->top_level_item();
 
4408
 
12487
4409
      return new_cond;
12488
4410
    }
12489
4411
  }
12500
4422
        non-constant, so that they are not evaluated at optimization time.
12501
4423
      */
12502
4424
      (!used_table && exclude_expensive_cond && cond->is_expensive()))
 
4425
  {
12503
4426
    return (COND*) 0;                           // Can't check this yet
 
4427
  }
 
4428
 
12504
4429
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
 
4430
  {
12505
4431
    return cond;                                // Not boolean op
 
4432
  }
12506
4433
 
12507
4434
  /*
12508
4435
    Remove equalities that are guaranteed to be true by use of 'ref' access
12512
4439
  {
12513
4440
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
12514
4441
    Item *right_item= ((Item_func*) cond)->arguments()[1];
12515
 
    if (left_item->type() == Item::FIELD_ITEM &&
12516
 
        test_if_ref((Item_field*) left_item,right_item))
 
4442
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
12517
4443
    {
12518
4444
      cond->marker=3;                   // Checked when read
12519
4445
      return (COND*) 0;
12520
4446
    }
12521
 
    if (right_item->type() == Item::FIELD_ITEM &&
12522
 
        test_if_ref((Item_field*) right_item,left_item))
 
4447
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
12523
4448
    {
12524
4449
      cond->marker=3;                   // Checked when read
12525
4450
      return (COND*) 0;
12529
4454
  return cond;
12530
4455
}
12531
4456
 
12532
 
 
12533
 
static Item *
12534
 
part_of_refkey(Table *table,Field *field)
 
4457
static Item *part_of_refkey(Table *table,Field *field)
12535
4458
{
12536
4459
  if (!table->reginfo.join_tab)
 
4460
  {
12537
4461
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
 
4462
  }
12538
4463
 
12539
4464
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
12540
4465
  if (ref_parts)
12541
4466
  {
12542
 
    KEY_PART_INFO *key_part=
 
4467
    KeyPartInfo *key_part=
12543
4468
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
12544
4469
    uint32_t part;
12545
4470
 
12546
4471
    for (part=0 ; part < ref_parts ; part++)
12547
4472
    {
12548
4473
      if (table->reginfo.join_tab->ref.cond_guards[part])
 
4474
      {
12549
4475
        return 0;
 
4476
      }
12550
4477
    }
12551
4478
 
12552
4479
    for (part=0 ; part < ref_parts ; part++,key_part++)
 
4480
    {
12553
4481
      if (field->eq(key_part->field) &&
12554
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
 
4482
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
 
4483
          //If field can be NULL, we should not remove this predicate, as
 
4484
          //it may lead to non-rejection of NULL values.
 
4485
          !(field->real_maybe_null()))
 
4486
      {
12555
4487
        return table->reginfo.join_tab->ref.items[part];
 
4488
      }
 
4489
    }
12556
4490
  }
 
4491
 
12557
4492
  return (Item*) 0;
12558
4493
}
12559
4494
 
12560
 
 
12561
4495
/**
12562
4496
  Test if one can use the key to resolve order_st BY.
12563
4497
 
12578
4512
  @retval
12579
4513
    -1   Reverse key can be used
12580
4514
*/
12581
 
 
12582
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
12583
 
                                uint32_t *used_key_parts)
 
4515
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
12584
4516
{
12585
 
  KEY_PART_INFO *key_part,*key_part_end;
12586
 
  key_part=table->key_info[idx].key_part;
12587
 
  key_part_end=key_part+table->key_info[idx].key_parts;
 
4517
  KeyPartInfo *key_part= NULL;
 
4518
  KeyPartInfo *key_part_end= NULL;
 
4519
  key_part= table->key_info[idx].key_part;
 
4520
  key_part_end= key_part + table->key_info[idx].key_parts;
12588
4521
  key_part_map const_key_parts=table->const_key_parts[idx];
12589
 
  int reverse=0;
 
4522
  int reverse= 0;
12590
4523
  bool on_primary_key= false;
12591
4524
 
12592
4525
  for (; order ; order=order->next, const_key_parts>>=1)
12596
4529
 
12597
4530
    /*
12598
4531
      Skip key parts that are constants in the WHERE clause.
12599
 
      These are already skipped in the order_st BY by const_expression_in_where()
 
4532
      These are already skipped in the ORDER BY by const_expression_in_where()
12600
4533
    */
12601
4534
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
 
4535
    {
12602
4536
      key_part++;
 
4537
    }
12603
4538
 
12604
4539
    if (key_part == key_part_end)
12605
4540
    {
12609
4544
        the primary key as a suffix.
12610
4545
      */
12611
4546
      if (!on_primary_key &&
12612
 
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
12613
 
          table->s->primary_key != MAX_KEY)
 
4547
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
 
4548
          table->getShare()->hasPrimaryKey())
12614
4549
      {
12615
4550
        on_primary_key= true;
12616
 
        key_part= table->key_info[table->s->primary_key].key_part;
12617
 
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
12618
 
        const_key_parts=table->const_key_parts[table->s->primary_key];
 
4551
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
 
4552
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
 
4553
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
12619
4554
 
12620
4555
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
 
4556
        {
12621
4557
          key_part++;
 
4558
        }
 
4559
 
12622
4560
        /*
12623
4561
         The primary and secondary key parts were all const (i.e. there's
12624
4562
         one row).  The sorting doesn't matter.
12625
4563
        */
12626
4564
        if (key_part == key_part_end && reverse == 0)
12627
 
          return(1);
 
4565
        {
 
4566
          return 1;
 
4567
        }
12628
4568
      }
12629
4569
      else
12630
 
        return(0);
 
4570
      {
 
4571
        return 0;
 
4572
      }
12631
4573
    }
12632
4574
 
12633
4575
    if (key_part->field != field)
12634
 
      return(0);
 
4576
    {
 
4577
      return 0;
 
4578
    }
12635
4579
 
12636
4580
    /* set flag to 1 if we can use read-next on key, else to -1 */
12637
4581
    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
12638
4582
           1 : -1);
12639
4583
    if (reverse && flag != reverse)
12640
 
      return(0);
 
4584
    {
 
4585
      return 0;
 
4586
    }
12641
4587
    reverse=flag;                               // Remember if reverse
12642
4588
    key_part++;
12643
4589
  }
12644
4590
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
12645
 
    (uint) (key_part - table->key_info[idx].key_part);
12646
 
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
12647
 
                         HA_READ_PREV))
 
4591
    (uint32_t) (key_part - table->key_info[idx].key_part);
 
4592
  if (reverse == -1 && !(table->index_flags(idx) & HA_READ_PREV))
 
4593
  {
12648
4594
    reverse= 0;                                 // Index can't be used
 
4595
  }
 
4596
 
12649
4597
  return(reverse);
12650
4598
}
12651
4599
 
12652
 
 
12653
4600
/**
12654
4601
  Test if a second key is the subkey of the first one.
12655
4602
 
12665
4612
  @retval
12666
4613
    0   no sub key
12667
4614
*/
12668
 
 
12669
 
inline bool
12670
 
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
12671
 
          KEY_PART_INFO *ref_key_part_end)
 
4615
inline bool is_subkey(KeyPartInfo *key_part,
 
4616
                      KeyPartInfo *ref_key_part,
 
4617
                      KeyPartInfo *ref_key_part_end)
12672
4618
{
12673
4619
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
12674
 
    if (!key_part->field->eq(ref_key_part->field))
 
4620
  {
 
4621
    if (! key_part->field->eq(ref_key_part->field))
 
4622
    {
12675
4623
      return 0;
 
4624
    }
 
4625
  }
 
4626
 
12676
4627
  return 1;
12677
4628
}
12678
4629
 
12687
4638
    - MAX_KEY                   If we can't use other key
12688
4639
    - the number of found key   Otherwise
12689
4640
*/
12690
 
 
12691
 
static uint
12692
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
12693
 
               const key_map *usable_keys)
 
4641
static uint32_t test_if_subkey(Order *order,
 
4642
                               Table *table,
 
4643
                               uint32_t ref,
 
4644
                               uint32_t ref_key_parts,
 
4645
                               const key_map *usable_keys)
12694
4646
{
12695
4647
  uint32_t nr;
12696
4648
  uint32_t min_length= UINT32_MAX;
12697
4649
  uint32_t best= MAX_KEY;
12698
4650
  uint32_t not_used;
12699
 
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
12700
 
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
 
4651
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
 
4652
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
12701
4653
 
12702
 
  for (nr= 0 ; nr < table->s->keys ; nr++)
 
4654
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
12703
4655
  {
12704
 
    if (usable_keys->is_set(nr) &&
 
4656
    if (usable_keys->test(nr) &&
12705
4657
        table->key_info[nr].key_length < min_length &&
12706
4658
        table->key_info[nr].key_parts >= ref_key_parts &&
12707
4659
        is_subkey(table->key_info[nr].key_part, ref_key_part,
12715
4667
  return best;
12716
4668
}
12717
4669
 
12718
 
 
12719
4670
/**
12720
4671
  Check if GROUP BY/DISTINCT can be optimized away because the set is
12721
4672
  already known to be distinct.
12747
4698
  @retval
12748
4699
    0                    not found.
12749
4700
*/
12750
 
 
12751
 
static bool
12752
 
list_contains_unique_index(Table *table,
12753
 
                          bool (*find_func) (Field *, void *), void *data)
 
4701
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
12754
4702
{
12755
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
4703
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
12756
4704
  {
12757
 
    if (keynr == table->s->primary_key ||
 
4705
    if (keynr == table->getShare()->getPrimaryKey() ||
12758
4706
         (table->key_info[keynr].flags & HA_NOSAME))
12759
4707
    {
12760
 
      KEY *keyinfo= table->key_info + keynr;
12761
 
      KEY_PART_INFO *key_part, *key_part_end;
 
4708
      KeyInfo *keyinfo= table->key_info + keynr;
 
4709
      KeyPartInfo *key_part= NULL;
 
4710
      KeyPartInfo *key_part_end= NULL;
12762
4711
 
12763
4712
      for (key_part=keyinfo->key_part,
12764
4713
           key_part_end=key_part+ keyinfo->key_parts;
12765
4714
           key_part < key_part_end;
12766
4715
           key_part++)
12767
4716
      {
12768
 
        if (key_part->field->maybe_null() ||
12769
 
            !find_func(key_part->field, data))
 
4717
        if (key_part->field->maybe_null() || ! find_func(key_part->field, data))
 
4718
        {
12770
4719
          break;
 
4720
        }
12771
4721
      }
 
4722
 
12772
4723
      if (key_part == key_part_end)
 
4724
      {
12773
4725
        return 1;
 
4726
      }
12774
4727
    }
12775
4728
  }
12776
4729
  return 0;
12777
4730
}
12778
4731
 
12779
 
 
12780
4732
/**
12781
4733
  Helper function for list_contains_unique_index.
12782
4734
  Find a field reference in a list of order_st structures.
12790
4742
  @retval
12791
4743
    0                    not found.
12792
4744
*/
12793
 
 
12794
 
static bool
12795
 
find_field_in_order_list (Field *field, void *data)
 
4745
bool find_field_in_order_list (Field *field, void *data)
12796
4746
{
12797
 
  order_st *group= (order_st *) data;
 
4747
  Order *group= (Order *) data;
12798
4748
  bool part_found= 0;
12799
 
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4749
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
12800
4750
  {
12801
4751
    Item *item= (*tmp_group->item)->real_item();
12802
4752
    if (item->type() == Item::FIELD_ITEM &&
12809
4759
  return part_found;
12810
4760
}
12811
4761
 
12812
 
 
12813
4762
/**
12814
4763
  Helper function for list_contains_unique_index.
12815
4764
  Find a field reference in a dynamic list of Items.
12823
4772
  @retval
12824
4773
    0                    not found.
12825
4774
*/
12826
 
 
12827
 
static bool
12828
 
find_field_in_item_list (Field *field, void *data)
 
4775
bool find_field_in_item_list (Field *field, void *data)
12829
4776
{
12830
4777
  List<Item> *fields= (List<Item> *) data;
12831
4778
  bool part_found= 0;
12832
 
  List_iterator<Item> li(*fields);
 
4779
  List<Item>::iterator li(fields->begin());
12833
4780
  Item *item;
12834
4781
 
12835
4782
  while ((item= li++))
12844
4791
  return part_found;
12845
4792
}
12846
4793
 
12847
 
 
12848
4794
/**
12849
 
  Test if we can skip the order_st BY by using an index.
 
4795
  Test if we can skip the ORDER BY by using an index.
12850
4796
 
12851
4797
  SYNOPSIS
12852
4798
    test_if_skip_sort_order()
12856
4802
      no_changes
12857
4803
      map
12858
4804
 
12859
 
  If we can use an index, the JOIN_TAB / tab->select struct
 
4805
  If we can use an index, the JoinTable / tab->select struct
12860
4806
  is changed to use the index.
12861
4807
 
12862
4808
  The index must cover all fields in <order>, or it will not be considered.
12870
4816
  @retval
12871
4817
    1    We can use an index.
12872
4818
*/
12873
 
 
12874
 
static bool
12875
 
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
12876
 
                        bool no_changes, const key_map *map)
 
4819
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
12877
4820
{
12878
4821
  int32_t ref_key;
12879
4822
  uint32_t ref_key_parts;
12880
4823
  int order_direction;
12881
4824
  uint32_t used_key_parts;
12882
4825
  Table *table=tab->table;
12883
 
  SQL_SELECT *select=tab->select;
 
4826
  optimizer::SqlSelect *select= tab->select;
12884
4827
  key_map usable_keys;
12885
 
  QUICK_SELECT_I *save_quick= 0;
 
4828
  optimizer::QuickSelectInterface *save_quick= NULL;
12886
4829
 
12887
4830
  /*
12888
4831
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
12890
4833
  */
12891
4834
  usable_keys= *map;
12892
4835
 
12893
 
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4836
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
12894
4837
  {
12895
4838
    Item *item= (*tmp_order->item)->real_item();
12896
4839
    if (item->type() != Item::FIELD_ITEM)
12897
4840
    {
12898
 
      usable_keys.clear_all();
12899
 
      return(0);
12900
 
    }
12901
 
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
12902
 
    if (usable_keys.is_clear_all())
12903
 
      return(0);                                        // No usable keys
 
4841
      usable_keys.reset();
 
4842
      return 0;
 
4843
    }
 
4844
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
 
4845
    if (usable_keys.none())
 
4846
    {
 
4847
      return 0;                                 // No usable keys
 
4848
    }
12904
4849
  }
12905
4850
 
12906
4851
  ref_key= -1;
12909
4854
  {
12910
4855
    ref_key=       tab->ref.key;
12911
4856
    ref_key_parts= tab->ref.key_parts;
12912
 
    if (tab->type == JT_REF_OR_NULL)
12913
 
      return(0);
 
4857
    if (tab->type == AM_REF_OR_NULL)
 
4858
    {
 
4859
      return 0;
 
4860
    }
12914
4861
  }
12915
 
  else if (select && select->quick)             // Range found by opt_range
 
4862
  else if (select && select->quick)             // Range found by optimizer/range
12916
4863
  {
12917
4864
    int quick_type= select->quick->get_type();
12918
4865
    save_quick= select->quick;
12919
4866
    /*
12920
4867
      assume results are not ordered when index merge is used
12921
 
      TODO: sergeyp: Results of all index merge selects actually are ordered
 
4868
      @todo sergeyp: Results of all index merge selects actually are ordered
12922
4869
      by clustered PK values.
12923
4870
    */
12924
4871
 
12925
 
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
12926
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
12927
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
12928
 
      return(0);
 
4872
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
 
4873
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
 
4874
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
 
4875
    {
 
4876
      return 0;
 
4877
    }
12929
4878
    ref_key=       select->quick->index;
12930
4879
    ref_key_parts= select->quick->used_key_parts;
12931
4880
  }
12935
4884
    /*
12936
4885
      We come here when there is a REF key.
12937
4886
    */
12938
 
    if (!usable_keys.is_set(ref_key))
 
4887
    if (! usable_keys.test(ref_key))
12939
4888
    {
12940
4889
      /*
12941
 
        We come here when ref_key is not among usable_keys
 
4890
        We come here when ref_key is not among usable_keys
12942
4891
      */
12943
4892
      uint32_t new_ref_key;
12944
4893
      /*
12945
 
        If using index only read, only consider other possible index only
12946
 
        keys
 
4894
        If using index only read, only consider other possible index only
 
4895
        keys
12947
4896
      */
12948
 
      if (table->covering_keys.is_set(ref_key))
12949
 
        usable_keys.intersect(table->covering_keys);
 
4897
      if (table->covering_keys.test(ref_key))
 
4898
        usable_keys&= table->covering_keys;
 
4899
 
12950
4900
      if (tab->pre_idx_push_select_cond)
12951
4901
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
 
4902
 
12952
4903
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
12953
4904
                                       &usable_keys)) < MAX_KEY)
12954
4905
      {
12955
 
        /* Found key that can be used to retrieve data in sorted order */
12956
 
        if (tab->ref.key >= 0)
12957
 
        {
 
4906
        /* Found key that can be used to retrieve data in sorted order */
 
4907
        if (tab->ref.key >= 0)
 
4908
        {
12958
4909
          /*
12959
4910
            We'll use ref access method on key new_ref_key. In general case
12960
4911
            the index search tuple for new_ref_key will be different (e.g.
12963
4914
            "part1 = const1 AND part2=const2".
12964
4915
            So we build tab->ref from scratch here.
12965
4916
          */
12966
 
          KEYUSE *keyuse= tab->keyuse;
12967
 
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
 
4917
          optimizer::KeyUse *keyuse= tab->keyuse;
 
4918
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
12968
4919
            keyuse++;
12969
4920
 
12970
 
          if (create_ref_for_key(tab->join, tab, keyuse,
12971
 
                                 tab->join->const_table_map))
12972
 
            return(0);
12973
 
        }
12974
 
        else
12975
 
        {
 
4921
          if (create_ref_for_key(tab->join, tab, keyuse, tab->join->const_table_map))
 
4922
          {
 
4923
            return 0;
 
4924
          }
 
4925
        }
 
4926
        else
 
4927
        {
12976
4928
          /*
12977
 
            The range optimizer constructed QUICK_RANGE for ref_key, and
 
4929
            The range optimizer constructed QuickRange for ref_key, and
12978
4930
            we want to use instead new_ref_key as the index. We can't
12979
4931
            just change the index of the quick select, because this may
12980
4932
            result in an incosistent QUICK_SELECT object. Below we
12982
4934
            parameres are set correctly by the range optimizer.
12983
4935
           */
12984
4936
          key_map new_ref_key_map;
12985
 
          new_ref_key_map.clear_all();  // Force the creation of quick select
12986
 
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
 
4937
          new_ref_key_map.reset();  // Force the creation of quick select
 
4938
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
12987
4939
 
12988
4940
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
12989
4941
                                        (tab->join->select_options &
12992
4944
                                        tab->join->unit->select_limit_cnt,0,
12993
4945
                                        true) <=
12994
4946
              0)
12995
 
            return(0);
12996
 
        }
 
4947
          {
 
4948
            return 0;
 
4949
          }
 
4950
        }
12997
4951
        ref_key= new_ref_key;
12998
4952
      }
12999
4953
    }
13000
4954
    /* Check if we get the rows in requested sorted order by using the key */
13001
 
    if (usable_keys.is_set(ref_key) &&
 
4955
    if (usable_keys.test(ref_key) &&
13002
4956
        (order_direction= test_if_order_by_key(order,table,ref_key,
13003
4957
                                               &used_key_parts)))
13004
4958
      goto check_reverse_order;
13019
4973
    int best_key= -1;
13020
4974
    bool is_best_covering= false;
13021
4975
    double fanout= 1;
13022
 
    JOIN *join= tab->join;
 
4976
    Join *join= tab->join;
13023
4977
    uint32_t tablenr= tab - join->join_tab;
13024
 
    ha_rows table_records= table->file->stats.records;
 
4978
    ha_rows table_records= table->cursor->stats.records;
13025
4979
    bool group= join->group && order == join->group_list;
 
4980
    optimizer::Position cur_pos;
13026
4981
 
13027
4982
    /*
13028
4983
      If not used with LIMIT, only use keys if the whole query can be
13035
4990
        filesort() and join cache are usually faster than reading in
13036
4991
        index order and not using join cache
13037
4992
        */
13038
 
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
13039
 
        return(0);
13040
 
      keys= *table->file->keys_to_use_for_scanning();
13041
 
      keys.merge(table->covering_keys);
 
4993
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
4994
      {
 
4995
        return 0;
 
4996
      }
 
4997
      keys= *table->cursor->keys_to_use_for_scanning();
 
4998
      keys|= table->covering_keys;
13042
4999
 
13043
5000
      /*
13044
 
        We are adding here also the index specified in FORCE INDEX clause,
13045
 
        if any.
 
5001
        We are adding here also the index specified in FORCE INDEX clause,
 
5002
        if any.
13046
5003
        This is to allow users to use index in order_st BY.
13047
5004
      */
13048
5005
      if (table->force_index)
13049
 
        keys.merge(group ? table->keys_in_use_for_group_by :
13050
 
                           table->keys_in_use_for_order_by);
13051
 
      keys.intersect(usable_keys);
 
5006
      {
 
5007
        keys|= (group ? table->keys_in_use_for_group_by :
 
5008
                                table->keys_in_use_for_order_by);
 
5009
      }
 
5010
      keys&= usable_keys;
13052
5011
    }
13053
5012
    else
 
5013
    {
13054
5014
      keys= usable_keys;
13055
 
 
13056
 
    read_time= join->best_positions[tablenr].read_time;
 
5015
    }
 
5016
 
 
5017
    cur_pos= join->getPosFromOptimalPlan(tablenr);
 
5018
    read_time= cur_pos.getCost();
 
5019
 
13057
5020
    for (uint32_t i= tablenr+1; i < join->tables; i++)
13058
 
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
 
5021
    {
 
5022
      cur_pos= join->getPosFromOptimalPlan(i);
 
5023
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
 
5024
    }
13059
5025
 
13060
 
    for (nr=0; nr < table->s->keys ; nr++)
 
5026
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
13061
5027
    {
13062
5028
      int direction;
13063
 
      if (keys.is_set(nr) &&
 
5029
      if (keys.test(nr) &&
13064
5030
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
13065
5031
      {
13066
 
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
5032
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
13067
5033
 
13068
5034
        /*
13069
 
          Don't use an index scan with order_st BY without limit.
 
5035
          Don't use an index scan with ORDER BY without limit.
13070
5036
          For GROUP BY without limit always use index scan
13071
5037
          if there is a suitable index.
13072
5038
          Why we hold to this asymmetry hardly can be explained
13073
5039
          rationally. It's easy to demonstrate that using
13074
5040
          temporary table + filesort could be cheaper for grouping
13075
5041
          queries too.
13076
 
        */
 
5042
        */
13077
5043
        if (is_covering ||
13078
5044
            select_limit != HA_POS_ERROR ||
13079
5045
            (ref_key < 0 && (group || table->force_index)))
13080
5046
        {
13081
5047
          double rec_per_key;
13082
5048
          double index_scan_time;
13083
 
          KEY *keyinfo= tab->table->key_info+nr;
 
5049
          KeyInfo *keyinfo= tab->table->key_info+nr;
13084
5050
          if (select_limit == HA_POS_ERROR)
 
5051
          {
13085
5052
            select_limit= table_records;
 
5053
          }
 
5054
 
13086
5055
          if (group)
13087
5056
          {
13088
5057
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
13089
 
            set_if_bigger(rec_per_key, 1);
 
5058
            set_if_bigger(rec_per_key, 1.0);
13090
5059
            /*
13091
5060
              With a grouping query each group containing on average
13092
5061
              rec_per_key records produces only one row that will
13093
5062
              be included into the result set.
13094
 
            */
 
5063
            */
13095
5064
            if (select_limit > table_records/rec_per_key)
13096
 
                select_limit= table_records;
 
5065
            {
 
5066
              select_limit= table_records;
 
5067
            }
13097
5068
            else
 
5069
            {
13098
5070
              select_limit= (ha_rows) (select_limit*rec_per_key);
 
5071
            }
13099
5072
          }
13100
5073
          /*
13101
5074
            If tab=tk is not the last joined table tn then to get first
13106
5079
            So the estimate for L/fanout(tk,tn) will be too optimistic
13107
5080
            and as result we'll choose an index scan when using ref/range
13108
5081
            access + filesort will be cheaper.
13109
 
          */
 
5082
          */
13110
5083
          select_limit= (ha_rows) (select_limit < fanout ?
13111
5084
                                   1 : select_limit/fanout);
13112
5085
          /*
13120
5093
            <=> N > table->quick_condition_rows.
13121
5094
          */
13122
5095
          if (select_limit > table->quick_condition_rows)
 
5096
          {
13123
5097
            select_limit= table_records;
 
5098
          }
13124
5099
          else
 
5100
          {
13125
5101
            select_limit= (ha_rows) (select_limit *
13126
5102
                                     (double) table_records /
13127
 
                                      table->quick_condition_rows);
 
5103
                                     table->quick_condition_rows);
 
5104
          }
13128
5105
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
13129
 
          set_if_bigger(rec_per_key, 1);
 
5106
          set_if_bigger(rec_per_key, 1.0);
13130
5107
          /*
13131
5108
            Here we take into account the fact that rows are
13132
5109
            accessed in sequences rec_per_key records in each.
13133
5110
            Rows in such a sequence are supposed to be ordered
13134
5111
            by rowid/primary key. When reading the data
13135
5112
            in a sequence we'll touch not more pages than the
13136
 
            table file contains.
 
5113
            table cursor contains.
13137
5114
            TODO. Use the formula for a disk sweep sequential access
13138
5115
            to calculate the cost of accessing data rows for one
13139
5116
            index entry.
13140
 
          */
13141
 
          index_scan_time= select_limit/rec_per_key *
13142
 
                           cmin(rec_per_key, table->file->scan_time());
13143
 
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
13144
 
              index_scan_time < read_time)
 
5117
          */
 
5118
          index_scan_time= select_limit/rec_per_key * min(rec_per_key, table->cursor->scan_time());
 
5119
 
 
5120
          if (is_covering || (ref_key < 0 && (group || table->force_index)) || index_scan_time < read_time)
13145
5121
          {
13146
5122
            ha_rows quick_records= table_records;
13147
5123
            if (is_best_covering && !is_covering)
 
5124
            {
13148
5125
              continue;
13149
 
            if (table->quick_keys.is_set(nr))
 
5126
            }
 
5127
 
 
5128
            if (table->quick_keys.test(nr))
 
5129
            {
13150
5130
              quick_records= table->quick_rows[nr];
 
5131
            }
 
5132
 
13151
5133
            if (best_key < 0 ||
13152
 
                (select_limit <= cmin(quick_records,best_records) ?
 
5134
                (select_limit <= min(quick_records,best_records) ?
13153
5135
                 keyinfo->key_parts < best_key_parts :
13154
5136
                 quick_records < best_records))
13155
5137
            {
13160
5142
              best_key_direction= direction;
13161
5143
            }
13162
5144
          }
13163
 
        }
 
5145
        }
13164
5146
      }
13165
5147
    }
 
5148
 
13166
5149
    if (best_key >= 0)
13167
5150
    {
13168
5151
      bool quick_created= false;
13169
 
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
 
5152
      if (table->quick_keys.test(best_key) && best_key != ref_key)
13170
5153
      {
13171
 
        key_map map;
13172
 
        map.clear_all();       // Force the creation of quick select
13173
 
        map.set_bit(best_key); // only best_key.
 
5154
        key_map test_map;
 
5155
        test_map.reset();       // Force the creation of quick select
 
5156
        test_map.set(best_key); // only best_key.
13174
5157
        quick_created=
13175
 
          select->test_quick_select(join->session, map, 0,
 
5158
          select->test_quick_select(join->session, test_map, 0,
13176
5159
                                    join->select_options & OPTION_FOUND_ROWS ?
13177
5160
                                    HA_POS_ERROR :
13178
5161
                                    join->unit->select_limit_cnt,
13181
5164
      if (!no_changes)
13182
5165
      {
13183
5166
        if (!quick_created)
13184
 
        {
 
5167
        {
13185
5168
          tab->index= best_key;
13186
5169
          tab->read_first_record= best_key_direction > 0 ?
13187
5170
                                  join_read_first:join_read_last;
13188
 
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
 
5171
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
13189
5172
          if (select && select->quick)
13190
5173
          {
13191
 
            delete select->quick;
13192
 
            select->quick= 0;
 
5174
            safe_delete(select->quick);
13193
5175
          }
13194
 
          if (table->covering_keys.is_set(best_key))
 
5176
          if (table->covering_keys.test(best_key))
13195
5177
          {
13196
5178
            table->key_read=1;
13197
 
            table->file->extra(HA_EXTRA_KEYREAD);
 
5179
            table->cursor->extra(HA_EXTRA_KEYREAD);
13198
5180
          }
13199
 
          table->file->ha_index_or_rnd_end();
 
5181
          table->cursor->ha_index_or_rnd_end();
13200
5182
          if (join->select_options & SELECT_DESCRIBE)
13201
5183
          {
13202
5184
            tab->ref.key= -1;
13205
5187
              tab->limit= select_limit;
13206
5188
          }
13207
5189
        }
13208
 
        else if (tab->type != JT_ALL)
 
5190
        else if (tab->type != AM_ALL)
13209
5191
        {
13210
5192
          /*
13211
5193
            We're about to use a quick access to the table.
13213
5195
            method is actually used.
13214
5196
          */
13215
5197
          assert(tab->select->quick);
13216
 
          tab->type=JT_ALL;
 
5198
          tab->type= AM_ALL;
13217
5199
          tab->use_quick=1;
13218
5200
          tab->ref.key= -1;
13219
5201
          tab->ref.key_parts=0;         // Don't use ref key.
13220
5202
          tab->read_first_record= join_init_read_record;
13221
 
          /*
13222
 
            TODO: update the number of records in join->best_positions[tablenr]
13223
 
          */
13224
5203
        }
13225
5204
      }
13226
5205
      used_key_parts= best_key_parts;
13227
5206
      order_direction= best_key_direction;
13228
5207
    }
13229
5208
    else
13230
 
      return(0);
 
5209
    {
 
5210
      return 0;
 
5211
    }
13231
5212
  }
13232
5213
 
13233
5214
check_reverse_order:
13234
 
  if (order_direction == -1)            // If order_st BY ... DESC
 
5215
  if (order_direction == -1)            // If ORDER BY ... DESC
13235
5216
  {
13236
5217
    if (select && select->quick)
13237
5218
    {
13238
5219
      /*
13239
 
        Don't reverse the sort order, if it's already done.
 
5220
        Don't reverse the sort order, if it's already done.
13240
5221
        (In some cases test_if_order_by_key() can be called multiple times
13241
5222
      */
13242
 
      if (!select->quick->reverse_sorted())
 
5223
      if (! select->quick->reverse_sorted())
13243
5224
      {
13244
 
        QUICK_SELECT_DESC *tmp;
 
5225
        optimizer::QuickSelectDescending *tmp= NULL;
13245
5226
        bool error= false;
13246
5227
        int quick_type= select->quick->get_type();
13247
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
13248
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
13249
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
13250
 
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
5228
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
 
5229
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
 
5230
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
 
5231
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
13251
5232
        {
13252
5233
          tab->limit= 0;
13253
5234
          select->quick= save_quick;
13254
 
          return(0);                   // Use filesort
 
5235
          return 0; // Use filesort
13255
5236
        }
13256
5237
 
13257
 
        /* order_st BY range_key DESC */
13258
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
13259
 
                                    used_key_parts, &error);
13260
 
        if (!tmp || error)
13261
 
        {
13262
 
          delete tmp;
 
5238
        /* ORDER BY range_key DESC */
 
5239
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
 
5240
                                                  used_key_parts,
 
5241
                                                  &error);
 
5242
        if (! tmp || error)
 
5243
        {
 
5244
          delete tmp;
13263
5245
          select->quick= save_quick;
13264
5246
          tab->limit= 0;
13265
 
          return(0);            // Reverse sort not supported
13266
 
        }
13267
 
        select->quick=tmp;
 
5247
          return 0; // Reverse sort not supported
 
5248
        }
 
5249
        select->quick= tmp;
13268
5250
      }
13269
5251
    }
13270
 
    else if (tab->type != JT_NEXT &&
 
5252
    else if (tab->type != AM_NEXT &&
13271
5253
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
13272
5254
    {
13273
5255
      /*
13274
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
5256
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
13275
5257
 
13276
 
        Use a traversal function that starts by reading the last row
13277
 
        with key part (A) and then traverse the index backwards.
 
5258
        Use a traversal function that starts by reading the last row
 
5259
        with key part (A) and then traverse the index backwards.
13278
5260
      */
13279
5261
      tab->read_first_record= join_read_last_key;
13280
5262
      tab->read_record.read_record= join_read_prev_same;
13281
5263
    }
13282
5264
  }
13283
5265
  else if (select && select->quick)
 
5266
  {
13284
5267
    select->quick->sorted= 1;
13285
 
  return(1);
 
5268
  }
 
5269
 
 
5270
  return 1;
13286
5271
}
13287
5272
 
13288
 
 
13289
5273
/*
13290
5274
  If not selecting by given key, create an index how records should be read
13291
5275
 
13292
5276
  SYNOPSIS
13293
5277
   create_sort_index()
13294
 
     session            Thread handler
 
5278
     session            Thread Cursor
13295
5279
     tab                Table to sort (in join structure)
13296
5280
     order              How table should be sorted
13297
5281
     filesort_limit     Max number of rows that needs to be sorted
13304
5288
  IMPLEMENTATION
13305
5289
   - If there is an index that can be used, 'tab' is modified to use
13306
5290
     this index.
13307
 
   - If no index, create with filesort() an index file that can be used to
 
5291
   - If no index, create with filesort() an index cursor that can be used to
13308
5292
     retrieve rows in order (should be done with 'read_record').
13309
5293
     The sorted data is stored in tab->table and will be freed when calling
13310
 
     free_io_cache(tab->table).
 
5294
     tab->table->free_io_cache().
13311
5295
 
13312
5296
  RETURN VALUES
13313
5297
    0           ok
13314
5298
    -1          Some fatal error
13315
5299
    1           No records
13316
5300
*/
13317
 
 
13318
 
static int
13319
 
create_sort_index(Session *session, JOIN *join, order_st *order,
13320
 
                  ha_rows filesort_limit, ha_rows select_limit,
13321
 
                  bool is_order_by)
 
5301
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
13322
5302
{
13323
5303
  uint32_t length= 0;
13324
5304
  ha_rows examined_rows;
13325
5305
  Table *table;
13326
 
  SQL_SELECT *select;
13327
 
  JOIN_TAB *tab;
 
5306
  optimizer::SqlSelect *select= NULL;
 
5307
  JoinTable *tab;
13328
5308
 
13329
5309
  if (join->tables == join->const_tables)
13330
 
    return(0);                          // One row, no need to sort
 
5310
    return 0;                           // One row, no need to sort
13331
5311
  tab=    join->join_tab + join->const_tables;
13332
5312
  table=  tab->table;
13333
5313
  select= tab->select;
13340
5320
  */
13341
5321
  if ((order != join->group_list ||
13342
5322
       !(join->select_options & SELECT_BIG_RESULT) ||
13343
 
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
5323
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
13344
5324
      test_if_skip_sort_order(tab,order,select_limit,0,
13345
5325
                              is_order_by ?  &table->keys_in_use_for_order_by :
13346
5326
                              &table->keys_in_use_for_group_by))
13347
 
    return(0);
13348
 
  for (order_st *ord= join->order; ord; ord= ord->next)
 
5327
  {
 
5328
    return 0;
 
5329
  }
 
5330
 
 
5331
  for (Order *ord= join->order; ord; ord= ord->next)
 
5332
  {
13349
5333
    length++;
13350
 
  if (!(join->sortorder=
13351
 
        make_unireg_sortorder(order, &length, join->sortorder)))
13352
 
    goto err;                           /* purecov: inspected */
 
5334
  }
13353
5335
 
13354
 
  table->sort.io_cache= new IO_CACHE;
13355
 
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
 
5336
  join->sortorder= make_unireg_sortorder(order, &length, join->sortorder);
 
5337
  table->sort.io_cache= new internal::io_cache_st;
13356
5338
  table->status=0;                              // May be wrong if quick_select
13357
5339
 
13358
5340
  // If table has a range, move it to select
13366
5348
        We can only use 'Only index' if quick key is same as ref_key
13367
5349
        and in index_merge 'Only index' cannot be used
13368
5350
      */
13369
 
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
 
5351
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
13370
5352
      {
13371
 
        table->key_read=0;
13372
 
        table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5353
        table->key_read=0;
 
5354
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13373
5355
      }
13374
5356
    }
13375
5357
    else
13376
5358
    {
13377
5359
      /*
13378
 
        We have a ref on a const;  Change this to a range that filesort
13379
 
        can use.
13380
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
13381
 
        field, quick will contain an empty record set.
 
5360
        We have a ref on a const;  Change this to a range that filesort
 
5361
        can use.
 
5362
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
5363
        field, quick will contain an empty record set.
13382
5364
      */
13383
 
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
13384
 
                                                     tab->found_records))))
13385
 
        goto err;
 
5365
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session,
 
5366
                                                                 table,
 
5367
                                                                 &tab->ref,
 
5368
                                                                 tab->found_records))))
 
5369
      {
 
5370
        return(-1);
 
5371
      }
13386
5372
    }
13387
5373
  }
13388
5374
 
13389
 
  /* Fill schema tables with data before filesort if it's necessary */
13390
 
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
13391
 
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
13392
 
    goto err;
 
5375
  if (table->getShare()->getType())
 
5376
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
13393
5377
 
13394
 
  if (table->s->tmp_table)
13395
 
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
13396
 
  table->sort.found_records=filesort(session, table,join->sortorder, length,
13397
 
                                     select, filesort_limit, 0,
13398
 
                                     &examined_rows);
 
5378
  FileSort filesort(*session);
 
5379
  table->sort.found_records=filesort.run(table,join->sortorder, length,
 
5380
                                         select, filesort_limit, 0,
 
5381
                                         examined_rows);
13399
5382
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
13400
5383
  if (select)
13401
5384
  {
13405
5388
  tab->select_cond=0;
13406
5389
  tab->last_inner= 0;
13407
5390
  tab->first_unmatched= 0;
13408
 
  tab->type=JT_ALL;                             // Read with normal read_record
 
5391
  tab->type= AM_ALL;                            // Read with normal read_record
13409
5392
  tab->read_first_record= join_init_read_record;
13410
5393
  tab->join->examined_rows+=examined_rows;
13411
5394
  if (table->key_read)                          // Restore if we used indexes
13412
5395
  {
13413
5396
    table->key_read=0;
13414
 
    table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5397
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13415
5398
  }
 
5399
 
13416
5400
  return(table->sort.found_records == HA_POS_ERROR);
13417
 
err:
13418
 
  return(-1);
13419
 
}
13420
 
 
13421
 
static bool copy_blobs(Field **ptr)
13422
 
{
13423
 
  for (; *ptr ; ptr++)
13424
 
  {
13425
 
    if ((*ptr)->flags & BLOB_FLAG)
13426
 
      if (((Field_blob *) (*ptr))->copy())
13427
 
        return 1;                               // Error
13428
 
  }
13429
 
  return 0;
13430
 
}
13431
 
 
13432
 
static void free_blobs(Field **ptr)
13433
 
{
13434
 
  for (; *ptr ; ptr++)
13435
 
  {
13436
 
    if ((*ptr)->flags & BLOB_FLAG)
13437
 
      ((Field_blob *) (*ptr))->free();
13438
 
  }
13439
 
}
13440
 
 
13441
 
 
13442
 
static int
13443
 
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
13444
 
{
13445
 
  int error;
13446
 
  uint32_t reclength,offset;
13447
 
  uint32_t field_count;
13448
 
  Session *session= join->session;
13449
 
 
13450
 
  entry->reginfo.lock_type=TL_WRITE;
13451
 
 
13452
 
  /* Calculate how many saved fields there is in list */
13453
 
  field_count=0;
13454
 
  List_iterator<Item> it(fields);
13455
 
  Item *item;
13456
 
  while ((item=it++))
13457
 
  {
13458
 
    if (item->get_tmp_table_field() && ! item->const_item())
13459
 
      field_count++;
13460
 
  }
13461
 
 
13462
 
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
13463
 
  {                    // only const items with no OPTION_FOUND_ROWS
13464
 
    join->unit->select_limit_cnt= 1;            // Only send first row
13465
 
    return(0);
13466
 
  }
13467
 
  Field **first_field=entry->field+entry->s->fields - field_count;
13468
 
  offset= (field_count ?
13469
 
           entry->field[entry->s->fields - field_count]->
13470
 
           offset(entry->record[0]) : 0);
13471
 
  reclength= entry->s->reclength-offset;
13472
 
 
13473
 
  free_io_cache(entry);                         // Safety
13474
 
  entry->file->info(HA_STATUS_VARIABLE);
13475
 
  if (entry->s->db_type() == heap_hton ||
13476
 
      (!entry->s->blob_fields &&
13477
 
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
13478
 
        session->variables.sortbuff_size)))
13479
 
    error= remove_dup_with_hash_index(join->session, entry,
13480
 
                                     field_count, first_field,
13481
 
                                     reclength, having);
13482
 
  else
13483
 
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
13484
 
                                  having);
13485
 
 
13486
 
  free_blobs(first_field);
13487
 
  return(error);
13488
 
}
13489
 
 
13490
 
 
13491
 
static int remove_dup_with_compare(Session *session, Table *table, Field **first_field,
13492
 
                                   uint32_t offset, Item *having)
13493
 
{
13494
 
  handler *file=table->file;
 
5401
}
 
5402
 
 
5403
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
 
5404
{
 
5405
  Cursor *cursor=table->cursor;
13495
5406
  char *org_record,*new_record;
13496
5407
  unsigned char *record;
13497
5408
  int error;
13498
 
  uint32_t reclength= table->s->reclength-offset;
13499
 
 
13500
 
  org_record=(char*) (record=table->record[0])+offset;
13501
 
  new_record=(char*) table->record[1]+offset;
13502
 
 
13503
 
  file->ha_rnd_init(1);
13504
 
  error=file->rnd_next(record);
 
5409
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
 
5410
 
 
5411
  org_record=(char*) (record=table->getInsertRecord())+offset;
 
5412
  new_record=(char*) table->getUpdateRecord()+offset;
 
5413
 
 
5414
  if ((error= cursor->startTableScan(1)))
 
5415
    goto err;
 
5416
 
 
5417
  error=cursor->rnd_next(record);
13505
5418
  for (;;)
13506
5419
  {
13507
 
    if (session->killed)
 
5420
    if (session->getKilled())
13508
5421
    {
13509
5422
      session->send_kill_message();
13510
5423
      error=0;
13513
5426
    if (error)
13514
5427
    {
13515
5428
      if (error == HA_ERR_RECORD_DELETED)
13516
 
        continue;
 
5429
      {
 
5430
        continue;
 
5431
      }
13517
5432
      if (error == HA_ERR_END_OF_FILE)
13518
 
        break;
 
5433
      {
 
5434
        break;
 
5435
      }
13519
5436
      goto err;
13520
5437
    }
13521
5438
    if (having && !having->val_int())
13522
5439
    {
13523
 
      if ((error=file->ha_delete_row(record)))
13524
 
        goto err;
13525
 
      error=file->rnd_next(record);
 
5440
      if ((error=cursor->deleteRecord(record)))
 
5441
      {
 
5442
        goto err;
 
5443
      }
 
5444
      error=cursor->rnd_next(record);
13526
5445
      continue;
13527
5446
    }
13528
 
    if (copy_blobs(first_field))
13529
 
    {
13530
 
      my_message(ER_OUTOFMEMORY, ER(ER_OUTOFMEMORY), MYF(0));
13531
 
      error=0;
13532
 
      goto err;
13533
 
    }
 
5447
    copy_blobs(first_field);
13534
5448
    memcpy(new_record,org_record,reclength);
13535
5449
 
13536
 
    /* Read through rest of file and mark duplicated rows deleted */
 
5450
    /* Read through rest of cursor and mark duplicated rows deleted */
13537
5451
    bool found=0;
13538
5452
    for (;;)
13539
5453
    {
13540
 
      if ((error=file->rnd_next(record)))
 
5454
      if ((error=cursor->rnd_next(record)))
13541
5455
      {
13542
 
        if (error == HA_ERR_RECORD_DELETED)
13543
 
          continue;
13544
 
        if (error == HA_ERR_END_OF_FILE)
13545
 
          break;
13546
 
        goto err;
 
5456
        if (error == HA_ERR_RECORD_DELETED)
 
5457
        {
 
5458
          continue;
 
5459
        }
 
5460
        if (error == HA_ERR_END_OF_FILE)
 
5461
        {
 
5462
          break;
 
5463
        }
 
5464
 
 
5465
        goto err;
13547
5466
      }
13548
5467
      if (table->compare_record(first_field) == 0)
13549
5468
      {
13550
 
        if ((error=file->ha_delete_row(record)))
13551
 
          goto err;
 
5469
        if ((error=cursor->deleteRecord(record)))
 
5470
        {
 
5471
          goto err;
 
5472
        }
13552
5473
      }
13553
5474
      else if (!found)
13554
5475
      {
13555
 
        found=1;
13556
 
        file->position(record); // Remember position
 
5476
        found= 1;
 
5477
        cursor->position(record);       // Remember position
13557
5478
      }
13558
5479
    }
13559
5480
    if (!found)
13560
 
      break;                                    // End of file
13561
 
    /* Restart search on next row */
13562
 
    error=file->restart_rnd_next(record,file->ref);
 
5481
    {
 
5482
      break;                                    // End of cursor
 
5483
    }
 
5484
    /* Move current position to the next row */
 
5485
    error= cursor->rnd_pos(record, cursor->ref);
13563
5486
  }
13564
5487
 
13565
 
  file->extra(HA_EXTRA_NO_CACHE);
13566
 
  return(0);
 
5488
  cursor->extra(HA_EXTRA_NO_CACHE);
 
5489
  return 0;
13567
5490
err:
13568
 
  file->extra(HA_EXTRA_NO_CACHE);
 
5491
  cursor->extra(HA_EXTRA_NO_CACHE);
13569
5492
  if (error)
13570
 
    file->print_error(error,MYF(0));
13571
 
  return(1);
 
5493
  {
 
5494
    table->print_error(error,MYF(0));
 
5495
  }
 
5496
  return 1;
13572
5497
}
13573
5498
 
13574
 
 
13575
5499
/**
13576
5500
  Generate a hash index for each row to quickly find duplicate rows.
13577
5501
 
13578
5502
  @note
13579
5503
    Note that this will not work on tables with blobs!
13580
5504
*/
13581
 
 
13582
 
static int remove_dup_with_hash_index(Session *session, Table *table,
13583
 
                                      uint32_t field_count,
13584
 
                                      Field **first_field,
13585
 
                                      uint32_t key_length,
13586
 
                                      Item *having)
 
5505
int remove_dup_with_hash_index(Session *session,
 
5506
                               Table *table,
 
5507
                               uint32_t field_count,
 
5508
                               Field **first_field,
 
5509
                               uint32_t key_length,
 
5510
                               Item *having)
13587
5511
{
13588
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
5512
  unsigned char *key_pos, *record=table->getInsertRecord();
13589
5513
  int error;
13590
 
  handler *file= table->file;
 
5514
  Cursor &cursor= *table->cursor;
13591
5515
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
13592
 
  uint32_t *field_lengths,*field_length;
 
5516
  uint32_t *field_length;
13593
5517
  HASH hash;
13594
 
 
13595
 
  if (!my_multi_malloc(MYF(MY_WME),
13596
 
                       &key_buffer,
13597
 
                       (uint) ((key_length + extra_length) *
13598
 
                               (long) file->stats.records),
13599
 
                       &field_lengths,
13600
 
                       (uint) (field_count*sizeof(*field_lengths)),
13601
 
                       NULL))
13602
 
    return(1);
 
5518
  std::vector<unsigned char> key_buffer((key_length + extra_length) * (long) cursor.stats.records);
 
5519
  std::vector<uint32_t> field_lengths(field_count);
13603
5520
 
13604
5521
  {
13605
5522
    Field **ptr;
13606
5523
    uint32_t total_length= 0;
13607
 
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
 
5524
 
 
5525
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
13608
5526
    {
13609
5527
      uint32_t length= (*ptr)->sort_length();
13610
5528
      (*field_length++)= length;
13615
5533
    extra_length= ALIGN_SIZE(key_length)-key_length;
13616
5534
  }
13617
5535
 
13618
 
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0,
13619
 
                key_length, (hash_get_key) 0, 0, 0))
 
5536
  hash_init(&hash, &my_charset_bin, (uint32_t) cursor.stats.records, 0, key_length, (hash_get_key) 0, 0, 0);
 
5537
 
 
5538
  if ((error= cursor.startTableScan(1)))
13620
5539
  {
13621
 
    free((char*) key_buffer);
13622
 
    return(1);
 
5540
    goto err;
13623
5541
  }
13624
5542
 
13625
 
  file->ha_rnd_init(1);
13626
 
  key_pos=key_buffer;
 
5543
  key_pos= &key_buffer[0];
13627
5544
  for (;;)
13628
5545
  {
13629
 
    unsigned char *org_key_pos;
13630
 
    if (session->killed)
 
5546
    if (session->getKilled())
13631
5547
    {
13632
5548
      session->send_kill_message();
13633
5549
      error=0;
13634
5550
      goto err;
13635
5551
    }
13636
 
    if ((error=file->rnd_next(record)))
 
5552
    if ((error=cursor.rnd_next(record)))
13637
5553
    {
13638
5554
      if (error == HA_ERR_RECORD_DELETED)
13639
 
        continue;
 
5555
      {
 
5556
        continue;
 
5557
      }
 
5558
 
13640
5559
      if (error == HA_ERR_END_OF_FILE)
13641
 
        break;
 
5560
      {
 
5561
        break;
 
5562
      }
 
5563
 
13642
5564
      goto err;
13643
5565
    }
13644
5566
    if (having && !having->val_int())
13645
5567
    {
13646
 
      if ((error=file->ha_delete_row(record)))
13647
 
        goto err;
 
5568
      if ((error=cursor.deleteRecord(record)))
 
5569
      {
 
5570
        goto err;
 
5571
      }
13648
5572
      continue;
13649
5573
    }
13650
5574
 
13651
5575
    /* copy fields to key buffer */
13652
 
    org_key_pos= key_pos;
13653
 
    field_length=field_lengths;
 
5576
    unsigned char* org_key_pos= key_pos;
 
5577
    field_length= &field_lengths[0];
13654
5578
    for (Field **ptr= first_field ; *ptr ; ptr++)
13655
5579
    {
13656
5580
      (*ptr)->sort_string(key_pos,*field_length);
13660
5584
    if (hash_search(&hash, org_key_pos, key_length))
13661
5585
    {
13662
5586
      /* Duplicated found ; Remove the row */
13663
 
      if ((error=file->ha_delete_row(record)))
13664
 
        goto err;
 
5587
      if ((error=cursor.deleteRecord(record)))
 
5588
        goto err;
13665
5589
    }
13666
5590
    else
 
5591
    {
13667
5592
      (void) my_hash_insert(&hash, org_key_pos);
 
5593
    }
13668
5594
    key_pos+=extra_length;
13669
5595
  }
13670
 
  free((char*) key_buffer);
13671
5596
  hash_free(&hash);
13672
 
  file->extra(HA_EXTRA_NO_CACHE);
13673
 
  (void) file->ha_rnd_end();
13674
 
  return(0);
 
5597
  cursor.extra(HA_EXTRA_NO_CACHE);
 
5598
  (void) cursor.endTableScan();
 
5599
  return 0;
13675
5600
 
13676
5601
err:
13677
 
  free((char*) key_buffer);
13678
5602
  hash_free(&hash);
13679
 
  file->extra(HA_EXTRA_NO_CACHE);
13680
 
  (void) file->ha_rnd_end();
 
5603
  cursor.extra(HA_EXTRA_NO_CACHE);
 
5604
  (void) cursor.endTableScan();
13681
5605
  if (error)
13682
 
    file->print_error(error,MYF(0));
13683
 
  return(1);
 
5606
  {
 
5607
    table->print_error(error,MYF(0));
 
5608
  }
 
5609
 
 
5610
  return 1;
13684
5611
}
13685
5612
 
13686
 
 
13687
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
13688
 
                                  SORT_FIELD *sortorder)
 
5613
SortField* make_unireg_sortorder(Order* order, uint32_t* length, SortField* sortorder)
13689
5614
{
13690
 
  uint32_t count;
13691
 
  SORT_FIELD *sort,*pos;
 
5615
  SortField *sort,*pos;
13692
5616
 
13693
 
  count=0;
13694
 
  for (order_st *tmp = order; tmp; tmp=tmp->next)
 
5617
  uint32_t count= 0;
 
5618
  for (Order *tmp = order; tmp; tmp=tmp->next)
13695
5619
    count++;
13696
 
  if (!sortorder)
13697
 
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
13698
 
                                       (cmax(count, *length) + 1));
 
5620
  if (not sortorder)
 
5621
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) * (max(count, *length) + 1));
13699
5622
  pos= sort= sortorder;
13700
5623
 
13701
 
  if (!pos)
13702
 
    return 0;
13703
 
 
13704
 
  for (;order;order=order->next,pos++)
 
5624
  for (; order; order= order->next,pos++)
13705
5625
  {
13706
5626
    Item *item= order->item[0]->real_item();
13707
5627
    pos->field= 0; pos->item= 0;
 
5628
 
13708
5629
    if (item->type() == Item::FIELD_ITEM)
 
5630
    {
13709
5631
      pos->field= ((Item_field*) item)->field;
 
5632
    }
13710
5633
    else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
 
5634
    {
13711
5635
      pos->field= ((Item_sum*) item)->get_tmp_table_field();
 
5636
    }
13712
5637
    else if (item->type() == Item::COPY_STR_ITEM)
13713
5638
    {                                           // Blob patch
13714
5639
      pos->item= ((Item_copy_string*) item)->item;
13715
5640
    }
13716
5641
    else
 
5642
    {
13717
5643
      pos->item= *order->item;
 
5644
    }
 
5645
 
13718
5646
    pos->reverse=! order->asc;
13719
5647
  }
13720
5648
  *length=count;
13721
 
  return(sort);
13722
 
}
13723
 
 
13724
 
 
13725
 
/*****************************************************************************
13726
 
  Fill join cache with packed records
13727
 
  Records are stored in tab->cache.buffer and last record in
13728
 
  last record is stored with pointers to blobs to support very big
13729
 
  records
13730
 
******************************************************************************/
13731
 
 
13732
 
static int
13733
 
join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count)
13734
 
{
13735
 
  register unsigned int i;
13736
 
  unsigned int length, blobs;
13737
 
  size_t size;
13738
 
  CACHE_FIELD *copy,**blob_ptr;
13739
 
  JOIN_CACHE  *cache;
13740
 
  JOIN_TAB *join_tab;
13741
 
 
13742
 
  cache= &tables[table_count].cache;
13743
 
  cache->fields=blobs=0;
13744
 
 
13745
 
  join_tab=tables;
13746
 
  for (i=0 ; i < table_count ; i++,join_tab++)
13747
 
  {
13748
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
13749
 
      calc_used_field_length(session, join_tab);
13750
 
    cache->fields+=join_tab->used_fields;
13751
 
    blobs+=join_tab->used_blobs;
13752
 
 
13753
 
    /* SemiJoinDuplicateElimination: reserve space for rowid */
13754
 
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
13755
 
    {
13756
 
      cache->fields++;
13757
 
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
13758
 
    }
13759
 
  }
13760
 
  if (!(cache->field=(CACHE_FIELD*)
13761
 
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
13762
 
 
13763
 
                  sizeof(CACHE_FIELD*))))
13764
 
  {
13765
 
    free((unsigned char*) cache->buff);         /* purecov: inspected */
13766
 
    cache->buff=0;                              /* purecov: inspected */
13767
 
    return(1);                          /* purecov: inspected */
13768
 
  }
13769
 
  copy=cache->field;
13770
 
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
13771
 
    (cache->field+cache->fields+table_count*2);
13772
 
 
13773
 
  length=0;
13774
 
  for (i=0 ; i < table_count ; i++)
13775
 
  {
13776
 
    uint32_t null_fields=0, used_fields;
13777
 
    Field **f_ptr,*field;
13778
 
    MY_BITMAP *read_set= tables[i].table->read_set;
13779
 
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13780
 
         used_fields ;
13781
 
         f_ptr++)
13782
 
    {
13783
 
      field= *f_ptr;
13784
 
      if (bitmap_is_set(read_set, field->field_index))
13785
 
      {
13786
 
        used_fields--;
13787
 
        length+=field->fill_cache_field(copy);
13788
 
        if (copy->blob_field)
13789
 
          (*blob_ptr++)=copy;
13790
 
        if (field->maybe_null())
13791
 
          null_fields++;
13792
 
        copy->get_rowid= NULL;
13793
 
        copy++;
13794
 
      }
13795
 
    }
13796
 
    /* Copy null bits from table */
13797
 
    if (null_fields && tables[i].table->getNullFields())
13798
 
    {                                           /* must copy null bits */
13799
 
      copy->str= tables[i].table->null_flags;
13800
 
      copy->length= tables[i].table->s->null_bytes;
13801
 
      copy->strip=0;
13802
 
      copy->blob_field=0;
13803
 
      copy->get_rowid= NULL;
13804
 
      length+=copy->length;
13805
 
      copy++;
13806
 
      cache->fields++;
13807
 
    }
13808
 
    /* If outer join table, copy null_row flag */
13809
 
    if (tables[i].table->maybe_null)
13810
 
    {
13811
 
      copy->str= (unsigned char*) &tables[i].table->null_row;
13812
 
      copy->length=sizeof(tables[i].table->null_row);
13813
 
      copy->strip=0;
13814
 
      copy->blob_field=0;
13815
 
      copy->get_rowid= NULL;
13816
 
      length+=copy->length;
13817
 
      copy++;
13818
 
      cache->fields++;
13819
 
    }
13820
 
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
13821
 
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
13822
 
    {
13823
 
      copy->str= tables[i].table->file->ref;
13824
 
      copy->length= tables[i].table->file->ref_length;
13825
 
      copy->strip=0;
13826
 
      copy->blob_field=0;
13827
 
      copy->get_rowid= NULL;
13828
 
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
13829
 
      {
13830
 
        /* We will need to call h->position(): */
13831
 
        copy->get_rowid= tables[i].table;
13832
 
        /* And those after us won't have to: */
13833
 
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
13834
 
      }
13835
 
      copy++;
13836
 
    }
13837
 
  }
13838
 
 
13839
 
  cache->length=length+blobs*sizeof(char*);
13840
 
  cache->blobs=blobs;
13841
 
  *blob_ptr= NULL;                                      /* End sequentel */
13842
 
  size= cmax(session->variables.join_buff_size, (uint32_t)cache->length);
13843
 
  if (!(cache->buff=(unsigned char*) malloc(size)))
13844
 
    return 1;                           /* Don't use cache */ /* purecov: inspected */
13845
 
  cache->end=cache->buff+size;
13846
 
  reset_cache_write(cache);
13847
 
  return 0;
13848
 
}
13849
 
 
13850
 
 
13851
 
static uint32_t used_blob_length(CACHE_FIELD **ptr)
13852
 
{
13853
 
  uint32_t length,blob_length;
13854
 
  for (length=0 ; *ptr ; ptr++)
13855
 
  {
13856
 
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
13857
 
    length+=blob_length;
13858
 
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
13859
 
  }
13860
 
  return length;
13861
 
}
13862
 
 
13863
 
 
13864
 
static bool
13865
 
store_record_in_cache(JOIN_CACHE *cache)
13866
 
{
13867
 
  uint32_t length;
13868
 
  unsigned char *pos;
13869
 
  CACHE_FIELD *copy,*end_field;
13870
 
  bool last_record;
13871
 
 
13872
 
  pos=cache->pos;
13873
 
  end_field=cache->field+cache->fields;
13874
 
 
13875
 
  length=cache->length;
13876
 
  if (cache->blobs)
13877
 
    length+= used_blob_length(cache->blob_ptr);
13878
 
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
13879
 
    cache->ptr_record=cache->records;
13880
 
  /*
13881
 
    There is room in cache. Put record there
13882
 
  */
13883
 
  cache->records++;
13884
 
  for (copy=cache->field ; copy < end_field; copy++)
13885
 
  {
13886
 
    if (copy->blob_field)
13887
 
    {
13888
 
      if (last_record)
13889
 
      {
13890
 
        copy->blob_field->get_image(pos, copy->length+sizeof(char*),
13891
 
                                    copy->blob_field->charset());
13892
 
        pos+=copy->length+sizeof(char*);
13893
 
      }
13894
 
      else
13895
 
      {
13896
 
        copy->blob_field->get_image(pos, copy->length, // blob length
13897
 
                                    copy->blob_field->charset());
13898
 
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
13899
 
        pos+=copy->length+copy->blob_length;
13900
 
      }
13901
 
    }
13902
 
    else
13903
 
    {
13904
 
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
13905
 
      if (copy->get_rowid)
13906
 
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
13907
 
 
13908
 
      if (copy->strip)
13909
 
      {
13910
 
        unsigned char *str,*end;
13911
 
        for (str=copy->str,end= str+copy->length;
13912
 
             end > str && end[-1] == ' ' ;
13913
 
             end--) ;
13914
 
        length=(uint) (end-str);
13915
 
        memcpy(pos+2, str, length);
13916
 
        int2store(pos, length);
13917
 
        pos+= length+2;
13918
 
      }
13919
 
      else
13920
 
      {
13921
 
        memcpy(pos,copy->str,copy->length);
13922
 
        pos+=copy->length;
13923
 
      }
13924
 
    }
13925
 
  }
13926
 
  cache->pos=pos;
13927
 
  return last_record || (size_t) (cache->end - pos) < cache->length;
13928
 
}
13929
 
 
13930
 
 
13931
 
static void
13932
 
reset_cache_read(JOIN_CACHE *cache)
13933
 
{
13934
 
  cache->record_nr=0;
13935
 
  cache->pos=cache->buff;
13936
 
}
13937
 
 
13938
 
 
13939
 
static void reset_cache_write(JOIN_CACHE *cache)
13940
 
{
13941
 
  reset_cache_read(cache);
13942
 
  cache->records= 0;
13943
 
  cache->ptr_record= UINT32_MAX;
13944
 
}
13945
 
 
13946
 
 
13947
 
static void
13948
 
read_cached_record(JOIN_TAB *tab)
13949
 
{
13950
 
  unsigned char *pos;
13951
 
  uint32_t length;
13952
 
  bool last_record;
13953
 
  CACHE_FIELD *copy,*end_field;
13954
 
 
13955
 
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
13956
 
  pos=tab->cache.pos;
13957
 
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
13958
 
       copy < end_field;
13959
 
       copy++)
13960
 
  {
13961
 
    if (copy->blob_field)
13962
 
    {
13963
 
      if (last_record)
13964
 
      {
13965
 
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
13966
 
                                    copy->blob_field->charset());
13967
 
        pos+=copy->length+sizeof(char*);
13968
 
      }
13969
 
      else
13970
 
      {
13971
 
        copy->blob_field->set_ptr(pos, pos+copy->length);
13972
 
        pos+=copy->length+copy->blob_field->get_length();
13973
 
      }
13974
 
    }
13975
 
    else
13976
 
    {
13977
 
      if (copy->strip)
13978
 
      {
13979
 
        length= uint2korr(pos);
13980
 
        memcpy(copy->str, pos+2, length);
13981
 
        memset(copy->str+length, ' ', copy->length-length);
13982
 
        pos+= 2 + length;
13983
 
      }
13984
 
      else
13985
 
      {
13986
 
        memcpy(copy->str,pos,copy->length);
13987
 
        pos+=copy->length;
13988
 
      }
13989
 
    }
13990
 
  }
13991
 
  tab->cache.pos=pos;
13992
 
  return;
13993
 
}
13994
 
 
 
5649
  return sort;
 
5650
}
13995
5651
 
13996
5652
/*
13997
5653
  eq_ref: Create the lookup key and check if it is the same as saved key
14011
5667
    false  The created key is the same as the previous one (and the record
14012
5668
           is already in table->record)
14013
5669
*/
14014
 
 
14015
 
static bool
14016
 
cmp_buffer_with_ref(JOIN_TAB *tab)
 
5670
static bool cmp_buffer_with_ref(JoinTable *tab)
14017
5671
{
14018
5672
  bool no_prev_key;
14019
5673
  if (!tab->ref.disable_cache)
14025
5679
    }
14026
5680
  }
14027
5681
  else
 
5682
  {
14028
5683
    no_prev_key= true;
14029
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
14030
 
      no_prev_key)
 
5684
  }
 
5685
 
 
5686
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) || no_prev_key)
 
5687
  {
14031
5688
    return 1;
14032
 
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
14033
 
    != 0;
 
5689
  }
 
5690
  return (memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length) != 0);
14034
5691
}
14035
5692
 
14036
 
 
14037
 
bool
14038
 
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
 
5693
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
14039
5694
{
14040
5695
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
14041
5696
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
14042
5697
  bool result= 0;
14043
5698
 
14044
 
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
 
5699
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
14045
5700
  {
14046
5701
    if ((*copy)->copy() & 1)
14047
5702
    {
14053
5708
  return result;
14054
5709
}
14055
5710
 
14056
 
 
14057
5711
/*****************************************************************************
14058
5712
  Group and order functions
14059
5713
*****************************************************************************/
14060
5714
 
14061
5715
/**
14062
 
  Resolve an order_st BY or GROUP BY column reference.
 
5716
  Resolve an ORDER BY or GROUP BY column reference.
14063
5717
 
14064
5718
  Given a column reference (represented by 'order') from a GROUP BY or order_st
14065
5719
  BY clause, find the actual column it represents. If the column being
14066
5720
  resolved is from the GROUP BY clause, the procedure searches the SELECT
14067
5721
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
14068
 
  the order_st BY clause, only the SELECT list is being searched.
 
5722
  the ORDER BY clause, only the SELECT list is being searched.
14069
5723
 
14070
5724
  If 'order' is resolved to an Item, then order->item is set to the found
14071
5725
  Item. If there is no item for the found column (that is, it was resolved
14090
5744
  @retval
14091
5745
    true  if error occurred
14092
5746
*/
14093
 
 
14094
 
static bool
14095
 
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
14096
 
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
14097
 
                   bool is_group_field)
 
5747
static bool find_order_in_list(Session *session,
 
5748
                               Item **ref_pointer_array,
 
5749
                               TableList *tables,
 
5750
                               Order *order,
 
5751
                               List<Item> &fields,
 
5752
                               List<Item> &all_fields,
 
5753
                               bool is_group_field)
14098
5754
{
14099
5755
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
14100
5756
  Item::Type order_item_type;
14109
5765
  */
14110
5766
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
14111
5767
  {                                             /* Order by position */
14112
 
    uint32_t count= (uint) order_item->val_int();
14113
 
    if (!count || count > fields.elements)
 
5768
    uint32_t count= (uint32_t) order_item->val_int();
 
5769
    if (!count || count > fields.size())
14114
5770
    {
14115
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
14116
 
               order_item->full_name(), session->where);
 
5771
      my_error(ER_BAD_FIELD_ERROR, MYF(0), order_item->full_name(), session->where());
14117
5772
      return true;
14118
5773
    }
14119
5774
    order->item= ref_pointer_array + count - 1;
14123
5778
    return false;
14124
5779
  }
14125
5780
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
14126
 
  select_item= find_item_in_list(order_item, fields, &counter,
14127
 
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
 
5781
  select_item= find_item_in_list(session, order_item, fields, &counter, REPORT_EXCEPT_NOT_FOUND, &resolution);
14128
5782
  if (!select_item)
 
5783
  {
14129
5784
    return true; /* The item is not unique, or some other error occured. */
 
5785
  }
14130
5786
 
14131
5787
 
14132
5788
  /* Check whether the resolved field is not ambiguos. */
14138
5794
      original field name, we should additionaly check if we have conflict
14139
5795
      for this name (in case if we would perform lookup in all tables).
14140
5796
    */
14141
 
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
14142
 
        order_item->fix_fields(session, order->item))
 
5797
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed && order_item->fix_fields(session, order->item))
 
5798
    {
14143
5799
      return true;
 
5800
    }
14144
5801
 
14145
5802
    /* Lookup the current GROUP field in the FROM clause. */
14146
5803
    order_item_type= order_item->type();
14149
5806
        order_item_type == Item::REF_ITEM)
14150
5807
    {
14151
5808
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
14152
 
                                       NULL, &view_ref, IGNORE_ERRORS, true,
14153
 
                                       false);
 
5809
                                       NULL, &view_ref, IGNORE_ERRORS, false);
14154
5810
      if (!from_field)
 
5811
      {
14155
5812
        from_field= (Field*) not_found_field;
 
5813
      }
14156
5814
    }
14157
5815
 
14158
5816
    if (from_field == not_found_field ||
14191
5849
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
14192
5850
                          ER(ER_NON_UNIQ_ERROR),
14193
5851
                          ((Item_ident*) order_item)->field_name,
14194
 
                          current_session->where);
 
5852
                          session->where());
14195
5853
    }
14196
5854
  }
14197
5855
 
14211
5869
      (order_item->fix_fields(session, order->item) ||
14212
5870
       (order_item= *order->item)->check_cols(1) ||
14213
5871
       session->is_fatal_error))
 
5872
  {
14214
5873
    return true; /* Wrong field. */
 
5874
  }
14215
5875
 
14216
 
  uint32_t el= all_fields.elements;
 
5876
  uint32_t el= all_fields.size();
14217
5877
  all_fields.push_front(order_item); /* Add new field to field list. */
14218
5878
  ref_pointer_array[el]= order_item;
14219
5879
  order->item= ref_pointer_array + el;
14220
5880
  return false;
14221
5881
}
14222
5882
 
14223
 
 
14224
5883
/**
14225
5884
  Change order to point at item in select list.
14226
5885
 
14227
5886
  If item isn't a number and doesn't exits in the select list, add it the
14228
5887
  the field list.
14229
5888
*/
14230
 
 
14231
 
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
14232
 
                List<Item> &fields, List<Item> &all_fields, order_st *order)
 
5889
int setup_order(Session *session,
 
5890
                Item **ref_pointer_array,
 
5891
                TableList *tables,
 
5892
                            List<Item> &fields,
 
5893
                List<Item> &all_fields,
 
5894
                Order *order)
14233
5895
{
14234
 
  session->where="order clause";
14235
 
  for (; order; order=order->next)
 
5896
  session->setWhere("order clause");
 
5897
  for (; order; order= order->next)
14236
5898
  {
14237
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
14238
 
                           all_fields, false))
 
5899
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields, all_fields, false))
 
5900
    {
14239
5901
      return 1;
 
5902
    }
14240
5903
  }
14241
5904
  return 0;
14242
5905
}
14243
5906
 
14244
 
 
14245
5907
/**
14246
5908
  Intitialize the GROUP BY list.
14247
5909
 
14248
 
  @param session                        Thread handler
 
5910
  @param session                        Thread Cursor
14249
5911
  @param ref_pointer_array      We store references to all fields that was
14250
5912
                               not in 'fields' here.
14251
5913
  @param fields         All fields in the select part. Any item in
14267
5929
  @retval
14268
5930
    1  error (probably out of memory)
14269
5931
*/
14270
 
 
14271
 
int
14272
 
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
14273
 
            List<Item> &fields, List<Item> &all_fields, order_st *order,
14274
 
            bool *hidden_group_fields)
 
5932
int setup_group(Session *session,
 
5933
                Item **ref_pointer_array,
 
5934
                TableList *tables,
 
5935
                      List<Item> &fields,
 
5936
                List<Item> &all_fields,
 
5937
                Order *order,
 
5938
                      bool *hidden_group_fields)
14275
5939
{
14276
5940
  *hidden_group_fields=0;
14277
 
  order_st *ord;
14278
5941
 
14279
 
  if (!order)
 
5942
  if (order == NULL)
 
5943
  {
14280
5944
    return 0;                           /* Everything is ok */
14281
 
 
14282
 
  uint32_t org_fields=all_fields.elements;
14283
 
 
14284
 
  session->where="group statement";
14285
 
  for (ord= order; ord; ord= ord->next)
 
5945
  }
 
5946
 
 
5947
  uint32_t org_fields=all_fields.size();
 
5948
 
 
5949
  session->setWhere("group statement");
 
5950
  for (Order *ord= order; ord; ord= ord->next)
14286
5951
  {
14287
5952
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
14288
5953
                           all_fields, true))
 
5954
    {
14289
5955
      return 1;
 
5956
    }
14290
5957
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
14291
5958
    if ((*ord->item)->with_sum_func)
14292
5959
    {
14294
5961
      return 1;
14295
5962
    }
14296
5963
  }
 
5964
 
14297
5965
  /* MODE_ONLY_FULL_GROUP_BY */
14298
5966
  {
14299
5967
    /*
14314
5982
    Item *item;
14315
5983
    Item_field *field;
14316
5984
    int cur_pos_in_select_list= 0;
14317
 
    List_iterator<Item> li(fields);
14318
 
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
 
5985
    List<Item>::iterator li(fields.begin());
 
5986
    List<Item_field>::iterator naf_it(session->lex().current_select->non_agg_fields.begin());
14319
5987
 
14320
5988
    field= naf_it++;
14321
5989
    while (field && (item=li++))
14329
5997
        {
14330
5998
          /* Skip fields from previous expressions. */
14331
5999
          if (field->marker < cur_pos_in_select_list)
 
6000
          {
14332
6001
            goto next_field;
 
6002
          }
 
6003
 
14333
6004
          /* Found a field from the next expression. */
14334
6005
          if (field->marker > cur_pos_in_select_list)
 
6006
          {
14335
6007
            break;
 
6008
          }
 
6009
 
14336
6010
          /*
14337
6011
            Check whether the field occur in the GROUP BY list.
14338
6012
            Throw the error later if the field isn't found.
14339
6013
          */
14340
 
          for (ord= order; ord; ord= ord->next)
 
6014
          for (Order *ord= order; ord; ord= ord->next)
 
6015
          {
14341
6016
            if ((*ord->item)->eq((Item*)field, 0))
 
6017
            {
14342
6018
              goto next_field;
 
6019
            }
 
6020
          }
 
6021
 
14343
6022
          /*
14344
 
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
14345
 
            ER_NON_GROUPING_FIELD_USED
 
6023
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
14346
6024
          */
14347
6025
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
14348
6026
          return 1;
14353
6031
      cur_pos_in_select_list++;
14354
6032
    }
14355
6033
  }
14356
 
  if (org_fields != all_fields.elements)
 
6034
 
 
6035
  if (org_fields != all_fields.size())
 
6036
  {
14357
6037
    *hidden_group_fields=1;                     // group fields is not used
 
6038
  }
 
6039
 
14358
6040
  return 0;
14359
6041
}
14360
6042
 
14364
6046
  Try to use the fields in the order given by 'order' to allow one to
14365
6047
  optimize away 'order by'.
14366
6048
*/
14367
 
 
14368
 
static order_st *
14369
 
create_distinct_group(Session *session, Item **ref_pointer_array,
14370
 
                      order_st *order_list, List<Item> &fields,
14371
 
                      List<Item> &, bool *all_order_by_fields_used)
 
6049
Order *create_distinct_group(Session *session,
 
6050
                                Item **ref_pointer_array,
 
6051
                                Order *order_list,
 
6052
                                List<Item> &fields,
 
6053
                                List<Item> &,
 
6054
                                bool *all_order_by_fields_used)
14372
6055
{
14373
 
  List_iterator<Item> li(fields);
14374
 
  Item *item;
14375
 
  order_st *order,*group,**prev;
 
6056
  List<Item>::iterator li(fields.begin());
 
6057
  Order *order,*group,**prev;
14376
6058
 
14377
6059
  *all_order_by_fields_used= 1;
14378
 
  while ((item=li++))
 
6060
  while (Item* item=li++)
 
6061
  {
14379
6062
    item->marker=0;                     /* Marker that field is not used */
 
6063
  }
14380
6064
 
14381
6065
  prev= &group;  group=0;
14382
6066
  for (order=order_list ; order; order=order->next)
14383
6067
  {
14384
6068
    if (order->in_field_list)
14385
6069
    {
14386
 
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
14387
 
      if (!ord)
14388
 
        return 0;
 
6070
      Order *ord=(Order*) session->mem.memdup(order,sizeof(Order));
14389
6071
      *prev=ord;
14390
6072
      prev= &ord->next;
14391
6073
      (*ord->item)->marker=1;
14392
6074
    }
14393
6075
    else
 
6076
    {
14394
6077
      *all_order_by_fields_used= 0;
 
6078
    }
14395
6079
  }
14396
6080
 
14397
 
  li.rewind();
14398
 
  while ((item=li++))
 
6081
  li= fields.begin();
 
6082
  while (Item* item=li++)
14399
6083
  {
14400
6084
    if (!item->const_item() && !item->with_sum_func && !item->marker)
14401
6085
    {
14403
6087
        Don't put duplicate columns from the SELECT list into the
14404
6088
        GROUP BY list.
14405
6089
      */
14406
 
      order_st *ord_iter;
 
6090
      Order *ord_iter;
14407
6091
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
 
6092
      {
14408
6093
        if ((*ord_iter->item)->eq(item, 1))
 
6094
        {
14409
6095
          goto next_item;
 
6096
        }
 
6097
      }
14410
6098
 
14411
 
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
14412
 
      if (!ord)
14413
 
        return 0;
 
6099
      Order *ord=(Order*) session->mem.calloc(sizeof(Order));
14414
6100
 
14415
6101
      /*
14416
6102
        We have here only field_list (not all_field_list), so we can use
14429
6115
  return group;
14430
6116
}
14431
6117
 
14432
 
 
14433
6118
/**
14434
6119
  Update join with count of the different type of fields.
14435
6120
*/
14436
 
 
14437
 
void
14438
 
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
14439
 
                  List<Item> &fields, bool reset_with_sum_func)
 
6121
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
14440
6122
{
14441
 
  List_iterator<Item> li(fields);
 
6123
  List<Item>::iterator li(fields.begin());
14442
6124
  Item *field;
14443
6125
 
14444
 
  param->field_count=param->sum_func_count=param->func_count=
14445
 
    param->hidden_field_count=0;
 
6126
  param->field_count=param->sum_func_count=param->func_count= param->hidden_field_count=0;
14446
6127
  param->quick_group=1;
 
6128
 
14447
6129
  while ((field=li++))
14448
6130
  {
14449
6131
    Item::Type real_type= field->real_item()->type();
14450
6132
    if (real_type == Item::FIELD_ITEM)
 
6133
    {
14451
6134
      param->field_count++;
 
6135
    }
14452
6136
    else if (real_type == Item::SUM_FUNC_ITEM)
14453
6137
    {
14454
6138
      if (! field->const_item())
14455
6139
      {
14456
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
6140
        Item_sum *sum_item=(Item_sum*) field->real_item();
14457
6141
        if (!sum_item->depended_from() ||
14458
6142
            sum_item->depended_from() == select_lex)
14459
6143
        {
14460
6144
          if (!sum_item->quick_group)
 
6145
          {
14461
6146
            param->quick_group=0;                       // UDF SUM function
 
6147
          }
14462
6148
          param->sum_func_count++;
14463
6149
 
14464
6150
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
14476
6162
    {
14477
6163
      param->func_count++;
14478
6164
      if (reset_with_sum_func)
14479
 
        field->with_sum_func=0;
14480
 
    }
14481
 
  }
14482
 
}
14483
 
 
14484
 
 
14485
 
/**
14486
 
  Return 1 if second is a subpart of first argument.
14487
 
 
14488
 
  If first parts has different direction, change it to second part
14489
 
  (group is sorted like order)
14490
 
*/
14491
 
 
14492
 
static bool
14493
 
test_if_subpart(order_st *a,order_st *b)
14494
 
{
14495
 
  for (; a && b; a=a->next,b=b->next)
14496
 
  {
14497
 
    if ((*a->item)->eq(*b->item,1))
14498
 
      a->asc=b->asc;
14499
 
    else
14500
 
      return 0;
14501
 
  }
14502
 
  return test(!b);
14503
 
}
14504
 
 
14505
 
/**
14506
 
  Return table number if there is only one table in sort order
14507
 
  and group and order is compatible, else return 0.
14508
 
*/
14509
 
 
14510
 
static Table *
14511
 
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
14512
 
{
14513
 
  table_map map= (table_map) 0;
14514
 
 
14515
 
  if (!a)
14516
 
    a=b;                                        // Only one need to be given
14517
 
  else if (!b)
14518
 
    b=a;
14519
 
 
14520
 
  for (; a && b; a=a->next,b=b->next)
14521
 
  {
14522
 
    if (!(*a->item)->eq(*b->item,1))
14523
 
      return(0);
14524
 
    map|=a->item[0]->used_tables();
14525
 
  }
14526
 
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
14527
 
    return(0);
14528
 
 
14529
 
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
14530
 
  if (map != tables->table->map)
14531
 
    return(0);                          // More than one table
14532
 
  return(tables->table);
14533
 
}
14534
 
 
14535
 
 
14536
 
/**
14537
 
  calc how big buffer we need for comparing group entries.
14538
 
*/
14539
 
 
14540
 
static void
14541
 
calc_group_buffer(JOIN *join,order_st *group)
14542
 
{
14543
 
  uint32_t key_length=0, parts=0, null_parts=0;
14544
 
 
14545
 
  if (group)
14546
 
    join->group= 1;
14547
 
  for (; group ; group=group->next)
14548
 
  {
14549
 
    Item *group_item= *group->item;
14550
 
    Field *field= group_item->get_tmp_table_field();
14551
 
    if (field)
14552
 
    {
14553
 
      enum_field_types type;
14554
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
14555
 
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
14556
 
      else if (type == DRIZZLE_TYPE_VARCHAR)
14557
 
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
14558
 
      else
14559
 
        key_length+= field->pack_length();
14560
 
    }
14561
 
    else
14562
 
    {
14563
 
      switch (group_item->result_type()) {
14564
 
      case REAL_RESULT:
14565
 
        key_length+= sizeof(double);
14566
 
        break;
14567
 
      case INT_RESULT:
14568
 
        key_length+= sizeof(int64_t);
14569
 
        break;
14570
 
      case DECIMAL_RESULT:
14571
 
        key_length+= my_decimal_get_binary_size(group_item->max_length -
14572
 
                                                (group_item->decimals ? 1 : 0),
14573
 
                                                group_item->decimals);
14574
 
        break;
14575
 
      case STRING_RESULT:
14576
6165
      {
14577
 
        enum enum_field_types type= group_item->field_type();
14578
 
        /*
14579
 
          As items represented as DATE/TIME fields in the group buffer
14580
 
          have STRING_RESULT result type, we increase the length
14581
 
          by 8 as maximum pack length of such fields.
14582
 
        */
14583
 
        if (type == DRIZZLE_TYPE_TIME ||
14584
 
            type == DRIZZLE_TYPE_DATE ||
14585
 
            type == DRIZZLE_TYPE_DATETIME ||
14586
 
            type == DRIZZLE_TYPE_TIMESTAMP)
14587
 
        {
14588
 
          key_length+= 8;
14589
 
        }
14590
 
        else
14591
 
        {
14592
 
          /*
14593
 
            Group strings are taken as varstrings and require an length field.
14594
 
            A field is not yet created by create_tmp_field()
14595
 
            and the sizes should match up.
14596
 
          */
14597
 
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
14598
 
        }
14599
 
        break;
14600
 
      }
14601
 
      default:
14602
 
        /* This case should never be choosen */
14603
 
        assert(0);
14604
 
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
14605
 
      }
14606
 
    }
14607
 
    parts++;
14608
 
    if (group_item->maybe_null)
14609
 
      null_parts++;
14610
 
  }
14611
 
  join->tmp_table_param.group_length=key_length+null_parts;
14612
 
  join->tmp_table_param.group_parts=parts;
14613
 
  join->tmp_table_param.group_null_parts=null_parts;
14614
 
}
14615
 
 
14616
 
 
14617
 
/**
14618
 
  allocate group fields or take prepared (cached).
14619
 
 
14620
 
  @param main_join   join of current select
14621
 
  @param curr_join   current join (join of current select or temporary copy
14622
 
                     of it)
14623
 
 
14624
 
  @retval
14625
 
    0   ok
14626
 
  @retval
14627
 
    1   failed
14628
 
*/
14629
 
 
14630
 
static bool
14631
 
make_group_fields(JOIN *main_join, JOIN *curr_join)
14632
 
{
14633
 
  if (main_join->group_fields_cache.elements)
14634
 
  {
14635
 
    curr_join->group_fields= main_join->group_fields_cache;
14636
 
    curr_join->sort_and_group= 1;
14637
 
  }
14638
 
  else
14639
 
  {
14640
 
    if (alloc_group_fields(curr_join, curr_join->group_list))
14641
 
      return (1);
14642
 
    main_join->group_fields_cache= curr_join->group_fields;
14643
 
  }
14644
 
  return (0);
14645
 
}
14646
 
 
14647
 
 
14648
 
/**
14649
 
  Get a list of buffers for saveing last group.
14650
 
 
14651
 
  Groups are saved in reverse order for easyer check loop.
14652
 
*/
14653
 
 
14654
 
static bool
14655
 
alloc_group_fields(JOIN *join,order_st *group)
14656
 
{
14657
 
  if (group)
14658
 
  {
14659
 
    for (; group ; group=group->next)
14660
 
    {
14661
 
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
14662
 
      if (!tmp || join->group_fields.push_front(tmp))
14663
 
        return true;
14664
 
    }
14665
 
  }
14666
 
  join->sort_and_group=1;                       /* Mark for do_select */
14667
 
  return false;
14668
 
}
14669
 
 
 
6166
        field->with_sum_func= 0;
 
6167
      }
 
6168
    }
 
6169
  }
 
6170
}
14670
6171
 
14671
6172
/*
14672
6173
  Test if a single-row cache of items changed, and update the cache.
14680
6181
  @return -1 if no item changed
14681
6182
  @return index of the first item that changed
14682
6183
*/
14683
 
 
14684
6184
int test_if_item_cache_changed(List<Cached_item> &list)
14685
6185
{
14686
 
  List_iterator<Cached_item> li(list);
14687
 
  int idx= -1,i;
 
6186
  List<Cached_item>::iterator li(list.begin());
 
6187
  int idx= -1;
14688
6188
  Cached_item *buff;
14689
6189
 
14690
 
  for (i=(int) list.elements-1 ; (buff=li++) ; i--)
 
6190
  for (int i=(int) list.size()-1 ; (buff=li++) ; i--)
14691
6191
  {
14692
6192
    if (buff->cmp())
14693
 
      idx=i;
 
6193
      idx= i;
14694
6194
  }
14695
 
  return(idx);
 
6195
  return idx;
14696
6196
}
14697
6197
 
14698
 
 
14699
6198
/**
14700
6199
  Setup copy_fields to save fields at start of new group.
14701
6200
 
14724
6223
  @retval
14725
6224
    !=0   error
14726
6225
*/
14727
 
 
14728
 
bool
14729
 
setup_copy_fields(Session *session, TMP_TABLE_PARAM *param,
14730
 
                  Item **ref_pointer_array,
14731
 
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
14732
 
                  uint32_t elements, List<Item> &all_fields)
 
6226
bool setup_copy_fields(Session *session,
 
6227
                       Tmp_Table_Param *param,
 
6228
                       Item **ref_pointer_array,
 
6229
                       List<Item> &res_selected_fields,
 
6230
                       List<Item> &res_all_fields,
 
6231
                       uint32_t elements,
 
6232
                       List<Item> &all_fields)
14733
6233
{
14734
6234
  Item *pos;
14735
 
  List_iterator_fast<Item> li(all_fields);
14736
 
  Copy_field *copy= NULL;
14737
 
  res_selected_fields.empty();
14738
 
  res_all_fields.empty();
14739
 
  List_iterator_fast<Item> itr(res_all_fields);
 
6235
  List<Item>::iterator li(all_fields.begin());
 
6236
  CopyField *copy= NULL;
 
6237
  res_selected_fields.clear();
 
6238
  res_all_fields.clear();
 
6239
  List<Item>::iterator itr(res_all_fields.begin());
14740
6240
  List<Item> extra_funcs;
14741
 
  uint32_t i, border= all_fields.elements - elements;
 
6241
  uint32_t border= all_fields.size() - elements;
14742
6242
 
14743
6243
  if (param->field_count &&
14744
 
      !(copy=param->copy_field= new Copy_field[param->field_count]))
14745
 
    goto err2;
 
6244
      !(copy= param->copy_field= new CopyField[param->field_count]))
 
6245
  {
 
6246
    return true;
 
6247
  }
14746
6248
 
14747
 
  param->copy_funcs.empty();
14748
 
  for (i= 0; (pos= li++); i++)
 
6249
  param->copy_funcs.clear();
 
6250
  for (uint32_t i= 0; (pos= li++); i++)
14749
6251
  {
14750
6252
    Field *field;
14751
6253
    unsigned char *tmp;
14752
6254
    Item *real_pos= pos->real_item();
14753
6255
    if (real_pos->type() == Item::FIELD_ITEM)
14754
6256
    {
14755
 
      Item_field *item;
14756
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
14757
 
        goto err;
 
6257
      Item_field* item= new Item_field(session, ((Item_field*) real_pos));
14758
6258
      if (pos->type() == Item::REF_ITEM)
14759
6259
      {
14760
6260
        /* preserve the names of the ref when dereferncing */
14764
6264
        item->name= ref->name;
14765
6265
      }
14766
6266
      pos= item;
 
6267
 
14767
6268
      if (item->field->flags & BLOB_FLAG)
14768
6269
      {
14769
 
        if (!(pos= new Item_copy_string(pos)))
14770
 
          goto err;
14771
 
       /*
14772
 
         Item_copy_string::copy for function can call
14773
 
         Item_copy_string::val_int for blob via Item_ref.
14774
 
         But if Item_copy_string::copy for blob isn't called before,
14775
 
         it's value will be wrong
14776
 
         so let's insert Item_copy_string for blobs in the beginning of
14777
 
         copy_funcs
14778
 
         (to see full test case look at having.test, BUG #4358)
14779
 
       */
14780
 
        if (param->copy_funcs.push_front(pos))
14781
 
          goto err;
 
6270
        pos= new Item_copy_string(pos);
 
6271
            /*
 
6272
              Item_copy_string::copy for function can call
 
6273
              Item_copy_string::val_int for blob via Item_ref.
 
6274
              But if Item_copy_string::copy for blob isn't called before,
 
6275
              it's value will be wrong
 
6276
              so let's insert Item_copy_string for blobs in the beginning of
 
6277
              copy_funcs
 
6278
              (to see full test case look at having.test, BUG #4358)
 
6279
            */
 
6280
        param->copy_funcs.push_front(pos);
14782
6281
      }
14783
6282
      else
14784
6283
      {
14785
 
        /*
14786
 
           set up save buffer and change result_field to point at
14787
 
           saved value
14788
 
        */
14789
 
        field= item->field;
14790
 
        item->result_field=field->new_field(session->mem_root,field->table, 1);
14791
6284
        /*
14792
 
          We need to allocate one extra byte for null handling and
14793
 
          another extra byte to not get warnings from purify in
14794
 
          Field_varstring::val_int
 
6285
          set up save buffer and change result_field to point at
 
6286
          saved value
14795
6287
        */
14796
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
14797
 
          goto err;
 
6288
        field= item->field;
 
6289
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
 
6290
              /*
 
6291
                We need to allocate one extra byte for null handling and
 
6292
                another extra byte to not get warnings from purify in
 
6293
                Field_varstring::val_int
 
6294
              */
 
6295
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
 
6296
        {
 
6297
          goto err;
 
6298
        }
 
6299
 
14798
6300
        if (copy)
14799
6301
        {
14800
6302
          copy->set(tmp, item->result_field);
14801
6303
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
14802
 
#ifdef HAVE_purify
 
6304
#ifdef HAVE_VALGRIND
14803
6305
          copy->to_ptr[copy->from_length]= 0;
14804
6306
#endif
14805
6307
          copy++;
14813
6315
             !real_pos->with_sum_func)
14814
6316
    {                                           // Save for send fields
14815
6317
      pos= real_pos;
14816
 
      /* TODO:
14817
 
         In most cases this result will be sent to the user.
14818
 
         This should be changed to use copy_int or copy_real depending
14819
 
         on how the value is to be used: In some cases this may be an
14820
 
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
6318
      /*
 
6319
        @todo In most cases this result will be sent to the user.
 
6320
        This should be changed to use copy_int or copy_real depending
 
6321
        on how the value is to be used: In some cases this may be an
 
6322
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
14821
6323
      */
14822
 
      if (!(pos=new Item_copy_string(pos)))
14823
 
        goto err;
 
6324
      pos=new Item_copy_string(pos);
14824
6325
      if (i < border)                           // HAVING, order_st and GROUP BY
14825
6326
      {
14826
 
        if (extra_funcs.push_back(pos))
14827
 
          goto err;
14828
 
      }
14829
 
      else if (param->copy_funcs.push_back(pos))
14830
 
        goto err;
 
6327
        extra_funcs.push_back(pos);
 
6328
      }
 
6329
      else 
 
6330
      {
 
6331
        param->copy_funcs.push_back(pos);
 
6332
      }
14831
6333
    }
14832
6334
    res_all_fields.push_back(pos);
14833
 
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
 
6335
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
14834
6336
      pos;
14835
6337
  }
14836
6338
  param->copy_field_end= copy;
14837
6339
 
14838
 
  for (i= 0; i < border; i++)
 
6340
  for (uint32_t i= 0; i < border; i++)
 
6341
  {
14839
6342
    itr++;
 
6343
  }
14840
6344
  itr.sublist(res_selected_fields, elements);
14841
6345
  /*
14842
 
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
 
6346
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
14843
6347
    reference used in these will resolve to a item that is already calculated
14844
6348
  */
14845
6349
  param->copy_funcs.concat(&extra_funcs);
14846
6350
 
14847
 
  return(0);
 
6351
  return 0;
14848
6352
 
14849
 
 err:
 
6353
err:
14850
6354
  if (copy)
14851
 
    delete [] param->copy_field;                        // This is never 0
 
6355
  {
 
6356
    delete[] param->copy_field;
 
6357
  }
14852
6358
  param->copy_field=0;
14853
 
err2:
14854
 
  return(true);
 
6359
  return true;
14855
6360
}
14856
6361
 
14857
 
 
14858
6362
/**
14859
6363
  Make a copy of all simple SELECT'ed items.
14860
6364
 
14861
6365
  This is done at the start of a new group so that we can retrieve
14862
6366
  these later when the group changes.
14863
6367
*/
14864
 
 
14865
 
void
14866
 
copy_fields(TMP_TABLE_PARAM *param)
 
6368
void copy_fields(Tmp_Table_Param *param)
14867
6369
{
14868
 
  Copy_field *ptr=param->copy_field;
14869
 
  Copy_field *end=param->copy_field_end;
14870
 
 
14871
 
  for (; ptr != end; ptr++)
 
6370
  for (CopyField *ptr= param->copy_field; ptr != param->copy_field_end; ptr++)
 
6371
  {
14872
6372
    (*ptr->do_copy)(ptr);
 
6373
  }
14873
6374
 
14874
 
  List_iterator_fast<Item> it(param->copy_funcs);
 
6375
  List<Item>::iterator it(param->copy_funcs.begin());
14875
6376
  Item_copy_string *item;
14876
6377
  while ((item = (Item_copy_string*) it++))
 
6378
  {
14877
6379
    item->copy();
14878
 
}
14879
 
 
14880
 
 
14881
 
/**
14882
 
  Make an array of pointers to sum_functions to speed up
14883
 
  sum_func calculation.
14884
 
 
14885
 
  @retval
14886
 
    0   ok
14887
 
  @retval
14888
 
    1   Error
14889
 
*/
14890
 
 
14891
 
bool JOIN::alloc_func_list()
14892
 
{
14893
 
  uint32_t func_count, group_parts;
14894
 
 
14895
 
  func_count= tmp_table_param.sum_func_count;
14896
 
  /*
14897
 
    If we are using rollup, we need a copy of the summary functions for
14898
 
    each level
14899
 
  */
14900
 
  if (rollup.state != ROLLUP::STATE_NONE)
14901
 
    func_count*= (send_group_parts+1);
14902
 
 
14903
 
  group_parts= send_group_parts;
14904
 
  /*
14905
 
    If distinct, reserve memory for possible
14906
 
    disctinct->group_by optimization
14907
 
  */
14908
 
  if (select_distinct)
14909
 
  {
14910
 
    group_parts+= fields_list.elements;
14911
 
    /*
14912
 
      If the order_st clause is specified then it's possible that
14913
 
      it also will be optimized, so reserve space for it too
14914
 
    */
14915
 
    if (order)
14916
 
    {
14917
 
      order_st *ord;
14918
 
      for (ord= order; ord; ord= ord->next)
14919
 
        group_parts++;
14920
 
    }
14921
 
  }
14922
 
 
14923
 
  /* This must use calloc() as rollup_make_fields depends on this */
14924
 
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
14925
 
                                      sizeof(Item_sum***) * (group_parts+1));
14926
 
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
14927
 
  return(sum_funcs == 0);
14928
 
}
14929
 
 
14930
 
 
14931
 
/**
14932
 
  Initialize 'sum_funcs' array with all Item_sum objects.
14933
 
 
14934
 
  @param field_list        All items
14935
 
  @param send_fields       Items in select list
14936
 
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
14937
 
  @param recompute         Set to true if sum_funcs must be recomputed
14938
 
 
14939
 
  @retval
14940
 
    0  ok
14941
 
  @retval
14942
 
    1  error
14943
 
*/
14944
 
 
14945
 
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
14946
 
                              bool before_group_by, bool recompute)
14947
 
{
14948
 
  List_iterator_fast<Item> it(field_list);
14949
 
  Item_sum **func;
14950
 
  Item *item;
14951
 
 
14952
 
  if (*sum_funcs && !recompute)
14953
 
    return(false); /* We have already initialized sum_funcs. */
14954
 
 
14955
 
  func= sum_funcs;
14956
 
  while ((item=it++))
14957
 
  {
14958
 
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
14959
 
        (!((Item_sum*) item)->depended_from() ||
14960
 
         ((Item_sum *)item)->depended_from() == select_lex))
14961
 
      *func++= (Item_sum*) item;
14962
 
  }
14963
 
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
14964
 
  {
14965
 
    rollup.state= ROLLUP::STATE_READY;
14966
 
    if (rollup_make_fields(field_list, send_fields, &func))
14967
 
      return(true);                     // Should never happen
14968
 
  }
14969
 
  else if (rollup.state == ROLLUP::STATE_NONE)
14970
 
  {
14971
 
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
14972
 
      sum_funcs_end[i]= func;
14973
 
  }
14974
 
  else if (rollup.state == ROLLUP::STATE_READY)
14975
 
    return(false);                         // Don't put end marker
14976
 
  *func=0;                                      // End marker
14977
 
  return(false);
14978
 
}
14979
 
 
 
6380
  }
 
6381
}
14980
6382
 
14981
6383
/**
14982
6384
  Change all funcs and sum_funcs to fields in tmp table, and create
14994
6396
  @retval
14995
6397
    !=0   error
14996
6398
*/
14997
 
 
14998
 
static bool
14999
 
change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
15000
 
                         List<Item> &res_selected_fields,
15001
 
                         List<Item> &res_all_fields,
15002
 
                         uint32_t elements, List<Item> &all_fields)
 
6399
bool change_to_use_tmp_fields(Session *session,
 
6400
                              Item **ref_pointer_array,
 
6401
                                                List<Item> &res_selected_fields,
 
6402
                                                List<Item> &res_all_fields,
 
6403
                                                uint32_t elements,
 
6404
                              List<Item> &all_fields)
15003
6405
{
15004
 
  List_iterator_fast<Item> it(all_fields);
 
6406
  List<Item>::iterator it(all_fields.begin());
15005
6407
  Item *item_field,*item;
15006
6408
 
15007
 
  res_selected_fields.empty();
15008
 
  res_all_fields.empty();
 
6409
  res_selected_fields.clear();
 
6410
  res_all_fields.clear();
15009
6411
 
15010
 
  uint32_t i, border= all_fields.elements - elements;
 
6412
  uint32_t i, border= all_fields.size() - elements;
15011
6413
  for (i= 0; (item= it++); i++)
15012
6414
  {
15013
6415
    Field *field;
15014
6416
 
15015
 
    if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) ||
15016
 
        (item->type() == Item::FUNC_ITEM &&
15017
 
         ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
 
6417
    if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) or
 
6418
        (item->type() == Item::FUNC_ITEM and ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
 
6419
    {
15018
6420
      item_field= item;
 
6421
    }
15019
6422
    else
15020
6423
    {
15021
6424
      if (item->type() == Item::FIELD_ITEM)
15022
6425
      {
15023
 
        item_field= item->get_tmp_table_item(session);
 
6426
        item_field= item->get_tmp_table_item(session);
15024
6427
      }
15025
6428
      else if ((field= item->get_tmp_table_field()))
15026
6429
      {
15027
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
15028
 
          item_field= ((Item_sum*) item)->result_item(field);
15029
 
        else
15030
 
          item_field= (Item*) new Item_field(field);
15031
 
        if (!item_field)
15032
 
          return(true);                    // Fatal error
 
6430
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
 
6431
        {
 
6432
          item_field= ((Item_sum*) item)->result_item(field);
 
6433
        }
 
6434
        else
 
6435
        {
 
6436
          item_field= (Item*) new Item_field(field);
 
6437
        }
 
6438
 
 
6439
        if (item_field == NULL)
 
6440
        {
 
6441
          return true;                    // Fatal error
 
6442
        }
15033
6443
 
15034
6444
        if (item->real_item()->type() != Item::FIELD_ITEM)
 
6445
        {
15035
6446
          field->orig_table= 0;
15036
 
        item_field->name= item->name;
 
6447
        }
 
6448
 
 
6449
        item_field->name= item->name;
 
6450
 
15037
6451
        if (item->type() == Item::REF_ITEM)
15038
6452
        {
15039
6453
          Item_field *ifield= (Item_field *) item_field;
15043
6457
        }
15044
6458
      }
15045
6459
      else
15046
 
        item_field= item;
 
6460
      {
 
6461
        item_field= item;
 
6462
      }
15047
6463
    }
15048
6464
    res_all_fields.push_back(item_field);
15049
 
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
15050
 
      item_field;
 
6465
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]= item_field;
15051
6466
  }
15052
6467
 
15053
 
  List_iterator_fast<Item> itr(res_all_fields);
 
6468
  List<Item>::iterator itr(res_all_fields.begin());
15054
6469
  for (i= 0; i < border; i++)
15055
6470
    itr++;
15056
6471
  itr.sublist(res_selected_fields, elements);
15057
 
  return(false);
 
6472
  return false;
15058
6473
}
15059
6474
 
15060
 
 
15061
6475
/**
15062
6476
  Change all sum_func refs to fields to point at fields in tmp table.
15063
6477
  Change all funcs to be fields in tmp table.
15074
6488
  @retval
15075
6489
    1   error
15076
6490
*/
15077
 
 
15078
 
static bool
15079
 
change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
15080
 
                          List<Item> &res_selected_fields,
15081
 
                          List<Item> &res_all_fields, uint32_t elements,
15082
 
                          List<Item> &all_fields)
 
6491
bool change_refs_to_tmp_fields(Session *session,
 
6492
                               Item **ref_pointer_array,
 
6493
                               List<Item> &res_selected_fields,
 
6494
                               List<Item> &res_all_fields,
 
6495
                               uint32_t elements,
 
6496
                                                 List<Item> &all_fields)
15083
6497
{
15084
 
  List_iterator_fast<Item> it(all_fields);
 
6498
  List<Item>::iterator it(all_fields.begin());
15085
6499
  Item *item, *new_item;
15086
 
  res_selected_fields.empty();
15087
 
  res_all_fields.empty();
 
6500
  res_selected_fields.clear();
 
6501
  res_all_fields.clear();
15088
6502
 
15089
 
  uint32_t i, border= all_fields.elements - elements;
 
6503
  uint32_t i, border= all_fields.size() - elements;
15090
6504
  for (i= 0; (item= it++); i++)
15091
6505
  {
15092
6506
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
15093
 
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
 
6507
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
15094
6508
      new_item;
15095
6509
  }
15096
6510
 
15097
 
  List_iterator_fast<Item> itr(res_all_fields);
 
6511
  List<Item>::iterator itr(res_all_fields.begin());
15098
6512
  for (i= 0; i < border; i++)
 
6513
  {
15099
6514
    itr++;
 
6515
  }
15100
6516
  itr.sublist(res_selected_fields, elements);
15101
6517
 
15102
6518
  return session->is_fatal_error;
15103
6519
}
15104
6520
 
15105
 
 
15106
 
 
15107
6521
/******************************************************************************
15108
6522
  Code for calculating functions
15109
6523
******************************************************************************/
15110
6524
 
15111
 
 
15112
6525
/**
15113
6526
  Call ::setup for all sum functions.
15114
6527
 
15115
 
  @param session           thread handler
 
6528
  @param session           thread Cursor
15116
6529
  @param func_ptr      sum function list
15117
6530
 
15118
6531
  @retval
15120
6533
  @retval
15121
6534
    true   error
15122
6535
*/
15123
 
 
15124
 
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
6536
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
15125
6537
{
15126
6538
  Item_sum *func;
15127
6539
  while ((func= *(func_ptr++)))
15128
6540
  {
15129
6541
    if (func->setup(session))
15130
 
      return(true);
 
6542
    {
 
6543
      return true;
 
6544
    }
15131
6545
  }
15132
 
  return(false);
 
6546
  return false;
15133
6547
}
15134
6548
 
15135
 
 
15136
 
static void
15137
 
init_tmptable_sum_functions(Item_sum **func_ptr)
 
6549
void init_tmptable_sum_functions(Item_sum **func_ptr)
15138
6550
{
15139
6551
  Item_sum *func;
15140
6552
  while ((func= *(func_ptr++)))
 
6553
  {
15141
6554
    func->reset_field();
 
6555
  }
15142
6556
}
15143
6557
 
15144
 
 
15145
6558
/** Update record 0 in tmp_table from record 1. */
15146
 
 
15147
 
static void
15148
 
update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
6559
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
15149
6560
{
15150
6561
  Item_sum *func;
15151
6562
  while ((func= *(func_ptr++)))
 
6563
  {
15152
6564
    func->update_field();
 
6565
  }
15153
6566
}
15154
6567
 
15155
 
 
15156
6568
/** Copy result of sum functions to record in tmp_table. */
15157
 
 
15158
 
static void
15159
 
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
6569
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
15160
6570
{
15161
6571
  for (; func_ptr != end_ptr ; func_ptr++)
 
6572
  {
15162
6573
    (void) (*func_ptr)->save_in_result_field(1);
15163
 
  return;
 
6574
  }
15164
6575
}
15165
6576
 
15166
 
 
15167
 
static bool
15168
 
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
6577
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
15169
6578
{
15170
6579
  for (; func_ptr != end_ptr ;func_ptr++)
15171
6580
  {
15172
6581
    if ((*func_ptr)->reset())
15173
6582
      return 1;
15174
6583
  }
 
6584
 
15175
6585
  /* If rollup, calculate the upper sum levels */
15176
6586
  for ( ; *func_ptr ; func_ptr++)
15177
6587
  {
15178
6588
    if ((*func_ptr)->add())
 
6589
    {
15179
6590
      return 1;
 
6591
    }
15180
6592
  }
15181
6593
  return 0;
15182
6594
}
15183
6595
 
15184
 
 
15185
 
static bool
15186
 
update_sum_func(Item_sum **func_ptr)
 
6596
bool update_sum_func(Item_sum **func_ptr)
15187
6597
{
15188
6598
  Item_sum *func;
15189
6599
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
 
6600
  {
15190
6601
    if (func->add())
 
6602
    {
15191
6603
      return 1;
 
6604
    }
 
6605
  }
15192
6606
  return 0;
15193
6607
}
15194
6608
 
15195
6609
/** Copy result of functions to record in tmp_table. */
15196
 
 
15197
 
void
15198
 
copy_funcs(Item **func_ptr)
 
6610
bool copy_funcs(Item **func_ptr, const Session *session)
15199
6611
{
15200
6612
  Item *func;
15201
6613
  for (; (func = *func_ptr) ; func_ptr++)
 
6614
  {
15202
6615
    func->save_in_result_field(1);
15203
 
}
15204
 
 
15205
 
 
15206
 
/**
15207
 
  Create a condition for a const reference and add this to the
15208
 
  currenct select for the table.
15209
 
*/
15210
 
 
15211
 
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
15212
 
{
15213
 
  if (!join_tab->ref.key_parts)
15214
 
    return(false);
15215
 
 
15216
 
  Item_cond_and *cond=new Item_cond_and();
15217
 
  Table *table=join_tab->table;
15218
 
  int error;
15219
 
  if (!cond)
15220
 
    return(true);
15221
 
 
15222
 
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
15223
 
  {
15224
 
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
15225
 
                              fieldnr-1];
15226
 
    Item *value=join_tab->ref.items[i];
15227
 
    cond->add(new Item_func_equal(new Item_field(field), value));
15228
 
  }
15229
 
  if (session->is_fatal_error)
15230
 
    return(true);
15231
 
 
15232
 
  if (!cond->fixed)
15233
 
    cond->fix_fields(session, (Item**)&cond);
15234
 
  if (join_tab->select)
15235
 
  {
15236
 
    error=(int) cond->add(join_tab->select->cond);
15237
 
    join_tab->select_cond=join_tab->select->cond=cond;
15238
 
  }
15239
 
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
15240
 
                                          &error)))
15241
 
    join_tab->select_cond=cond;
15242
 
 
15243
 
  return(error ? true : false);
15244
 
}
15245
 
 
 
6616
    /*
 
6617
      Need to check the THD error state because Item::val_xxx() don't
 
6618
      return error code, but can generate errors
 
6619
      @todo change it for a real status check when Item::val_xxx()
 
6620
      are extended to return status code.
 
6621
    */
 
6622
    if (session->is_error())
 
6623
    {
 
6624
      return true;
 
6625
    }
 
6626
  }
 
6627
  return false;
 
6628
}
15246
6629
 
15247
6630
/**
15248
6631
  Free joins of subselect of this select.
15249
6632
 
15250
6633
  @param session      Session pointer
15251
 
  @param select   pointer to st_select_lex which subselects joins we will free
 
6634
  @param select   pointer to Select_Lex which subselects joins we will free
15252
6635
*/
15253
 
 
15254
 
void free_underlaid_joins(Session *, SELECT_LEX *select)
 
6636
void free_underlaid_joins(Session *, Select_Lex *select)
15255
6637
{
15256
 
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
 
6638
  for (Select_Lex_Unit *unit= select->first_inner_unit();
15257
6639
       unit;
15258
6640
       unit= unit->next_unit())
15259
6641
    unit->cleanup();
15294
6676
  @param changed        out:  returns 1 if item contains a replaced field item
15295
6677
 
15296
6678
  @todo
15297
 
    - TODO: Some functions are not null-preserving. For those functions
 
6679
    - @todo Some functions are not null-preserving. For those functions
15298
6680
    updating of the maybe_null attribute is an overkill.
15299
6681
 
15300
6682
  @retval
15302
6684
  @retval
15303
6685
    1   on error
15304
6686
*/
15305
 
 
15306
 
static bool change_group_ref(Session *session, Item_func *expr, order_st *group_list,
15307
 
                             bool *changed)
 
6687
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
15308
6688
{
15309
6689
  if (expr->arg_count)
15310
6690
  {
15311
 
    Name_resolution_context *context= &session->lex->current_select->context;
 
6691
    Name_resolution_context *context= &session->lex().current_select->context;
15312
6692
    Item **arg,**arg_end;
15313
6693
    bool arg_changed= false;
15314
6694
    for (arg= expr->arguments(),
15318
6698
      Item *item= *arg;
15319
6699
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
15320
6700
      {
15321
 
        order_st *group_tmp;
 
6701
        Order *group_tmp;
15322
6702
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15323
6703
        {
15324
6704
          if (item->eq(*group_tmp->item,0))
15325
6705
          {
15326
 
            Item *new_item;
15327
 
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
15328
 
                                        item->name)))
15329
 
              return 1;                                 // fatal_error is set
15330
 
            session->change_item_tree(arg, new_item);
 
6706
            Item* new_item= new Item_ref(context, group_tmp->item, 0, item->name);
 
6707
            *arg= new_item;
15331
6708
            arg_changed= true;
15332
6709
          }
15333
6710
        }
15335
6712
      else if (item->type() == Item::FUNC_ITEM)
15336
6713
      {
15337
6714
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
6715
        {
15338
6716
          return 1;
 
6717
        }
15339
6718
      }
15340
6719
    }
 
6720
 
15341
6721
    if (arg_changed)
15342
6722
    {
15343
6723
      expr->maybe_null= 1;
15348
6728
}
15349
6729
 
15350
6730
 
15351
 
/** Allocate memory needed for other rollup functions. */
15352
 
 
15353
 
bool JOIN::rollup_init()
15354
 
{
15355
 
  uint32_t i,j;
15356
 
  Item **ref_array;
15357
 
 
15358
 
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
15359
 
  rollup.state= ROLLUP::STATE_INITED;
15360
 
 
15361
 
  /*
15362
 
    Create pointers to the different sum function groups
15363
 
    These are updated by rollup_make_fields()
15364
 
  */
15365
 
  tmp_table_param.group_parts= send_group_parts;
15366
 
 
15367
 
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
15368
 
                                                sizeof(Item**) +
15369
 
                                                sizeof(List<Item>) +
15370
 
                                                ref_pointer_array_size)
15371
 
                                                * send_group_parts )))
15372
 
    return 1;
15373
 
 
15374
 
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
15375
 
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
15376
 
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
15377
 
 
15378
 
  /*
15379
 
    Prepare space for field list for the different levels
15380
 
    These will be filled up in rollup_make_fields()
15381
 
  */
15382
 
  for (i= 0 ; i < send_group_parts ; i++)
15383
 
  {
15384
 
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
15385
 
    List<Item> *rollup_fields= &rollup.fields[i];
15386
 
    rollup_fields->empty();
15387
 
    rollup.ref_pointer_arrays[i]= ref_array;
15388
 
    ref_array+= all_fields.elements;
15389
 
  }
15390
 
  for (i= 0 ; i < send_group_parts; i++)
15391
 
  {
15392
 
    for (j=0 ; j < fields_list.elements ; j++)
15393
 
      rollup.fields[i].push_back(rollup.null_items[i]);
15394
 
  }
15395
 
  List_iterator<Item> it(all_fields);
15396
 
  Item *item;
15397
 
  while ((item= it++))
15398
 
  {
15399
 
    order_st *group_tmp;
15400
 
    bool found_in_group= 0;
15401
 
 
15402
 
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15403
 
    {
15404
 
      if (*group_tmp->item == item)
15405
 
      {
15406
 
        item->maybe_null= 1;
15407
 
        found_in_group= 1;
15408
 
        if (item->const_item())
15409
 
        {
15410
 
          /*
15411
 
            For ROLLUP queries each constant item referenced in GROUP BY list
15412
 
            is wrapped up into an Item_func object yielding the same value
15413
 
            as the constant item. The objects of the wrapper class are never
15414
 
            considered as constant items and besides they inherit all
15415
 
            properties of the Item_result_field class.
15416
 
            This wrapping allows us to ensure writing constant items
15417
 
            into temporary tables whenever the result of the ROLLUP
15418
 
            operation has to be written into a temporary table, e.g. when
15419
 
            ROLLUP is used together with DISTINCT in the SELECT list.
15420
 
            Usually when creating temporary tables for a intermidiate
15421
 
            result we do not include fields for constant expressions.
15422
 
          */
15423
 
          Item* new_item= new Item_func_rollup_const(item);
15424
 
          if (!new_item)
15425
 
            return 1;
15426
 
          new_item->fix_fields(session, (Item **) 0);
15427
 
          session->change_item_tree(it.ref(), new_item);
15428
 
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
15429
 
          {
15430
 
            if (*tmp->item == item)
15431
 
              session->change_item_tree(tmp->item, new_item);
15432
 
          }
15433
 
        }
15434
 
      }
15435
 
    }
15436
 
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
15437
 
    {
15438
 
      bool changed= false;
15439
 
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
15440
 
        return 1;
15441
 
      /*
15442
 
        We have to prevent creation of a field in a temporary table for
15443
 
        an expression that contains GROUP BY attributes.
15444
 
        Marking the expression item as 'with_sum_func' will ensure this.
15445
 
      */
15446
 
      if (changed)
15447
 
        item->with_sum_func= 1;
15448
 
    }
15449
 
  }
15450
 
  return 0;
15451
 
}
15452
 
 
15453
 
 
15454
 
/**
15455
 
  Fill up rollup structures with pointers to fields to use.
15456
 
 
15457
 
  Creates copies of item_sum items for each sum level.
15458
 
 
15459
 
  @param fields_arg             List of all fields (hidden and real ones)
15460
 
  @param sel_fields             Pointer to selected fields
15461
 
  @param func                   Store here a pointer to all fields
15462
 
 
15463
 
  @retval
15464
 
    0   if ok;
15465
 
    In this case func is pointing to next not used element.
15466
 
  @retval
15467
 
    1    on error
15468
 
*/
15469
 
 
15470
 
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
15471
 
                              Item_sum ***func)
15472
 
{
15473
 
  List_iterator_fast<Item> it(fields_arg);
15474
 
  Item *first_field= sel_fields.head();
15475
 
  uint32_t level;
15476
 
 
15477
 
  /*
15478
 
    Create field lists for the different levels
15479
 
 
15480
 
    The idea here is to have a separate field list for each rollup level to
15481
 
    avoid all runtime checks of which columns should be NULL.
15482
 
 
15483
 
    The list is stored in reverse order to get sum function in such an order
15484
 
    in func that it makes it easy to reset them with init_sum_functions()
15485
 
 
15486
 
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
15487
 
 
15488
 
    rollup.fields[0] will contain list where a,b,c is NULL
15489
 
    rollup.fields[1] will contain list where b,c is NULL
15490
 
    ...
15491
 
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
15492
 
    ...
15493
 
    sum_funcs_end[0] points to all sum functions
15494
 
    sum_funcs_end[1] points to all sum functions, except grand totals
15495
 
    ...
15496
 
  */
15497
 
 
15498
 
  for (level=0 ; level < send_group_parts ; level++)
15499
 
  {
15500
 
    uint32_t i;
15501
 
    uint32_t pos= send_group_parts - level -1;
15502
 
    bool real_fields= 0;
15503
 
    Item *item;
15504
 
    List_iterator<Item> new_it(rollup.fields[pos]);
15505
 
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
15506
 
    order_st *start_group;
15507
 
 
15508
 
    /* Point to first hidden field */
15509
 
    Item **ref_array= ref_array_start + fields_arg.elements-1;
15510
 
 
15511
 
    /* Remember where the sum functions ends for the previous level */
15512
 
    sum_funcs_end[pos+1]= *func;
15513
 
 
15514
 
    /* Find the start of the group for this level */
15515
 
    for (i= 0, start_group= group_list ;
15516
 
         i++ < pos ;
15517
 
         start_group= start_group->next)
15518
 
      ;
15519
 
 
15520
 
    it.rewind();
15521
 
    while ((item= it++))
15522
 
    {
15523
 
      if (item == first_field)
15524
 
      {
15525
 
        real_fields= 1;                         // End of hidden fields
15526
 
        ref_array= ref_array_start;
15527
 
      }
15528
 
 
15529
 
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
15530
 
          (!((Item_sum*) item)->depended_from() ||
15531
 
           ((Item_sum *)item)->depended_from() == select_lex))
15532
 
 
15533
 
      {
15534
 
        /*
15535
 
          This is a top level summary function that must be replaced with
15536
 
          a sum function that is reset for this level.
15537
 
 
15538
 
          NOTE: This code creates an object which is not that nice in a
15539
 
          sub select.  Fortunately it's not common to have rollup in
15540
 
          sub selects.
15541
 
        */
15542
 
        item= item->copy_or_same(session);
15543
 
        ((Item_sum*) item)->make_unique();
15544
 
        *(*func)= (Item_sum*) item;
15545
 
        (*func)++;
15546
 
      }
15547
 
      else
15548
 
      {
15549
 
        /* Check if this is something that is part of this group by */
15550
 
        order_st *group_tmp;
15551
 
        for (group_tmp= start_group, i= pos ;
15552
 
             group_tmp ; group_tmp= group_tmp->next, i++)
15553
 
        {
15554
 
          if (*group_tmp->item == item)
15555
 
          {
15556
 
            /*
15557
 
              This is an element that is used by the GROUP BY and should be
15558
 
              set to NULL in this level
15559
 
            */
15560
 
            Item_null_result *null_item= new (session->mem_root) Item_null_result();
15561
 
            if (!null_item)
15562
 
              return 1;
15563
 
            item->maybe_null= 1;                // Value will be null sometimes
15564
 
            null_item->result_field= item->get_tmp_table_field();
15565
 
            item= null_item;
15566
 
            break;
15567
 
          }
15568
 
        }
15569
 
      }
15570
 
      *ref_array= item;
15571
 
      if (real_fields)
15572
 
      {
15573
 
        (void) new_it++;                        // Point to next item
15574
 
        new_it.replace(item);                   // Replace previous
15575
 
        ref_array++;
15576
 
      }
15577
 
      else
15578
 
        ref_array--;
15579
 
    }
15580
 
  }
15581
 
  sum_funcs_end[0]= *func;                      // Point to last function
15582
 
  return 0;
15583
 
}
15584
 
 
15585
 
/**
15586
 
  Send all rollup levels higher than the current one to the client.
15587
 
 
15588
 
  @b SAMPLE
15589
 
    @code
15590
 
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
15591
 
  @endcode
15592
 
 
15593
 
  @param idx            Level we are on:
15594
 
                        - 0 = Total sum level
15595
 
                        - 1 = First group changed  (a)
15596
 
                        - 2 = Second group changed (a,b)
15597
 
 
15598
 
  @retval
15599
 
    0   ok
15600
 
  @retval
15601
 
    1   If send_data_failed()
15602
 
*/
15603
 
 
15604
 
int JOIN::rollup_send_data(uint32_t idx)
15605
 
{
15606
 
  uint32_t i;
15607
 
  for (i= send_group_parts ; i-- > idx ; )
15608
 
  {
15609
 
    /* Get reference pointers to sum functions in place */
15610
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
15611
 
           ref_pointer_array_size);
15612
 
    if ((!having || having->val_int()))
15613
 
    {
15614
 
      if (send_records < unit->select_limit_cnt && do_send_rows &&
15615
 
          result->send_data(rollup.fields[i]))
15616
 
        return 1;
15617
 
      send_records++;
15618
 
    }
15619
 
  }
15620
 
  /* Restore ref_pointer_array */
15621
 
  set_items_ref_array(current_ref_pointer_array);
15622
 
  return 0;
15623
 
}
15624
 
 
15625
 
/**
15626
 
  Write all rollup levels higher than the current one to a temp table.
15627
 
 
15628
 
  @b SAMPLE
15629
 
    @code
15630
 
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
15631
 
  @endcode
15632
 
 
15633
 
  @param idx                 Level we are on:
15634
 
                               - 0 = Total sum level
15635
 
                               - 1 = First group changed  (a)
15636
 
                               - 2 = Second group changed (a,b)
15637
 
  @param table               reference to temp table
15638
 
 
15639
 
  @retval
15640
 
    0   ok
15641
 
  @retval
15642
 
    1   if write_data_failed()
15643
 
*/
15644
 
 
15645
 
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
15646
 
{
15647
 
  uint32_t i;
15648
 
  for (i= send_group_parts ; i-- > idx ; )
15649
 
  {
15650
 
    /* Get reference pointers to sum functions in place */
15651
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
15652
 
           ref_pointer_array_size);
15653
 
    if ((!having || having->val_int()))
15654
 
    {
15655
 
      int write_error;
15656
 
      Item *item;
15657
 
      List_iterator_fast<Item> it(rollup.fields[i]);
15658
 
      while ((item= it++))
15659
 
      {
15660
 
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
15661
 
          item->save_in_result_field(1);
15662
 
      }
15663
 
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
15664
 
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
15665
 
      {
15666
 
        if (create_myisam_from_heap(session, table_arg,
15667
 
                                    tmp_table_param.start_recinfo,
15668
 
                                    &tmp_table_param.recinfo,
15669
 
                                    write_error, 0))
15670
 
          return 1;
15671
 
      }
15672
 
    }
15673
 
  }
15674
 
  /* Restore ref_pointer_array */
15675
 
  set_items_ref_array(current_ref_pointer_array);
15676
 
  return 0;
15677
 
}
15678
 
 
15679
 
/**
15680
 
  clear results if there are not rows found for group
15681
 
  (end_send_group/end_write_group)
15682
 
*/
15683
 
 
15684
 
void JOIN::clear()
15685
 
{
15686
 
  clear_tables(this);
15687
 
  copy_fields(&tmp_table_param);
15688
 
 
15689
 
  if (sum_funcs)
15690
 
  {
15691
 
    Item_sum *func, **func_ptr= sum_funcs;
15692
 
    while ((func= *(func_ptr++)))
15693
 
      func->clear();
15694
 
  }
15695
 
}
15696
 
 
15697
 
/**
15698
 
  EXPLAIN handling.
15699
 
 
15700
 
  Send a description about what how the select will be done to stdout.
15701
 
*/
15702
 
 
15703
 
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
15704
 
                     bool distinct,const char *message)
15705
 
{
15706
 
  List<Item> field_list;
15707
 
  List<Item> item_list;
15708
 
  Session *session=join->session;
15709
 
  select_result *result=join->result;
15710
 
  Item *item_null= new Item_null();
15711
 
  const CHARSET_INFO * const cs= system_charset_info;
15712
 
  int quick_type;
15713
 
  /* Don't log this into the slow query log */
15714
 
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
15715
 
  join->unit->offset_limit_cnt= 0;
15716
 
 
15717
 
  /*
15718
 
    NOTE: the number/types of items pushed into item_list must be in sync with
15719
 
    EXPLAIN column types as they're "defined" in Session::send_explain_fields()
15720
 
  */
15721
 
  if (message)
15722
 
  {
15723
 
    item_list.push_back(new Item_int((int32_t)
15724
 
                                     join->select_lex->select_number));
15725
 
    item_list.push_back(new Item_string(join->select_lex->type,
15726
 
                                        strlen(join->select_lex->type), cs));
15727
 
    for (uint32_t i=0 ; i < 7; i++)
15728
 
      item_list.push_back(item_null);
15729
 
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
15730
 
      item_list.push_back(item_null);
15731
 
 
15732
 
    item_list.push_back(new Item_string(message,strlen(message),cs));
15733
 
    if (result->send_data(item_list))
15734
 
      join->error= 1;
15735
 
  }
15736
 
  else if (join->select_lex == join->unit->fake_select_lex)
15737
 
  {
15738
 
    /*
15739
 
      here we assume that the query will return at least two rows, so we
15740
 
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
15741
 
      and no filesort will be actually done, but executing all selects in
15742
 
      the UNION to provide precise EXPLAIN information will hardly be
15743
 
      appreciated :)
15744
 
    */
15745
 
    char table_name_buffer[NAME_LEN];
15746
 
    item_list.empty();
15747
 
    /* id */
15748
 
    item_list.push_back(new Item_null);
15749
 
    /* select_type */
15750
 
    item_list.push_back(new Item_string(join->select_lex->type,
15751
 
                                        strlen(join->select_lex->type),
15752
 
                                        cs));
15753
 
    /* table */
15754
 
    {
15755
 
      SELECT_LEX *sl= join->unit->first_select();
15756
 
      uint32_t len= 6, lastop= 0;
15757
 
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
15758
 
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
15759
 
      {
15760
 
        len+= lastop;
15761
 
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
15762
 
                         "%u,", sl->select_number);
15763
 
      }
15764
 
      if (sl || len + lastop >= NAME_LEN)
15765
 
      {
15766
 
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
15767
 
        len+= 4;
15768
 
      }
15769
 
      else
15770
 
      {
15771
 
        len+= lastop;
15772
 
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
15773
 
      }
15774
 
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
15775
 
    }
15776
 
    /* type */
15777
 
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
15778
 
                                          strlen(join_type_str[JT_ALL]),
15779
 
                                          cs));
15780
 
    /* possible_keys */
15781
 
    item_list.push_back(item_null);
15782
 
    /* key*/
15783
 
    item_list.push_back(item_null);
15784
 
    /* key_len */
15785
 
    item_list.push_back(item_null);
15786
 
    /* ref */
15787
 
    item_list.push_back(item_null);
15788
 
    /* in_rows */
15789
 
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
15790
 
      item_list.push_back(item_null);
15791
 
    /* rows */
15792
 
    item_list.push_back(item_null);
15793
 
    /* extra */
15794
 
    if (join->unit->global_parameters->order_list.first)
15795
 
      item_list.push_back(new Item_string("Using filesort",
15796
 
                                          14, cs));
15797
 
    else
15798
 
      item_list.push_back(new Item_string("", 0, cs));
15799
 
 
15800
 
    if (result->send_data(item_list))
15801
 
      join->error= 1;
15802
 
  }
15803
 
  else
15804
 
  {
15805
 
    table_map used_tables=0;
15806
 
    for (uint32_t i=0 ; i < join->tables ; i++)
15807
 
    {
15808
 
      JOIN_TAB *tab=join->join_tab+i;
15809
 
      Table *table=tab->table;
15810
 
      TableList *table_list= tab->table->pos_in_table_list;
15811
 
      char buff[512];
15812
 
      char buff1[512], buff2[512], buff3[512];
15813
 
      char keylen_str_buf[64];
15814
 
      String extra(buff, sizeof(buff),cs);
15815
 
      char table_name_buffer[NAME_LEN];
15816
 
      String tmp1(buff1,sizeof(buff1),cs);
15817
 
      String tmp2(buff2,sizeof(buff2),cs);
15818
 
      String tmp3(buff3,sizeof(buff3),cs);
15819
 
      extra.length(0);
15820
 
      tmp1.length(0);
15821
 
      tmp2.length(0);
15822
 
      tmp3.length(0);
15823
 
 
15824
 
      quick_type= -1;
15825
 
      item_list.empty();
15826
 
      /* id */
15827
 
      item_list.push_back(new Item_uint((uint32_t)
15828
 
                                       join->select_lex->select_number));
15829
 
      /* select_type */
15830
 
      item_list.push_back(new Item_string(join->select_lex->type,
15831
 
                                          strlen(join->select_lex->type),
15832
 
                                          cs));
15833
 
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
15834
 
      {
15835
 
        quick_type= tab->select->quick->get_type();
15836
 
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
15837
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
15838
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
15839
 
          tab->type = JT_INDEX_MERGE;
15840
 
        else
15841
 
          tab->type = JT_RANGE;
15842
 
      }
15843
 
      /* table */
15844
 
      if (table->derived_select_number)
15845
 
      {
15846
 
        /* Derived table name generation */
15847
 
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
15848
 
                          "<derived%u>",
15849
 
                          table->derived_select_number);
15850
 
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
15851
 
      }
15852
 
      else
15853
 
      {
15854
 
        TableList *real_table= table->pos_in_table_list;
15855
 
        item_list.push_back(new Item_string(real_table->alias,
15856
 
                                            strlen(real_table->alias),
15857
 
                                            cs));
15858
 
      }
15859
 
      /* "type" column */
15860
 
      item_list.push_back(new Item_string(join_type_str[tab->type],
15861
 
                                          strlen(join_type_str[tab->type]),
15862
 
                                          cs));
15863
 
      /* Build "possible_keys" value and add it to item_list */
15864
 
      if (!tab->keys.is_clear_all())
15865
 
      {
15866
 
        uint32_t j;
15867
 
        for (j=0 ; j < table->s->keys ; j++)
15868
 
        {
15869
 
          if (tab->keys.is_set(j))
15870
 
          {
15871
 
            if (tmp1.length())
15872
 
              tmp1.append(',');
15873
 
            tmp1.append(table->key_info[j].name,
15874
 
                        strlen(table->key_info[j].name),
15875
 
                        system_charset_info);
15876
 
          }
15877
 
        }
15878
 
      }
15879
 
      if (tmp1.length())
15880
 
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
15881
 
      else
15882
 
        item_list.push_back(item_null);
15883
 
 
15884
 
      /* Build "key", "key_len", and "ref" values and add them to item_list */
15885
 
      if (tab->ref.key_parts)
15886
 
      {
15887
 
        KEY *key_info=table->key_info+ tab->ref.key;
15888
 
        register uint32_t length;
15889
 
        item_list.push_back(new Item_string(key_info->name,
15890
 
                                            strlen(key_info->name),
15891
 
                                            system_charset_info));
15892
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
15893
 
                keylen_str_buf;
15894
 
        item_list.push_back(new Item_string(keylen_str_buf, length,
15895
 
                                            system_charset_info));
15896
 
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
15897
 
        {
15898
 
          if (tmp2.length())
15899
 
            tmp2.append(',');
15900
 
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
15901
 
                      system_charset_info);
15902
 
        }
15903
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
15904
 
      }
15905
 
      else if (tab->type == JT_NEXT)
15906
 
      {
15907
 
        KEY *key_info=table->key_info+ tab->index;
15908
 
        register uint32_t length;
15909
 
        item_list.push_back(new Item_string(key_info->name,
15910
 
                                            strlen(key_info->name),cs));
15911
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
15912
 
                keylen_str_buf;
15913
 
        item_list.push_back(new Item_string(keylen_str_buf,
15914
 
                                            length,
15915
 
                                            system_charset_info));
15916
 
        item_list.push_back(item_null);
15917
 
      }
15918
 
      else if (tab->select && tab->select->quick)
15919
 
      {
15920
 
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
15921
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
15922
 
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
15923
 
        item_list.push_back(item_null);
15924
 
      }
15925
 
      else
15926
 
      {
15927
 
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
15928
 
        {
15929
 
          const char *tmp_buff;
15930
 
          int f_idx;
15931
 
          if (table_list->has_db_lookup_value)
15932
 
          {
15933
 
            f_idx= table_list->schema_table->idx_field1;
15934
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
15935
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
15936
 
          }
15937
 
          if (table_list->has_table_lookup_value)
15938
 
          {
15939
 
            if (table_list->has_db_lookup_value)
15940
 
              tmp2.append(',');
15941
 
            f_idx= table_list->schema_table->idx_field2;
15942
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
15943
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
15944
 
          }
15945
 
          if (tmp2.length())
15946
 
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
15947
 
          else
15948
 
            item_list.push_back(item_null);
15949
 
        }
15950
 
        else
15951
 
          item_list.push_back(item_null);
15952
 
        item_list.push_back(item_null);
15953
 
        item_list.push_back(item_null);
15954
 
      }
15955
 
 
15956
 
      /* Add "rows" field to item_list. */
15957
 
      if (table_list->schema_table)
15958
 
      {
15959
 
        /* in_rows */
15960
 
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
15961
 
          item_list.push_back(item_null);
15962
 
        /* rows */
15963
 
        item_list.push_back(item_null);
15964
 
      }
15965
 
      else
15966
 
      {
15967
 
        double examined_rows;
15968
 
        if (tab->select && tab->select->quick)
15969
 
          examined_rows= rows2double(tab->select->quick->records);
15970
 
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
15971
 
          examined_rows= rows2double(tab->limit ? tab->limit :
15972
 
                                     tab->table->file->records());
15973
 
        else
15974
 
          examined_rows= join->best_positions[i].records_read;
15975
 
 
15976
 
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
15977
 
                                         MY_INT64_NUM_DECIMAL_DIGITS));
15978
 
 
15979
 
        /* Add "filtered" field to item_list. */
15980
 
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
15981
 
        {
15982
 
          float f= 0.0;
15983
 
          if (examined_rows)
15984
 
            f= (float) (100.0 * join->best_positions[i].records_read /
15985
 
                        examined_rows);
15986
 
          item_list.push_back(new Item_float(f, 2));
15987
 
        }
15988
 
      }
15989
 
 
15990
 
      /* Build "Extra" field and add it to item_list. */
15991
 
      bool key_read=table->key_read;
15992
 
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
15993
 
          table->covering_keys.is_set(tab->index))
15994
 
        key_read=1;
15995
 
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
15996
 
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
15997
 
        key_read=1;
15998
 
 
15999
 
      if (tab->info)
16000
 
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
16001
 
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
16002
 
      {
16003
 
        if (tab->packed_info & TAB_INFO_USING_INDEX)
16004
 
          extra.append(STRING_WITH_LEN("; Using index"));
16005
 
        if (tab->packed_info & TAB_INFO_USING_WHERE)
16006
 
          extra.append(STRING_WITH_LEN("; Using where"));
16007
 
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
16008
 
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
16009
 
        /* Skip initial "; "*/
16010
 
        const char *str= extra.ptr();
16011
 
        uint32_t len= extra.length();
16012
 
        if (len)
16013
 
        {
16014
 
          str += 2;
16015
 
          len -= 2;
16016
 
        }
16017
 
        item_list.push_back(new Item_string(str, len, cs));
16018
 
      }
16019
 
      else
16020
 
      {
16021
 
        uint32_t keyno= MAX_KEY;
16022
 
        if (tab->ref.key_parts)
16023
 
          keyno= tab->ref.key;
16024
 
        else if (tab->select && tab->select->quick)
16025
 
          keyno = tab->select->quick->index;
16026
 
 
16027
 
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
16028
 
            table->file->pushed_idx_cond)
16029
 
          extra.append(STRING_WITH_LEN("; Using index condition"));
16030
 
 
16031
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
16032
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
16033
 
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
16034
 
        {
16035
 
          extra.append(STRING_WITH_LEN("; Using "));
16036
 
          tab->select->quick->add_info_string(&extra);
16037
 
        }
16038
 
          if (tab->select)
16039
 
        {
16040
 
          if (tab->use_quick == 2)
16041
 
          {
16042
 
            /* 4 bits per 1 hex digit + terminating '\0' */
16043
 
            char buf[MAX_KEY / 4 + 1];
16044
 
            extra.append(STRING_WITH_LEN("; Range checked for each "
16045
 
                                         "record (index map: 0x"));
16046
 
            extra.append(tab->keys.print(buf));
16047
 
            extra.append(')');
16048
 
          }
16049
 
          else if (tab->select->cond)
16050
 
          {
16051
 
            const COND *pushed_cond= tab->table->file->pushed_cond;
16052
 
 
16053
 
            if (session->variables.engine_condition_pushdown && pushed_cond)
16054
 
            {
16055
 
              extra.append(STRING_WITH_LEN("; Using where with pushed "
16056
 
                                           "condition"));
16057
 
              if (session->lex->describe & DESCRIBE_EXTENDED)
16058
 
              {
16059
 
                extra.append(STRING_WITH_LEN(": "));
16060
 
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
16061
 
              }
16062
 
            }
16063
 
            else
16064
 
              extra.append(STRING_WITH_LEN("; Using where"));
16065
 
          }
16066
 
        }
16067
 
        if (key_read)
16068
 
        {
16069
 
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
16070
 
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
16071
 
          else
16072
 
            extra.append(STRING_WITH_LEN("; Using index"));
16073
 
        }
16074
 
        if (table->reginfo.not_exists_optimize)
16075
 
          extra.append(STRING_WITH_LEN("; Not exists"));
16076
 
 
16077
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
16078
 
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
16079
 
             HA_MRR_USE_DEFAULT_IMPL))
16080
 
        {
16081
 
          extra.append(STRING_WITH_LEN("; Using MRR"));
16082
 
        }
16083
 
 
16084
 
        if (table_list->schema_table &&
16085
 
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
16086
 
        {
16087
 
          if (!table_list->table_open_method)
16088
 
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
16089
 
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
16090
 
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
16091
 
          else
16092
 
            extra.append(STRING_WITH_LEN("; Open_full_table"));
16093
 
          if (table_list->has_db_lookup_value &&
16094
 
              table_list->has_table_lookup_value)
16095
 
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
16096
 
          else if (table_list->has_db_lookup_value ||
16097
 
                   table_list->has_table_lookup_value)
16098
 
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
16099
 
          else
16100
 
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
16101
 
        }
16102
 
        if (need_tmp_table)
16103
 
        {
16104
 
          need_tmp_table=0;
16105
 
          extra.append(STRING_WITH_LEN("; Using temporary"));
16106
 
        }
16107
 
        if (need_order)
16108
 
        {
16109
 
          need_order=0;
16110
 
          extra.append(STRING_WITH_LEN("; Using filesort"));
16111
 
        }
16112
 
        if (distinct & test_all_bits(used_tables,session->used_tables))
16113
 
          extra.append(STRING_WITH_LEN("; Distinct"));
16114
 
 
16115
 
        if (tab->insideout_match_tab)
16116
 
        {
16117
 
          extra.append(STRING_WITH_LEN("; LooseScan"));
16118
 
        }
16119
 
 
16120
 
        if (tab->flush_weedout_table)
16121
 
          extra.append(STRING_WITH_LEN("; Start temporary"));
16122
 
        else if (tab->check_weed_out_table)
16123
 
          extra.append(STRING_WITH_LEN("; End temporary"));
16124
 
        else if (tab->do_firstmatch)
16125
 
        {
16126
 
          extra.append(STRING_WITH_LEN("; FirstMatch("));
16127
 
          Table *prev_table=tab->do_firstmatch->table;
16128
 
          if (prev_table->derived_select_number)
16129
 
          {
16130
 
            char namebuf[NAME_LEN];
16131
 
            /* Derived table name generation */
16132
 
            int len= snprintf(namebuf, sizeof(namebuf)-1,
16133
 
                              "<derived%u>",
16134
 
                              prev_table->derived_select_number);
16135
 
            extra.append(namebuf, len);
16136
 
          }
16137
 
          else
16138
 
            extra.append(prev_table->pos_in_table_list->alias);
16139
 
          extra.append(STRING_WITH_LEN(")"));
16140
 
        }
16141
 
 
16142
 
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
16143
 
        {
16144
 
          if (tab->ref.cond_guards[part])
16145
 
          {
16146
 
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
16147
 
            break;
16148
 
          }
16149
 
        }
16150
 
 
16151
 
        if (i > 0 && tab[-1].next_select == sub_select_cache)
16152
 
          extra.append(STRING_WITH_LEN("; Using join buffer"));
16153
 
 
16154
 
        /* Skip initial "; "*/
16155
 
        const char *str= extra.ptr();
16156
 
        uint32_t len= extra.length();
16157
 
        if (len)
16158
 
        {
16159
 
          str += 2;
16160
 
          len -= 2;
16161
 
        }
16162
 
        item_list.push_back(new Item_string(str, len, cs));
16163
 
      }
16164
 
      // For next iteration
16165
 
      used_tables|=table->map;
16166
 
      if (result->send_data(item_list))
16167
 
        join->error= 1;
16168
 
    }
16169
 
  }
16170
 
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
16171
 
       unit;
16172
 
       unit= unit->next_unit())
16173
 
  {
16174
 
    if (mysql_explain_union(session, unit, result))
16175
 
      return;
16176
 
  }
16177
 
  return;
16178
 
}
16179
 
 
16180
 
 
16181
 
bool mysql_explain_union(Session *session, SELECT_LEX_UNIT *unit, select_result *result)
16182
 
{
16183
 
  bool res= 0;
16184
 
  SELECT_LEX *first= unit->first_select();
16185
 
 
16186
 
  for (SELECT_LEX *sl= first;
16187
 
       sl;
16188
 
       sl= sl->next_select())
16189
 
  {
16190
 
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
16191
 
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
16192
 
    sl->type= (((&session->lex->select_lex)==sl)?
16193
 
               (sl->first_inner_unit() || sl->next_select() ?
16194
 
                "PRIMARY" : "SIMPLE"):
16195
 
               ((sl == first)?
16196
 
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
16197
 
                 "DERIVED":
16198
 
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
16199
 
                  "DEPENDENT SUBQUERY":
16200
 
                  (uncacheable?"UNCACHEABLE SUBQUERY":
16201
 
                   "SUBQUERY"))):
16202
 
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
16203
 
                 "DEPENDENT UNION":
16204
 
                 uncacheable?"UNCACHEABLE UNION":
16205
 
                 "UNION")));
16206
 
    sl->options|= SELECT_DESCRIBE;
16207
 
  }
16208
 
  if (unit->is_union())
16209
 
  {
16210
 
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
16211
 
    unit->fake_select_lex->type= "UNION RESULT";
16212
 
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
16213
 
    if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
16214
 
      res= unit->exec();
16215
 
    res|= unit->cleanup();
16216
 
  }
16217
 
  else
16218
 
  {
16219
 
    session->lex->current_select= first;
16220
 
    unit->set_limit(unit->global_parameters);
16221
 
    res= mysql_select(session, &first->ref_pointer_array,
16222
 
                        (TableList*) first->table_list.first,
16223
 
                        first->with_wild, first->item_list,
16224
 
                        first->where,
16225
 
                        first->order_list.elements +
16226
 
                        first->group_list.elements,
16227
 
                        (order_st*) first->order_list.first,
16228
 
                        (order_st*) first->group_list.first,
16229
 
                        first->having,
16230
 
                        (order_st*) session->lex->proc_list.first,
16231
 
                        first->options | session->options | SELECT_DESCRIBE,
16232
 
                        result, unit, first);
16233
 
  }
16234
 
  return(res || session->is_error());
16235
 
}
16236
 
 
16237
 
 
16238
6731
static void print_table_array(Session *session, String *str, TableList **table,
16239
6732
                              TableList **end)
16240
6733
{
16241
 
  (*table)->print(session, str, QT_ORDINARY);
 
6734
  (*table)->print(session, str);
16242
6735
 
16243
6736
  for (TableList **tbl= table + 1; tbl < end; tbl++)
16244
6737
  {
16249
6742
      str->append(STRING_WITH_LEN(" left join "));
16250
6743
    }
16251
6744
    else if (curr->straight)
 
6745
    {
16252
6746
      str->append(STRING_WITH_LEN(" straight_join "));
16253
 
    else if (curr->sj_inner_tables)
16254
 
      str->append(STRING_WITH_LEN(" semi join "));
 
6747
    }
16255
6748
    else
 
6749
    {
16256
6750
      str->append(STRING_WITH_LEN(" join "));
16257
 
    curr->print(session, str, QT_ORDINARY);
 
6751
    }
 
6752
    curr->print(session, str);
16258
6753
    if (curr->on_expr)
16259
6754
    {
16260
6755
      str->append(STRING_WITH_LEN(" on("));
16261
 
      curr->on_expr->print(str, QT_ORDINARY);
 
6756
      curr->on_expr->print(str);
16262
6757
      str->append(')');
16263
6758
    }
16264
6759
  }
16265
6760
}
16266
6761
 
16267
 
 
16268
6762
/**
16269
6763
  Print joins from the FROM clause.
16270
 
  @param session     thread handler
 
6764
  @param session     thread Cursor
16271
6765
  @param str     string where table should be printed
16272
6766
  @param tables  list of tables in join
16273
 
  @query_type    type of the query is being generated
16274
6767
*/
16275
 
 
16276
 
static void print_join(Session *session, String *str,
16277
 
                       List<TableList> *tables, enum_query_type)
 
6768
void print_join(Session *session, String *str,
 
6769
                List<TableList> *tables)
16278
6770
{
16279
6771
  /* List is reversed => we should reverse it before using */
16280
 
  List_iterator_fast<TableList> ti(*tables);
16281
 
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
16282
 
                                                tables->elements);
16283
 
  if (table == 0)
16284
 
    return;  // out of memory
 
6772
  List<TableList>::iterator ti(tables->begin());
 
6773
  TableList **table= new (session->mem) TableList*[tables->size()];
16285
6774
 
16286
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
6775
  for (TableList **t= table + (tables->size() - 1); t >= table; t--)
 
6776
  {
16287
6777
    *t= ti++;
16288
 
 
16289
 
  /*
16290
 
    If the first table is a semi-join nest, swap it with something that is
16291
 
    not a semi-join nest.
16292
 
  */
16293
 
  if ((*table)->sj_inner_tables)
16294
 
  {
16295
 
    TableList **end= table + tables->elements;
16296
 
    for (TableList **t2= table; t2!=end; t2++)
16297
 
    {
16298
 
      if (!(*t2)->sj_inner_tables)
16299
 
      {
16300
 
        TableList *tmp= *t2;
16301
 
        *t2= *table;
16302
 
        *table= tmp;
16303
 
        break;
16304
 
      }
16305
 
    }
16306
 
  }
16307
 
  assert(tables->elements >= 1);
16308
 
  print_table_array(session, str, table, table + tables->elements);
16309
 
}
16310
 
 
16311
 
 
16312
 
/**
16313
 
  @brief Print an index hint
16314
 
 
16315
 
  @details Prints out the USE|FORCE|IGNORE index hint.
16316
 
 
16317
 
  @param      session         the current thread
16318
 
  @param[out] str         appends the index hint here
16319
 
  @param      hint        what the hint is (as string : "USE INDEX"|
16320
 
                          "FORCE INDEX"|"IGNORE INDEX")
16321
 
  @param      hint_length the length of the string in 'hint'
16322
 
  @param      indexes     a list of index names for the hint
16323
 
*/
16324
 
 
16325
 
void
16326
 
Index_hint::print(Session *session, String *str)
16327
 
{
16328
 
  switch (type)
16329
 
  {
16330
 
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
16331
 
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
16332
 
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
16333
 
  }
16334
 
  str->append (STRING_WITH_LEN(" ("));
16335
 
  if (key_name.length)
16336
 
  {
16337
 
    if (session && is_primary_key_name(key_name.str))
16338
 
      str->append(is_primary_key_name(key_name.str));
16339
 
    else
16340
 
      append_identifier(session, str, key_name.str, key_name.length);
16341
 
  }
16342
 
  str->append(')');
16343
 
}
16344
 
 
16345
 
 
16346
 
/**
16347
 
  Print table as it should be in join list.
16348
 
 
16349
 
  @param str   string where table should be printed
16350
 
*/
16351
 
 
16352
 
void TableList::print(Session *session, String *str, enum_query_type query_type)
16353
 
{
16354
 
  if (nested_join)
16355
 
  {
16356
 
    str->append('(');
16357
 
    print_join(session, str, &nested_join->join_list, query_type);
16358
 
    str->append(')');
16359
 
  }
16360
 
  else
16361
 
  {
16362
 
    const char *cmp_name;                         // Name to compare with alias
16363
 
    if (derived)
16364
 
    {
16365
 
      // A derived table
16366
 
      str->append('(');
16367
 
      derived->print(str, query_type);
16368
 
      str->append(')');
16369
 
      cmp_name= "";                               // Force printing of alias
16370
 
    }
16371
 
    else
16372
 
    {
16373
 
      // A normal table
16374
 
      {
16375
 
        append_identifier(session, str, db, db_length);
16376
 
        str->append('.');
16377
 
      }
16378
 
      if (schema_table)
16379
 
      {
16380
 
        append_identifier(session, str, schema_table_name,
16381
 
                          strlen(schema_table_name));
16382
 
        cmp_name= schema_table_name;
16383
 
      }
16384
 
      else
16385
 
      {
16386
 
        append_identifier(session, str, table_name, table_name_length);
16387
 
        cmp_name= table_name;
16388
 
      }
16389
 
    }
16390
 
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
16391
 
    {
16392
 
      char t_alias_buff[MAX_ALIAS_NAME];
16393
 
      const char *t_alias= alias;
16394
 
 
16395
 
      str->append(' ');
16396
 
      if (lower_case_table_names== 1)
16397
 
      {
16398
 
        if (alias && alias[0])
16399
 
        {
16400
 
          strcpy(t_alias_buff, alias);
16401
 
          my_casedn_str(files_charset_info, t_alias_buff);
16402
 
          t_alias= t_alias_buff;
16403
 
        }
16404
 
      }
16405
 
 
16406
 
      append_identifier(session, str, t_alias, strlen(t_alias));
16407
 
    }
16408
 
 
16409
 
    if (index_hints)
16410
 
    {
16411
 
      List_iterator<Index_hint> it(*index_hints);
16412
 
      Index_hint *hint;
16413
 
 
16414
 
      while ((hint= it++))
16415
 
      {
16416
 
        str->append (STRING_WITH_LEN(" "));
16417
 
        hint->print (session, str);
16418
 
      }
16419
 
    }
16420
 
  }
16421
 
}
16422
 
 
16423
 
 
16424
 
void st_select_lex::print(Session *session, String *str, enum_query_type query_type)
 
6778
  }
 
6779
  assert(tables->size() >= 1);
 
6780
  print_table_array(session, str, table, table + tables->size());
 
6781
}
 
6782
 
 
6783
void Select_Lex::print(Session *session, String *str)
16425
6784
{
16426
6785
  /* QQ: session may not be set for sub queries, but this should be fixed */
16427
 
  if (!session)
 
6786
  if (not session)
 
6787
  {
16428
6788
    session= current_session;
 
6789
  }
 
6790
 
16429
6791
 
16430
6792
  str->append(STRING_WITH_LEN("select "));
16431
6793
 
16432
6794
  /* First add options */
16433
6795
  if (options & SELECT_STRAIGHT_JOIN)
16434
6796
    str->append(STRING_WITH_LEN("straight_join "));
16435
 
  if ((session->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
16436
 
      (this == &session->lex->select_lex))
16437
 
    str->append(STRING_WITH_LEN("high_priority "));
 
6797
 
16438
6798
  if (options & SELECT_DISTINCT)
16439
6799
    str->append(STRING_WITH_LEN("distinct "));
 
6800
 
16440
6801
  if (options & SELECT_SMALL_RESULT)
16441
6802
    str->append(STRING_WITH_LEN("sql_small_result "));
 
6803
 
16442
6804
  if (options & SELECT_BIG_RESULT)
16443
6805
    str->append(STRING_WITH_LEN("sql_big_result "));
 
6806
 
16444
6807
  if (options & OPTION_BUFFER_RESULT)
16445
6808
    str->append(STRING_WITH_LEN("sql_buffer_result "));
 
6809
 
16446
6810
  if (options & OPTION_FOUND_ROWS)
16447
6811
    str->append(STRING_WITH_LEN("sql_calc_found_rows "));
16448
6812
 
16449
6813
  //Item List
16450
6814
  bool first= 1;
16451
 
  List_iterator_fast<Item> it(item_list);
 
6815
  List<Item>::iterator it(item_list.begin());
16452
6816
  Item *item;
16453
6817
  while ((item= it++))
16454
6818
  {
16455
6819
    if (first)
 
6820
    {
16456
6821
      first= 0;
 
6822
    }
16457
6823
    else
 
6824
    {
16458
6825
      str->append(',');
16459
 
    item->print_item_w_name(str, query_type);
 
6826
    }
 
6827
    item->print_item_w_name(str);
16460
6828
  }
16461
6829
 
16462
6830
  /*
16463
6831
    from clause
16464
 
    TODO: support USING/FORCE/IGNORE index
 
6832
    @todo support USING/FORCE/IGNORE index
16465
6833
  */
16466
 
  if (table_list.elements)
 
6834
  if (table_list.size())
16467
6835
  {
16468
6836
    str->append(STRING_WITH_LEN(" from "));
16469
6837
    /* go through join tree */
16470
 
    print_join(session, str, &top_join_list, query_type);
 
6838
    print_join(session, str, &top_join_list);
16471
6839
  }
16472
6840
  else if (where)
16473
6841
  {
16486
6854
  {
16487
6855
    str->append(STRING_WITH_LEN(" where "));
16488
6856
    if (cur_where)
16489
 
      cur_where->print(str, query_type);
 
6857
    {
 
6858
      cur_where->print(str);
 
6859
    }
16490
6860
    else
 
6861
    {
16491
6862
      str->append(cond_value != Item::COND_FALSE ? "1" : "0");
 
6863
    }
16492
6864
  }
16493
6865
 
16494
6866
  // group by & olap
16495
 
  if (group_list.elements)
 
6867
  if (group_list.size())
16496
6868
  {
16497
6869
    str->append(STRING_WITH_LEN(" group by "));
16498
 
    print_order(str, (order_st *) group_list.first, query_type);
 
6870
    print_order(str, (Order *) group_list.first);
16499
6871
    switch (olap)
16500
6872
    {
16501
6873
      case CUBE_TYPE:
16518
6890
  {
16519
6891
    str->append(STRING_WITH_LEN(" having "));
16520
6892
    if (cur_having)
16521
 
      cur_having->print(str, query_type);
 
6893
    {
 
6894
      cur_having->print(str);
 
6895
    }
16522
6896
    else
 
6897
    {
16523
6898
      str->append(having_value != Item::COND_FALSE ? "1" : "0");
 
6899
    }
16524
6900
  }
16525
6901
 
16526
 
  if (order_list.elements)
 
6902
  if (order_list.size())
16527
6903
  {
16528
6904
    str->append(STRING_WITH_LEN(" order by "));
16529
 
    print_order(str, (order_st *) order_list.first, query_type);
 
6905
    print_order(str, (Order *) order_list.first);
16530
6906
  }
16531
6907
 
16532
6908
  // limit
16533
 
  print_limit(session, str, query_type);
 
6909
  print_limit(session, str);
16534
6910
 
16535
6911
  // PROCEDURE unsupported here
16536
6912
}
16537
6913
 
16538
 
 
16539
 
/**
16540
 
  change select_result object of JOIN.
16541
 
 
16542
 
  @param res            new select_result object
16543
 
 
16544
 
  @retval
16545
 
    false   OK
16546
 
  @retval
16547
 
    true    error
16548
 
*/
16549
 
 
16550
 
bool JOIN::change_result(select_result *res)
16551
 
{
16552
 
  result= res;
16553
 
  if (result->prepare(fields_list, select_lex->master_unit()) ||
16554
 
                     result->prepare2())
16555
 
  {
16556
 
    return(true);
16557
 
  }
16558
 
  return(false);
16559
 
}
16560
 
 
16561
6914
/**
16562
6915
  @} (end of group Query_Optimizer)
16563
6916
*/
 
6917
 
 
6918
} /* namespace drizzled */