~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Monty Taylor
  • Date: 2009-03-08 23:45:12 UTC
  • mto: (923.2.1 mordred)
  • mto: This revision was merged to the branch mainline in revision 921.
  • Revision ID: mordred@inaugust.com-20090308234512-tqkygxtu1iaig23s
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/**
17
17
  @file
18
18
 
19
19
  @brief
20
 
  select_query and join optimization
 
20
  mysql_select and join optimization
 
21
 
21
22
 
22
23
  @defgroup Query_Optimizer  Query Optimizer
23
24
  @{
24
25
*/
25
 
#include <config.h>
26
 
 
27
 
#include <string>
28
 
#include <iostream>
29
 
#include <algorithm>
30
 
#include <vector>
31
 
 
32
 
#include <drizzled/sql_select.h> /* include join.h */
33
 
 
 
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>
34
32
#include <drizzled/error.h>
35
33
#include <drizzled/gettext.h>
36
34
#include <drizzled/util/test.h>
48
46
#include <drizzled/check_stack_overrun.h>
49
47
#include <drizzled/lock.h>
50
48
#include <drizzled/item/outer_ref.h>
51
 
#include <drizzled/index_hint.h>
52
 
#include <drizzled/records.h>
53
 
#include <drizzled/internal/iocache.h>
54
 
#include <drizzled/drizzled.h>
55
 
#include <drizzled/plugin/storage_engine.h>
56
 
 
57
 
#include <drizzled/sql_union.h>
58
 
#include <drizzled/optimizer/key_field.h>
59
 
#include <drizzled/optimizer/position.h>
60
 
#include <drizzled/optimizer/sargable_param.h>
61
 
#include <drizzled/optimizer/key_use.h>
62
 
#include <drizzled/optimizer/range.h>
63
 
#include <drizzled/optimizer/quick_range_select.h>
64
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
65
 
 
66
 
#include <drizzled/filesort.h>
67
 
#include <drizzled/sql_lex.h>
68
 
#include <drizzled/session.h>
69
 
#include <drizzled/sort_field.h>
70
 
#include <drizzled/select_result.h>
 
49
 
 
50
#include <string>
71
51
 
72
52
using namespace std;
73
53
 
74
 
namespace drizzled
75
 
{
76
 
 
77
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
 
54
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
55
                              "MAYBE_REF","ALL","range","index",
 
56
                              "ref_or_null","unique_subquery","index_subquery",
 
57
                              "index_merge"
 
58
};
 
59
 
 
60
struct st_sargable_param;
 
61
 
 
62
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
63
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
64
                                 DYNAMIC_ARRAY *keyuse);
 
65
static bool update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,
 
66
                                JOIN_TAB *join_tab,
 
67
                                uint32_t tables, COND *conds,
 
68
                                COND_EQUAL *cond_equal,
 
69
                                table_map table_map, Select_Lex *select_lex,
 
70
                                st_sargable_param **sargables);
 
71
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
72
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
73
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
74
                               table_map used_tables);
 
75
static bool choose_plan(JOIN *join,table_map join_tables);
 
76
 
 
77
static void best_access_path(JOIN *join, JOIN_TAB *s, Session *session,
 
78
                             table_map remaining_tables, uint32_t idx,
 
79
                             double record_count, double read_time);
 
80
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
81
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
82
                             uint32_t depth, uint32_t prune_level);
 
83
static bool best_extension_by_limited_search(JOIN *join,
 
84
                                             table_map remaining_tables,
 
85
                                             uint32_t idx, double record_count,
 
86
                                             double read_time, uint32_t depth,
 
87
                                             uint32_t prune_level);
 
88
static uint32_t determine_search_depth(JOIN* join);
 
89
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
 
90
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
91
/*
 
92
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
93
  tested and approved.
 
94
*/
 
95
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
96
                      double record_count,double read_time);
 
97
static uint32_t cache_record_length(JOIN *join,uint32_t index);
 
98
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
99
static bool get_best_combination(JOIN *join);
 
100
static store_key *get_store_key(Session *session,
 
101
                                KEYUSE *keyuse, table_map used_tables,
 
102
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
 
103
                                uint32_t maybe_null);
 
104
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
105
static void make_outerjoin_info(JOIN *join);
 
106
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
107
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
108
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
109
static void update_depend_map(JOIN *join);
 
110
static void update_depend_map(JOIN *join, order_st *order);
 
111
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
112
                           bool change_list, bool *simple_order);
 
113
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
114
                            List<Item> &fields, bool send_row,
 
115
                            uint64_t select_options, const char *info,
 
116
                            Item *having);
78
117
static COND *build_equal_items(Session *session, COND *cond,
79
118
                               COND_EQUAL *inherited,
80
119
                               List<TableList> *join_list,
81
120
                               COND_EQUAL **cond_equal_ref);
82
 
 
 
121
static COND* substitute_for_best_equal_field(COND *cond,
 
122
                                             COND_EQUAL *cond_equal,
 
123
                                             void *table_join_idx);
 
124
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
125
                            COND *conds, bool top, bool in_sj);
 
126
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
127
static void restore_prev_nj_state(JOIN_TAB *last);
 
128
static void reset_nj_counters(List<TableList> *join_list);
 
129
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
130
                                          uint32_t first_unused);
 
131
 
 
132
static
 
133
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
134
static void restore_prev_sj_state(const table_map remaining_tables,
 
135
                                  const JOIN_TAB *tab);
 
136
 
 
137
static COND *optimize_cond(JOIN *join, COND *conds,
 
138
                           List<TableList> *join_list,
 
139
                           Item::cond_result *cond_value);
 
140
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
141
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
142
 
 
143
static enum_nested_loop_state
 
144
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
145
                     int error);
 
146
static enum_nested_loop_state
 
147
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
148
static enum_nested_loop_state
 
149
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
150
static enum_nested_loop_state
 
151
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
152
static enum_nested_loop_state
 
153
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
154
static enum_nested_loop_state
 
155
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
156
static enum_nested_loop_state
 
157
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
158
 
 
159
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
160
static int join_read_system(JOIN_TAB *tab);
 
161
static int join_read_const(JOIN_TAB *tab);
 
162
static int join_read_key(JOIN_TAB *tab);
 
163
static int join_read_always_key(JOIN_TAB *tab);
 
164
static int join_read_last_key(JOIN_TAB *tab);
 
165
static int join_no_more_records(READ_RECORD *info);
 
166
static int join_read_next(READ_RECORD *info);
 
167
static int join_read_next_different(READ_RECORD *info);
 
168
static int join_init_quick_read_record(JOIN_TAB *tab);
 
169
static int test_if_quick_select(JOIN_TAB *tab);
 
170
static int join_init_read_record(JOIN_TAB *tab);
 
171
static int join_read_first(JOIN_TAB *tab);
 
172
static int join_read_next_same(READ_RECORD *info);
 
173
static int join_read_next_same_diff(READ_RECORD *info);
 
174
static int join_read_last(JOIN_TAB *tab);
 
175
static int join_read_prev_same(READ_RECORD *info);
 
176
static int join_read_prev(READ_RECORD *info);
 
177
int join_read_always_key_or_null(JOIN_TAB *tab);
 
178
int join_read_next_same_or_null(READ_RECORD *info);
 
179
static COND *make_cond_for_table(COND *cond,table_map table,
 
180
                                 table_map used_table,
 
181
                                 bool exclude_expensive_cond);
83
182
static Item* part_of_refkey(Table *form,Field *field);
84
 
static bool cmp_buffer_with_ref(JoinTable *tab);
85
 
static void change_cond_ref_to_const(Session *session,
86
 
                                     list<COND_CMP>& save_list,
87
 
                                     Item *and_father,
88
 
                                     Item *cond,
89
 
                                     Item *field,
90
 
                                     Item *value);
91
 
static bool copy_blobs(Field **ptr);
 
183
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
184
                                    ha_rows select_limit, bool no_changes,
 
185
                                    const key_map *map);
 
186
static bool list_contains_unique_index(Table *table,
 
187
                          bool (*find_func) (Field *, void *), void *data);
 
188
static bool find_field_in_item_list (Field *field, void *data);
 
189
static bool find_field_in_order_list (Field *field, void *data);
 
190
static int create_sort_index(Session *session, JOIN *join, order_st *order,
 
191
                             ha_rows filesort_limit, ha_rows select_limit,
 
192
                             bool is_order_by);
 
193
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
194
                             Item *having);
 
195
static int remove_dup_with_compare(Session *session, Table *entry, Field **field,
 
196
                                   uint32_t offset, Item *having);
 
197
static int remove_dup_with_hash_index(Session *session,Table *table,
 
198
                                      uint32_t field_count, Field **first_field,
 
199
                                      uint32_t key_length, Item *having);
 
200
static int join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count);
 
201
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
202
static bool store_record_in_cache(JOIN_CACHE *cache);
 
203
static void reset_cache_read(JOIN_CACHE *cache);
 
204
static void reset_cache_write(JOIN_CACHE *cache);
 
205
static void read_cached_record(JOIN_TAB *tab);
 
206
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
207
static order_st *create_distinct_group(Session *session, Item **ref_pointer_array,
 
208
                                    order_st *order, List<Item> &fields,
 
209
                                    List<Item> &all_fields,
 
210
                                    bool *all_order_by_fields_used);
 
211
static bool test_if_subpart(order_st *a,order_st *b);
 
212
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
213
static void calc_group_buffer(JOIN *join,order_st *group);
 
214
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
215
static bool alloc_group_fields(JOIN *join,order_st *group);
 
216
// Create list for using with tempory table
 
217
static bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
 
218
                                     List<Item> &new_list1,
 
219
                                     List<Item> &new_list2,
 
220
                                     uint32_t elements, List<Item> &items);
 
221
// Create list for using with tempory table
 
222
static bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
223
                                      List<Item> &new_list1,
 
224
                                      List<Item> &new_list2,
 
225
                                      uint32_t elements, List<Item> &items);
 
226
static void init_tmptable_sum_functions(Item_sum **func);
 
227
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
228
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
229
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
 
230
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
 
231
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
232
static bool update_sum_func(Item_sum **func);
 
233
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
234
                            bool distinct, const char *message=NULL);
 
235
static Item *remove_additional_cond(Item* conds);
 
236
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
237
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
238
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
239
                                       Item *new_cond, bool fix_fields);
92
240
 
93
241
static bool eval_const_cond(COND *cond)
94
242
{
95
243
    return ((Item_func*) cond)->val_int() ? true : false;
96
244
}
97
245
 
 
246
 
98
247
/*
99
248
  This is used to mark equalities that were made from i-th IN-equality.
100
249
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
103
252
const char *subq_sj_cond_name=
104
253
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
254
 
106
 
static bool copy_blobs(Field **ptr)
 
255
static bool bitmap_covers(const table_map x, const table_map y)
107
256
{
108
 
  for (; *ptr ; ptr++)
109
 
  {
110
 
    if ((*ptr)->flags & BLOB_FLAG)
111
 
      if (((Field_blob *) (*ptr))->copy())
112
 
        return 1;                               // Error
113
 
  }
114
 
  return 0;
 
257
  return !test(y & ~x);
115
258
}
116
259
 
117
260
/**
118
261
  This handles SELECT with and without UNION.
119
262
*/
 
263
 
120
264
bool handle_select(Session *session, LEX *lex, select_result *result,
121
265
                   uint64_t setup_tables_done_option)
122
266
{
123
267
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
268
  register Select_Lex *select_lex = &lex->select_lex;
 
269
  DRIZZLE_SELECT_START();
126
270
 
127
271
  if (select_lex->master_unit()->is_union() ||
128
272
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
273
    res= mysql_union(session, lex, result, &lex->unit, setup_tables_done_option);
133
274
  else
134
275
  {
135
276
    Select_Lex_Unit *unit= &lex->unit;
136
277
    unit->set_limit(unit->global_parameters);
137
278
    session->session_marker= 0;
138
279
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
 
280
      'options' of mysql_select will be set in JOIN, as far as JOIN for
140
281
      every PS/SP execution new, we will not need reset this flag if
141
282
      setup_tables_done_option changed for next rexecution
142
283
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
 
284
    res= mysql_select(session, &select_lex->ref_pointer_array,
145
285
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
286
                      select_lex->with_wild, select_lex->item_list,
148
287
                      select_lex->where,
149
288
                      select_lex->order_list.elements +
150
289
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
290
                      (order_st*) select_lex->order_list.first,
 
291
                      (order_st*) select_lex->group_list.first,
153
292
                      select_lex->having,
 
293
                      (order_st*) lex->proc_list.first,
154
294
                      select_lex->options | session->options |
155
295
                      setup_tables_done_option,
156
296
                      result, unit, select_lex);
159
299
  if (unlikely(res))
160
300
    result->abort();
161
301
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
302
  DRIZZLE_SELECT_END();
 
303
  return(res);
164
304
}
165
305
 
 
306
 
166
307
/*
167
308
  Fix fields referenced from inner selects.
168
309
 
203
344
    true  an error occured
204
345
    false ok
205
346
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
347
 
 
348
bool
 
349
fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
 
350
                 Item **ref_pointer_array)
210
351
{
211
352
  Item_outer_ref *ref;
212
353
  bool res= false;
213
354
  bool direct_ref= false;
214
355
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
356
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
357
  while ((ref= ref_it++))
217
358
  {
218
359
    Item *item= ref->outer_ref;
219
360
    Item **item_ref= ref->ref;
220
361
    Item_ref *new_ref;
221
362
    /*
222
 
      @todo this field item already might be present in the select list.
 
363
      TODO: this field item already might be present in the select list.
223
364
      In this case instead of adding new field item we could use an
224
365
      existing one. The change will lead to less operations for copying fields,
225
366
      smaller temporary tables and less data passed through filesort.
273
414
  return res;
274
415
}
275
416
 
 
417
/**
 
418
  Function to setup clauses without sum functions.
 
419
*/
 
420
inline int setup_without_group(Session *session, Item **ref_pointer_array,
 
421
                               TableList *tables,
 
422
                               TableList *leaves,
 
423
                               List<Item> &fields,
 
424
                               List<Item> &all_fields,
 
425
                               COND **conds,
 
426
                               order_st *order,
 
427
                               order_st *group, bool *hidden_group_fields)
 
428
{
 
429
  int res;
 
430
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
431
 
 
432
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
433
  res= setup_conds(session, tables, leaves, conds);
 
434
 
 
435
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
436
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
 
437
                          order);
 
438
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
439
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
 
440
                          group, hidden_group_fields);
 
441
  session->lex->allow_sum_func= save_allow_sum_func;
 
442
  return(res);
 
443
}
 
444
 
276
445
/*****************************************************************************
277
446
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
447
  mysql_select assumes that all tables are already opened
279
448
*****************************************************************************/
280
449
 
 
450
/**
 
451
  Prepare of whole select (including sub queries in future).
 
452
 
 
453
  @todo
 
454
    Add check of calculation of GROUP functions and fields:
 
455
    SELECT COUNT(*)+table.col1 from table1;
 
456
 
 
457
  @retval
 
458
    -1   on error
 
459
  @retval
 
460
    0   on success
 
461
*/
 
462
int
 
463
JOIN::prepare(Item ***rref_pointer_array,
 
464
              TableList *tables_init,
 
465
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
466
              order_st *order_init, order_st *group_init,
 
467
              Item *having_init,
 
468
              order_st *proc_param_init, Select_Lex *select_lex_arg,
 
469
              Select_Lex_Unit *unit_arg)
 
470
{
 
471
  // to prevent double initialization on EXPLAIN
 
472
  if (optimized)
 
473
    return(0);
 
474
 
 
475
  conds= conds_init;
 
476
  order= order_init;
 
477
  group_list= group_init;
 
478
  having= having_init;
 
479
  proc_param= proc_param_init;
 
480
  tables_list= tables_init;
 
481
  select_lex= select_lex_arg;
 
482
  select_lex->join= this;
 
483
  join_list= &select_lex->top_join_list;
 
484
  union_part= unit_arg->is_union();
 
485
 
 
486
  session->lex->current_select->is_item_list_lookup= 1;
 
487
  /*
 
488
    If we have already executed SELECT, then it have not sense to prevent
 
489
    its table from update (see unique_table())
 
490
  */
 
491
  if (session->derived_tables_processing)
 
492
    select_lex->exclude_from_table_unique_test= true;
 
493
 
 
494
  /* Check that all tables, fields, conds and order are ok */
 
495
 
 
496
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
497
      setup_tables_and_check_access(session, &select_lex->context, join_list,
 
498
                                    tables_list, &select_lex->leaf_tables,
 
499
                                    false))
 
500
      return(-1);
 
501
 
 
502
  TableList *table_ptr;
 
503
  for (table_ptr= select_lex->leaf_tables;
 
504
       table_ptr;
 
505
       table_ptr= table_ptr->next_leaf)
 
506
    tables++;
 
507
 
 
508
  if (setup_wild(session, tables_list, fields_list, &all_fields, wild_num) ||
 
509
      select_lex->setup_ref_array(session, og_num) ||
 
510
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
511
                   &all_fields, 1) ||
 
512
      setup_without_group(session, (*rref_pointer_array), tables_list,
 
513
                          select_lex->leaf_tables, fields_list,
 
514
                          all_fields, &conds, order, group_list,
 
515
                          &hidden_group_fields))
 
516
    return(-1);                         /* purecov: inspected */
 
517
 
 
518
  ref_pointer_array= *rref_pointer_array;
 
519
 
 
520
  if (having)
 
521
  {
 
522
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
523
    session->where="having clause";
 
524
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
525
    select_lex->having_fix_field= 1;
 
526
    bool having_fix_rc= (!having->fixed &&
 
527
                         (having->fix_fields(session, &having) ||
 
528
                          having->check_cols(1)));
 
529
    select_lex->having_fix_field= 0;
 
530
    if (having_fix_rc || session->is_error())
 
531
      return(-1);                               /* purecov: inspected */
 
532
    session->lex->allow_sum_func= save_allow_sum_func;
 
533
  }
 
534
 
 
535
  {
 
536
    Item_subselect *subselect;
 
537
    Item_in_subselect *in_subs= NULL;
 
538
    /*
 
539
      Are we in a subquery predicate?
 
540
      TODO: the block below will be executed for every PS execution without need.
 
541
    */
 
542
    if ((subselect= select_lex->master_unit()->item))
 
543
    {
 
544
      bool do_semijoin= !test(session->variables.optimizer_switch &
 
545
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
546
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
547
        in_subs= (Item_in_subselect*)subselect;
 
548
 
 
549
      /*
 
550
        Check if we're in subquery that is a candidate for flattening into a
 
551
        semi-join (which is done done in flatten_subqueries()). The
 
552
        requirements are:
 
553
          1. Subquery predicate is an IN/=ANY subq predicate
 
554
          2. Subquery is a single SELECT (not a UNION)
 
555
          3. Subquery does not have GROUP BY or order_st BY
 
556
          4. Subquery does not use aggregate functions or HAVING
 
557
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
558
          6. No execution method was already chosen (by a prepared statement).
 
559
 
 
560
          (*). We are not in a subquery of a single table UPDATE/DELETE that
 
561
               doesn't have a JOIN (TODO: We should handle this at some
 
562
               point by switching to multi-table UPDATE/DELETE)
 
563
 
 
564
          (**). We're not in a confluent table-less subquery, like
 
565
                "SELECT 1".
 
566
      */
 
567
      if (in_subs &&                                                    // 1
 
568
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
569
          !select_lex->group_list.elements && !order &&                 // 3
 
570
          !having && !select_lex->with_sum_func &&                      // 4
 
571
          session->session_marker &&                                            // 5
 
572
          select_lex->outer_select()->join &&                           // (*)
 
573
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
 
574
          do_semijoin &&
 
575
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
576
      {
 
577
        {
 
578
          if (!in_subs->left_expr->fixed &&
 
579
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
 
580
          {
 
581
            return(-1);
 
582
          }
 
583
          /*
 
584
            Check that the right part of the subselect contains no more than one
 
585
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
586
          */
 
587
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
588
             (select_lex->item_list.elements !=
 
589
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
590
          {
 
591
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
592
            return(-1);
 
593
          }
 
594
        }
 
595
 
 
596
        /* Register the subquery for further processing */
 
597
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
 
598
        in_subs->expr_join_nest= (TableList*)session->session_marker;
 
599
      }
 
600
      else
 
601
      {
 
602
        bool do_materialize= !test(session->variables.optimizer_switch &
 
603
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
604
        /*
 
605
          Check if the subquery predicate can be executed via materialization.
 
606
          The required conditions are:
 
607
          1. Subquery predicate is an IN/=ANY subq predicate
 
608
          2. Subquery is a single SELECT (not a UNION)
 
609
          3. Subquery is not a table-less query. In this case there is no
 
610
             point in materializing.
 
611
          4. Subquery predicate is a top-level predicate
 
612
             (this implies it is not negated)
 
613
             TODO: this is a limitation that should be lifeted once we
 
614
             implement correct NULL semantics (WL#3830)
 
615
          5. Subquery is non-correlated
 
616
             TODO:
 
617
             This is an overly restrictive condition. It can be extended to:
 
618
             (Subquery is non-correlated ||
 
619
              Subquery is correlated to any query outer to IN predicate ||
 
620
              (Subquery is correlated to the immediate outer query &&
 
621
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
622
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
623
          6. No execution method was already chosen (by a prepared statement).
 
624
 
 
625
          (*) The subquery must be part of a SELECT statement. The current
 
626
               condition also excludes multi-table update statements.
 
627
 
 
628
          We have to determine whether we will perform subquery materialization
 
629
          before calling the IN=>EXISTS transformation, so that we know whether to
 
630
          perform the whole transformation or only that part of it which wraps
 
631
          Item_in_subselect in an Item_in_optimizer.
 
632
        */
 
633
        if (do_materialize &&
 
634
            in_subs  &&                                                   // 1
 
635
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
636
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
637
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
638
        {
 
639
          if (in_subs->is_top_level_item() &&                             // 4
 
640
              !in_subs->is_correlated &&                                  // 5
 
641
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
642
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
643
        }
 
644
 
 
645
        Item_subselect::trans_res trans_res;
 
646
        if ((trans_res= subselect->select_transformer(this)) !=
 
647
            Item_subselect::RES_OK)
 
648
        {
 
649
          return((trans_res == Item_subselect::RES_ERROR));
 
650
        }
 
651
      }
 
652
    }
 
653
  }
 
654
 
 
655
  if (order)
 
656
  {
 
657
    order_st *ord;
 
658
    for (ord= order; ord; ord= ord->next)
 
659
    {
 
660
      Item *item= *ord->item;
 
661
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
662
        item->split_sum_func(session, ref_pointer_array, all_fields);
 
663
    }
 
664
  }
 
665
 
 
666
  if (having && having->with_sum_func)
 
667
    having->split_sum_func(session, ref_pointer_array, all_fields,
 
668
                           &having, true);
 
669
  if (select_lex->inner_sum_func_list)
 
670
  {
 
671
    Item_sum *end=select_lex->inner_sum_func_list;
 
672
    Item_sum *item_sum= end;
 
673
    do
 
674
    {
 
675
      item_sum= item_sum->next;
 
676
      item_sum->split_sum_func(session, ref_pointer_array,
 
677
                               all_fields, item_sum->ref_by, false);
 
678
    } while (item_sum != end);
 
679
  }
 
680
 
 
681
  if (select_lex->inner_refs_list.elements &&
 
682
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
 
683
    return(-1);
 
684
 
 
685
  /*
 
686
    Check if there are references to un-aggregated columns when computing
 
687
    aggregate functions with implicit grouping (there is no GROUP BY).
 
688
 
 
689
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
690
  */
 
691
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
692
  {
 
693
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
694
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
695
    return(-1);
 
696
  }
 
697
  {
 
698
    /* Caclulate the number of groups */
 
699
    send_group_parts= 0;
 
700
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
701
      send_group_parts++;
 
702
  }
 
703
 
 
704
  if (error)
 
705
    goto err;                                   /* purecov: inspected */
 
706
 
 
707
  if (result && result->prepare(fields_list, unit_arg))
 
708
    goto err;                                   /* purecov: inspected */
 
709
 
 
710
  /* Init join struct */
 
711
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
712
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
713
  this->group= group_list != 0;
 
714
  unit= unit_arg;
 
715
 
 
716
#ifdef RESTRICTED_GROUP
 
717
  if (sum_func_count && !group_list && (func_count || field_count))
 
718
  {
 
719
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
720
    goto err;
 
721
  }
 
722
#endif
 
723
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
724
    goto err;
 
725
  if (alloc_func_list())
 
726
    goto err;
 
727
 
 
728
  return(0); // All OK
 
729
 
 
730
err:
 
731
  return(-1);                           /* purecov: inspected */
 
732
}
 
733
 
 
734
 
 
735
/*
 
736
  Remove the predicates pushed down into the subquery
 
737
 
 
738
  SYNOPSIS
 
739
    JOIN::remove_subq_pushed_predicates()
 
740
      where   IN  Must be NULL
 
741
              OUT The remaining WHERE condition, or NULL
 
742
 
 
743
  DESCRIPTION
 
744
    Given that this join will be executed using (unique|index)_subquery,
 
745
    without "checking NULL", remove the predicates that were pushed down
 
746
    into the subquery.
 
747
 
 
748
    If the subquery compares scalar values, we can remove the condition that
 
749
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
750
    engine)
 
751
 
 
752
    If the subquery compares row values, we need to keep the wrapped
 
753
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
754
    and non-NULL values, we'll do a full table scan and will rely on the
 
755
    equalities corresponding to non-NULL parts of left tuple to filter out
 
756
    non-matching records.
 
757
 
 
758
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
759
    fact that subquery engine will be using index lookup. This must be done only
 
760
    for cases where there are no conversion errors of significance, e.g. 257
 
761
    that is searched in a byte. But this requires homogenization of the return
 
762
    codes of all Field*::store() methods.
 
763
*/
 
764
 
 
765
void JOIN::remove_subq_pushed_predicates(Item **where)
 
766
{
 
767
  if (conds->type() == Item::FUNC_ITEM &&
 
768
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
769
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
770
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
771
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
772
                   ((Item_func *)conds)->arguments()[0]))
 
773
  {
 
774
    *where= 0;
 
775
    return;
 
776
  }
 
777
}
 
778
 
 
779
 
281
780
/*
282
781
  Index lookup-based subquery: save some flags for EXPLAIN output
283
782
 
296
795
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
796
    and set appropriate flags in join_tab->packed_info.
298
797
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
798
 
 
799
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
800
{
301
801
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
802
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
803
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
804
  if (where)
305
805
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
313
813
  }
314
814
}
315
815
 
 
816
 
 
817
 
 
818
 
 
819
/*
 
820
  Check if the table's rowid is included in the temptable
 
821
 
 
822
  SYNOPSIS
 
823
    sj_table_is_included()
 
824
      join      The join
 
825
      join_tab  The table to be checked
 
826
 
 
827
  DESCRIPTION
 
828
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
829
    in the temptable. This is so if
 
830
 
 
831
    1. The table is not embedded within some semi-join nest
 
832
    2. The has been pulled out of a semi-join nest, or
 
833
 
 
834
    3. The table is functionally dependent on some previous table
 
835
 
 
836
    [4. This is also true for constant tables that can't be
 
837
        NULL-complemented but this function is not called for such tables]
 
838
 
 
839
  RETURN
 
840
    true  - Include table's rowid
 
841
    false - Don't
 
842
*/
 
843
 
 
844
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
845
{
 
846
  if (join_tab->emb_sj_nest)
 
847
    return false;
 
848
 
 
849
  /* Check if this table is functionally dependent on the tables that
 
850
     are within the same outer join nest
 
851
  */
 
852
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
853
  if (join_tab->type == JT_EQ_REF)
 
854
  {
 
855
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
856
    uint32_t idx;
 
857
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
858
    {
 
859
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
860
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
861
        return true;
 
862
    }
 
863
    /* Ok, functionally dependent */
 
864
    return false;
 
865
  }
 
866
  /* Not functionally dependent => need to include*/
 
867
  return true;
 
868
}
 
869
 
 
870
 
 
871
/*
 
872
  Setup the strategies to eliminate semi-join duplicates.
 
873
 
 
874
  SYNOPSIS
 
875
    setup_semijoin_dups_elimination()
 
876
      join           Join to process
 
877
      options        Join options (needed to see if join buffering will be
 
878
                     used or not)
 
879
      no_jbuf_after  Another bit of information re where join buffering will
 
880
                     be used.
 
881
 
 
882
  DESCRIPTION
 
883
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
884
    strategies:
 
885
 
 
886
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
887
                         of row combinations)
 
888
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
889
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
890
                  together and picking the 1st one)
 
891
 
 
892
    The join order has "duplicate-generating ranges", and every range is
 
893
    served by one strategy or a combination of FirstMatch with with some
 
894
    other strategy.
 
895
 
 
896
    "Duplicate-generating range" is defined as a range within the join order
 
897
    that contains all of the inner tables of a semi-join. All ranges must be
 
898
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
899
    are joined together, which is equivalent to converting
 
900
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
901
    to
 
902
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
903
    .
 
904
 
 
905
    Applicability conditions are as follows:
 
906
 
 
907
    DuplicateWeedout strategy
 
908
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
909
 
 
910
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
911
      +------+  +=========================+  +---+
 
912
        (1)                 (2)               (3)
 
913
 
 
914
       (1) - Prefix of OuterTables (those that participate in
 
915
             IN-equality and/or are correlated with subquery) and outer
 
916
             Noncorrelated Tables.
 
917
       (2) - The handled range. The range starts with the first sj-inner
 
918
             table, and covers all sj-inner and outer tables
 
919
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
920
             may follow in any order.
 
921
       (3) - The suffix of outer Noncorrelated tables.
 
922
 
 
923
    FirstMatch strategy
 
924
    ~~~~~~~~~~~~~~~~~~~
 
925
 
 
926
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
927
      +------+  +==================+  +---+
 
928
        (1)             (2)          (3)
 
929
 
 
930
      (1) - Prefix of outer and non-correlated tables
 
931
      (2) - The handled range, which may contain only inner and
 
932
            non-correlated tables.
 
933
      (3) - The suffix of outer Noncorrelated tables.
 
934
 
 
935
    InsideOut strategy
 
936
    ~~~~~~~~~~~~~~~~~~
 
937
 
 
938
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
939
     +--------+   +===========+ +=============+   +------+
 
940
        (1)           (2)          (3)              (4)
 
941
 
 
942
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
943
            all the non-trivially correlated outer tables. (non-trivially means
 
944
            that the correlation is not just through the IN-equality).
 
945
 
 
946
      (2) - Inner table for which the InsideOut scan is performed.
 
947
 
 
948
      (3) - The remainder of the duplicate-generating range. It is served by
 
949
            application of FirstMatch strategy, with the exception that
 
950
            outer IN-correlated tables are considered to be non-correlated.
 
951
 
 
952
      (4) - THe suffix of outer and outer non-correlated tables.
 
953
 
 
954
    If several strategies are applicable, their relative priorities are:
 
955
      1. InsideOut
 
956
      2. FirstMatch
 
957
      3. DuplicateWeedout
 
958
 
 
959
    This function walks over the join order and sets up the strategies by
 
960
    setting appropriate members in join_tab structures.
 
961
 
 
962
  RETURN
 
963
    false  OK
 
964
    true   Out of memory error
 
965
*/
 
966
 
 
967
static
 
968
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
969
{
 
970
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
971
  struct {
 
972
    /*
 
973
      0 - invalid (EOF marker),
 
974
      1 - InsideOut,
 
975
      2 - Temptable (maybe confluent),
 
976
      3 - Temptable with join buffering
 
977
    */
 
978
    uint32_t strategy;
 
979
    uint32_t start_idx; /* Left range bound */
 
980
    uint32_t end_idx;   /* Right range bound */
 
981
    /*
 
982
      For Temptable strategy: Bitmap of all outer and correlated tables from
 
983
      all involved join nests.
 
984
    */
 
985
    table_map outer_tables;
 
986
  } dups_ranges [MAX_TABLES];
 
987
 
 
988
  TableList *emb_insideout_nest= NULL;
 
989
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
990
                               tables) whose ranges we're in */
 
991
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
992
  table_map range_start_map= 0; /* table_map at current range start */
 
993
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
994
  int cur_range= 0;
 
995
  uint32_t i;
 
996
 
 
997
  /*
 
998
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
999
  */
 
1000
  for (i=join->const_tables ; i < join->tables ; i++)
 
1001
  {
 
1002
    JOIN_TAB *tab=join->join_tab+i;
 
1003
    Table *table=tab->table;
 
1004
    cur_map |= table->map;
 
1005
 
 
1006
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
1007
    {
 
1008
      if (!emb_sj_map)
 
1009
      {
 
1010
        dups_ranges[cur_range].start_idx= i;
 
1011
        range_start_map= cur_map & ~table->map;
 
1012
        /*
 
1013
          Remember if this is a possible start of range that is covered by
 
1014
          the InsideOut strategy (the reason that it is not covered could
 
1015
          be that it overlaps with anther semi-join's range. we don't
 
1016
          support InsideOut for joined ranges)
 
1017
        */
 
1018
        if (join->best_positions[i].use_insideout_scan)
 
1019
          emb_insideout_nest= tab->emb_sj_nest;
 
1020
      }
 
1021
 
 
1022
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1023
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1024
 
 
1025
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1026
      {
 
1027
        /*
 
1028
          Two different semi-joins interleave. This cannot be handled by
 
1029
          InsideOut strategy.
 
1030
        */
 
1031
        emb_insideout_nest= NULL;
 
1032
      }
 
1033
    }
 
1034
 
 
1035
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1036
    {
 
1037
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1038
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1039
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1040
      {
 
1041
        /*
 
1042
          This table uses join buffering, which makes use of FirstMatch or
 
1043
          InsideOut strategies impossible for the current and (we assume)
 
1044
          preceding duplicate-producing ranges.
 
1045
          That is, for the join order:
 
1046
 
 
1047
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1048
                  |     |     |     |          | \
 
1049
                  +-----+     +-----+          |  join buffering use
 
1050
                     r1          r2         we're here
 
1051
 
 
1052
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1053
          strategy that spans all the tables, starting from the very 1st
 
1054
          one.
 
1055
        */
 
1056
        dealing_with_jbuf= true;
 
1057
        emb_insideout_nest= false;
 
1058
 
 
1059
        /*
 
1060
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1061
          do not matter:
 
1062
        */
 
1063
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1064
        {
 
1065
          dups_ranges[cur_range].outer_tables |=
 
1066
            dups_ranges[prev_range].outer_tables;
 
1067
        }
 
1068
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1069
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1070
        cur_range=  0;
 
1071
      }
 
1072
 
 
1073
      /*
 
1074
        Check if we are at the end of duplicate-producing range. We are if
 
1075
 
 
1076
        1. It's an InsideOut range (which presumes all correlated tables are
 
1077
           in the prefix), and all inner tables are in the join order prefix,
 
1078
           or
 
1079
        2. It's a DuplicateElimination range (possibly covering several
 
1080
           SJ-nests), and all inner, outer, and correlated tables of all
 
1081
           sj-nests are in the join order prefix.
 
1082
      */
 
1083
      bool end_of_range= false;
 
1084
      if (emb_insideout_nest &&
 
1085
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1086
      {
 
1087
        /* Save that this range is handled with InsideOut: */
 
1088
        dups_ranges[cur_range].strategy= 1;
 
1089
        end_of_range= true;
 
1090
      }
 
1091
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1092
      {
 
1093
        /*
 
1094
          This is a complete range to be handled with either DuplicateWeedout
 
1095
          or FirstMatch
 
1096
        */
 
1097
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1098
        /*
 
1099
          This will hold tables from within the range that need to be put
 
1100
          into the join buffer before we can use the FirstMatch on its tail.
 
1101
        */
 
1102
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
 
1103
                                             ~range_start_map;
 
1104
        end_of_range= true;
 
1105
      }
 
1106
 
 
1107
      if (end_of_range)
 
1108
      {
 
1109
        dups_ranges[cur_range].end_idx= i+1;
 
1110
        emb_sj_map= emb_outer_tables= 0;
 
1111
        emb_insideout_nest= NULL;
 
1112
        dealing_with_jbuf= false;
 
1113
        dups_ranges[++cur_range].strategy= 0;
 
1114
      }
 
1115
    }
 
1116
  }
 
1117
 
 
1118
  Session *session= join->session;
 
1119
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1120
  /*
 
1121
    Second pass: setup the chosen strategies
 
1122
  */
 
1123
  for (int j= 0; j < cur_range; j++)
 
1124
  {
 
1125
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1126
    JOIN_TAB *jump_to;
 
1127
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1128
    {
 
1129
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1130
      jump_to= tab++;
 
1131
    }
 
1132
    else // DuplicateWeedout strategy
 
1133
    {
 
1134
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1135
      table_map weed_cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1136
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1137
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1138
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1139
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1140
      JOIN_TAB *last_outer_tab= tab - 1;
 
1141
      /*
 
1142
        Walk through the range and remember
 
1143
         - tables that need their rowids to be put into temptable
 
1144
         - the last outer table
 
1145
      */
 
1146
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1147
      {
 
1148
        if (sj_table_is_included(join, tab))
 
1149
        {
 
1150
          last_tab->join_tab= tab;
 
1151
          last_tab->rowid_offset= jt_rowid_offset;
 
1152
          jt_rowid_offset += tab->table->file->ref_length;
 
1153
          if (tab->table->maybe_null)
 
1154
          {
 
1155
            last_tab->null_byte= jt_null_bits / 8;
 
1156
            last_tab->null_bit= jt_null_bits++;
 
1157
          }
 
1158
          last_tab++;
 
1159
          tab->table->prepare_for_position();
 
1160
          tab->rowid_keep_flags= rowid_keep_flags;
 
1161
        }
 
1162
        weed_cur_map |= tab->table->map;
 
1163
        if (!tab->emb_sj_nest && bitmap_covers(weed_cur_map,
 
1164
                                               dups_ranges[j].outer_tables))
 
1165
          last_outer_tab= tab;
 
1166
      }
 
1167
 
 
1168
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1169
      {
 
1170
        SJ_TMP_TABLE *sjtbl;
 
1171
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1172
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1173
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
 
1174
          return(true);
 
1175
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1176
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1177
        sjtbl->rowid_len= jt_rowid_offset;
 
1178
        sjtbl->null_bits= jt_null_bits;
 
1179
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1180
 
 
1181
        *next_sjtbl_ptr= sjtbl;
 
1182
        next_sjtbl_ptr= &(sjtbl->next);
 
1183
        sjtbl->next= NULL;
 
1184
 
 
1185
        sjtbl->tmp_table=
 
1186
          create_duplicate_weedout_tmp_table(session,
 
1187
                                             sjtbl->rowid_len +
 
1188
                                             sjtbl->null_bytes,
 
1189
                                             sjtbl);
 
1190
 
 
1191
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1192
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1193
      }
 
1194
      tab= last_outer_tab + 1;
 
1195
      jump_to= last_outer_tab;
 
1196
    }
 
1197
 
 
1198
    /* Create the FirstMatch tail */
 
1199
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1200
    {
 
1201
      if (tab->emb_sj_nest)
 
1202
        tab->do_firstmatch= jump_to;
 
1203
      else
 
1204
        jump_to= tab;
 
1205
    }
 
1206
  }
 
1207
  return(false);
 
1208
}
 
1209
 
 
1210
 
 
1211
static void cleanup_sj_tmp_tables(JOIN *join)
 
1212
{
 
1213
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
 
1214
       sj_tbl= sj_tbl->next)
 
1215
  {
 
1216
    if (sj_tbl->tmp_table)
 
1217
    {
 
1218
      sj_tbl->tmp_table->free_tmp_table(join->session);
 
1219
    }
 
1220
  }
 
1221
  join->sj_tmp_tables= NULL;
 
1222
}
 
1223
 
 
1224
uint32_t make_join_orderinfo(JOIN *join);
 
1225
 
 
1226
/**
 
1227
  global select optimisation.
 
1228
 
 
1229
  @note
 
1230
    error code saved in field 'error'
 
1231
 
 
1232
  @retval
 
1233
    0   success
 
1234
  @retval
 
1235
    1   error
 
1236
*/
 
1237
 
 
1238
int
 
1239
JOIN::optimize()
 
1240
{
 
1241
  // to prevent double initialization on EXPLAIN
 
1242
  if (optimized)
 
1243
    return(0);
 
1244
  optimized= 1;
 
1245
 
 
1246
  session->set_proc_info("optimizing");
 
1247
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1248
              unit->select_limit_cnt);
 
1249
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1250
  select_limit= unit->select_limit_cnt;
 
1251
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1252
    select_limit= HA_POS_ERROR;
 
1253
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1254
  // Ignore errors of execution if option IGNORE present
 
1255
  if (session->lex->ignore)
 
1256
    session->lex->current_select->no_error= 1;
 
1257
 
 
1258
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1259
  /* Add HAVING to WHERE if possible */
 
1260
  if (having && !group_list && !sum_func_count)
 
1261
  {
 
1262
    if (!conds)
 
1263
    {
 
1264
      conds= having;
 
1265
      having= 0;
 
1266
    }
 
1267
    else if ((conds=new Item_cond_and(conds,having)))
 
1268
    {
 
1269
      /*
 
1270
        Item_cond_and can't be fixed after creation, so we do not check
 
1271
        conds->fixed
 
1272
      */
 
1273
      conds->fix_fields(session, &conds);
 
1274
      conds->change_ref_to_fields(session, tables_list);
 
1275
      conds->top_level_item();
 
1276
      having= 0;
 
1277
    }
 
1278
  }
 
1279
#endif
 
1280
 
 
1281
  /* Convert all outer joins to inner joins if possible */
 
1282
  conds= simplify_joins(this, join_list, conds, true, false);
 
1283
  build_bitmap_for_nested_joins(join_list, 0);
 
1284
 
 
1285
  conds= optimize_cond(this, conds, join_list, &cond_value);
 
1286
  if (session->is_error())
 
1287
  {
 
1288
    error= 1;
 
1289
    return(1);
 
1290
  }
 
1291
 
 
1292
  {
 
1293
    having= optimize_cond(this, having, join_list, &having_value);
 
1294
    if (session->is_error())
 
1295
    {
 
1296
      error= 1;
 
1297
      return(1);
 
1298
    }
 
1299
    if (select_lex->where)
 
1300
      select_lex->cond_value= cond_value;
 
1301
    if (select_lex->having)
 
1302
      select_lex->having_value= having_value;
 
1303
 
 
1304
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
 
1305
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1306
    {                                           /* Impossible cond */
 
1307
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1308
                           "Impossible HAVING" : "Impossible WHERE";
 
1309
      error= 0;
 
1310
      return(0);
 
1311
    }
 
1312
  }
 
1313
 
 
1314
  /* Optimize count(*), cmin() and cmax() */
 
1315
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1316
  {
 
1317
    int res;
 
1318
    /*
 
1319
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1320
      to the WHERE conditions,
 
1321
      or 1 if all items were resolved,
 
1322
      or 0, or an error number HA_ERR_...
 
1323
    */
 
1324
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1325
    {
 
1326
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1327
      {
 
1328
        zero_result_cause= "No matching min/max row";
 
1329
        error=0;
 
1330
        return(0);
 
1331
      }
 
1332
      if (res > 1)
 
1333
      {
 
1334
        error= res;
 
1335
        return(1);
 
1336
      }
 
1337
      if (res < 0)
 
1338
      {
 
1339
        zero_result_cause= "No matching min/max row";
 
1340
        error=0;
 
1341
        return(0);
 
1342
      }
 
1343
      zero_result_cause= "Select tables optimized away";
 
1344
      tables_list= 0;                           // All tables resolved
 
1345
      /*
 
1346
        Extract all table-independent conditions and replace the WHERE
 
1347
        clause with them. All other conditions were computed by opt_sum_query
 
1348
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1349
        so there is no need to compute the whole WHERE clause again.
 
1350
        Notice that make_cond_for_table() will always succeed to remove all
 
1351
        computed conditions, because opt_sum_query() is applicable only to
 
1352
        conjunctions.
 
1353
        Preserve conditions for EXPLAIN.
 
1354
      */
 
1355
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
1356
      {
 
1357
        COND *table_independent_conds=
 
1358
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1359
        conds= table_independent_conds;
 
1360
      }
 
1361
    }
 
1362
  }
 
1363
  if (!tables_list)
 
1364
  {
 
1365
    error= 0;
 
1366
    return(0);
 
1367
  }
 
1368
  error= -1;                                    // Error is sent to client
 
1369
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1370
 
 
1371
  /* Calculate how to do the join */
 
1372
  session->set_proc_info("statistics");
 
1373
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1374
      session->is_fatal_error)
 
1375
  {
 
1376
    return(1);
 
1377
  }
 
1378
 
 
1379
  /* Remove distinct if only const tables */
 
1380
  select_distinct= select_distinct && (const_tables != tables);
 
1381
  session->set_proc_info("preparing");
 
1382
  if (result->initialize_tables(this))
 
1383
  {
 
1384
    return(1);                          // error == -1
 
1385
  }
 
1386
  if (const_table_map != found_const_table_map &&
 
1387
      !(select_options & SELECT_DESCRIBE) &&
 
1388
      (!conds ||
 
1389
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1390
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
1391
  {
 
1392
    zero_result_cause= "no matching row in const table";
 
1393
    error= 0;
 
1394
    return(0);
 
1395
  }
 
1396
  if (!(session->options & OPTION_BIG_SELECTS) &&
 
1397
      best_read > (double) session->variables.max_join_size &&
 
1398
      !(select_options & SELECT_DESCRIBE))
 
1399
  {                                             /* purecov: inspected */
 
1400
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1401
    error= -1;
 
1402
    return(1);
 
1403
  }
 
1404
  if (const_tables && !session->locked_tables &&
 
1405
      !(select_options & SELECT_NO_UNLOCK))
 
1406
    mysql_unlock_some_tables(session, table, const_tables);
 
1407
  if (!conds && outer_join)
 
1408
  {
 
1409
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1410
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1411
  }
 
1412
  select= make_select(*table, const_table_map,
 
1413
                      const_table_map, conds, 1, &error);
 
1414
  if (error)
 
1415
  {                                             /* purecov: inspected */
 
1416
    error= -1;                                  /* purecov: inspected */
 
1417
    return(1);
 
1418
  }
 
1419
 
 
1420
  reset_nj_counters(join_list);
 
1421
  make_outerjoin_info(this);
 
1422
 
 
1423
  /*
 
1424
    Among the equal fields belonging to the same multiple equality
 
1425
    choose the one that is to be retrieved first and substitute
 
1426
    all references to these in where condition for a reference for
 
1427
    the selected field.
 
1428
  */
 
1429
  if (conds)
 
1430
  {
 
1431
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1432
    conds->update_used_tables();
 
1433
  }
 
1434
 
 
1435
  /*
 
1436
    Permorm the the optimization on fields evaluation mentioned above
 
1437
    for all on expressions.
 
1438
  */
 
1439
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1440
  {
 
1441
    if (*tab->on_expr_ref)
 
1442
    {
 
1443
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1444
                                                         tab->cond_equal,
 
1445
                                                         map2table);
 
1446
      (*tab->on_expr_ref)->update_used_tables();
 
1447
    }
 
1448
  }
 
1449
 
 
1450
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
 
1451
      (select_options & SELECT_DESCRIBE) &&
 
1452
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
1453
  {
 
1454
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1455
  }
 
1456
  if (make_join_select(this, select, conds))
 
1457
  {
 
1458
    zero_result_cause=
 
1459
      "Impossible WHERE noticed after reading const tables";
 
1460
    return(0);                          // error == 0
 
1461
  }
 
1462
 
 
1463
  error= -1;                                    /* if goto err */
 
1464
 
 
1465
  /* Optimize distinct away if possible */
 
1466
  {
 
1467
    order_st *org_order= order;
 
1468
    order=remove_const(this, order,conds,1, &simple_order);
 
1469
    if (session->is_error())
 
1470
    {
 
1471
      error= 1;
 
1472
      return(1);
 
1473
    }
 
1474
 
 
1475
    /*
 
1476
      If we are using order_st BY NULL or order_st BY const_expression,
 
1477
      return result in any order (even if we are using a GROUP BY)
 
1478
    */
 
1479
    if (!order && org_order)
 
1480
      skip_sort_order= 1;
 
1481
  }
 
1482
  /*
 
1483
     Check if we can optimize away GROUP BY/DISTINCT.
 
1484
     We can do that if there are no aggregate functions, the
 
1485
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1486
     (if present) contain direct references to all key parts of
 
1487
     an unique index (in whatever order) and if the key parts of the
 
1488
     unique index cannot contain NULLs.
 
1489
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1490
     be the same (as long as they are unique).
 
1491
 
 
1492
     The FROM clause must contain a single non-constant table.
 
1493
  */
 
1494
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1495
      !tmp_table_param.sum_func_count &&
 
1496
      (!join_tab[const_tables].select ||
 
1497
       !join_tab[const_tables].select->quick ||
 
1498
       join_tab[const_tables].select->quick->get_type() !=
 
1499
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1500
  {
 
1501
    if (group_list &&
 
1502
       list_contains_unique_index(join_tab[const_tables].table,
 
1503
                                 find_field_in_order_list,
 
1504
                                 (void *) group_list))
 
1505
    {
 
1506
      /*
 
1507
        We have found that grouping can be removed since groups correspond to
 
1508
        only one row anyway, but we still have to guarantee correct result
 
1509
        order. The line below effectively rewrites the query from GROUP BY
 
1510
        <fields> to order_st BY <fields>. There are two exceptions:
 
1511
        - if skip_sort_order is set (see above), then we can simply skip
 
1512
          GROUP BY;
 
1513
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1514
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1515
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1516
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1517
          order_st BY fields.
 
1518
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1519
          'order' as is.
 
1520
       */
 
1521
      if (!order || test_if_subpart(group_list, order))
 
1522
          order= skip_sort_order ? 0 : group_list;
 
1523
      /*
 
1524
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
 
1525
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1526
      */
 
1527
      join_tab->table->keys_in_use_for_order_by=
 
1528
        join_tab->table->keys_in_use_for_group_by;
 
1529
      group_list= 0;
 
1530
      group= 0;
 
1531
    }
 
1532
    if (select_distinct &&
 
1533
       list_contains_unique_index(join_tab[const_tables].table,
 
1534
                                 find_field_in_item_list,
 
1535
                                 (void *) &fields_list))
 
1536
    {
 
1537
      select_distinct= 0;
 
1538
    }
 
1539
  }
 
1540
  if (group_list || tmp_table_param.sum_func_count)
 
1541
  {
 
1542
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1543
      select_distinct=0;
 
1544
  }
 
1545
  else if (select_distinct && tables - const_tables == 1)
 
1546
  {
 
1547
    /*
 
1548
      We are only using one table. In this case we change DISTINCT to a
 
1549
      GROUP BY query if:
 
1550
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1551
        BY only uses selected fields.
 
1552
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1553
      - We are scanning the whole table without LIMIT
 
1554
        This can happen if:
 
1555
        - We are using CALC_FOUND_ROWS
 
1556
        - We are using an order_st BY that can't be optimized away.
 
1557
 
 
1558
      We don't want to use this optimization when we are using LIMIT
 
1559
      because in this case we can just create a temporary table that
 
1560
      holds LIMIT rows and stop when this table is full.
 
1561
    */
 
1562
    JOIN_TAB *tab= &join_tab[const_tables];
 
1563
    bool all_order_fields_used;
 
1564
    if (order)
 
1565
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
 
1566
        &tab->table->keys_in_use_for_order_by);
 
1567
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
 
1568
                                          order, fields_list, all_fields,
 
1569
                                          &all_order_fields_used)))
 
1570
    {
 
1571
      bool skip_group= (skip_sort_order &&
 
1572
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
 
1573
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1574
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1575
      if ((skip_group && all_order_fields_used) ||
 
1576
          select_limit == HA_POS_ERROR ||
 
1577
          (order && !skip_sort_order))
 
1578
      {
 
1579
        /*  Change DISTINCT to GROUP BY */
 
1580
        select_distinct= 0;
 
1581
        no_order= !order;
 
1582
        if (all_order_fields_used)
 
1583
        {
 
1584
          if (order && skip_sort_order)
 
1585
          {
 
1586
            /*
 
1587
              Force MySQL to read the table in sorted order to get result in
 
1588
              order_st BY order.
 
1589
            */
 
1590
            tmp_table_param.quick_group=0;
 
1591
          }
 
1592
          order=0;
 
1593
        }
 
1594
        group=1;                                // For end_write_group
 
1595
      }
 
1596
      else
 
1597
        group_list= 0;
 
1598
    }
 
1599
    else if (session->is_fatal_error)                   // End of memory
 
1600
      return(1);
 
1601
  }
 
1602
  simple_group= 0;
 
1603
  {
 
1604
    order_st *old_group_list;
 
1605
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1606
                             rollup.state == ROLLUP::STATE_NONE,
 
1607
                             &simple_group);
 
1608
    if (session->is_error())
 
1609
    {
 
1610
      error= 1;
 
1611
      return(1);
 
1612
    }
 
1613
    if (old_group_list && !group_list)
 
1614
      select_distinct= 0;
 
1615
  }
 
1616
  if (!group_list && group)
 
1617
  {
 
1618
    order=0;                                    // The output has only one row
 
1619
    simple_order=1;
 
1620
    select_distinct= 0;                       // No need in distinct for 1 row
 
1621
    group_optimized_away= 1;
 
1622
  }
 
1623
 
 
1624
  calc_group_buffer(this, group_list);
 
1625
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1626
 
 
1627
  if (test_if_subpart(group_list, order) ||
 
1628
      (!group_list && tmp_table_param.sum_func_count))
 
1629
    order=0;
 
1630
 
 
1631
  // Can't use sort on head table if using row cache
 
1632
  if (full_join)
 
1633
  {
 
1634
    if (group_list)
 
1635
      simple_group=0;
 
1636
    if (order)
 
1637
      simple_order=0;
 
1638
  }
 
1639
 
 
1640
  /*
 
1641
    Check if we need to create a temporary table.
 
1642
    This has to be done if all tables are not already read (const tables)
 
1643
    and one of the following conditions holds:
 
1644
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1645
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1646
    - We are using different order_st BY and GROUP BY orders
 
1647
    - The user wants us to buffer the result.
 
1648
  */
 
1649
  need_tmp= (const_tables != tables &&
 
1650
             ((select_distinct || !simple_order || !simple_group) ||
 
1651
              (group_list && order) ||
 
1652
              test(select_options & OPTION_BUFFER_RESULT)));
 
1653
 
 
1654
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1655
  uint64_t select_opts_for_readinfo=
 
1656
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1657
 
 
1658
  sj_tmp_tables= NULL;
 
1659
  if (!select_lex->sj_nests.is_empty())
 
1660
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1661
                                    no_jbuf_after);
 
1662
 
 
1663
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1664
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1665
    return(1);
 
1666
 
 
1667
  /* Create all structures needed for materialized subquery execution. */
 
1668
  if (setup_subquery_materialization())
 
1669
    return(1);
 
1670
 
 
1671
  /*
 
1672
    is this simple IN subquery?
 
1673
  */
 
1674
  if (!group_list && !order &&
 
1675
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1676
      tables == 1 && conds &&
 
1677
      !unit->is_union())
 
1678
  {
 
1679
    if (!having)
 
1680
    {
 
1681
      Item *where= conds;
 
1682
      if (join_tab[0].type == JT_EQ_REF &&
 
1683
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1684
      {
 
1685
        remove_subq_pushed_predicates(&where);
 
1686
        save_index_subquery_explain_info(join_tab, where);
 
1687
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1688
        error= 0;
 
1689
        return(unit->item->
 
1690
                    change_engine(new
 
1691
                                  subselect_uniquesubquery_engine(session,
 
1692
                                                                  join_tab,
 
1693
                                                                  unit->item,
 
1694
                                                                  where)));
 
1695
      }
 
1696
      else if (join_tab[0].type == JT_REF &&
 
1697
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1698
      {
 
1699
        remove_subq_pushed_predicates(&where);
 
1700
        save_index_subquery_explain_info(join_tab, where);
 
1701
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1702
        error= 0;
 
1703
        return(unit->item->
 
1704
                    change_engine(new
 
1705
                                  subselect_indexsubquery_engine(session,
 
1706
                                                                 join_tab,
 
1707
                                                                 unit->item,
 
1708
                                                                 where,
 
1709
                                                                 NULL,
 
1710
                                                                 0)));
 
1711
      }
 
1712
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1713
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1714
               having->name == in_having_cond)
 
1715
    {
 
1716
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1717
      error= 0;
 
1718
      conds= remove_additional_cond(conds);
 
1719
      save_index_subquery_explain_info(join_tab, conds);
 
1720
      return(unit->item->
 
1721
                  change_engine(new subselect_indexsubquery_engine(session,
 
1722
                                                                   join_tab,
 
1723
                                                                   unit->item,
 
1724
                                                                   conds,
 
1725
                                                                   having,
 
1726
                                                                   1)));
 
1727
    }
 
1728
 
 
1729
  }
 
1730
  /*
 
1731
    Need to tell handlers that to play it safe, it should fetch all
 
1732
    columns of the primary key of the tables: this is because MySQL may
 
1733
    build row pointers for the rows, and for all columns of the primary key
 
1734
    the read set has not necessarily been set by the server code.
 
1735
  */
 
1736
  if (need_tmp || select_distinct || group_list || order)
 
1737
  {
 
1738
    for (uint32_t i = const_tables; i < tables; i++)
 
1739
      join_tab[i].table->prepare_for_position();
 
1740
  }
 
1741
 
 
1742
  if (const_tables != tables)
 
1743
  {
 
1744
    /*
 
1745
      Because filesort always does a full table scan or a quick range scan
 
1746
      we must add the removed reference to the select for the table.
 
1747
      We only need to do this when we have a simple_order or simple_group
 
1748
      as in other cases the join is done before the sort.
 
1749
    */
 
1750
    if ((order || group_list) &&
 
1751
        (join_tab[const_tables].type != JT_ALL) &&
 
1752
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1753
        ((order && simple_order) || (group_list && simple_group)))
 
1754
    {
 
1755
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
 
1756
        return(1);
 
1757
      }
 
1758
    }
 
1759
 
 
1760
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1761
        ((group_list &&
 
1762
          (!simple_group ||
 
1763
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1764
                                    unit->select_limit_cnt, 0,
 
1765
                                    &join_tab[const_tables].table->
 
1766
                                    keys_in_use_for_group_by))) ||
 
1767
         select_distinct) &&
 
1768
        tmp_table_param.quick_group)
 
1769
    {
 
1770
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1771
    }
 
1772
    if (order)
 
1773
    {
 
1774
      /*
 
1775
        Force using of tmp table if sorting by a SP or UDF function due to
 
1776
        their expensive and probably non-deterministic nature.
 
1777
      */
 
1778
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1779
      {
 
1780
        Item *item= *tmp_order->item;
 
1781
        if (item->is_expensive())
 
1782
        {
 
1783
          /* Force tmp table without sort */
 
1784
          need_tmp=1; simple_order=simple_group=0;
 
1785
          break;
 
1786
        }
 
1787
      }
 
1788
    }
 
1789
  }
 
1790
 
 
1791
  tmp_having= having;
 
1792
  if (select_options & SELECT_DESCRIBE)
 
1793
  {
 
1794
    error= 0;
 
1795
    return(0);
 
1796
  }
 
1797
  having= 0;
 
1798
 
 
1799
  /*
 
1800
    The loose index scan access method guarantees that all grouping or
 
1801
    duplicate row elimination (for distinct) is already performed
 
1802
    during data retrieval, and that all MIN/MAX functions are already
 
1803
    computed for each group. Thus all MIN/MAX functions should be
 
1804
    treated as regular functions, and there is no need to perform
 
1805
    grouping in the main execution loop.
 
1806
    Notice that currently loose index scan is applicable only for
 
1807
    single table queries, thus it is sufficient to test only the first
 
1808
    join_tab element of the plan for its access method.
 
1809
  */
 
1810
  if (join_tab->is_using_loose_index_scan())
 
1811
    tmp_table_param.precomputed_group_by= true;
 
1812
 
 
1813
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1814
  if (need_tmp)
 
1815
  {
 
1816
    session->set_proc_info("Creating tmp table");
 
1817
 
 
1818
    init_items_ref_array();
 
1819
 
 
1820
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1821
                                         fields_list.elements);
 
1822
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1823
                                                             (order_st*) 0);
 
1824
    /*
 
1825
      Pushing LIMIT to the temporary table creation is not applicable
 
1826
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1827
      there are aggregate functions, because in all these cases we need
 
1828
      all result rows.
 
1829
    */
 
1830
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1831
                             !tmp_group &&
 
1832
                             !session->lex->current_select->with_sum_func) ?
 
1833
                            select_limit : HA_POS_ERROR;
 
1834
 
 
1835
    if (!(exec_tmp_table1=
 
1836
          create_tmp_table(session, &tmp_table_param, all_fields,
 
1837
                           tmp_group,
 
1838
                           group_list ? 0 : select_distinct,
 
1839
                           group_list && simple_group,
 
1840
                           select_options,
 
1841
                           tmp_rows_limit,
 
1842
                           (char *) "")))
 
1843
                {
 
1844
      return(1);
 
1845
    }
 
1846
 
 
1847
    /*
 
1848
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1849
      - we are sorting the table and writing complete group rows to the
 
1850
        temp table.
 
1851
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1852
        on all columns.
 
1853
 
 
1854
      If having is not handled here, it will be checked before the row
 
1855
      is sent to the client.
 
1856
    */
 
1857
    if (tmp_having &&
 
1858
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1859
      having= tmp_having;
 
1860
 
 
1861
    /* if group or order on first table, sort first */
 
1862
    if (group_list && simple_group)
 
1863
    {
 
1864
      session->set_proc_info("Sorting for group");
 
1865
      if (create_sort_index(session, this, group_list,
 
1866
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1867
          alloc_group_fields(this, group_list) ||
 
1868
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1869
          setup_sum_funcs(session, sum_funcs))
 
1870
      {
 
1871
        return(1);
 
1872
      }
 
1873
      group_list=0;
 
1874
    }
 
1875
    else
 
1876
    {
 
1877
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1878
          setup_sum_funcs(session, sum_funcs))
 
1879
      {
 
1880
        return(1);
 
1881
      }
 
1882
 
 
1883
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1884
      {
 
1885
        session->set_proc_info("Sorting for order");
 
1886
        if (create_sort_index(session, this, order,
 
1887
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1888
        {
 
1889
          return(1);
 
1890
        }
 
1891
        order=0;
 
1892
      }
 
1893
    }
 
1894
 
 
1895
    /*
 
1896
      Optimize distinct when used on some of the tables
 
1897
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1898
      In this case we can stop scanning t2 when we have found one t1.a
 
1899
    */
 
1900
 
 
1901
    if (exec_tmp_table1->distinct)
 
1902
    {
 
1903
      table_map used_tables= session->used_tables;
 
1904
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1905
      do
 
1906
      {
 
1907
        if (used_tables & last_join_tab->table->map)
 
1908
          break;
 
1909
        last_join_tab->not_used_in_distinct=1;
 
1910
      } while (last_join_tab-- != join_tab);
 
1911
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1912
      if (order && skip_sort_order)
 
1913
      {
 
1914
        /* Should always succeed */
 
1915
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1916
                                    order, unit->select_limit_cnt, 0,
 
1917
                                    &join_tab[const_tables].table->
 
1918
                                      keys_in_use_for_order_by))
 
1919
          order=0;
 
1920
      }
 
1921
    }
 
1922
 
 
1923
    /*
 
1924
      If this join belongs to an uncacheable subquery save
 
1925
      the original join
 
1926
    */
 
1927
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1928
        init_save_join_tab())
 
1929
      return(-1);                         /* purecov: inspected */
 
1930
  }
 
1931
 
 
1932
  error= 0;
 
1933
  return(0);
 
1934
}
 
1935
 
 
1936
 
 
1937
/**
 
1938
  Restore values in temporary join.
 
1939
*/
 
1940
void JOIN::restore_tmp()
 
1941
{
 
1942
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1943
}
 
1944
 
 
1945
 
 
1946
int
 
1947
JOIN::reinit()
 
1948
{
 
1949
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1950
                                    select_lex->offset_limit->val_uint() :
 
1951
                                    0UL);
 
1952
 
 
1953
  first_record= 0;
 
1954
 
 
1955
  if (exec_tmp_table1)
 
1956
  {
 
1957
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1958
    exec_tmp_table1->file->ha_delete_all_rows();
 
1959
    free_io_cache(exec_tmp_table1);
 
1960
    filesort_free_buffers(exec_tmp_table1,0);
 
1961
  }
 
1962
  if (exec_tmp_table2)
 
1963
  {
 
1964
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1965
    exec_tmp_table2->file->ha_delete_all_rows();
 
1966
    free_io_cache(exec_tmp_table2);
 
1967
    filesort_free_buffers(exec_tmp_table2,0);
 
1968
  }
 
1969
  if (items0)
 
1970
    set_items_ref_array(items0);
 
1971
 
 
1972
  if (join_tab_save)
 
1973
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1974
 
 
1975
  if (tmp_join)
 
1976
    restore_tmp();
 
1977
 
 
1978
  /* Reset of sum functions */
 
1979
  if (sum_funcs)
 
1980
  {
 
1981
    Item_sum *func, **func_ptr= sum_funcs;
 
1982
    while ((func= *(func_ptr++)))
 
1983
      func->clear();
 
1984
  }
 
1985
 
 
1986
  return(0);
 
1987
}
 
1988
 
 
1989
/**
 
1990
   @brief Save the original join layout
 
1991
 
 
1992
   @details Saves the original join layout so it can be reused in
 
1993
   re-execution and for EXPLAIN.
 
1994
 
 
1995
   @return Operation status
 
1996
   @retval 0      success.
 
1997
   @retval 1      error occurred.
 
1998
*/
 
1999
 
 
2000
bool
 
2001
JOIN::init_save_join_tab()
 
2002
{
 
2003
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
 
2004
    return 1;                                  /* purecov: inspected */
 
2005
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2006
  restore_tmp();
 
2007
  return 0;
 
2008
}
 
2009
 
 
2010
 
 
2011
bool
 
2012
JOIN::save_join_tab()
 
2013
{
 
2014
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2015
  {
 
2016
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
 
2017
                                                sizeof(JOIN_TAB) * tables)))
 
2018
      return 1;
 
2019
  }
 
2020
  return 0;
 
2021
}
 
2022
 
 
2023
 
 
2024
/**
 
2025
  Exec select.
 
2026
 
 
2027
  @todo
 
2028
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2029
    finally replace sorting with index scan if there is a LIMIT clause in
 
2030
    the query.  It's never shown in EXPLAIN!
 
2031
 
 
2032
  @todo
 
2033
    When can we have here session->net.report_error not zero?
 
2034
*/
 
2035
void
 
2036
JOIN::exec()
 
2037
{
 
2038
  List<Item> *columns_list= &fields_list;
 
2039
  int      tmp_error;
 
2040
 
 
2041
  session->set_proc_info("executing");
 
2042
  error= 0;
 
2043
 
 
2044
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2045
  {                                           
 
2046
    /* Only test of functions */
 
2047
    if (select_options & SELECT_DESCRIBE)
 
2048
      select_describe(this, false, false, false,
 
2049
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2050
    else
 
2051
    {
 
2052
      result->send_fields(*columns_list,
 
2053
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2054
      /*
 
2055
        We have to test for 'conds' here as the WHERE may not be constant
 
2056
        even if we don't have any tables for prepared statements or if
 
2057
        conds uses something like 'rand()'.
 
2058
      */
 
2059
      if (cond_value != Item::COND_FALSE &&
 
2060
          (!conds || conds->val_int()) &&
 
2061
          (!having || having->val_int()))
 
2062
      {
 
2063
        if (do_send_rows && result->send_data(fields_list))
 
2064
          error= 1;
 
2065
        else
 
2066
        {
 
2067
          error= (int) result->send_eof();
 
2068
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
 
2069
        }
 
2070
      }
 
2071
      else
 
2072
      {
 
2073
        error= (int) result->send_eof();
 
2074
        send_records= 0;
 
2075
      }
 
2076
    }
 
2077
    /* Single select (without union) always returns 0 or 1 row */
 
2078
    session->limit_found_rows= send_records;
 
2079
    session->examined_row_count= 0;
 
2080
    return;
 
2081
  }
 
2082
  /*
 
2083
    Don't reset the found rows count if there're no tables as
 
2084
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2085
    It must be accumulated from all join iterations of all join parts.
 
2086
  */
 
2087
  if (tables)
 
2088
    session->limit_found_rows= 0;
 
2089
 
 
2090
  if (zero_result_cause)
 
2091
  {
 
2092
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2093
                            *columns_list,
 
2094
                            send_row_on_empty_set(),
 
2095
                            select_options,
 
2096
                            zero_result_cause,
 
2097
                            having);
 
2098
    return;
 
2099
  }
 
2100
 
 
2101
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2102
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2103
    return;
 
2104
 
 
2105
  if (select_options & SELECT_DESCRIBE)
 
2106
  {
 
2107
    /*
 
2108
      Check if we managed to optimize order_st BY away and don't use temporary
 
2109
      table to resolve order_st BY: in that case, we only may need to do
 
2110
      filesort for GROUP BY.
 
2111
    */
 
2112
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2113
    {
 
2114
      /*
 
2115
        Reset 'order' to 'group_list' and reinit variables describing
 
2116
        'order'
 
2117
      */
 
2118
      order= group_list;
 
2119
      simple_order= simple_group;
 
2120
      skip_sort_order= 0;
 
2121
    }
 
2122
    if (order &&
 
2123
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2124
        (const_tables == tables ||
 
2125
         ((simple_order || skip_sort_order) &&
 
2126
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2127
                                  select_limit, 0,
 
2128
                                  &join_tab[const_tables].table->
 
2129
                                    keys_in_use_for_query))))
 
2130
      order=0;
 
2131
    having= tmp_having;
 
2132
    select_describe(this, need_tmp,
 
2133
                    order != 0 && !skip_sort_order,
 
2134
                    select_distinct,
 
2135
                    !tables ? "No tables used" : NULL);
 
2136
    return;
 
2137
  }
 
2138
 
 
2139
  JOIN *curr_join= this;
 
2140
  List<Item> *curr_all_fields= &all_fields;
 
2141
  List<Item> *curr_fields_list= &fields_list;
 
2142
  Table *curr_tmp_table= 0;
 
2143
  /*
 
2144
    Initialize examined rows here because the values from all join parts
 
2145
    must be accumulated in examined_row_count. Hence every join
 
2146
    iteration must count from zero.
 
2147
  */
 
2148
  curr_join->examined_rows= 0;
 
2149
 
 
2150
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2151
  if (need_tmp)
 
2152
  {
 
2153
    if (tmp_join)
 
2154
    {
 
2155
      /*
 
2156
        We are in a non cacheable sub query. Get the saved join structure
 
2157
        after optimization.
 
2158
        (curr_join may have been modified during last exection and we need
 
2159
        to reset it)
 
2160
      */
 
2161
      curr_join= tmp_join;
 
2162
    }
 
2163
    curr_tmp_table= exec_tmp_table1;
 
2164
 
 
2165
    /* Copy data to the temporary table */
 
2166
    session->set_proc_info("Copying to tmp table");
 
2167
    if (!curr_join->sort_and_group &&
 
2168
        curr_join->const_tables != curr_join->tables)
 
2169
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2170
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2171
    {
 
2172
      error= tmp_error;
 
2173
      return;
 
2174
    }
 
2175
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2176
 
 
2177
    if (curr_join->having)
 
2178
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2179
 
 
2180
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2181
    curr_join->all_fields= *curr_all_fields;
 
2182
    if (!items1)
 
2183
    {
 
2184
      items1= items0 + all_fields.elements;
 
2185
      if (sort_and_group || curr_tmp_table->group)
 
2186
      {
 
2187
        if (change_to_use_tmp_fields(session, items1,
 
2188
                                     tmp_fields_list1, tmp_all_fields1,
 
2189
                                     fields_list.elements, all_fields))
 
2190
          return;
 
2191
      }
 
2192
      else
 
2193
      {
 
2194
        if (change_refs_to_tmp_fields(session, items1,
 
2195
                                      tmp_fields_list1, tmp_all_fields1,
 
2196
                                      fields_list.elements, all_fields))
 
2197
          return;
 
2198
      }
 
2199
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2200
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2201
      curr_join->items1= items1;
 
2202
    }
 
2203
    curr_all_fields= &tmp_all_fields1;
 
2204
    curr_fields_list= &tmp_fields_list1;
 
2205
    curr_join->set_items_ref_array(items1);
 
2206
 
 
2207
    if (sort_and_group || curr_tmp_table->group)
 
2208
    {
 
2209
      curr_join->tmp_table_param.field_count+=
 
2210
        curr_join->tmp_table_param.sum_func_count+
 
2211
        curr_join->tmp_table_param.func_count;
 
2212
      curr_join->tmp_table_param.sum_func_count=
 
2213
        curr_join->tmp_table_param.func_count= 0;
 
2214
    }
 
2215
    else
 
2216
    {
 
2217
      curr_join->tmp_table_param.field_count+=
 
2218
        curr_join->tmp_table_param.func_count;
 
2219
      curr_join->tmp_table_param.func_count= 0;
 
2220
    }
 
2221
 
 
2222
    if (curr_tmp_table->group)
 
2223
    {                                           // Already grouped
 
2224
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2225
        curr_join->order= curr_join->group_list;  /* order by group */
 
2226
      curr_join->group_list= 0;
 
2227
    }
 
2228
 
 
2229
    /*
 
2230
      If we have different sort & group then we must sort the data by group
 
2231
      and copy it to another tmp table
 
2232
      This code is also used if we are using distinct something
 
2233
      we haven't been able to store in the temporary table yet
 
2234
      like SEC_TO_TIME(SUM(...)).
 
2235
    */
 
2236
 
 
2237
    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))
 
2238
    {                                   /* Must copy to another table */
 
2239
      /* Free first data from old join */
 
2240
      curr_join->join_free();
 
2241
      if (make_simple_join(curr_join, curr_tmp_table))
 
2242
        return;
 
2243
      calc_group_buffer(curr_join, group_list);
 
2244
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2245
                        curr_join->tmp_all_fields1,
 
2246
                        curr_join->select_distinct && !curr_join->group_list);
 
2247
      curr_join->tmp_table_param.hidden_field_count=
 
2248
        (curr_join->tmp_all_fields1.elements-
 
2249
         curr_join->tmp_fields_list1.elements);
 
2250
 
 
2251
 
 
2252
      if (exec_tmp_table2)
 
2253
        curr_tmp_table= exec_tmp_table2;
 
2254
      else
 
2255
      {
 
2256
        /* group data to new table */
 
2257
 
 
2258
        /*
 
2259
          If the access method is loose index scan then all MIN/MAX
 
2260
          functions are precomputed, and should be treated as regular
 
2261
          functions. See extended comment in JOIN::exec.
 
2262
        */
 
2263
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2264
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2265
 
 
2266
        if (!(curr_tmp_table=
 
2267
              exec_tmp_table2= create_tmp_table(session,
 
2268
                                                &curr_join->tmp_table_param,
 
2269
                                                *curr_all_fields,
 
2270
                                                (order_st*) 0,
 
2271
                                                curr_join->select_distinct &&
 
2272
                                                !curr_join->group_list,
 
2273
                                                1, curr_join->select_options,
 
2274
                                                HA_POS_ERROR,
 
2275
                                                (char *) "")))
 
2276
          return;
 
2277
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2278
      }
 
2279
      if (curr_join->group_list)
 
2280
      {
 
2281
        session->set_proc_info("Creating sort index");
 
2282
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2283
        {
 
2284
          return;
 
2285
        }
 
2286
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
2287
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2288
            make_group_fields(this, curr_join))
 
2289
        {
 
2290
          return;
 
2291
        }
 
2292
        sortorder= curr_join->sortorder;
 
2293
      }
 
2294
 
 
2295
      session->set_proc_info("Copying to group table");
 
2296
      tmp_error= -1;
 
2297
      if (curr_join != this)
 
2298
      {
 
2299
        if (sum_funcs2)
 
2300
        {
 
2301
          curr_join->sum_funcs= sum_funcs2;
 
2302
          curr_join->sum_funcs_end= sum_funcs_end2;
 
2303
        }
 
2304
        else
 
2305
        {
 
2306
          curr_join->alloc_func_list();
 
2307
          sum_funcs2= curr_join->sum_funcs;
 
2308
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2309
        }
 
2310
      }
 
2311
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2312
                                        1, true))
 
2313
        return;
 
2314
      curr_join->group_list= 0;
 
2315
      if (!curr_join->sort_and_group &&
 
2316
          curr_join->const_tables != curr_join->tables)
 
2317
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2318
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2319
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2320
      {
 
2321
        error= tmp_error;
 
2322
        return;
 
2323
      }
 
2324
      end_read_record(&curr_join->join_tab->read_record);
 
2325
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2326
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2327
 
 
2328
      // No sum funcs anymore
 
2329
      if (!items2)
 
2330
      {
 
2331
        items2= items1 + all_fields.elements;
 
2332
        if (change_to_use_tmp_fields(session, items2,
 
2333
                                     tmp_fields_list2, tmp_all_fields2,
 
2334
                                     fields_list.elements, tmp_all_fields1))
 
2335
          return;
 
2336
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2337
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2338
      }
 
2339
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2340
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2341
      curr_join->set_items_ref_array(items2);
 
2342
      curr_join->tmp_table_param.field_count+=
 
2343
        curr_join->tmp_table_param.sum_func_count;
 
2344
      curr_join->tmp_table_param.sum_func_count= 0;
 
2345
    }
 
2346
    if (curr_tmp_table->distinct)
 
2347
      curr_join->select_distinct=0;             /* Each row is unique */
 
2348
 
 
2349
    curr_join->join_free();                     /* Free quick selects */
 
2350
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2351
    {
 
2352
      session->set_proc_info("Removing duplicates");
 
2353
      if (curr_join->tmp_having)
 
2354
        curr_join->tmp_having->update_used_tables();
 
2355
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2356
                            *curr_fields_list, curr_join->tmp_having))
 
2357
        return;
 
2358
      curr_join->tmp_having=0;
 
2359
      curr_join->select_distinct=0;
 
2360
    }
 
2361
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2362
    if (make_simple_join(curr_join, curr_tmp_table))
 
2363
      return;
 
2364
    calc_group_buffer(curr_join, curr_join->group_list);
 
2365
    count_field_types(select_lex, &curr_join->tmp_table_param,
 
2366
                      *curr_all_fields, 0);
 
2367
 
 
2368
  }
 
2369
 
 
2370
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2371
  {
 
2372
    if (make_group_fields(this, curr_join))
 
2373
    {
 
2374
      return;
 
2375
    }
 
2376
    if (!items3)
 
2377
    {
 
2378
      if (!items0)
 
2379
        init_items_ref_array();
 
2380
      items3= ref_pointer_array + (all_fields.elements*4);
 
2381
      setup_copy_fields(session, &curr_join->tmp_table_param,
 
2382
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2383
                        curr_fields_list->elements, *curr_all_fields);
 
2384
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2385
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2386
      tmp_table_param.save_copy_field_end=
 
2387
        curr_join->tmp_table_param.copy_field_end;
 
2388
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2389
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2390
    }
 
2391
    else
 
2392
    {
 
2393
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2394
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2395
      curr_join->tmp_table_param.copy_field_end=
 
2396
        tmp_table_param.save_copy_field_end;
 
2397
    }
 
2398
    curr_fields_list= &tmp_fields_list3;
 
2399
    curr_all_fields= &tmp_all_fields3;
 
2400
    curr_join->set_items_ref_array(items3);
 
2401
 
 
2402
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2403
                                      1, true) ||
 
2404
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2405
        session->is_fatal_error)
 
2406
      return;
 
2407
  }
 
2408
  if (curr_join->group_list || curr_join->order)
 
2409
  {
 
2410
    session->set_proc_info("Sorting result");
 
2411
    /* If we have already done the group, add HAVING to sorted table */
 
2412
    if (curr_join->tmp_having && ! curr_join->group_list &&
 
2413
        ! curr_join->sort_and_group)
 
2414
    {
 
2415
      // Some tables may have been const
 
2416
      curr_join->tmp_having->update_used_tables();
 
2417
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2418
      table_map used_tables= (curr_join->const_table_map |
 
2419
                              curr_table->table->map);
 
2420
 
 
2421
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2422
                                                 used_tables,
 
2423
                                                 used_tables, 0);
 
2424
      if (sort_table_cond)
 
2425
      {
 
2426
        if (!curr_table->select)
 
2427
          if (!(curr_table->select= new SQL_SELECT))
 
2428
            return;
 
2429
        if (!curr_table->select->cond)
 
2430
          curr_table->select->cond= sort_table_cond;
 
2431
        else                                    // This should never happen
 
2432
        {
 
2433
          if (!(curr_table->select->cond=
 
2434
                new Item_cond_and(curr_table->select->cond,
 
2435
                                  sort_table_cond)))
 
2436
            return;
 
2437
          /*
 
2438
            Item_cond_and do not need fix_fields for execution, its parameters
 
2439
            are fixed or do not need fix_fields, too
 
2440
          */
 
2441
          curr_table->select->cond->quick_fix_field();
 
2442
        }
 
2443
        curr_table->select_cond= curr_table->select->cond;
 
2444
        curr_table->select_cond->top_level_item();
 
2445
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2446
                                                   ~ (table_map) 0,
 
2447
                                                   ~used_tables, 0);
 
2448
      }
 
2449
    }
 
2450
    {
 
2451
      if (group)
 
2452
        curr_join->select_limit= HA_POS_ERROR;
 
2453
      else
 
2454
      {
 
2455
        /*
 
2456
          We can abort sorting after session->select_limit rows if we there is no
 
2457
          WHERE clause for any tables after the sorted one.
 
2458
        */
 
2459
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2460
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2461
        for (; curr_table < end_table ; curr_table++)
 
2462
        {
 
2463
          /*
 
2464
            table->keyuse is set in the case there was an original WHERE clause
 
2465
            on the table that was optimized away.
 
2466
          */
 
2467
          if (curr_table->select_cond ||
 
2468
              (curr_table->keyuse && !curr_table->first_inner))
 
2469
          {
 
2470
            /* We have to sort all rows */
 
2471
            curr_join->select_limit= HA_POS_ERROR;
 
2472
            break;
 
2473
          }
 
2474
        }
 
2475
      }
 
2476
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2477
      {
 
2478
        return;
 
2479
      }
 
2480
      /*
 
2481
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2482
        chose FILESORT to be faster than INDEX SCAN or there is no
 
2483
        suitable index present.
 
2484
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2485
        finally replace sorting with index scan if there is a LIMIT clause in
 
2486
        the query. XXX: it's never shown in EXPLAIN!
 
2487
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2488
      */
 
2489
      if (create_sort_index(session, curr_join,
 
2490
                            curr_join->group_list ?
 
2491
                            curr_join->group_list : curr_join->order,
 
2492
                            curr_join->select_limit,
 
2493
                            (select_options & OPTION_FOUND_ROWS ?
 
2494
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2495
                            curr_join->group_list ? true : false))
 
2496
        return;
 
2497
      sortorder= curr_join->sortorder;
 
2498
      if (curr_join->const_tables != curr_join->tables &&
 
2499
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2500
      {
 
2501
        /*
 
2502
          If no IO cache exists for the first table then we are using an
 
2503
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2504
          attribute on the INDEX SCAN.
 
2505
        */
 
2506
        skip_sort_order= 1;
 
2507
      }
 
2508
    }
 
2509
  }
 
2510
  /* XXX: When can we have here session->is_error() not zero? */
 
2511
  if (session->is_error())
 
2512
  {
 
2513
    error= session->is_error();
 
2514
    return;
 
2515
  }
 
2516
  curr_join->having= curr_join->tmp_having;
 
2517
  curr_join->fields= curr_fields_list;
 
2518
 
 
2519
  {
 
2520
    session->set_proc_info("Sending data");
 
2521
    result->send_fields(*curr_fields_list,
 
2522
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2523
    error= do_select(curr_join, curr_fields_list, NULL);
 
2524
    session->limit_found_rows= curr_join->send_records;
 
2525
  }
 
2526
 
 
2527
  /* Accumulate the counts from all join iterations of all join parts. */
 
2528
  session->examined_row_count+= curr_join->examined_rows;
 
2529
 
 
2530
  /*
 
2531
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2532
    for a derived table which is always materialized.
 
2533
    Otherwise we would not be able to print the query  correctly.
 
2534
  */
 
2535
  if (items0 &&
 
2536
      (session->lex->describe & DESCRIBE_EXTENDED) &&
 
2537
      select_lex->linkage == DERIVED_TABLE_TYPE)
 
2538
    set_items_ref_array(items0);
 
2539
 
 
2540
  return;
 
2541
}
 
2542
 
 
2543
 
 
2544
/**
 
2545
  Clean up join.
 
2546
 
 
2547
  @return
 
2548
    Return error that hold JOIN.
 
2549
*/
 
2550
 
 
2551
int
 
2552
JOIN::destroy()
 
2553
{
 
2554
  select_lex->join= 0;
 
2555
 
 
2556
  if (tmp_join)
 
2557
  {
 
2558
    if (join_tab != tmp_join->join_tab)
 
2559
    {
 
2560
      JOIN_TAB *tab, *end;
 
2561
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2562
        tab->cleanup();
 
2563
    }
 
2564
    tmp_join->tmp_join= 0;
 
2565
    tmp_table_param.copy_field=0;
 
2566
    return(tmp_join->destroy());
 
2567
  }
 
2568
  cond_equal= 0;
 
2569
 
 
2570
  cleanup(1);
 
2571
  if (exec_tmp_table1)
 
2572
    exec_tmp_table1->free_tmp_table(session);
 
2573
  if (exec_tmp_table2)
 
2574
    exec_tmp_table2->free_tmp_table(session);
 
2575
  delete select;
 
2576
  delete_dynamic(&keyuse);
 
2577
  return(error);
 
2578
}
 
2579
 
 
2580
 
 
2581
 
316
2582
/**
317
2583
  An entry point to single-unit select (a select without UNION).
318
2584
 
319
 
  @param session                  thread Cursor
 
2585
  @param session                  thread handler
320
2586
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2587
                              the top-level select_lex for this query
322
2588
  @param tables               list of all tables used in this query.
332
2598
                              for a, b and c in this list.
333
2599
  @param conds                top level item of an expression representing
334
2600
                              WHERE clause of the top level select
335
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2601
  @param og_num               total number of order_st BY and GROUP BY clauses
336
2602
                              arguments
337
 
  @param order                linked list of ORDER BY agruments
 
2603
  @param order                linked list of order_st BY agruments
338
2604
  @param group                linked list of GROUP BY arguments
339
2605
  @param having               top level item of HAVING expression
 
2606
  @param proc_param           list of PROCEDUREs
340
2607
  @param select_options       select options (BIG_RESULT, etc)
341
2608
  @param result               an instance of result set handling class.
342
2609
                              This object is responsible for send result
355
2622
  @retval
356
2623
    true   an error
357
2624
*/
358
 
bool select_query(Session *session,
359
 
                  Item ***rref_pointer_array,
360
 
                  TableList *tables, 
361
 
                  uint32_t wild_num, 
362
 
                  List<Item> &fields,
363
 
                  COND *conds, 
364
 
                  uint32_t og_num,  
365
 
                  Order *order,
366
 
                  Order *group,
367
 
                  Item *having, 
368
 
                  uint64_t select_options,
369
 
                  select_result *result, 
370
 
                  Select_Lex_Unit *unit,
371
 
                  Select_Lex *select_lex)
 
2625
 
 
2626
bool
 
2627
mysql_select(Session *session, Item ***rref_pointer_array,
 
2628
             TableList *tables, uint32_t wild_num, List<Item> &fields,
 
2629
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2630
             Item *having, order_st *proc_param, uint64_t select_options,
 
2631
             select_result *result, Select_Lex_Unit *unit,
 
2632
             Select_Lex *select_lex)
372
2633
{
373
2634
  bool err;
374
2635
  bool free_join= 1;
375
2636
 
376
2637
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2638
  JOIN *join;
378
2639
  if (select_lex->join != 0)
379
2640
  {
380
2641
    join= select_lex->join;
383
2644
      creation
384
2645
    */
385
2646
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2647
        (select_options & SELECT_DESCRIBE))
387
2648
    {
388
2649
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2650
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2651
        //here is EXPLAIN of subselect or derived table
 
2652
        if (join->change_result(result))
 
2653
        {
 
2654
          return(true);
 
2655
        }
395
2656
      }
396
2657
      else
397
2658
      {
398
2659
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
399
 
                               conds, og_num, order, group, having, select_lex, unit)))
400
 
        {
401
 
          goto err;
402
 
        }
 
2660
                               conds, og_num, order, group, having, proc_param,
 
2661
                               select_lex, unit)))
 
2662
        {
 
2663
          goto err;
 
2664
        }
403
2665
      }
404
2666
    }
405
2667
    free_join= 0;
407
2669
  }
408
2670
  else
409
2671
  {
410
 
    if (!(join= new Join(session, fields, select_options, result)))
411
 
      return(true);
 
2672
    if (!(join= new JOIN(session, fields, select_options, result)))
 
2673
        return(true);
412
2674
    session->set_proc_info("init");
413
2675
    session->used_tables=0;                         // Updated by setup_fields
414
2676
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2677
                           conds, og_num, order, group, having, proc_param,
416
2678
                           select_lex, unit)) == true)
417
2679
    {
418
2680
      goto err;
419
2681
    }
420
2682
  }
421
2683
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2684
  if (join->flatten_subqueries())
 
2685
  {
 
2686
    err= 1;
 
2687
    goto err;
 
2688
  }
 
2689
 
 
2690
  if ((err= join->optimize()))
 
2691
  {
 
2692
    goto err;                                   // 1
 
2693
  }
 
2694
 
 
2695
  if (session->lex->describe & DESCRIBE_EXTENDED)
429
2696
  {
430
2697
    join->conds_history= join->conds;
431
2698
    join->having_history= (join->having?join->having:join->tmp_having);
436
2703
 
437
2704
  join->exec();
438
2705
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2706
  if (session->lex->describe & DESCRIBE_EXTENDED)
440
2707
  {
441
2708
    select_lex->where= join->conds_history;
442
2709
    select_lex->having= join->having_history;
452
2719
  return(join->error);
453
2720
}
454
2721
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2722
 
 
2723
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
 
2724
                          Item_in_subselect* const *el2)
 
2725
{
 
2726
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
 
2727
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2728
}
 
2729
 
 
2730
 
 
2731
inline Item * and_items(Item* cond, Item *item)
456
2732
{
457
2733
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2734
}
459
2735
 
 
2736
 
 
2737
static TableList *alloc_join_nest(Session *session)
 
2738
{
 
2739
  TableList *tbl;
 
2740
  if (!(tbl= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2741
                                       sizeof(nested_join_st))))
 
2742
    return NULL;
 
2743
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl +
 
2744
                                    ALIGN_SIZE(sizeof(TableList)));
 
2745
  return tbl;
 
2746
}
 
2747
 
 
2748
 
 
2749
void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
 
2750
{
 
2751
  List_iterator<TableList> it(*tlist);
 
2752
  TableList *table;
 
2753
  while ((table= it++))
 
2754
  {
 
2755
    if (table->on_expr)
 
2756
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2757
    if (table->nested_join)
 
2758
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2759
  }
 
2760
}
 
2761
 
 
2762
 
 
2763
/*
 
2764
  Convert a subquery predicate into a TableList semi-join nest
 
2765
 
 
2766
  SYNOPSIS
 
2767
    convert_subq_to_sj()
 
2768
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON
 
2769
                    clause
 
2770
       subq_pred    Subquery predicate to be converted
 
2771
 
 
2772
  DESCRIPTION
 
2773
    Convert a subquery predicate into a TableList semi-join nest. All the
 
2774
    prerequisites are already checked, so the conversion is always successfull.
 
2775
 
 
2776
    Prepared Statements: the transformation is permanent:
 
2777
     - Changes in TableList structures are naturally permanent
 
2778
     - Item tree changes are performed on statement MEM_ROOT:
 
2779
        = we activate statement MEM_ROOT
 
2780
        = this function is called before the first fix_prepare_information
 
2781
          call.
 
2782
 
 
2783
    This is intended because the criteria for subquery-to-sj conversion remain
 
2784
    constant for the lifetime of the Prepared Statement.
 
2785
 
 
2786
  RETURN
 
2787
    false  OK
 
2788
    true   Out of memory error
 
2789
*/
 
2790
 
 
2791
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2792
{
 
2793
  Select_Lex *parent_lex= parent_join->select_lex;
 
2794
  TableList *emb_tbl_nest= NULL;
 
2795
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2796
  Session *session= parent_join->session;
 
2797
 
 
2798
  /*
 
2799
    1. Find out where to put the predicate into.
 
2800
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2801
  */
 
2802
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2803
  {
 
2804
    if (subq_pred->expr_join_nest->nested_join)
 
2805
    {
 
2806
      /*
 
2807
        We're dealing with
 
2808
 
 
2809
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2810
 
 
2811
        The sj-nest will be inserted into the brackets nest.
 
2812
      */
 
2813
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2814
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2815
    }
 
2816
    else if (!subq_pred->expr_join_nest->outer_join)
 
2817
    {
 
2818
      /*
 
2819
        We're dealing with
 
2820
 
 
2821
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2822
 
 
2823
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2824
        parent. This is ok because tblX is joined as an inner join.
 
2825
      */
 
2826
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2827
      if (emb_tbl_nest)
 
2828
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2829
    }
 
2830
    else if (!subq_pred->expr_join_nest->nested_join)
 
2831
    {
 
2832
      TableList *outer_tbl= subq_pred->expr_join_nest;
 
2833
      TableList *wrap_nest;
 
2834
      /*
 
2835
        We're dealing with
 
2836
 
 
2837
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2838
 
 
2839
        we'll need to convert it into:
 
2840
 
 
2841
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2842
                        |                      |
 
2843
                        |<----- wrap_nest ---->|
 
2844
 
 
2845
        Q:  other subqueries may be pointing to this element. What to do?
 
2846
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2847
            But we'll need to fix other pointers.
 
2848
        A2: Another way: have TableList::next_ptr so the following
 
2849
            subqueries know the table has been nested.
 
2850
        A3: changes in the TableList::outer_join will make everything work
 
2851
            automatically.
 
2852
      */
 
2853
      if (!(wrap_nest= alloc_join_nest(parent_join->session)))
 
2854
      {
 
2855
        return(true);
 
2856
      }
 
2857
      wrap_nest->embedding= outer_tbl->embedding;
 
2858
      wrap_nest->join_list= outer_tbl->join_list;
 
2859
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2860
 
 
2861
      wrap_nest->nested_join->join_list.empty();
 
2862
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2863
 
 
2864
      outer_tbl->embedding= wrap_nest;
 
2865
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2866
 
 
2867
      /*
 
2868
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2869
        and on_expr
 
2870
      */
 
2871
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2872
      outer_tbl->outer_join= 0;
 
2873
 
 
2874
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2875
      outer_tbl->on_expr= NULL;
 
2876
 
 
2877
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2878
      TableList *tbl;
 
2879
      while ((tbl= li++))
 
2880
      {
 
2881
        if (tbl == outer_tbl)
 
2882
        {
 
2883
          li.replace(wrap_nest);
 
2884
          break;
 
2885
        }
 
2886
      }
 
2887
      /*
 
2888
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
 
2889
        semi-join nest into it
 
2890
      */
 
2891
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2892
      emb_tbl_nest=  wrap_nest;
 
2893
    }
 
2894
  }
 
2895
 
 
2896
  TableList *sj_nest;
 
2897
  nested_join_st *nested_join;
 
2898
  if (!(sj_nest= alloc_join_nest(parent_join->session)))
 
2899
  {
 
2900
    return(true);
 
2901
  }
 
2902
  nested_join= sj_nest->nested_join;
 
2903
 
 
2904
  sj_nest->join_list= emb_join_list;
 
2905
  sj_nest->embedding= emb_tbl_nest;
 
2906
  sj_nest->alias= (char*) "(sj-nest)";
 
2907
  /* Nests do not participate in those 'chains', so: */
 
2908
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2909
  emb_join_list->push_back(sj_nest);
 
2910
 
 
2911
  /*
 
2912
    nested_join->used_tables and nested_join->not_null_tables are
 
2913
    initialized in simplify_joins().
 
2914
  */
 
2915
 
 
2916
  /*
 
2917
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2918
       sj-nest.
 
2919
  */
 
2920
  Select_Lex *subq_lex= subq_pred->unit->first_select();
 
2921
  nested_join->join_list.empty();
 
2922
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2923
  TableList *tl, *last_leaf;
 
2924
  while ((tl= li++))
 
2925
  {
 
2926
    tl->embedding= sj_nest;
 
2927
    tl->join_list= &nested_join->join_list;
 
2928
    nested_join->join_list.push_back(tl);
 
2929
  }
 
2930
 
 
2931
  /*
 
2932
    Reconnect the next_leaf chain.
 
2933
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2934
          Inserting them at the beginning would be a bit faster.
 
2935
    NOTE: We actually insert them at the front! That's because the order is
 
2936
          reversed in this list.
 
2937
  */
 
2938
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2939
  tl->next_leaf= subq_lex->leaf_tables;
 
2940
  last_leaf= tl;
 
2941
 
 
2942
  /*
 
2943
    Same as above for next_local chain
 
2944
    (a theory: a next_local chain always starts with ::leaf_tables
 
2945
     because view's tables are inserted after the view)
 
2946
  */
 
2947
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2948
  tl->next_local= subq_lex->leaf_tables;
 
2949
 
 
2950
  /* A theory: no need to re-connect the next_global chain */
 
2951
 
 
2952
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2953
 
 
2954
  // The subqueries were replaced for Item_int(1) earlier
 
2955
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2956
  /*TODO: also reset the 'with_subselect' there. */
 
2957
 
 
2958
  /* n. Adjust the parent_join->tables counter */
 
2959
  uint32_t table_no= parent_join->tables;
 
2960
  /* n. Walk through child's tables and adjust table->map */
 
2961
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2962
  {
 
2963
    tl->table->tablenr= table_no;
 
2964
    tl->table->map= ((table_map)1) << table_no;
 
2965
    Select_Lex *old_sl= tl->select_lex;
 
2966
    tl->select_lex= parent_join->select_lex;
 
2967
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2968
      emb->select_lex= parent_join->select_lex;
 
2969
  }
 
2970
  parent_join->tables += subq_lex->join->tables;
 
2971
 
 
2972
  /*
 
2973
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2974
    Add the subquery-induced equalities too.
 
2975
  */
 
2976
  Select_Lex *save_lex= session->lex->current_select;
 
2977
  session->lex->current_select=subq_lex;
 
2978
  if (!subq_pred->left_expr->fixed &&
 
2979
       subq_pred->left_expr->fix_fields(session, &subq_pred->left_expr))
 
2980
    return(true);
 
2981
  session->lex->current_select=save_lex;
 
2982
 
 
2983
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2984
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2985
                                        subq_pred->left_expr->used_tables();
 
2986
  sj_nest->sj_on_expr= subq_lex->where;
 
2987
 
 
2988
  /*
 
2989
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2990
    Additionally, for InsideOut strategy
 
2991
     - Record the number of IN-equalities.
 
2992
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2993
       see which of the expressions are bound and which are not (for those
 
2994
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2995
 
 
2996
       (TODO: can we just create a list of pointers and hope the expressions
 
2997
       will not substitute themselves on fix_fields()? or we need to wrap
 
2998
       them into Item_direct_view_refs and store pointers to those. The
 
2999
       pointers to Item_direct_view_refs are guaranteed to be stable as
 
3000
       Item_direct_view_refs doesn't substitute itself with anything in
 
3001
       Item_direct_view_ref::fix_fields.
 
3002
  */
 
3003
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3004
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3005
 
 
3006
  if (subq_pred->left_expr->cols() == 1)
 
3007
  {
 
3008
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3009
 
 
3010
    Item *item_eq= new Item_func_eq(subq_pred->left_expr,
 
3011
                                    subq_lex->ref_pointer_array[0]);
 
3012
    item_eq->name= (char*)subq_sj_cond_name;
 
3013
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3014
  }
 
3015
  else
 
3016
  {
 
3017
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3018
    {
 
3019
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3020
                                                element_index(i));
 
3021
      Item *item_eq=
 
3022
        new Item_func_eq(subq_pred->left_expr->element_index(i),
 
3023
                         subq_lex->ref_pointer_array[i]);
 
3024
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3025
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3026
    }
 
3027
  }
 
3028
  /* Fix the created equality and AND */
 
3029
  sj_nest->sj_on_expr->fix_fields(parent_join->session, &sj_nest->sj_on_expr);
 
3030
 
 
3031
  /*
 
3032
    Walk through sj nest's WHERE and ON expressions and call
 
3033
    item->fix_table_changes() for all items.
 
3034
  */
 
3035
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3036
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3037
 
 
3038
 
 
3039
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3040
  subq_lex->master_unit()->exclude_level();
 
3041
 
 
3042
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3043
  if (emb_tbl_nest)
 
3044
  {
 
3045
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
 
3046
                                     sj_nest->sj_on_expr);
 
3047
    emb_tbl_nest->on_expr->fix_fields(parent_join->session, &emb_tbl_nest->on_expr);
 
3048
  }
 
3049
  else
 
3050
  {
 
3051
    /* Inject into the WHERE */
 
3052
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3053
    parent_join->conds->fix_fields(parent_join->session, &parent_join->conds);
 
3054
    parent_join->select_lex->where= parent_join->conds;
 
3055
  }
 
3056
 
 
3057
  return(false);
 
3058
}
 
3059
 
 
3060
 
 
3061
/*
 
3062
  Convert candidate subquery predicates to semi-joins
 
3063
 
 
3064
  SYNOPSIS
 
3065
    JOIN::flatten_subqueries()
 
3066
 
 
3067
  DESCRIPTION
 
3068
    Convert candidate subquery predicates to semi-joins.
 
3069
 
 
3070
  RETURN
 
3071
    false  OK
 
3072
    true   Error
 
3073
*/
 
3074
 
 
3075
bool JOIN::flatten_subqueries()
 
3076
{
 
3077
  Item_in_subselect **in_subq;
 
3078
  Item_in_subselect **in_subq_end;
 
3079
 
 
3080
  if (sj_subselects.elements() == 0)
 
3081
    return(false);
 
3082
 
 
3083
  /* 1. Fix children subqueries */
 
3084
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
 
3085
       in_subq != in_subq_end; in_subq++)
 
3086
  {
 
3087
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3088
    child_join->outer_tables = child_join->tables;
 
3089
    if (child_join->flatten_subqueries())
 
3090
      return(true);
 
3091
    (*in_subq)->sj_convert_priority=
 
3092
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3093
  }
 
3094
  
 
3095
  bool outer_join_disable_semi_join= false;
 
3096
  /*
 
3097
   * Temporary measure: disable semi-joins when they are together with outer
 
3098
   * joins.
 
3099
   *
 
3100
   * @see LP Bug #314911
 
3101
   */
 
3102
  for (TableList *tbl= select_lex->leaf_tables; tbl; tbl=tbl->next_leaf)
 
3103
  {
 
3104
    TableList *embedding= tbl->embedding;
 
3105
    if (tbl->on_expr || (tbl->embedding && !(embedding->sj_on_expr && 
 
3106
                                            !embedding->embedding)))
 
3107
    {
 
3108
      in_subq= sj_subselects.front();
 
3109
      outer_join_disable_semi_join= true;
 
3110
    }
 
3111
  }
 
3112
 
 
3113
  if (! outer_join_disable_semi_join)
 
3114
  {
 
3115
    /*
 
3116
      2. Pick which subqueries to convert:
 
3117
        sort the subquery array
 
3118
        - prefer correlated subqueries over uncorrelated;
 
3119
        - prefer subqueries that have greater number of outer tables;
 
3120
    */
 
3121
    sj_subselects.sort(subq_sj_candidate_cmp);
 
3122
    // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3123
    /* Replace all subqueries to be flattened with Item_int(1) */
 
3124
    for (in_subq= sj_subselects.front();
 
3125
        in_subq != in_subq_end &&
 
3126
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3127
        in_subq++)
 
3128
    {
 
3129
      if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3130
        return(true);
 
3131
    }
 
3132
 
 
3133
    for (in_subq= sj_subselects.front();
 
3134
        in_subq != in_subq_end &&
 
3135
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3136
        in_subq++)
 
3137
    {
 
3138
      if (convert_subq_to_sj(this, *in_subq))
 
3139
        return(true);
 
3140
    }
 
3141
  }
 
3142
 
 
3143
  /* 3. Finalize those we didn't convert */
 
3144
  for (; in_subq!= in_subq_end; in_subq++)
 
3145
  {
 
3146
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3147
    Item_subselect::trans_res res;
 
3148
    (*in_subq)->changed= 0;
 
3149
    (*in_subq)->fixed= 0;
 
3150
    res= (*in_subq)->select_transformer(child_join);
 
3151
    if (res == Item_subselect::RES_ERROR)
 
3152
      return(true);
 
3153
 
 
3154
    (*in_subq)->changed= 1;
 
3155
    (*in_subq)->fixed= 1;
 
3156
 
 
3157
    Item *substitute= (*in_subq)->substitution;
 
3158
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3159
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3160
      return(true);
 
3161
 
 
3162
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
 
3163
    //  return(true);
 
3164
  }
 
3165
  sj_subselects.clear();
 
3166
  return(false);
 
3167
}
 
3168
 
 
3169
 
 
3170
/**
 
3171
  Setup for execution all subqueries of a query, for which the optimizer
 
3172
  chose hash semi-join.
 
3173
 
 
3174
  @details Iterate over all subqueries of the query, and if they are under an
 
3175
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3176
  - try to initialize all data structures needed for the materialized execution
 
3177
    of the IN predicate,
 
3178
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3179
    previously blocked during JOIN::prepare.
 
3180
 
 
3181
  This method is part of the "code generation" query processing phase.
 
3182
 
 
3183
  This phase must be called after substitute_for_best_equal_field() because
 
3184
  that function may replace items with other items from a multiple equality,
 
3185
  and we need to reference the correct items in the index access method of the
 
3186
  IN predicate.
 
3187
 
 
3188
  @return Operation status
 
3189
  @retval false     success.
 
3190
  @retval true      error occurred.
 
3191
*/
 
3192
 
 
3193
bool JOIN::setup_subquery_materialization()
 
3194
{
 
3195
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
 
3196
       un= un->next_unit())
 
3197
  {
 
3198
    for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
 
3199
    {
 
3200
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3201
      if (subquery_predicate &&
 
3202
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3203
      {
 
3204
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3205
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3206
            in_subs->setup_engine())
 
3207
          return true;
 
3208
      }
 
3209
    }
 
3210
  }
 
3211
  return false;
 
3212
}
 
3213
 
 
3214
 
 
3215
/*
 
3216
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3217
 
 
3218
  SYNOPSIS
 
3219
    find_eq_ref_candidate()
 
3220
      table             Table to be checked
 
3221
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3222
                        count.
 
3223
 
 
3224
  DESCRIPTION
 
3225
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3226
 
 
3227
  TODO
 
3228
    Check again if it is feasible to factor common parts with constant table
 
3229
    search
 
3230
 
 
3231
  RETURN
 
3232
    true  - There exists an eq_ref(outer-tables) candidate
 
3233
    false - Otherwise
 
3234
*/
 
3235
 
 
3236
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3237
{
 
3238
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3239
  uint32_t key;
 
3240
 
 
3241
  if (keyuse)
 
3242
  {
 
3243
    while (1) /* For each key */
 
3244
    {
 
3245
      key= keyuse->key;
 
3246
      KEY *keyinfo= table->key_info + key;
 
3247
      key_part_map bound_parts= 0;
 
3248
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3249
      {
 
3250
        do  /* For all equalities on all key parts */
 
3251
        {
 
3252
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3253
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3254
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3255
          {
 
3256
            bound_parts |= 1 << keyuse->keypart;
 
3257
          }
 
3258
          keyuse++;
 
3259
        } while (keyuse->key == key && keyuse->table == table);
 
3260
 
 
3261
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3262
          return true;
 
3263
        if (keyuse->table != table)
 
3264
          return false;
 
3265
      }
 
3266
      else
 
3267
      {
 
3268
        do
 
3269
        {
 
3270
          keyuse++;
 
3271
          if (keyuse->table != table)
 
3272
            return false;
 
3273
        }
 
3274
        while (keyuse->key == key);
 
3275
      }
 
3276
    }
 
3277
  }
 
3278
  return false;
 
3279
}
 
3280
 
 
3281
 
 
3282
/*
 
3283
  Pull tables out of semi-join nests, if possible
 
3284
 
 
3285
  SYNOPSIS
 
3286
    pull_out_semijoin_tables()
 
3287
      join  The join where to do the semi-join flattening
 
3288
 
 
3289
  DESCRIPTION
 
3290
    Try to pull tables out of semi-join nests.
 
3291
 
 
3292
    PRECONDITIONS
 
3293
    When this function is called, the join may have several semi-join nests
 
3294
    (possibly within different semi-join nests), but it is guaranteed that
 
3295
    one semi-join nest does not contain another.
 
3296
 
 
3297
    ACTION
 
3298
    A table can be pulled out of the semi-join nest if
 
3299
     - It is a constant table
 
3300
     - It is accessed
 
3301
 
 
3302
    POSTCONDITIONS
 
3303
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3304
       tables)
 
3305
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3306
     * Semi-join nests TableList::sj_inner_tables
 
3307
 
 
3308
    This operation is (and should be) performed at each PS execution since
 
3309
    tables may become/cease to be constant across PS reexecutions.
 
3310
 
 
3311
  RETURN
 
3312
    0 - OK
 
3313
    1 - Out of memory error
 
3314
*/
 
3315
 
 
3316
int pull_out_semijoin_tables(JOIN *join)
 
3317
{
 
3318
  TableList *sj_nest;
 
3319
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3320
 
 
3321
  /* Try pulling out of the each of the semi-joins */
 
3322
  while ((sj_nest= sj_list_it++))
 
3323
  {
 
3324
    /* Action #1: Mark the constant tables to be pulled out */
 
3325
    table_map pulled_tables= 0;
 
3326
 
 
3327
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3328
    TableList *tbl;
 
3329
    while ((tbl= child_li++))
 
3330
    {
 
3331
      if (tbl->table)
 
3332
      {
 
3333
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3334
        if (tbl->table->map & join->const_table_map)
 
3335
        {
 
3336
          pulled_tables |= tbl->table->map;
 
3337
        }
 
3338
      }
 
3339
    }
 
3340
 
 
3341
    /*
 
3342
      Action #2: Find which tables we can pull out based on
 
3343
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3344
      us to pull out some other tables too.
 
3345
    */
 
3346
    bool pulled_a_table;
 
3347
    do
 
3348
    {
 
3349
      pulled_a_table= false;
 
3350
      child_li.rewind();
 
3351
      while ((tbl= child_li++))
 
3352
      {
 
3353
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3354
        {
 
3355
          if (find_eq_ref_candidate(tbl->table,
 
3356
                                    sj_nest->nested_join->used_tables &
 
3357
                                    ~pulled_tables))
 
3358
          {
 
3359
            pulled_a_table= true;
 
3360
            pulled_tables |= tbl->table->map;
 
3361
          }
 
3362
        }
 
3363
      }
 
3364
    } while (pulled_a_table);
 
3365
 
 
3366
    child_li.rewind();
 
3367
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3368
    {
 
3369
      (sj_nest)->sj_inner_tables= 0;
 
3370
      while ((tbl= child_li++))
 
3371
      {
 
3372
        if (tbl->table)
 
3373
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3374
      }
 
3375
    }
 
3376
    else
 
3377
    {
 
3378
      /* Record the bitmap of inner tables, mark the inner tables */
 
3379
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
 
3380
                             ~pulled_tables;
 
3381
      (sj_nest)->sj_inner_tables= inner_tables;
 
3382
      while ((tbl= child_li++))
 
3383
      {
 
3384
        if (tbl->table)
 
3385
        {
 
3386
          if (inner_tables & tbl->table->map)
 
3387
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3388
          else
 
3389
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3390
        }
 
3391
      }
 
3392
    }
 
3393
  }
 
3394
  return(0);
 
3395
}
 
3396
 
460
3397
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3398
  Create JOIN_TABS, make a guess about the table types,
462
3399
  Approximate how many records will be used in each table
463
3400
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3401
 
 
3402
 
 
3403
static ha_rows get_quick_record_count(Session *session, SQL_SELECT *select,
 
3404
                                      Table *table,
 
3405
                                      const key_map *keys,ha_rows limit)
465
3406
{
466
3407
  int error;
467
3408
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
482
3423
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3424
}
484
3425
 
 
3426
/*
 
3427
   This structure is used to collect info on potentially sargable
 
3428
   predicates in order to check whether they become sargable after
 
3429
   reading const tables.
 
3430
   We form a bitmap of indexes that can be used for sargable predicates.
 
3431
   Only such indexes are involved in range analysis.
 
3432
*/
 
3433
typedef struct st_sargable_param
 
3434
{
 
3435
  Field *field;              /* field against which to check sargability */
 
3436
  Item **arg_value;          /* values of potential keys for lookups     */
 
3437
  uint32_t num_values;           /* number of values in the above array      */
 
3438
} SARGABLE_PARAM;
 
3439
 
 
3440
/**
 
3441
  Calculate the best possible join and initialize the join structure.
 
3442
 
 
3443
  @retval
 
3444
    0   ok
 
3445
  @retval
 
3446
    1   Fatal error
 
3447
*/
 
3448
 
 
3449
static bool
 
3450
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3451
                     DYNAMIC_ARRAY *keyuse_array)
 
3452
{
 
3453
  int error;
 
3454
  Table *table;
 
3455
  uint32_t i,table_count,const_count,key;
 
3456
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3457
  key_map const_ref, eq_part;
 
3458
  Table **table_vector;
 
3459
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3460
  KEYUSE *keyuse,*start_keyuse;
 
3461
  table_map outer_join=0;
 
3462
  SARGABLE_PARAM *sargables= 0;
 
3463
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3464
 
 
3465
  table_count=join->tables;
 
3466
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
 
3467
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3468
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
3469
  if (!stat || !stat_ref || !table_vector)
 
3470
    return(1);                          // Eom /* purecov: inspected */
 
3471
 
 
3472
  join->best_ref=stat_vector;
 
3473
 
 
3474
  stat_end=stat+table_count;
 
3475
  found_const_table_map= all_table_map=0;
 
3476
  const_count=0;
 
3477
 
 
3478
  for (s= stat, i= 0;
 
3479
       tables;
 
3480
       s++, tables= tables->next_leaf, i++)
 
3481
  {
 
3482
    TableList *embedding= tables->embedding;
 
3483
    stat_vector[i]=s;
 
3484
    s->keys.init();
 
3485
    s->const_keys.init();
 
3486
    s->checked_keys.init();
 
3487
    s->needed_reg.init();
 
3488
    table_vector[i]=s->table=table=tables->table;
 
3489
    table->pos_in_table_list= tables;
 
3490
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3491
    if(error)
 
3492
    {
 
3493
        table->file->print_error(error, MYF(0));
 
3494
        return(1);
 
3495
    }
 
3496
    table->quick_keys.clear_all();
 
3497
    table->reginfo.join_tab=s;
 
3498
    table->reginfo.not_exists_optimize=0;
 
3499
    memset(table->const_key_parts, 0,
 
3500
           sizeof(key_part_map)*table->s->keys);
 
3501
    all_table_map|= table->map;
 
3502
    s->join=join;
 
3503
    s->info=0;                                  // For describe
 
3504
 
 
3505
    s->dependent= tables->dep_tables;
 
3506
    s->key_dependent= 0;
 
3507
    if (tables->schema_table)
 
3508
      table->file->stats.records= 2;
 
3509
    table->quick_condition_rows= table->file->stats.records;
 
3510
 
 
3511
    s->on_expr_ref= &tables->on_expr;
 
3512
    if (*s->on_expr_ref)
 
3513
    {
 
3514
      /* s is the only inner table of an outer join */
 
3515
      if (!table->file->stats.records && !embedding)
 
3516
      {                                         // Empty table
 
3517
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3518
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3519
        continue;
 
3520
      }
 
3521
      outer_join|= table->map;
 
3522
      s->embedding_map= 0;
 
3523
      for (;embedding; embedding= embedding->embedding)
 
3524
        s->embedding_map|= embedding->nested_join->nj_map;
 
3525
      continue;
 
3526
    }
 
3527
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3528
    {
 
3529
      /* s belongs to a nested join, maybe to several embedded joins */
 
3530
      s->embedding_map= 0;
 
3531
      do
 
3532
      {
 
3533
        nested_join_st *nested_join= embedding->nested_join;
 
3534
        s->embedding_map|=nested_join->nj_map;
 
3535
        s->dependent|= embedding->dep_tables;
 
3536
        embedding= embedding->embedding;
 
3537
        outer_join|= nested_join->used_tables;
 
3538
      }
 
3539
      while (embedding);
 
3540
      continue;
 
3541
    }
 
3542
    if ((table->file->stats.records <= 1) &&
 
3543
        !s->dependent &&
 
3544
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3545
    {
 
3546
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3547
    }
 
3548
  }
 
3549
  stat_vector[i]=0;
 
3550
  join->outer_join=outer_join;
 
3551
 
 
3552
  if (join->outer_join)
 
3553
  {
 
3554
    /*
 
3555
       Build transitive closure for relation 'to be dependent on'.
 
3556
       This will speed up the plan search for many cases with outer joins,
 
3557
       as well as allow us to catch illegal cross references/
 
3558
       Warshall's algorithm is used to build the transitive closure.
 
3559
       As we use bitmaps to represent the relation the complexity
 
3560
       of the algorithm is O((number of tables)^2).
 
3561
    */
 
3562
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3563
    {
 
3564
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3565
      {
 
3566
        table= stat[j].table;
 
3567
        if (s->dependent & table->map)
 
3568
          s->dependent |= table->reginfo.join_tab->dependent;
 
3569
      }
 
3570
      if (s->dependent)
 
3571
        s->table->maybe_null= 1;
 
3572
    }
 
3573
    /* Catch illegal cross references for outer joins */
 
3574
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3575
    {
 
3576
      if (s->dependent & s->table->map)
 
3577
      {
 
3578
        join->tables=0;                 // Don't use join->table
 
3579
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3580
        return(1);
 
3581
      }
 
3582
      s->key_dependent= s->dependent;
 
3583
    }
 
3584
  }
 
3585
 
 
3586
  if (conds || outer_join)
 
3587
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
 
3588
                            conds, join->cond_equal,
 
3589
                            ~outer_join, join->select_lex, &sargables))
 
3590
      return(1);
 
3591
 
 
3592
  /* Read tables with 0 or 1 rows (system tables) */
 
3593
  join->const_table_map= 0;
 
3594
 
 
3595
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3596
       p_pos < p_end ;
 
3597
       p_pos++)
 
3598
  {
 
3599
    int tmp;
 
3600
    s= p_pos->table;
 
3601
    s->type=JT_SYSTEM;
 
3602
    join->const_table_map|=s->table->map;
 
3603
    if ((tmp=join_read_const_table(s, p_pos)))
 
3604
    {
 
3605
      if (tmp > 0)
 
3606
        return(1);                      // Fatal error
 
3607
    }
 
3608
    else
 
3609
      found_const_table_map|= s->table->map;
 
3610
  }
 
3611
 
 
3612
  /* loop until no more const tables are found */
 
3613
  int ref_changed;
 
3614
  do
 
3615
  {
 
3616
  more_const_tables_found:
 
3617
    ref_changed = 0;
 
3618
    found_ref=0;
 
3619
 
 
3620
    /*
 
3621
      We only have to loop from stat_vector + const_count as
 
3622
      set_position() will move all const_tables first in stat_vector
 
3623
    */
 
3624
 
 
3625
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3626
    {
 
3627
      table=s->table;
 
3628
 
 
3629
      /*
 
3630
        If equi-join condition by a key is null rejecting and after a
 
3631
        substitution of a const table the key value happens to be null
 
3632
        then we can state that there are no matches for this equi-join.
 
3633
      */
 
3634
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3635
      {
 
3636
        /*
 
3637
          When performing an outer join operation if there are no matching rows
 
3638
          for the single row of the outer table all the inner tables are to be
 
3639
          null complemented and thus considered as constant tables.
 
3640
          Here we apply this consideration to the case of outer join operations
 
3641
          with a single inner table only because the case with nested tables
 
3642
          would require a more thorough analysis.
 
3643
          TODO. Apply single row substitution to null complemented inner tables
 
3644
          for nested outer join operations.
 
3645
        */
 
3646
        while (keyuse->table == table)
 
3647
        {
 
3648
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3649
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3650
          {
 
3651
            s->type= JT_CONST;
 
3652
            mark_as_null_row(table);
 
3653
            found_const_table_map|= table->map;
 
3654
            join->const_table_map|= table->map;
 
3655
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3656
            goto more_const_tables_found;
 
3657
           }
 
3658
          keyuse++;
 
3659
        }
 
3660
      }
 
3661
 
 
3662
      if (s->dependent)                         // If dependent on some table
 
3663
      {
 
3664
        // All dep. must be constants
 
3665
        if (s->dependent & ~(found_const_table_map))
 
3666
          continue;
 
3667
        if (table->file->stats.records <= 1L &&
 
3668
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3669
            !table->pos_in_table_list->embedding)
 
3670
        {                                       // system table
 
3671
          int tmp= 0;
 
3672
          s->type=JT_SYSTEM;
 
3673
          join->const_table_map|=table->map;
 
3674
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3675
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3676
          {
 
3677
            if (tmp > 0)
 
3678
              return(1);                        // Fatal error
 
3679
          }
 
3680
          else
 
3681
            found_const_table_map|= table->map;
 
3682
          continue;
 
3683
        }
 
3684
      }
 
3685
      /* check if table can be read by key or table only uses const refs */
 
3686
      if ((keyuse=s->keyuse))
 
3687
      {
 
3688
        s->type= JT_REF;
 
3689
        while (keyuse->table == table)
 
3690
        {
 
3691
          start_keyuse=keyuse;
 
3692
          key=keyuse->key;
 
3693
          s->keys.set_bit(key);               // QQ: remove this ?
 
3694
 
 
3695
          refs=0;
 
3696
          const_ref.clear_all();
 
3697
          eq_part.clear_all();
 
3698
          do
 
3699
          {
 
3700
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3701
            {
 
3702
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3703
                const_ref.set_bit(keyuse->keypart);
 
3704
              else
 
3705
                refs|=keyuse->used_tables;
 
3706
              eq_part.set_bit(keyuse->keypart);
 
3707
            }
 
3708
            keyuse++;
 
3709
          } while (keyuse->table == table && keyuse->key == key);
 
3710
 
 
3711
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3712
              !table->pos_in_table_list->embedding)
 
3713
          {
 
3714
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3715
                 == HA_NOSAME)
 
3716
            {
 
3717
              if (const_ref == eq_part)
 
3718
              {                                 // Found everything for ref.
 
3719
                int tmp;
 
3720
                ref_changed = 1;
 
3721
                s->type= JT_CONST;
 
3722
                join->const_table_map|=table->map;
 
3723
                set_position(join,const_count++,s,start_keyuse);
 
3724
                if (create_ref_for_key(join, s, start_keyuse,
 
3725
                                       found_const_table_map))
 
3726
                  return(1);
 
3727
                if ((tmp=join_read_const_table(s,
 
3728
                                               join->positions+const_count-1)))
 
3729
                {
 
3730
                  if (tmp > 0)
 
3731
                    return(1);                  // Fatal error
 
3732
                }
 
3733
                else
 
3734
                  found_const_table_map|= table->map;
 
3735
                break;
 
3736
              }
 
3737
              else
 
3738
                found_ref|= refs;      // Table is const if all refs are const
 
3739
            }
 
3740
            else if (const_ref == eq_part)
 
3741
              s->const_keys.set_bit(key);
 
3742
          }
 
3743
        }
 
3744
      }
 
3745
    }
 
3746
  } while (join->const_table_map & found_ref && ref_changed);
 
3747
 
 
3748
  /*
 
3749
    Update info on indexes that can be used for search lookups as
 
3750
    reading const tables may has added new sargable predicates.
 
3751
  */
 
3752
  if (const_count && sargables)
 
3753
  {
 
3754
    for( ; sargables->field ; sargables++)
 
3755
    {
 
3756
      Field *field= sargables->field;
 
3757
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3758
      key_map possible_keys= field->key_start;
 
3759
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3760
      bool is_const= 1;
 
3761
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3762
        is_const&= sargables->arg_value[j]->const_item();
 
3763
      if (is_const)
 
3764
        join_tab[0].const_keys.merge(possible_keys);
 
3765
    }
 
3766
  }
 
3767
 
 
3768
  if (pull_out_semijoin_tables(join))
 
3769
    return(true);
 
3770
 
 
3771
  /* Calc how many (possible) matched records in each table */
 
3772
 
 
3773
  for (s=stat ; s < stat_end ; s++)
 
3774
  {
 
3775
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3776
    {
 
3777
      /* Only one matching row */
 
3778
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3779
      continue;
 
3780
    }
 
3781
    /* Approximate found rows and time to read them */
 
3782
    s->found_records=s->records=s->table->file->stats.records;
 
3783
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3784
 
 
3785
    /*
 
3786
      Set a max range of how many seeks we can expect when using keys
 
3787
      This is can't be to high as otherwise we are likely to use
 
3788
      table scan.
 
3789
    */
 
3790
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3791
                        (double) s->read_time*3);
 
3792
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3793
      s->worst_seeks=2.0;
 
3794
 
 
3795
    /*
 
3796
      Add to stat->const_keys those indexes for which all group fields or
 
3797
      all select distinct fields participate in one index.
 
3798
    */
 
3799
    add_group_and_distinct_keys(join, s);
 
3800
 
 
3801
    if (!s->const_keys.is_clear_all() &&
 
3802
        !s->table->pos_in_table_list->embedding)
 
3803
    {
 
3804
      ha_rows records;
 
3805
      SQL_SELECT *select;
 
3806
      select= make_select(s->table, found_const_table_map,
 
3807
                          found_const_table_map,
 
3808
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3809
                          1, &error);
 
3810
      if (!select)
 
3811
        return(1);
 
3812
      records= get_quick_record_count(join->session, select, s->table,
 
3813
                                      &s->const_keys, join->row_limit);
 
3814
      s->quick=select->quick;
 
3815
      s->needed_reg=select->needed_reg;
 
3816
      select->quick=0;
 
3817
      if (records == 0 && s->table->reginfo.impossible_range)
 
3818
      {
 
3819
        /*
 
3820
          Impossible WHERE or ON expression
 
3821
          In case of ON, we mark that the we match one empty NULL row.
 
3822
          In case of WHERE, don't set found_const_table_map to get the
 
3823
          caller to abort with a zero row result.
 
3824
        */
 
3825
        join->const_table_map|= s->table->map;
 
3826
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3827
        s->type= JT_CONST;
 
3828
        if (*s->on_expr_ref)
 
3829
        {
 
3830
          /* Generate empty row */
 
3831
          s->info= "Impossible ON condition";
 
3832
          found_const_table_map|= s->table->map;
 
3833
          s->type= JT_CONST;
 
3834
          mark_as_null_row(s->table);           // All fields are NULL
 
3835
        }
 
3836
      }
 
3837
      if (records != HA_POS_ERROR)
 
3838
      {
 
3839
        s->found_records=records;
 
3840
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3841
      }
 
3842
      delete select;
 
3843
    }
 
3844
  }
 
3845
 
 
3846
  join->join_tab=stat;
 
3847
  join->map2table=stat_ref;
 
3848
  join->table= join->all_tables=table_vector;
 
3849
  join->const_tables=const_count;
 
3850
  join->found_const_table_map=found_const_table_map;
 
3851
 
 
3852
  /* Find an optimal join order of the non-constant tables. */
 
3853
  if (join->const_tables != join->tables)
 
3854
  {
 
3855
    optimize_keyuse(join, keyuse_array);
 
3856
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3857
      return(true);
 
3858
  }
 
3859
  else
 
3860
  {
 
3861
    memcpy(join->best_positions, join->positions,
 
3862
           sizeof(POSITION)*join->const_tables);
 
3863
    join->best_read=1.0;
 
3864
  }
 
3865
  /* Generate an execution plan from the found optimal join order. */
 
3866
  return(join->session->killed || get_best_combination(join));
 
3867
}
 
3868
 
 
3869
 
485
3870
/*****************************************************************************
486
3871
  Check with keys are used and with tables references with tables
487
3872
  Updates in stat:
490
3875
          keyuse     Pointer to possible keys
491
3876
*****************************************************************************/
492
3877
 
 
3878
/// Used when finding key fields
 
3879
typedef struct key_field_t {
 
3880
  Field         *field;
 
3881
  Item          *val;                   ///< May be empty if diff constant
 
3882
  uint          level;
 
3883
  uint          optimize; // KEY_OPTIMIZE_*
 
3884
  bool          eq_func;
 
3885
  /**
 
3886
    If true, the condition this struct represents will not be satisfied
 
3887
    when val IS NULL.
 
3888
  */
 
3889
  bool          null_rejecting;
 
3890
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3891
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3892
} KEY_FIELD;
 
3893
 
 
3894
/**
 
3895
  Merge new key definitions to old ones, remove those not used in both.
 
3896
 
 
3897
  This is called for OR between different levels.
 
3898
 
 
3899
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3900
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3901
  that are internally transformed to something like:.
 
3902
 
 
3903
  @code
 
3904
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
 
3905
  @endcode
 
3906
 
 
3907
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3908
  result has null_rejecting=true if it is set for both ORed references.
 
3909
  for example:
 
3910
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3911
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3912
 
 
3913
  @todo
 
3914
    The result of this is that we're missing some 'ref' accesses.
 
3915
    OptimizerTeam: Fix this
 
3916
*/
 
3917
 
 
3918
static KEY_FIELD *
 
3919
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3920
                 uint32_t and_level)
 
3921
{
 
3922
  if (start == new_fields)
 
3923
    return start;                               // Impossible or
 
3924
  if (new_fields == end)
 
3925
    return start;                               // No new fields, skip all
 
3926
 
 
3927
  KEY_FIELD *first_free=new_fields;
 
3928
 
 
3929
  /* Mark all found fields in old array */
 
3930
  for (; new_fields != end ; new_fields++)
 
3931
  {
 
3932
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3933
    {
 
3934
      if (old->field == new_fields->field)
 
3935
      {
 
3936
        /*
 
3937
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3938
          it can return false where it is feasible to make it return true.
 
3939
 
 
3940
          The cause is as follows: Some of the tables are already known to be
 
3941
          const tables (the detection code is in make_join_statistics(),
 
3942
          above the update_ref_and_keys() call), but we didn't propagate
 
3943
          information about this: Table::const_table is not set to true, and
 
3944
          Item::update_used_tables() hasn't been called for each item.
 
3945
          The result of this is that we're missing some 'ref' accesses.
 
3946
          TODO: OptimizerTeam: Fix this
 
3947
        */
 
3948
        if (!new_fields->val->const_item())
 
3949
        {
 
3950
          /*
 
3951
            If the value matches, we can use the key reference.
 
3952
            If not, we keep it until we have examined all new values
 
3953
          */
 
3954
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3955
          {
 
3956
            old->level= and_level;
 
3957
            old->optimize= ((old->optimize & new_fields->optimize &
 
3958
                             KEY_OPTIMIZE_EXISTS) |
 
3959
                            ((old->optimize | new_fields->optimize) &
 
3960
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3961
            old->null_rejecting= (old->null_rejecting &&
 
3962
                                  new_fields->null_rejecting);
 
3963
          }
 
3964
        }
 
3965
        else if (old->eq_func && new_fields->eq_func &&
 
3966
                 old->val->eq_by_collation(new_fields->val,
 
3967
                                           old->field->binary(),
 
3968
                                           old->field->charset()))
 
3969
 
 
3970
        {
 
3971
          old->level= and_level;
 
3972
          old->optimize= ((old->optimize & new_fields->optimize &
 
3973
                           KEY_OPTIMIZE_EXISTS) |
 
3974
                          ((old->optimize | new_fields->optimize) &
 
3975
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3976
          old->null_rejecting= (old->null_rejecting &&
 
3977
                                new_fields->null_rejecting);
 
3978
        }
 
3979
        else if (old->eq_func && new_fields->eq_func &&
 
3980
                 ((old->val->const_item() && old->val->is_null()) ||
 
3981
                  new_fields->val->is_null()))
 
3982
        {
 
3983
          /* field = expression OR field IS NULL */
 
3984
          old->level= and_level;
 
3985
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3986
          /*
 
3987
            Remember the NOT NULL value unless the value does not depend
 
3988
            on other tables.
 
3989
          */
 
3990
          if (!old->val->used_tables() && old->val->is_null())
 
3991
            old->val= new_fields->val;
 
3992
          /* The referred expression can be NULL: */
 
3993
          old->null_rejecting= 0;
 
3994
        }
 
3995
        else
 
3996
        {
 
3997
          /*
 
3998
            We are comparing two different const.  In this case we can't
 
3999
            use a key-lookup on this so it's better to remove the value
 
4000
            and let the range optimzier handle it
 
4001
          */
 
4002
          if (old == --first_free)              // If last item
 
4003
            break;
 
4004
          *old= *first_free;                    // Remove old value
 
4005
          old--;                                // Retry this value
 
4006
        }
 
4007
      }
 
4008
    }
 
4009
  }
 
4010
  /* Remove all not used items */
 
4011
  for (KEY_FIELD *old=start ; old != first_free ;)
 
4012
  {
 
4013
    if (old->level != and_level)
 
4014
    {                                           // Not used in all levels
 
4015
      if (old == --first_free)
 
4016
        break;
 
4017
      *old= *first_free;                        // Remove old value
 
4018
      continue;
 
4019
    }
 
4020
    old++;
 
4021
  }
 
4022
  return first_free;
 
4023
}
 
4024
 
 
4025
 
 
4026
/**
 
4027
  Add a possible key to array of possible keys if it's usable as a key
 
4028
 
 
4029
    @param key_fields      Pointer to add key, if usable
 
4030
    @param and_level       And level, to be stored in KEY_FIELD
 
4031
    @param cond            Condition predicate
 
4032
    @param field           Field used in comparision
 
4033
    @param eq_func         True if we used =, <=> or IS NULL
 
4034
    @param value           Value used for comparison with field
 
4035
    @param usable_tables   Tables which can be used for key optimization
 
4036
    @param sargables       IN/OUT Array of found sargable candidates
 
4037
 
 
4038
  @note
 
4039
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4040
    table, we store this to be able to do not exists optimization later.
 
4041
 
 
4042
  @returns
 
4043
    *key_fields is incremented if we stored a key in the array
 
4044
*/
 
4045
 
 
4046
static void
 
4047
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
 
4048
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4049
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4050
{
 
4051
  uint32_t exists_optimize= 0;
 
4052
  if (!(field->flags & PART_KEY_FLAG))
 
4053
  {
 
4054
    // Don't remove column IS NULL on a LEFT JOIN table
 
4055
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4056
        !field->table->maybe_null || field->null_ptr)
 
4057
      return;                                   // Not a key. Skip it
 
4058
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4059
    assert(num_values == 1);
 
4060
  }
 
4061
  else
 
4062
  {
 
4063
    table_map used_tables=0;
 
4064
    bool optimizable=0;
 
4065
    for (uint32_t i=0; i<num_values; i++)
 
4066
    {
 
4067
      used_tables|=(value[i])->used_tables();
 
4068
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4069
        optimizable=1;
 
4070
    }
 
4071
    if (!optimizable)
 
4072
      return;
 
4073
    if (!(usable_tables & field->table->map))
 
4074
    {
 
4075
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4076
          !field->table->maybe_null || field->null_ptr)
 
4077
        return;                                 // Can't use left join optimize
 
4078
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4079
    }
 
4080
    else
 
4081
    {
 
4082
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4083
      key_map possible_keys=field->key_start;
 
4084
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4085
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4086
 
 
4087
      /*
 
4088
        Save the following cases:
 
4089
        Field op constant
 
4090
        Field LIKE constant where constant doesn't start with a wildcard
 
4091
        Field = field2 where field2 is in a different table
 
4092
        Field op formula
 
4093
        Field IS NULL
 
4094
        Field IS NOT NULL
 
4095
         Field BETWEEN ...
 
4096
         Field IN ...
 
4097
      */
 
4098
      stat[0].key_dependent|=used_tables;
 
4099
 
 
4100
      bool is_const=1;
 
4101
      for (uint32_t i=0; i<num_values; i++)
 
4102
      {
 
4103
        if (!(is_const&= value[i]->const_item()))
 
4104
          break;
 
4105
      }
 
4106
      if (is_const)
 
4107
        stat[0].const_keys.merge(possible_keys);
 
4108
      else if (!eq_func)
 
4109
      {
 
4110
        /*
 
4111
          Save info to be able check whether this predicate can be
 
4112
          considered as sargable for range analisis after reading const tables.
 
4113
          We do not save info about equalities as update_const_equal_items
 
4114
          will take care of updating info on keys from sargable equalities.
 
4115
        */
 
4116
        (*sargables)--;
 
4117
        (*sargables)->field= field;
 
4118
        (*sargables)->arg_value= value;
 
4119
        (*sargables)->num_values= num_values;
 
4120
      }
 
4121
      /*
 
4122
        We can't always use indexes when comparing a string index to a
 
4123
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4124
        eq_func is NEVER true when num_values > 1
 
4125
       */
 
4126
      if (!eq_func)
 
4127
      {
 
4128
        /*
 
4129
          Additional optimization: if we're processing
 
4130
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4131
          "t.key = c1".
 
4132
          TODO: This is a very limited fix. A more generic fix is possible.
 
4133
          There are 2 options:
 
4134
          A) Make equality propagation code be able to handle BETWEEN
 
4135
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4136
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4137
             and use them in equality propagation process (see details in
 
4138
             OptimizerKBAndTodo)
 
4139
        */
 
4140
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4141
            ((Item_func_between*) cond)->negated ||
 
4142
            !value[0]->eq(value[1], field->binary()))
 
4143
          return;
 
4144
        eq_func= true;
 
4145
      }
 
4146
 
 
4147
      if (field->result_type() == STRING_RESULT)
 
4148
      {
 
4149
        if ((*value)->result_type() != STRING_RESULT)
 
4150
        {
 
4151
          if (field->cmp_type() != (*value)->result_type())
 
4152
            return;
 
4153
        }
 
4154
        else
 
4155
        {
 
4156
          /*
 
4157
            We can't use indexes if the effective collation
 
4158
            of the operation differ from the field collation.
 
4159
          */
 
4160
          if (field->cmp_type() == STRING_RESULT &&
 
4161
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4162
            return;
 
4163
        }
 
4164
      }
 
4165
    }
 
4166
  }
 
4167
  /*
 
4168
    For the moment eq_func is always true. This slot is reserved for future
 
4169
    extensions where we want to remembers other things than just eq comparisons
 
4170
  */
 
4171
  assert(eq_func);
 
4172
  /* Store possible eq field */
 
4173
  (*key_fields)->field=         field;
 
4174
  (*key_fields)->eq_func=       eq_func;
 
4175
  (*key_fields)->val=           *value;
 
4176
  (*key_fields)->level=         and_level;
 
4177
  (*key_fields)->optimize=      exists_optimize;
 
4178
  /*
 
4179
    If the condition has form "tbl.keypart = othertbl.field" and
 
4180
    othertbl.field can be NULL, there will be no matches if othertbl.field
 
4181
    has NULL value.
 
4182
    We use null_rejecting in add_not_null_conds() to add
 
4183
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4184
  */
 
4185
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4186
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4187
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4188
                                  ((Item_field*)*value)->field->maybe_null());
 
4189
  (*key_fields)->cond_guard= NULL;
 
4190
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
 
4191
                              cond->name < subq_sj_cond_name + 64)?
 
4192
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4193
  (*key_fields)++;
 
4194
}
 
4195
 
 
4196
/**
 
4197
  Add possible keys to array of possible keys originated from a simple
 
4198
  predicate.
 
4199
 
 
4200
    @param  key_fields     Pointer to add key, if usable
 
4201
    @param  and_level      And level, to be stored in KEY_FIELD
 
4202
    @param  cond           Condition predicate
 
4203
    @param  field          Field used in comparision
 
4204
    @param  eq_func        True if we used =, <=> or IS NULL
 
4205
    @param  value          Value used for comparison with field
 
4206
                           Is NULL for BETWEEN and IN
 
4207
    @param  usable_tables  Tables which can be used for key optimization
 
4208
    @param  sargables      IN/OUT Array of found sargable candidates
 
4209
 
 
4210
  @note
 
4211
    If field items f1 and f2 belong to the same multiple equality and
 
4212
    a key is added for f1, the the same key is added for f2.
 
4213
 
 
4214
  @returns
 
4215
    *key_fields is incremented if we stored a key in the array
 
4216
*/
 
4217
 
 
4218
static void
 
4219
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4220
                     Item_func *cond, Item_field *field_item,
 
4221
                     bool eq_func, Item **val,
 
4222
                     uint32_t num_values, table_map usable_tables,
 
4223
                     SARGABLE_PARAM **sargables)
 
4224
{
 
4225
  Field *field= field_item->field;
 
4226
  add_key_field(key_fields, and_level, cond, field,
 
4227
                eq_func, val, num_values, usable_tables, sargables);
 
4228
  Item_equal *item_equal= field_item->item_equal;
 
4229
  if (item_equal)
 
4230
  {
 
4231
    /*
 
4232
      Add to the set of possible key values every substitution of
 
4233
      the field for an equal field included into item_equal
 
4234
    */
 
4235
    Item_equal_iterator it(*item_equal);
 
4236
    Item_field *item;
 
4237
    while ((item= it++))
 
4238
    {
 
4239
      if (!field->eq(item->field))
 
4240
      {
 
4241
        add_key_field(key_fields, and_level, cond, item->field,
 
4242
                      eq_func, val, num_values, usable_tables,
 
4243
                      sargables);
 
4244
      }
 
4245
    }
 
4246
  }
 
4247
}
 
4248
 
 
4249
static void
 
4250
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4251
               COND *cond, table_map usable_tables,
 
4252
               SARGABLE_PARAM **sargables)
 
4253
{
 
4254
  if (cond->type() == Item_func::COND_ITEM)
 
4255
  {
 
4256
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4257
    KEY_FIELD *org_key_fields= *key_fields;
 
4258
 
 
4259
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4260
    {
 
4261
      Item *item;
 
4262
      while ((item=li++))
 
4263
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4264
                       sargables);
 
4265
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4266
        org_key_fields->level= *and_level;
 
4267
    }
 
4268
    else
 
4269
    {
 
4270
      (*and_level)++;
 
4271
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4272
                     sargables);
 
4273
      Item *item;
 
4274
      while ((item=li++))
 
4275
      {
 
4276
        KEY_FIELD *start_key_fields= *key_fields;
 
4277
        (*and_level)++;
 
4278
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4279
                       sargables);
 
4280
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4281
                                     *key_fields,++(*and_level));
 
4282
      }
 
4283
    }
 
4284
    return;
 
4285
  }
 
4286
 
 
4287
  /*
 
4288
    Subquery optimization: Conditions that are pushed down into subqueries
 
4289
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4290
    but need to set cond_guard for KEYUSE elements generated from it.
 
4291
  */
 
4292
  {
 
4293
    if (cond->type() == Item::FUNC_ITEM &&
 
4294
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4295
    {
 
4296
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4297
      if (!join->group_list && !join->order &&
 
4298
          join->unit->item &&
 
4299
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4300
          !join->unit->is_union())
 
4301
      {
 
4302
        KEY_FIELD *save= *key_fields;
 
4303
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4304
                       sargables);
 
4305
        // Indicate that this ref access candidate is for subquery lookup:
 
4306
        for (; save != *key_fields; save++)
 
4307
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4308
      }
 
4309
      return;
 
4310
    }
 
4311
  }
 
4312
 
 
4313
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4314
  if (cond->type() != Item::FUNC_ITEM)
 
4315
    return;
 
4316
  Item_func *cond_func= (Item_func*) cond;
 
4317
  switch (cond_func->select_optimize()) {
 
4318
  case Item_func::OPTIMIZE_NONE:
 
4319
    break;
 
4320
  case Item_func::OPTIMIZE_KEY:
 
4321
  {
 
4322
    Item **values;
 
4323
    // BETWEEN, IN, NE
 
4324
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4325
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4326
    {
 
4327
      values= cond_func->arguments()+1;
 
4328
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4329
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4330
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4331
        values--;
 
4332
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4333
                  cond_func->argument_count() != 2);
 
4334
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4335
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4336
                           0, values,
 
4337
                           cond_func->argument_count()-1,
 
4338
                           usable_tables, sargables);
 
4339
    }
 
4340
    if (cond_func->functype() == Item_func::BETWEEN)
 
4341
    {
 
4342
      values= cond_func->arguments();
 
4343
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4344
      {
 
4345
        Item_field *field_item;
 
4346
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4347
            &&
 
4348
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4349
        {
 
4350
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4351
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4352
                               field_item, 0, values, 1, usable_tables,
 
4353
                               sargables);
 
4354
        }
 
4355
      }
 
4356
    }
 
4357
    break;
 
4358
  }
 
4359
  case Item_func::OPTIMIZE_OP:
 
4360
  {
 
4361
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4362
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4363
 
 
4364
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4365
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4366
    {
 
4367
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4368
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4369
                           equal_func,
 
4370
                           cond_func->arguments()+1, 1, usable_tables,
 
4371
                           sargables);
 
4372
    }
 
4373
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4374
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4375
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4376
    {
 
4377
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4378
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4379
                           equal_func,
 
4380
                           cond_func->arguments(),1,usable_tables,
 
4381
                           sargables);
 
4382
    }
 
4383
    break;
 
4384
  }
 
4385
  case Item_func::OPTIMIZE_NULL:
 
4386
    /* column_name IS [NOT] NULL */
 
4387
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4388
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4389
    {
 
4390
      Item *tmp=new Item_null;
 
4391
      if (unlikely(!tmp))                       // Should never be true
 
4392
        return;
 
4393
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4394
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4395
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4396
                           &tmp, 1, usable_tables, sargables);
 
4397
    }
 
4398
    break;
 
4399
  case Item_func::OPTIMIZE_EQUAL:
 
4400
    Item_equal *item_equal= (Item_equal *) cond;
 
4401
    Item *const_item= item_equal->get_const();
 
4402
    Item_equal_iterator it(*item_equal);
 
4403
    Item_field *item;
 
4404
    if (const_item)
 
4405
    {
 
4406
      /*
 
4407
        For each field field1 from item_equal consider the equality
 
4408
        field1=const_item as a condition allowing an index access of the table
 
4409
        with field1 by the keys value of field1.
 
4410
      */
 
4411
      while ((item= it++))
 
4412
      {
 
4413
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4414
                      true, &const_item, 1, usable_tables, sargables);
 
4415
      }
 
4416
    }
 
4417
    else
 
4418
    {
 
4419
      /*
 
4420
        Consider all pairs of different fields included into item_equal.
 
4421
        For each of them (field1, field1) consider the equality
 
4422
        field1=field2 as a condition allowing an index access of the table
 
4423
        with field1 by the keys value of field2.
 
4424
      */
 
4425
      Item_equal_iterator fi(*item_equal);
 
4426
      while ((item= fi++))
 
4427
      {
 
4428
        Field *field= item->field;
 
4429
        while ((item= it++))
 
4430
        {
 
4431
          if (!field->eq(item->field))
 
4432
          {
 
4433
            add_key_field(key_fields, *and_level, cond_func, field,
 
4434
                          true, (Item **) &item, 1, usable_tables,
 
4435
                          sargables);
 
4436
          }
 
4437
        }
 
4438
        it.rewind();
 
4439
      }
 
4440
    }
 
4441
    break;
 
4442
  }
 
4443
}
493
4444
 
494
4445
/**
495
4446
  Add all keys with uses 'field' for some keypart.
496
4447
 
497
4448
  If field->and_level != and_level then only mark key_part as const_part.
498
4449
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4450
 
 
4451
static uint
 
4452
max_part_bit(key_part_map bits)
500
4453
{
501
4454
  uint32_t found;
502
4455
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4456
  return found;
504
4457
}
505
4458
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4459
static void
 
4460
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4461
{
 
4462
  Field *field=key_field->field;
 
4463
  Table *form= field->table;
 
4464
  KEYUSE keyuse;
 
4465
 
 
4466
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4467
  {
 
4468
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4469
    {
 
4470
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4471
        continue;
 
4472
 
 
4473
      uint32_t key_parts= (uint32_t) form->key_info[key].key_parts;
 
4474
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4475
      {
 
4476
        if (field->eq(form->key_info[key].key_part[part].field))
 
4477
        {
 
4478
          keyuse.table= field->table;
 
4479
          keyuse.val =  key_field->val;
 
4480
          keyuse.key =  key;
 
4481
          keyuse.keypart=part;
 
4482
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4483
          keyuse.used_tables=key_field->val->used_tables();
 
4484
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4485
          keyuse.null_rejecting= key_field->null_rejecting;
 
4486
          keyuse.cond_guard= key_field->cond_guard;
 
4487
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4488
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4489
        }
 
4490
      }
 
4491
    }
 
4492
  }
 
4493
}
 
4494
 
 
4495
static int
 
4496
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4497
{
508
4498
  int res;
509
 
  if (a->getTable()->tablenr != b->getTable()->tablenr)
510
 
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
511
 
  if (a->getKey() != b->getKey())
512
 
    return static_cast<int>((a->getKey() - b->getKey()));
513
 
  if (a->getKeypart() != b->getKeypart())
514
 
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
4499
  if (a->table->tablenr != b->table->tablenr)
 
4500
    return (int) (a->table->tablenr - b->table->tablenr);
 
4501
  if (a->key != b->key)
 
4502
    return (int) (a->key - b->key);
 
4503
  if (a->keypart != b->keypart)
 
4504
    return (int) (a->keypart - b->keypart);
515
4505
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4506
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4507
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4508
    return res;
519
4509
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
520
 
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
521
 
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
 
4510
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4511
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4512
}
 
4513
 
 
4514
 
 
4515
/*
 
4516
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4517
 
 
4518
    This function populates KEY_FIELD array with entries generated from the
 
4519
    ON condition of the given nested join, and does the same for nested joins
 
4520
    contained within this nested join.
 
4521
 
 
4522
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4523
  @param[in,out]  end                 End of the key field array
 
4524
  @param[in,out]  and_level           And-level
 
4525
  @param[in,out]  sargables           Array of found sargable candidates
 
4526
 
 
4527
 
 
4528
  @note
 
4529
    We can add accesses to the tables that are direct children of this nested
 
4530
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4531
 
 
4532
    Example for #1 (outer brackets pair denotes nested join this function is
 
4533
    invoked for):
 
4534
    @code
 
4535
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4536
    @endcode
 
4537
    Example for #2:
 
4538
    @code
 
4539
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4540
    @endcode
 
4541
    In examples 1-2 for condition cond, we can add 'ref' access candidates to
 
4542
    t1 only.
 
4543
    Example #3:
 
4544
    @code
 
4545
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4546
    @endcode
 
4547
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4548
*/
 
4549
 
 
4550
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4551
                                  KEY_FIELD **end, uint32_t *and_level,
 
4552
                                  SARGABLE_PARAM **sargables)
 
4553
{
 
4554
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4555
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4556
  bool have_another = false;
 
4557
  table_map tables= 0;
 
4558
  TableList *table;
 
4559
  assert(nested_join_table->nested_join);
 
4560
 
 
4561
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4562
                                            (table= li++))))
 
4563
  {
 
4564
    if (table->nested_join)
 
4565
    {
 
4566
      if (!table->on_expr)
 
4567
      {
 
4568
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4569
        have_another= true;
 
4570
        li2= li;
 
4571
        li= List_iterator<TableList>(table->nested_join->join_list);
 
4572
      }
 
4573
      else
 
4574
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4575
    }
 
4576
    else
 
4577
      if (!table->on_expr)
 
4578
        tables |= table->table->map;
 
4579
  }
 
4580
  if (nested_join_table->on_expr)
 
4581
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4582
                   sargables);
522
4583
}
523
4584
 
524
4585
 
526
4587
  Update keyuse array with all possible keys we can use to fetch rows.
527
4588
 
528
4589
  @param       session
529
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4590
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4591
  @param       join_tab       Array in tablenr_order
531
4592
  @param       tables         Number of tables in join
532
4593
  @param       cond           WHERE condition (note that the function analyzes
535
4596
                              for which we can make ref access based the WHERE
536
4597
                              clause)
537
4598
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
 
4599
  @param[out]  sargables      Array of found sargable candidates
539
4600
 
540
4601
   @retval
541
4602
     0  OK
542
4603
   @retval
543
4604
     1  Out of memory.
544
4605
*/
545
 
bool update_ref_and_keys(Session *session,
546
 
                         DYNAMIC_ARRAY *keyuse,
547
 
                         JoinTable *join_tab,
548
 
                         uint32_t tables,
549
 
                         COND *cond, 
550
 
                         COND_EQUAL *,
551
 
                         table_map normal_tables,
552
 
                         Select_Lex *select_lex,
553
 
                         vector<optimizer::SargableParam> &sargables)
 
4606
 
 
4607
static bool
 
4608
update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4609
                    uint32_t tables, COND *cond, COND_EQUAL *,
 
4610
                    table_map normal_tables, Select_Lex *select_lex,
 
4611
                    SARGABLE_PARAM **sargables)
554
4612
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
 
4613
  uint  and_level,i,found_eq_constant;
 
4614
  KEY_FIELD *key_fields, *end, *field;
557
4615
  uint32_t sz;
558
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
 
4616
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
559
4617
 
560
4618
  /*
561
 
    All predicates that are used to fill arrays of KeyField
562
 
    and SargableParam classes have at most 2 arguments
 
4619
    We use the same piece of memory to store both  KEY_FIELD
 
4620
    and SARGABLE_PARAM structure.
 
4621
    KEY_FIELD values are placed at the beginning this memory
 
4622
    while  SARGABLE_PARAM values are put at the end.
 
4623
    All predicates that are used to fill arrays of KEY_FIELD
 
4624
    and SARGABLE_PARAM structures have at most 2 arguments
563
4625
    except BETWEEN predicates that have 3 arguments and
564
4626
    IN predicates.
565
4627
    This any predicate if it's not BETWEEN/IN can be used
566
 
    directly to fill at most 2 array elements, either of KeyField 
567
 
    or SargableParam type. For a BETWEEN predicate 3 elements
 
4628
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4629
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4630
    can be filled as this predicate is considered as
569
4631
    saragable with respect to each of its argument.
570
4632
    An IN predicate can require at most 1 element as currently
574
4636
    can be not more than select_lex->max_equal_elems such
575
4637
    substitutions.
576
4638
  */
577
 
  sz= sizeof(optimizer::KeyField) *
578
 
      (((session->getLex()->current_select->cond_count+1)*2 +
579
 
        session->getLex()->current_select->between_count)*m+1);
580
 
  if (! (key_fields= (optimizer::KeyField*) session->getMemRoot()->allocate(sz)))
581
 
    return true;
 
4639
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4640
      (((session->lex->current_select->cond_count+1)*2 +
 
4641
        session->lex->current_select->between_count)*m+1);
 
4642
  if (!(key_fields=(KEY_FIELD*) session->alloc(sz)))
 
4643
    return true; /* purecov: inspected */
582
4644
  and_level= 0;
583
4645
  field= end= key_fields;
 
4646
  *sargables= (SARGABLE_PARAM *) key_fields +
 
4647
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4648
  /* set a barrier for the array of SARGABLE_PARAM */
 
4649
  (*sargables)[0].field= 0;
584
4650
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4651
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4652
    return true;
587
4653
  if (cond)
588
4654
  {
589
4655
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4656
                   sargables);
591
 
    for (; field != end; field++)
 
4657
    for (; field != end ; field++)
592
4658
    {
593
 
      add_key_part(keyuse, field);
 
4659
      add_key_part(keyuse,field);
594
4660
      /* Mark that we can optimize LEFT JOIN */
595
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
596
 
          ! field->getField()->real_maybe_null())
597
 
      {
598
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
599
 
      }
 
4661
      if (field->val->type() == Item::NULL_ITEM &&
 
4662
          !field->field->real_maybe_null())
 
4663
        field->field->table->reginfo.not_exists_optimize=1;
600
4664
    }
601
4665
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4666
  for (i=0 ; i < tables ; i++)
603
4667
  {
604
4668
    /*
605
4669
      Block the creation of keys for inner tables of outer joins.
618
4682
 
619
4683
  /* Process ON conditions for the nested joins */
620
4684
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
4685
    List_iterator<TableList> li(*join_tab->join->join_list);
622
4686
    TableList *table;
623
4687
    while ((table= li++))
624
4688
    {
625
 
      if (table->getNestedJoin())
 
4689
      if (table->nested_join)
626
4690
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
627
4691
                              sargables);
628
4692
    }
643
4707
  */
644
4708
  if (keyuse->elements)
645
4709
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4710
    KEYUSE key_end,*prev,*save_pos,*use;
647
4711
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
 
4712
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4713
          (qsort_cmp) sort_keyuse);
650
4714
 
651
4715
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
4716
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
4717
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4718
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4719
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4720
    found_eq_constant=0;
 
4721
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4722
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4723
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4724
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4725
      {
662
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
663
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
664
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
665
 
        {
666
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
667
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
668
 
            continue;                           /* remove */
669
 
        }
670
 
        else if (use->getKeypart() != 0)                // First found must be 0
671
 
          continue;
 
4726
        if (use->key == prev->key && use->table == prev->table)
 
4727
        {
 
4728
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4729
            continue;                           /* remove */
 
4730
        }
 
4731
        else if (use->keypart != 0)             // First found must be 0
 
4732
          continue;
 
4733
      }
672
4734
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4735
#ifdef HAVE_purify
 
4736
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4737
      if (save_pos != use)
676
4738
#endif
677
 
          *save_pos= *use;
678
 
        prev=use;
679
 
        found_eq_constant= ! use->getUsedTables();
680
 
        /* Save ptr to first use */
681
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
682
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
683
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
684
 
        save_pos++;
685
 
      }
686
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
687
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
688
 
      keyuse->elements= i;
 
4739
        *save_pos= *use;
 
4740
      prev=use;
 
4741
      found_eq_constant= !use->used_tables;
 
4742
      /* Save ptr to first use */
 
4743
      if (!use->table->reginfo.join_tab->keyuse)
 
4744
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4745
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4746
      save_pos++;
689
4747
    }
 
4748
    i=(uint32_t) (save_pos-(KEYUSE*) keyuse->buffer);
 
4749
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4750
    keyuse->elements=i;
690
4751
  }
691
4752
  return false;
692
4753
}
694
4755
/**
695
4756
  Update some values in keyuse for faster choose_plan() loop.
696
4757
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4758
 
 
4759
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4760
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4761
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4762
 
703
4763
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4764
  {
711
4771
      Constant tables are ignored.
712
4772
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4773
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4774
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4775
    if (keyuse->used_tables &
 
4776
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4777
               ~OUTER_REF_TABLE_BIT)))
716
4778
    {
717
4779
      uint32_t tablenr;
718
4780
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4781
      if (map == 1)                     // Only one table
720
4782
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4783
        Table *tmp_table=join->all_tables[tablenr];
 
4784
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
723
4785
      }
724
4786
    }
725
4787
    /*
726
4788
      Outer reference (external field) is constant for single executing
727
4789
      of subquery
728
4790
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4791
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4792
      keyuse->ref_table_rows= 1;
731
4793
  }
732
4794
}
733
4795
 
749
4811
  @return
750
4812
    None
751
4813
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4814
 
 
4815
static void
 
4816
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4817
{
754
4818
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4819
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4820
  order_st      *cur_group;
757
4821
  Item_field *cur_item;
758
4822
  key_map possible_keys(0);
759
4823
 
766
4830
  else if (join->select_distinct)
767
4831
  { /* Collect all query fields referenced in the SELECT clause. */
768
4832
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4833
    List_iterator<Item> select_items_it(select_items);
770
4834
    Item *item;
771
4835
    while ((item= select_items_it++))
772
4836
      item->walk(&Item::collect_item_field_processor, 0,
780
4844
 
781
4845
  /* Intersect the keys of all group fields. */
782
4846
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4847
  possible_keys.merge(cur_item->field->part_of_key);
784
4848
  while ((cur_item= indexed_fields_it++))
785
4849
  {
786
 
    possible_keys&= cur_item->field->part_of_key;
787
 
  }
788
 
 
789
 
  if (possible_keys.any())
790
 
    join_tab->const_keys|= possible_keys;
791
 
}
792
 
 
793
 
/**
794
 
  Compare two JoinTable objects based on the number of accessed records.
795
 
 
796
 
  @param ptr1 pointer to first JoinTable object
797
 
  @param ptr2 pointer to second JoinTable object
 
4850
    possible_keys.intersect(cur_item->field->part_of_key);
 
4851
  }
 
4852
 
 
4853
  if (!possible_keys.is_clear_all())
 
4854
    join_tab->const_keys.merge(possible_keys);
 
4855
}
 
4856
 
 
4857
 
 
4858
/*****************************************************************************
 
4859
  Go through all combinations of not marked tables and find the one
 
4860
  which uses least records
 
4861
*****************************************************************************/
 
4862
 
 
4863
/** Save const tables first as used tables. */
 
4864
 
 
4865
static void
 
4866
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4867
{
 
4868
  join->positions[idx].table= table;
 
4869
  join->positions[idx].key=key;
 
4870
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4871
  join->positions[idx].ref_depend_map= 0;
 
4872
 
 
4873
  /* Move the const table as down as possible in best_ref */
 
4874
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4875
  JOIN_TAB *next=join->best_ref[idx];
 
4876
  for (;next != table ; pos++)
 
4877
  {
 
4878
    JOIN_TAB *tmp=pos[0];
 
4879
    pos[0]=next;
 
4880
    next=tmp;
 
4881
  }
 
4882
  join->best_ref[idx]=table;
 
4883
}
 
4884
 
 
4885
 
 
4886
/*
 
4887
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4888
 
 
4889
  SYNOPSIS
 
4890
    get_bound_sj_equalities()
 
4891
      sj_nest           Semi-join nest
 
4892
      remaining_tables  Tables that are not yet bound
 
4893
 
 
4894
  DESCRIPTION
 
4895
    Given a semi-join nest, find out which of the IN-equalities have their
 
4896
    left part expression bound (i.e. the said expression doesn't refer to
 
4897
    any of remaining_tables and can be evaluated).
 
4898
 
 
4899
  RETURN
 
4900
    Bitmap of bound IN-equalities.
 
4901
*/
 
4902
 
 
4903
uint64_t get_bound_sj_equalities(TableList *sj_nest,
 
4904
                                  table_map remaining_tables)
 
4905
{
 
4906
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4907
  Item *item;
 
4908
  uint32_t i= 0;
 
4909
  uint64_t res= 0;
 
4910
  while ((item= li++))
 
4911
  {
 
4912
    /*
 
4913
      Q: should this take into account equality propagation and how?
 
4914
      A: If e->outer_side is an Item_field, walk over the equality
 
4915
         class and see if there is an element that is bound?
 
4916
      (this is an optional feature)
 
4917
    */
 
4918
    if (!(item->used_tables() & remaining_tables))
 
4919
    {
 
4920
      res |= 1UL < i;
 
4921
    }
 
4922
  }
 
4923
  return res;
 
4924
}
 
4925
 
 
4926
 
 
4927
/**
 
4928
  Find the best access path for an extension of a partial execution
 
4929
  plan and add this path to the plan.
 
4930
 
 
4931
  The function finds the best access path to table 's' from the passed
 
4932
  partial plan where an access path is the general term for any means to
 
4933
  access the data in 's'. An access path may use either an index or a scan,
 
4934
  whichever is cheaper. The input partial plan is passed via the array
 
4935
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4936
  cost are stored in 'join->positions[idx]'.
 
4937
 
 
4938
  @param join             pointer to the structure providing all context info
 
4939
                          for the query
 
4940
  @param s                the table to be joined by the function
 
4941
  @param session              thread for the connection that submitted the query
 
4942
  @param remaining_tables set of tables not included into the partial plan yet
 
4943
  @param idx              the length of the partial plan
 
4944
  @param record_count     estimate for the number of records returned by the
 
4945
                          partial plan
 
4946
  @param read_time        the cost of the partial plan
 
4947
 
 
4948
  @return
 
4949
    None
 
4950
*/
 
4951
 
 
4952
static void
 
4953
best_access_path(JOIN      *join,
 
4954
                 JOIN_TAB  *s,
 
4955
                 Session       *session,
 
4956
                 table_map remaining_tables,
 
4957
                 uint32_t      idx,
 
4958
                 double    record_count,
 
4959
                 double)
 
4960
{
 
4961
  KEYUSE *best_key=         0;
 
4962
  uint32_t best_max_key_part=   0;
 
4963
  bool found_constraint= 0;
 
4964
  double best=              DBL_MAX;
 
4965
  double best_time=         DBL_MAX;
 
4966
  double records=           DBL_MAX;
 
4967
  table_map best_ref_depends_map= 0;
 
4968
  double tmp;
 
4969
  ha_rows rec;
 
4970
  uint32_t best_is_sj_inside_out=    0;
 
4971
 
 
4972
  if (s->keyuse)
 
4973
  {                                            /* Use key if possible */
 
4974
    Table *table= s->table;
 
4975
    KEYUSE *keyuse,*start_key=0;
 
4976
    double best_records= DBL_MAX;
 
4977
    uint32_t max_key_part=0;
 
4978
    uint64_t bound_sj_equalities= 0;
 
4979
    bool try_sj_inside_out= false;
 
4980
    /*
 
4981
      Discover the bound equalites. We need to do this, if
 
4982
        1. The next table is an SJ-inner table, and
 
4983
        2. It is the first table from that semijoin, and
 
4984
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4985
           all or none of their tables in join_table_map), except
 
4986
           s->emb_sj_nest (which we've just entered).
 
4987
        3. All correlation references from this sj-nest are bound
 
4988
    */
 
4989
    if (s->emb_sj_nest &&                                                 // (1)
 
4990
        s->emb_sj_nest->sj_in_exprs < 64 &&
 
4991
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4992
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4993
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4994
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4995
    {
 
4996
      /* This table is an InsideOut scan candidate */
 
4997
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
 
4998
                                                   remaining_tables);
 
4999
      try_sj_inside_out= true;
 
5000
    }
 
5001
 
 
5002
    /* Test how we can use keys */
 
5003
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
5004
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
5005
    {
 
5006
      key_part_map found_part= 0;
 
5007
      table_map found_ref= 0;
 
5008
      uint32_t key= keyuse->key;
 
5009
      KEY *keyinfo= table->key_info+key;
 
5010
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
5011
      key_part_map const_part= 0;
 
5012
      /* The or-null keypart in ref-or-null access: */
 
5013
      key_part_map ref_or_null_part= 0;
 
5014
 
 
5015
      /* Calculate how many key segments of the current key we can use */
 
5016
      start_key= keyuse;
 
5017
      uint64_t handled_sj_equalities=0;
 
5018
      key_part_map sj_insideout_map= 0;
 
5019
 
 
5020
      do /* For each keypart */
 
5021
      {
 
5022
        uint32_t keypart= keyuse->keypart;
 
5023
        table_map best_part_found_ref= 0;
 
5024
        double best_prev_record_reads= DBL_MAX;
 
5025
 
 
5026
        do /* For each way to access the keypart */
 
5027
        {
 
5028
 
 
5029
          /*
 
5030
            if 1. expression doesn't refer to forward tables
 
5031
               2. we won't get two ref-or-null's
 
5032
          */
 
5033
          if (!(remaining_tables & keyuse->used_tables) &&
 
5034
              !(ref_or_null_part && (keyuse->optimize &
 
5035
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5036
          {
 
5037
            found_part|= keyuse->keypart_map;
 
5038
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5039
              const_part|= keyuse->keypart_map;
 
5040
 
 
5041
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5042
                                                      keyuse->used_tables));
 
5043
            if (tmp2 < best_prev_record_reads)
 
5044
            {
 
5045
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5046
              best_prev_record_reads= tmp2;
 
5047
            }
 
5048
            if (rec > keyuse->ref_table_rows)
 
5049
              rec= keyuse->ref_table_rows;
 
5050
            /*
 
5051
              If there is one 'key_column IS NULL' expression, we can
 
5052
              use this ref_or_null optimisation of this field
 
5053
            */
 
5054
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5055
              ref_or_null_part |= keyuse->keypart_map;
 
5056
          }
 
5057
 
 
5058
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5059
          {
 
5060
            if (!(remaining_tables & keyuse->used_tables))
 
5061
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5062
            else
 
5063
            {
 
5064
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5065
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5066
            }
 
5067
          }
 
5068
 
 
5069
          keyuse++;
 
5070
        } while (keyuse->table == table && keyuse->key == key &&
 
5071
                 keyuse->keypart == keypart);
 
5072
        found_ref|= best_part_found_ref;
 
5073
      } while (keyuse->table == table && keyuse->key == key);
 
5074
 
 
5075
      /*
 
5076
        Assume that that each key matches a proportional part of table.
 
5077
      */
 
5078
      if (!found_part && !handled_sj_equalities)
 
5079
        continue;                               // Nothing usable found
 
5080
 
 
5081
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5082
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5083
 
 
5084
      bool sj_inside_out_scan= false;
 
5085
      {
 
5086
        found_constraint= 1;
 
5087
        /*
 
5088
          Check if InsideOut scan is applicable:
 
5089
          1. All IN-equalities are either "bound" or "handled"
 
5090
          2. Index keyparts are
 
5091
             ...
 
5092
        */
 
5093
        if (try_sj_inside_out &&
 
5094
            table->covering_keys.is_set(key) &&
 
5095
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5096
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5097
        {
 
5098
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5099
          if (n_fixed_parts != keyinfo->key_parts &&
 
5100
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5101
               PREV_BITS(uint, keyinfo->key_parts))
 
5102
          {
 
5103
            /*
 
5104
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5105
              check if all of them are covered.
 
5106
            */
 
5107
            sj_inside_out_scan= true;
 
5108
            if (!n_fixed_parts)
 
5109
            {
 
5110
              /*
 
5111
                It's a confluent ref scan.
 
5112
 
 
5113
                That is, all found KEYUSE elements refer to IN-equalities,
 
5114
                and there is really no ref access because there is no
 
5115
                  t.keypart0 = {bound expression}
 
5116
 
 
5117
                Calculate the cost of complete loose index scan.
 
5118
              */
 
5119
              records= (double)s->table->file->stats.records;
 
5120
 
 
5121
              /* The cost is entire index scan cost (divided by 2) */
 
5122
              best_time= s->table->file->index_only_read_time(key, records);
 
5123
 
 
5124
              /* Now figure how many different keys we will get */
 
5125
              ulong rpc;
 
5126
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5127
                records= records / rpc;
 
5128
              start_key= NULL;
 
5129
            }
 
5130
          }
 
5131
        }
 
5132
 
 
5133
        /*
 
5134
          Check if we found full key
 
5135
        */
 
5136
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5137
            !ref_or_null_part)
 
5138
        {                                         /* use eq key */
 
5139
          max_key_part= UINT32_MAX;
 
5140
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5141
          {
 
5142
            tmp = prev_record_reads(join, idx, found_ref);
 
5143
            records=1.0;
 
5144
          }
 
5145
          else
 
5146
          {
 
5147
            if (!found_ref)
 
5148
            {                                     /* We found a const key */
 
5149
              /*
 
5150
                ReuseRangeEstimateForRef-1:
 
5151
                We get here if we've found a ref(const) (c_i are constants):
 
5152
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5153
 
 
5154
                If range optimizer was able to construct a "range"
 
5155
                access on this index, then its condition "quick_cond" was
 
5156
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5157
                from the range optimizer.
 
5158
 
 
5159
                Proof of (*): By properties of range and ref optimizers
 
5160
                quick_cond will be equal or tighther than ref_const_cond.
 
5161
                ref_const_cond already covers "smallest" possible interval -
 
5162
                a singlepoint interval over all keyparts. Therefore,
 
5163
                quick_cond is equivalent to ref_const_cond (if it was an
 
5164
                empty interval we wouldn't have got here).
 
5165
              */
 
5166
              if (table->quick_keys.is_set(key))
 
5167
                records= (double) table->quick_rows[key];
 
5168
              else
 
5169
              {
 
5170
                /* quick_range couldn't use key! */
 
5171
                records= (double) s->records/rec;
 
5172
              }
 
5173
            }
 
5174
            else
 
5175
            {
 
5176
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5177
              {                                   /* Prefer longer keys */
 
5178
                records=
 
5179
                  ((double) s->records / (double) rec *
 
5180
                   (1.0 +
 
5181
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5182
                     (double) table->s->max_key_length)));
 
5183
                if (records < 2.0)
 
5184
                  records=2.0;               /* Can't be as good as a unique */
 
5185
              }
 
5186
              /*
 
5187
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5188
                E(#rows) from range optimizer. Make another try:
 
5189
 
 
5190
                If range optimizer produced E(#rows) for a prefix of the ref
 
5191
                access we're considering, and that E(#rows) is lower then our
 
5192
                current estimate, make an adjustment. The criteria of when we
 
5193
                can make an adjustment is a special case of the criteria used
 
5194
                in ReuseRangeEstimateForRef-3.
 
5195
              */
 
5196
              if (table->quick_keys.is_set(key) &&
 
5197
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5198
                  table->quick_n_ranges[key] == 1 &&
 
5199
                  records > (double) table->quick_rows[key])
 
5200
              {
 
5201
                records= (double) table->quick_rows[key];
 
5202
              }
 
5203
            }
 
5204
            /* Limit the number of matched rows */
 
5205
            tmp= records;
 
5206
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5207
            if (table->covering_keys.is_set(key))
 
5208
            {
 
5209
              /* we can use only index tree */
 
5210
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5211
            }
 
5212
            else
 
5213
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5214
          }
 
5215
        }
 
5216
        else
 
5217
        {
 
5218
          /*
 
5219
            Use as much key-parts as possible and a uniq key is better
 
5220
            than a not unique key
 
5221
            Set tmp to (previous record count) * (records / combination)
 
5222
          */
 
5223
          if ((found_part & 1) &&
 
5224
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5225
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5226
          {
 
5227
            max_key_part= max_part_bit(found_part);
 
5228
            /*
 
5229
              ReuseRangeEstimateForRef-3:
 
5230
              We're now considering a ref[or_null] access via
 
5231
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
 
5232
              (same-as-above but with one cond replaced
 
5233
               with "t.keypart_i IS NULL")]  (**)
 
5234
 
 
5235
              Try re-using E(#rows) from "range" optimizer:
 
5236
              We can do so if "range" optimizer used the same intervals as
 
5237
              in (**). The intervals used by range optimizer may be not
 
5238
              available at this point (as "range" access might have choosen to
 
5239
              create quick select over another index), so we can't compare
 
5240
              them to (**). We'll make indirect judgements instead.
 
5241
              The sufficient conditions for re-use are:
 
5242
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5243
                   this is not satisfied we have no way to know which ranges
 
5244
                   will be actually scanned by 'ref' until we execute the
 
5245
                   join)
 
5246
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5247
                   is apparently a necessary requirement)
 
5248
 
 
5249
              We also have a property that "range optimizer produces equal or
 
5250
              tighter set of scan intervals than ref(const) optimizer". Each
 
5251
              of the intervals in (**) are "tightest possible" intervals when
 
5252
              one limits itself to using keyparts 1..K (which we do in #2).
 
5253
              From here it follows that range access used either one, or
 
5254
              both of the (I1) and (I2) intervals:
 
5255
 
 
5256
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
 
5257
               (same-as-above but with one cond replaced
 
5258
                with "t.keypart_i IS NULL")               (I2)
 
5259
 
 
5260
              The remaining part is to exclude the situation where range
 
5261
              optimizer used one interval while we're considering
 
5262
              ref-or-null and looking for estimate for two intervals. This
 
5263
              is done by last limitation:
 
5264
 
 
5265
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5266
            */
 
5267
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5268
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5269
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5270
            {
 
5271
              tmp= records= (double) table->quick_rows[key];
 
5272
            }
 
5273
            else
 
5274
            {
 
5275
              /* Check if we have statistic about the distribution */
 
5276
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5277
              {
 
5278
                /*
 
5279
                  Fix for the case where the index statistics is too
 
5280
                  optimistic: If
 
5281
                  (1) We're considering ref(const) and there is quick select
 
5282
                      on the same index,
 
5283
                  (2) and that quick select uses more keyparts (i.e. it will
 
5284
                      scan equal/smaller interval then this ref(const))
 
5285
                  (3) and E(#rows) for quick select is higher then our
 
5286
                      estimate,
 
5287
                  Then
 
5288
                    We'll use E(#rows) from quick select.
 
5289
 
 
5290
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5291
                  cheaper in some cases ?
 
5292
                  TODO: figure this out and adjust the plan choice if needed.
 
5293
                */
 
5294
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5295
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5296
                    records < (double)table->quick_rows[key])         // (3)
 
5297
                  records= (double)table->quick_rows[key];
 
5298
 
 
5299
                tmp= records;
 
5300
              }
 
5301
              else
 
5302
              {
 
5303
                /*
 
5304
                  Assume that the first key part matches 1% of the file
 
5305
                  and that the whole key matches 10 (duplicates) or 1
 
5306
                  (unique) records.
 
5307
                  Assume also that more key matches proportionally more
 
5308
                  records
 
5309
                  This gives the formula:
 
5310
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5311
 
 
5312
                  b = records matched by whole key
 
5313
                  a = records matched by first key part (1% of all records?)
 
5314
                  c = number of key parts in key
 
5315
                  x = used key parts (1 <= x <= c)
 
5316
                */
 
5317
                double rec_per_key;
 
5318
                if (!(rec_per_key=(double)
 
5319
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5320
                  rec_per_key=(double) s->records/rec+1;
 
5321
 
 
5322
                if (!s->records)
 
5323
                  tmp = 0;
 
5324
                else if (rec_per_key/(double) s->records >= 0.01)
 
5325
                  tmp = rec_per_key;
 
5326
                else
 
5327
                {
 
5328
                  double a=s->records*0.01;
 
5329
                  if (keyinfo->key_parts > 1)
 
5330
                    tmp= (max_key_part * (rec_per_key - a) +
 
5331
                          a*keyinfo->key_parts - rec_per_key)/
 
5332
                         (keyinfo->key_parts-1);
 
5333
                  else
 
5334
                    tmp= a;
 
5335
                  set_if_bigger(tmp,1.0);
 
5336
                }
 
5337
                records = (uint32_t) tmp;
 
5338
              }
 
5339
 
 
5340
              if (ref_or_null_part)
 
5341
              {
 
5342
                /* We need to do two key searches to find key */
 
5343
                tmp *= 2.0;
 
5344
                records *= 2.0;
 
5345
              }
 
5346
 
 
5347
              /*
 
5348
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5349
                E(#rows) from range optimizer. Make another try:
 
5350
 
 
5351
                If range optimizer produced E(#rows) for a prefix of the ref
 
5352
                access we're considering, and that E(#rows) is lower then our
 
5353
                current estimate, make the adjustment.
 
5354
 
 
5355
                The decision whether we can re-use the estimate from the range
 
5356
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5357
                applied to first table->quick_key_parts[key] key parts.
 
5358
              */
 
5359
              if (table->quick_keys.is_set(key) &&
 
5360
                  table->quick_key_parts[key] <= max_key_part &&
 
5361
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5362
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
5363
                                                     const_part) ? 1 : 0) &&
 
5364
                  records > (double) table->quick_rows[key])
 
5365
              {
 
5366
                tmp= records= (double) table->quick_rows[key];
 
5367
              }
 
5368
            }
 
5369
 
 
5370
            /* Limit the number of matched rows */
 
5371
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5372
            if (table->covering_keys.is_set(key))
 
5373
            {
 
5374
              /* we can use only index tree */
 
5375
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5376
            }
 
5377
            else
 
5378
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5379
          }
 
5380
          else
 
5381
            tmp= best_time;                    // Do nothing
 
5382
        }
 
5383
 
 
5384
        if (sj_inside_out_scan && !start_key)
 
5385
        {
 
5386
          tmp= tmp/2;
 
5387
          if (records)
 
5388
            records= records/2;
 
5389
        }
 
5390
 
 
5391
      }
 
5392
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5393
      {
 
5394
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5395
        best= tmp;
 
5396
        best_records= records;
 
5397
        best_key= start_key;
 
5398
        best_max_key_part= max_key_part;
 
5399
        best_ref_depends_map= found_ref;
 
5400
        best_is_sj_inside_out= sj_inside_out_scan;
 
5401
      }
 
5402
    }
 
5403
    records= best_records;
 
5404
  }
 
5405
 
 
5406
  /*
 
5407
    Don't test table scan if it can't be better.
 
5408
    Prefer key lookup if we would use the same key for scanning.
 
5409
 
 
5410
    Don't do a table scan on InnoDB tables, if we can read the used
 
5411
    parts of the row from any of the used index.
 
5412
    This is because table scans uses index and we would not win
 
5413
    anything by using a table scan.
 
5414
 
 
5415
    A word for word translation of the below if-statement in sergefp's
 
5416
    understanding: we check if we should use table scan if:
 
5417
    (1) The found 'ref' access produces more records than a table scan
 
5418
        (or index scan, or quick select), or 'ref' is more expensive than
 
5419
        any of them.
 
5420
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5421
        'range' access using index IDX, and the best way to perform 'ref'
 
5422
        access is to use the same index IDX, with the same or more key parts.
 
5423
        (note: it is not clear how this rule is/should be extended to
 
5424
        index_merge quick selects)
 
5425
    (3) See above note about InnoDB.
 
5426
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5427
             path, but there is no quick select)
 
5428
        If the condition in the above brackets holds, then the only possible
 
5429
        "table scan" access method is ALL/index (there is no quick select).
 
5430
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5431
        choose it over ALL/index, there is no need to consider a full table
 
5432
        scan.
 
5433
  */
 
5434
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5435
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5436
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5437
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5438
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5439
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5440
  {                                             // Check full join
 
5441
    ha_rows rnd_records= s->found_records;
 
5442
    /*
 
5443
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5444
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5445
      preceding this table in the join order we're now considering), then
 
5446
      assume that 25% of the rows will be filtered out by this condition.
 
5447
 
 
5448
      This heuristic is supposed to force tables used in exprZ to be before
 
5449
      this table in join order.
 
5450
    */
 
5451
    if (found_constraint)
 
5452
      rnd_records-= rnd_records/4;
 
5453
 
 
5454
    /*
 
5455
      If applicable, get a more accurate estimate. Don't use the two
 
5456
      heuristics at once.
 
5457
    */
 
5458
    if (s->table->quick_condition_rows != s->found_records)
 
5459
      rnd_records= s->table->quick_condition_rows;
 
5460
 
 
5461
    /*
 
5462
      Range optimizer never proposes a RANGE if it isn't better
 
5463
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5464
      Here we estimate its cost.
 
5465
    */
 
5466
    if (s->quick)
 
5467
    {
 
5468
      /*
 
5469
        For each record we:
 
5470
        - read record range through 'quick'
 
5471
        - skip rows which does not satisfy WHERE constraints
 
5472
        TODO:
 
5473
        We take into account possible use of join cache for ALL/index
 
5474
        access (see first else-branch below), but we don't take it into
 
5475
        account here for range/index_merge access. Find out why this is so.
 
5476
      */
 
5477
      tmp= record_count *
 
5478
        (s->quick->read_time +
 
5479
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5480
    }
 
5481
    else
 
5482
    {
 
5483
      /* Estimate cost of reading table. */
 
5484
      tmp= s->table->file->scan_time();
 
5485
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5486
      {
 
5487
        /*
 
5488
          For each record we have to:
 
5489
          - read the whole table record
 
5490
          - skip rows which does not satisfy join condition
 
5491
        */
 
5492
        tmp= record_count *
 
5493
          (tmp +
 
5494
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5495
      }
 
5496
      else
 
5497
      {
 
5498
        /* We read the table as many times as join buffer becomes full. */
 
5499
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5500
                           record_count /
 
5501
                           (double) session->variables.join_buff_size));
 
5502
        /*
 
5503
            We don't make full cartesian product between rows in the scanned
 
5504
           table and existing records because we skip all rows from the
 
5505
           scanned table, which does not satisfy join condition when
 
5506
           we read the table (see flush_cached_records for details). Here we
 
5507
           take into account cost to read and skip these records.
 
5508
        */
 
5509
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5510
      }
 
5511
    }
 
5512
 
 
5513
    /*
 
5514
      We estimate the cost of evaluating WHERE clause for found records
 
5515
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5516
      tmp give us total cost of using Table SCAN
 
5517
    */
 
5518
    if (best == DBL_MAX ||
 
5519
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5520
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5521
    {
 
5522
      /*
 
5523
        If the table has a range (s->quick is set) make_join_select()
 
5524
        will ensure that this will be used
 
5525
      */
 
5526
      best= tmp;
 
5527
      records= rows2double(rnd_records);
 
5528
      best_key= 0;
 
5529
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5530
      best_ref_depends_map= 0;
 
5531
      best_is_sj_inside_out= false;
 
5532
    }
 
5533
  }
 
5534
 
 
5535
  /* Update the cost information for the current partial plan */
 
5536
  join->positions[idx].records_read= records;
 
5537
  join->positions[idx].read_time=    best;
 
5538
  join->positions[idx].key=          best_key;
 
5539
  join->positions[idx].table=        s;
 
5540
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5541
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5542
 
 
5543
  if (!best_key &&
 
5544
      idx == join->const_tables &&
 
5545
      s->table == join->sort_by_table &&
 
5546
      join->unit->select_limit_cnt >= records)
 
5547
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5548
 
 
5549
  return;
 
5550
}
 
5551
 
 
5552
 
 
5553
/**
 
5554
  Selects and invokes a search strategy for an optimal query plan.
 
5555
 
 
5556
  The function checks user-configurable parameters that control the search
 
5557
  strategy for an optimal plan, selects the search method and then invokes
 
5558
  it. Each specific optimization procedure stores the final optimal plan in
 
5559
  the array 'join->best_positions', and the cost of the plan in
 
5560
  'join->best_read'.
 
5561
 
 
5562
  @param join         pointer to the structure providing all context info for
 
5563
                      the query
 
5564
  @param join_tables  set of the tables in the query
 
5565
 
 
5566
  @todo
 
5567
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5568
    the greedy version. Will be removed when greedy_search is approved.
 
5569
 
 
5570
  @retval
 
5571
    false       ok
 
5572
  @retval
 
5573
    true        Fatal error
 
5574
*/
 
5575
 
 
5576
static bool
 
5577
choose_plan(JOIN *join, table_map join_tables)
 
5578
{
 
5579
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
 
5580
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
 
5581
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5582
 
 
5583
  join->cur_embedding_map= 0;
 
5584
  reset_nj_counters(join->join_list);
 
5585
  /*
 
5586
    if (SELECT_STRAIGHT_JOIN option is set)
 
5587
      reorder tables so dependent tables come after tables they depend
 
5588
      on, otherwise keep tables in the order they were specified in the query
 
5589
    else
 
5590
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5591
      records accessed.
 
5592
  */
 
5593
  my_qsort(join->best_ref + join->const_tables,
 
5594
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5595
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5596
  join->cur_emb_sj_nests= 0;
 
5597
  if (straight_join)
 
5598
  {
 
5599
    optimize_straight_join(join, join_tables);
 
5600
  }
 
5601
  else
 
5602
  {
 
5603
    if (search_depth == MAX_TABLES+2)
 
5604
    { /*
 
5605
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5606
        the greedy version. Will be removed when greedy_search is approved.
 
5607
      */
 
5608
      join->best_read= DBL_MAX;
 
5609
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5610
        return(true);
 
5611
    }
 
5612
    else
 
5613
    {
 
5614
      if (search_depth == 0)
 
5615
        /* Automatically determine a reasonable value for 'search_depth' */
 
5616
        search_depth= determine_search_depth(join);
 
5617
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5618
        return(true);
 
5619
    }
 
5620
  }
 
5621
 
 
5622
  /*
 
5623
    Store the cost of this query into a user variable
 
5624
    Don't update last_query_cost for statements that are not "flat joins" :
 
5625
    i.e. they have subqueries, unions or call stored procedures.
 
5626
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5627
  */
 
5628
  if (join->session->lex->is_single_level_stmt())
 
5629
    join->session->status_var.last_query_cost= join->best_read;
 
5630
  return(false);
 
5631
}
 
5632
 
 
5633
 
 
5634
/**
 
5635
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5636
 
 
5637
  @param ptr1 pointer to first JOIN_TAB object
 
5638
  @param ptr2 pointer to second JOIN_TAB object
798
5639
 
799
5640
  NOTES
800
5641
    The order relation implemented by join_tab_cmp() is not transitive,
814
5655
  @retval
815
5656
    0  if equal
816
5657
*/
 
5658
 
817
5659
int join_tab_cmp(const void* ptr1, const void* ptr2)
818
5660
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5661
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5662
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5663
 
822
5664
  if (jt1->dependent & jt2->table->map)
823
5665
    return 1;
830
5672
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5673
}
832
5674
 
 
5675
 
833
5676
/**
834
5677
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5678
*/
 
5679
 
836
5680
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5681
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5682
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5683
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5684
 
841
5685
  if (jt1->dependent & jt2->table->map)
842
5686
    return 1;
846
5690
}
847
5691
 
848
5692
/**
 
5693
  Heuristic procedure to automatically guess a reasonable degree of
 
5694
  exhaustiveness for the greedy search procedure.
 
5695
 
 
5696
  The procedure estimates the optimization time and selects a search depth
 
5697
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5698
  find. If the number of tables in the query exceeds some constant, then
 
5699
  search_depth is set to this constant.
 
5700
 
 
5701
  @param join   pointer to the structure providing all context info for
 
5702
                the query
 
5703
 
 
5704
  @note
 
5705
    This is an extremely simplistic implementation that serves as a stub for a
 
5706
    more advanced analysis of the join. Ideally the search depth should be
 
5707
    determined by learning from previous query optimizations, because it will
 
5708
    depend on the CPU power (and other factors).
 
5709
 
 
5710
  @todo
 
5711
    this value should be determined dynamically, based on statistics:
 
5712
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5713
 
 
5714
  @todo
 
5715
    this value could be determined by some mapping of the form:
 
5716
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5717
 
 
5718
  @return
 
5719
    A positive integer that specifies the search depth (and thus the
 
5720
    exhaustiveness) of the depth-first search algorithm used by
 
5721
    'greedy_search'.
 
5722
*/
 
5723
 
 
5724
static uint
 
5725
determine_search_depth(JOIN *join)
 
5726
{
 
5727
  uint32_t table_count=  join->tables - join->const_tables;
 
5728
  uint32_t search_depth;
 
5729
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5730
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5731
 
 
5732
  if (table_count <= max_tables_for_exhaustive_opt)
 
5733
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5734
  else
 
5735
    /*
 
5736
      TODO: this value could be determined by some mapping of the form:
 
5737
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5738
    */
 
5739
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5740
 
 
5741
  return search_depth;
 
5742
}
 
5743
 
 
5744
 
 
5745
/**
 
5746
  Select the best ways to access the tables in a query without reordering them.
 
5747
 
 
5748
    Find the best access paths for each query table and compute their costs
 
5749
    according to their order in the array 'join->best_ref' (thus without
 
5750
    reordering the join tables). The function calls sequentially
 
5751
    'best_access_path' for each table in the query to select the best table
 
5752
    access method. The final optimal plan is stored in the array
 
5753
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5754
 
 
5755
  @param join          pointer to the structure providing all context info for
 
5756
                       the query
 
5757
  @param join_tables   set of the tables in the query
 
5758
 
 
5759
  @note
 
5760
    This function can be applied to:
 
5761
    - queries with STRAIGHT_JOIN
 
5762
    - internally to compute the cost of an arbitrary QEP
 
5763
  @par
 
5764
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5765
    optimization process to finalize a QEP as it is.
 
5766
*/
 
5767
 
 
5768
static void
 
5769
optimize_straight_join(JOIN *join, table_map join_tables)
 
5770
{
 
5771
  JOIN_TAB *s;
 
5772
  uint32_t idx= join->const_tables;
 
5773
  double    record_count= 1.0;
 
5774
  double    read_time=    0.0;
 
5775
 
 
5776
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5777
  {
 
5778
    /* Find the best access method from 's' to the current partial plan */
 
5779
    advance_sj_state(join_tables, s);
 
5780
    best_access_path(join, s, join->session, join_tables, idx,
 
5781
                     record_count, read_time);
 
5782
    /* compute the cost of the new plan extended with 's' */
 
5783
    record_count*= join->positions[idx].records_read;
 
5784
    read_time+=    join->positions[idx].read_time;
 
5785
    join_tables&= ~(s->table->map);
 
5786
    ++idx;
 
5787
  }
 
5788
 
 
5789
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5790
  if (join->sort_by_table &&
 
5791
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5792
    read_time+= record_count;  // We have to make a temp table
 
5793
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5794
  join->best_read= read_time;
 
5795
}
 
5796
 
 
5797
 
 
5798
/**
 
5799
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5800
 
 
5801
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5802
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5803
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5804
    selects the most promising table, and extends the current partial QEP with
 
5805
    that table.  Currenly the most 'promising' table is the one with least
 
5806
    expensive extension.\
 
5807
 
 
5808
    There are two extreme cases:
 
5809
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5810
    best complete continuation of the partial QEP. This continuation can be
 
5811
    used directly as a result of the search.
 
5812
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5813
    consideres the extension of the current QEP with each of the remaining
 
5814
    unoptimized tables.
 
5815
 
 
5816
    All other cases are in-between these two extremes. Thus the parameter
 
5817
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5818
    value, the longer the optimizaton time and possibly the better the
 
5819
    resulting plan. The lower the value, the fewer alternative plans are
 
5820
    estimated, but the more likely to get a bad QEP.
 
5821
 
 
5822
    All intermediate and final results of the procedure are stored in 'join':
 
5823
    - join->positions     : modified for every partial QEP that is explored
 
5824
    - join->best_positions: modified for the current best complete QEP
 
5825
    - join->best_read     : modified for the current best complete QEP
 
5826
    - join->best_ref      : might be partially reordered
 
5827
 
 
5828
    The final optimal plan is stored in 'join->best_positions', and its
 
5829
    corresponding cost in 'join->best_read'.
 
5830
 
 
5831
  @note
 
5832
    The following pseudocode describes the algorithm of 'greedy_search':
 
5833
 
 
5834
    @code
 
5835
    procedure greedy_search
 
5836
    input: remaining_tables
 
5837
    output: pplan;
 
5838
    {
 
5839
      pplan = <>;
 
5840
      do {
 
5841
        (t, a) = best_extension(pplan, remaining_tables);
 
5842
        pplan = concat(pplan, (t, a));
 
5843
        remaining_tables = remaining_tables - t;
 
5844
      } while (remaining_tables != {})
 
5845
      return pplan;
 
5846
    }
 
5847
 
 
5848
  @endcode
 
5849
    where 'best_extension' is a placeholder for a procedure that selects the
 
5850
    most "promising" of all tables in 'remaining_tables'.
 
5851
    Currently this estimate is performed by calling
 
5852
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5853
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5854
    mainly depends on that of 'best_extension_by_limited_search'.
 
5855
 
 
5856
  @par
 
5857
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5858
    worst-case complexity of this algorithm is <=
 
5859
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5860
    complexity of greedy_search is O(N!).
 
5861
 
 
5862
  @par
 
5863
    In the future, 'greedy_search' might be extended to support other
 
5864
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5865
 
 
5866
  @param join             pointer to the structure providing all context info
 
5867
                          for the query
 
5868
  @param remaining_tables set of tables not included into the partial plan yet
 
5869
  @param search_depth     controlls the exhaustiveness of the search
 
5870
  @param prune_level      the pruning heuristics that should be applied during
 
5871
                          search
 
5872
 
 
5873
  @retval
 
5874
    false       ok
 
5875
  @retval
 
5876
    true        Fatal error
 
5877
*/
 
5878
 
 
5879
static bool
 
5880
greedy_search(JOIN      *join,
 
5881
              table_map remaining_tables,
 
5882
              uint32_t      search_depth,
 
5883
              uint32_t      prune_level)
 
5884
{
 
5885
  double    record_count= 1.0;
 
5886
  double    read_time=    0.0;
 
5887
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
5888
  uint32_t      best_idx;
 
5889
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5890
  POSITION  best_pos;
 
5891
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5892
 
 
5893
  /* number of tables that remain to be optimized */
 
5894
  size_remain= my_count_bits(remaining_tables);
 
5895
 
 
5896
  do {
 
5897
    /* Find the extension of the current QEP with the lowest cost */
 
5898
    join->best_read= DBL_MAX;
 
5899
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5900
                                         read_time, search_depth, prune_level))
 
5901
      return(true);
 
5902
 
 
5903
    if (size_remain <= search_depth)
 
5904
    {
 
5905
      /*
 
5906
        'join->best_positions' contains a complete optimal extension of the
 
5907
        current partial QEP.
 
5908
      */
 
5909
      return(false);
 
5910
    }
 
5911
 
 
5912
    /* select the first table in the optimal extension as most promising */
 
5913
    best_pos= join->best_positions[idx];
 
5914
    best_table= best_pos.table;
 
5915
    /*
 
5916
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5917
      'join->positions' for cost estimates, therefore we have to update its
 
5918
      value.
 
5919
    */
 
5920
    join->positions[idx]= best_pos;
 
5921
 
 
5922
    /* find the position of 'best_table' in 'join->best_ref' */
 
5923
    best_idx= idx;
 
5924
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5925
    while (pos && best_table != pos)
 
5926
      pos= join->best_ref[++best_idx];
 
5927
    assert((pos != NULL)); // should always find 'best_table'
 
5928
    /* move 'best_table' at the first free position in the array of joins */
 
5929
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5930
 
 
5931
    /* compute the cost of the new plan extended with 'best_table' */
 
5932
    record_count*= join->positions[idx].records_read;
 
5933
    read_time+=    join->positions[idx].read_time;
 
5934
 
 
5935
    remaining_tables&= ~(best_table->table->map);
 
5936
    --size_remain;
 
5937
    ++idx;
 
5938
  } while (true);
 
5939
}
 
5940
 
 
5941
 
 
5942
/**
 
5943
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5944
  exhaustive search.
 
5945
 
 
5946
    The procedure searches for the optimal ordering of the query tables in set
 
5947
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5948
    each table. The choice of a table order and an access path for each table
 
5949
    constitutes a query execution plan (QEP) that fully specifies how to
 
5950
    execute the query.
 
5951
 
 
5952
    The maximal size of the found plan is controlled by the parameter
 
5953
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5954
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5955
    of only some of the query tables. Such "partial" optimal plans are useful
 
5956
    only as input to query optimization procedures, and cannot be used directly
 
5957
    to execute a query.
 
5958
 
 
5959
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5960
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5961
    evaluates the cost of the partial plan extended by all access plans for
 
5962
    each of the relations in 'remaining_tables', expands the current partial
 
5963
    plan with the access plan that results in lowest cost of the expanded
 
5964
    partial plan, and removes the corresponding relation from
 
5965
    'remaining_tables'. The algorithm continues until it either constructs a
 
5966
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5967
    search_depth.
 
5968
 
 
5969
    The final optimal plan is stored in 'join->best_positions'. The
 
5970
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5971
 
 
5972
  @note
 
5973
    The procedure uses a recursive depth-first search where the depth of the
 
5974
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5975
    parameter 'search_depth'.
 
5976
 
 
5977
  @note
 
5978
    The pseudocode below describes the algorithm of
 
5979
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5980
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5981
    the complexity of greedy_search is O(N!).
 
5982
 
 
5983
    @code
 
5984
    procedure best_extension_by_limited_search(
 
5985
      pplan in,             // in, partial plan of tables-joined-so-far
 
5986
      pplan_cost,           // in, cost of pplan
 
5987
      remaining_tables,     // in, set of tables not referenced in pplan
 
5988
      best_plan_so_far,     // in/out, best plan found so far
 
5989
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5990
      search_depth)         // in, maximum size of the plans being considered
 
5991
    {
 
5992
      for each table T from remaining_tables
 
5993
      {
 
5994
        // Calculate the cost of using table T as above
 
5995
        cost = complex-series-of-calculations;
 
5996
 
 
5997
        // Add the cost to the cost so far.
 
5998
        pplan_cost+= cost;
 
5999
 
 
6000
        if (pplan_cost >= best_plan_so_far_cost)
 
6001
          // pplan_cost already too great, stop search
 
6002
          continue;
 
6003
 
 
6004
        pplan= expand pplan by best_access_method;
 
6005
        remaining_tables= remaining_tables - table T;
 
6006
        if (remaining_tables is not an empty set
 
6007
            and
 
6008
            search_depth > 1)
 
6009
        {
 
6010
          best_extension_by_limited_search(pplan, pplan_cost,
 
6011
                                           remaining_tables,
 
6012
                                           best_plan_so_far,
 
6013
                                           best_plan_so_far_cost,
 
6014
                                           search_depth - 1);
 
6015
        }
 
6016
        else
 
6017
        {
 
6018
          best_plan_so_far_cost= pplan_cost;
 
6019
          best_plan_so_far= pplan;
 
6020
        }
 
6021
      }
 
6022
    }
 
6023
    @endcode
 
6024
 
 
6025
  @note
 
6026
    When 'best_extension_by_limited_search' is called for the first time,
 
6027
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6028
    The actual implementation provides a way to optionally use pruning
 
6029
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6030
    space by skipping some partial plans.
 
6031
 
 
6032
  @note
 
6033
    The parameter 'search_depth' provides control over the recursion
 
6034
    depth, and thus the size of the resulting optimal plan.
 
6035
 
 
6036
  @param join             pointer to the structure providing all context info
 
6037
                          for the query
 
6038
  @param remaining_tables set of tables not included into the partial plan yet
 
6039
  @param idx              length of the partial QEP in 'join->positions';
 
6040
                          since a depth-first search is used, also corresponds
 
6041
                          to the current depth of the search tree;
 
6042
                          also an index in the array 'join->best_ref';
 
6043
  @param record_count     estimate for the number of records returned by the
 
6044
                          best partial plan
 
6045
  @param read_time        the cost of the best partial plan
 
6046
  @param search_depth     maximum depth of the recursion and thus size of the
 
6047
                          found optimal plan
 
6048
                          (0 < search_depth <= join->tables+1).
 
6049
  @param prune_level      pruning heuristics that should be applied during
 
6050
                          optimization
 
6051
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6052
 
 
6053
  @retval
 
6054
    false       ok
 
6055
  @retval
 
6056
    true        Fatal error
 
6057
*/
 
6058
 
 
6059
static bool
 
6060
best_extension_by_limited_search(JOIN      *join,
 
6061
                                 table_map remaining_tables,
 
6062
                                 uint32_t      idx,
 
6063
                                 double    record_count,
 
6064
                                 double    read_time,
 
6065
                                 uint32_t      search_depth,
 
6066
                                 uint32_t      prune_level)
 
6067
{
 
6068
  Session *session= join->session;
 
6069
  if (session->killed)  // Abort
 
6070
    return(true);
 
6071
 
 
6072
  /*
 
6073
     'join' is a partial plan with lower cost than the best plan so far,
 
6074
     so continue expanding it further with the tables in 'remaining_tables'.
 
6075
  */
 
6076
  JOIN_TAB *s;
 
6077
  double best_record_count= DBL_MAX;
 
6078
  double best_read_time=    DBL_MAX;
 
6079
 
 
6080
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6081
  {
 
6082
    table_map real_table_bit= s->table->map;
 
6083
    if ((remaining_tables & real_table_bit) &&
 
6084
        !(remaining_tables & s->dependent) &&
 
6085
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6086
    {
 
6087
      double current_record_count, current_read_time;
 
6088
      advance_sj_state(remaining_tables, s);
 
6089
 
 
6090
      /*
 
6091
        psergey-insideout-todo:
 
6092
          when best_access_path() detects it could do an InsideOut scan or
 
6093
          some other scan, have it return an insideout scan and a flag that
 
6094
          requests to "fork" this loop iteration. (Q: how does that behave
 
6095
          when the depth is insufficient??)
 
6096
      */
 
6097
      /* Find the best access method from 's' to the current partial plan */
 
6098
      best_access_path(join, s, session, remaining_tables, idx,
 
6099
                       record_count, read_time);
 
6100
      /* Compute the cost of extending the plan with 's' */
 
6101
      current_record_count= record_count * join->positions[idx].records_read;
 
6102
      current_read_time=    read_time + join->positions[idx].read_time;
 
6103
 
 
6104
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6105
      if ((current_read_time +
 
6106
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6107
      {
 
6108
        restore_prev_nj_state(s);
 
6109
        restore_prev_sj_state(remaining_tables, s);
 
6110
        continue;
 
6111
      }
 
6112
 
 
6113
      /*
 
6114
        Prune some less promising partial plans. This heuristic may miss
 
6115
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6116
      */
 
6117
      if (prune_level == 1)
 
6118
      {
 
6119
        if (best_record_count > current_record_count ||
 
6120
            best_read_time > current_read_time ||
 
6121
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6122
        {
 
6123
          if (best_record_count >= current_record_count &&
 
6124
              best_read_time >= current_read_time &&
 
6125
              /* TODO: What is the reasoning behind this condition? */
 
6126
              (!(s->key_dependent & remaining_tables) ||
 
6127
               join->positions[idx].records_read < 2.0))
 
6128
          {
 
6129
            best_record_count= current_record_count;
 
6130
            best_read_time=    current_read_time;
 
6131
          }
 
6132
        }
 
6133
        else
 
6134
        {
 
6135
          restore_prev_nj_state(s);
 
6136
          restore_prev_sj_state(remaining_tables, s);
 
6137
          continue;
 
6138
        }
 
6139
      }
 
6140
 
 
6141
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6142
      { /* Recursively expand the current partial plan */
 
6143
        std::swap(join->best_ref[idx], *pos);
 
6144
        if (best_extension_by_limited_search(join,
 
6145
                                             remaining_tables & ~real_table_bit,
 
6146
                                             idx + 1,
 
6147
                                             current_record_count,
 
6148
                                             current_read_time,
 
6149
                                             search_depth - 1,
 
6150
                                             prune_level))
 
6151
          return(true);
 
6152
        std::swap(join->best_ref[idx], *pos);
 
6153
      }
 
6154
      else
 
6155
      { /*
 
6156
          'join' is either the best partial QEP with 'search_depth' relations,
 
6157
          or the best complete QEP so far, whichever is smaller.
 
6158
        */
 
6159
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6160
        if (join->sort_by_table &&
 
6161
            join->sort_by_table !=
 
6162
            join->positions[join->const_tables].table->table)
 
6163
          /* We have to make a temp table */
 
6164
          current_read_time+= current_record_count;
 
6165
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6166
        {
 
6167
          memcpy(join->best_positions, join->positions,
 
6168
                 sizeof(POSITION) * (idx + 1));
 
6169
          join->best_read= current_read_time - 0.001;
 
6170
        }
 
6171
      }
 
6172
      restore_prev_nj_state(s);
 
6173
      restore_prev_sj_state(remaining_tables, s);
 
6174
    }
 
6175
  }
 
6176
  return(false);
 
6177
}
 
6178
 
 
6179
 
 
6180
/**
 
6181
  @todo
 
6182
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6183
  tested and accepted.
 
6184
 
 
6185
  RETURN VALUES
 
6186
    false       ok
 
6187
    true        Fatal error
 
6188
*/
 
6189
static bool
 
6190
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6191
          double read_time)
 
6192
{
 
6193
  Session *session= join->session;
 
6194
  if (session->killed)
 
6195
    return(true);
 
6196
  if (!rest_tables)
 
6197
  {
 
6198
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6199
    if (join->sort_by_table &&
 
6200
        join->sort_by_table !=
 
6201
        join->positions[join->const_tables].table->table)
 
6202
      read_time+=record_count;                  // We have to make a temp table
 
6203
    if (read_time < join->best_read)
 
6204
    {
 
6205
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6206
      join->best_read= read_time - 0.001;
 
6207
    }
 
6208
    return(false);
 
6209
  }
 
6210
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6211
    return(false);                                      /* Found better before */
 
6212
 
 
6213
  JOIN_TAB *s;
 
6214
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6215
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6216
  {
 
6217
    table_map real_table_bit=s->table->map;
 
6218
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6219
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6220
    {
 
6221
      double records, best;
 
6222
      advance_sj_state(rest_tables, s);
 
6223
      best_access_path(join, s, session, rest_tables, idx, record_count,
 
6224
                       read_time);
 
6225
      records= join->positions[idx].records_read;
 
6226
      best= join->positions[idx].read_time;
 
6227
      /*
 
6228
        Go to the next level only if there hasn't been a better key on
 
6229
        this level! This will cut down the search for a lot simple cases!
 
6230
      */
 
6231
      double current_record_count=record_count*records;
 
6232
      double current_read_time=read_time+best;
 
6233
      if (best_record_count > current_record_count ||
 
6234
          best_read_time > current_read_time ||
 
6235
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6236
      {
 
6237
        if (best_record_count >= current_record_count &&
 
6238
            best_read_time >= current_read_time &&
 
6239
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6240
        {
 
6241
          best_record_count=current_record_count;
 
6242
          best_read_time=current_read_time;
 
6243
        }
 
6244
        std::swap(join->best_ref[idx], *pos);
 
6245
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6246
                      current_record_count,current_read_time))
 
6247
          return(true);
 
6248
        std::swap(join->best_ref[idx], *pos);
 
6249
      }
 
6250
      restore_prev_nj_state(s);
 
6251
      restore_prev_sj_state(rest_tables, s);
 
6252
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6253
        break;                          // Don't test all combinations
 
6254
    }
 
6255
  }
 
6256
  return(false);
 
6257
}
 
6258
 
 
6259
 
 
6260
/**
849
6261
  Find how much space the prevous read not const tables takes in cache.
850
6262
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6263
 
 
6264
static void calc_used_field_length(Session *, JOIN_TAB *join_tab)
852
6265
{
853
6266
  uint32_t null_fields,blobs,fields,rec_length;
854
6267
  Field **f_ptr,*field;
 
6268
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6269
 
856
6270
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6271
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6272
  {
859
 
    if (field->isReadSet())
 
6273
    if (bitmap_is_set(read_set, field->field_index))
860
6274
    {
861
6275
      uint32_t flags=field->flags;
862
6276
      fields++;
863
6277
      rec_length+=field->pack_length();
864
6278
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6279
        blobs++;
866
6280
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6281
        null_fields++;
868
6282
    }
869
6283
  }
870
6284
  if (null_fields)
873
6287
    rec_length+=sizeof(bool);
874
6288
  if (blobs)
875
6289
  {
876
 
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
877
 
                                     (join_tab->table->getRecordLength()- rec_length));
878
 
    rec_length+= max((uint32_t)4,blob_length);
879
 
  }
880
 
  join_tab->used_fields= fields;
881
 
  join_tab->used_fieldlength= rec_length;
882
 
  join_tab->used_blobs= blobs;
883
 
}
884
 
 
885
 
StoredKey *get_store_key(Session *session,
886
 
                         optimizer::KeyUse *keyuse,
887
 
                         table_map used_tables,
888
 
                         KeyPartInfo *key_part,
889
 
                         unsigned char *key_buff,
890
 
                         uint32_t maybe_null)
891
 
{
892
 
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
893
 
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
 
6290
    uint32_t blob_length=(uint32_t) (join_tab->table->file->stats.mean_rec_length-
 
6291
                             (join_tab->table->getRecordLength()- rec_length));
 
6292
    rec_length+=(uint32_t) cmax((uint32_t)4,blob_length);
 
6293
  }
 
6294
  join_tab->used_fields=fields;
 
6295
  join_tab->used_fieldlength=rec_length;
 
6296
  join_tab->used_blobs=blobs;
 
6297
}
 
6298
 
 
6299
 
 
6300
static uint
 
6301
cache_record_length(JOIN *join,uint32_t idx)
 
6302
{
 
6303
  uint32_t length=0;
 
6304
  JOIN_TAB **pos,**end;
 
6305
  Session *session=join->session;
 
6306
 
 
6307
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6308
       pos != end ;
 
6309
       pos++)
 
6310
  {
 
6311
    JOIN_TAB *join_tab= *pos;
 
6312
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6313
      calc_used_field_length(session, join_tab);
 
6314
    length+=join_tab->used_fieldlength;
 
6315
  }
 
6316
  return length;
 
6317
}
 
6318
 
 
6319
 
 
6320
/*
 
6321
  Get the number of different row combinations for subset of partial join
 
6322
 
 
6323
  SYNOPSIS
 
6324
    prev_record_reads()
 
6325
      join       The join structure
 
6326
      idx        Number of tables in the partial join order (i.e. the
 
6327
                 partial join order is in join->positions[0..idx-1])
 
6328
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6329
                 row combinations.
 
6330
 
 
6331
  DESCRIPTION
 
6332
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6333
    tables within that join order (specified in found_ref), find out how many
 
6334
    distinct row combinations of subset tables will be in the result of the
 
6335
    partial join order.
 
6336
 
 
6337
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6338
    method. The ref access depends on current rows of tables in found_ref.
 
6339
    We want to count # of different ref accesses. We assume two ref accesses
 
6340
    will be different if at least one of access parameters is different.
 
6341
    Example: consider a query
 
6342
 
 
6343
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6344
 
 
6345
    and a join order:
 
6346
      t1,  ref access on t1.key=c1
 
6347
      t2,  ref access on t2.key=c2
 
6348
      t3,  ref access on t3.key=t1.field
 
6349
 
 
6350
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6351
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6352
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6353
            n_distinct_ref_scans = #records_read(t1)
 
6354
 
 
6355
    The reason for having this function (at least the latest version of it)
 
6356
    is that we need to account for buffering in join execution.
 
6357
 
 
6358
    An edge-case example: if we have a non-first table in join accessed via
 
6359
    ref(const) or ref(param) where there is a small number of different
 
6360
    values of param, then the access will likely hit the disk cache and will
 
6361
    not require any disk seeks.
 
6362
 
 
6363
    The proper solution would be to assume an LRU disk cache of some size,
 
6364
    calculate probability of cache hits, etc. For now we just count
 
6365
    identical ref accesses as one.
 
6366
 
 
6367
  RETURN
 
6368
    Expected number of row combinations
 
6369
*/
 
6370
 
 
6371
static double
 
6372
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6373
{
 
6374
  double found=1.0;
 
6375
  POSITION *pos_end= join->positions - 1;
 
6376
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6377
  {
 
6378
    if (pos->table->table->map & found_ref)
 
6379
    {
 
6380
      found_ref|= pos->ref_depend_map;
 
6381
      /*
 
6382
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
 
6383
        with no matching row we will get position[t2].records_read==0.
 
6384
        Actually the size of output is one null-complemented row, therefore
 
6385
        we will use value of 1 whenever we get records_read==0.
 
6386
 
 
6387
        Note
 
6388
        - the above case can't occur if inner part of outer join has more
 
6389
          than one table: table with no matches will not be marked as const.
 
6390
 
 
6391
        - Ideally we should add 1 to records_read for every possible null-
 
6392
          complemented row. We're not doing it because: 1. it will require
 
6393
          non-trivial code and add overhead. 2. The value of records_read
 
6394
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6395
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6396
      */
 
6397
      if (pos->records_read > DBL_EPSILON)
 
6398
        found*= pos->records_read;
 
6399
    }
 
6400
  }
 
6401
  return found;
 
6402
}
 
6403
 
 
6404
 
 
6405
/**
 
6406
  Set up join struct according to best position.
 
6407
*/
 
6408
 
 
6409
static bool
 
6410
get_best_combination(JOIN *join)
 
6411
{
 
6412
  uint32_t i,tablenr;
 
6413
  table_map used_tables;
 
6414
  JOIN_TAB *join_tab,*j;
 
6415
  KEYUSE *keyuse;
 
6416
  uint32_t table_count;
 
6417
  Session *session=join->session;
 
6418
 
 
6419
  table_count=join->tables;
 
6420
  if (!(join->join_tab=join_tab=
 
6421
        (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
 
6422
    return(true);
 
6423
 
 
6424
  join->full_join=0;
 
6425
 
 
6426
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6427
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
6428
  {
 
6429
    Table *form;
 
6430
    *j= *join->best_positions[tablenr].table;
 
6431
    form=join->table[tablenr]=j->table;
 
6432
    used_tables|= form->map;
 
6433
    form->reginfo.join_tab=j;
 
6434
    if (!*j->on_expr_ref)
 
6435
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6436
    if (j->type == JT_CONST)
 
6437
      continue;                                 // Handled in make_join_stat..
 
6438
 
 
6439
    j->ref.key = -1;
 
6440
    j->ref.key_parts=0;
 
6441
 
 
6442
    if (j->type == JT_SYSTEM)
 
6443
      continue;
 
6444
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
6445
    {
 
6446
      j->type=JT_ALL;
 
6447
      if (tablenr != join->const_tables)
 
6448
        join->full_join=1;
 
6449
    }
 
6450
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6451
      return(true);                        // Something went wrong
 
6452
  }
 
6453
 
 
6454
  for (i=0 ; i < table_count ; i++)
 
6455
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6456
  update_depend_map(join);
 
6457
  return(0);
 
6458
}
 
6459
 
 
6460
 
 
6461
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6462
                               table_map used_tables)
 
6463
{
 
6464
  KEYUSE *keyuse=org_keyuse;
 
6465
  Session  *session= join->session;
 
6466
  uint32_t keyparts,length,key;
 
6467
  Table *table;
 
6468
  KEY *keyinfo;
 
6469
 
 
6470
  /*  Use best key from find_best */
 
6471
  table=j->table;
 
6472
  key=keyuse->key;
 
6473
  keyinfo=table->key_info+key;
 
6474
 
 
6475
  {
 
6476
    keyparts=length=0;
 
6477
    uint32_t found_part_ref_or_null= 0;
 
6478
    /*
 
6479
      Calculate length for the used key
 
6480
      Stop if there is a missing key part or when we find second key_part
 
6481
      with KEY_OPTIMIZE_REF_OR_NULL
 
6482
    */
 
6483
    do
 
6484
    {
 
6485
      if (!(~used_tables & keyuse->used_tables))
 
6486
      {
 
6487
        if (keyparts == keyuse->keypart &&
 
6488
            !(found_part_ref_or_null & keyuse->optimize))
 
6489
        {
 
6490
          keyparts++;
 
6491
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6492
          found_part_ref_or_null|= keyuse->optimize;
 
6493
        }
 
6494
      }
 
6495
      keyuse++;
 
6496
    } while (keyuse->table == table && keyuse->key == key);
 
6497
  }
 
6498
 
 
6499
  /* set up fieldref */
 
6500
  keyinfo=table->key_info+key;
 
6501
  j->ref.key_parts=keyparts;
 
6502
  j->ref.key_length=length;
 
6503
  j->ref.key=(int) key;
 
6504
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
 
6505
      !(j->ref.key_copy= (store_key**) session->alloc((sizeof(store_key*) *
 
6506
                                                   (keyparts+1)))) ||
 
6507
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
6508
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6509
  {
 
6510
    return(true);
 
6511
  }
 
6512
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
 
6513
  j->ref.key_err=1;
 
6514
  j->ref.null_rejecting= 0;
 
6515
  j->ref.disable_cache= false;
 
6516
  keyuse=org_keyuse;
 
6517
 
 
6518
  store_key **ref_key= j->ref.key_copy;
 
6519
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6520
  bool keyuse_uses_no_tables= true;
 
6521
  {
 
6522
    uint32_t i;
 
6523
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
6524
    {
 
6525
      while (keyuse->keypart != i ||
 
6526
             ((~used_tables) & keyuse->used_tables))
 
6527
        keyuse++;                               /* Skip other parts */
 
6528
 
 
6529
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6530
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6531
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6532
      if (keyuse->null_rejecting)
 
6533
        j->ref.null_rejecting |= 1 << i;
 
6534
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6535
      if (!keyuse->used_tables &&
 
6536
          !(join->select_options & SELECT_DESCRIBE))
 
6537
      {                                 // Compare against constant
 
6538
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6539
                           key_buff + maybe_null,
 
6540
                           maybe_null ?  key_buff : 0,
 
6541
                           keyinfo->key_part[i].length, keyuse->val);
 
6542
        if (session->is_fatal_error)
 
6543
          return(true);
 
6544
        tmp.copy();
 
6545
      }
 
6546
      else
 
6547
        *ref_key++= get_store_key(session,
 
6548
                                  keyuse,join->const_table_map,
 
6549
                                  &keyinfo->key_part[i],
 
6550
                                  key_buff, maybe_null);
 
6551
      /*
 
6552
        Remember if we are going to use REF_OR_NULL
 
6553
        But only if field _really_ can be null i.e. we force JT_REF
 
6554
        instead of JT_REF_OR_NULL in case if field can't be null
 
6555
      */
 
6556
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6557
        null_ref_key= key_buff;
 
6558
      key_buff+=keyinfo->key_part[i].store_length;
 
6559
    }
 
6560
  }
 
6561
  *ref_key=0;                           // end_marker
 
6562
  if (j->type == JT_CONST)
 
6563
    j->table->const_table= 1;
 
6564
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
 
6565
           keyparts != keyinfo->key_parts || null_ref_key)
 
6566
  {
 
6567
    /* Must read with repeat */
 
6568
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
6569
    j->ref.null_ref_key= null_ref_key;
 
6570
  }
 
6571
  else if (keyuse_uses_no_tables)
 
6572
  {
 
6573
    /*
 
6574
      This happen if we are using a constant expression in the ON part
 
6575
      of an LEFT JOIN.
 
6576
      SELECT * FROM a LEFT JOIN b ON b.key=30
 
6577
      Here we should not mark the table as a 'const' as a field may
 
6578
      have a 'normal' value or a NULL value.
 
6579
    */
 
6580
    j->type=JT_CONST;
 
6581
  }
 
6582
  else
 
6583
    j->type=JT_EQ_REF;
 
6584
  return(0);
 
6585
}
 
6586
 
 
6587
 
 
6588
 
 
6589
static store_key *
 
6590
get_store_key(Session *session, KEYUSE *keyuse, table_map used_tables,
 
6591
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6592
{
 
6593
  if (!((~used_tables) & keyuse->used_tables))          // if const item
894
6594
  {
895
6595
    return new store_key_const_item(session,
896
6596
                                    key_part->field,
897
6597
                                    key_buff + maybe_null,
898
6598
                                    maybe_null ? key_buff : 0,
899
6599
                                    key_part->length,
900
 
                                    key_use_val);
 
6600
                                    keyuse->val);
901
6601
  }
902
 
  else if (key_use_val->type() == Item::FIELD_ITEM ||
903
 
           (key_use_val->type() == Item::REF_ITEM &&
904
 
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
905
 
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
906
 
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
907
 
  {
 
6602
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6603
           (keyuse->val->type() == Item::REF_ITEM &&
 
6604
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6605
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6606
             Item_ref::DIRECT_REF &&
 
6607
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
908
6608
    return new store_key_field(session,
909
6609
                               key_part->field,
910
6610
                               key_buff + maybe_null,
911
6611
                               maybe_null ? key_buff : 0,
912
6612
                               key_part->length,
913
 
                               ((Item_field*) key_use_val->real_item())->field,
914
 
                               key_use_val->full_name());
915
 
  }
 
6613
                               ((Item_field*) keyuse->val->real_item())->field,
 
6614
                               keyuse->val->full_name());
916
6615
  return new store_key_item(session,
917
6616
                            key_part->field,
918
6617
                            key_buff + maybe_null,
919
6618
                            maybe_null ? key_buff : 0,
920
6619
                            key_part->length,
921
 
                            key_use_val);
 
6620
                            keyuse->val);
922
6621
}
923
6622
 
924
6623
/**
927
6626
  @return
928
6627
    returns 1 if there was some conversion made when the field was stored.
929
6628
*/
930
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6629
 
 
6630
bool
 
6631
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
931
6632
{
932
6633
  bool error;
933
 
  Table *table= field->getTable();
 
6634
  Table *table= field->table;
934
6635
  Session *session= table->in_use;
935
6636
  ha_rows cuted_fields=session->cuted_fields;
936
6637
 
937
6638
  /*
938
6639
    we should restore old value of count_cuted_fields because
939
 
    store_val_in_field can be called from insert_query
 
6640
    store_val_in_field can be called from mysql_insert
940
6641
    with select_insert, which make count_cuted_fields= 1
941
6642
   */
942
6643
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
946
6647
  return error || cuted_fields != session->cuted_fields;
947
6648
}
948
6649
 
 
6650
 
 
6651
static bool
 
6652
make_simple_join(JOIN *join,Table *tmp_table)
 
6653
{
 
6654
  Table **tableptr;
 
6655
  JOIN_TAB *join_tab;
 
6656
 
 
6657
  /*
 
6658
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6659
    to this function through JOIN::exec (may happen for sub-queries).
 
6660
  */
 
6661
  if (!join->table_reexec)
 
6662
  {
 
6663
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
6664
      return(true);                        /* purecov: inspected */
 
6665
    if (join->tmp_join)
 
6666
      join->tmp_join->table_reexec= join->table_reexec;
 
6667
  }
 
6668
  if (!join->join_tab_reexec)
 
6669
  {
 
6670
    if (!(join->join_tab_reexec=
 
6671
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
 
6672
      return(true);                        /* purecov: inspected */
 
6673
    if (join->tmp_join)
 
6674
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6675
  }
 
6676
  tableptr= join->table_reexec;
 
6677
  join_tab= join->join_tab_reexec;
 
6678
 
 
6679
  join->join_tab=join_tab;
 
6680
  join->table=tableptr; tableptr[0]=tmp_table;
 
6681
  join->tables=1;
 
6682
  join->const_tables=0;
 
6683
  join->const_table_map=0;
 
6684
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6685
    join->tmp_table_param.func_count=0;
 
6686
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6687
  join->first_record=join->sort_and_group=0;
 
6688
  join->send_records=(ha_rows) 0;
 
6689
  join->group=0;
 
6690
  join->row_limit=join->unit->select_limit_cnt;
 
6691
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6692
 
 
6693
  join_tab->cache.buff=0;                       /* No caching */
 
6694
  join_tab->table=tmp_table;
 
6695
  join_tab->select=0;
 
6696
  join_tab->select_cond=0;
 
6697
  join_tab->quick=0;
 
6698
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6699
  join_tab->keys.init();
 
6700
  join_tab->keys.set_all();                     /* test everything in quick */
 
6701
  join_tab->info=0;
 
6702
  join_tab->on_expr_ref=0;
 
6703
  join_tab->last_inner= 0;
 
6704
  join_tab->first_unmatched= 0;
 
6705
  join_tab->ref.key = -1;
 
6706
  join_tab->not_used_in_distinct=0;
 
6707
  join_tab->read_first_record= join_init_read_record;
 
6708
  join_tab->join=join;
 
6709
  join_tab->ref.key_parts= 0;
 
6710
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6711
  join_tab->do_firstmatch= NULL;
 
6712
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6713
  tmp_table->status=0;
 
6714
  tmp_table->null_row=0;
 
6715
  return(false);
 
6716
}
 
6717
 
 
6718
 
949
6719
inline void add_cond_and_fix(Item **e1, Item *e2)
950
6720
{
951
6721
  if (*e1)
961
6731
    *e1= e2;
962
6732
}
963
6733
 
964
 
bool create_ref_for_key(Join *join, 
965
 
                        JoinTable *j, 
966
 
                        optimizer::KeyUse *org_keyuse,
967
 
                        table_map used_tables)
968
 
{
969
 
  optimizer::KeyUse *keyuse= org_keyuse;
970
 
  Session  *session= join->session;
971
 
  uint32_t keyparts;
972
 
  uint32_t length;
973
 
  uint32_t key;
974
 
  Table *table= NULL;
975
 
  KeyInfo *keyinfo= NULL;
976
 
 
977
 
  /*  Use best key from find_best */
978
 
  table= j->table;
979
 
  key= keyuse->getKey();
980
 
  keyinfo= table->key_info + key;
981
 
 
982
 
  {
983
 
    keyparts= length= 0;
984
 
    uint32_t found_part_ref_or_null= 0;
985
 
    /*
986
 
      Calculate length for the used key
987
 
      Stop if there is a missing key part or when we find second key_part
988
 
      with KEY_OPTIMIZE_REF_OR_NULL
989
 
    */
990
 
    do
991
 
    {
992
 
      if (! (~used_tables & keyuse->getUsedTables()))
993
 
      {
994
 
        if (keyparts == keyuse->getKeypart() &&
995
 
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
996
 
        {
997
 
          keyparts++;
998
 
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
999
 
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
1000
 
        }
1001
 
      }
1002
 
      keyuse++;
1003
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
1004
 
  }
1005
 
 
1006
 
  /* set up fieldref */
1007
 
  keyinfo=table->key_info+key;
1008
 
  j->ref.key_parts=keyparts;
1009
 
  j->ref.key_length=length;
1010
 
  j->ref.key=(int) key;
1011
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1012
 
      !(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
1013
 
               (keyparts+1)))) ||
1014
 
      !(j->ref.items=    (Item**) session->getMemRoot()->allocate(sizeof(Item*)*keyparts)) ||
1015
 
      !(j->ref.cond_guards= (bool**) session->getMemRoot()->allocate(sizeof(uint*)*keyparts)))
1016
 
  {
1017
 
    return(true);
1018
 
  }
1019
 
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
1020
 
  j->ref.key_err=1;
1021
 
  j->ref.null_rejecting= 0;
1022
 
  j->ref.disable_cache= false;
1023
 
  keyuse=org_keyuse;
1024
 
 
1025
 
  StoredKey **ref_key= j->ref.key_copy;
1026
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
1027
 
  bool keyuse_uses_no_tables= true;
1028
 
  {
1029
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
1030
 
    {
1031
 
      while (keyuse->getKeypart() != i ||
1032
 
             ((~used_tables) & keyuse->getUsedTables()))
1033
 
        keyuse++;       /* Skip other parts */
1034
 
 
1035
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1036
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1037
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1038
 
      if (keyuse->isNullRejected())
1039
 
        j->ref.null_rejecting |= 1 << i;
1040
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1041
 
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1042
 
      {         // Compare against constant
1043
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
1044
 
                           key_buff + maybe_null,
1045
 
                           maybe_null ?  key_buff : 0,
1046
 
                           keyinfo->key_part[i].length, keyuse->getVal());
1047
 
        if (session->is_fatal_error)
1048
 
          return(true);
1049
 
        tmp.copy();
1050
 
      }
1051
 
      else
1052
 
        *ref_key++= get_store_key(session,
1053
 
          keyuse,join->const_table_map,
1054
 
          &keyinfo->key_part[i],
1055
 
          key_buff, maybe_null);
1056
 
      /*
1057
 
        Remember if we are going to use REF_OR_NULL
1058
 
        But only if field _really_ can be null i.e. we force AM_REF
1059
 
        instead of AM_REF_OR_NULL in case if field can't be null
1060
 
      */
1061
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1062
 
        null_ref_key= key_buff;
1063
 
      key_buff+=keyinfo->key_part[i].store_length;
1064
 
    }
1065
 
  }
1066
 
  *ref_key= 0;       // end_marker
1067
 
  if (j->type == AM_CONST)
1068
 
    j->table->const_table= 1;
1069
 
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1070
 
           keyparts != keyinfo->key_parts || null_ref_key)
1071
 
  {
1072
 
    /* Must read with repeat */
1073
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
1074
 
    j->ref.null_ref_key= null_ref_key;
1075
 
  }
1076
 
  else if (keyuse_uses_no_tables)
1077
 
  {
1078
 
    /*
1079
 
      This happen if we are using a constant expression in the ON part
1080
 
      of an LEFT JOIN.
1081
 
      SELECT * FROM a LEFT JOIN b ON b.key=30
1082
 
      Here we should not mark the table as a 'const' as a field may
1083
 
      have a 'normal' value or a NULL value.
1084
 
    */
1085
 
    j->type= AM_CONST;
1086
 
  }
1087
 
  else
1088
 
    j->type= AM_EQ_REF;
1089
 
  return 0;
1090
 
}
1091
6734
 
1092
6735
/**
1093
6736
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1133
6776
 
1134
6777
    Implementation overview
1135
6778
      1. update_ref_and_keys() accumulates info about null-rejecting
1136
 
         predicates in in KeyField::null_rejecting
1137
 
      1.1 add_key_part saves these to KeyUse.
1138
 
      2. create_ref_for_key copies them to table_reference_st.
 
6779
         predicates in in KEY_FIELD::null_rejecting
 
6780
      1.1 add_key_part saves these to KEYUSE.
 
6781
      2. create_ref_for_key copies them to TABLE_REF.
1139
6782
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6783
         appropiate JOIN_TAB members.
1141
6784
*/
1142
 
void add_not_null_conds(Join *join)
 
6785
 
 
6786
static void add_not_null_conds(JOIN *join)
1143
6787
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6788
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
1145
6789
  {
1146
 
    JoinTable *tab=join->join_tab+i;
1147
 
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1148
 
         tab->type == AM_REF_OR_NULL) &&
 
6790
    JOIN_TAB *tab=join->join_tab+i;
 
6791
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
 
6792
         tab->type == JT_REF_OR_NULL) &&
1149
6793
        !tab->table->maybe_null)
1150
6794
    {
1151
6795
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1156
6800
          Item *notnull;
1157
6801
          assert(item->type() == Item::FIELD_ITEM);
1158
6802
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6803
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6804
          /*
1161
6805
            For UPDATE queries such as:
1162
6806
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1197
6841
    -  pointer to the guarded predicate, if success
1198
6842
    -  0, otherwise
1199
6843
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6844
 
 
6845
static COND*
 
6846
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6847
{
1202
6848
  COND *tmp;
1203
6849
  assert(cond != 0);
1213
6859
  return tmp;
1214
6860
}
1215
6861
 
 
6862
 
 
6863
/**
 
6864
  Fill in outer join related info for the execution plan structure.
 
6865
 
 
6866
    For each outer join operation left after simplification of the
 
6867
    original query the function set up the following pointers in the linear
 
6868
    structure join->join_tab representing the selected execution plan.
 
6869
    The first inner table t0 for the operation is set to refer to the last
 
6870
    inner table tk through the field t0->last_inner.
 
6871
    Any inner table ti for the operation are set to refer to the first
 
6872
    inner table ti->first_inner.
 
6873
    The first inner table t0 for the operation is set to refer to the
 
6874
    first inner table of the embedding outer join operation, if there is any,
 
6875
    through the field t0->first_upper.
 
6876
    The on expression for the outer join operation is attached to the
 
6877
    corresponding first inner table through the field t0->on_expr_ref.
 
6878
    Here ti are structures of the JOIN_TAB type.
 
6879
 
 
6880
  EXAMPLE. For the query:
 
6881
  @code
 
6882
        SELECT * FROM t1
 
6883
                      LEFT JOIN
 
6884
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6885
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6886
          WHERE t1.c > 5,
 
6887
  @endcode
 
6888
 
 
6889
    given the execution plan with the table order t1,t2,t3,t4
 
6890
    is selected, the following references will be set;
 
6891
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6892
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6893
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
 
6894
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6895
 
 
6896
  @param join   reference to the info fully describing the query
 
6897
 
 
6898
  @note
 
6899
    The function assumes that the simplification procedure has been
 
6900
    already applied to the join query (see simplify_joins).
 
6901
    This function can be called only after the execution plan
 
6902
    has been chosen.
 
6903
*/
 
6904
 
 
6905
static void
 
6906
make_outerjoin_info(JOIN *join)
 
6907
{
 
6908
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6909
  {
 
6910
    JOIN_TAB *tab=join->join_tab+i;
 
6911
    Table *table=tab->table;
 
6912
    TableList *tbl= table->pos_in_table_list;
 
6913
    TableList *embedding= tbl->embedding;
 
6914
 
 
6915
    if (tbl->outer_join)
 
6916
    {
 
6917
      /*
 
6918
        Table tab is the only one inner table for outer join.
 
6919
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6920
        is in the query above.)
 
6921
      */
 
6922
      tab->last_inner= tab->first_inner= tab;
 
6923
      tab->on_expr_ref= &tbl->on_expr;
 
6924
      tab->cond_equal= tbl->cond_equal;
 
6925
      if (embedding)
 
6926
        tab->first_upper= embedding->nested_join->first_nested;
 
6927
    }
 
6928
    for ( ; embedding ; embedding= embedding->embedding)
 
6929
    {
 
6930
      /* Ignore sj-nests: */
 
6931
      if (!embedding->on_expr)
 
6932
        continue;
 
6933
      nested_join_st *nested_join= embedding->nested_join;
 
6934
      if (!nested_join->counter_)
 
6935
      {
 
6936
        /*
 
6937
          Table tab is the first inner table for nested_join.
 
6938
          Save reference to it in the nested join structure.
 
6939
        */
 
6940
        nested_join->first_nested= tab;
 
6941
        tab->on_expr_ref= &embedding->on_expr;
 
6942
        tab->cond_equal= tbl->cond_equal;
 
6943
        if (embedding->embedding)
 
6944
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6945
      }
 
6946
      if (!tab->first_inner)
 
6947
        tab->first_inner= nested_join->first_nested;
 
6948
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6949
        break;
 
6950
      /* Table tab is the last inner table for nested join. */
 
6951
      nested_join->first_nested->last_inner= tab;
 
6952
    }
 
6953
  }
 
6954
  return;
 
6955
}
 
6956
 
 
6957
 
 
6958
static bool
 
6959
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6960
{
 
6961
  Session *session= join->session;
 
6962
  if (select)
 
6963
  {
 
6964
    add_not_null_conds(join);
 
6965
    table_map used_tables;
 
6966
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6967
    {                        /* there may be a select without a cond. */
 
6968
      if (join->tables > 1)
 
6969
        cond->update_used_tables();             // Tablenr may have changed
 
6970
      if (join->const_tables == join->tables &&
 
6971
          session->lex->current_select->master_unit() ==
 
6972
          &session->lex->unit)          // not upper level SELECT
 
6973
        join->const_table_map|=RAND_TABLE_BIT;
 
6974
      {                                         // Check const tables
 
6975
        COND *const_cond=
 
6976
          make_cond_for_table(cond,
 
6977
                              join->const_table_map,
 
6978
                              (table_map) 0, 1);
 
6979
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6980
             tab < join->join_tab+join->tables ; tab++)
 
6981
        {
 
6982
          if (*tab->on_expr_ref)
 
6983
          {
 
6984
            JOIN_TAB *cond_tab= tab->first_inner;
 
6985
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6986
                                           join->const_table_map,
 
6987
                                           (  table_map) 0, 0);
 
6988
            if (!tmp)
 
6989
              continue;
 
6990
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6991
            if (!tmp)
 
6992
              return(1);
 
6993
            tmp->quick_fix_field();
 
6994
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6995
                                    new Item_cond_and(cond_tab->select_cond,
 
6996
                                                      tmp);
 
6997
            if (!cond_tab->select_cond)
 
6998
              return(1);
 
6999
            cond_tab->select_cond->quick_fix_field();
 
7000
          }
 
7001
        }
 
7002
        if (const_cond && !const_cond->val_int())
 
7003
        {
 
7004
          return(1);     // Impossible const condition
 
7005
        }
 
7006
      }
 
7007
    }
 
7008
    used_tables=((select->const_tables=join->const_table_map) |
 
7009
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7010
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
7011
    {
 
7012
      JOIN_TAB *tab=join->join_tab+i;
 
7013
      /*
 
7014
        first_inner is the X in queries like:
 
7015
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7016
      */
 
7017
      JOIN_TAB *first_inner_tab= tab->first_inner;
 
7018
      table_map current_map= tab->table->map;
 
7019
      bool use_quick_range=0;
 
7020
      COND *tmp;
 
7021
 
 
7022
      /*
 
7023
        Following force including random expression in last table condition.
 
7024
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7025
      */
 
7026
      if (i == join->tables-1)
 
7027
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7028
      used_tables|=current_map;
 
7029
 
 
7030
      if (tab->type == JT_REF && tab->quick &&
 
7031
          (uint32_t) tab->ref.key == tab->quick->index &&
 
7032
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7033
      {
 
7034
        /* Range uses longer key;  Use this instead of ref on key */
 
7035
        tab->type=JT_ALL;
 
7036
        use_quick_range=1;
 
7037
        tab->use_quick=1;
 
7038
        tab->ref.key= -1;
 
7039
        tab->ref.key_parts=0;           // Don't use ref key.
 
7040
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7041
        /*
 
7042
          We will use join cache here : prevent sorting of the first
 
7043
          table only and sort at the end.
 
7044
        */
 
7045
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7046
          join->full_join= 1;
 
7047
      }
 
7048
 
 
7049
      tmp= NULL;
 
7050
      if (cond)
 
7051
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7052
      if (cond && !tmp && tab->quick)
 
7053
      {                                         // Outer join
 
7054
        if (tab->type != JT_ALL)
 
7055
        {
 
7056
          /*
 
7057
            Don't use the quick method
 
7058
            We come here in the case where we have 'key=constant' and
 
7059
            the test is removed by make_cond_for_table()
 
7060
          */
 
7061
          delete tab->quick;
 
7062
          tab->quick= 0;
 
7063
        }
 
7064
        else
 
7065
        {
 
7066
          /*
 
7067
            Hack to handle the case where we only refer to a table
 
7068
            in the ON part of an OUTER JOIN. In this case we want the code
 
7069
            below to check if we should use 'quick' instead.
 
7070
          */
 
7071
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7072
        }
 
7073
 
 
7074
      }
 
7075
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7076
          tab->type == JT_EQ_REF)
 
7077
      {
 
7078
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7079
                                       session->memdup((unsigned char*) select,
 
7080
                                                   sizeof(*select)));
 
7081
        if (!sel)
 
7082
          return(1);                    // End of memory
 
7083
        /*
 
7084
          If tab is an inner table of an outer join operation,
 
7085
          add a match guard to the pushed down predicate.
 
7086
          The guard will turn the predicate on only after
 
7087
          the first match for outer tables is encountered.
 
7088
        */
 
7089
        if (cond && tmp)
 
7090
        {
 
7091
          /*
 
7092
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7093
            a cond, so neutralize the hack above.
 
7094
          */
 
7095
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7096
            return(1);
 
7097
          tab->select_cond=sel->cond=tmp;
 
7098
          /* Push condition to storage engine if this is enabled
 
7099
             and the condition is not guarded */
 
7100
          tab->table->file->pushed_cond= NULL;
 
7101
          if (session->variables.engine_condition_pushdown)
 
7102
          {
 
7103
            COND *push_cond=
 
7104
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7105
            if (push_cond)
 
7106
            {
 
7107
              /* Push condition to handler */
 
7108
              if (!tab->table->file->cond_push(push_cond))
 
7109
                tab->table->file->pushed_cond= push_cond;
 
7110
            }
 
7111
          }
 
7112
        }
 
7113
        else
 
7114
          tab->select_cond= sel->cond= NULL;
 
7115
 
 
7116
        sel->head=tab->table;
 
7117
        if (tab->quick)
 
7118
        {
 
7119
          /* Use quick key read if it's a constant and it's not used
 
7120
             with key reading */
 
7121
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7122
              && (tab->type != JT_REF || (uint32_t) tab->ref.key == tab->quick->index))
 
7123
          {
 
7124
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7125
            sel->quick_keys.clear_all();
 
7126
            sel->needed_reg.clear_all();
 
7127
          }
 
7128
          else
 
7129
          {
 
7130
            delete tab->quick;
 
7131
          }
 
7132
          tab->quick=0;
 
7133
        }
 
7134
        uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
 
7135
        if (i == join->const_tables && ref_key)
 
7136
        {
 
7137
          if (!tab->const_keys.is_clear_all() &&
 
7138
              tab->table->reginfo.impossible_range)
 
7139
            return(1);
 
7140
        }
 
7141
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7142
        {
 
7143
          if (!tab->const_keys.is_clear_all() &&
 
7144
              tab->table->reginfo.impossible_range)
 
7145
            return(1);                          // Impossible range
 
7146
          /*
 
7147
            We plan to scan all rows.
 
7148
            Check again if we should use an index.
 
7149
            We could have used an column from a previous table in
 
7150
            the index if we are using limit and this is the first table
 
7151
          */
 
7152
 
 
7153
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7154
              (!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)))
 
7155
          {
 
7156
            /* Join with outer join condition */
 
7157
            COND *orig_cond=sel->cond;
 
7158
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7159
 
 
7160
            /*
 
7161
              We can't call sel->cond->fix_fields,
 
7162
              as it will break tab->on_expr if it's AND condition
 
7163
              (fix_fields currently removes extra AND/OR levels).
 
7164
              Yet attributes of the just built condition are not needed.
 
7165
              Thus we call sel->cond->quick_fix_field for safety.
 
7166
            */
 
7167
            if (sel->cond && !sel->cond->fixed)
 
7168
              sel->cond->quick_fix_field();
 
7169
 
 
7170
            if (sel->test_quick_select(session, tab->keys,
 
7171
                                       used_tables & ~ current_map,
 
7172
                                       (join->select_options &
 
7173
                                        OPTION_FOUND_ROWS ?
 
7174
                                        HA_POS_ERROR :
 
7175
                                        join->unit->select_limit_cnt), 0,
 
7176
                                        false) < 0)
 
7177
            {
 
7178
              /*
 
7179
                Before reporting "Impossible WHERE" for the whole query
 
7180
                we have to check isn't it only "impossible ON" instead
 
7181
              */
 
7182
              sel->cond=orig_cond;
 
7183
              if (!*tab->on_expr_ref ||
 
7184
                  sel->test_quick_select(session, tab->keys,
 
7185
                                         used_tables & ~ current_map,
 
7186
                                         (join->select_options &
 
7187
                                          OPTION_FOUND_ROWS ?
 
7188
                                          HA_POS_ERROR :
 
7189
                                          join->unit->select_limit_cnt),0,
 
7190
                                          false) < 0)
 
7191
                return(1);                      // Impossible WHERE
 
7192
            }
 
7193
            else
 
7194
              sel->cond=orig_cond;
 
7195
 
 
7196
            /* Fix for EXPLAIN */
 
7197
            if (sel->quick)
 
7198
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7199
          }
 
7200
          else
 
7201
          {
 
7202
            sel->needed_reg=tab->needed_reg;
 
7203
            sel->quick_keys.clear_all();
 
7204
          }
 
7205
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7206
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7207
          {
 
7208
            tab->keys=sel->quick_keys;
 
7209
            tab->keys.merge(sel->needed_reg);
 
7210
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7211
                             (select->quick_keys.is_clear_all() ||
 
7212
                              (select->quick &&
 
7213
                               (select->quick->records >= 100L)))) ?
 
7214
              2 : 1;
 
7215
            sel->read_tables= used_tables & ~current_map;
 
7216
          }
 
7217
          if (i != join->const_tables && tab->use_quick != 2)
 
7218
          {                                     /* Read with cache */
 
7219
            if (cond &&
 
7220
                (tmp=make_cond_for_table(cond,
 
7221
                                         join->const_table_map |
 
7222
                                         current_map,
 
7223
                                         current_map, 0)))
 
7224
            {
 
7225
              tab->cache.select=(SQL_SELECT*)
 
7226
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7227
              tab->cache.select->cond=tmp;
 
7228
              tab->cache.select->read_tables=join->const_table_map;
 
7229
            }
 
7230
          }
 
7231
        }
 
7232
      }
 
7233
 
 
7234
      /*
 
7235
        Push down conditions from all on expressions.
 
7236
        Each of these conditions are guarded by a variable
 
7237
        that turns if off just before null complemented row for
 
7238
        outer joins is formed. Thus, the condition from an
 
7239
        'on expression' are guaranteed not to be checked for
 
7240
        the null complemented row.
 
7241
      */
 
7242
 
 
7243
      /* First push down constant conditions from on expressions */
 
7244
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7245
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7246
      {
 
7247
        if (*join_tab->on_expr_ref)
 
7248
        {
 
7249
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7250
          tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7251
                                   join->const_table_map,
 
7252
                                   (table_map) 0, 0);
 
7253
          if (!tmp)
 
7254
            continue;
 
7255
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7256
          if (!tmp)
 
7257
            return(1);
 
7258
          tmp->quick_fix_field();
 
7259
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7260
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7261
          if (!cond_tab->select_cond)
 
7262
            return(1);
 
7263
          cond_tab->select_cond->quick_fix_field();
 
7264
        }
 
7265
      }
 
7266
 
 
7267
      /* Push down non-constant conditions from on expressions */
 
7268
      JOIN_TAB *last_tab= tab;
 
7269
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7270
      {
 
7271
        /*
 
7272
          Table tab is the last inner table of an outer join.
 
7273
          An on expression is always attached to it.
 
7274
        */
 
7275
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7276
 
 
7277
        table_map used_tables2= (join->const_table_map |
 
7278
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7279
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7280
        {
 
7281
          current_map= tab->table->map;
 
7282
          used_tables2|= current_map;
 
7283
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7284
                                              current_map, 0);
 
7285
          if (tmp_cond)
 
7286
          {
 
7287
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7288
            /*
 
7289
              First add the guards for match variables of
 
7290
              all embedding outer join operations.
 
7291
            */
 
7292
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7293
                                                     tmp_cond,
 
7294
                                                     first_inner_tab)))
 
7295
              return(1);
 
7296
            /*
 
7297
              Now add the guard turning the predicate off for
 
7298
              the null complemented row.
 
7299
            */
 
7300
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7301
                                              &first_inner_tab->
 
7302
                                              not_null_compl);
 
7303
            if (tmp_cond)
 
7304
              tmp_cond->quick_fix_field();
 
7305
            /* Add the predicate to other pushed down predicates */
 
7306
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7307
                                  new Item_cond_and(cond_tab->select_cond,
 
7308
                                                    tmp_cond);
 
7309
            if (!cond_tab->select_cond)
 
7310
              return(1);
 
7311
            cond_tab->select_cond->quick_fix_field();
 
7312
          }
 
7313
        }
 
7314
        first_inner_tab= first_inner_tab->first_upper;
 
7315
      }
 
7316
    }
 
7317
  }
 
7318
  return(0);
 
7319
}
 
7320
 
 
7321
 
 
7322
/*
 
7323
  Check if given expression uses only table fields covered by the given index
 
7324
 
 
7325
  SYNOPSIS
 
7326
    uses_index_fields_only()
 
7327
      item           Expression to check
 
7328
      tbl            The table having the index
 
7329
      keyno          The index number
 
7330
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7331
 
 
7332
  DESCRIPTION
 
7333
    Check if given expression only uses fields covered by index #keyno in the
 
7334
    table tbl. The expression can use any fields in any other tables.
 
7335
 
 
7336
    The expression is guaranteed not to be AND or OR - those constructs are
 
7337
    handled outside of this function.
 
7338
 
 
7339
  RETURN
 
7340
    true   Yes
 
7341
    false  No
 
7342
*/
 
7343
 
 
7344
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
 
7345
                            bool other_tbls_ok)
 
7346
{
 
7347
  if (item->const_item())
 
7348
    return true;
 
7349
 
 
7350
  /*
 
7351
    Don't push down the triggered conditions. Nested outer joins execution
 
7352
    code may need to evaluate a condition several times (both triggered and
 
7353
    untriggered), and there is no way to put thi
 
7354
    TODO: Consider cloning the triggered condition and using the copies for:
 
7355
      1. push the first copy down, to have most restrictive index condition
 
7356
         possible
 
7357
      2. Put the second copy into tab->select_cond.
 
7358
  */
 
7359
  if (item->type() == Item::FUNC_ITEM &&
 
7360
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7361
    return false;
 
7362
 
 
7363
  if (!(item->used_tables() & tbl->map))
 
7364
    return other_tbls_ok;
 
7365
 
 
7366
  Item::Type item_type= item->type();
 
7367
  switch (item_type) {
 
7368
  case Item::FUNC_ITEM:
 
7369
    {
 
7370
      /* This is a function, apply condition recursively to arguments */
 
7371
      Item_func *item_func= (Item_func*)item;
 
7372
      Item **child;
 
7373
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7374
      for (child= item_func->arguments(); child != item_end; child++)
 
7375
      {
 
7376
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7377
          return false;
 
7378
      }
 
7379
      return true;
 
7380
    }
 
7381
  case Item::COND_ITEM:
 
7382
    {
 
7383
      /* This is a function, apply condition recursively to arguments */
 
7384
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7385
      Item *list_item;
 
7386
      while ((list_item=li++))
 
7387
      {
 
7388
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7389
          return false;
 
7390
      }
 
7391
      return true;
 
7392
    }
 
7393
  case Item::FIELD_ITEM:
 
7394
    {
 
7395
      Item_field *item_field= (Item_field*)item;
 
7396
      if (item_field->field->table != tbl)
 
7397
        return true;
 
7398
      return item_field->field->part_of_key.is_set(keyno);
 
7399
    }
 
7400
  case Item::REF_ITEM:
 
7401
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7402
                                  other_tbls_ok);
 
7403
  default:
 
7404
    return false; /* Play it safe, don't push unknown non-const items */
 
7405
  }
 
7406
}
 
7407
 
 
7408
 
1216
7409
#define ICP_COND_USES_INDEX_ONLY 10
1217
7410
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
7411
/*
 
7412
  Get a part of the condition that can be checked using only index fields
 
7413
 
 
7414
  SYNOPSIS
 
7415
    make_cond_for_index()
 
7416
      cond           The source condition
 
7417
      table          The table that is partially available
 
7418
      keyno          The index in the above table. Only fields covered by the index
 
7419
                     are available
 
7420
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7421
 
 
7422
  DESCRIPTION
 
7423
    Get a part of the condition that can be checked when for the given table
 
7424
    we have values only of fields covered by some index. The condition may
 
7425
    refer to other tables, it is assumed that we have values of all of their
 
7426
    fields.
 
7427
 
 
7428
    Example:
 
7429
      make_cond_for_index(
 
7430
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7431
          t2, keyno(t2.key1))
 
7432
      will return
 
7433
        "cond(t1.field) AND cond(t2.key2)"
 
7434
 
 
7435
  RETURN
 
7436
    Index condition, or NULL if no condition could be inferred.
 
7437
*/
 
7438
 
 
7439
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7440
                          bool other_tbls_ok)
 
7441
{
 
7442
  if (!cond)
 
7443
    return NULL;
 
7444
  if (cond->type() == Item::COND_ITEM)
 
7445
  {
 
7446
    uint32_t n_marked= 0;
 
7447
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7448
    {
 
7449
      Item_cond_and *new_cond=new Item_cond_and;
 
7450
      if (!new_cond)
 
7451
        return (COND*) 0;
 
7452
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7453
      Item *item;
 
7454
      while ((item=li++))
 
7455
      {
 
7456
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7457
        if (fix)
 
7458
          new_cond->argument_list()->push_back(fix);
 
7459
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7460
      }
 
7461
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7462
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7463
      switch (new_cond->argument_list()->elements) {
 
7464
      case 0:
 
7465
        return (COND*) 0;
 
7466
      case 1:
 
7467
        return new_cond->argument_list()->head();
 
7468
      default:
 
7469
        new_cond->quick_fix_field();
 
7470
        return new_cond;
 
7471
      }
 
7472
    }
 
7473
    else /* It's OR */
 
7474
    {
 
7475
      Item_cond_or *new_cond=new Item_cond_or;
 
7476
      if (!new_cond)
 
7477
        return (COND*) 0;
 
7478
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7479
      Item *item;
 
7480
      while ((item=li++))
 
7481
      {
 
7482
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7483
        if (!fix)
 
7484
          return (COND*) 0;
 
7485
        new_cond->argument_list()->push_back(fix);
 
7486
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7487
      }
 
7488
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7489
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7490
      new_cond->quick_fix_field();
 
7491
      new_cond->top_level_item();
 
7492
      return new_cond;
 
7493
    }
 
7494
  }
 
7495
 
 
7496
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7497
    return (COND*) 0;
 
7498
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7499
  return cond;
 
7500
}
 
7501
 
 
7502
 
 
7503
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7504
{
 
7505
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7506
    return 0; /* Already checked */
 
7507
 
 
7508
  if (cond->type() == Item::COND_ITEM)
 
7509
  {
 
7510
    table_map tbl_map= 0;
 
7511
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7512
    {
 
7513
      /* Create new top level AND item */
 
7514
      Item_cond_and *new_cond=new Item_cond_and;
 
7515
      if (!new_cond)
 
7516
        return (COND*) 0;
 
7517
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7518
      Item *item;
 
7519
      while ((item=li++))
 
7520
      {
 
7521
        Item *fix= make_cond_remainder(item, exclude_index);
 
7522
        if (fix)
 
7523
        {
 
7524
          new_cond->argument_list()->push_back(fix);
 
7525
          tbl_map |= fix->used_tables();
 
7526
        }
 
7527
      }
 
7528
      switch (new_cond->argument_list()->elements) {
 
7529
      case 0:
 
7530
        return (COND*) 0;
 
7531
      case 1:
 
7532
        return new_cond->argument_list()->head();
 
7533
      default:
 
7534
        new_cond->quick_fix_field();
 
7535
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7536
        return new_cond;
 
7537
      }
 
7538
    }
 
7539
    else /* It's OR */
 
7540
    {
 
7541
      Item_cond_or *new_cond=new Item_cond_or;
 
7542
      if (!new_cond)
 
7543
        return (COND*) 0;
 
7544
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7545
      Item *item;
 
7546
      while ((item=li++))
 
7547
      {
 
7548
        Item *fix= make_cond_remainder(item, false);
 
7549
        if (!fix)
 
7550
          return (COND*) 0;
 
7551
        new_cond->argument_list()->push_back(fix);
 
7552
        tbl_map |= fix->used_tables();
 
7553
      }
 
7554
      new_cond->quick_fix_field();
 
7555
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7556
      new_cond->top_level_item();
 
7557
      return new_cond;
 
7558
    }
 
7559
  }
 
7560
  return cond;
 
7561
}
 
7562
 
 
7563
 
 
7564
/*
 
7565
  Try to extract and push the index condition
 
7566
 
 
7567
  SYNOPSIS
 
7568
    push_index_cond()
 
7569
      tab            A join tab that has tab->table->file and its condition
 
7570
                     in tab->select_cond
 
7571
      keyno          Index for which extract and push the condition
 
7572
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7573
 
 
7574
  DESCRIPTION
 
7575
    Try to extract and push the index condition down to table handler
 
7576
*/
 
7577
 
 
7578
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7579
{
 
7580
  Item *idx_cond;
 
7581
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7582
      tab->join->session->variables.engine_condition_pushdown)
 
7583
  {
 
7584
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7585
                                  other_tbls_ok);
 
7586
 
 
7587
    if (idx_cond)
 
7588
    {
 
7589
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7590
      Item *idx_remainder_cond=
 
7591
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7592
 
 
7593
      /*
 
7594
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7595
        condition.
 
7596
        TODO: This check happens to work on current ICP implementations, but
 
7597
        there may exist a compliant implementation that will not work
 
7598
        correctly with it. Sort this out when we stabilize the condition
 
7599
        pushdown APIs.
 
7600
      */
 
7601
      if (idx_remainder_cond != idx_cond)
 
7602
        tab->ref.disable_cache= true;
 
7603
 
 
7604
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7605
 
 
7606
      if (row_cond)
 
7607
      {
 
7608
        if (!idx_remainder_cond)
 
7609
          tab->select_cond= row_cond;
 
7610
        else
 
7611
        {
 
7612
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7613
          tab->select_cond->quick_fix_field();
 
7614
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
 
7615
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7616
        }
 
7617
      }
 
7618
      else
 
7619
        tab->select_cond= idx_remainder_cond;
 
7620
      if (tab->select)
 
7621
      {
 
7622
        tab->select->cond= tab->select_cond;
 
7623
      }
 
7624
    }
 
7625
  }
 
7626
  return;
 
7627
}
 
7628
 
 
7629
 
 
7630
 
 
7631
    /*
 
7632
      Determine if the set is already ordered for order_st BY, so it can
 
7633
      disable join cache because it will change the ordering of the results.
 
7634
      Code handles sort table that is at any location (not only first after
 
7635
      the const tables) despite the fact that it's currently prohibited.
 
7636
      We must disable join cache if the first non-const table alone is
 
7637
      ordered. If there is a temp table the ordering is done as a last
 
7638
      operation and doesn't prevent join cache usage.
 
7639
    */
 
7640
uint32_t make_join_orderinfo(JOIN *join)
 
7641
{
 
7642
  uint32_t i;
 
7643
  if (join->need_tmp)
 
7644
    return join->tables;
 
7645
 
 
7646
  for (i=join->const_tables ; i < join->tables ; i++)
 
7647
  {
 
7648
    JOIN_TAB *tab=join->join_tab+i;
 
7649
    Table *table=tab->table;
 
7650
    if ((table == join->sort_by_table &&
 
7651
         (!join->order || join->skip_sort_order)) ||
 
7652
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7653
    {
 
7654
      break;
 
7655
    }
 
7656
  }
 
7657
  return i;
 
7658
}
 
7659
 
 
7660
 
 
7661
/*
 
7662
  Plan refinement stage: do various set ups for the executioner
 
7663
 
 
7664
  SYNOPSIS
 
7665
    make_join_readinfo()
 
7666
      join           Join being processed
 
7667
      options        Join's options (checking for SELECT_DESCRIBE,
 
7668
                     SELECT_NO_JOIN_CACHE)
 
7669
      no_jbuf_after  Don't use join buffering after table with this number.
 
7670
 
 
7671
  DESCRIPTION
 
7672
    Plan refinement stage: do various set ups for the executioner
 
7673
      - set up use of join buffering
 
7674
      - push index conditions
 
7675
      - increment counters
 
7676
      - etc
 
7677
 
 
7678
  RETURN
 
7679
    false - OK
 
7680
    true  - Out of memory
 
7681
*/
 
7682
 
 
7683
static bool
 
7684
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7685
{
 
7686
  uint32_t i;
 
7687
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7688
  bool sorted= 1;
 
7689
 
 
7690
  for (i=join->const_tables ; i < join->tables ; i++)
 
7691
  {
 
7692
    JOIN_TAB *tab=join->join_tab+i;
 
7693
    Table *table=tab->table;
 
7694
    bool using_join_cache;
 
7695
    tab->read_record.table= table;
 
7696
    tab->read_record.file=table->file;
 
7697
    tab->next_select=sub_select;                /* normal select */
 
7698
    /*
 
7699
      TODO: don't always instruct first table's ref/range access method to
 
7700
      produce sorted output.
 
7701
    */
 
7702
    tab->sorted= sorted;
 
7703
    sorted= 0;                                  // only first must be sorted
 
7704
    if (tab->insideout_match_tab)
 
7705
    {
 
7706
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
 
7707
                                                         [tab->index].
 
7708
                                                         key_length)))
 
7709
        return true;
 
7710
    }
 
7711
    switch (tab->type) {
 
7712
    case JT_SYSTEM:                             // Only happens with left join
 
7713
      table->status=STATUS_NO_RECORD;
 
7714
      tab->read_first_record= join_read_system;
 
7715
      tab->read_record.read_record= join_no_more_records;
 
7716
      break;
 
7717
    case JT_CONST:                              // Only happens with left join
 
7718
      table->status=STATUS_NO_RECORD;
 
7719
      tab->read_first_record= join_read_const;
 
7720
      tab->read_record.read_record= join_no_more_records;
 
7721
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7722
          !table->no_keyread)
 
7723
      {
 
7724
        table->key_read=1;
 
7725
        table->file->extra(HA_EXTRA_KEYREAD);
 
7726
      }
 
7727
      break;
 
7728
    case JT_EQ_REF:
 
7729
      table->status=STATUS_NO_RECORD;
 
7730
      if (tab->select)
 
7731
      {
 
7732
        delete tab->select->quick;
 
7733
        tab->select->quick=0;
 
7734
      }
 
7735
      delete tab->quick;
 
7736
      tab->quick=0;
 
7737
      tab->read_first_record= join_read_key;
 
7738
      tab->read_record.read_record= join_no_more_records;
 
7739
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7740
          !table->no_keyread)
 
7741
      {
 
7742
        table->key_read=1;
 
7743
        table->file->extra(HA_EXTRA_KEYREAD);
 
7744
      }
 
7745
      else
 
7746
        push_index_cond(tab, tab->ref.key, true);
 
7747
      break;
 
7748
    case JT_REF_OR_NULL:
 
7749
    case JT_REF:
 
7750
      table->status=STATUS_NO_RECORD;
 
7751
      if (tab->select)
 
7752
      {
 
7753
        delete tab->select->quick;
 
7754
        tab->select->quick=0;
 
7755
      }
 
7756
      delete tab->quick;
 
7757
      tab->quick=0;
 
7758
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7759
          !table->no_keyread)
 
7760
      {
 
7761
        table->key_read=1;
 
7762
        table->file->extra(HA_EXTRA_KEYREAD);
 
7763
      }
 
7764
      else
 
7765
        push_index_cond(tab, tab->ref.key, true);
 
7766
      if (tab->type == JT_REF)
 
7767
      {
 
7768
        tab->read_first_record= join_read_always_key;
 
7769
        tab->read_record.read_record= tab->insideout_match_tab?
 
7770
           join_read_next_same_diff : join_read_next_same;
 
7771
      }
 
7772
      else
 
7773
      {
 
7774
        tab->read_first_record= join_read_always_key_or_null;
 
7775
        tab->read_record.read_record= join_read_next_same_or_null;
 
7776
      }
 
7777
      break;
 
7778
    case JT_ALL:
 
7779
      /*
 
7780
        If previous table use cache
 
7781
        If the incoming data set is already sorted don't use cache.
 
7782
      */
 
7783
      table->status=STATUS_NO_RECORD;
 
7784
      using_join_cache= false;
 
7785
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7786
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7787
          !tab->insideout_match_tab)
 
7788
      {
 
7789
        if ((options & SELECT_DESCRIBE) ||
 
7790
            !join_init_cache(join->session,join->join_tab+join->const_tables,
 
7791
                             i-join->const_tables))
 
7792
        {
 
7793
          using_join_cache= true;
 
7794
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7795
        }
 
7796
      }
 
7797
      /* These init changes read_record */
 
7798
      if (tab->use_quick == 2)
 
7799
      {
 
7800
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7801
        tab->read_first_record= join_init_quick_read_record;
 
7802
        if (statistics)
 
7803
          status_var_increment(join->session->status_var.select_range_check_count);
 
7804
      }
 
7805
      else
 
7806
      {
 
7807
        tab->read_first_record= join_init_read_record;
 
7808
        if (i == join->const_tables)
 
7809
        {
 
7810
          if (tab->select && tab->select->quick)
 
7811
          {
 
7812
            if (statistics)
 
7813
              status_var_increment(join->session->status_var.select_range_count);
 
7814
          }
 
7815
          else
 
7816
          {
 
7817
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7818
            if (statistics)
 
7819
              status_var_increment(join->session->status_var.select_scan_count);
 
7820
          }
 
7821
        }
 
7822
        else
 
7823
        {
 
7824
          if (tab->select && tab->select->quick)
 
7825
          {
 
7826
            if (statistics)
 
7827
              status_var_increment(join->session->status_var.select_full_range_join_count);
 
7828
          }
 
7829
          else
 
7830
          {
 
7831
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7832
            if (statistics)
 
7833
              status_var_increment(join->session->status_var.select_full_join_count);
 
7834
          }
 
7835
        }
 
7836
        if (!table->no_keyread)
 
7837
        {
 
7838
          if (tab->select && tab->select->quick &&
 
7839
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7840
              table->covering_keys.is_set(tab->select->quick->index))
 
7841
          {
 
7842
            table->key_read=1;
 
7843
            table->file->extra(HA_EXTRA_KEYREAD);
 
7844
          }
 
7845
          else if (!table->covering_keys.is_clear_all() &&
 
7846
                   !(tab->select && tab->select->quick))
 
7847
          {                                     // Only read index tree
 
7848
            if (!tab->insideout_match_tab)
 
7849
            {
 
7850
              /*
 
7851
                See bug #26447: "Using the clustered index for a table scan
 
7852
                is always faster than using a secondary index".
 
7853
              */
 
7854
              if (table->s->primary_key != MAX_KEY &&
 
7855
                  table->file->primary_key_is_clustered())
 
7856
                tab->index= table->s->primary_key;
 
7857
              else
 
7858
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7859
            }
 
7860
            tab->read_first_record= join_read_first;
 
7861
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7862
          }
 
7863
        }
 
7864
        if (tab->select && tab->select->quick &&
 
7865
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7866
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7867
      }
 
7868
      break;
 
7869
    default:
 
7870
      break;                                    /* purecov: deadcode */
 
7871
    case JT_UNKNOWN:
 
7872
    case JT_MAYBE_REF:
 
7873
      abort();                                  /* purecov: deadcode */
 
7874
    }
 
7875
  }
 
7876
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7877
  return(false);
 
7878
}
 
7879
 
 
7880
 
 
7881
/**
 
7882
  Give error if we some tables are done with a full join.
 
7883
 
 
7884
  This is used by multi_table_update and multi_table_delete when running
 
7885
  in safe mode.
 
7886
 
 
7887
  @param join           Join condition
 
7888
 
 
7889
  @retval
 
7890
    0   ok
 
7891
  @retval
 
7892
    1   Error (full join used)
 
7893
*/
 
7894
 
 
7895
bool error_if_full_join(JOIN *join)
 
7896
{
 
7897
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7898
       tab < end;
 
7899
       tab++)
 
7900
  {
 
7901
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7902
    {
 
7903
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7904
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7905
      return(1);
 
7906
    }
 
7907
  }
 
7908
  return(0);
 
7909
}
 
7910
 
 
7911
 
 
7912
/**
 
7913
  cleanup JOIN_TAB.
 
7914
*/
 
7915
 
 
7916
void JOIN_TAB::cleanup()
 
7917
{
 
7918
  delete select;
 
7919
  select= 0;
 
7920
  delete quick;
 
7921
  quick= 0;
1227
7922
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
7923
    free(cache.buff);
1232
 
  }
1233
7924
  cache.buff= 0;
1234
7925
  limit= 0;
1235
7926
  if (table)
1237
7928
    if (table->key_read)
1238
7929
    {
1239
7930
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7931
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7932
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7933
    table->file->ha_index_or_rnd_end();
1243
7934
    /*
1244
7935
      We need to reset this for next select
1245
7936
      (Tested in part_of_refkey)
1246
7937
    */
1247
7938
    table->reginfo.join_tab= 0;
1248
7939
  }
1249
 
  read_record.end_read_record();
1250
 
}
1251
 
 
1252
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1253
 
{
1254
 
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1255
 
  {
1256
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1257
 
      return 0;
1258
 
  }
1259
 
  return 1;
1260
 
}
1261
 
 
1262
 
/**
1263
 
  Remove the following expressions from ORDER BY and GROUP BY:
 
7940
  end_read_record(&read_record);
 
7941
}
 
7942
 
 
7943
 
 
7944
/**
 
7945
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7946
  (table cursors), free quick selects.
 
7947
 
 
7948
    This function is called in the end of execution of a JOIN, before the used
 
7949
    tables are unlocked and closed.
 
7950
 
 
7951
    For a join that is resolved using a temporary table, the first sweep is
 
7952
    performed against actual tables and an intermediate result is inserted
 
7953
    into the temprorary table.
 
7954
    The last sweep is performed against the temporary table. Therefore,
 
7955
    the base tables and associated buffers used to fill the temporary table
 
7956
    are no longer needed, and this function is called to free them.
 
7957
 
 
7958
    For a join that is performed without a temporary table, this function
 
7959
    is called after all rows are sent, but before EOF packet is sent.
 
7960
 
 
7961
    For a simple SELECT with no subqueries this function performs a full
 
7962
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7963
    tables.
 
7964
 
 
7965
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7966
    do the full cleanup and need to do a partial cleanup only.
 
7967
    - If a JOIN is not the top level join, we must not unlock the tables
 
7968
    because the outer select may not have been evaluated yet, and we
 
7969
    can't unlock only selected tables of a query.
 
7970
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7971
    should not free quick selects and join buffers because they will be
 
7972
    needed for the next execution of the correlated subquery.
 
7973
    - However, if this is a JOIN for a [sub]select, which is not
 
7974
    a correlated subquery itself, but has subqueries, we can free it
 
7975
    fully and also free JOINs of all its subqueries. The exception
 
7976
    is a subquery in SELECT list, e.g: @n
 
7977
    SELECT a, (select cmax(b) from t1) group by c @n
 
7978
    This subquery will not be evaluated at first sweep and its value will
 
7979
    not be inserted into the temporary table. Instead, it's evaluated
 
7980
    when selecting from the temporary table. Therefore, it can't be freed
 
7981
    here even though it's not correlated.
 
7982
 
 
7983
  @todo
 
7984
    Unlock tables even if the join isn't top level select in the tree
 
7985
*/
 
7986
 
 
7987
void JOIN::join_free()
 
7988
{
 
7989
  Select_Lex_Unit *tmp_unit;
 
7990
  Select_Lex *sl;
 
7991
  /*
 
7992
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7993
    free all tables.
 
7994
  */
 
7995
  bool full= (!select_lex->uncacheable && !session->lex->describe);
 
7996
  bool can_unlock= full;
 
7997
 
 
7998
  cleanup(full);
 
7999
 
 
8000
  for (tmp_unit= select_lex->first_inner_unit();
 
8001
       tmp_unit;
 
8002
       tmp_unit= tmp_unit->next_unit())
 
8003
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
8004
    {
 
8005
      Item_subselect *subselect= sl->master_unit()->item;
 
8006
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
8007
      /*
 
8008
        If this join is evaluated, we can fully clean it up and clean up all
 
8009
        its underlying joins even if they are correlated -- they will not be
 
8010
        used any more anyway.
 
8011
        If this join is not yet evaluated, we still must clean it up to
 
8012
        close its table cursors -- it may never get evaluated, as in case of
 
8013
        ... HAVING false OR a IN (SELECT ...))
 
8014
        but all table cursors must be closed before the unlock.
 
8015
      */
 
8016
      sl->cleanup_all_joins(full_local);
 
8017
      /* Can't unlock if at least one JOIN is still needed */
 
8018
      can_unlock= can_unlock && full_local;
 
8019
    }
 
8020
 
 
8021
  /*
 
8022
    We are not using tables anymore
 
8023
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8024
  */
 
8025
  if (can_unlock && lock && session->lock &&
 
8026
      !(select_options & SELECT_NO_UNLOCK) &&
 
8027
      !select_lex->subquery_in_having &&
 
8028
      (select_lex == (session->lex->unit.fake_select_lex ?
 
8029
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
8030
  {
 
8031
    /*
 
8032
      TODO: unlock tables even if the join isn't top level select in the
 
8033
      tree.
 
8034
    */
 
8035
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
 
8036
    lock= 0;
 
8037
  }
 
8038
 
 
8039
  return;
 
8040
}
 
8041
 
 
8042
 
 
8043
/**
 
8044
  Free resources of given join.
 
8045
 
 
8046
  @param fill   true if we should free all resources, call with full==1
 
8047
                should be last, before it this function can be called with
 
8048
                full==0
 
8049
 
 
8050
  @note
 
8051
    With subquery this function definitely will be called several times,
 
8052
    but even for simple query it can be called several times.
 
8053
*/
 
8054
 
 
8055
void JOIN::cleanup(bool full)
 
8056
{
 
8057
  if (table)
 
8058
  {
 
8059
    JOIN_TAB *tab,*end;
 
8060
    /*
 
8061
      Only a sorted table may be cached.  This sorted table is always the
 
8062
      first non const table in join->table
 
8063
    */
 
8064
    if (tables > const_tables) // Test for not-const tables
 
8065
    {
 
8066
      free_io_cache(table[const_tables]);
 
8067
      filesort_free_buffers(table[const_tables],full);
 
8068
    }
 
8069
 
 
8070
    if (full)
 
8071
    {
 
8072
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8073
        tab->cleanup();
 
8074
      table= 0;
 
8075
    }
 
8076
    else
 
8077
    {
 
8078
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8079
      {
 
8080
        if (tab->table)
 
8081
          tab->table->file->ha_index_or_rnd_end();
 
8082
      }
 
8083
    }
 
8084
    cleanup_sj_tmp_tables(this);//
 
8085
  }
 
8086
  /*
 
8087
    We are not using tables anymore
 
8088
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8089
  */
 
8090
  if (full)
 
8091
  {
 
8092
    if (tmp_join)
 
8093
      tmp_table_param.copy_field= 0;
 
8094
    group_fields.delete_elements();
 
8095
    /*
 
8096
      We can't call delete_elements() on copy_funcs as this will cause
 
8097
      problems in free_elements() as some of the elements are then deleted.
 
8098
    */
 
8099
    tmp_table_param.copy_funcs.empty();
 
8100
    /*
 
8101
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8102
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8103
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8104
      be removed in tmp_table_param.cleanup().
 
8105
    */
 
8106
    if (tmp_join &&
 
8107
        tmp_join != this &&
 
8108
        tmp_join->tmp_table_param.copy_field ==
 
8109
        tmp_table_param.copy_field)
 
8110
    {
 
8111
      tmp_join->tmp_table_param.copy_field=
 
8112
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8113
    }
 
8114
    tmp_table_param.cleanup();
 
8115
  }
 
8116
  return;
 
8117
}
 
8118
 
 
8119
 
 
8120
/**
 
8121
  Remove the following expressions from order_st BY and GROUP BY:
1264
8122
  Constant expressions @n
1265
8123
  Expression that only uses tables that are of type EQ_REF and the reference
1266
8124
  is in the order_st list or if all refereed tables are of the above type.
1267
8125
 
1268
8126
  In the following, the X field can be removed:
1269
8127
  @code
1270
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
1271
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
 
8128
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8129
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1272
8130
  @endcode
1273
8131
 
1274
8132
  These can't be optimized:
1275
8133
  @code
1276
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
1277
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
1278
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
 
8134
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8135
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8136
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1279
8137
  @endcode
1280
8138
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8139
 
 
8140
static bool
 
8141
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1282
8142
{
1283
8143
  if (tab->cached_eq_ref_table)                 // If cached
1284
8144
    return tab->eq_ref_table;
1285
8145
  tab->cached_eq_ref_table=1;
1286
8146
  /* We can skip const tables only if not an outer table */
1287
 
  if (tab->type == AM_CONST && !tab->first_inner)
1288
 
    return (tab->eq_ref_table=1);
1289
 
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
8147
  if (tab->type == JT_CONST && !tab->first_inner)
 
8148
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8149
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8150
    return (tab->eq_ref_table=0);               // We must use this
1291
8151
  Item **ref_item=tab->ref.items;
1292
8152
  Item **end=ref_item+tab->ref.key_parts;
1297
8157
  {
1298
8158
    if (! (*ref_item)->const_item())
1299
8159
    {                                           // Not a const ref
1300
 
      Order *order;
 
8160
      order_st *order;
1301
8161
      for (order=start_order ; order ; order=order->next)
1302
8162
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8163
        if ((*ref_item)->eq(order->item[0],0))
 
8164
          break;
1305
8165
      }
1306
8166
      if (order)
1307
8167
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8168
        found++;
 
8169
        assert(!(order->used & map));
 
8170
        order->used|=map;
 
8171
        continue;                               // Used in order_st BY
1312
8172
      }
1313
8173
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8174
        return (tab->eq_ref_table=0);
1315
8175
    }
1316
8176
  }
1317
8177
  /* Check that there was no reference to table before sort order */
1323
8183
      continue;
1324
8184
    }
1325
8185
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8186
      return (tab->eq_ref_table=0);
 
8187
  }
 
8188
  return tab->eq_ref_table=1;
 
8189
}
 
8190
 
 
8191
 
 
8192
static bool
 
8193
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8194
{
 
8195
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8196
  {
 
8197
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8198
      return 0;
 
8199
  }
 
8200
  return 1;
 
8201
}
 
8202
 
 
8203
 
 
8204
/** Update the dependency map for the tables. */
 
8205
 
 
8206
static void update_depend_map(JOIN *join)
 
8207
{
 
8208
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8209
 
 
8210
  for (; join_tab != end ; join_tab++)
 
8211
  {
 
8212
    TABLE_REF *ref= &join_tab->ref;
 
8213
    table_map depend_map=0;
 
8214
    Item **item=ref->items;
 
8215
    uint32_t i;
 
8216
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8217
      depend_map|=(*item)->used_tables();
 
8218
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8219
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8220
    for (JOIN_TAB **tab=join->map2table;
 
8221
         depend_map ;
 
8222
         tab++,depend_map>>=1 )
 
8223
    {
 
8224
      if (depend_map & 1)
 
8225
        ref->depend_map|=(*tab)->ref.depend_map;
 
8226
    }
 
8227
  }
 
8228
}
 
8229
 
 
8230
 
 
8231
/** Update the dependency map for the sort order. */
 
8232
 
 
8233
static void update_depend_map(JOIN *join, order_st *order)
 
8234
{
 
8235
  for (; order ; order=order->next)
 
8236
  {
 
8237
    table_map depend_map;
 
8238
    order->item[0]->update_used_tables();
 
8239
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8240
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8241
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8242
        && !order->item[0]->with_sum_func)
 
8243
    {
 
8244
      for (JOIN_TAB **tab=join->map2table;
 
8245
           depend_map ;
 
8246
           tab++, depend_map>>=1)
 
8247
      {
 
8248
        if (depend_map & 1)
 
8249
          order->depend_map|=(*tab)->ref.depend_map;
 
8250
      }
 
8251
    }
 
8252
  }
 
8253
}
 
8254
 
 
8255
 
 
8256
/**
 
8257
  Remove all constants and check if order_st only contains simple
 
8258
  expressions.
 
8259
 
 
8260
  simple_order is set to 1 if sort_order only uses fields from head table
 
8261
  and the head table is not a LEFT JOIN table.
 
8262
 
 
8263
  @param join                   Join handler
 
8264
  @param first_order            List of SORT or GROUP order
 
8265
  @param cond                   WHERE statement
 
8266
  @param change_list            Set to 1 if we should remove things from list.
 
8267
                               If this is not set, then only simple_order is
 
8268
                               calculated.
 
8269
  @param simple_order           Set to 1 if we are only using simple expressions
 
8270
 
 
8271
  @return
 
8272
    Returns new sort order
 
8273
*/
 
8274
 
 
8275
static order_st *
 
8276
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8277
             bool change_list, bool *simple_order)
 
8278
{
 
8279
  if (join->tables == join->const_tables)
 
8280
    return change_list ? 0 : first_order;               // No need to sort
 
8281
 
 
8282
  order_st *order,**prev_ptr;
 
8283
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8284
  table_map not_const_tables= ~join->const_table_map;
 
8285
  table_map ref;
 
8286
 
 
8287
  prev_ptr= &first_order;
 
8288
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8289
 
 
8290
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8291
 
 
8292
  update_depend_map(join, first_order);
 
8293
  for (order=first_order; order ; order=order->next)
 
8294
  {
 
8295
    table_map order_tables=order->item[0]->used_tables();
 
8296
    if (order->item[0]->with_sum_func)
 
8297
      *simple_order=0;                          // Must do a temp table to sort
 
8298
    else if (!(order_tables & not_const_tables))
 
8299
    {
 
8300
      if (order->item[0]->with_subselect)
 
8301
        order->item[0]->val_str(&order->item[0]->str_value);
 
8302
      continue;                                 // skip const item
 
8303
    }
 
8304
    else
 
8305
    {
 
8306
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8307
        *simple_order=0;
 
8308
      else
 
8309
      {
 
8310
        Item *comp_item=0;
 
8311
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8312
        {
 
8313
          continue;
 
8314
        }
 
8315
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8316
        {
 
8317
          if (!(order_tables & first_table) &&
 
8318
              only_eq_ref_tables(join,first_order, ref))
 
8319
          {
 
8320
            continue;
 
8321
          }
 
8322
          *simple_order=0;                      // Must do a temp table to sort
 
8323
        }
 
8324
      }
 
8325
    }
 
8326
    if (change_list)
 
8327
      *prev_ptr= order;                         // use this entry
 
8328
    prev_ptr= &order->next;
 
8329
  }
 
8330
  if (change_list)
 
8331
    *prev_ptr=0;
 
8332
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8333
    *simple_order=1;
 
8334
  return(first_order);
 
8335
}
 
8336
 
 
8337
 
 
8338
static int
 
8339
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8340
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8341
                 const char *info, Item *having)
 
8342
{
 
8343
  if (select_options & SELECT_DESCRIBE)
 
8344
  {
 
8345
    select_describe(join, false, false, false, info);
 
8346
    return(0);
 
8347
  }
 
8348
 
 
8349
  join->join_free();
 
8350
 
 
8351
  if (send_row)
 
8352
  {
 
8353
    for (TableList *table= tables; table; table= table->next_leaf)
 
8354
      mark_as_null_row(table->table);           // All fields are NULL
 
8355
    if (having && having->val_int() == 0)
 
8356
      send_row=0;
 
8357
  }
 
8358
  if (!(result->send_fields(fields,
 
8359
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8360
  {
 
8361
    if (send_row)
 
8362
    {
 
8363
      List_iterator_fast<Item> it(fields);
 
8364
      Item *item;
 
8365
      while ((item= it++))
 
8366
        item->no_rows_in_result();
 
8367
      result->send_data(fields);
 
8368
    }
 
8369
    result->send_eof();                         // Should be safe
 
8370
  }
 
8371
  /* Update results for FOUND_ROWS */
 
8372
  join->session->limit_found_rows= join->session->examined_row_count= 0;
 
8373
  return(0);
 
8374
}
 
8375
 
 
8376
/*
 
8377
  used only in JOIN::clear
 
8378
*/
 
8379
static void clear_tables(JOIN *join)
 
8380
{
 
8381
  /*
 
8382
    must clear only the non-const tables, as const tables
 
8383
    are not re-calculated.
 
8384
  */
 
8385
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8386
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8387
}
 
8388
 
 
8389
/*****************************************************************************
 
8390
  Make som simple condition optimization:
 
8391
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8392
  Remove all dummy tests 'item = item', 'const op const'.
 
8393
  Remove all 'item is NULL', when item can never be null!
 
8394
  item->marker should be 0 for all items on entry
 
8395
  Return in cond_value false if condition is impossible (1 = 2)
 
8396
*****************************************************************************/
 
8397
 
 
8398
class COND_CMP :public ilink {
 
8399
public:
 
8400
  static void *operator new(size_t size)
 
8401
  {
 
8402
    return (void*) sql_alloc((uint32_t) size);
 
8403
  }
 
8404
  static void operator delete(void *, size_t)
 
8405
  { TRASH(ptr, size); }
 
8406
 
 
8407
  Item *and_level;
 
8408
  Item_func *cmp_func;
 
8409
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8410
};
 
8411
 
 
8412
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8413
template class I_List<COND_CMP>;
 
8414
template class I_List_iterator<COND_CMP>;
 
8415
#endif
 
8416
 
1330
8417
 
1331
8418
/**
1332
8419
  Find the multiple equality predicate containing a field.
1346
8433
    - Item_equal for the found multiple equality predicate if a success;
1347
8434
    - NULL otherwise.
1348
8435
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8436
 
 
8437
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8438
                            bool *inherited_fl)
1350
8439
{
1351
8440
  Item_equal *item= 0;
1352
8441
  bool in_upper_level= false;
1353
8442
  while (cond_equal)
1354
8443
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8444
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8445
    while ((item= li++))
1357
8446
    {
1358
8447
      if (item->contains(field))
1367
8456
  return item;
1368
8457
}
1369
8458
 
 
8459
 
1370
8460
/**
1371
8461
  Check whether an equality can be used to build multiple equalities.
1372
8462
 
1448
8538
  @retval
1449
8539
    false   otherwise
1450
8540
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8541
 
 
8542
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8543
                                  Item *item, COND_EQUAL *cond_equal)
1455
8544
{
1456
8545
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8546
      right_item->type() == Item::FIELD_ITEM &&
1517
8606
        /* Merge two multiple equalities forming a new one */
1518
8607
        left_item_equal->merge(right_item_equal);
1519
8608
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8609
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8610
        while ((li++) != right_item_equal) {};
1522
8611
        li.remove();
1523
8612
      }
1612
8701
  return false;
1613
8702
}
1614
8703
 
 
8704
 
1615
8705
/**
1616
8706
  Convert row equalities into a conjunction of regular equalities.
1617
8707
 
1637
8727
  @retval
1638
8728
    false   otherwise
1639
8729
*/
1640
 
static bool check_row_equality(Session *session,
1641
 
                               Item *left_row, 
1642
 
                               Item_row *right_row,
1643
 
                               COND_EQUAL *cond_equal,
1644
 
                               List<Item>* eq_list)
 
8730
 
 
8731
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
 
8732
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
1645
8733
{
1646
8734
  uint32_t n= left_row->cols();
1647
8735
  for (uint32_t i= 0 ; i < n; i++)
1657
8745
                                       (Item_row *) right_item,
1658
8746
                                       cond_equal, eq_list);
1659
8747
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8748
        session->lex->current_select->cond_count++;
1661
8749
    }
1662
8750
    else
1663
8751
    {
1664
8752
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
 
8753
      session->lex->current_select->cond_count++;
1666
8754
    }
1667
8755
 
1668
8756
    if (!is_converted)
1678
8766
  return true;
1679
8767
}
1680
8768
 
 
8769
 
1681
8770
/**
1682
8771
  Eliminate row equalities and form multiple equalities predicates.
1683
8772
 
1707
8796
           or, if the equality is neither a simple one nor a row equality,
1708
8797
           or, if the procedure fails by a fatal error.
1709
8798
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8799
 
 
8800
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
 
8801
                           List<Item> *eq_list)
1711
8802
{
1712
8803
  if (item->type() == Item::FUNC_ITEM &&
1713
8804
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8809
    if (left_item->type() == Item::ROW_ITEM &&
1719
8810
        right_item->type() == Item::ROW_ITEM)
1720
8811
    {
1721
 
      session->getLex()->current_select->cond_count--;
 
8812
      session->lex->current_select->cond_count--;
1722
8813
      return check_row_equality(session,
1723
8814
                                (Item_row *) left_item,
1724
8815
                                (Item_row *) right_item,
1730
8821
  return false;
1731
8822
}
1732
8823
 
 
8824
 
1733
8825
/**
1734
8826
  Replace all equality predicates in a condition by multiple equality items.
1735
8827
 
1749
8841
    just an argument of a comparison predicate.
1750
8842
    The function also determines the maximum number of members in
1751
8843
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8844
    session->lex->current_select->max_equal_elems.
1753
8845
 
1754
8846
  @note
1755
8847
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1793
8885
  @return
1794
8886
    pointer to the transformed condition
1795
8887
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8888
 
 
8889
static COND *build_equal_items_for_cond(Session *session, COND *cond,
 
8890
                                        COND_EQUAL *inherited)
1797
8891
{
1798
8892
  Item_equal *item_equal;
1799
8893
  COND_EQUAL cond_equal;
1806
8900
      Item_func::COND_AND_FUNC;
1807
8901
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
8902
 
1809
 
    List<Item>::iterator li(args->begin());
 
8903
    List_iterator<Item> li(*args);
1810
8904
    Item *item;
1811
8905
 
1812
8906
    if (and_level)
1828
8922
          li.remove();
1829
8923
      }
1830
8924
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8925
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8926
      while ((item_equal= it++))
1833
8927
      {
1834
8928
        item_equal->fix_length_and_dec();
1835
8929
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8930
        set_if_bigger(session->lex->current_select->max_equal_elems,
1837
8931
                      item_equal->members());
1838
8932
      }
1839
8933
 
1844
8938
       Make replacement of equality predicates for lower levels
1845
8939
       of the condition expression.
1846
8940
    */
1847
 
    li= args->begin();
 
8941
    li.rewind();
1848
8942
    while ((item= li++))
1849
8943
    {
1850
8944
      Item *new_item;
1889
8983
        {
1890
8984
          item_equal->fix_length_and_dec();
1891
8985
          item_equal->update_used_tables();
1892
 
        }
 
8986
        }
1893
8987
        else
1894
8988
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8989
        set_if_bigger(session->lex->current_select->max_equal_elems,
1896
8990
                      item_equal->members());
1897
8991
        return item_equal;
1898
8992
      }
1901
8995
        /*
1902
8996
          Here a new AND level must be created. It can happen only
1903
8997
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
8998
        */
1905
8999
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
9000
        and_cond->quick_fix_field();
1907
9001
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
9002
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
9003
        while ((item_equal= it++))
1910
9004
        {
1911
9005
          item_equal->fix_length_and_dec();
1912
9006
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
9007
          set_if_bigger(session->lex->current_select->max_equal_elems,
1914
9008
                        item_equal->members());
1915
9009
        }
1916
9010
        and_cond->cond_equal= cond_equal;
1935
9029
  return cond;
1936
9030
}
1937
9031
 
 
9032
 
1938
9033
/**
1939
9034
  Build multiple equalities for a condition and all on expressions that
1940
9035
  inherit these multiple equalities.
1989
9084
    can get more freedom in performing join operations.
1990
9085
    Althogh we don't use this property now, it probably makes sense to use
1991
9086
    it in the future.
1992
 
  @param session                      Thread Cursor
 
9087
  @param session                      Thread handler
1993
9088
  @param cond                condition to build the multiple equalities for
1994
9089
  @param inherited           path to all inherited multiple equality items
1995
9090
  @param join_list           list of join tables to which the condition
2000
9095
  @return
2001
9096
    pointer to the transformed condition containing multiple equalities
2002
9097
*/
 
9098
 
2003
9099
static COND *build_equal_items(Session *session, COND *cond,
2004
9100
                               COND_EQUAL *inherited,
2005
9101
                               List<TableList> *join_list,
2031
9127
  if (join_list)
2032
9128
  {
2033
9129
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9130
    List_iterator<TableList> li(*join_list);
2035
9131
 
2036
9132
    while ((table= li++))
2037
9133
    {
2038
9134
      if (table->on_expr)
2039
9135
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9136
        List<TableList> *nested_join_list= table->nested_join ?
 
9137
          &table->nested_join->join_list : NULL;
2042
9138
        /*
2043
9139
          We can modify table->on_expr because its old value will
2044
9140
          be restored before re-execution of PS/SP.
2053
9149
  return cond;
2054
9150
}
2055
9151
 
 
9152
 
2056
9153
/**
2057
9154
  Compare field items by table order in the execution plan.
2058
9155
 
2072
9169
  @retval
2073
9170
    0  otherwise
2074
9171
*/
 
9172
 
2075
9173
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9174
                                  Item_field *field2,
 
9175
                                  void *table_join_idx)
2078
9176
{
2079
9177
  int cmp= 0;
2080
9178
  bool outer_ref= 0;
2090
9188
  }
2091
9189
  if (outer_ref)
2092
9190
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9191
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9192
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9193
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9194
}
2097
9195
 
 
9196
 
2098
9197
/**
2099
9198
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9199
 
2134
9233
    a pointer to the simple generated equality, if success.
2135
9234
    - 0, otherwise.
2136
9235
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9236
 
 
9237
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9238
                                  Item_equal *item_equal)
2138
9239
{
2139
9240
  List<Item> eq_list;
2140
9241
  Item_func_eq *eq_item= 0;
2204
9305
  return cond;
2205
9306
}
2206
9307
 
 
9308
 
2207
9309
/**
2208
9310
  Substitute every field reference in a condition by the best equal field
2209
9311
  and eliminate all multiple equality predicates.
2231
9333
  @return
2232
9334
    The transformed condition
2233
9335
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9336
 
 
9337
static COND* substitute_for_best_equal_field(COND *cond,
 
9338
                                             COND_EQUAL *cond_equal,
 
9339
                                             void *table_join_idx)
2235
9340
{
2236
9341
  Item_equal *item_equal;
2237
9342
 
2246
9351
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9352
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9353
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9354
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2250
9355
      while ((item_equal= it++))
2251
9356
      {
2252
9357
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9358
      }
2254
9359
    }
2255
9360
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9361
    List_iterator<Item> li(*cond_list);
2257
9362
    Item *item;
2258
9363
    while ((item= li++))
2259
9364
    {
2269
9374
 
2270
9375
    if (and_level)
2271
9376
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9377
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9378
      while ((item_equal= it++))
2274
9379
      {
2275
9380
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2299
9404
  return cond;
2300
9405
}
2301
9406
 
 
9407
 
2302
9408
/**
2303
9409
  Check appearance of new constant items in multiple equalities
2304
9410
  of a condition after reading a constant table.
2311
9417
  @param cond       condition whose multiple equalities are to be checked
2312
9418
  @param table      constant table that has been read
2313
9419
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9420
 
 
9421
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9422
{
2316
9423
  if (!(cond->used_tables() & tab->table->map))
2317
9424
    return;
2319
9426
  if (cond->type() == Item::COND_ITEM)
2320
9427
  {
2321
9428
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9429
    List_iterator_fast<Item> li(*cond_list);
2323
9430
    Item *item;
2324
9431
    while ((item= li++))
2325
9432
      update_const_equal_items(item, tab);
2338
9445
      while ((item_field= it++))
2339
9446
      {
2340
9447
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9448
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9449
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9450
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9451
        stat[0].const_keys.merge(possible_keys);
2345
9452
 
2346
9453
        /*
2347
9454
          For each field in the multiple equality (for which we know that it
2348
9455
          is a constant) we have to find its corresponding key part, and set
2349
9456
          that key part in const_key_parts.
2350
9457
        */
2351
 
        if (possible_keys.any())
 
9458
        if (!possible_keys.is_clear_all())
2352
9459
        {
2353
 
          Table *field_tab= field->getTable();
2354
 
          optimizer::KeyUse *use;
2355
 
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2356
 
            if (possible_keys.test(use->getKey()) &&
2357
 
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
 
9460
          Table *field_tab= field->table;
 
9461
          KEYUSE *use;
 
9462
          for (use= stat->keyuse; use && use->table == field_tab; use++)
 
9463
            if (possible_keys.is_set(use->key) &&
 
9464
                field_tab->key_info[use->key].key_part[use->keypart].field ==
2358
9465
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9466
              field_tab->const_key_parts[use->key]|= use->keypart_map;
2360
9467
        }
2361
9468
      }
2362
9469
    }
2363
9470
  }
2364
9471
}
2365
9472
 
 
9473
 
2366
9474
/*
2367
9475
  change field = field to field = const for each found field = const in the
2368
9476
  and_level
2369
9477
*/
2370
 
static void change_cond_ref_to_const(Session *session,
2371
 
                                     list<COND_CMP>& save_list,
2372
 
                                     Item *and_father,
2373
 
                                     Item *cond,
2374
 
                                     Item *field,
2375
 
                                     Item *value)
 
9478
 
 
9479
static void
 
9480
change_cond_ref_to_const(Session *session, I_List<COND_CMP> *save_list,
 
9481
                         Item *and_father, Item *cond,
 
9482
                         Item *field, Item *value)
2376
9483
{
2377
9484
  if (cond->type() == Item::COND_ITEM)
2378
9485
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9486
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9487
      Item_func::COND_AND_FUNC;
 
9488
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9489
    Item *item;
2382
9490
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9491
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
 
9492
                               field, value);
2385
9493
    return;
2386
9494
  }
2387
9495
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9508
       left_item->collation.collation == value->collation.collation))
2401
9509
  {
2402
9510
    Item *tmp=value->clone_item();
 
9511
    tmp->collation.set(right_item->collation);
 
9512
 
2403
9513
    if (tmp)
2404
9514
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
9515
      session->change_item_tree(args + 1, tmp);
2407
9516
      func->update_used_tables();
2408
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2409
 
                and_father != cond && 
2410
 
          ! left_item->const_item())
 
9517
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9518
          && and_father != cond && !left_item->const_item())
2411
9519
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9520
        cond->marker=1;
 
9521
        COND_CMP *tmp2;
 
9522
        if ((tmp2=new COND_CMP(and_father,func)))
 
9523
          save_list->push_back(tmp2);
2414
9524
      }
2415
9525
      func->set_cmp_func();
2416
9526
    }
2422
9532
            right_item->collation.collation == value->collation.collation))
2423
9533
  {
2424
9534
    Item *tmp= value->clone_item();
 
9535
    tmp->collation.set(left_item->collation);
 
9536
 
2425
9537
    if (tmp)
2426
9538
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
9539
      session->change_item_tree(args, tmp);
2429
9540
      value= tmp;
2430
9541
      func->update_used_tables();
2431
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2432
 
          and_father != cond && 
2433
 
          ! right_item->const_item())
 
9542
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9543
          && and_father != cond && !right_item->const_item())
2434
9544
      {
2435
9545
        args[0]= args[1];                       // For easy check
2436
9546
        session->change_item_tree(args + 1, value);
2437
 
        cond->marker=1;
2438
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9547
        cond->marker=1;
 
9548
        COND_CMP *tmp2;
 
9549
        if ((tmp2=new COND_CMP(and_father,func)))
 
9550
          save_list->push_back(tmp2);
2439
9551
      }
2440
9552
      func->set_cmp_func();
2441
9553
    }
2450
9562
  @return
2451
9563
    new conditions
2452
9564
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9565
 
 
9566
static Item *remove_additional_cond(Item* conds)
2454
9567
{
2455
9568
  if (conds->name == in_additional_cond)
2456
9569
    return 0;
2457
9570
  if (conds->type() == Item::COND_ITEM)
2458
9571
  {
2459
9572
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9573
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9574
    Item *item;
2462
9575
    while ((item= li++))
2463
9576
    {
2473
9586
  return conds;
2474
9587
}
2475
9588
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9589
static void
 
9590
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
 
9591
                         COND *and_father, COND *cond)
2480
9592
{
2481
9593
  if (cond->type() == Item::COND_ITEM)
2482
9594
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9595
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9596
      Item_func::COND_AND_FUNC;
 
9597
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9598
    Item *item;
2486
 
    list<COND_CMP> save;
 
9599
    I_List<COND_CMP> save;
2487
9600
    while ((item=li++))
2488
9601
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9602
      propagate_cond_constants(session, &save,and_level ? cond : item, item);
2490
9603
    }
2491
9604
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9605
    {                                           // Handle other found items
 
9606
      I_List_iterator<COND_CMP> cond_itr(save);
 
9607
      COND_CMP *cond_cmp;
 
9608
      while ((cond_cmp=cond_itr++))
2495
9609
      {
2496
 
        Item **args= iter->second->arguments();
2497
 
        if (not args[0]->const_item())
2498
 
        {
2499
 
          change_cond_ref_to_const(session, save, iter->first,
2500
 
                                   iter->first, args[0], args[1] );
2501
 
        }
 
9610
        Item **args= cond_cmp->cmp_func->arguments();
 
9611
        if (!args[0]->const_item())
 
9612
          change_cond_ref_to_const(session, &save,cond_cmp->and_level,
 
9613
                                   cond_cmp->and_level, args[0], args[1]);
2502
9614
      }
2503
9615
    }
2504
9616
  }
2505
9617
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9618
  {
2507
9619
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9620
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9621
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9622
    {
2511
9623
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9624
      Item **args= func->arguments();
2515
9627
      if (!(left_const && right_const) &&
2516
9628
          args[0]->result_type() == args[1]->result_type())
2517
9629
      {
2518
 
        if (right_const)
2519
 
        {
2520
 
                resolve_const_item(session, &args[1], args[0]);
2521
 
          func->update_used_tables();
2522
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2523
 
                                        args[0], args[1]);
2524
 
        }
2525
 
        else if (left_const)
2526
 
        {
2527
 
                resolve_const_item(session, &args[0], args[1]);
2528
 
          func->update_used_tables();
2529
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2530
 
                                        args[1], args[0]);
2531
 
        }
2532
 
      }
2533
 
    }
2534
 
  }
2535
 
}
 
9630
        if (right_const)
 
9631
        {
 
9632
          resolve_const_item(session, &args[1], args[0]);
 
9633
          func->update_used_tables();
 
9634
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9635
                                   args[0], args[1]);
 
9636
        }
 
9637
        else if (left_const)
 
9638
        {
 
9639
          resolve_const_item(session, &args[0], args[1]);
 
9640
          func->update_used_tables();
 
9641
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9642
                                   args[1], args[0]);
 
9643
        }
 
9644
      }
 
9645
    }
 
9646
  }
 
9647
}
 
9648
 
 
9649
 
 
9650
/**
 
9651
  Simplify joins replacing outer joins by inner joins whenever it's
 
9652
  possible.
 
9653
 
 
9654
    The function, during a retrieval of join_list,  eliminates those
 
9655
    outer joins that can be converted into inner join, possibly nested.
 
9656
    It also moves the on expressions for the converted outer joins
 
9657
    and from inner joins to conds.
 
9658
    The function also calculates some attributes for nested joins:
 
9659
    - used_tables
 
9660
    - not_null_tables
 
9661
    - dep_tables.
 
9662
    - on_expr_dep_tables
 
9663
    The first two attributes are used to test whether an outer join can
 
9664
    be substituted for an inner join. The third attribute represents the
 
9665
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9666
    on table t1, then in any evaluated execution plan table access to
 
9667
    table t2 must precede access to table t2. This relation is used also
 
9668
    to check whether the query contains  invalid cross-references.
 
9669
    The forth attribute is an auxiliary one and is used to calculate
 
9670
    dep_tables.
 
9671
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9672
    execution plan, the dependencies required by the straight join
 
9673
    modifiers are reflected in this attribute as well.
 
9674
    The function also removes all braces that can be removed from the join
 
9675
    expression without changing its meaning.
 
9676
 
 
9677
  @note
 
9678
    An outer join can be replaced by an inner join if the where condition
 
9679
    or the on expression for an embedding nested join contains a conjunctive
 
9680
    predicate rejecting null values for some attribute of the inner tables.
 
9681
 
 
9682
    E.g. in the query:
 
9683
    @code
 
9684
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9685
    @endcode
 
9686
    the predicate t2.b < 5 rejects nulls.
 
9687
    The query is converted first to:
 
9688
    @code
 
9689
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9690
    @endcode
 
9691
    then to the equivalent form:
 
9692
    @code
 
9693
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9694
    @endcode
 
9695
 
 
9696
 
 
9697
    Similarly the following query:
 
9698
    @code
 
9699
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9700
        WHERE t2.c < 5
 
9701
    @endcode
 
9702
    is converted to:
 
9703
    @code
 
9704
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
 
9705
 
 
9706
    @endcode
 
9707
 
 
9708
    One conversion might trigger another:
 
9709
    @code
 
9710
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9711
                       LEFT JOIN t3 ON t3.b=t2.b
 
9712
        WHERE t3 IS NOT NULL =>
 
9713
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9714
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
 
9715
      SELECT * FROM t1, t2, t3
 
9716
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9717
  @endcode
 
9718
 
 
9719
    The function removes all unnecessary braces from the expression
 
9720
    produced by the conversions.
 
9721
    E.g.
 
9722
    @code
 
9723
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9724
    @endcode
 
9725
    finally is converted to:
 
9726
    @code
 
9727
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9728
 
 
9729
    @endcode
 
9730
 
 
9731
 
 
9732
    It also will remove braces from the following queries:
 
9733
    @code
 
9734
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9735
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9736
    @endcode
 
9737
 
 
9738
    The benefit of this simplification procedure is that it might return
 
9739
    a query for which the optimizer can evaluate execution plan with more
 
9740
    join orders. With a left join operation the optimizer does not
 
9741
    consider any plan where one of the inner tables is before some of outer
 
9742
    tables.
 
9743
 
 
9744
  IMPLEMENTATION
 
9745
    The function is implemented by a recursive procedure.  On the recursive
 
9746
    ascent all attributes are calculated, all outer joins that can be
 
9747
    converted are replaced and then all unnecessary braces are removed.
 
9748
    As join list contains join tables in the reverse order sequential
 
9749
    elimination of outer joins does not require extra recursive calls.
 
9750
 
 
9751
  SEMI-JOIN NOTES
 
9752
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9753
    an "ancestor" semi-join nest)
 
9754
 
 
9755
  EXAMPLES
 
9756
    Here is an example of a join query with invalid cross references:
 
9757
    @code
 
9758
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
 
9759
    @endcode
 
9760
 
 
9761
  @param join        reference to the query info
 
9762
  @param join_list   list representation of the join to be converted
 
9763
  @param conds       conditions to add on expressions for converted joins
 
9764
  @param top         true <=> conds is the where condition
 
9765
 
 
9766
  @return
 
9767
    - The new condition, if success
 
9768
    - 0, otherwise
 
9769
*/
 
9770
 
 
9771
static COND *
 
9772
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9773
               bool in_sj)
 
9774
{
 
9775
  TableList *table;
 
9776
  nested_join_st *nested_join;
 
9777
  TableList *prev_table= 0;
 
9778
  List_iterator<TableList> li(*join_list);
 
9779
 
 
9780
  /*
 
9781
    Try to simplify join operations from join_list.
 
9782
    The most outer join operation is checked for conversion first.
 
9783
  */
 
9784
  while ((table= li++))
 
9785
  {
 
9786
    table_map used_tables;
 
9787
    table_map not_null_tables= (table_map) 0;
 
9788
 
 
9789
    if ((nested_join= table->nested_join))
 
9790
    {
 
9791
      /*
 
9792
         If the element of join_list is a nested join apply
 
9793
         the procedure to its nested join list first.
 
9794
      */
 
9795
      if (table->on_expr)
 
9796
      {
 
9797
        Item *expr= table->on_expr;
 
9798
        /*
 
9799
           If an on expression E is attached to the table,
 
9800
           check all null rejected predicates in this expression.
 
9801
           If such a predicate over an attribute belonging to
 
9802
           an inner table of an embedded outer join is found,
 
9803
           the outer join is converted to an inner join and
 
9804
           the corresponding on expression is added to E.
 
9805
        */
 
9806
        expr= simplify_joins(join, &nested_join->join_list,
 
9807
                             expr, false, in_sj || table->sj_on_expr);
 
9808
 
 
9809
        if (!table->prep_on_expr || expr != table->on_expr)
 
9810
        {
 
9811
          assert(expr);
 
9812
 
 
9813
          table->on_expr= expr;
 
9814
          table->prep_on_expr= expr->copy_andor_structure(join->session);
 
9815
        }
 
9816
      }
 
9817
      nested_join->used_tables= (table_map) 0;
 
9818
      nested_join->not_null_tables=(table_map) 0;
 
9819
      conds= simplify_joins(join, &nested_join->join_list, conds, top,
 
9820
                            in_sj || table->sj_on_expr);
 
9821
      used_tables= nested_join->used_tables;
 
9822
      not_null_tables= nested_join->not_null_tables;
 
9823
    }
 
9824
    else
 
9825
    {
 
9826
      if (!table->prep_on_expr)
 
9827
        table->prep_on_expr= table->on_expr;
 
9828
      used_tables= table->table->map;
 
9829
      if (conds)
 
9830
        not_null_tables= conds->not_null_tables();
 
9831
    }
 
9832
 
 
9833
    if (table->embedding)
 
9834
    {
 
9835
      table->embedding->nested_join->used_tables|= used_tables;
 
9836
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9837
    }
 
9838
 
 
9839
    if (!table->outer_join || (used_tables & not_null_tables))
 
9840
    {
 
9841
      /*
 
9842
        For some of the inner tables there are conjunctive predicates
 
9843
        that reject nulls => the outer join can be replaced by an inner join.
 
9844
      */
 
9845
      table->outer_join= 0;
 
9846
      if (table->on_expr)
 
9847
      {
 
9848
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9849
        if (conds)
 
9850
        {
 
9851
          conds= and_conds(conds, table->on_expr);
 
9852
          conds->top_level_item();
 
9853
          /* conds is always a new item as both cond and on_expr existed */
 
9854
          assert(!conds->fixed);
 
9855
          conds->fix_fields(join->session, &conds);
 
9856
        }
 
9857
        else
 
9858
          conds= table->on_expr;
 
9859
        table->prep_on_expr= table->on_expr= 0;
 
9860
      }
 
9861
    }
 
9862
 
 
9863
    if (!top)
 
9864
      continue;
 
9865
 
 
9866
    /*
 
9867
      Only inner tables of non-convertible outer joins
 
9868
      remain with on_expr.
 
9869
    */
 
9870
    if (table->on_expr)
 
9871
    {
 
9872
      table->dep_tables|= table->on_expr->used_tables();
 
9873
      if (table->embedding)
 
9874
      {
 
9875
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
 
9876
        /*
 
9877
           Embedding table depends on tables used
 
9878
           in embedded on expressions.
 
9879
        */
 
9880
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9881
      }
 
9882
      else
 
9883
        table->dep_tables&= ~table->table->map;
 
9884
    }
 
9885
 
 
9886
    if (prev_table)
 
9887
    {
 
9888
      /* The order of tables is reverse: prev_table follows table */
 
9889
      if (prev_table->straight)
 
9890
        prev_table->dep_tables|= used_tables;
 
9891
      if (prev_table->on_expr)
 
9892
      {
 
9893
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9894
        table_map prev_used_tables= prev_table->nested_join ?
 
9895
                                    prev_table->nested_join->used_tables :
 
9896
                                    prev_table->table->map;
 
9897
        /*
 
9898
          If on expression contains only references to inner tables
 
9899
          we still make the inner tables dependent on the outer tables.
 
9900
          It would be enough to set dependency only on one outer table
 
9901
          for them. Yet this is really a rare case.
 
9902
        */
 
9903
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9904
          prev_table->dep_tables|= used_tables;
 
9905
      }
 
9906
    }
 
9907
    prev_table= table;
 
9908
  }
 
9909
 
 
9910
  /*
 
9911
    Flatten nested joins that can be flattened.
 
9912
    no ON expression and not a semi-join => can be flattened.
 
9913
  */
 
9914
  li.rewind();
 
9915
  while ((table= li++))
 
9916
  {
 
9917
    nested_join= table->nested_join;
 
9918
    if (table->sj_on_expr && !in_sj)
 
9919
    {
 
9920
       /*
 
9921
         If this is a semi-join that is not contained within another semi-join,
 
9922
         leave it intact (otherwise it is flattened)
 
9923
       */
 
9924
      join->select_lex->sj_nests.push_back(table);
 
9925
    }
 
9926
    else if (nested_join && !table->on_expr)
 
9927
    {
 
9928
      TableList *tbl;
 
9929
      List_iterator<TableList> it(nested_join->join_list);
 
9930
      while ((tbl= it++))
 
9931
      {
 
9932
        tbl->embedding= table->embedding;
 
9933
        tbl->join_list= table->join_list;
 
9934
      }
 
9935
      li.replace(nested_join->join_list);
 
9936
    }
 
9937
  }
 
9938
  return(conds);
 
9939
}
 
9940
 
 
9941
 
 
9942
/**
 
9943
  Assign each nested join structure a bit in nested_join_map.
 
9944
 
 
9945
    Assign each nested join structure (except "confluent" ones - those that
 
9946
    embed only one element) a bit in nested_join_map.
 
9947
 
 
9948
  @param join          Join being processed
 
9949
  @param join_list     List of tables
 
9950
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9951
                       call
 
9952
 
 
9953
  @note
 
9954
    This function is called after simplify_joins(), when there are no
 
9955
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9956
    we will not run out of bits in nested_join_map.
 
9957
 
 
9958
  @return
 
9959
    First unused bit in nested_join_map after the call.
 
9960
*/
 
9961
 
 
9962
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
9963
                                          uint32_t first_unused)
 
9964
{
 
9965
  List_iterator<TableList> li(*join_list);
 
9966
  TableList *table;
 
9967
  while ((table= li++))
 
9968
  {
 
9969
    nested_join_st *nested_join;
 
9970
    if ((nested_join= table->nested_join))
 
9971
    {
 
9972
      /*
 
9973
        It is guaranteed by simplify_joins() function that a nested join
 
9974
        that has only one child is either
 
9975
         - a single-table view (the child is the underlying table), or
 
9976
         - a single-table semi-join nest
 
9977
 
 
9978
        We don't assign bits to such sj-nests because
 
9979
        1. it is redundant (a "sequence" of one table cannot be interleaved
 
9980
            with anything)
 
9981
        2. we could run out bits in nested_join_map otherwise.
 
9982
      */
 
9983
      if (nested_join->join_list.elements != 1)
 
9984
      {
 
9985
        /* Don't assign bits to sj-nests */
 
9986
        if (table->on_expr)
 
9987
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9988
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9989
                                                    first_unused);
 
9990
      }
 
9991
    }
 
9992
  }
 
9993
  return(first_unused);
 
9994
}
 
9995
 
 
9996
 
 
9997
/**
 
9998
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
9999
 
 
10000
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
10001
    the passed join_list.
 
10002
 
 
10003
  @param join_list  List of nested joins to process. It may also contain base
 
10004
                    tables which will be ignored.
 
10005
*/
 
10006
 
 
10007
static void reset_nj_counters(List<TableList> *join_list)
 
10008
{
 
10009
  List_iterator<TableList> li(*join_list);
 
10010
  TableList *table;
 
10011
  while ((table= li++))
 
10012
  {
 
10013
    nested_join_st *nested_join;
 
10014
    if ((nested_join= table->nested_join))
 
10015
    {
 
10016
      nested_join->counter_= 0;
 
10017
      reset_nj_counters(&nested_join->join_list);
 
10018
    }
 
10019
  }
 
10020
  return;
 
10021
}
 
10022
 
2536
10023
 
2537
10024
/**
2538
10025
  Check interleaving with an inner tables of an outer join for
2610
10097
         position:
2611
10098
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10099
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10100
          2. {each nested_join_st structure not simplified away}->counter - number
2614
10101
             of this nested join's children that have already been added to to
2615
10102
             the partial join order.
2616
10103
  @endverbatim
2617
10104
 
2618
10105
  @param join       Join being processed
 
10106
  @param last_tab   Last table in current partial join order (this function is
 
10107
                    not called for empty partial join orders)
2619
10108
  @param next_tab   Table we're going to extend the current partial join with
2620
10109
 
2621
10110
  @retval
2624
10113
  @retval
2625
10114
    true   Requested join order extension not allowed.
2626
10115
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10116
 
 
10117
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10118
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10119
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10120
  JOIN *join= last_tab->join;
2631
10121
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10122
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10123
  {
2634
10124
    /*
2635
10125
      next_tab is outside of the "pair of brackets" we're currently in.
2642
10132
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10133
    X,Y,Z in the above picture)
2644
10134
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10135
  for (;next_emb; next_emb= next_emb->embedding)
2646
10136
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10137
    next_emb->nested_join->counter_++;
 
10138
    if (next_emb->nested_join->counter_ == 1)
2649
10139
    {
2650
10140
      /*
2651
10141
        next_emb is the first table inside a nested join we've "entered". In
2652
10142
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10143
        X bracket might have Y pair bracket.
2654
10144
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10145
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10146
    }
2657
10147
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10148
    if (next_emb->nested_join->join_list.elements !=
 
10149
        next_emb->nested_join->counter_)
2660
10150
      break;
2661
10151
 
2662
10152
    /*
2663
10153
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10154
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10155
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10156
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10157
  }
2668
10158
  return false;
2669
10159
}
2670
10160
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
 
10161
 
 
10162
/**
 
10163
  Nested joins perspective: Remove the last table from the join order.
 
10164
 
 
10165
    Remove the last table from the partial join order and update the nested
 
10166
    joins counters and join->cur_embedding_map. It is ok to call this
 
10167
    function for the first table in join order (for which
 
10168
    check_interleaving_with_nj has not been called)
 
10169
 
 
10170
  @param last  join table to remove, it is assumed to be the last in current
 
10171
               partial join order.
 
10172
*/
 
10173
 
 
10174
static void restore_prev_nj_state(JOIN_TAB *last)
 
10175
{
 
10176
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10177
  JOIN *join= last->join;
 
10178
  while (last_emb)
 
10179
  {
 
10180
    if (last_emb->on_expr)
 
10181
    {
 
10182
      if (!(--last_emb->nested_join->counter_))
 
10183
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10184
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10185
               last_emb->nested_join->counter_)
 
10186
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10187
      else
 
10188
        break;
 
10189
    }
 
10190
    last_emb= last_emb->embedding;
 
10191
  }
 
10192
}
 
10193
 
 
10194
 
 
10195
 
 
10196
static
 
10197
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10198
{
 
10199
  TableList *emb_sj_nest;
 
10200
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10201
  {
 
10202
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10203
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10204
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10205
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10206
  }
 
10207
}
 
10208
 
 
10209
 
 
10210
/*
 
10211
  we assume remaining_tables doesnt contain @tab.
 
10212
*/
 
10213
 
 
10214
static void restore_prev_sj_state(const table_map remaining_tables,
 
10215
                                  const JOIN_TAB *tab)
 
10216
{
 
10217
  TableList *emb_sj_nest;
 
10218
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10219
  {
 
10220
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10221
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) ==
 
10222
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10223
    {
 
10224
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10225
    }
 
10226
  }
 
10227
}
 
10228
 
 
10229
 
 
10230
static COND *
 
10231
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10232
              Item::cond_result *cond_value)
2672
10233
{
2673
10234
  Session *session= join->session;
2674
10235
 
2688
10249
                             &join->cond_equal);
2689
10250
 
2690
10251
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10252
    propagate_cond_constants(session, (I_List<COND_CMP> *) 0, conds, conds);
2693
10253
    /*
2694
10254
      Remove all instances of item == item
2695
10255
      Remove all and-levels where CONST item != CONST item
2699
10259
  return(conds);
2700
10260
}
2701
10261
 
 
10262
 
2702
10263
/**
2703
10264
  Remove const and eq items.
2704
10265
 
2709
10270
    - COND_TRUE   : always true ( 1 = 1 )
2710
10271
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10272
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10273
 
 
10274
COND *
 
10275
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
2713
10276
{
2714
10277
  if (cond->type() == Item::COND_ITEM)
2715
10278
  {
2716
10279
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
10280
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10281
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10282
    Item::cond_result tmp_cond_value;
2720
10283
    bool should_fix_fields= false;
2721
10284
 
2755
10318
          }
2756
10319
          break;
2757
10320
        case Item::COND_UNDEF:                  /* Impossible */
2758
 
          break;
 
10321
          break; /* purecov: deadcode */
2759
10322
      }
2760
10323
    }
2761
10324
 
2769
10332
    {                                           
2770
10333
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
2771
10334
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10335
      ((Item_cond*) cond)->argument_list()->empty();
2773
10336
      return item;
2774
10337
    }
2775
10338
  }
2791
10354
    {
2792
10355
      Field *field= ((Item_field*) args[0])->field;
2793
10356
      if (field->flags & AUTO_INCREMENT_FLAG 
2794
 
          && ! field->getTable()->maybe_null 
 
10357
          && ! field->table->maybe_null 
2795
10358
          && session->options & OPTION_AUTO_IS_NULL
2796
10359
          && (
2797
10360
            session->first_successful_insert_id_in_prev_stmt > 0 
2847
10410
  }
2848
10411
  else if (cond->const_item() && !cond->is_expensive())
2849
10412
  /*
2850
 
    @todo
 
10413
    TODO:
2851
10414
    Excluding all expensive functions is too restritive we should exclude only
2852
 
    materialized IN subquery predicates because they can't yet be evaluated
2853
 
    here (they need additional initialization that is done later on).
2854
 
 
2855
 
    The proper way to exclude the subqueries would be to walk the cond tree and
2856
 
    check for materialized subqueries there.
2857
 
 
 
10415
    materialized IN because it is created later than this phase, and cannot be
 
10416
    evaluated at this point.
 
10417
    The condition should be something as (need to fix member access):
 
10418
      !(cond->type() == Item::FUNC_ITEM &&
 
10419
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10420
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10421
  */
2859
10422
  {
2860
10423
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2898
10461
    true    can be used
2899
10462
    false   cannot be used
2900
10463
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10464
static bool
 
10465
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10466
{
2903
10467
  return r->const_item() &&
2904
10468
    /* elements must be compared as dates */
2913
10477
/**
2914
10478
  Return true if the item is a const value in all the WHERE clause.
2915
10479
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10480
 
 
10481
static bool
 
10482
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10483
{
2918
10484
  if (cond->type() == Item::COND_ITEM)
2919
10485
  {
2920
10486
    bool and_level= (((Item_cond*) cond)->functype()
2921
10487
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10488
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10489
    Item *item;
2924
10490
    while ((item=li++))
2925
10491
    {
2926
10492
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10493
      if (res)                                  // Is a const value
2928
10494
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10495
        if (and_level)
 
10496
          return 1;
2931
10497
      }
2932
10498
      else if (!and_level)
2933
 
        return 0;
 
10499
        return 0;
2934
10500
    }
2935
10501
    return and_level ? 0 : 1;
2936
10502
  }
2938
10504
  {                                             // boolan compare function
2939
10505
    Item_func* func= (Item_func*) cond;
2940
10506
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10507
        func->functype() != Item_func::EQ_FUNC)
2942
10508
      return 0;
2943
10509
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10510
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10512
    {
2947
10513
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10514
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10515
        if (*const_item)
 
10516
          return right_item->eq(*const_item, 1);
 
10517
        *const_item=right_item;
 
10518
        return 1;
2953
10519
      }
2954
10520
    }
2955
10521
    else if (right_item->eq(comp_item,1))
2956
10522
    {
2957
10523
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10524
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
 
10525
        if (*const_item)
 
10526
          return left_item->eq(*const_item, 1);
 
10527
        *const_item=left_item;
 
10528
        return 1;
2963
10529
      }
2964
10530
    }
2965
10531
  }
2966
10532
  return 0;
2967
10533
}
2968
10534
 
 
10535
 
2969
10536
/**
2970
10537
  @details
2971
10538
  Rows produced by a join sweep may end up in a temporary table or be sent
2977
10544
  @return
2978
10545
    end_select function to use. This function can't fail.
2979
10546
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
10547
 
 
10548
Next_select_func setup_end_select_func(JOIN *join)
2981
10549
{
2982
10550
  Table *table= join->tmp_table;
2983
10551
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
2989
10557
    if (table->group && tmp_tbl->sum_func_count &&
2990
10558
        !tmp_tbl->precomputed_group_by)
2991
10559
    {
2992
 
      if (table->getShare()->sizeKeys())
 
10560
      if (table->s->keys)
2993
10561
      {
2994
 
        end_select= end_update;
 
10562
        end_select=end_update;
2995
10563
      }
2996
10564
      else
2997
10565
      {
2998
 
        end_select= end_unique_update;
 
10566
        end_select=end_unique_update;
2999
10567
      }
3000
10568
    }
3001
10569
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
10570
    {
3003
 
      end_select= end_write_group;
 
10571
      end_select=end_write_group;
3004
10572
    }
3005
10573
    else
3006
10574
    {
3007
 
      end_select= end_write;
 
10575
      end_select=end_write;
3008
10576
      if (tmp_tbl->precomputed_group_by)
3009
10577
      {
3010
10578
        /*
3032
10600
  return end_select;
3033
10601
}
3034
10602
 
 
10603
 
3035
10604
/**
3036
10605
  Make a join of all tables and write it on socket or to table.
3037
10606
 
3042
10611
  @retval
3043
10612
    -1  if error should be sent
3044
10613
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10614
 
 
10615
static int
 
10616
do_select(JOIN *join,List<Item> *fields,Table *table)
3046
10617
{
3047
10618
  int rc= 0;
3048
10619
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
 
10620
  JOIN_TAB *join_tab= NULL;
3050
10621
 
3051
10622
  join->tmp_table= table;                       /* Save for easy recursion */
3052
10623
  join->fields= fields;
3053
10624
 
3054
10625
  if (table)
3055
10626
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
10627
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10628
    empty_record(table);
3058
10629
    if (table->group && join->tmp_table_param.sum_func_count &&
3059
 
        table->getShare()->sizeKeys() && !table->cursor->inited)
3060
 
    {
3061
 
      int tmp_error;
3062
 
      tmp_error= table->cursor->startIndexScan(0, 0);
3063
 
      if (tmp_error != 0)
3064
 
      {
3065
 
        table->print_error(tmp_error, MYF(0));
3066
 
        return -1;
3067
 
      }
3068
 
    }
 
10630
        table->s->keys && !table->file->inited)
 
10631
      table->file->ha_index_init(0, 0);
3069
10632
  }
3070
10633
  /* Set up select_end */
3071
10634
  Next_select_func end_select= setup_end_select_func(join);
3086
10649
    {
3087
10650
      error= (*end_select)(join, 0, 0);
3088
10651
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
10652
        error= (*end_select)(join, 0, 1);
3090
10653
 
3091
10654
      /*
3092
10655
        If we don't go through evaluate_join_record(), do the counting
3124
10687
    if (!table)                                 // If sending data to client
3125
10688
    {
3126
10689
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
10690
        The following will unlock all cursors if the command wasn't an
 
10691
        update command
3129
10692
      */
3130
10693
      join->join_free();                        // Unlock all cursors
3131
10694
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
10695
        rc= 1;                                  // Don't send error
3133
10696
    }
3134
10697
  }
3135
10698
  else
3137
10700
  if (table)
3138
10701
  {
3139
10702
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10703
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
10704
    {
3142
10705
      new_errno= tmp;
3143
10706
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10707
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
10708
    {
3146
10709
      new_errno= tmp;
3147
10710
    }
3148
10711
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
10712
      table->file->print_error(new_errno,MYF(0));
3150
10713
  }
3151
10714
  return(join->session->is_error() ? -1 : rc);
3152
10715
}
3153
10716
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10717
 
 
10718
enum_nested_loop_state
 
10719
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
10720
{
3156
10721
  enum_nested_loop_state rc;
3157
10722
 
3162
10727
      rc= sub_select(join,join_tab,end_of_records);
3163
10728
    return rc;
3164
10729
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
10730
  if (join->session->killed)            // If aborted by user
3166
10731
  {
3167
10732
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
10733
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
10734
  }
3170
10735
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
10736
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
10737
    if (!store_record_in_cache(&join_tab->cache))
3173
10738
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
10739
    return flush_cached_records(join,join_tab,false);
3175
10740
  }
3298
10863
  @return
3299
10864
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
10865
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10866
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10867
 
 
10868
enum_nested_loop_state
 
10869
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
10870
{
3303
10871
  join_tab->table->null_row=0;
3304
10872
  if (end_of_records)
3306
10874
 
3307
10875
  int error;
3308
10876
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
10877
  READ_RECORD *info= &join_tab->read_record;
 
10878
 
 
10879
  if (join_tab->flush_weedout_table)
 
10880
  {
 
10881
    do_sj_reset(join_tab->flush_weedout_table);
 
10882
  }
3310
10883
 
3311
10884
  if (join->resume_nested_loop)
3312
10885
  {
3359
10932
  return rc;
3360
10933
}
3361
10934
 
3362
 
int safe_index_read(JoinTable *tab)
 
10935
 
 
10936
 
 
10937
 
 
10938
/*
 
10939
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10940
 
 
10941
  SYNPOSIS
 
10942
    do_sj_dups_weedout()
 
10943
 
 
10944
  RETURN
 
10945
    -1  Error
 
10946
    1   The row combination is a duplicate (discard it)
 
10947
    0   The row combination is not a duplicate (continue)
 
10948
*/
 
10949
 
 
10950
int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
 
10951
{
 
10952
  int error;
 
10953
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10954
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10955
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
10956
  unsigned char *nulls_ptr= ptr;
 
10957
 
 
10958
  /* Put the the rowids tuple into table->record[0]: */
 
10959
 
 
10960
  // 1. Store the length
 
10961
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10962
  {
 
10963
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10964
    ptr++;
 
10965
  }
 
10966
  else
 
10967
  {
 
10968
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10969
    ptr += 2;
 
10970
  }
 
10971
 
 
10972
  // 2. Zero the null bytes
 
10973
  if (sjtbl->null_bytes)
 
10974
  {
 
10975
    memset(ptr, 0, sjtbl->null_bytes);
 
10976
    ptr += sjtbl->null_bytes;
 
10977
  }
 
10978
 
 
10979
  // 3. Put the rowids
 
10980
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10981
  {
 
10982
    handler *h= tab->join_tab->table->file;
 
10983
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10984
    {
 
10985
      /* It's a NULL-complemented row */
 
10986
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10987
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10988
    }
 
10989
    else
 
10990
    {
 
10991
      /* Copy the rowid value */
 
10992
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10993
        h->position(tab->join_tab->table->record[0]);
 
10994
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10995
    }
 
10996
  }
 
10997
 
 
10998
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10999
  if (error)
 
11000
  {
 
11001
    /* create_myisam_from_heap will generate error if needed */
 
11002
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
11003
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
 
11004
                                &sjtbl->recinfo, error, 1))
 
11005
      return -1;
 
11006
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
11007
    return 1;
 
11008
  }
 
11009
  return 0;
 
11010
}
 
11011
 
 
11012
 
 
11013
/*
 
11014
  SemiJoinDuplicateElimination: Reset the temporary table
 
11015
*/
 
11016
 
 
11017
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
11018
{
 
11019
  if (sj_tbl->tmp_table)
 
11020
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
11021
  return 0;
 
11022
}
 
11023
 
 
11024
/*
 
11025
  Process one record of the nested loop join.
 
11026
 
 
11027
    This function will evaluate parts of WHERE/ON clauses that are
 
11028
    applicable to the partial record on hand and in case of success
 
11029
    submit this record to the next level of the nested loop.
 
11030
*/
 
11031
 
 
11032
static enum_nested_loop_state
 
11033
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11034
                     int error)
 
11035
{
 
11036
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11037
  ha_rows found_records=join->found_records;
 
11038
  COND *select_cond= join_tab->select_cond;
 
11039
 
 
11040
  if (error > 0 || (join->session->is_error()))     // Fatal error
 
11041
    return NESTED_LOOP_ERROR;
 
11042
  if (error < 0)
 
11043
    return NESTED_LOOP_NO_MORE_ROWS;
 
11044
  if (join->session->killed)                    // Aborted by user
 
11045
  {
 
11046
    join->session->send_kill_message();
 
11047
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11048
  }
 
11049
  if (!select_cond || select_cond->val_int())
 
11050
  {
 
11051
    /*
 
11052
      There is no select condition or the attached pushed down
 
11053
      condition is true => a match is found.
 
11054
    */
 
11055
    bool found= 1;
 
11056
    while (join_tab->first_unmatched && found)
 
11057
    {
 
11058
      /*
 
11059
        The while condition is always false if join_tab is not
 
11060
        the last inner join table of an outer join operation.
 
11061
      */
 
11062
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11063
      /*
 
11064
        Mark that a match for current outer table is found.
 
11065
        This activates push down conditional predicates attached
 
11066
        to the all inner tables of the outer join.
 
11067
      */
 
11068
      first_unmatched->found= 1;
 
11069
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11070
      {
 
11071
        if (tab->table->reginfo.not_exists_optimize)
 
11072
          return NESTED_LOOP_NO_MORE_ROWS;
 
11073
        /* Check all predicates that has just been activated. */
 
11074
        /*
 
11075
          Actually all predicates non-guarded by first_unmatched->found
 
11076
          will be re-evaluated again. It could be fixed, but, probably,
 
11077
          it's not worth doing now.
 
11078
        */
 
11079
        if (tab->select_cond && !tab->select_cond->val_int())
 
11080
        {
 
11081
          /* The condition attached to table tab is false */
 
11082
          if (tab == join_tab)
 
11083
            found= 0;
 
11084
          else
 
11085
          {
 
11086
            /*
 
11087
              Set a return point if rejected predicate is attached
 
11088
              not to the last table of the current nest level.
 
11089
            */
 
11090
            join->return_tab= tab;
 
11091
            return NESTED_LOOP_OK;
 
11092
          }
 
11093
        }
 
11094
      }
 
11095
      /*
 
11096
        Check whether join_tab is not the last inner table
 
11097
        for another embedding outer join.
 
11098
      */
 
11099
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11100
          first_unmatched->last_inner != join_tab)
 
11101
        first_unmatched= 0;
 
11102
      join_tab->first_unmatched= first_unmatched;
 
11103
    }
 
11104
 
 
11105
    JOIN_TAB *return_tab= join->return_tab;
 
11106
    join_tab->found_match= true;
 
11107
    if (join_tab->check_weed_out_table)
 
11108
    {
 
11109
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
 
11110
      if (res == -1)
 
11111
        return NESTED_LOOP_ERROR;
 
11112
      if (res == 1)
 
11113
        return NESTED_LOOP_OK;
 
11114
    }
 
11115
    else if (join_tab->do_firstmatch)
 
11116
    {
 
11117
      /*
 
11118
        We should return to the join_tab->do_firstmatch after we have
 
11119
        enumerated all the suffixes for current prefix row combination
 
11120
      */
 
11121
      return_tab= join_tab->do_firstmatch;
 
11122
    }
 
11123
 
 
11124
    /*
 
11125
      It was not just a return to lower loop level when one
 
11126
      of the newly activated predicates is evaluated as false
 
11127
      (See above join->return_tab= tab).
 
11128
    */
 
11129
    join->examined_rows++;
 
11130
    join->session->row_count++;
 
11131
 
 
11132
    if (found)
 
11133
    {
 
11134
      enum enum_nested_loop_state rc;
 
11135
      /* A match from join_tab is found for the current partial join. */
 
11136
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11137
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11138
        return rc;
 
11139
      if (return_tab < join->return_tab)
 
11140
        join->return_tab= return_tab;
 
11141
 
 
11142
      if (join->return_tab < join_tab)
 
11143
        return NESTED_LOOP_OK;
 
11144
      /*
 
11145
        Test if this was a SELECT DISTINCT query on a table that
 
11146
        was not in the field list;  In this case we can abort if
 
11147
        we found a row, as no new rows can be added to the result.
 
11148
      */
 
11149
      if (not_used_in_distinct && found_records != join->found_records)
 
11150
        return NESTED_LOOP_NO_MORE_ROWS;
 
11151
    }
 
11152
    else
 
11153
      join_tab->read_record.file->unlock_row();
 
11154
  }
 
11155
  else
 
11156
  {
 
11157
    /*
 
11158
      The condition pushed down to the table join_tab rejects all rows
 
11159
      with the beginning coinciding with the current partial join.
 
11160
    */
 
11161
    join->examined_rows++;
 
11162
    join->session->row_count++;
 
11163
    join_tab->read_record.file->unlock_row();
 
11164
  }
 
11165
  return NESTED_LOOP_OK;
 
11166
}
 
11167
 
 
11168
 
 
11169
/**
 
11170
 
 
11171
  @details
 
11172
    Construct a NULL complimented partial join record and feed it to the next
 
11173
    level of the nested loop. This function is used in case we have
 
11174
    an OUTER join and no matching record was found.
 
11175
*/
 
11176
 
 
11177
static enum_nested_loop_state
 
11178
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11179
{
 
11180
  /*
 
11181
    The table join_tab is the first inner table of a outer join operation
 
11182
    and no matches has been found for the current outer row.
 
11183
  */
 
11184
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11185
  /* Cache variables for faster loop */
 
11186
  COND *select_cond;
 
11187
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11188
  {
 
11189
    /* Change the the values of guard predicate variables. */
 
11190
    join_tab->found= 1;
 
11191
    join_tab->not_null_compl= 0;
 
11192
    /* The outer row is complemented by nulls for each inner tables */
 
11193
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11194
    mark_as_null_row(join_tab->table);       // For group by without error
 
11195
    select_cond= join_tab->select_cond;
 
11196
    /* Check all attached conditions for inner table rows. */
 
11197
    if (select_cond && !select_cond->val_int())
 
11198
      return NESTED_LOOP_OK;
 
11199
  }
 
11200
  join_tab--;
 
11201
  /*
 
11202
    The row complemented by nulls might be the first row
 
11203
    of embedding outer joins.
 
11204
    If so, perform the same actions as in the code
 
11205
    for the first regular outer join row above.
 
11206
  */
 
11207
  for ( ; ; )
 
11208
  {
 
11209
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11210
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11211
        first_unmatched->last_inner != join_tab)
 
11212
      first_unmatched= 0;
 
11213
    join_tab->first_unmatched= first_unmatched;
 
11214
    if (!first_unmatched)
 
11215
      break;
 
11216
    first_unmatched->found= 1;
 
11217
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11218
    {
 
11219
      if (tab->select_cond && !tab->select_cond->val_int())
 
11220
      {
 
11221
        join->return_tab= tab;
 
11222
        return NESTED_LOOP_OK;
 
11223
      }
 
11224
    }
 
11225
  }
 
11226
  /*
 
11227
    The row complemented by nulls satisfies all conditions
 
11228
    attached to inner tables.
 
11229
    Send the row complemented by nulls to be joined with the
 
11230
    remaining tables.
 
11231
  */
 
11232
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11233
}
 
11234
 
 
11235
 
 
11236
static enum_nested_loop_state
 
11237
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11238
{
 
11239
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11240
  int error;
 
11241
  READ_RECORD *info;
 
11242
 
 
11243
  join_tab->table->null_row= 0;
 
11244
  if (!join_tab->cache.records)
 
11245
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11246
  if (skip_last)
 
11247
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11248
  if (join_tab->use_quick == 2)
 
11249
  {
 
11250
    if (join_tab->select->quick)
 
11251
    {                                   /* Used quick select last. reset it */
 
11252
      delete join_tab->select->quick;
 
11253
      join_tab->select->quick=0;
 
11254
    }
 
11255
  }
 
11256
 /* read through all records */
 
11257
  if ((error=join_init_read_record(join_tab)))
 
11258
  {
 
11259
    reset_cache_write(&join_tab->cache);
 
11260
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11261
  }
 
11262
 
 
11263
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11264
  {
 
11265
    tmp->status=tmp->table->status;
 
11266
    tmp->table->status=0;
 
11267
  }
 
11268
 
 
11269
  info= &join_tab->read_record;
 
11270
  do
 
11271
  {
 
11272
    if (join->session->killed)
 
11273
    {
 
11274
      join->session->send_kill_message();
 
11275
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11276
    }
 
11277
    SQL_SELECT *select=join_tab->select;
 
11278
    if (rc == NESTED_LOOP_OK &&
 
11279
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11280
    {
 
11281
      uint32_t i;
 
11282
      reset_cache_read(&join_tab->cache);
 
11283
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11284
      {
 
11285
        read_cached_record(join_tab);
 
11286
        if (!select || !select->skip_record())
 
11287
        {
 
11288
          int res= 0;
 
11289
          if (!join_tab->check_weed_out_table ||
 
11290
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
 
11291
          {
 
11292
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11293
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11294
            {
 
11295
              reset_cache_write(&join_tab->cache);
 
11296
              return rc;
 
11297
            }
 
11298
          }
 
11299
          if (res == -1)
 
11300
            return NESTED_LOOP_ERROR;
 
11301
        }
 
11302
      }
 
11303
    }
 
11304
  } while (!(error=info->read_record(info)));
 
11305
 
 
11306
  if (skip_last)
 
11307
    read_cached_record(join_tab);               // Restore current record
 
11308
  reset_cache_write(&join_tab->cache);
 
11309
  if (error > 0)                                // Fatal error
 
11310
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11311
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11312
    tmp2->table->status=tmp2->status;
 
11313
  return NESTED_LOOP_OK;
 
11314
}
 
11315
 
 
11316
int safe_index_read(JOIN_TAB *tab)
3363
11317
{
3364
11318
  int error;
3365
11319
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11320
  if ((error=table->file->index_read_map(table->record[0],
3367
11321
                                         tab->ref.key_buff,
3368
11322
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
11323
                                         HA_READ_KEY_EXACT)))
3371
11325
  return 0;
3372
11326
}
3373
11327
 
 
11328
 
 
11329
static int
 
11330
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
11331
{
 
11332
  int error;
 
11333
  Table *table=tab->table;
 
11334
  table->const_table=1;
 
11335
  table->null_row=0;
 
11336
  table->status=STATUS_NO_RECORD;
 
11337
 
 
11338
  if (tab->type == JT_SYSTEM)
 
11339
  {
 
11340
    if ((error=join_read_system(tab)))
 
11341
    {                                           // Info for DESCRIBE
 
11342
      tab->info="const row not found";
 
11343
      /* Mark for EXPLAIN that the row was not found */
 
11344
      pos->records_read=0.0;
 
11345
      pos->ref_depend_map= 0;
 
11346
      if (!table->maybe_null || error > 0)
 
11347
        return(error);
 
11348
    }
 
11349
  }
 
11350
  else
 
11351
  {
 
11352
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11353
        !table->no_keyread &&
 
11354
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
11355
    {
 
11356
      table->key_read=1;
 
11357
      table->file->extra(HA_EXTRA_KEYREAD);
 
11358
      tab->index= tab->ref.key;
 
11359
    }
 
11360
    error=join_read_const(tab);
 
11361
    if (table->key_read)
 
11362
    {
 
11363
      table->key_read=0;
 
11364
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
11365
    }
 
11366
    if (error)
 
11367
    {
 
11368
      tab->info="unique row not found";
 
11369
      /* Mark for EXPLAIN that the row was not found */
 
11370
      pos->records_read=0.0;
 
11371
      pos->ref_depend_map= 0;
 
11372
      if (!table->maybe_null || error > 0)
 
11373
        return(error);
 
11374
    }
 
11375
  }
 
11376
  if (*tab->on_expr_ref && !table->null_row)
 
11377
  {
 
11378
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
11379
      mark_as_null_row(table);
 
11380
  }
 
11381
  if (!table->null_row)
 
11382
    table->maybe_null=0;
 
11383
 
 
11384
  /* Check appearance of new constant items in Item_equal objects */
 
11385
  JOIN *join= tab->join;
 
11386
  if (join->conds)
 
11387
    update_const_equal_items(join->conds, tab);
 
11388
  TableList *tbl;
 
11389
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
11390
  {
 
11391
    TableList *embedded;
 
11392
    TableList *embedding= tbl;
 
11393
    do
 
11394
    {
 
11395
      embedded= embedding;
 
11396
      if (embedded->on_expr)
 
11397
         update_const_equal_items(embedded->on_expr, tab);
 
11398
      embedding= embedded->embedding;
 
11399
    }
 
11400
    while (embedding &&
 
11401
           embedding->nested_join->join_list.head() == embedded);
 
11402
  }
 
11403
 
 
11404
  return(0);
 
11405
}
 
11406
 
 
11407
 
 
11408
static int
 
11409
join_read_system(JOIN_TAB *tab)
 
11410
{
 
11411
  Table *table= tab->table;
 
11412
  int error;
 
11413
  if (table->status & STATUS_GARBAGE)           // If first read
 
11414
  {
 
11415
    if ((error=table->file->read_first_row(table->record[0],
 
11416
                                           table->s->primary_key)))
 
11417
    {
 
11418
      if (error != HA_ERR_END_OF_FILE)
 
11419
        return table->report_error(error);
 
11420
      mark_as_null_row(tab->table);
 
11421
      empty_record(table);                      // Make empty record
 
11422
      return -1;
 
11423
    }
 
11424
    update_virtual_fields_marked_for_write(table);
 
11425
    store_record(table,record[1]);
 
11426
  }
 
11427
  else if (!table->status)                      // Only happens with left join
 
11428
    restore_record(table,record[1]);                    // restore old record
 
11429
  table->null_row=0;
 
11430
  return table->status ? -1 : 0;
 
11431
}
 
11432
 
 
11433
 
3374
11434
/**
3375
11435
  Read a (constant) table when there is at most one matching row.
3376
11436
 
3383
11443
  @retval
3384
11444
    1   Got an error (other than row not found) during read
3385
11445
*/
3386
 
int join_read_const(JoinTable *tab)
 
11446
 
 
11447
static int
 
11448
join_read_const(JOIN_TAB *tab)
3387
11449
{
3388
11450
  int error;
3389
11451
  Table *table= tab->table;
3391
11453
  {
3392
11454
    table->status= 0;
3393
11455
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11456
      error=HA_ERR_KEY_NOT_FOUND;
3395
11457
    else
3396
11458
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
11459
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
3398
11460
                                            (unsigned char*) tab->ref.key_buff,
3399
11461
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
11462
                                            HA_READ_KEY_EXACT);
3402
11464
    if (error)
3403
11465
    {
3404
11466
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
11467
      mark_as_null_row(tab->table);
 
11468
      empty_record(table);
3407
11469
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
11470
        return table->report_error(error);
3409
11471
      return -1;
3410
11472
    }
3411
 
    table->storeRecord();
 
11473
    update_virtual_fields_marked_for_write(table);
 
11474
    store_record(table,record[1]);
3412
11475
  }
3413
11476
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
11477
  {
3415
11478
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
11479
    restore_record(table,record[1]);                    // restore old record
3417
11480
  }
3418
11481
  table->null_row=0;
3419
11482
  return table->status ? -1 : 0;
3420
11483
}
3421
11484
 
 
11485
 
3422
11486
/*
3423
11487
  eq_ref access method implementation: "read_first" function
3424
11488
 
3425
11489
  SYNOPSIS
3426
11490
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
11491
      tab  JOIN_TAB of the accessed table
3428
11492
 
3429
11493
  DESCRIPTION
3430
11494
    This is "read_fist" function for the "ref" access method. The difference
3435
11499
   -1  - Row not found
3436
11500
    1  - Error
3437
11501
*/
3438
 
int join_read_key(JoinTable *tab)
 
11502
 
 
11503
static int
 
11504
join_read_key(JOIN_TAB *tab)
3439
11505
{
3440
11506
  int error;
3441
11507
  Table *table= tab->table;
3442
11508
 
3443
 
  if (!table->cursor->inited)
 
11509
  if (!table->file->inited)
3444
11510
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
11511
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
11512
  }
3451
11513
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
11514
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
11515
  if (cmp_buffer_with_ref(tab) ||
3454
11516
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
11517
  {
3458
11520
      table->status=STATUS_NOT_FOUND;
3459
11521
      return -1;
3460
11522
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11523
    error=table->file->index_read_map(table->record[0],
3462
11524
                                      tab->ref.key_buff,
3463
11525
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
11526
                                      HA_READ_KEY_EXACT);
3469
11531
  return table->status ? -1 : 0;
3470
11532
}
3471
11533
 
 
11534
 
3472
11535
/*
3473
11536
  ref access method implementation: "read_first" function
3474
11537
 
3475
11538
  SYNOPSIS
3476
11539
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
11540
      tab  JOIN_TAB of the accessed table
3478
11541
 
3479
11542
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
 
11543
    This is "read_fist" function for the "ref" access method.
3481
11544
 
3482
11545
    The functon must leave the index initialized when it returns.
3483
11546
    ref_or_null access implementation depends on that.
3487
11550
   -1  - Row not found
3488
11551
    1  - Error
3489
11552
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
11553
 
 
11554
static int
 
11555
join_read_always_key(JOIN_TAB *tab)
3491
11556
{
3492
11557
  int error;
3493
11558
  Table *table= tab->table;
3494
11559
 
3495
11560
  /* Initialize the index first */
3496
 
  if (!table->cursor->inited)
3497
 
  {
3498
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3499
 
    if (error != 0)
3500
 
      return table->report_error(error);
3501
 
  }
 
11561
  if (!table->file->inited)
 
11562
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3502
11563
 
3503
11564
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
11565
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3509
11570
 
3510
11571
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3511
11572
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11573
  if ((error=table->file->index_read_map(table->record[0],
3513
11574
                                         tab->ref.key_buff,
3514
11575
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
11576
                                         HA_READ_KEY_EXACT)))
3516
11577
  {
3517
11578
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
11579
      return table->report_error(error);
3519
 
    return -1;
 
11580
    return -1; /* purecov: inspected */
3520
11581
  }
3521
 
 
 
11582
  update_virtual_fields_marked_for_write(table);
3522
11583
  return 0;
3523
11584
}
3524
11585
 
 
11586
 
3525
11587
/**
3526
 
  This function is used when optimizing away ORDER BY in
3527
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11588
  This function is used when optimizing away order_st BY in
 
11589
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3528
11590
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
11591
 
 
11592
static int
 
11593
join_read_last_key(JOIN_TAB *tab)
3530
11594
{
3531
11595
  int error;
3532
11596
  Table *table= tab->table;
3533
11597
 
3534
 
  if (!table->cursor->inited)
3535
 
  {
3536
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3537
 
    if (error != 0)
3538
 
      return table->report_error(error);
3539
 
  }
 
11598
  if (!table->file->inited)
 
11599
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3540
11600
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3541
11601
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11602
  if ((error=table->file->index_read_last_map(table->record[0],
3543
11603
                                              tab->ref.key_buff,
3544
11604
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
11605
  {
3546
11606
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
11607
      return table->report_error(error);
3548
 
    return -1;
 
11608
    return -1; /* purecov: inspected */
3549
11609
  }
3550
11610
  return 0;
3551
11611
}
3552
11612
 
3553
 
int join_no_more_records(ReadRecord *)
 
11613
 
 
11614
        /* ARGSUSED */
 
11615
static int
 
11616
join_no_more_records(READ_RECORD *)
3554
11617
{
3555
11618
  return -1;
3556
11619
}
3557
11620
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
11621
static int
 
11622
join_read_next_same_diff(READ_RECORD *info)
3559
11623
{
3560
11624
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
11625
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
11626
  if (tab->insideout_match_tab->found_match)
3563
11627
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11628
    KEY *key= tab->table->key_info + tab->index;
3565
11629
    do
3566
11630
    {
3567
11631
      int error;
3568
11632
      /* Save index tuple from record to the buffer */
3569
11633
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
11634
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11635
      if ((error=table->file->index_next_same(table->record[0],
3572
11636
                                              tab->ref.key_buff,
3573
11637
                                              tab->ref.key_length)))
3574
11638
      {
3586
11650
    return join_read_next_same(info);
3587
11651
}
3588
11652
 
3589
 
int join_read_next_same(ReadRecord *info)
 
11653
static int
 
11654
join_read_next_same(READ_RECORD *info)
3590
11655
{
3591
11656
  int error;
3592
11657
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
11658
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
11659
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11660
  if ((error=table->file->index_next_same(table->record[0],
3596
11661
                                          tab->ref.key_buff,
3597
11662
                                          tab->ref.key_length)))
3598
11663
  {
3601
11666
    table->status= STATUS_GARBAGE;
3602
11667
    return -1;
3603
11668
  }
3604
 
 
 
11669
  update_virtual_fields_marked_for_write(table);
3605
11670
  return 0;
3606
11671
}
3607
11672
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
11673
 
 
11674
static int
 
11675
join_read_prev_same(READ_RECORD *info)
3609
11676
{
3610
11677
  int error;
3611
11678
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
11679
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
11680
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11681
  if ((error=table->file->index_prev(table->record[0])))
3615
11682
    return table->report_error(error);
3616
11683
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
11684
                      tab->ref.key_length))
3619
11686
    table->status=STATUS_NOT_FOUND;
3620
11687
    error= -1;
3621
11688
  }
 
11689
  update_virtual_fields_marked_for_write(table);
3622
11690
  return error;
3623
11691
}
3624
11692
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
11693
 
 
11694
static int
 
11695
join_init_quick_read_record(JOIN_TAB *tab)
3626
11696
{
3627
11697
  if (test_if_quick_select(tab) == -1)
3628
11698
    return -1;                                  /* No possible records */
3629
11699
  return join_init_read_record(tab);
3630
11700
}
3631
11701
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
11702
 
 
11703
int rr_sequential(READ_RECORD *info);
 
11704
int init_read_record_seq(JOIN_TAB *tab)
3633
11705
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
11706
  tab->read_record.read_record= rr_sequential;
 
11707
  if (tab->read_record.file->ha_rnd_init(1))
3637
11708
    return 1;
3638
11709
  return (*tab->read_record.read_record)(&tab->read_record);
3639
11710
}
3640
11711
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
11712
static int
 
11713
test_if_quick_select(JOIN_TAB *tab)
3642
11714
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
11715
  delete tab->select->quick;
 
11716
  tab->select->quick=0;
3645
11717
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11718
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11719
                                        false);
3647
11720
}
3648
11721
 
3649
 
int join_init_read_record(JoinTable *tab)
 
11722
 
 
11723
static int
 
11724
join_init_read_record(JOIN_TAB *tab)
3650
11725
{
3651
11726
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
11727
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
11728
  init_read_record(&tab->read_record, tab->join->session, tab->table,
 
11729
                   tab->select,1,1);
3657
11730
  return (*tab->read_record.read_record)(&tab->read_record);
3658
11731
}
3659
11732
 
3660
 
int join_read_first(JoinTable *tab)
 
11733
 
 
11734
static int
 
11735
join_read_first(JOIN_TAB *tab)
3661
11736
{
3662
11737
  int error;
3663
11738
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11739
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
11740
      !table->no_keyread)
3666
11741
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11742
    table->key_read=1;
 
11743
    table->file->extra(HA_EXTRA_KEYREAD);
3669
11744
  }
3670
 
  tab->table->status= 0;
 
11745
  tab->table->status=0;
3671
11746
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
11747
  tab->read_record.file=table->file;
3673
11748
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
11749
  tab->read_record.record=table->record[0];
3675
11750
  if (tab->insideout_match_tab)
3676
11751
  {
3677
11752
    tab->read_record.do_insideout_scan= tab;
3684
11759
    tab->read_record.do_insideout_scan= 0;
3685
11760
  }
3686
11761
 
3687
 
  if (!table->cursor->inited)
3688
 
  {
3689
 
    error= table->cursor->startIndexScan(tab->index, tab->sorted);
3690
 
    if (error != 0)
3691
 
    {
3692
 
      table->report_error(error);
3693
 
      return -1;
3694
 
    }
3695
 
  }
3696
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
11762
  if (!table->file->inited)
 
11763
    table->file->ha_index_init(tab->index, tab->sorted);
 
11764
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
11765
  {
3698
11766
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
11767
      table->report_error(error);
3700
11768
    return -1;
3701
11769
  }
3702
 
 
 
11770
  if (not error)
 
11771
    update_virtual_fields_marked_for_write(tab->table);
3703
11772
  return 0;
3704
11773
}
3705
11774
 
3706
 
int join_read_next_different(ReadRecord *info)
 
11775
 
 
11776
static int
 
11777
join_read_next_different(READ_RECORD *info)
3707
11778
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
11779
  JOIN_TAB *tab= info->do_insideout_scan;
3709
11780
  if (tab->insideout_match_tab->found_match)
3710
11781
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11782
    KEY *key= tab->table->key_info + tab->index;
3712
11783
    do
3713
11784
    {
3714
11785
      int error;
3715
11786
      /* Save index tuple from record to the buffer */
3716
11787
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
11788
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
 
11789
      if ((error=info->file->index_next(info->record)))
3719
11790
        return info->table->report_error(error);
 
11791
      if (not error)
 
11792
        update_virtual_fields_marked_for_write(tab->table);
3720
11793
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3721
11794
                      tab->insideout_buf, key->key_length));
3722
11795
    tab->insideout_match_tab->found_match= 0;
3726
11799
    return join_read_next(info);
3727
11800
}
3728
11801
 
3729
 
int join_read_next(ReadRecord *info)
 
11802
 
 
11803
static int
 
11804
join_read_next(READ_RECORD *info)
3730
11805
{
3731
11806
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
 
11807
  if ((error=info->file->index_next(info->record)))
3733
11808
    return info->table->report_error(error);
 
11809
  if (not error)
 
11810
    update_virtual_fields_marked_for_write(info->table);
3734
11811
  return 0;
3735
11812
}
3736
11813
 
3737
 
int join_read_last(JoinTable *tab)
 
11814
 
 
11815
static int
 
11816
join_read_last(JOIN_TAB *tab)
3738
11817
{
3739
11818
  Table *table=tab->table;
3740
11819
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11820
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
11821
      !table->no_keyread)
3743
11822
  {
3744
11823
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11824
    table->file->extra(HA_EXTRA_KEYREAD);
3746
11825
  }
3747
11826
  tab->table->status=0;
3748
11827
  tab->read_record.read_record=join_read_prev;
3749
11828
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
11829
  tab->read_record.file=table->file;
3751
11830
  tab->read_record.index=tab->index;
3752
 
  tab->read_record.record=table->getInsertRecord();
3753
 
  if (!table->cursor->inited)
3754
 
  {
3755
 
    error= table->cursor->startIndexScan(tab->index, 1);
3756
 
    if (error != 0)
3757
 
      return table->report_error(error);
3758
 
  }
3759
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
 
11831
  tab->read_record.record=table->record[0];
 
11832
  if (!table->file->inited)
 
11833
    table->file->ha_index_init(tab->index, 1);
 
11834
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3760
11835
    return table->report_error(error);
3761
 
 
 
11836
  if (not error)
 
11837
    update_virtual_fields_marked_for_write(tab->table);
3762
11838
  return 0;
3763
11839
}
3764
11840
 
3765
 
int join_read_prev(ReadRecord *info)
 
11841
 
 
11842
static int
 
11843
join_read_prev(READ_RECORD *info)
3766
11844
{
3767
11845
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
 
11846
  if ((error= info->file->index_prev(info->record)))
3769
11847
    return info->table->report_error(error);
3770
 
 
 
11848
  if (not error)
 
11849
    update_virtual_fields_marked_for_write(info->table);
3771
11850
  return 0;
3772
11851
}
3773
11852
 
3774
11853
/**
3775
11854
  Reading of key with key reference and one part that may be NULL.
3776
11855
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
11856
 
 
11857
int
 
11858
join_read_always_key_or_null(JOIN_TAB *tab)
3778
11859
{
3779
11860
  int res;
3780
11861
 
3788
11869
  return safe_index_read(tab);
3789
11870
}
3790
11871
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
11872
 
 
11873
int
 
11874
join_read_next_same_or_null(READ_RECORD *info)
3792
11875
{
3793
11876
  int error;
3794
11877
  if ((error= join_read_next_same(info)) >= 0)
3795
11878
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11879
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
11880
 
3798
11881
  /* Test if we have already done a read after null key */
3799
11882
  if (*tab->ref.null_ref_key)
3802
11885
  return safe_index_read(tab);                  // then read null keys
3803
11886
}
3804
11887
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11888
 
 
11889
/*****************************************************************************
 
11890
  DESCRIPTION
 
11891
    Functions that end one nested loop iteration. Different functions
 
11892
    are used to support GROUP BY clause and to redirect records
 
11893
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11894
    network client.
 
11895
 
 
11896
  RETURN VALUES
 
11897
    NESTED_LOOP_OK           - the record has been successfully handled
 
11898
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11899
                               was detected
 
11900
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11901
                               the record
 
11902
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11903
                               additionally, the nested loop produced the
 
11904
                               number of rows specified in the LIMIT clause
 
11905
                               for the query
 
11906
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11907
                               additionally, there is a cursor and the nested
 
11908
                               loop algorithm produced the number of rows
 
11909
                               that is specified for current cursor fetch
 
11910
                               operation.
 
11911
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11912
*****************************************************************************/
 
11913
 
 
11914
/* ARGSUSED */
 
11915
static enum_nested_loop_state
 
11916
end_send(JOIN *join, JOIN_TAB *,
 
11917
         bool end_of_records)
 
11918
{
 
11919
  if (!end_of_records)
 
11920
  {
 
11921
    int error;
 
11922
    if (join->having && join->having->val_int() == 0)
 
11923
      return(NESTED_LOOP_OK);               // Didn't match having
 
11924
    error=0;
 
11925
    if (join->do_send_rows)
 
11926
      error=join->result->send_data(*join->fields);
 
11927
    if (error)
 
11928
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11929
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11930
        join->do_send_rows)
 
11931
    {
 
11932
      if (join->select_options & OPTION_FOUND_ROWS)
 
11933
      {
 
11934
        JOIN_TAB *jt=join->join_tab;
 
11935
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11936
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11937
            !(jt->select && jt->select->quick) &&
 
11938
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11939
            (jt->ref.key < 0))
 
11940
        {
 
11941
          /* Join over all rows in table;  Return number of found rows */
 
11942
          Table *table=jt->table;
 
11943
 
 
11944
          join->select_options ^= OPTION_FOUND_ROWS;
 
11945
          if (table->sort.record_pointers ||
 
11946
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11947
          {
 
11948
            /* Using filesort */
 
11949
            join->send_records= table->sort.found_records;
 
11950
          }
 
11951
          else
 
11952
          {
 
11953
            table->file->info(HA_STATUS_VARIABLE);
 
11954
            join->send_records= table->file->stats.records;
 
11955
          }
 
11956
        }
 
11957
        else
 
11958
        {
 
11959
          join->do_send_rows= 0;
 
11960
          if (join->unit->fake_select_lex)
 
11961
            join->unit->fake_select_lex->select_limit= 0;
 
11962
          return(NESTED_LOOP_OK);
 
11963
        }
 
11964
      }
 
11965
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11966
    }
 
11967
    else if (join->send_records >= join->fetch_limit)
 
11968
    {
 
11969
      /*
 
11970
        There is a server side cursor and all rows for
 
11971
        this fetch request are sent.
 
11972
      */
 
11973
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11974
    }
 
11975
  }
 
11976
 
 
11977
  return(NESTED_LOOP_OK);
 
11978
}
 
11979
 
 
11980
 
 
11981
/* ARGSUSED */
 
11982
enum_nested_loop_state
 
11983
end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
3806
11984
{
3807
11985
  int idx= -1;
3808
11986
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3815
11993
    {
3816
11994
      if (idx < (int) join->send_group_parts)
3817
11995
      {
3818
 
        int error=0;
3819
 
        {
3820
 
          if (!join->first_record)
3821
 
          {
3822
 
                  List<Item>::iterator it(join->fields->begin());
3823
 
                  Item *item;
3824
 
            /* No matching rows for group function */
3825
 
            join->clear();
 
11996
        int error=0;
 
11997
        {
 
11998
          if (!join->first_record)
 
11999
          {
 
12000
            List_iterator_fast<Item> it(*join->fields);
 
12001
            Item *item;
 
12002
            /* No matching rows for group function */
 
12003
            join->clear();
3826
12004
 
3827
12005
            while ((item= it++))
3828
12006
              item->no_rows_in_result();
3829
 
          }
3830
 
          if (join->having && join->having->val_int() == 0)
3831
 
            error= -1;                          // Didn't satisfy having
3832
 
          else
3833
 
          {
3834
 
            if (join->do_send_rows)
3835
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
3836
 
            join->send_records++;
3837
 
          }
3838
 
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
3839
 
          {
3840
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
3841
 
              error= 1;
3842
 
          }
3843
 
        }
3844
 
        if (error > 0)
3845
 
          return(NESTED_LOOP_ERROR);
3846
 
        if (end_of_records)
3847
 
          return(NESTED_LOOP_OK);
3848
 
        if (join->send_records >= join->unit->select_limit_cnt &&
3849
 
            join->do_send_rows)
3850
 
        {
3851
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
3852
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
3853
 
          join->do_send_rows=0;
3854
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
12007
          }
 
12008
          if (join->having && join->having->val_int() == 0)
 
12009
            error= -1;                          // Didn't satisfy having
 
12010
          else
 
12011
          {
 
12012
            if (join->do_send_rows)
 
12013
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
12014
            join->send_records++;
 
12015
          }
 
12016
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
12017
          {
 
12018
            if (join->rollup_send_data((uint32_t) (idx+1)))
 
12019
              error= 1;
 
12020
          }
 
12021
        }
 
12022
        if (error > 0)
 
12023
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
12024
        if (end_of_records)
 
12025
          return(NESTED_LOOP_OK);
 
12026
        if (join->send_records >= join->unit->select_limit_cnt &&
 
12027
            join->do_send_rows)
 
12028
        {
 
12029
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
12030
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
12031
          join->do_send_rows=0;
 
12032
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
12033
        }
3856
12034
        else if (join->send_records >= join->fetch_limit)
3857
12035
        {
3870
12048
    else
3871
12049
    {
3872
12050
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
12051
        return(NESTED_LOOP_OK);
3874
12052
      join->first_record=1;
3875
12053
      test_if_item_cache_changed(join->group_fields);
3876
12054
    }
3882
12060
      */
3883
12061
      copy_fields(&join->tmp_table_param);
3884
12062
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
12063
        return(NESTED_LOOP_ERROR);
3886
12064
      return(ok_code);
3887
12065
    }
3888
12066
  }
3891
12069
  return(NESTED_LOOP_OK);
3892
12070
}
3893
12071
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12072
 
 
12073
/* ARGSUSED */
 
12074
enum_nested_loop_state
 
12075
end_write(JOIN *join, JOIN_TAB *,
 
12076
          bool end_of_records)
 
12077
{
 
12078
  Table *table=join->tmp_table;
 
12079
 
 
12080
  if (join->session->killed)                    // Aborted by user
 
12081
  {
 
12082
    join->session->send_kill_message();
 
12083
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12084
  }
 
12085
  if (!end_of_records)
 
12086
  {
 
12087
    copy_fields(&join->tmp_table_param);
 
12088
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12089
    if (!join->having || join->having->val_int())
 
12090
    {
 
12091
      int error;
 
12092
      join->found_records++;
 
12093
      if ((error=table->file->ha_write_row(table->record[0])))
 
12094
      {
 
12095
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12096
          goto end;
 
12097
        if (create_myisam_from_heap(join->session, table,
 
12098
                                    join->tmp_table_param.start_recinfo,
 
12099
                                    &join->tmp_table_param.recinfo,
 
12100
                                    error, 1))
 
12101
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12102
        table->s->uniques=0;                    // To ensure rows are the same
 
12103
      }
 
12104
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12105
          join->do_send_rows)
 
12106
      {
 
12107
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12108
          return(NESTED_LOOP_QUERY_LIMIT);
 
12109
        join->do_send_rows=0;
 
12110
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12111
        return(NESTED_LOOP_OK);
 
12112
      }
 
12113
    }
 
12114
  }
 
12115
end:
 
12116
  return(NESTED_LOOP_OK);
 
12117
}
 
12118
 
 
12119
/* ARGSUSED */
 
12120
/** Group by searching after group record and updating it if possible. */
 
12121
 
 
12122
static enum_nested_loop_state
 
12123
end_update(JOIN *join, JOIN_TAB *,
 
12124
           bool end_of_records)
 
12125
{
 
12126
  Table *table=join->tmp_table;
 
12127
  order_st   *group;
 
12128
  int     error;
 
12129
 
 
12130
  if (end_of_records)
 
12131
    return(NESTED_LOOP_OK);
 
12132
  if (join->session->killed)                    // Aborted by user
 
12133
  {
 
12134
    join->session->send_kill_message();
 
12135
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12136
  }
 
12137
 
 
12138
  join->found_records++;
 
12139
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12140
  /* Make a key of group index */
 
12141
  for (group=table->group ; group ; group=group->next)
 
12142
  {
 
12143
    Item *item= *group->item;
 
12144
    item->save_org_in_field(group->field);
 
12145
    /* Store in the used key if the field was 0 */
 
12146
    if (item->maybe_null)
 
12147
      group->buff[-1]= (char) group->field->is_null();
 
12148
  }
 
12149
  if (!table->file->index_read_map(table->record[1],
 
12150
                                   join->tmp_table_param.group_buff,
 
12151
                                   HA_WHOLE_KEY,
 
12152
                                   HA_READ_KEY_EXACT))
 
12153
  {                                             /* Update old record */
 
12154
    restore_record(table,record[1]);
 
12155
    update_tmptable_sum_func(join->sum_funcs,table);
 
12156
    if ((error=table->file->ha_update_row(table->record[1],
 
12157
                                          table->record[0])))
 
12158
    {
 
12159
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12160
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12161
    }
 
12162
    return(NESTED_LOOP_OK);
 
12163
  }
 
12164
 
 
12165
  /*
 
12166
    Copy null bits from group key to table
 
12167
    We can't copy all data as the key may have different format
 
12168
    as the row data (for example as with VARCHAR keys)
 
12169
  */
 
12170
  KEY_PART_INFO *key_part;
 
12171
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12172
       group ;
 
12173
       group=group->next,key_part++)
 
12174
  {
 
12175
    if (key_part->null_bit)
 
12176
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12177
  }
 
12178
  init_tmptable_sum_functions(join->sum_funcs);
 
12179
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12180
  if ((error=table->file->ha_write_row(table->record[0])))
 
12181
  {
 
12182
    if (create_myisam_from_heap(join->session, table,
 
12183
                                join->tmp_table_param.start_recinfo,
 
12184
                                &join->tmp_table_param.recinfo,
 
12185
                                error, 0))
 
12186
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12187
    /* Change method to update rows */
 
12188
    table->file->ha_index_init(0, 0);
 
12189
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12190
  }
 
12191
  join->send_records++;
 
12192
  return(NESTED_LOOP_OK);
 
12193
}
 
12194
 
 
12195
 
 
12196
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12197
 
 
12198
static enum_nested_loop_state
 
12199
end_unique_update(JOIN *join, JOIN_TAB *,
 
12200
                  bool end_of_records)
 
12201
{
 
12202
  Table *table=join->tmp_table;
 
12203
  int     error;
 
12204
 
 
12205
  if (end_of_records)
 
12206
    return(NESTED_LOOP_OK);
 
12207
  if (join->session->killed)                    // Aborted by user
 
12208
  {
 
12209
    join->session->send_kill_message();
 
12210
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12211
  }
 
12212
 
 
12213
  init_tmptable_sum_functions(join->sum_funcs);
 
12214
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12215
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12216
 
 
12217
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12218
    join->send_records++;                       // New group
 
12219
  else
 
12220
  {
 
12221
    if ((int) table->file->get_dup_key(error) < 0)
 
12222
    {
 
12223
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12224
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12225
    }
 
12226
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12227
    {
 
12228
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12229
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12230
    }
 
12231
    restore_record(table,record[1]);
 
12232
    update_tmptable_sum_func(join->sum_funcs,table);
 
12233
    if ((error=table->file->ha_update_row(table->record[1],
 
12234
                                          table->record[0])))
 
12235
    {
 
12236
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12237
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12238
    }
 
12239
  }
 
12240
  return(NESTED_LOOP_OK);
 
12241
}
 
12242
 
 
12243
 
 
12244
/* ARGSUSED */
 
12245
enum_nested_loop_state
 
12246
end_write_group(JOIN *join, JOIN_TAB *,
 
12247
                bool end_of_records)
3895
12248
{
3896
12249
  Table *table=join->tmp_table;
3897
12250
  int     idx= -1;
3898
12251
 
3899
 
  if (join->session->getKilled())
 
12252
  if (join->session->killed)
3900
12253
  {                                             // Aborted by user
3901
12254
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
12255
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
12256
  }
3904
12257
  if (!join->first_record || end_of_records ||
3905
12258
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
12262
      int send_group_parts= join->send_group_parts;
3910
12263
      if (idx < send_group_parts)
3911
12264
      {
3912
 
        if (!join->first_record)
3913
 
        {
3914
 
          /* No matching rows for group function */
3915
 
          join->clear();
3916
 
        }
3917
 
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
3918
 
        if (!join->having || join->having->val_int())
3919
 
        {
3920
 
          int error= table->cursor->insertRecord(table->getInsertRecord());
3921
 
 
3922
 
          if (error)
3923
 
          {
3924
 
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3925
 
            return NESTED_LOOP_ERROR;
3926
 
          }
3927
 
        }
3928
 
        if (join->rollup.getState() != Rollup::STATE_NONE)
3929
 
        {
3930
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3931
 
            return NESTED_LOOP_ERROR;
3932
 
        }
3933
 
        if (end_of_records)
3934
 
          return NESTED_LOOP_OK;
 
12265
        if (!join->first_record)
 
12266
        {
 
12267
          /* No matching rows for group function */
 
12268
          join->clear();
 
12269
        }
 
12270
        copy_sum_funcs(join->sum_funcs,
 
12271
                       join->sum_funcs_end[send_group_parts]);
 
12272
        if (!join->having || join->having->val_int())
 
12273
        {
 
12274
          int error= table->file->ha_write_row(table->record[0]);
 
12275
          if (error && create_myisam_from_heap(join->session, table,
 
12276
                                               join->tmp_table_param.start_recinfo,
 
12277
                                                &join->tmp_table_param.recinfo,
 
12278
                                               error, 0))
 
12279
            return(NESTED_LOOP_ERROR);
 
12280
        }
 
12281
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
12282
        {
 
12283
          if (join->rollup_write_data((uint32_t) (idx+1), table))
 
12284
            return(NESTED_LOOP_ERROR);
 
12285
        }
 
12286
        if (end_of_records)
 
12287
          return(NESTED_LOOP_OK);
3935
12288
      }
3936
12289
    }
3937
12290
    else
3938
12291
    {
3939
12292
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
12293
        return(NESTED_LOOP_OK);
3941
12294
      join->first_record=1;
3942
12295
      test_if_item_cache_changed(join->group_fields);
3943
12296
    }
3944
12297
    if (idx < (int) join->send_group_parts)
3945
12298
    {
3946
12299
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
12300
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
12301
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
12302
        return(NESTED_LOOP_ERROR);
 
12303
      return(NESTED_LOOP_OK);
3952
12304
    }
3953
12305
  }
3954
12306
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
12307
    return(NESTED_LOOP_ERROR);
 
12308
  return(NESTED_LOOP_OK);
3957
12309
}
3958
12310
 
 
12311
 
3959
12312
/*****************************************************************************
3960
12313
  Remove calculation with tables that aren't yet read. Remove also tests
3961
12314
  against fields that are read through key where the table is not a
3962
12315
  outer join table.
3963
12316
  We can't remove tests that are made against columns which are stored
3964
12317
  in sorted order.
 
12318
*****************************************************************************/
 
12319
 
 
12320
/**
3965
12321
  @return
3966
 
    1 if right_item used is a removable reference key on left_item
3967
 
    0 otherwise.
3968
 
****************************************************************************/
3969
 
bool test_if_ref(Item_field *left_item,Item *right_item)
 
12322
    1 if right_item is used removable reference key on left_item
 
12323
*/
 
12324
 
 
12325
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
12326
{
3971
12327
  Field *field=left_item->field;
3972
12328
  // No need to change const test. We also have to keep tests on LEFT JOIN
3973
 
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
 
12329
  if (!field->table->const_table && !field->table->maybe_null)
3974
12330
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12331
    Item *ref_item=part_of_refkey(field->table,field);
3976
12332
    if (ref_item && ref_item->eq(right_item,1))
3977
12333
    {
3978
12334
      right_item= right_item->real_item();
3979
12335
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12336
        return (field->eq_def(((Item_field *) right_item)->field));
3981
12337
      /* remove equalities injected by IN->EXISTS transformation */
3982
12338
      else if (right_item->type() == Item::CACHE_ITEM)
3983
12339
        return ((Item_cache *)right_item)->eq_def (field);
3984
12340
      if (right_item->const_item() && !(right_item->is_null()))
3985
12341
      {
3986
 
        /*
3987
 
          We can remove binary fields and numerical fields except float,
3988
 
          as float comparison isn't 100 % secure
3989
 
          We have to keep normal strings to be able to check for end spaces
 
12342
        /*
 
12343
          We can remove binary fields and numerical fields except float,
 
12344
          as float comparison isn't 100 % secure
 
12345
          We have to keep normal strings to be able to check for end spaces
3990
12346
 
3991
 
                sergefp: the above seems to be too restrictive. Counterexample:
3992
 
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
3993
 
                  insert into t100 values ('a'),('a ');
3994
 
                  explain select * from t100 where v='a';
3995
 
                The EXPLAIN shows 'using Where'. Running the query returns both
3996
 
                rows, so it seems there are no problems with endspace in the most
3997
 
                frequent case?
3998
 
        */
3999
 
        if (field->binary() &&
4000
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4001
 
            field->decimals() == 0)
4002
 
        {
4003
 
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4004
 
        }
 
12347
          sergefp: the above seems to be too restrictive. Counterexample:
 
12348
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12349
            insert into t100 values ('a'),('a ');
 
12350
            explain select * from t100 where v='a';
 
12351
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12352
          rows, so it seems there are no problems with endspace in the most
 
12353
          frequent case?
 
12354
        */
 
12355
        if (field->binary() &&
 
12356
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12357
            field->decimals() == 0)
 
12358
        {
 
12359
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12360
        }
4005
12361
      }
4006
12362
    }
4007
12363
  }
4008
 
  return 0;
 
12364
  return 0;                                     // keep test
 
12365
}
 
12366
 
 
12367
/**
 
12368
   @brief Replaces an expression destructively inside the expression tree of
 
12369
   the WHERE clase.
 
12370
 
 
12371
   @note Because of current requirements for semijoin flattening, we do not
 
12372
   need to recurse here, hence this function will only examine the top-level
 
12373
   AND conditions. (see JOIN::prepare, comment above the line
 
12374
   'if (do_materialize)'
 
12375
 
 
12376
   @param join The top-level query.
 
12377
   @param old_cond The expression to be replaced.
 
12378
   @param new_cond The expression to be substituted.
 
12379
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
 
12380
   the new expression.
 
12381
   @return <code>true</code> if there was an error, <code>false</code> if
 
12382
   successful.
 
12383
*/
 
12384
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
12385
                                       Item *new_cond, bool do_fix_fields)
 
12386
{
 
12387
  if (join->conds == old_cond) {
 
12388
    join->conds= new_cond;
 
12389
    if (do_fix_fields)
 
12390
      new_cond->fix_fields(join->session, &join->conds);
 
12391
    return false;
 
12392
  }
 
12393
 
 
12394
  if (join->conds->type() == Item::COND_ITEM) {
 
12395
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12396
    Item *item;
 
12397
    while ((item= li++))
 
12398
      if (item == old_cond)
 
12399
      {
 
12400
        li.replace(new_cond);
 
12401
        if (do_fix_fields)
 
12402
          new_cond->fix_fields(join->session, li.ref());
 
12403
        return false;
 
12404
      }
 
12405
  }
 
12406
 
 
12407
  return true;
4009
12408
}
4010
12409
 
4011
12410
/*
4039
12438
  RETURN
4040
12439
    Extracted condition
4041
12440
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12441
 
 
12442
static COND *
 
12443
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12444
                    bool exclude_expensive_cond)
4043
12445
{
4044
12446
  if (used_table && !(cond->used_tables() & used_table) &&
4045
 
    /*
4046
 
      Exclude constant conditions not checked at optimization time if
4047
 
      the table we are pushing conditions to is the first one.
4048
 
      As a result, such conditions are not considered as already checked
4049
 
      and will be checked at execution time, attached to the first table.
4050
 
    */
4051
 
    !((used_table & 1) && cond->is_expensive()))
 
12447
      /*
 
12448
        Exclude constant conditions not checked at optimization time if
 
12449
        the table we are pushing conditions to is the first one.
 
12450
        As a result, such conditions are not considered as already checked
 
12451
        and will be checked at execution time, attached to the first table.
 
12452
      */
 
12453
      !((used_table & 1) && cond->is_expensive()))
4052
12454
    return (COND*) 0;                           // Already checked
4053
12455
  if (cond->type() == Item::COND_ITEM)
4054
12456
  {
4057
12459
      /* Create new top level AND item */
4058
12460
      Item_cond_and *new_cond=new Item_cond_and;
4059
12461
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12462
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12463
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
12464
      Item *item;
4063
12465
      while ((item=li++))
4064
12466
      {
4065
 
        Item *fix= make_cond_for_table(item,tables,used_table,
4066
 
                                            exclude_expensive_cond);
4067
 
        if (fix)
4068
 
          new_cond->argument_list()->push_back(fix);
 
12467
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12468
                                      exclude_expensive_cond);
 
12469
        if (fix)
 
12470
          new_cond->argument_list()->push_back(fix);
4069
12471
      }
4070
 
      switch (new_cond->argument_list()->elements) 
4071
 
      {
4072
 
        case 0:
4073
 
          return (COND*) 0;                     // Always true
4074
 
        case 1:
4075
 
          return new_cond->argument_list()->head();
4076
 
        default:
4077
 
          /*
4078
 
            Item_cond_and do not need fix_fields for execution, its parameters
4079
 
            are fixed or do not need fix_fields, too
4080
 
          */
4081
 
          new_cond->quick_fix_field();
4082
 
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4083
 
          return new_cond;
 
12472
      switch (new_cond->argument_list()->elements) {
 
12473
      case 0:
 
12474
        return (COND*) 0;                       // Always true
 
12475
      case 1:
 
12476
        return new_cond->argument_list()->head();
 
12477
      default:
 
12478
        /*
 
12479
          Item_cond_and do not need fix_fields for execution, its parameters
 
12480
          are fixed or do not need fix_fields, too
 
12481
        */
 
12482
        new_cond->quick_fix_field();
 
12483
        new_cond->used_tables_cache=
 
12484
          ((Item_cond_and*) cond)->used_tables_cache &
 
12485
          tables;
 
12486
        return new_cond;
4084
12487
      }
4085
12488
    }
4086
12489
    else
4087
12490
    {                                           // Or list
4088
12491
      Item_cond_or *new_cond=new Item_cond_or;
4089
12492
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12493
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12494
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
12495
      Item *item;
4093
12496
      while ((item=li++))
4094
12497
      {
4095
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4096
 
        if (!fix)
4097
 
          return (COND*) 0;                     // Always true
4098
 
        new_cond->argument_list()->push_back(fix);
 
12498
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12499
        if (!fix)
 
12500
          return (COND*) 0;                     // Always true
 
12501
        new_cond->argument_list()->push_back(fix);
4099
12502
      }
4100
12503
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
12504
        Item_cond_and do not need fix_fields for execution, its parameters
 
12505
        are fixed or do not need fix_fields, too
4103
12506
      */
4104
12507
      new_cond->quick_fix_field();
4105
12508
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4132
12535
  {
4133
12536
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
12537
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4135
 
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
 
12538
    if (left_item->type() == Item::FIELD_ITEM &&
 
12539
        test_if_ref((Item_field*) left_item,right_item))
4136
12540
    {
4137
12541
      cond->marker=3;                   // Checked when read
4138
12542
      return (COND*) 0;
4139
12543
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12544
    if (right_item->type() == Item::FIELD_ITEM &&
 
12545
        test_if_ref((Item_field*) right_item,left_item))
4141
12546
    {
4142
12547
      cond->marker=3;                   // Checked when read
4143
12548
      return (COND*) 0;
4147
12552
  return cond;
4148
12553
}
4149
12554
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
12555
 
 
12556
static Item *
 
12557
part_of_refkey(Table *table,Field *field)
4151
12558
{
4152
12559
  if (!table->reginfo.join_tab)
4153
12560
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4155
12562
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
12563
  if (ref_parts)
4157
12564
  {
4158
 
    KeyPartInfo *key_part=
 
12565
    KEY_PART_INFO *key_part=
4159
12566
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
12567
    uint32_t part;
4161
12568
 
4166
12573
    }
4167
12574
 
4168
12575
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
12576
      if (field->eq(key_part->field) &&
4171
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4172
 
          //If field can be NULL, we should not remove this predicate, as
4173
 
          //it may lead to non-rejection of NULL values. 
4174
 
          !(field->real_maybe_null()))
4175
 
      {
 
12577
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
12578
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
12579
  }
4180
12580
  return (Item*) 0;
4181
12581
}
4182
12582
 
 
12583
 
4183
12584
/**
4184
12585
  Test if one can use the key to resolve order_st BY.
4185
12586
 
4200
12601
  @retval
4201
12602
    -1   Reverse key can be used
4202
12603
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12604
 
 
12605
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12606
                                uint32_t *used_key_parts)
4204
12607
{
4205
 
  KeyPartInfo *key_part= NULL;
4206
 
  KeyPartInfo *key_part_end= NULL;
4207
 
  key_part= table->key_info[idx].key_part;
4208
 
  key_part_end= key_part + table->key_info[idx].key_parts;
 
12608
  KEY_PART_INFO *key_part,*key_part_end;
 
12609
  key_part=table->key_info[idx].key_part;
 
12610
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
12611
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
12612
  int reverse=0;
4211
12613
  bool on_primary_key= false;
4212
12614
 
4213
12615
  for (; order ; order=order->next, const_key_parts>>=1)
4217
12619
 
4218
12620
    /*
4219
12621
      Skip key parts that are constants in the WHERE clause.
4220
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12622
      These are already skipped in the order_st BY by const_expression_in_where()
4221
12623
    */
4222
12624
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
12625
      key_part++;
4230
12632
        the primary key as a suffix.
4231
12633
      */
4232
12634
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
12635
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12636
          table->s->primary_key != MAX_KEY)
4235
12637
      {
4236
12638
        on_primary_key= true;
4237
 
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4238
 
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4239
 
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
 
12639
        key_part= table->key_info[table->s->primary_key].key_part;
 
12640
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12641
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
12642
 
4241
12643
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
12644
          key_part++;
4264
12666
  }
4265
12667
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4266
12668
    (uint32_t) (key_part - table->key_info[idx].key_part);
4267
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
12669
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
12670
                         HA_READ_PREV))
4269
12671
    reverse= 0;                                 // Index can't be used
4270
12672
  return(reverse);
4271
12673
}
4272
12674
 
 
12675
 
4273
12676
/**
4274
12677
  Test if a second key is the subkey of the first one.
4275
12678
 
4285
12688
  @retval
4286
12689
    0   no sub key
4287
12690
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
12691
 
 
12692
inline bool
 
12693
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12694
          KEY_PART_INFO *ref_key_part_end)
4291
12695
{
4292
12696
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
12697
    if (!key_part->field->eq(ref_key_part->field))
4294
12698
      return 0;
4295
12699
  return 1;
4296
12700
}
4306
12710
    - MAX_KEY                   If we can't use other key
4307
12711
    - the number of found key   Otherwise
4308
12712
*/
4309
 
static uint32_t test_if_subkey(Order *order,
4310
 
                               Table *table,
4311
 
                               uint32_t ref,
4312
 
                               uint32_t ref_key_parts,
4313
 
                               const key_map *usable_keys)
 
12713
 
 
12714
static uint
 
12715
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12716
               const key_map *usable_keys)
4314
12717
{
4315
12718
  uint32_t nr;
4316
12719
  uint32_t min_length= UINT32_MAX;
4317
12720
  uint32_t best= MAX_KEY;
4318
12721
  uint32_t not_used;
4319
 
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4320
 
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
 
12722
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12723
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
12724
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12725
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
12726
  {
4324
 
    if (usable_keys->test(nr) &&
 
12727
    if (usable_keys->is_set(nr) &&
4325
12728
        table->key_info[nr].key_length < min_length &&
4326
12729
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
12730
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
12738
  return best;
4336
12739
}
4337
12740
 
 
12741
 
4338
12742
/**
4339
12743
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
12744
  already known to be distinct.
4366
12770
  @retval
4367
12771
    0                    not found.
4368
12772
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12773
 
 
12774
static bool
 
12775
list_contains_unique_index(Table *table,
 
12776
                          bool (*find_func) (Field *, void *), void *data)
4370
12777
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12778
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
4372
12779
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12780
    if (keynr == table->s->primary_key ||
4374
12781
         (table->key_info[keynr].flags & HA_NOSAME))
4375
12782
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
12783
      KEY *keyinfo= table->key_info + keynr;
 
12784
      KEY_PART_INFO *key_part, *key_part_end;
4379
12785
 
4380
12786
      for (key_part=keyinfo->key_part,
4381
12787
           key_part_end=key_part+ keyinfo->key_parts;
4383
12789
           key_part++)
4384
12790
      {
4385
12791
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
12792
            !find_func(key_part->field, data))
4387
12793
          break;
4388
12794
      }
4389
12795
      if (key_part == key_part_end)
4393
12799
  return 0;
4394
12800
}
4395
12801
 
 
12802
 
4396
12803
/**
4397
12804
  Helper function for list_contains_unique_index.
4398
12805
  Find a field reference in a list of order_st structures.
4406
12813
  @retval
4407
12814
    0                    not found.
4408
12815
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
12816
 
 
12817
static bool
 
12818
find_field_in_order_list (Field *field, void *data)
4410
12819
{
4411
 
  Order *group= (Order *) data;
 
12820
  order_st *group= (order_st *) data;
4412
12821
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12822
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
12823
  {
4415
12824
    Item *item= (*tmp_group->item)->real_item();
4416
12825
    if (item->type() == Item::FIELD_ITEM &&
4423
12832
  return part_found;
4424
12833
}
4425
12834
 
 
12835
 
4426
12836
/**
4427
12837
  Helper function for list_contains_unique_index.
4428
12838
  Find a field reference in a dynamic list of Items.
4436
12846
  @retval
4437
12847
    0                    not found.
4438
12848
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
12849
 
 
12850
static bool
 
12851
find_field_in_item_list (Field *field, void *data)
4440
12852
{
4441
12853
  List<Item> *fields= (List<Item> *) data;
4442
12854
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
12855
  List_iterator<Item> li(*fields);
4444
12856
  Item *item;
4445
12857
 
4446
12858
  while ((item= li++))
4455
12867
  return part_found;
4456
12868
}
4457
12869
 
 
12870
 
4458
12871
/**
4459
 
  Test if we can skip the ORDER BY by using an index.
 
12872
  Test if we can skip the order_st BY by using an index.
4460
12873
 
4461
12874
  SYNOPSIS
4462
12875
    test_if_skip_sort_order()
4466
12879
      no_changes
4467
12880
      map
4468
12881
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
12882
  If we can use an index, the JOIN_TAB / tab->select struct
4470
12883
  is changed to use the index.
4471
12884
 
4472
12885
  The index must cover all fields in <order>, or it will not be considered.
4480
12893
  @retval
4481
12894
    1    We can use an index.
4482
12895
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12896
 
 
12897
static bool
 
12898
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12899
                        bool no_changes, const key_map *map)
4484
12900
{
4485
12901
  int32_t ref_key;
4486
12902
  uint32_t ref_key_parts;
4487
12903
  int order_direction;
4488
12904
  uint32_t used_key_parts;
4489
12905
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
12906
  SQL_SELECT *select=tab->select;
4491
12907
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12908
  QUICK_SELECT_I *save_quick= 0;
4493
12909
 
4494
12910
  /*
4495
12911
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4497
12913
  */
4498
12914
  usable_keys= *map;
4499
12915
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12916
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
12917
  {
4502
12918
    Item *item= (*tmp_order->item)->real_item();
4503
12919
    if (item->type() != Item::FIELD_ITEM)
4504
12920
    {
4505
 
      usable_keys.reset();
 
12921
      usable_keys.clear_all();
4506
12922
      return(0);
4507
12923
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
12924
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12925
    if (usable_keys.is_clear_all())
4510
12926
      return(0);                                        // No usable keys
4511
12927
  }
4512
12928
 
4516
12932
  {
4517
12933
    ref_key=       tab->ref.key;
4518
12934
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
12935
    if (tab->type == JT_REF_OR_NULL)
4520
12936
      return(0);
4521
12937
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12938
  else if (select && select->quick)             // Range found by opt_range
4523
12939
  {
4524
12940
    int quick_type= select->quick->get_type();
4525
12941
    save_quick= select->quick;
4526
12942
    /*
4527
12943
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
12944
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
12945
      by clustered PK values.
4530
12946
    */
4531
12947
 
4532
 
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4533
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4534
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
 
12948
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
12949
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
12950
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
12951
      return(0);
4536
12952
    ref_key=       select->quick->index;
4537
12953
    ref_key_parts= select->quick->used_key_parts;
4542
12958
    /*
4543
12959
      We come here when there is a REF key.
4544
12960
    */
4545
 
    if (! usable_keys.test(ref_key))
 
12961
    if (!usable_keys.is_set(ref_key))
4546
12962
    {
4547
12963
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
12964
        We come here when ref_key is not among usable_keys
4549
12965
      */
4550
12966
      uint32_t new_ref_key;
4551
12967
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
12968
        If using index only read, only consider other possible index only
 
12969
        keys
4554
12970
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
12971
      if (table->covering_keys.is_set(ref_key))
 
12972
        usable_keys.intersect(table->covering_keys);
4557
12973
      if (tab->pre_idx_push_select_cond)
4558
12974
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
12975
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
12976
                                       &usable_keys)) < MAX_KEY)
4561
12977
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
12978
        /* Found key that can be used to retrieve data in sorted order */
 
12979
        if (tab->ref.key >= 0)
 
12980
        {
4565
12981
          /*
4566
12982
            We'll use ref access method on key new_ref_key. In general case
4567
12983
            the index search tuple for new_ref_key will be different (e.g.
4570
12986
            "part1 = const1 AND part2=const2".
4571
12987
            So we build tab->ref from scratch here.
4572
12988
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12989
          KEYUSE *keyuse= tab->keyuse;
 
12990
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
12991
            keyuse++;
4576
12992
 
4577
12993
          if (create_ref_for_key(tab->join, tab, keyuse,
4578
12994
                                 tab->join->const_table_map))
4579
12995
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
12996
        }
 
12997
        else
 
12998
        {
4583
12999
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
13000
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
13001
            we want to use instead new_ref_key as the index. We can't
4586
13002
            just change the index of the quick select, because this may
4587
13003
            result in an incosistent QUICK_SELECT object. Below we
4589
13005
            parameres are set correctly by the range optimizer.
4590
13006
           */
4591
13007
          key_map new_ref_key_map;
4592
 
          new_ref_key_map.reset();  // Force the creation of quick select
4593
 
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
 
13008
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
13009
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
13010
 
4595
13011
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
4596
13012
                                        (tab->join->select_options &
4600
13016
                                        true) <=
4601
13017
              0)
4602
13018
            return(0);
4603
 
        }
 
13019
        }
4604
13020
        ref_key= new_ref_key;
4605
13021
      }
4606
13022
    }
4607
13023
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
13024
    if (usable_keys.is_set(ref_key) &&
4609
13025
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
13026
                                               &used_key_parts)))
4611
13027
      goto check_reverse_order;
4626
13042
    int best_key= -1;
4627
13043
    bool is_best_covering= false;
4628
13044
    double fanout= 1;
4629
 
    Join *join= tab->join;
 
13045
    JOIN *join= tab->join;
4630
13046
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
13047
    ha_rows table_records= table->file->stats.records;
4632
13048
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
13049
 
4635
13050
    /*
4636
13051
      If not used with LIMIT, only use keys if the whole query can be
4643
13058
        filesort() and join cache are usually faster than reading in
4644
13059
        index order and not using join cache
4645
13060
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13061
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
13062
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
13063
      keys= *table->file->keys_to_use_for_scanning();
 
13064
      keys.merge(table->covering_keys);
4650
13065
 
4651
13066
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
 
13067
        We are adding here also the index specified in FORCE INDEX clause,
 
13068
        if any.
4654
13069
        This is to allow users to use index in order_st BY.
4655
13070
      */
4656
13071
      if (table->force_index)
4657
 
        keys|= (group ? table->keys_in_use_for_group_by :
4658
 
                                table->keys_in_use_for_order_by);
4659
 
      keys&= usable_keys;
 
13072
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13073
                           table->keys_in_use_for_order_by);
 
13074
      keys.intersect(usable_keys);
4660
13075
    }
4661
13076
    else
4662
13077
      keys= usable_keys;
4663
13078
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
 
13079
    read_time= join->best_positions[tablenr].read_time;
4666
13080
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4667
 
    {
4668
 
      cur_pos= join->getPosFromOptimalPlan(i);
4669
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4670
 
    }
 
13081
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
13082
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13083
    for (nr=0; nr < table->s->keys ; nr++)
4673
13084
    {
4674
13085
      int direction;
4675
 
      if (keys.test(nr) &&
 
13086
      if (keys.is_set(nr) &&
4676
13087
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
13088
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
 
13089
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
4679
13090
 
4680
13091
        /*
4681
 
          Don't use an index scan with ORDER BY without limit.
 
13092
          Don't use an index scan with order_st BY without limit.
4682
13093
          For GROUP BY without limit always use index scan
4683
13094
          if there is a suitable index.
4684
13095
          Why we hold to this asymmetry hardly can be explained
4685
13096
          rationally. It's easy to demonstrate that using
4686
13097
          temporary table + filesort could be cheaper for grouping
4687
13098
          queries too.
4688
 
        */
 
13099
        */
4689
13100
        if (is_covering ||
4690
13101
            select_limit != HA_POS_ERROR ||
4691
13102
            (ref_key < 0 && (group || table->force_index)))
4692
13103
        {
4693
13104
          double rec_per_key;
4694
13105
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13106
          KEY *keyinfo= tab->table->key_info+nr;
4696
13107
          if (select_limit == HA_POS_ERROR)
4697
13108
            select_limit= table_records;
4698
13109
          if (group)
4699
13110
          {
4700
13111
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
13112
            set_if_bigger(rec_per_key, 1);
4702
13113
            /*
4703
13114
              With a grouping query each group containing on average
4704
13115
              rec_per_key records produces only one row that will
4705
13116
              be included into the result set.
4706
 
            */
 
13117
            */
4707
13118
            if (select_limit > table_records/rec_per_key)
4708
13119
                select_limit= table_records;
4709
13120
            else
4718
13129
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
13130
            and as result we'll choose an index scan when using ref/range
4720
13131
            access + filesort will be cheaper.
4721
 
          */
 
13132
          */
4722
13133
          select_limit= (ha_rows) (select_limit < fanout ?
4723
13134
                                   1 : select_limit/fanout);
4724
13135
          /*
4738
13149
                                     (double) table_records /
4739
13150
                                      table->quick_condition_rows);
4740
13151
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
13152
          set_if_bigger(rec_per_key, 1);
4742
13153
          /*
4743
13154
            Here we take into account the fact that rows are
4744
13155
            accessed in sequences rec_per_key records in each.
4745
13156
            Rows in such a sequence are supposed to be ordered
4746
13157
            by rowid/primary key. When reading the data
4747
13158
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
13159
            table file contains.
4749
13160
            TODO. Use the formula for a disk sweep sequential access
4750
13161
            to calculate the cost of accessing data rows for one
4751
13162
            index entry.
4752
 
          */
 
13163
          */
4753
13164
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
13165
                           cmin(rec_per_key, table->file->scan_time());
4755
13166
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
13167
              index_scan_time < read_time)
4757
13168
          {
4758
13169
            ha_rows quick_records= table_records;
4759
13170
            if (is_best_covering && !is_covering)
4760
13171
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
13172
            if (table->quick_keys.is_set(nr))
4762
13173
              quick_records= table->quick_rows[nr];
4763
13174
            if (best_key < 0 ||
4764
 
                (select_limit <= min(quick_records,best_records) ?
 
13175
                (select_limit <= cmin(quick_records,best_records) ?
4765
13176
                 keyinfo->key_parts < best_key_parts :
4766
13177
                 quick_records < best_records))
4767
13178
            {
4772
13183
              best_key_direction= direction;
4773
13184
            }
4774
13185
          }
4775
 
        }
 
13186
        }
4776
13187
      }
4777
13188
    }
4778
13189
    if (best_key >= 0)
4779
13190
    {
4780
13191
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13192
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
13193
      {
4783
13194
        key_map test_map;
4784
 
        test_map.reset();       // Force the creation of quick select
4785
 
        test_map.set(best_key); // only best_key.
 
13195
        test_map.clear_all();       // Force the creation of quick select
 
13196
        test_map.set_bit(best_key); // only best_key.
4786
13197
        quick_created=
4787
13198
          select->test_quick_select(join->session, test_map, 0,
4788
13199
                                    join->select_options & OPTION_FOUND_ROWS ?
4793
13204
      if (!no_changes)
4794
13205
      {
4795
13206
        if (!quick_created)
4796
 
        {
 
13207
        {
4797
13208
          tab->index= best_key;
4798
13209
          tab->read_first_record= best_key_direction > 0 ?
4799
13210
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13211
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
13212
          if (select && select->quick)
4802
13213
          {
4803
 
            safe_delete(select->quick);
 
13214
            delete select->quick;
 
13215
            select->quick= 0;
4804
13216
          }
4805
 
          if (table->covering_keys.test(best_key))
 
13217
          if (table->covering_keys.is_set(best_key))
4806
13218
          {
4807
13219
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13220
            table->file->extra(HA_EXTRA_KEYREAD);
4809
13221
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
13222
          table->file->ha_index_or_rnd_end();
4811
13223
          if (join->select_options & SELECT_DESCRIBE)
4812
13224
          {
4813
13225
            tab->ref.key= -1;
4816
13228
              tab->limit= select_limit;
4817
13229
          }
4818
13230
        }
4819
 
        else if (tab->type != AM_ALL)
 
13231
        else if (tab->type != JT_ALL)
4820
13232
        {
4821
13233
          /*
4822
13234
            We're about to use a quick access to the table.
4824
13236
            method is actually used.
4825
13237
          */
4826
13238
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
13239
          tab->type=JT_ALL;
4828
13240
          tab->use_quick=1;
4829
13241
          tab->ref.key= -1;
4830
13242
          tab->ref.key_parts=0;         // Don't use ref key.
4831
13243
          tab->read_first_record= join_init_read_record;
 
13244
          /*
 
13245
            TODO: update the number of records in join->best_positions[tablenr]
 
13246
          */
4832
13247
        }
4833
13248
      }
4834
13249
      used_key_parts= best_key_parts;
4839
13254
  }
4840
13255
 
4841
13256
check_reverse_order:
4842
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13257
  if (order_direction == -1)            // If order_st BY ... DESC
4843
13258
  {
4844
13259
    if (select && select->quick)
4845
13260
    {
4846
13261
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
13262
        Don't reverse the sort order, if it's already done.
4848
13263
        (In some cases test_if_order_by_key() can be called multiple times
4849
13264
      */
4850
 
      if (! select->quick->reverse_sorted())
 
13265
      if (!select->quick->reverse_sorted())
4851
13266
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13267
        QUICK_SELECT_DESC *tmp;
4853
13268
        bool error= false;
4854
13269
        int quick_type= select->quick->get_type();
4855
 
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4856
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
4857
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4858
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
 
13270
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13271
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13272
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13273
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4859
13274
        {
4860
13275
          tab->limit= 0;
4861
13276
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
 
13277
          return(0);                   // Use filesort
4863
13278
        }
4864
13279
 
4865
 
        /* ORDER BY range_key DESC */
4866
 
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
4867
 
                                                  used_key_parts, 
4868
 
                                                  &error);
4869
 
        if (! tmp || error)
4870
 
        {
4871
 
          delete tmp;
 
13280
        /* order_st BY range_key DESC */
 
13281
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13282
                                    used_key_parts, &error);
 
13283
        if (!tmp || error)
 
13284
        {
 
13285
          delete tmp;
4872
13286
          select->quick= save_quick;
4873
13287
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
13288
          return(0);            // Reverse sort not supported
 
13289
        }
 
13290
        select->quick=tmp;
4877
13291
      }
4878
13292
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
13293
    else if (tab->type != JT_NEXT &&
4880
13294
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
13295
    {
4882
13296
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13297
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4884
13298
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
13299
        Use a traversal function that starts by reading the last row
 
13300
        with key part (A) and then traverse the index backwards.
4887
13301
      */
4888
13302
      tab->read_first_record= join_read_last_key;
4889
13303
      tab->read_record.read_record= join_read_prev_same;
4891
13305
  }
4892
13306
  else if (select && select->quick)
4893
13307
    select->quick->sorted= 1;
4894
 
  return 1;
 
13308
  return(1);
4895
13309
}
4896
13310
 
 
13311
 
4897
13312
/*
4898
13313
  If not selecting by given key, create an index how records should be read
4899
13314
 
4900
13315
  SYNOPSIS
4901
13316
   create_sort_index()
4902
 
     session            Thread Cursor
 
13317
     session            Thread handler
4903
13318
     tab                Table to sort (in join structure)
4904
13319
     order              How table should be sorted
4905
13320
     filesort_limit     Max number of rows that needs to be sorted
4912
13327
  IMPLEMENTATION
4913
13328
   - If there is an index that can be used, 'tab' is modified to use
4914
13329
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
13330
   - If no index, create with filesort() an index file that can be used to
4916
13331
     retrieve rows in order (should be done with 'read_record').
4917
13332
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
13333
     free_io_cache(tab->table).
4919
13334
 
4920
13335
  RETURN VALUES
4921
13336
    0           ok
4922
13337
    -1          Some fatal error
4923
13338
    1           No records
4924
13339
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13340
 
 
13341
static int
 
13342
create_sort_index(Session *session, JOIN *join, order_st *order,
 
13343
                  ha_rows filesort_limit, ha_rows select_limit,
 
13344
                  bool is_order_by)
4926
13345
{
4927
13346
  uint32_t length= 0;
4928
13347
  ha_rows examined_rows;
4929
13348
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
13349
  SQL_SELECT *select;
 
13350
  JOIN_TAB *tab;
4932
13351
 
4933
13352
  if (join->tables == join->const_tables)
4934
13353
    return(0);                          // One row, no need to sort
4944
13363
  */
4945
13364
  if ((order != join->group_list ||
4946
13365
       !(join->select_options & SELECT_BIG_RESULT) ||
4947
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
 
13366
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
4948
13367
      test_if_skip_sort_order(tab,order,select_limit,0,
4949
13368
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
13369
                              &table->keys_in_use_for_group_by))
4951
13370
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13371
  for (order_st *ord= join->order; ord; ord= ord->next)
4953
13372
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
13373
  if (!(join->sortorder=
 
13374
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13375
    goto err;                           /* purecov: inspected */
4958
13376
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13377
  table->sort.io_cache= new IO_CACHE;
 
13378
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
4960
13379
  table->status=0;                              // May be wrong if quick_select
4961
13380
 
4962
13381
  // If table has a range, move it to select
4972
13391
      */
4973
13392
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
4974
13393
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13394
        table->key_read=0;
 
13395
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
13396
      }
4978
13397
    }
4979
13398
    else
4980
13399
    {
4981
13400
      /*
4982
 
        We have a ref on a const;  Change this to a range that filesort
4983
 
        can use.
4984
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
4985
 
        field, quick will contain an empty record set.
 
13401
        We have a ref on a const;  Change this to a range that filesort
 
13402
        can use.
 
13403
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13404
        field, quick will contain an empty record set.
4986
13405
      */
4987
 
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session, 
4988
 
                                                                 table, 
4989
 
                                                                 &tab->ref,
4990
 
                                                                 tab->found_records))))
4991
 
      {
4992
 
        return(-1);
4993
 
      }
 
13406
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
 
13407
                                                     tab->found_records))))
 
13408
        goto err;
4994
13409
    }
4995
13410
  }
4996
13411
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13412
  /* Fill schema tables with data before filesort if it's necessary */
 
13413
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13414
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13415
    goto err;
4999
13416
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
13417
  if (table->s->tmp_table)
 
13418
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13419
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
13420
                                     select, filesort_limit, 0,
 
13421
                                     &examined_rows);
5004
13422
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
13423
  if (select)
5006
13424
  {
5010
13428
  tab->select_cond=0;
5011
13429
  tab->last_inner= 0;
5012
13430
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13431
  tab->type=JT_ALL;                             // Read with normal read_record
5014
13432
  tab->read_first_record= join_init_read_record;
5015
13433
  tab->join->examined_rows+=examined_rows;
5016
13434
  if (table->key_read)                          // Restore if we used indexes
5017
13435
  {
5018
13436
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13437
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
13438
  }
5021
 
 
5022
13439
  return(table->sort.found_records == HA_POS_ERROR);
5023
 
}
5024
 
 
5025
 
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5026
 
{
5027
 
  Cursor *cursor=table->cursor;
 
13440
err:
 
13441
  return(-1);
 
13442
}
 
13443
 
 
13444
static bool copy_blobs(Field **ptr)
 
13445
{
 
13446
  for (; *ptr ; ptr++)
 
13447
  {
 
13448
    if ((*ptr)->flags & BLOB_FLAG)
 
13449
      if (((Field_blob *) (*ptr))->copy())
 
13450
        return 1;                               // Error
 
13451
  }
 
13452
  return 0;
 
13453
}
 
13454
 
 
13455
static void free_blobs(Field **ptr)
 
13456
{
 
13457
  for (; *ptr ; ptr++)
 
13458
  {
 
13459
    if ((*ptr)->flags & BLOB_FLAG)
 
13460
      ((Field_blob *) (*ptr))->free();
 
13461
  }
 
13462
}
 
13463
 
 
13464
 
 
13465
static int
 
13466
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13467
{
 
13468
  int error;
 
13469
  uint32_t reclength,offset;
 
13470
  uint32_t field_count;
 
13471
  Session *session= join->session;
 
13472
 
 
13473
  entry->reginfo.lock_type=TL_WRITE;
 
13474
 
 
13475
  /* Calculate how many saved fields there is in list */
 
13476
  field_count=0;
 
13477
  List_iterator<Item> it(fields);
 
13478
  Item *item;
 
13479
  while ((item=it++))
 
13480
  {
 
13481
    if (item->get_tmp_table_field() && ! item->const_item())
 
13482
      field_count++;
 
13483
  }
 
13484
 
 
13485
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
 
13486
  {                    // only const items with no OPTION_FOUND_ROWS
 
13487
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13488
    return(0);
 
13489
  }
 
13490
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13491
  offset= (field_count ?
 
13492
           entry->field[entry->s->fields - field_count]->
 
13493
           offset(entry->record[0]) : 0);
 
13494
  reclength= entry->s->reclength-offset;
 
13495
 
 
13496
  free_io_cache(entry);                         // Safety
 
13497
  entry->file->info(HA_STATUS_VARIABLE);
 
13498
  if (entry->s->db_type() == heap_hton ||
 
13499
      (!entry->s->blob_fields &&
 
13500
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13501
        session->variables.sortbuff_size)))
 
13502
    error= remove_dup_with_hash_index(join->session, entry,
 
13503
                                     field_count, first_field,
 
13504
                                     reclength, having);
 
13505
  else
 
13506
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
 
13507
                                  having);
 
13508
 
 
13509
  free_blobs(first_field);
 
13510
  return(error);
 
13511
}
 
13512
 
 
13513
 
 
13514
static int remove_dup_with_compare(Session *session, Table *table, Field **first_field,
 
13515
                                   uint32_t offset, Item *having)
 
13516
{
 
13517
  handler *file=table->file;
5028
13518
  char *org_record,*new_record;
5029
13519
  unsigned char *record;
5030
13520
  int error;
5031
 
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5032
 
 
5033
 
  org_record=(char*) (record=table->getInsertRecord())+offset;
5034
 
  new_record=(char*) table->getUpdateRecord()+offset;
5035
 
 
5036
 
  if ((error= cursor->startTableScan(1)))
5037
 
    goto err;
5038
 
 
5039
 
  error=cursor->rnd_next(record);
 
13521
  uint32_t reclength= table->s->reclength-offset;
 
13522
 
 
13523
  org_record=(char*) (record=table->record[0])+offset;
 
13524
  new_record=(char*) table->record[1]+offset;
 
13525
 
 
13526
  file->ha_rnd_init(1);
 
13527
  error=file->rnd_next(record);
5040
13528
  for (;;)
5041
13529
  {
5042
 
    if (session->getKilled())
 
13530
    if (session->killed)
5043
13531
    {
5044
13532
      session->send_kill_message();
5045
13533
      error=0;
5048
13536
    if (error)
5049
13537
    {
5050
13538
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
13539
        continue;
5052
13540
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
13541
        break;
5054
13542
      goto err;
5055
13543
    }
5056
13544
    if (having && !having->val_int())
5057
13545
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
13546
      if ((error=file->ha_delete_row(record)))
 
13547
        goto err;
 
13548
      error=file->rnd_next(record);
5061
13549
      continue;
5062
13550
    }
5063
13551
    if (copy_blobs(first_field))
5068
13556
    }
5069
13557
    memcpy(new_record,org_record,reclength);
5070
13558
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13559
    /* Read through rest of file and mark duplicated rows deleted */
5072
13560
    bool found=0;
5073
13561
    for (;;)
5074
13562
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
13563
      if ((error=file->rnd_next(record)))
5076
13564
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
13565
        if (error == HA_ERR_RECORD_DELETED)
 
13566
          continue;
 
13567
        if (error == HA_ERR_END_OF_FILE)
 
13568
          break;
 
13569
        goto err;
5082
13570
      }
5083
13571
      if (table->compare_record(first_field) == 0)
5084
13572
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
13573
        if ((error=file->ha_delete_row(record)))
 
13574
          goto err;
5087
13575
      }
5088
13576
      else if (!found)
5089
13577
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
13578
        found=1;
 
13579
        file->position(record); // Remember position
5092
13580
      }
5093
13581
    }
5094
13582
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13583
      break;                                    // End of file
 
13584
    /* Restart search on next row */
 
13585
    error=file->restart_rnd_next(record,file->ref);
5098
13586
  }
5099
13587
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13588
  file->extra(HA_EXTRA_NO_CACHE);
5101
13589
  return(0);
5102
13590
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13591
  file->extra(HA_EXTRA_NO_CACHE);
5104
13592
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
13593
    file->print_error(error,MYF(0));
5106
13594
  return(1);
5107
13595
}
5108
13596
 
 
13597
 
5109
13598
/**
5110
13599
  Generate a hash index for each row to quickly find duplicate rows.
5111
13600
 
5112
13601
  @note
5113
13602
    Note that this will not work on tables with blobs!
5114
13603
*/
5115
 
int remove_dup_with_hash_index(Session *session, 
5116
 
                               Table *table,
5117
 
                               uint32_t field_count,
5118
 
                               Field **first_field,
5119
 
                               uint32_t key_length,
5120
 
                               Item *having)
 
13604
 
 
13605
static int remove_dup_with_hash_index(Session *session, Table *table,
 
13606
                                      uint32_t field_count,
 
13607
                                      Field **first_field,
 
13608
                                      uint32_t key_length,
 
13609
                                      Item *having)
5121
13610
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13611
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
5123
13612
  int error;
5124
 
  Cursor *cursor= table->cursor;
 
13613
  handler *file= table->file;
5125
13614
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
13615
  uint32_t *field_lengths,*field_length;
5127
13616
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
13617
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
13618
  if (!my_multi_malloc(MYF(MY_WME),
 
13619
                       &key_buffer,
 
13620
                       (uint32_t) ((key_length + extra_length) *
 
13621
                               (long) file->stats.records),
 
13622
                       &field_lengths,
 
13623
                       (uint32_t) (field_count*sizeof(*field_lengths)),
 
13624
                       NULL))
 
13625
    return(1);
5133
13626
 
5134
13627
  {
5135
13628
    Field **ptr;
5136
13629
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13630
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
13631
    {
5140
13632
      uint32_t length= (*ptr)->sort_length();
5141
13633
      (*field_length++)= length;
5146
13638
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
13639
  }
5148
13640
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13641
  if (hash_init(&hash, &my_charset_bin, (uint32_t) file->stats.records, 0,
5150
13642
                key_length, (hash_get_key) 0, 0, 0))
5151
13643
  {
 
13644
    free((char*) key_buffer);
5152
13645
    return(1);
5153
13646
  }
5154
13647
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
13648
  file->ha_rnd_init(1);
 
13649
  key_pos=key_buffer;
5159
13650
  for (;;)
5160
13651
  {
5161
13652
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
13653
    if (session->killed)
5163
13654
    {
5164
13655
      session->send_kill_message();
5165
13656
      error=0;
5166
13657
      goto err;
5167
13658
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
13659
    if ((error=file->rnd_next(record)))
5169
13660
    {
5170
13661
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
13662
        continue;
5172
13663
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
13664
        break;
5174
13665
      goto err;
5175
13666
    }
5176
13667
    if (having && !having->val_int())
5177
13668
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
13669
      if ((error=file->ha_delete_row(record)))
 
13670
        goto err;
5180
13671
      continue;
5181
13672
    }
5182
13673
 
5183
13674
    /* copy fields to key buffer */
5184
13675
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
13676
    field_length=field_lengths;
5186
13677
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
13678
    {
5188
13679
      (*ptr)->sort_string(key_pos,*field_length);
5192
13683
    if (hash_search(&hash, org_key_pos, key_length))
5193
13684
    {
5194
13685
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
13686
      if ((error=file->ha_delete_row(record)))
 
13687
        goto err;
5197
13688
    }
5198
13689
    else
5199
13690
      (void) my_hash_insert(&hash, org_key_pos);
5200
13691
    key_pos+=extra_length;
5201
13692
  }
 
13693
  free((char*) key_buffer);
5202
13694
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
13695
  file->extra(HA_EXTRA_NO_CACHE);
 
13696
  (void) file->ha_rnd_end();
5205
13697
  return(0);
5206
13698
 
5207
13699
err:
 
13700
  free((char*) key_buffer);
5208
13701
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
13702
  file->extra(HA_EXTRA_NO_CACHE);
 
13703
  (void) file->ha_rnd_end();
5211
13704
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
13705
    file->print_error(error,MYF(0));
5213
13706
  return(1);
5214
13707
}
5215
13708
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13709
 
 
13710
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13711
                                  SORT_FIELD *sortorder)
5217
13712
{
5218
13713
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
13714
  SORT_FIELD *sort,*pos;
5220
13715
 
5221
13716
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13717
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5223
13718
    count++;
5224
13719
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5226
 
                                       (max(count, *length) + 1));
 
13720
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
13721
                                       (cmax(count, *length) + 1));
5227
13722
  pos= sort= sortorder;
5228
13723
 
5229
13724
  if (!pos)
5249
13744
  return(sort);
5250
13745
}
5251
13746
 
 
13747
 
 
13748
/*****************************************************************************
 
13749
  Fill join cache with packed records
 
13750
  Records are stored in tab->cache.buffer and last record in
 
13751
  last record is stored with pointers to blobs to support very big
 
13752
  records
 
13753
******************************************************************************/
 
13754
 
 
13755
static int
 
13756
join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count)
 
13757
{
 
13758
  register unsigned int i;
 
13759
  unsigned int length, blobs;
 
13760
  size_t size;
 
13761
  CACHE_FIELD *copy,**blob_ptr;
 
13762
  JOIN_CACHE  *cache;
 
13763
  JOIN_TAB *join_tab;
 
13764
 
 
13765
  cache= &tables[table_count].cache;
 
13766
  cache->fields=blobs=0;
 
13767
 
 
13768
  join_tab=tables;
 
13769
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13770
  {
 
13771
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13772
      calc_used_field_length(session, join_tab);
 
13773
    cache->fields+=join_tab->used_fields;
 
13774
    blobs+=join_tab->used_blobs;
 
13775
 
 
13776
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13777
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13778
    {
 
13779
      cache->fields++;
 
13780
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13781
    }
 
13782
  }
 
13783
  if (!(cache->field=(CACHE_FIELD*)
 
13784
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13785
 
 
13786
                  sizeof(CACHE_FIELD*))))
 
13787
  {
 
13788
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13789
    cache->buff=0;                              /* purecov: inspected */
 
13790
    return(1);                          /* purecov: inspected */
 
13791
  }
 
13792
  copy=cache->field;
 
13793
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13794
    (cache->field+cache->fields+table_count*2);
 
13795
 
 
13796
  length=0;
 
13797
  for (i=0 ; i < table_count ; i++)
 
13798
  {
 
13799
    uint32_t null_fields=0, used_fields;
 
13800
    Field **f_ptr,*field;
 
13801
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13802
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13803
         used_fields ;
 
13804
         f_ptr++)
 
13805
    {
 
13806
      field= *f_ptr;
 
13807
      if (bitmap_is_set(read_set, field->field_index))
 
13808
      {
 
13809
        used_fields--;
 
13810
        length+=field->fill_cache_field(copy);
 
13811
        if (copy->blob_field)
 
13812
          (*blob_ptr++)=copy;
 
13813
        if (field->maybe_null())
 
13814
          null_fields++;
 
13815
        copy->get_rowid= NULL;
 
13816
        copy++;
 
13817
      }
 
13818
    }
 
13819
    /* Copy null bits from table */
 
13820
    if (null_fields && tables[i].table->getNullFields())
 
13821
    {                                           /* must copy null bits */
 
13822
      copy->str= tables[i].table->null_flags;
 
13823
      copy->length= tables[i].table->s->null_bytes;
 
13824
      copy->strip=0;
 
13825
      copy->blob_field=0;
 
13826
      copy->get_rowid= NULL;
 
13827
      length+=copy->length;
 
13828
      copy++;
 
13829
      cache->fields++;
 
13830
    }
 
13831
    /* If outer join table, copy null_row flag */
 
13832
    if (tables[i].table->maybe_null)
 
13833
    {
 
13834
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13835
      copy->length=sizeof(tables[i].table->null_row);
 
13836
      copy->strip=0;
 
13837
      copy->blob_field=0;
 
13838
      copy->get_rowid= NULL;
 
13839
      length+=copy->length;
 
13840
      copy++;
 
13841
      cache->fields++;
 
13842
    }
 
13843
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13844
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13845
    {
 
13846
      copy->str= tables[i].table->file->ref;
 
13847
      copy->length= tables[i].table->file->ref_length;
 
13848
      copy->strip=0;
 
13849
      copy->blob_field=0;
 
13850
      copy->get_rowid= NULL;
 
13851
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13852
      {
 
13853
        /* We will need to call h->position(): */
 
13854
        copy->get_rowid= tables[i].table;
 
13855
        /* And those after us won't have to: */
 
13856
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13857
      }
 
13858
      copy++;
 
13859
    }
 
13860
  }
 
13861
 
 
13862
  cache->length=length+blobs*sizeof(char*);
 
13863
  cache->blobs=blobs;
 
13864
  *blob_ptr= NULL;                                      /* End sequentel */
 
13865
  size= max((size_t)session->variables.join_buff_size,
 
13866
            (size_t)cache->length);
 
13867
  if (!(cache->buff=(unsigned char*) malloc(size)))
 
13868
    return 1;                           /* Don't use cache */ /* purecov: inspected */
 
13869
  cache->end=cache->buff+size;
 
13870
  reset_cache_write(cache);
 
13871
  return 0;
 
13872
}
 
13873
 
 
13874
 
 
13875
static uint32_t used_blob_length(CACHE_FIELD **ptr)
 
13876
{
 
13877
  uint32_t length,blob_length;
 
13878
  for (length=0 ; *ptr ; ptr++)
 
13879
  {
 
13880
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13881
    length+=blob_length;
 
13882
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13883
  }
 
13884
  return length;
 
13885
}
 
13886
 
 
13887
 
 
13888
static bool
 
13889
store_record_in_cache(JOIN_CACHE *cache)
 
13890
{
 
13891
  uint32_t length;
 
13892
  unsigned char *pos;
 
13893
  CACHE_FIELD *copy,*end_field;
 
13894
  bool last_record;
 
13895
 
 
13896
  pos=cache->pos;
 
13897
  end_field=cache->field+cache->fields;
 
13898
 
 
13899
  length=cache->length;
 
13900
  if (cache->blobs)
 
13901
    length+= used_blob_length(cache->blob_ptr);
 
13902
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13903
    cache->ptr_record=cache->records;
 
13904
  /*
 
13905
    There is room in cache. Put record there
 
13906
  */
 
13907
  cache->records++;
 
13908
  for (copy=cache->field ; copy < end_field; copy++)
 
13909
  {
 
13910
    if (copy->blob_field)
 
13911
    {
 
13912
      if (last_record)
 
13913
      {
 
13914
        copy->blob_field->get_image(pos, copy->length+sizeof(char*),
 
13915
                                    copy->blob_field->charset());
 
13916
        pos+=copy->length+sizeof(char*);
 
13917
      }
 
13918
      else
 
13919
      {
 
13920
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13921
                                    copy->blob_field->charset());
 
13922
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13923
        pos+=copy->length+copy->blob_length;
 
13924
      }
 
13925
    }
 
13926
    else
 
13927
    {
 
13928
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13929
      if (copy->get_rowid)
 
13930
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13931
 
 
13932
      if (copy->strip)
 
13933
      {
 
13934
        unsigned char *str,*end;
 
13935
        for (str=copy->str,end= str+copy->length;
 
13936
             end > str && end[-1] == ' ' ;
 
13937
             end--) ;
 
13938
        length=(uint32_t) (end-str);
 
13939
        memcpy(pos+2, str, length);
 
13940
        int2store(pos, length);
 
13941
        pos+= length+2;
 
13942
      }
 
13943
      else
 
13944
      {
 
13945
        memcpy(pos,copy->str,copy->length);
 
13946
        pos+=copy->length;
 
13947
      }
 
13948
    }
 
13949
  }
 
13950
  cache->pos=pos;
 
13951
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13952
}
 
13953
 
 
13954
 
 
13955
static void
 
13956
reset_cache_read(JOIN_CACHE *cache)
 
13957
{
 
13958
  cache->record_nr=0;
 
13959
  cache->pos=cache->buff;
 
13960
}
 
13961
 
 
13962
 
 
13963
static void reset_cache_write(JOIN_CACHE *cache)
 
13964
{
 
13965
  reset_cache_read(cache);
 
13966
  cache->records= 0;
 
13967
  cache->ptr_record= UINT32_MAX;
 
13968
}
 
13969
 
 
13970
 
 
13971
static void
 
13972
read_cached_record(JOIN_TAB *tab)
 
13973
{
 
13974
  unsigned char *pos;
 
13975
  uint32_t length;
 
13976
  bool last_record;
 
13977
  CACHE_FIELD *copy,*end_field;
 
13978
 
 
13979
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13980
  pos=tab->cache.pos;
 
13981
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13982
       copy < end_field;
 
13983
       copy++)
 
13984
  {
 
13985
    if (copy->blob_field)
 
13986
    {
 
13987
      if (last_record)
 
13988
      {
 
13989
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13990
                                    copy->blob_field->charset());
 
13991
        pos+=copy->length+sizeof(char*);
 
13992
      }
 
13993
      else
 
13994
      {
 
13995
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
13996
        pos+=copy->length+copy->blob_field->get_length();
 
13997
      }
 
13998
    }
 
13999
    else
 
14000
    {
 
14001
      if (copy->strip)
 
14002
      {
 
14003
        length= uint2korr(pos);
 
14004
        memcpy(copy->str, pos+2, length);
 
14005
        memset(copy->str+length, ' ', copy->length-length);
 
14006
        pos+= 2 + length;
 
14007
      }
 
14008
      else
 
14009
      {
 
14010
        memcpy(copy->str,pos,copy->length);
 
14011
        pos+=copy->length;
 
14012
      }
 
14013
    }
 
14014
  }
 
14015
  tab->cache.pos=pos;
 
14016
  return;
 
14017
}
 
14018
 
 
14019
 
5252
14020
/*
5253
14021
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
14022
 
5267
14035
    false  The created key is the same as the previous one (and the record
5268
14036
           is already in table->record)
5269
14037
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14038
 
 
14039
static bool
 
14040
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
14041
{
5272
14042
  bool no_prev_key;
5273
14043
  if (!tab->ref.disable_cache)
5287
14057
    != 0;
5288
14058
}
5289
14059
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14060
 
 
14061
bool
 
14062
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
5291
14063
{
5292
14064
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
14065
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
5294
14066
  bool result= 0;
5295
14067
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14068
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
14069
  {
5298
14070
    if ((*copy)->copy() & 1)
5299
14071
    {
5305
14077
  return result;
5306
14078
}
5307
14079
 
 
14080
 
5308
14081
/*****************************************************************************
5309
14082
  Group and order functions
5310
14083
*****************************************************************************/
5311
14084
 
5312
14085
/**
5313
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14086
  Resolve an order_st BY or GROUP BY column reference.
5314
14087
 
5315
14088
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5316
14089
  BY clause, find the actual column it represents. If the column being
5317
14090
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
14091
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5319
 
  the ORDER BY clause, only the SELECT list is being searched.
 
14092
  the order_st BY clause, only the SELECT list is being searched.
5320
14093
 
5321
14094
  If 'order' is resolved to an Item, then order->item is set to the found
5322
14095
  Item. If there is no item for the found column (that is, it was resolved
5341
14114
  @retval
5342
14115
    true  if error occurred
5343
14116
*/
5344
 
static bool find_order_in_list(Session *session, 
5345
 
                               Item **ref_pointer_array, 
5346
 
                               TableList *tables,
5347
 
                               Order *order,
5348
 
                               List<Item> &fields,
5349
 
                               List<Item> &all_fields,
5350
 
                               bool is_group_field)
 
14117
 
 
14118
static bool
 
14119
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
 
14120
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14121
                   bool is_group_field)
5351
14122
{
5352
14123
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5353
14124
  Item::Type order_item_type;
5366
14137
    if (!count || count > fields.elements)
5367
14138
    {
5368
14139
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
14140
               order_item->full_name(), session->where);
5370
14141
      return true;
5371
14142
    }
5372
14143
    order->item= ref_pointer_array + count - 1;
5376
14147
    return false;
5377
14148
  }
5378
14149
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14150
  select_item= find_item_in_list(order_item, fields, &counter,
5380
14151
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
14152
  if (!select_item)
5382
14153
    return true; /* The item is not unique, or some other error occured. */
5402
14173
        order_item_type == Item::REF_ITEM)
5403
14174
    {
5404
14175
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14176
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14177
                                       false);
5406
14178
      if (!from_field)
5407
14179
        from_field= (Field*) not_found_field;
5408
14180
    }
5443
14215
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
14216
                          ER(ER_NON_UNIQ_ERROR),
5445
14217
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
14218
                          current_session->where);
5447
14219
    }
5448
14220
  }
5449
14221
 
5472
14244
  return false;
5473
14245
}
5474
14246
 
 
14247
 
5475
14248
/**
5476
14249
  Change order to point at item in select list.
5477
14250
 
5478
14251
  If item isn't a number and doesn't exits in the select list, add it the
5479
14252
  the field list.
5480
14253
*/
5481
 
int setup_order(Session *session,
5482
 
                Item **ref_pointer_array,
5483
 
                TableList *tables,
5484
 
                            List<Item> &fields,
5485
 
                List<Item> &all_fields,
5486
 
                Order *order)
 
14254
 
 
14255
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
 
14256
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5487
14257
{
5488
 
  session->setWhere("order clause");
 
14258
  session->where="order clause";
5489
14259
  for (; order; order=order->next)
5490
14260
  {
5491
14261
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5495
14265
  return 0;
5496
14266
}
5497
14267
 
 
14268
 
5498
14269
/**
5499
14270
  Intitialize the GROUP BY list.
5500
14271
 
5501
 
  @param session                        Thread Cursor
 
14272
  @param session                        Thread handler
5502
14273
  @param ref_pointer_array      We store references to all fields that was
5503
14274
                               not in 'fields' here.
5504
14275
  @param fields         All fields in the select part. Any item in
5520
14291
  @retval
5521
14292
    1  error (probably out of memory)
5522
14293
*/
5523
 
int setup_group(Session *session,
5524
 
                Item **ref_pointer_array,
5525
 
                TableList *tables,
5526
 
                      List<Item> &fields,
5527
 
                List<Item> &all_fields,
5528
 
                Order *order,
5529
 
                      bool *hidden_group_fields)
 
14294
 
 
14295
int
 
14296
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
 
14297
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14298
            bool *hidden_group_fields)
5530
14299
{
5531
14300
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
14301
  order_st *ord;
5533
14302
 
5534
14303
  if (!order)
5535
14304
    return 0;                           /* Everything is ok */
5536
14305
 
5537
14306
  uint32_t org_fields=all_fields.elements;
5538
14307
 
5539
 
  session->setWhere("group statement");
 
14308
  session->where="group statement";
5540
14309
  for (ord= order; ord; ord= ord->next)
5541
14310
  {
5542
14311
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5569
14338
    Item *item;
5570
14339
    Item_field *field;
5571
14340
    int cur_pos_in_select_list= 0;
5572
 
    List<Item>::iterator li(fields.begin());
5573
 
    List<Item_field>::iterator naf_it(session->getLex()->current_select->non_agg_fields.begin());
 
14341
    List_iterator<Item> li(fields);
 
14342
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
14343
 
5575
14344
    field= naf_it++;
5576
14345
    while (field && (item=li++))
5596
14365
            if ((*ord->item)->eq((Item*)field, 0))
5597
14366
              goto next_field;
5598
14367
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
14368
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
14369
            ER_NON_GROUPING_FIELD_USED
5600
14370
          */
5601
14371
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
14372
          return 1;
5618
14388
  Try to use the fields in the order given by 'order' to allow one to
5619
14389
  optimize away 'order by'.
5620
14390
*/
5621
 
Order *create_distinct_group(Session *session,
5622
 
                                Item **ref_pointer_array,
5623
 
                                Order *order_list,
5624
 
                                List<Item> &fields,
5625
 
                                List<Item> &,
5626
 
                                bool *all_order_by_fields_used)
 
14391
 
 
14392
static order_st *
 
14393
create_distinct_group(Session *session, Item **ref_pointer_array,
 
14394
                      order_st *order_list, List<Item> &fields,
 
14395
                      List<Item> &, bool *all_order_by_fields_used)
5627
14396
{
5628
 
  List<Item>::iterator li(fields.begin());
 
14397
  List_iterator<Item> li(fields);
5629
14398
  Item *item;
5630
 
  Order *order,*group,**prev;
 
14399
  order_st *order,*group,**prev;
5631
14400
 
5632
14401
  *all_order_by_fields_used= 1;
5633
14402
  while ((item=li++))
5638
14407
  {
5639
14408
    if (order->in_field_list)
5640
14409
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
14410
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5642
14411
      if (!ord)
5643
 
        return 0;
 
14412
        return 0;
5644
14413
      *prev=ord;
5645
14414
      prev= &ord->next;
5646
14415
      (*ord->item)->marker=1;
5649
14418
      *all_order_by_fields_used= 0;
5650
14419
  }
5651
14420
 
5652
 
  li= fields.begin();
 
14421
  li.rewind();
5653
14422
  while ((item=li++))
5654
14423
  {
5655
14424
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5658
14427
        Don't put duplicate columns from the SELECT list into the
5659
14428
        GROUP BY list.
5660
14429
      */
5661
 
      Order *ord_iter;
 
14430
      order_st *ord_iter;
5662
14431
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
14432
        if ((*ord_iter->item)->eq(item, 1))
5664
14433
          goto next_item;
5665
14434
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14435
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5667
14436
      if (!ord)
5668
 
        return 0;
 
14437
        return 0;
5669
14438
 
5670
14439
      /*
5671
14440
        We have here only field_list (not all_field_list), so we can use
5684
14453
  return group;
5685
14454
}
5686
14455
 
 
14456
 
5687
14457
/**
5688
14458
  Update join with count of the different type of fields.
5689
14459
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14460
 
 
14461
void
 
14462
count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
 
14463
                  List<Item> &fields, bool reset_with_sum_func)
5691
14464
{
5692
 
  List<Item>::iterator li(fields.begin());
 
14465
  List_iterator<Item> li(fields);
5693
14466
  Item *field;
5694
14467
 
5695
14468
  param->field_count=param->sum_func_count=param->func_count=
5704
14477
    {
5705
14478
      if (! field->const_item())
5706
14479
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14480
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
14481
        if (!sum_item->depended_from() ||
5709
14482
            sum_item->depended_from() == select_lex)
5710
14483
        {
5727
14500
    {
5728
14501
      param->func_count++;
5729
14502
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
14503
        field->with_sum_func=0;
 
14504
    }
 
14505
  }
 
14506
}
 
14507
 
 
14508
 
 
14509
/**
 
14510
  Return 1 if second is a subpart of first argument.
 
14511
 
 
14512
  If first parts has different direction, change it to second part
 
14513
  (group is sorted like order)
 
14514
*/
 
14515
 
 
14516
static bool
 
14517
test_if_subpart(order_st *a,order_st *b)
 
14518
{
 
14519
  for (; a && b; a=a->next,b=b->next)
 
14520
  {
 
14521
    if ((*a->item)->eq(*b->item,1))
 
14522
      a->asc=b->asc;
 
14523
    else
 
14524
      return 0;
 
14525
  }
 
14526
  return test(!b);
 
14527
}
 
14528
 
 
14529
/**
 
14530
  Return table number if there is only one table in sort order
 
14531
  and group and order is compatible, else return 0.
 
14532
*/
 
14533
 
 
14534
static Table *
 
14535
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14536
{
 
14537
  table_map map= (table_map) 0;
 
14538
 
 
14539
  if (!a)
 
14540
    a=b;                                        // Only one need to be given
 
14541
  else if (!b)
 
14542
    b=a;
 
14543
 
 
14544
  for (; a && b; a=a->next,b=b->next)
 
14545
  {
 
14546
    if (!(*a->item)->eq(*b->item,1))
 
14547
      return(0);
 
14548
    map|=a->item[0]->used_tables();
 
14549
  }
 
14550
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14551
    return(0);
 
14552
 
 
14553
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14554
  if (map != tables->table->map)
 
14555
    return(0);                          // More than one table
 
14556
  return(tables->table);
 
14557
}
 
14558
 
 
14559
 
 
14560
/**
 
14561
  calc how big buffer we need for comparing group entries.
 
14562
*/
 
14563
 
 
14564
static void
 
14565
calc_group_buffer(JOIN *join,order_st *group)
 
14566
{
 
14567
  uint32_t key_length=0, parts=0, null_parts=0;
 
14568
 
 
14569
  if (group)
 
14570
    join->group= 1;
 
14571
  for (; group ; group=group->next)
 
14572
  {
 
14573
    Item *group_item= *group->item;
 
14574
    Field *field= group_item->get_tmp_table_field();
 
14575
    if (field)
 
14576
    {
 
14577
      enum_field_types type;
 
14578
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14579
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14580
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14581
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14582
      else
 
14583
        key_length+= field->pack_length();
 
14584
    }
 
14585
    else
 
14586
    {
 
14587
      switch (group_item->result_type()) {
 
14588
      case REAL_RESULT:
 
14589
        key_length+= sizeof(double);
 
14590
        break;
 
14591
      case INT_RESULT:
 
14592
        key_length+= sizeof(int64_t);
 
14593
        break;
 
14594
      case DECIMAL_RESULT:
 
14595
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
14596
                                                (group_item->decimals ? 1 : 0),
 
14597
                                                group_item->decimals);
 
14598
        break;
 
14599
      case STRING_RESULT:
 
14600
      {
 
14601
        enum enum_field_types type= group_item->field_type();
 
14602
        /*
 
14603
          As items represented as DATE/TIME fields in the group buffer
 
14604
          have STRING_RESULT result type, we increase the length
 
14605
          by 8 as maximum pack length of such fields.
 
14606
        */
 
14607
        if (type == DRIZZLE_TYPE_DATE ||
 
14608
            type == DRIZZLE_TYPE_DATETIME ||
 
14609
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14610
        {
 
14611
          key_length+= 8;
 
14612
        }
 
14613
        else
 
14614
        {
 
14615
          /*
 
14616
            Group strings are taken as varstrings and require an length field.
 
14617
            A field is not yet created by create_tmp_field()
 
14618
            and the sizes should match up.
 
14619
          */
 
14620
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14621
        }
 
14622
        break;
 
14623
      }
 
14624
      default:
 
14625
        /* This case should never be choosen */
 
14626
        assert(0);
 
14627
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14628
      }
 
14629
    }
 
14630
    parts++;
 
14631
    if (group_item->maybe_null)
 
14632
      null_parts++;
 
14633
  }
 
14634
  join->tmp_table_param.group_length=key_length+null_parts;
 
14635
  join->tmp_table_param.group_parts=parts;
 
14636
  join->tmp_table_param.group_null_parts=null_parts;
 
14637
}
 
14638
 
 
14639
 
 
14640
/**
 
14641
  allocate group fields or take prepared (cached).
 
14642
 
 
14643
  @param main_join   join of current select
 
14644
  @param curr_join   current join (join of current select or temporary copy
 
14645
                     of it)
 
14646
 
 
14647
  @retval
 
14648
    0   ok
 
14649
  @retval
 
14650
    1   failed
 
14651
*/
 
14652
 
 
14653
static bool
 
14654
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14655
{
 
14656
  if (main_join->group_fields_cache.elements)
 
14657
  {
 
14658
    curr_join->group_fields= main_join->group_fields_cache;
 
14659
    curr_join->sort_and_group= 1;
 
14660
  }
 
14661
  else
 
14662
  {
 
14663
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14664
      return (1);
 
14665
    main_join->group_fields_cache= curr_join->group_fields;
 
14666
  }
 
14667
  return (0);
 
14668
}
 
14669
 
 
14670
 
 
14671
/**
 
14672
  Get a list of buffers for saveing last group.
 
14673
 
 
14674
  Groups are saved in reverse order for easyer check loop.
 
14675
*/
 
14676
 
 
14677
static bool
 
14678
alloc_group_fields(JOIN *join,order_st *group)
 
14679
{
 
14680
  if (group)
 
14681
  {
 
14682
    for (; group ; group=group->next)
 
14683
    {
 
14684
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
 
14685
      if (!tmp || join->group_fields.push_front(tmp))
 
14686
        return true;
 
14687
    }
 
14688
  }
 
14689
  join->sort_and_group=1;                       /* Mark for do_select */
 
14690
  return false;
 
14691
}
 
14692
 
5734
14693
 
5735
14694
/*
5736
14695
  Test if a single-row cache of items changed, and update the cache.
5744
14703
  @return -1 if no item changed
5745
14704
  @return index of the first item that changed
5746
14705
*/
 
14706
 
5747
14707
int test_if_item_cache_changed(List<Cached_item> &list)
5748
14708
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
14709
  List_iterator<Cached_item> li(list);
5750
14710
  int idx= -1,i;
5751
14711
  Cached_item *buff;
5752
14712
 
5758
14718
  return(idx);
5759
14719
}
5760
14720
 
 
14721
 
5761
14722
/**
5762
14723
  Setup copy_fields to save fields at start of new group.
5763
14724
 
5786
14747
  @retval
5787
14748
    !=0   error
5788
14749
*/
5789
 
bool setup_copy_fields(Session *session,
5790
 
                       Tmp_Table_Param *param,
5791
 
                       Item **ref_pointer_array,
5792
 
                       List<Item> &res_selected_fields,
5793
 
                       List<Item> &res_all_fields,
5794
 
                       uint32_t elements,
5795
 
                       List<Item> &all_fields)
 
14750
 
 
14751
bool
 
14752
setup_copy_fields(Session *session, Tmp_Table_Param *param,
 
14753
                  Item **ref_pointer_array,
 
14754
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14755
                  uint32_t elements, List<Item> &all_fields)
5796
14756
{
5797
14757
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
5799
 
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
14758
  List_iterator_fast<Item> li(all_fields);
 
14759
  Copy_field *copy= NULL;
 
14760
  res_selected_fields.empty();
 
14761
  res_all_fields.empty();
 
14762
  List_iterator_fast<Item> itr(res_all_fields);
5803
14763
  List<Item> extra_funcs;
5804
14764
  uint32_t i, border= all_fields.elements - elements;
5805
14765
 
5806
14766
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14767
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
14768
    goto err2;
5809
14769
 
5810
 
  param->copy_funcs.clear();
 
14770
  param->copy_funcs.empty();
5811
14771
  for (i= 0; (pos= li++); i++)
5812
14772
  {
5813
14773
    Field *field;
5817
14777
    {
5818
14778
      Item_field *item;
5819
14779
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
14780
        goto err;
5821
14781
      if (pos->type() == Item::REF_ITEM)
5822
14782
      {
5823
14783
        /* preserve the names of the ref when dereferncing */
5829
14789
      pos= item;
5830
14790
      if (item->field->flags & BLOB_FLAG)
5831
14791
      {
5832
 
        if (!(pos= new Item_copy_string(pos)))
5833
 
          goto err;
5834
 
            /*
5835
 
              Item_copy_string::copy for function can call
5836
 
              Item_copy_string::val_int for blob via Item_ref.
5837
 
              But if Item_copy_string::copy for blob isn't called before,
5838
 
              it's value will be wrong
5839
 
              so let's insert Item_copy_string for blobs in the beginning of
5840
 
              copy_funcs
5841
 
              (to see full test case look at having.test, BUG #4358)
5842
 
            */
5843
 
        if (param->copy_funcs.push_front(pos))
5844
 
          goto err;
 
14792
        if (!(pos= new Item_copy_string(pos)))
 
14793
          goto err;
 
14794
       /*
 
14795
         Item_copy_string::copy for function can call
 
14796
         Item_copy_string::val_int for blob via Item_ref.
 
14797
         But if Item_copy_string::copy for blob isn't called before,
 
14798
         it's value will be wrong
 
14799
         so let's insert Item_copy_string for blobs in the beginning of
 
14800
         copy_funcs
 
14801
         (to see full test case look at having.test, BUG #4358)
 
14802
       */
 
14803
        if (param->copy_funcs.push_front(pos))
 
14804
          goto err;
5845
14805
      }
5846
14806
      else
5847
14807
      {
 
14808
        /*
 
14809
           set up save buffer and change result_field to point at
 
14810
           saved value
 
14811
        */
 
14812
        field= item->field;
 
14813
        item->result_field=field->new_field(session->mem_root,field->table, 1);
5848
14814
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
14815
          We need to allocate one extra byte for null handling and
 
14816
          another extra byte to not get warnings from purify in
 
14817
          Field_varstring::val_int
5851
14818
        */
5852
 
        field= item->field;
5853
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
5854
 
              /*
5855
 
                We need to allocate one extra byte for null handling and
5856
 
                another extra byte to not get warnings from purify in
5857
 
                Field_varstring::val_int
5858
 
              */
5859
 
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
5860
 
          goto err;
 
14819
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14820
          goto err;
5861
14821
        if (copy)
5862
14822
        {
5863
14823
          copy->set(tmp, item->result_field);
5864
14824
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
14825
#ifdef HAVE_purify
5866
14826
          copy->to_ptr[copy->from_length]= 0;
5867
14827
#endif
5868
14828
          copy++;
5876
14836
             !real_pos->with_sum_func)
5877
14837
    {                                           // Save for send fields
5878
14838
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
5881
 
        This should be changed to use copy_int or copy_real depending
5882
 
        on how the value is to be used: In some cases this may be an
5883
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
14839
      /* TODO:
 
14840
         In most cases this result will be sent to the user.
 
14841
         This should be changed to use copy_int or copy_real depending
 
14842
         on how the value is to be used: In some cases this may be an
 
14843
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
14844
      */
5885
14845
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
 
14846
        goto err;
5887
14847
      if (i < border)                           // HAVING, order_st and GROUP BY
5888
14848
      {
5889
14849
        if (extra_funcs.push_back(pos))
5890
14850
          goto err;
5891
14851
      }
5892
14852
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
14853
        goto err;
5894
14854
    }
5895
14855
    res_all_fields.push_back(pos);
5896
14856
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5902
14862
    itr++;
5903
14863
  itr.sublist(res_selected_fields, elements);
5904
14864
  /*
5905
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14865
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5906
14866
    reference used in these will resolve to a item that is already calculated
5907
14867
  */
5908
14868
  param->copy_funcs.concat(&extra_funcs);
5909
14869
 
5910
14870
  return(0);
5911
14871
 
5912
 
err:
 
14872
 err:
5913
14873
  if (copy)
5914
14874
    delete [] param->copy_field;                        // This is never 0
5915
14875
  param->copy_field=0;
5917
14877
  return(true);
5918
14878
}
5919
14879
 
 
14880
 
5920
14881
/**
5921
14882
  Make a copy of all simple SELECT'ed items.
5922
14883
 
5923
14884
  This is done at the start of a new group so that we can retrieve
5924
14885
  these later when the group changes.
5925
14886
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
14887
 
 
14888
void
 
14889
copy_fields(Tmp_Table_Param *param)
5927
14890
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
14891
  Copy_field *ptr=param->copy_field;
 
14892
  Copy_field *end=param->copy_field_end;
5930
14893
 
5931
14894
  for (; ptr != end; ptr++)
5932
14895
    (*ptr->do_copy)(ptr);
5933
14896
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
14897
  List_iterator_fast<Item> it(param->copy_funcs);
5935
14898
  Item_copy_string *item;
5936
14899
  while ((item = (Item_copy_string*) it++))
5937
14900
    item->copy();
5938
14901
}
5939
14902
 
 
14903
 
 
14904
/**
 
14905
  Make an array of pointers to sum_functions to speed up
 
14906
  sum_func calculation.
 
14907
 
 
14908
  @retval
 
14909
    0   ok
 
14910
  @retval
 
14911
    1   Error
 
14912
*/
 
14913
 
 
14914
bool JOIN::alloc_func_list()
 
14915
{
 
14916
  uint32_t func_count, group_parts;
 
14917
 
 
14918
  func_count= tmp_table_param.sum_func_count;
 
14919
  /*
 
14920
    If we are using rollup, we need a copy of the summary functions for
 
14921
    each level
 
14922
  */
 
14923
  if (rollup.state != ROLLUP::STATE_NONE)
 
14924
    func_count*= (send_group_parts+1);
 
14925
 
 
14926
  group_parts= send_group_parts;
 
14927
  /*
 
14928
    If distinct, reserve memory for possible
 
14929
    disctinct->group_by optimization
 
14930
  */
 
14931
  if (select_distinct)
 
14932
  {
 
14933
    group_parts+= fields_list.elements;
 
14934
    /*
 
14935
      If the order_st clause is specified then it's possible that
 
14936
      it also will be optimized, so reserve space for it too
 
14937
    */
 
14938
    if (order)
 
14939
    {
 
14940
      order_st *ord;
 
14941
      for (ord= order; ord; ord= ord->next)
 
14942
        group_parts++;
 
14943
    }
 
14944
  }
 
14945
 
 
14946
  /* This must use calloc() as rollup_make_fields depends on this */
 
14947
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14948
                                      sizeof(Item_sum***) * (group_parts+1));
 
14949
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14950
  return(sum_funcs == 0);
 
14951
}
 
14952
 
 
14953
 
 
14954
/**
 
14955
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14956
 
 
14957
  @param field_list        All items
 
14958
  @param send_fields       Items in select list
 
14959
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14960
  @param recompute         Set to true if sum_funcs must be recomputed
 
14961
 
 
14962
  @retval
 
14963
    0  ok
 
14964
  @retval
 
14965
    1  error
 
14966
*/
 
14967
 
 
14968
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14969
                              bool before_group_by, bool recompute)
 
14970
{
 
14971
  List_iterator_fast<Item> it(field_list);
 
14972
  Item_sum **func;
 
14973
  Item *item;
 
14974
 
 
14975
  if (*sum_funcs && !recompute)
 
14976
    return(false); /* We have already initialized sum_funcs. */
 
14977
 
 
14978
  func= sum_funcs;
 
14979
  while ((item=it++))
 
14980
  {
 
14981
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14982
        (!((Item_sum*) item)->depended_from() ||
 
14983
         ((Item_sum *)item)->depended_from() == select_lex))
 
14984
      *func++= (Item_sum*) item;
 
14985
  }
 
14986
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14987
  {
 
14988
    rollup.state= ROLLUP::STATE_READY;
 
14989
    if (rollup_make_fields(field_list, send_fields, &func))
 
14990
      return(true);                     // Should never happen
 
14991
  }
 
14992
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14993
  {
 
14994
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
14995
      sum_funcs_end[i]= func;
 
14996
  }
 
14997
  else if (rollup.state == ROLLUP::STATE_READY)
 
14998
    return(false);                         // Don't put end marker
 
14999
  *func=0;                                      // End marker
 
15000
  return(false);
 
15001
}
 
15002
 
 
15003
 
5940
15004
/**
5941
15005
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
15006
  new list of all items.
5953
15017
  @retval
5954
15018
    !=0   error
5955
15019
*/
5956
 
bool change_to_use_tmp_fields(Session *session,
5957
 
                              Item **ref_pointer_array,
5958
 
                                                List<Item> &res_selected_fields,
5959
 
                                                List<Item> &res_all_fields,
5960
 
                                                uint32_t elements,
5961
 
                              List<Item> &all_fields)
 
15020
 
 
15021
static bool
 
15022
change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
 
15023
                         List<Item> &res_selected_fields,
 
15024
                         List<Item> &res_all_fields,
 
15025
                         uint32_t elements, List<Item> &all_fields)
5962
15026
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
15027
  List_iterator_fast<Item> it(all_fields);
5964
15028
  Item *item_field,*item;
5965
15029
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
15030
  res_selected_fields.empty();
 
15031
  res_all_fields.empty();
5968
15032
 
5969
15033
  uint32_t i, border= all_fields.elements - elements;
5970
15034
  for (i= 0; (item= it++); i++)
5979
15043
    {
5980
15044
      if (item->type() == Item::FIELD_ITEM)
5981
15045
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
15046
        item_field= item->get_tmp_table_item(session);
5983
15047
      }
5984
15048
      else if ((field= item->get_tmp_table_field()))
5985
15049
      {
5986
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
5987
 
          item_field= ((Item_sum*) item)->result_item(field);
5988
 
        else
5989
 
          item_field= (Item*) new Item_field(field);
5990
 
        if (!item_field)
5991
 
          return(true);                    // Fatal error
 
15050
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15051
          item_field= ((Item_sum*) item)->result_item(field);
 
15052
        else
 
15053
          item_field= (Item*) new Item_field(field);
 
15054
        if (!item_field)
 
15055
          return(true);                    // Fatal error
5992
15056
 
5993
15057
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
15058
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
15059
        item_field->name= item->name;
5996
15060
        if (item->type() == Item::REF_ITEM)
5997
15061
        {
5998
15062
          Item_field *ifield= (Item_field *) item_field;
6002
15066
        }
6003
15067
      }
6004
15068
      else
6005
 
        item_field= item;
 
15069
        item_field= item;
6006
15070
    }
6007
15071
    res_all_fields.push_back(item_field);
6008
15072
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
15073
      item_field;
6010
15074
  }
6011
15075
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15076
  List_iterator_fast<Item> itr(res_all_fields);
6013
15077
  for (i= 0; i < border; i++)
6014
15078
    itr++;
6015
15079
  itr.sublist(res_selected_fields, elements);
6016
15080
  return(false);
6017
15081
}
6018
15082
 
 
15083
 
6019
15084
/**
6020
15085
  Change all sum_func refs to fields to point at fields in tmp table.
6021
15086
  Change all funcs to be fields in tmp table.
6032
15097
  @retval
6033
15098
    1   error
6034
15099
*/
6035
 
bool change_refs_to_tmp_fields(Session *session,
6036
 
                               Item **ref_pointer_array,
6037
 
                               List<Item> &res_selected_fields,
6038
 
                               List<Item> &res_all_fields,
6039
 
                               uint32_t elements,
6040
 
                                                 List<Item> &all_fields)
 
15100
 
 
15101
static bool
 
15102
change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
15103
                          List<Item> &res_selected_fields,
 
15104
                          List<Item> &res_all_fields, uint32_t elements,
 
15105
                          List<Item> &all_fields)
6041
15106
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
15107
  List_iterator_fast<Item> it(all_fields);
6043
15108
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
15109
  res_selected_fields.empty();
 
15110
  res_all_fields.empty();
6046
15111
 
6047
15112
  uint32_t i, border= all_fields.elements - elements;
6048
15113
  for (i= 0; (item= it++); i++)
6052
15117
      new_item;
6053
15118
  }
6054
15119
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15120
  List_iterator_fast<Item> itr(res_all_fields);
6056
15121
  for (i= 0; i < border; i++)
6057
15122
    itr++;
6058
15123
  itr.sublist(res_selected_fields, elements);
6060
15125
  return session->is_fatal_error;
6061
15126
}
6062
15127
 
 
15128
 
 
15129
 
6063
15130
/******************************************************************************
6064
15131
  Code for calculating functions
6065
15132
******************************************************************************/
6066
15133
 
 
15134
 
6067
15135
/**
6068
15136
  Call ::setup for all sum functions.
6069
15137
 
6070
 
  @param session           thread Cursor
 
15138
  @param session           thread handler
6071
15139
  @param func_ptr      sum function list
6072
15140
 
6073
15141
  @retval
6075
15143
  @retval
6076
15144
    true   error
6077
15145
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15146
 
 
15147
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
6079
15148
{
6080
15149
  Item_sum *func;
6081
15150
  while ((func= *(func_ptr++)))
6086
15155
  return(false);
6087
15156
}
6088
15157
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15158
 
 
15159
static void
 
15160
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
15161
{
6091
15162
  Item_sum *func;
6092
15163
  while ((func= *(func_ptr++)))
6093
15164
    func->reset_field();
6094
15165
}
6095
15166
 
 
15167
 
6096
15168
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15169
 
 
15170
static void
 
15171
update_tmptable_sum_func(Item_sum **func_ptr, Table *)
6098
15172
{
6099
15173
  Item_sum *func;
6100
15174
  while ((func= *(func_ptr++)))
6101
15175
    func->update_field();
6102
15176
}
6103
15177
 
 
15178
 
6104
15179
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15180
 
 
15181
static void
 
15182
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
15183
{
6107
15184
  for (; func_ptr != end_ptr ; func_ptr++)
6108
15185
    (void) (*func_ptr)->save_in_result_field(1);
6109
15186
  return;
6110
15187
}
6111
15188
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15189
 
 
15190
static bool
 
15191
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
15192
{
6114
15193
  for (; func_ptr != end_ptr ;func_ptr++)
6115
15194
  {
6125
15204
  return 0;
6126
15205
}
6127
15206
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
15207
 
 
15208
static bool
 
15209
update_sum_func(Item_sum **func_ptr)
6129
15210
{
6130
15211
  Item_sum *func;
6131
15212
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
15216
}
6136
15217
 
6137
15218
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
15219
 
 
15220
void
 
15221
copy_funcs(Item **func_ptr)
6139
15222
{
6140
15223
  Item *func;
6141
15224
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
15225
    func->save_in_result_field(1);
6144
 
    /*
6145
 
      Need to check the THD error state because Item::val_xxx() don't
6146
 
      return error code, but can generate errors
6147
 
      @todo change it for a real status check when Item::val_xxx()
6148
 
      are extended to return status code.
6149
 
    */
6150
 
    if (session->is_error())
6151
 
      return true;
6152
 
  }
6153
 
  return false;
6154
 
}
 
15226
}
 
15227
 
 
15228
 
 
15229
/**
 
15230
  Create a condition for a const reference and add this to the
 
15231
  currenct select for the table.
 
15232
*/
 
15233
 
 
15234
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
 
15235
{
 
15236
  if (!join_tab->ref.key_parts)
 
15237
    return(false);
 
15238
 
 
15239
  Item_cond_and *cond=new Item_cond_and();
 
15240
  Table *table=join_tab->table;
 
15241
  int error;
 
15242
  if (!cond)
 
15243
    return(true);
 
15244
 
 
15245
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15246
  {
 
15247
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15248
                              fieldnr-1];
 
15249
    Item *value=join_tab->ref.items[i];
 
15250
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15251
  }
 
15252
  if (session->is_fatal_error)
 
15253
    return(true);
 
15254
 
 
15255
  if (!cond->fixed)
 
15256
    cond->fix_fields(session, (Item**)&cond);
 
15257
  if (join_tab->select)
 
15258
  {
 
15259
    error=(int) cond->add(join_tab->select->cond);
 
15260
    join_tab->select_cond=join_tab->select->cond=cond;
 
15261
  }
 
15262
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15263
                                          &error)))
 
15264
    join_tab->select_cond=cond;
 
15265
 
 
15266
  return(error ? true : false);
 
15267
}
 
15268
 
6155
15269
 
6156
15270
/**
6157
15271
  Free joins of subselect of this select.
6159
15273
  @param session      Session pointer
6160
15274
  @param select   pointer to Select_Lex which subselects joins we will free
6161
15275
*/
 
15276
 
6162
15277
void free_underlaid_joins(Session *, Select_Lex *select)
6163
15278
{
6164
15279
  for (Select_Lex_Unit *unit= select->first_inner_unit();
6202
15317
  @param changed        out:  returns 1 if item contains a replaced field item
6203
15318
 
6204
15319
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
15320
    - TODO: Some functions are not null-preserving. For those functions
6206
15321
    updating of the maybe_null attribute is an overkill.
6207
15322
 
6208
15323
  @retval
6210
15325
  @retval
6211
15326
    1   on error
6212
15327
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15328
 
 
15329
static bool change_group_ref(Session *session, Item_func *expr, order_st *group_list,
 
15330
                             bool *changed)
6214
15331
{
6215
15332
  if (expr->arg_count)
6216
15333
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
15334
    Name_resolution_context *context= &session->lex->current_select->context;
6218
15335
    Item **arg,**arg_end;
6219
15336
    bool arg_changed= false;
6220
15337
    for (arg= expr->arguments(),
6224
15341
      Item *item= *arg;
6225
15342
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
15343
      {
6227
 
        Order *group_tmp;
 
15344
        order_st *group_tmp;
6228
15345
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
15346
        {
6230
15347
          if (item->eq(*group_tmp->item,0))
6254
15371
}
6255
15372
 
6256
15373
 
 
15374
/** Allocate memory needed for other rollup functions. */
 
15375
 
 
15376
bool JOIN::rollup_init()
 
15377
{
 
15378
  uint32_t i,j;
 
15379
  Item **ref_array;
 
15380
 
 
15381
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15382
  rollup.state= ROLLUP::STATE_INITED;
 
15383
 
 
15384
  /*
 
15385
    Create pointers to the different sum function groups
 
15386
    These are updated by rollup_make_fields()
 
15387
  */
 
15388
  tmp_table_param.group_parts= send_group_parts;
 
15389
 
 
15390
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
15391
                                                sizeof(Item**) +
 
15392
                                                sizeof(List<Item>) +
 
15393
                                                ref_pointer_array_size)
 
15394
                                                * send_group_parts )))
 
15395
    return 1;
 
15396
 
 
15397
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15398
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15399
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15400
 
 
15401
  /*
 
15402
    Prepare space for field list for the different levels
 
15403
    These will be filled up in rollup_make_fields()
 
15404
  */
 
15405
  for (i= 0 ; i < send_group_parts ; i++)
 
15406
  {
 
15407
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
15408
    List<Item> *rollup_fields= &rollup.fields[i];
 
15409
    rollup_fields->empty();
 
15410
    rollup.ref_pointer_arrays[i]= ref_array;
 
15411
    ref_array+= all_fields.elements;
 
15412
  }
 
15413
  for (i= 0 ; i < send_group_parts; i++)
 
15414
  {
 
15415
    for (j=0 ; j < fields_list.elements ; j++)
 
15416
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15417
  }
 
15418
  List_iterator<Item> it(all_fields);
 
15419
  Item *item;
 
15420
  while ((item= it++))
 
15421
  {
 
15422
    order_st *group_tmp;
 
15423
    bool found_in_group= 0;
 
15424
 
 
15425
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15426
    {
 
15427
      if (*group_tmp->item == item)
 
15428
      {
 
15429
        item->maybe_null= 1;
 
15430
        found_in_group= 1;
 
15431
        if (item->const_item())
 
15432
        {
 
15433
          /*
 
15434
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15435
            is wrapped up into an Item_func object yielding the same value
 
15436
            as the constant item. The objects of the wrapper class are never
 
15437
            considered as constant items and besides they inherit all
 
15438
            properties of the Item_result_field class.
 
15439
            This wrapping allows us to ensure writing constant items
 
15440
            into temporary tables whenever the result of the ROLLUP
 
15441
            operation has to be written into a temporary table, e.g. when
 
15442
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15443
            Usually when creating temporary tables for a intermidiate
 
15444
            result we do not include fields for constant expressions.
 
15445
          */
 
15446
          Item* new_item= new Item_func_rollup_const(item);
 
15447
          if (!new_item)
 
15448
            return 1;
 
15449
          new_item->fix_fields(session, (Item **) 0);
 
15450
          session->change_item_tree(it.ref(), new_item);
 
15451
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15452
          {
 
15453
            if (*tmp->item == item)
 
15454
              session->change_item_tree(tmp->item, new_item);
 
15455
          }
 
15456
        }
 
15457
      }
 
15458
    }
 
15459
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15460
    {
 
15461
      bool changed= false;
 
15462
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
 
15463
        return 1;
 
15464
      /*
 
15465
        We have to prevent creation of a field in a temporary table for
 
15466
        an expression that contains GROUP BY attributes.
 
15467
        Marking the expression item as 'with_sum_func' will ensure this.
 
15468
      */
 
15469
      if (changed)
 
15470
        item->with_sum_func= 1;
 
15471
    }
 
15472
  }
 
15473
  return 0;
 
15474
}
 
15475
 
 
15476
 
 
15477
/**
 
15478
  Fill up rollup structures with pointers to fields to use.
 
15479
 
 
15480
  Creates copies of item_sum items for each sum level.
 
15481
 
 
15482
  @param fields_arg             List of all fields (hidden and real ones)
 
15483
  @param sel_fields             Pointer to selected fields
 
15484
  @param func                   Store here a pointer to all fields
 
15485
 
 
15486
  @retval
 
15487
    0   if ok;
 
15488
    In this case func is pointing to next not used element.
 
15489
  @retval
 
15490
    1    on error
 
15491
*/
 
15492
 
 
15493
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15494
                              Item_sum ***func)
 
15495
{
 
15496
  List_iterator_fast<Item> it(fields_arg);
 
15497
  Item *first_field= sel_fields.head();
 
15498
  uint32_t level;
 
15499
 
 
15500
  /*
 
15501
    Create field lists for the different levels
 
15502
 
 
15503
    The idea here is to have a separate field list for each rollup level to
 
15504
    avoid all runtime checks of which columns should be NULL.
 
15505
 
 
15506
    The list is stored in reverse order to get sum function in such an order
 
15507
    in func that it makes it easy to reset them with init_sum_functions()
 
15508
 
 
15509
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15510
 
 
15511
    rollup.fields[0] will contain list where a,b,c is NULL
 
15512
    rollup.fields[1] will contain list where b,c is NULL
 
15513
    ...
 
15514
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15515
    ...
 
15516
    sum_funcs_end[0] points to all sum functions
 
15517
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15518
    ...
 
15519
  */
 
15520
 
 
15521
  for (level=0 ; level < send_group_parts ; level++)
 
15522
  {
 
15523
    uint32_t i;
 
15524
    uint32_t pos= send_group_parts - level -1;
 
15525
    bool real_fields= 0;
 
15526
    Item *item;
 
15527
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15528
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15529
    order_st *start_group;
 
15530
 
 
15531
    /* Point to first hidden field */
 
15532
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15533
 
 
15534
    /* Remember where the sum functions ends for the previous level */
 
15535
    sum_funcs_end[pos+1]= *func;
 
15536
 
 
15537
    /* Find the start of the group for this level */
 
15538
    for (i= 0, start_group= group_list ;
 
15539
         i++ < pos ;
 
15540
         start_group= start_group->next)
 
15541
      ;
 
15542
 
 
15543
    it.rewind();
 
15544
    while ((item= it++))
 
15545
    {
 
15546
      if (item == first_field)
 
15547
      {
 
15548
        real_fields= 1;                         // End of hidden fields
 
15549
        ref_array= ref_array_start;
 
15550
      }
 
15551
 
 
15552
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15553
          (!((Item_sum*) item)->depended_from() ||
 
15554
           ((Item_sum *)item)->depended_from() == select_lex))
 
15555
 
 
15556
      {
 
15557
        /*
 
15558
          This is a top level summary function that must be replaced with
 
15559
          a sum function that is reset for this level.
 
15560
 
 
15561
          NOTE: This code creates an object which is not that nice in a
 
15562
          sub select.  Fortunately it's not common to have rollup in
 
15563
          sub selects.
 
15564
        */
 
15565
        item= item->copy_or_same(session);
 
15566
        ((Item_sum*) item)->make_unique();
 
15567
        *(*func)= (Item_sum*) item;
 
15568
        (*func)++;
 
15569
      }
 
15570
      else
 
15571
      {
 
15572
        /* Check if this is something that is part of this group by */
 
15573
        order_st *group_tmp;
 
15574
        for (group_tmp= start_group, i= pos ;
 
15575
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15576
        {
 
15577
          if (*group_tmp->item == item)
 
15578
          {
 
15579
            /*
 
15580
              This is an element that is used by the GROUP BY and should be
 
15581
              set to NULL in this level
 
15582
            */
 
15583
            Item_null_result *null_item= new (session->mem_root) Item_null_result();
 
15584
            if (!null_item)
 
15585
              return 1;
 
15586
            item->maybe_null= 1;                // Value will be null sometimes
 
15587
            null_item->result_field= item->get_tmp_table_field();
 
15588
            item= null_item;
 
15589
            break;
 
15590
          }
 
15591
        }
 
15592
      }
 
15593
      *ref_array= item;
 
15594
      if (real_fields)
 
15595
      {
 
15596
        (void) new_it++;                        // Point to next item
 
15597
        new_it.replace(item);                   // Replace previous
 
15598
        ref_array++;
 
15599
      }
 
15600
      else
 
15601
        ref_array--;
 
15602
    }
 
15603
  }
 
15604
  sum_funcs_end[0]= *func;                      // Point to last function
 
15605
  return 0;
 
15606
}
 
15607
 
 
15608
/**
 
15609
  Send all rollup levels higher than the current one to the client.
 
15610
 
 
15611
  @b SAMPLE
 
15612
    @code
 
15613
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15614
  @endcode
 
15615
 
 
15616
  @param idx            Level we are on:
 
15617
                        - 0 = Total sum level
 
15618
                        - 1 = First group changed  (a)
 
15619
                        - 2 = Second group changed (a,b)
 
15620
 
 
15621
  @retval
 
15622
    0   ok
 
15623
  @retval
 
15624
    1   If send_data_failed()
 
15625
*/
 
15626
 
 
15627
int JOIN::rollup_send_data(uint32_t idx)
 
15628
{
 
15629
  uint32_t i;
 
15630
  for (i= send_group_parts ; i-- > idx ; )
 
15631
  {
 
15632
    /* Get reference pointers to sum functions in place */
 
15633
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15634
           ref_pointer_array_size);
 
15635
    if ((!having || having->val_int()))
 
15636
    {
 
15637
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15638
          result->send_data(rollup.fields[i]))
 
15639
        return 1;
 
15640
      send_records++;
 
15641
    }
 
15642
  }
 
15643
  /* Restore ref_pointer_array */
 
15644
  set_items_ref_array(current_ref_pointer_array);
 
15645
  return 0;
 
15646
}
 
15647
 
 
15648
/**
 
15649
  Write all rollup levels higher than the current one to a temp table.
 
15650
 
 
15651
  @b SAMPLE
 
15652
    @code
 
15653
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15654
  @endcode
 
15655
 
 
15656
  @param idx                 Level we are on:
 
15657
                               - 0 = Total sum level
 
15658
                               - 1 = First group changed  (a)
 
15659
                               - 2 = Second group changed (a,b)
 
15660
  @param table               reference to temp table
 
15661
 
 
15662
  @retval
 
15663
    0   ok
 
15664
  @retval
 
15665
    1   if write_data_failed()
 
15666
*/
 
15667
 
 
15668
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15669
{
 
15670
  uint32_t i;
 
15671
  for (i= send_group_parts ; i-- > idx ; )
 
15672
  {
 
15673
    /* Get reference pointers to sum functions in place */
 
15674
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15675
           ref_pointer_array_size);
 
15676
    if ((!having || having->val_int()))
 
15677
    {
 
15678
      int write_error;
 
15679
      Item *item;
 
15680
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15681
      while ((item= it++))
 
15682
      {
 
15683
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15684
          item->save_in_result_field(1);
 
15685
      }
 
15686
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15687
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15688
      {
 
15689
        if (create_myisam_from_heap(session, table_arg,
 
15690
                                    tmp_table_param.start_recinfo,
 
15691
                                    &tmp_table_param.recinfo,
 
15692
                                    write_error, 0))
 
15693
          return 1;
 
15694
      }
 
15695
    }
 
15696
  }
 
15697
  /* Restore ref_pointer_array */
 
15698
  set_items_ref_array(current_ref_pointer_array);
 
15699
  return 0;
 
15700
}
 
15701
 
 
15702
/**
 
15703
  clear results if there are not rows found for group
 
15704
  (end_send_group/end_write_group)
 
15705
*/
 
15706
 
 
15707
void JOIN::clear()
 
15708
{
 
15709
  clear_tables(this);
 
15710
  copy_fields(&tmp_table_param);
 
15711
 
 
15712
  if (sum_funcs)
 
15713
  {
 
15714
    Item_sum *func, **func_ptr= sum_funcs;
 
15715
    while ((func= *(func_ptr++)))
 
15716
      func->clear();
 
15717
  }
 
15718
}
 
15719
 
 
15720
/**
 
15721
  EXPLAIN handling.
 
15722
 
 
15723
  Send a description about what how the select will be done to stdout.
 
15724
*/
 
15725
 
 
15726
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15727
                     bool distinct,const char *message)
 
15728
{
 
15729
  List<Item> field_list;
 
15730
  List<Item> item_list;
 
15731
  Session *session=join->session;
 
15732
  select_result *result=join->result;
 
15733
  Item *item_null= new Item_null();
 
15734
  const CHARSET_INFO * const cs= system_charset_info;
 
15735
  int quick_type;
 
15736
  /* Don't log this into the slow query log */
 
15737
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15738
  join->unit->offset_limit_cnt= 0;
 
15739
 
 
15740
  /*
 
15741
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15742
    EXPLAIN column types as they're "defined" in Session::send_explain_fields()
 
15743
  */
 
15744
  if (message)
 
15745
  {
 
15746
    item_list.push_back(new Item_int((int32_t)
 
15747
                                     join->select_lex->select_number));
 
15748
    item_list.push_back(new Item_string(join->select_lex->type,
 
15749
                                        strlen(join->select_lex->type), cs));
 
15750
    for (uint32_t i=0 ; i < 7; i++)
 
15751
      item_list.push_back(item_null);
 
15752
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15753
      item_list.push_back(item_null);
 
15754
 
 
15755
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15756
    if (result->send_data(item_list))
 
15757
      join->error= 1;
 
15758
  }
 
15759
  else if (join->select_lex == join->unit->fake_select_lex)
 
15760
  {
 
15761
    /*
 
15762
      here we assume that the query will return at least two rows, so we
 
15763
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15764
      and no filesort will be actually done, but executing all selects in
 
15765
      the UNION to provide precise EXPLAIN information will hardly be
 
15766
      appreciated :)
 
15767
    */
 
15768
    char table_name_buffer[NAME_LEN];
 
15769
    item_list.empty();
 
15770
    /* id */
 
15771
    item_list.push_back(new Item_null);
 
15772
    /* select_type */
 
15773
    item_list.push_back(new Item_string(join->select_lex->type,
 
15774
                                        strlen(join->select_lex->type),
 
15775
                                        cs));
 
15776
    /* table */
 
15777
    {
 
15778
      Select_Lex *sl= join->unit->first_select();
 
15779
      uint32_t len= 6, lastop= 0;
 
15780
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15781
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15782
      {
 
15783
        len+= lastop;
 
15784
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15785
                         "%u,", sl->select_number);
 
15786
      }
 
15787
      if (sl || len + lastop >= NAME_LEN)
 
15788
      {
 
15789
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15790
        len+= 4;
 
15791
      }
 
15792
      else
 
15793
      {
 
15794
        len+= lastop;
 
15795
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15796
      }
 
15797
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15798
    }
 
15799
    /* type */
 
15800
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15801
                                          strlen(join_type_str[JT_ALL]),
 
15802
                                          cs));
 
15803
    /* possible_keys */
 
15804
    item_list.push_back(item_null);
 
15805
    /* key*/
 
15806
    item_list.push_back(item_null);
 
15807
    /* key_len */
 
15808
    item_list.push_back(item_null);
 
15809
    /* ref */
 
15810
    item_list.push_back(item_null);
 
15811
    /* in_rows */
 
15812
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15813
      item_list.push_back(item_null);
 
15814
    /* rows */
 
15815
    item_list.push_back(item_null);
 
15816
    /* extra */
 
15817
    if (join->unit->global_parameters->order_list.first)
 
15818
      item_list.push_back(new Item_string("Using filesort",
 
15819
                                          14, cs));
 
15820
    else
 
15821
      item_list.push_back(new Item_string("", 0, cs));
 
15822
 
 
15823
    if (result->send_data(item_list))
 
15824
      join->error= 1;
 
15825
  }
 
15826
  else
 
15827
  {
 
15828
    table_map used_tables=0;
 
15829
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15830
    {
 
15831
      JOIN_TAB *tab=join->join_tab+i;
 
15832
      Table *table=tab->table;
 
15833
      TableList *table_list= tab->table->pos_in_table_list;
 
15834
      char buff[512];
 
15835
      char buff1[512], buff2[512], buff3[512];
 
15836
      char keylen_str_buf[64];
 
15837
      String extra(buff, sizeof(buff),cs);
 
15838
      char table_name_buffer[NAME_LEN];
 
15839
      String tmp1(buff1,sizeof(buff1),cs);
 
15840
      String tmp2(buff2,sizeof(buff2),cs);
 
15841
      String tmp3(buff3,sizeof(buff3),cs);
 
15842
      extra.length(0);
 
15843
      tmp1.length(0);
 
15844
      tmp2.length(0);
 
15845
      tmp3.length(0);
 
15846
 
 
15847
      quick_type= -1;
 
15848
      item_list.empty();
 
15849
      /* id */
 
15850
      item_list.push_back(new Item_uint((uint32_t)
 
15851
                                       join->select_lex->select_number));
 
15852
      /* select_type */
 
15853
      item_list.push_back(new Item_string(join->select_lex->type,
 
15854
                                          strlen(join->select_lex->type),
 
15855
                                          cs));
 
15856
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15857
      {
 
15858
        quick_type= tab->select->quick->get_type();
 
15859
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15860
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15861
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15862
          tab->type = JT_INDEX_MERGE;
 
15863
        else
 
15864
          tab->type = JT_RANGE;
 
15865
      }
 
15866
      /* table */
 
15867
      if (table->derived_select_number)
 
15868
      {
 
15869
        /* Derived table name generation */
 
15870
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15871
                          "<derived%u>",
 
15872
                          table->derived_select_number);
 
15873
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15874
      }
 
15875
      else
 
15876
      {
 
15877
        TableList *real_table= table->pos_in_table_list;
 
15878
        item_list.push_back(new Item_string(real_table->alias,
 
15879
                                            strlen(real_table->alias),
 
15880
                                            cs));
 
15881
      }
 
15882
      /* "type" column */
 
15883
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15884
                                          strlen(join_type_str[tab->type]),
 
15885
                                          cs));
 
15886
      /* Build "possible_keys" value and add it to item_list */
 
15887
      if (!tab->keys.is_clear_all())
 
15888
      {
 
15889
        uint32_t j;
 
15890
        for (j=0 ; j < table->s->keys ; j++)
 
15891
        {
 
15892
          if (tab->keys.is_set(j))
 
15893
          {
 
15894
            if (tmp1.length())
 
15895
              tmp1.append(',');
 
15896
            tmp1.append(table->key_info[j].name,
 
15897
                        strlen(table->key_info[j].name),
 
15898
                        system_charset_info);
 
15899
          }
 
15900
        }
 
15901
      }
 
15902
      if (tmp1.length())
 
15903
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15904
      else
 
15905
        item_list.push_back(item_null);
 
15906
 
 
15907
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15908
      if (tab->ref.key_parts)
 
15909
      {
 
15910
        KEY *key_info=table->key_info+ tab->ref.key;
 
15911
        register uint32_t length;
 
15912
        item_list.push_back(new Item_string(key_info->name,
 
15913
                                            strlen(key_info->name),
 
15914
                                            system_charset_info));
 
15915
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
 
15916
                keylen_str_buf;
 
15917
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15918
                                            system_charset_info));
 
15919
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15920
        {
 
15921
          if (tmp2.length())
 
15922
            tmp2.append(',');
 
15923
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15924
                      system_charset_info);
 
15925
        }
 
15926
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15927
      }
 
15928
      else if (tab->type == JT_NEXT)
 
15929
      {
 
15930
        KEY *key_info=table->key_info+ tab->index;
 
15931
        register uint32_t length;
 
15932
        item_list.push_back(new Item_string(key_info->name,
 
15933
                                            strlen(key_info->name),cs));
 
15934
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
 
15935
                keylen_str_buf;
 
15936
        item_list.push_back(new Item_string(keylen_str_buf,
 
15937
                                            length,
 
15938
                                            system_charset_info));
 
15939
        item_list.push_back(item_null);
 
15940
      }
 
15941
      else if (tab->select && tab->select->quick)
 
15942
      {
 
15943
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15944
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15945
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15946
        item_list.push_back(item_null);
 
15947
      }
 
15948
      else
 
15949
      {
 
15950
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15951
        {
 
15952
          const char *tmp_buff;
 
15953
          int f_idx;
 
15954
          if (table_list->has_db_lookup_value)
 
15955
          {
 
15956
            f_idx= table_list->schema_table->idx_field1;
 
15957
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15958
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15959
          }
 
15960
          if (table_list->has_table_lookup_value)
 
15961
          {
 
15962
            if (table_list->has_db_lookup_value)
 
15963
              tmp2.append(',');
 
15964
            f_idx= table_list->schema_table->idx_field2;
 
15965
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15966
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15967
          }
 
15968
          if (tmp2.length())
 
15969
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15970
          else
 
15971
            item_list.push_back(item_null);
 
15972
        }
 
15973
        else
 
15974
          item_list.push_back(item_null);
 
15975
        item_list.push_back(item_null);
 
15976
        item_list.push_back(item_null);
 
15977
      }
 
15978
 
 
15979
      /* Add "rows" field to item_list. */
 
15980
      if (table_list->schema_table)
 
15981
      {
 
15982
        /* in_rows */
 
15983
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15984
          item_list.push_back(item_null);
 
15985
        /* rows */
 
15986
        item_list.push_back(item_null);
 
15987
      }
 
15988
      else
 
15989
      {
 
15990
        double examined_rows;
 
15991
        if (tab->select && tab->select->quick)
 
15992
          examined_rows= rows2double(tab->select->quick->records);
 
15993
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15994
          examined_rows= rows2double(tab->limit ? tab->limit :
 
15995
                                     tab->table->file->records());
 
15996
        else
 
15997
          examined_rows= join->best_positions[i].records_read;
 
15998
 
 
15999
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
 
16000
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
16001
 
 
16002
        /* Add "filtered" field to item_list. */
 
16003
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
16004
        {
 
16005
          float f= 0.0;
 
16006
          if (examined_rows)
 
16007
            f= (float) (100.0 * join->best_positions[i].records_read /
 
16008
                        examined_rows);
 
16009
          item_list.push_back(new Item_float(f, 2));
 
16010
        }
 
16011
      }
 
16012
 
 
16013
      /* Build "Extra" field and add it to item_list. */
 
16014
      bool key_read=table->key_read;
 
16015
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
16016
          table->covering_keys.is_set(tab->index))
 
16017
        key_read=1;
 
16018
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
16019
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
16020
        key_read=1;
 
16021
 
 
16022
      if (tab->info)
 
16023
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16024
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16025
      {
 
16026
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16027
          extra.append(STRING_WITH_LEN("; Using index"));
 
16028
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16029
          extra.append(STRING_WITH_LEN("; Using where"));
 
16030
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16031
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16032
        /* Skip initial "; "*/
 
16033
        const char *str= extra.ptr();
 
16034
        uint32_t len= extra.length();
 
16035
        if (len)
 
16036
        {
 
16037
          str += 2;
 
16038
          len -= 2;
 
16039
        }
 
16040
        item_list.push_back(new Item_string(str, len, cs));
 
16041
      }
 
16042
      else
 
16043
      {
 
16044
        uint32_t keyno= MAX_KEY;
 
16045
        if (tab->ref.key_parts)
 
16046
          keyno= tab->ref.key;
 
16047
        else if (tab->select && tab->select->quick)
 
16048
          keyno = tab->select->quick->index;
 
16049
 
 
16050
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16051
            table->file->pushed_idx_cond)
 
16052
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16053
 
 
16054
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
16055
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16056
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16057
        {
 
16058
          extra.append(STRING_WITH_LEN("; Using "));
 
16059
          tab->select->quick->add_info_string(&extra);
 
16060
        }
 
16061
          if (tab->select)
 
16062
        {
 
16063
          if (tab->use_quick == 2)
 
16064
          {
 
16065
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16066
            char buf[MAX_KEY / 4 + 1];
 
16067
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16068
                                         "record (index map: 0x"));
 
16069
            extra.append(tab->keys.print(buf));
 
16070
            extra.append(')');
 
16071
          }
 
16072
          else if (tab->select->cond)
 
16073
          {
 
16074
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16075
 
 
16076
            if (session->variables.engine_condition_pushdown && pushed_cond)
 
16077
            {
 
16078
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16079
                                           "condition"));
 
16080
              if (session->lex->describe & DESCRIBE_EXTENDED)
 
16081
              {
 
16082
                extra.append(STRING_WITH_LEN(": "));
 
16083
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16084
              }
 
16085
            }
 
16086
            else
 
16087
              extra.append(STRING_WITH_LEN("; Using where"));
 
16088
          }
 
16089
        }
 
16090
        if (key_read)
 
16091
        {
 
16092
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16093
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16094
          else
 
16095
            extra.append(STRING_WITH_LEN("; Using index"));
 
16096
        }
 
16097
        if (table->reginfo.not_exists_optimize)
 
16098
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16099
 
 
16100
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16101
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16102
             HA_MRR_USE_DEFAULT_IMPL))
 
16103
        {
 
16104
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16105
        }
 
16106
 
 
16107
        if (table_list->schema_table &&
 
16108
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16109
        {
 
16110
          if (!table_list->table_open_method)
 
16111
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16112
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16113
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16114
          else
 
16115
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16116
          if (table_list->has_db_lookup_value &&
 
16117
              table_list->has_table_lookup_value)
 
16118
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16119
          else if (table_list->has_db_lookup_value ||
 
16120
                   table_list->has_table_lookup_value)
 
16121
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16122
          else
 
16123
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16124
        }
 
16125
        if (need_tmp_table)
 
16126
        {
 
16127
          need_tmp_table=0;
 
16128
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16129
        }
 
16130
        if (need_order)
 
16131
        {
 
16132
          need_order=0;
 
16133
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16134
        }
 
16135
        if (distinct & test_all_bits(used_tables,session->used_tables))
 
16136
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16137
 
 
16138
        if (tab->insideout_match_tab)
 
16139
        {
 
16140
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16141
        }
 
16142
 
 
16143
        if (tab->flush_weedout_table)
 
16144
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16145
        else if (tab->check_weed_out_table)
 
16146
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16147
        else if (tab->do_firstmatch)
 
16148
        {
 
16149
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16150
          Table *prev_table=tab->do_firstmatch->table;
 
16151
          if (prev_table->derived_select_number)
 
16152
          {
 
16153
            char namebuf[NAME_LEN];
 
16154
            /* Derived table name generation */
 
16155
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16156
                              "<derived%u>",
 
16157
                              prev_table->derived_select_number);
 
16158
            extra.append(namebuf, len);
 
16159
          }
 
16160
          else
 
16161
            extra.append(prev_table->pos_in_table_list->alias);
 
16162
          extra.append(STRING_WITH_LEN(")"));
 
16163
        }
 
16164
 
 
16165
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16166
        {
 
16167
          if (tab->ref.cond_guards[part])
 
16168
          {
 
16169
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16170
            break;
 
16171
          }
 
16172
        }
 
16173
 
 
16174
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16175
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16176
 
 
16177
        /* Skip initial "; "*/
 
16178
        const char *str= extra.ptr();
 
16179
        uint32_t len= extra.length();
 
16180
        if (len)
 
16181
        {
 
16182
          str += 2;
 
16183
          len -= 2;
 
16184
        }
 
16185
        item_list.push_back(new Item_string(str, len, cs));
 
16186
      }
 
16187
      // For next iteration
 
16188
      used_tables|=table->map;
 
16189
      if (result->send_data(item_list))
 
16190
        join->error= 1;
 
16191
    }
 
16192
  }
 
16193
  for (Select_Lex_Unit *unit= join->select_lex->first_inner_unit();
 
16194
       unit;
 
16195
       unit= unit->next_unit())
 
16196
  {
 
16197
    if (mysql_explain_union(session, unit, result))
 
16198
      return;
 
16199
  }
 
16200
  return;
 
16201
}
 
16202
 
 
16203
 
 
16204
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit, select_result *result)
 
16205
{
 
16206
  bool res= false;
 
16207
  Select_Lex *first= unit->first_select();
 
16208
 
 
16209
  for (Select_Lex *sl= first;
 
16210
       sl;
 
16211
       sl= sl->next_select())
 
16212
  {
 
16213
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16214
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16215
    sl->type= (((&session->lex->select_lex)==sl)?
 
16216
               (sl->first_inner_unit() || sl->next_select() ?
 
16217
                "PRIMARY" : "SIMPLE"):
 
16218
               ((sl == first)?
 
16219
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16220
                 "DERIVED":
 
16221
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16222
                  "DEPENDENT SUBQUERY":
 
16223
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16224
                   "SUBQUERY"))):
 
16225
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16226
                 "DEPENDENT UNION":
 
16227
                 uncacheable?"UNCACHEABLE UNION":
 
16228
                 "UNION")));
 
16229
    sl->options|= SELECT_DESCRIBE;
 
16230
  }
 
16231
  if (unit->is_union())
 
16232
  {
 
16233
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16234
    unit->fake_select_lex->type= "UNION RESULT";
 
16235
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16236
    if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16237
      res= unit->exec();
 
16238
    res|= unit->cleanup();
 
16239
  }
 
16240
  else
 
16241
  {
 
16242
    session->lex->current_select= first;
 
16243
    unit->set_limit(unit->global_parameters);
 
16244
    res= mysql_select(session, &first->ref_pointer_array,
 
16245
                        (TableList*) first->table_list.first,
 
16246
                        first->with_wild, first->item_list,
 
16247
                        first->where,
 
16248
                        first->order_list.elements +
 
16249
                        first->group_list.elements,
 
16250
                        (order_st*) first->order_list.first,
 
16251
                        (order_st*) first->group_list.first,
 
16252
                        first->having,
 
16253
                        (order_st*) session->lex->proc_list.first,
 
16254
                        first->options | session->options | SELECT_DESCRIBE,
 
16255
                        result, unit, first);
 
16256
  }
 
16257
  return(res || session->is_error());
 
16258
}
 
16259
 
 
16260
 
6257
16261
static void print_table_array(Session *session, String *str, TableList **table,
6258
16262
                              TableList **end)
6259
16263
{
6269
16273
    }
6270
16274
    else if (curr->straight)
6271
16275
      str->append(STRING_WITH_LEN(" straight_join "));
 
16276
    else if (curr->sj_inner_tables)
 
16277
      str->append(STRING_WITH_LEN(" semi join "));
6272
16278
    else
6273
16279
      str->append(STRING_WITH_LEN(" join "));
6274
16280
    curr->print(session, str, QT_ORDINARY);
6281
16287
  }
6282
16288
}
6283
16289
 
 
16290
 
6284
16291
/**
6285
16292
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
16293
  @param session     thread handler
6287
16294
  @param str     string where table should be printed
6288
16295
  @param tables  list of tables in join
6289
16296
  @query_type    type of the query is being generated
6290
16297
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
16298
 
 
16299
static void print_join(Session *session, String *str,
 
16300
                       List<TableList> *tables, enum_query_type)
6293
16301
{
6294
16302
  /* List is reversed => we should reverse it before using */
6295
 
  List<TableList>::iterator ti(tables->begin());
6296
 
  TableList **table= (TableList **)session->getMemRoot()->allocate(sizeof(TableList*) *
 
16303
  List_iterator_fast<TableList> ti(*tables);
 
16304
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
16305
                                                tables->elements);
6298
16306
  if (table == 0)
6299
16307
    return;  // out of memory
6300
16308
 
6301
16309
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6302
16310
    *t= ti++;
 
16311
 
 
16312
  /*
 
16313
    If the first table is a semi-join nest, swap it with something that is
 
16314
    not a semi-join nest.
 
16315
  */
 
16316
  if ((*table)->sj_inner_tables)
 
16317
  {
 
16318
    TableList **end= table + tables->elements;
 
16319
    for (TableList **t2= table; t2!=end; t2++)
 
16320
    {
 
16321
      if (!(*t2)->sj_inner_tables)
 
16322
      {
 
16323
        TableList *tmp= *t2;
 
16324
        *t2= *table;
 
16325
        *table= tmp;
 
16326
        break;
 
16327
      }
 
16328
    }
 
16329
  }
6303
16330
  assert(tables->elements >= 1);
6304
16331
  print_table_array(session, str, table, table + tables->elements);
6305
16332
}
6306
16333
 
 
16334
 
 
16335
/**
 
16336
  @brief Print an index hint
 
16337
 
 
16338
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16339
 
 
16340
  @param      session         the current thread
 
16341
  @param[out] str         appends the index hint here
 
16342
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16343
                          "FORCE INDEX"|"IGNORE INDEX")
 
16344
  @param      hint_length the length of the string in 'hint'
 
16345
  @param      indexes     a list of index names for the hint
 
16346
*/
 
16347
 
 
16348
void
 
16349
Index_hint::print(Session *session, String *str)
 
16350
{
 
16351
  switch (type)
 
16352
  {
 
16353
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16354
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16355
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16356
  }
 
16357
  str->append (STRING_WITH_LEN(" ("));
 
16358
  if (key_name.length)
 
16359
  {
 
16360
    if (session && is_primary_key_name(key_name.str))
 
16361
      str->append(is_primary_key_name(key_name.str));
 
16362
    else
 
16363
      str->append_identifier(key_name.str, key_name.length);
 
16364
  }
 
16365
  str->append(')');
 
16366
}
 
16367
 
 
16368
 
 
16369
/**
 
16370
  Print table as it should be in join list.
 
16371
 
 
16372
  @param str   string where table should be printed
 
16373
*/
 
16374
 
 
16375
void TableList::print(Session *session, String *str, enum_query_type query_type)
 
16376
{
 
16377
  if (nested_join)
 
16378
  {
 
16379
    str->append('(');
 
16380
    print_join(session, str, &nested_join->join_list, query_type);
 
16381
    str->append(')');
 
16382
  }
 
16383
  else
 
16384
  {
 
16385
    const char *cmp_name;                         // Name to compare with alias
 
16386
    if (derived)
 
16387
    {
 
16388
      // A derived table
 
16389
      str->append('(');
 
16390
      derived->print(str, query_type);
 
16391
      str->append(')');
 
16392
      cmp_name= "";                               // Force printing of alias
 
16393
    }
 
16394
    else
 
16395
    {
 
16396
      // A normal table
 
16397
      {
 
16398
        str->append_identifier(db, db_length);
 
16399
        str->append('.');
 
16400
      }
 
16401
      if (schema_table)
 
16402
      {
 
16403
        str->append_identifier(schema_table_name, strlen(schema_table_name));
 
16404
        cmp_name= schema_table_name;
 
16405
      }
 
16406
      else
 
16407
      {
 
16408
        str->append_identifier(table_name, table_name_length);
 
16409
        cmp_name= table_name;
 
16410
      }
 
16411
    }
 
16412
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16413
    {
 
16414
 
 
16415
      if (alias && alias[0])
 
16416
      {
 
16417
        str->append(' ');
 
16418
 
 
16419
        string t_alias(alias);
 
16420
        if (lower_case_table_names== 1)
 
16421
          transform(t_alias.begin(), t_alias.end(),
 
16422
                    t_alias.begin(), ::tolower);
 
16423
 
 
16424
        str->append_identifier(t_alias.c_str(), t_alias.length());
 
16425
      }
 
16426
 
 
16427
    }
 
16428
 
 
16429
    if (index_hints)
 
16430
    {
 
16431
      List_iterator<Index_hint> it(*index_hints);
 
16432
      Index_hint *hint;
 
16433
 
 
16434
      while ((hint= it++))
 
16435
      {
 
16436
        str->append (STRING_WITH_LEN(" "));
 
16437
        hint->print (session, str);
 
16438
      }
 
16439
    }
 
16440
  }
 
16441
}
 
16442
 
 
16443
 
6307
16444
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6308
16445
{
6309
16446
  /* QQ: session may not be set for sub queries, but this should be fixed */
6310
 
  if(not session)
 
16447
  if (!session)
6311
16448
    session= current_session;
6312
16449
 
6313
 
 
6314
16450
  str->append(STRING_WITH_LEN("select "));
6315
16451
 
6316
16452
  /* First add options */
6317
16453
  if (options & SELECT_STRAIGHT_JOIN)
6318
16454
    str->append(STRING_WITH_LEN("straight_join "));
 
16455
  if ((session->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16456
      (this == &session->lex->select_lex))
 
16457
    str->append(STRING_WITH_LEN("high_priority "));
6319
16458
  if (options & SELECT_DISTINCT)
6320
16459
    str->append(STRING_WITH_LEN("distinct "));
6321
16460
  if (options & SELECT_SMALL_RESULT)
6329
16468
 
6330
16469
  //Item List
6331
16470
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
16471
  List_iterator_fast<Item> it(item_list);
6333
16472
  Item *item;
6334
16473
  while ((item= it++))
6335
16474
  {
6342
16481
 
6343
16482
  /*
6344
16483
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
16484
    TODO: support USING/FORCE/IGNORE index
6346
16485
  */
6347
16486
  if (table_list.elements)
6348
16487
  {
6376
16515
  if (group_list.elements)
6377
16516
  {
6378
16517
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
16518
    print_order(str, (order_st *) group_list.first, query_type);
6380
16519
    switch (olap)
6381
16520
    {
6382
16521
      case CUBE_TYPE:
6407
16546
  if (order_list.elements)
6408
16547
  {
6409
16548
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
16549
    print_order(str, (order_st *) order_list.first, query_type);
6411
16550
  }
6412
16551
 
6413
16552
  // limit
6416
16555
  // PROCEDURE unsupported here
6417
16556
}
6418
16557
 
 
16558
 
 
16559
/**
 
16560
  change select_result object of JOIN.
 
16561
 
 
16562
  @param res            new select_result object
 
16563
 
 
16564
  @retval
 
16565
    false   OK
 
16566
  @retval
 
16567
    true    error
 
16568
*/
 
16569
 
 
16570
bool JOIN::change_result(select_result *res)
 
16571
{
 
16572
  result= res;
 
16573
  if (result->prepare(fields_list, select_lex->master_unit()))
 
16574
  {
 
16575
    return(true);
 
16576
  }
 
16577
  return(false);
 
16578
}
 
16579
 
6419
16580
/**
6420
16581
  @} (end of group Query_Optimizer)
6421
16582
*/
6422
 
 
6423
 
} /* namespace drizzled */