~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Monty Taylor
  • Date: 2009-03-03 07:39:39 UTC
  • mto: This revision was merged to the branch mainline in revision 910.
  • Revision ID: mordred@inaugust.com-20090303073939-rfswfdo68klfcp1o
Updated comment version indicators to handle drizzle versions.

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>
 
51
#include CMATH_H
 
52
 
 
53
#if defined(CMATH_NAMESPACE)
 
54
using namespace CMATH_NAMESPACE;
 
55
#endif
71
56
 
72
57
using namespace std;
73
58
 
74
 
namespace drizzled
75
 
{
76
 
 
77
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
 
59
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
60
                              "MAYBE_REF","ALL","range","index",
 
61
                              "ref_or_null","unique_subquery","index_subquery",
 
62
                              "index_merge"
 
63
};
 
64
 
 
65
struct st_sargable_param;
 
66
 
 
67
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
68
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
69
                                 DYNAMIC_ARRAY *keyuse);
 
70
static bool update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,
 
71
                                JOIN_TAB *join_tab,
 
72
                                uint32_t tables, COND *conds,
 
73
                                COND_EQUAL *cond_equal,
 
74
                                table_map table_map, Select_Lex *select_lex,
 
75
                                st_sargable_param **sargables);
 
76
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
77
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
78
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
79
                               table_map used_tables);
 
80
static bool choose_plan(JOIN *join,table_map join_tables);
 
81
 
 
82
static void best_access_path(JOIN *join, JOIN_TAB *s, Session *session,
 
83
                             table_map remaining_tables, uint32_t idx,
 
84
                             double record_count, double read_time);
 
85
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
86
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
87
                             uint32_t depth, uint32_t prune_level);
 
88
static bool best_extension_by_limited_search(JOIN *join,
 
89
                                             table_map remaining_tables,
 
90
                                             uint32_t idx, double record_count,
 
91
                                             double read_time, uint32_t depth,
 
92
                                             uint32_t prune_level);
 
93
static uint32_t determine_search_depth(JOIN* join);
 
94
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
 
95
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
96
/*
 
97
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
98
  tested and approved.
 
99
*/
 
100
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
101
                      double record_count,double read_time);
 
102
static uint32_t cache_record_length(JOIN *join,uint32_t index);
 
103
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
104
static bool get_best_combination(JOIN *join);
 
105
static store_key *get_store_key(Session *session,
 
106
                                KEYUSE *keyuse, table_map used_tables,
 
107
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
 
108
                                uint32_t maybe_null);
 
109
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
110
static void make_outerjoin_info(JOIN *join);
 
111
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
112
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
113
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
114
static void update_depend_map(JOIN *join);
 
115
static void update_depend_map(JOIN *join, order_st *order);
 
116
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
117
                           bool change_list, bool *simple_order);
 
118
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
119
                            List<Item> &fields, bool send_row,
 
120
                            uint64_t select_options, const char *info,
 
121
                            Item *having);
78
122
static COND *build_equal_items(Session *session, COND *cond,
79
123
                               COND_EQUAL *inherited,
80
124
                               List<TableList> *join_list,
81
125
                               COND_EQUAL **cond_equal_ref);
82
 
 
 
126
static COND* substitute_for_best_equal_field(COND *cond,
 
127
                                             COND_EQUAL *cond_equal,
 
128
                                             void *table_join_idx);
 
129
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
130
                            COND *conds, bool top, bool in_sj);
 
131
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
132
static void restore_prev_nj_state(JOIN_TAB *last);
 
133
static void reset_nj_counters(List<TableList> *join_list);
 
134
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
135
                                          uint32_t first_unused);
 
136
 
 
137
static
 
138
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
139
static void restore_prev_sj_state(const table_map remaining_tables,
 
140
                                  const JOIN_TAB *tab);
 
141
 
 
142
static COND *optimize_cond(JOIN *join, COND *conds,
 
143
                           List<TableList> *join_list,
 
144
                           Item::cond_result *cond_value);
 
145
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
146
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
147
 
 
148
static enum_nested_loop_state
 
149
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
150
                     int error);
 
151
static enum_nested_loop_state
 
152
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
153
static enum_nested_loop_state
 
154
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
155
static enum_nested_loop_state
 
156
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
157
static enum_nested_loop_state
 
158
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
159
static enum_nested_loop_state
 
160
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
161
static enum_nested_loop_state
 
162
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
163
 
 
164
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
165
static int join_read_system(JOIN_TAB *tab);
 
166
static int join_read_const(JOIN_TAB *tab);
 
167
static int join_read_key(JOIN_TAB *tab);
 
168
static int join_read_always_key(JOIN_TAB *tab);
 
169
static int join_read_last_key(JOIN_TAB *tab);
 
170
static int join_no_more_records(READ_RECORD *info);
 
171
static int join_read_next(READ_RECORD *info);
 
172
static int join_read_next_different(READ_RECORD *info);
 
173
static int join_init_quick_read_record(JOIN_TAB *tab);
 
174
static int test_if_quick_select(JOIN_TAB *tab);
 
175
static int join_init_read_record(JOIN_TAB *tab);
 
176
static int join_read_first(JOIN_TAB *tab);
 
177
static int join_read_next_same(READ_RECORD *info);
 
178
static int join_read_next_same_diff(READ_RECORD *info);
 
179
static int join_read_last(JOIN_TAB *tab);
 
180
static int join_read_prev_same(READ_RECORD *info);
 
181
static int join_read_prev(READ_RECORD *info);
 
182
int join_read_always_key_or_null(JOIN_TAB *tab);
 
183
int join_read_next_same_or_null(READ_RECORD *info);
 
184
static COND *make_cond_for_table(COND *cond,table_map table,
 
185
                                 table_map used_table,
 
186
                                 bool exclude_expensive_cond);
83
187
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);
 
188
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
189
                                    ha_rows select_limit, bool no_changes,
 
190
                                    const key_map *map);
 
191
static bool list_contains_unique_index(Table *table,
 
192
                          bool (*find_func) (Field *, void *), void *data);
 
193
static bool find_field_in_item_list (Field *field, void *data);
 
194
static bool find_field_in_order_list (Field *field, void *data);
 
195
static int create_sort_index(Session *session, JOIN *join, order_st *order,
 
196
                             ha_rows filesort_limit, ha_rows select_limit,
 
197
                             bool is_order_by);
 
198
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
199
                             Item *having);
 
200
static int remove_dup_with_compare(Session *session, Table *entry, Field **field,
 
201
                                   uint32_t offset, Item *having);
 
202
static int remove_dup_with_hash_index(Session *session,Table *table,
 
203
                                      uint32_t field_count, Field **first_field,
 
204
                                      uint32_t key_length, Item *having);
 
205
static int join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count);
 
206
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
207
static bool store_record_in_cache(JOIN_CACHE *cache);
 
208
static void reset_cache_read(JOIN_CACHE *cache);
 
209
static void reset_cache_write(JOIN_CACHE *cache);
 
210
static void read_cached_record(JOIN_TAB *tab);
 
211
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
212
static order_st *create_distinct_group(Session *session, Item **ref_pointer_array,
 
213
                                    order_st *order, List<Item> &fields,
 
214
                                    List<Item> &all_fields,
 
215
                                    bool *all_order_by_fields_used);
 
216
static bool test_if_subpart(order_st *a,order_st *b);
 
217
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
218
static void calc_group_buffer(JOIN *join,order_st *group);
 
219
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
220
static bool alloc_group_fields(JOIN *join,order_st *group);
 
221
// Create list for using with tempory table
 
222
static bool change_to_use_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
// Create list for using with tempory table
 
227
static bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
228
                                      List<Item> &new_list1,
 
229
                                      List<Item> &new_list2,
 
230
                                      uint32_t elements, List<Item> &items);
 
231
static void init_tmptable_sum_functions(Item_sum **func);
 
232
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
233
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
234
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
 
235
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
 
236
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
237
static bool update_sum_func(Item_sum **func);
 
238
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
239
                            bool distinct, const char *message=NULL);
 
240
static Item *remove_additional_cond(Item* conds);
 
241
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
242
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
243
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
244
                                       Item *new_cond, bool fix_fields);
92
245
 
93
246
static bool eval_const_cond(COND *cond)
94
247
{
95
248
    return ((Item_func*) cond)->val_int() ? true : false;
96
249
}
97
250
 
 
251
 
98
252
/*
99
253
  This is used to mark equalities that were made from i-th IN-equality.
100
254
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
103
257
const char *subq_sj_cond_name=
104
258
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
259
 
106
 
static bool copy_blobs(Field **ptr)
 
260
static bool bitmap_covers(const table_map x, const table_map y)
107
261
{
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;
 
262
  return !test(y & ~x);
115
263
}
116
264
 
117
265
/**
118
266
  This handles SELECT with and without UNION.
119
267
*/
 
268
 
120
269
bool handle_select(Session *session, LEX *lex, select_result *result,
121
270
                   uint64_t setup_tables_done_option)
122
271
{
123
272
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
273
  register Select_Lex *select_lex = &lex->select_lex;
 
274
  DRIZZLE_SELECT_START();
126
275
 
127
276
  if (select_lex->master_unit()->is_union() ||
128
277
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
278
    res= mysql_union(session, lex, result, &lex->unit, setup_tables_done_option);
133
279
  else
134
280
  {
135
281
    Select_Lex_Unit *unit= &lex->unit;
136
282
    unit->set_limit(unit->global_parameters);
137
283
    session->session_marker= 0;
138
284
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
 
285
      'options' of mysql_select will be set in JOIN, as far as JOIN for
140
286
      every PS/SP execution new, we will not need reset this flag if
141
287
      setup_tables_done_option changed for next rexecution
142
288
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
 
289
    res= mysql_select(session, &select_lex->ref_pointer_array,
145
290
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
291
                      select_lex->with_wild, select_lex->item_list,
148
292
                      select_lex->where,
149
293
                      select_lex->order_list.elements +
150
294
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
295
                      (order_st*) select_lex->order_list.first,
 
296
                      (order_st*) select_lex->group_list.first,
153
297
                      select_lex->having,
 
298
                      (order_st*) lex->proc_list.first,
154
299
                      select_lex->options | session->options |
155
300
                      setup_tables_done_option,
156
301
                      result, unit, select_lex);
159
304
  if (unlikely(res))
160
305
    result->abort();
161
306
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
307
  DRIZZLE_SELECT_END();
 
308
  return(res);
164
309
}
165
310
 
 
311
 
166
312
/*
167
313
  Fix fields referenced from inner selects.
168
314
 
203
349
    true  an error occured
204
350
    false ok
205
351
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
352
 
 
353
bool
 
354
fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
 
355
                 Item **ref_pointer_array)
210
356
{
211
357
  Item_outer_ref *ref;
212
358
  bool res= false;
213
359
  bool direct_ref= false;
214
360
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
361
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
362
  while ((ref= ref_it++))
217
363
  {
218
364
    Item *item= ref->outer_ref;
219
365
    Item **item_ref= ref->ref;
220
366
    Item_ref *new_ref;
221
367
    /*
222
 
      @todo this field item already might be present in the select list.
 
368
      TODO: this field item already might be present in the select list.
223
369
      In this case instead of adding new field item we could use an
224
370
      existing one. The change will lead to less operations for copying fields,
225
371
      smaller temporary tables and less data passed through filesort.
273
419
  return res;
274
420
}
275
421
 
 
422
/**
 
423
  Function to setup clauses without sum functions.
 
424
*/
 
425
inline int setup_without_group(Session *session, Item **ref_pointer_array,
 
426
                               TableList *tables,
 
427
                               TableList *leaves,
 
428
                               List<Item> &fields,
 
429
                               List<Item> &all_fields,
 
430
                               COND **conds,
 
431
                               order_st *order,
 
432
                               order_st *group, bool *hidden_group_fields)
 
433
{
 
434
  int res;
 
435
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
436
 
 
437
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
438
  res= setup_conds(session, tables, leaves, conds);
 
439
 
 
440
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
441
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
 
442
                          order);
 
443
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
444
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
 
445
                          group, hidden_group_fields);
 
446
  session->lex->allow_sum_func= save_allow_sum_func;
 
447
  return(res);
 
448
}
 
449
 
276
450
/*****************************************************************************
277
451
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
452
  mysql_select assumes that all tables are already opened
279
453
*****************************************************************************/
280
454
 
 
455
/**
 
456
  Prepare of whole select (including sub queries in future).
 
457
 
 
458
  @todo
 
459
    Add check of calculation of GROUP functions and fields:
 
460
    SELECT COUNT(*)+table.col1 from table1;
 
461
 
 
462
  @retval
 
463
    -1   on error
 
464
  @retval
 
465
    0   on success
 
466
*/
 
467
int
 
468
JOIN::prepare(Item ***rref_pointer_array,
 
469
              TableList *tables_init,
 
470
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
471
              order_st *order_init, order_st *group_init,
 
472
              Item *having_init,
 
473
              order_st *proc_param_init, Select_Lex *select_lex_arg,
 
474
              Select_Lex_Unit *unit_arg)
 
475
{
 
476
  // to prevent double initialization on EXPLAIN
 
477
  if (optimized)
 
478
    return(0);
 
479
 
 
480
  conds= conds_init;
 
481
  order= order_init;
 
482
  group_list= group_init;
 
483
  having= having_init;
 
484
  proc_param= proc_param_init;
 
485
  tables_list= tables_init;
 
486
  select_lex= select_lex_arg;
 
487
  select_lex->join= this;
 
488
  join_list= &select_lex->top_join_list;
 
489
  union_part= unit_arg->is_union();
 
490
 
 
491
  session->lex->current_select->is_item_list_lookup= 1;
 
492
  /*
 
493
    If we have already executed SELECT, then it have not sense to prevent
 
494
    its table from update (see unique_table())
 
495
  */
 
496
  if (session->derived_tables_processing)
 
497
    select_lex->exclude_from_table_unique_test= true;
 
498
 
 
499
  /* Check that all tables, fields, conds and order are ok */
 
500
 
 
501
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
502
      setup_tables_and_check_access(session, &select_lex->context, join_list,
 
503
                                    tables_list, &select_lex->leaf_tables,
 
504
                                    false))
 
505
      return(-1);
 
506
 
 
507
  TableList *table_ptr;
 
508
  for (table_ptr= select_lex->leaf_tables;
 
509
       table_ptr;
 
510
       table_ptr= table_ptr->next_leaf)
 
511
    tables++;
 
512
 
 
513
  if (setup_wild(session, tables_list, fields_list, &all_fields, wild_num) ||
 
514
      select_lex->setup_ref_array(session, og_num) ||
 
515
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
516
                   &all_fields, 1) ||
 
517
      setup_without_group(session, (*rref_pointer_array), tables_list,
 
518
                          select_lex->leaf_tables, fields_list,
 
519
                          all_fields, &conds, order, group_list,
 
520
                          &hidden_group_fields))
 
521
    return(-1);                         /* purecov: inspected */
 
522
 
 
523
  ref_pointer_array= *rref_pointer_array;
 
524
 
 
525
  if (having)
 
526
  {
 
527
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
528
    session->where="having clause";
 
529
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
530
    select_lex->having_fix_field= 1;
 
531
    bool having_fix_rc= (!having->fixed &&
 
532
                         (having->fix_fields(session, &having) ||
 
533
                          having->check_cols(1)));
 
534
    select_lex->having_fix_field= 0;
 
535
    if (having_fix_rc || session->is_error())
 
536
      return(-1);                               /* purecov: inspected */
 
537
    session->lex->allow_sum_func= save_allow_sum_func;
 
538
  }
 
539
 
 
540
  {
 
541
    Item_subselect *subselect;
 
542
    Item_in_subselect *in_subs= NULL;
 
543
    /*
 
544
      Are we in a subquery predicate?
 
545
      TODO: the block below will be executed for every PS execution without need.
 
546
    */
 
547
    if ((subselect= select_lex->master_unit()->item))
 
548
    {
 
549
      bool do_semijoin= !test(session->variables.optimizer_switch &
 
550
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
551
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
552
        in_subs= (Item_in_subselect*)subselect;
 
553
 
 
554
      /*
 
555
        Check if we're in subquery that is a candidate for flattening into a
 
556
        semi-join (which is done done in flatten_subqueries()). The
 
557
        requirements are:
 
558
          1. Subquery predicate is an IN/=ANY subq predicate
 
559
          2. Subquery is a single SELECT (not a UNION)
 
560
          3. Subquery does not have GROUP BY or order_st BY
 
561
          4. Subquery does not use aggregate functions or HAVING
 
562
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
563
          6. No execution method was already chosen (by a prepared statement).
 
564
 
 
565
          (*). We are not in a subquery of a single table UPDATE/DELETE that
 
566
               doesn't have a JOIN (TODO: We should handle this at some
 
567
               point by switching to multi-table UPDATE/DELETE)
 
568
 
 
569
          (**). We're not in a confluent table-less subquery, like
 
570
                "SELECT 1".
 
571
      */
 
572
      if (in_subs &&                                                    // 1
 
573
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
574
          !select_lex->group_list.elements && !order &&                 // 3
 
575
          !having && !select_lex->with_sum_func &&                      // 4
 
576
          session->session_marker &&                                            // 5
 
577
          select_lex->outer_select()->join &&                           // (*)
 
578
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
 
579
          do_semijoin &&
 
580
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
581
      {
 
582
        {
 
583
          if (!in_subs->left_expr->fixed &&
 
584
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
 
585
          {
 
586
            return(-1);
 
587
          }
 
588
          /*
 
589
            Check that the right part of the subselect contains no more than one
 
590
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
591
          */
 
592
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
593
             (select_lex->item_list.elements !=
 
594
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
595
          {
 
596
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
597
            return(-1);
 
598
          }
 
599
        }
 
600
 
 
601
        /* Register the subquery for further processing */
 
602
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
 
603
        in_subs->expr_join_nest= (TableList*)session->session_marker;
 
604
      }
 
605
      else
 
606
      {
 
607
        bool do_materialize= !test(session->variables.optimizer_switch &
 
608
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
609
        /*
 
610
          Check if the subquery predicate can be executed via materialization.
 
611
          The required conditions are:
 
612
          1. Subquery predicate is an IN/=ANY subq predicate
 
613
          2. Subquery is a single SELECT (not a UNION)
 
614
          3. Subquery is not a table-less query. In this case there is no
 
615
             point in materializing.
 
616
          4. Subquery predicate is a top-level predicate
 
617
             (this implies it is not negated)
 
618
             TODO: this is a limitation that should be lifeted once we
 
619
             implement correct NULL semantics (WL#3830)
 
620
          5. Subquery is non-correlated
 
621
             TODO:
 
622
             This is an overly restrictive condition. It can be extended to:
 
623
             (Subquery is non-correlated ||
 
624
              Subquery is correlated to any query outer to IN predicate ||
 
625
              (Subquery is correlated to the immediate outer query &&
 
626
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
627
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
628
          6. No execution method was already chosen (by a prepared statement).
 
629
 
 
630
          (*) The subquery must be part of a SELECT statement. The current
 
631
               condition also excludes multi-table update statements.
 
632
 
 
633
          We have to determine whether we will perform subquery materialization
 
634
          before calling the IN=>EXISTS transformation, so that we know whether to
 
635
          perform the whole transformation or only that part of it which wraps
 
636
          Item_in_subselect in an Item_in_optimizer.
 
637
        */
 
638
        if (do_materialize &&
 
639
            in_subs  &&                                                   // 1
 
640
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
641
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
642
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
643
        {
 
644
          if (in_subs->is_top_level_item() &&                             // 4
 
645
              !in_subs->is_correlated &&                                  // 5
 
646
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
647
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
648
        }
 
649
 
 
650
        Item_subselect::trans_res trans_res;
 
651
        if ((trans_res= subselect->select_transformer(this)) !=
 
652
            Item_subselect::RES_OK)
 
653
        {
 
654
          return((trans_res == Item_subselect::RES_ERROR));
 
655
        }
 
656
      }
 
657
    }
 
658
  }
 
659
 
 
660
  if (order)
 
661
  {
 
662
    order_st *ord;
 
663
    for (ord= order; ord; ord= ord->next)
 
664
    {
 
665
      Item *item= *ord->item;
 
666
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
667
        item->split_sum_func(session, ref_pointer_array, all_fields);
 
668
    }
 
669
  }
 
670
 
 
671
  if (having && having->with_sum_func)
 
672
    having->split_sum_func(session, ref_pointer_array, all_fields,
 
673
                           &having, true);
 
674
  if (select_lex->inner_sum_func_list)
 
675
  {
 
676
    Item_sum *end=select_lex->inner_sum_func_list;
 
677
    Item_sum *item_sum= end;
 
678
    do
 
679
    {
 
680
      item_sum= item_sum->next;
 
681
      item_sum->split_sum_func(session, ref_pointer_array,
 
682
                               all_fields, item_sum->ref_by, false);
 
683
    } while (item_sum != end);
 
684
  }
 
685
 
 
686
  if (select_lex->inner_refs_list.elements &&
 
687
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
 
688
    return(-1);
 
689
 
 
690
  /*
 
691
    Check if there are references to un-aggregated columns when computing
 
692
    aggregate functions with implicit grouping (there is no GROUP BY).
 
693
 
 
694
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
695
  */
 
696
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
697
  {
 
698
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
699
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
700
    return(-1);
 
701
  }
 
702
  {
 
703
    /* Caclulate the number of groups */
 
704
    send_group_parts= 0;
 
705
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
706
      send_group_parts++;
 
707
  }
 
708
 
 
709
  if (error)
 
710
    goto err;                                   /* purecov: inspected */
 
711
 
 
712
  if (result && result->prepare(fields_list, unit_arg))
 
713
    goto err;                                   /* purecov: inspected */
 
714
 
 
715
  /* Init join struct */
 
716
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
717
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
718
  this->group= group_list != 0;
 
719
  unit= unit_arg;
 
720
 
 
721
#ifdef RESTRICTED_GROUP
 
722
  if (sum_func_count && !group_list && (func_count || field_count))
 
723
  {
 
724
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
725
    goto err;
 
726
  }
 
727
#endif
 
728
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
729
    goto err;
 
730
  if (alloc_func_list())
 
731
    goto err;
 
732
 
 
733
  return(0); // All OK
 
734
 
 
735
err:
 
736
  return(-1);                           /* purecov: inspected */
 
737
}
 
738
 
 
739
 
 
740
/*
 
741
  Remove the predicates pushed down into the subquery
 
742
 
 
743
  SYNOPSIS
 
744
    JOIN::remove_subq_pushed_predicates()
 
745
      where   IN  Must be NULL
 
746
              OUT The remaining WHERE condition, or NULL
 
747
 
 
748
  DESCRIPTION
 
749
    Given that this join will be executed using (unique|index)_subquery,
 
750
    without "checking NULL", remove the predicates that were pushed down
 
751
    into the subquery.
 
752
 
 
753
    If the subquery compares scalar values, we can remove the condition that
 
754
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
755
    engine)
 
756
 
 
757
    If the subquery compares row values, we need to keep the wrapped
 
758
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
759
    and non-NULL values, we'll do a full table scan and will rely on the
 
760
    equalities corresponding to non-NULL parts of left tuple to filter out
 
761
    non-matching records.
 
762
 
 
763
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
764
    fact that subquery engine will be using index lookup. This must be done only
 
765
    for cases where there are no conversion errors of significance, e.g. 257
 
766
    that is searched in a byte. But this requires homogenization of the return
 
767
    codes of all Field*::store() methods.
 
768
*/
 
769
 
 
770
void JOIN::remove_subq_pushed_predicates(Item **where)
 
771
{
 
772
  if (conds->type() == Item::FUNC_ITEM &&
 
773
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
774
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
775
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
776
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
777
                   ((Item_func *)conds)->arguments()[0]))
 
778
  {
 
779
    *where= 0;
 
780
    return;
 
781
  }
 
782
}
 
783
 
 
784
 
281
785
/*
282
786
  Index lookup-based subquery: save some flags for EXPLAIN output
283
787
 
296
800
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
801
    and set appropriate flags in join_tab->packed_info.
298
802
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
803
 
 
804
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
805
{
301
806
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
807
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
808
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
809
  if (where)
305
810
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
313
818
  }
314
819
}
315
820
 
 
821
 
 
822
 
 
823
 
 
824
/*
 
825
  Check if the table's rowid is included in the temptable
 
826
 
 
827
  SYNOPSIS
 
828
    sj_table_is_included()
 
829
      join      The join
 
830
      join_tab  The table to be checked
 
831
 
 
832
  DESCRIPTION
 
833
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
834
    in the temptable. This is so if
 
835
 
 
836
    1. The table is not embedded within some semi-join nest
 
837
    2. The has been pulled out of a semi-join nest, or
 
838
 
 
839
    3. The table is functionally dependent on some previous table
 
840
 
 
841
    [4. This is also true for constant tables that can't be
 
842
        NULL-complemented but this function is not called for such tables]
 
843
 
 
844
  RETURN
 
845
    true  - Include table's rowid
 
846
    false - Don't
 
847
*/
 
848
 
 
849
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
850
{
 
851
  if (join_tab->emb_sj_nest)
 
852
    return false;
 
853
 
 
854
  /* Check if this table is functionally dependent on the tables that
 
855
     are within the same outer join nest
 
856
  */
 
857
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
858
  if (join_tab->type == JT_EQ_REF)
 
859
  {
 
860
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
861
    uint32_t idx;
 
862
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
863
    {
 
864
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
865
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
866
        return true;
 
867
    }
 
868
    /* Ok, functionally dependent */
 
869
    return false;
 
870
  }
 
871
  /* Not functionally dependent => need to include*/
 
872
  return true;
 
873
}
 
874
 
 
875
 
 
876
/*
 
877
  Setup the strategies to eliminate semi-join duplicates.
 
878
 
 
879
  SYNOPSIS
 
880
    setup_semijoin_dups_elimination()
 
881
      join           Join to process
 
882
      options        Join options (needed to see if join buffering will be
 
883
                     used or not)
 
884
      no_jbuf_after  Another bit of information re where join buffering will
 
885
                     be used.
 
886
 
 
887
  DESCRIPTION
 
888
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
889
    strategies:
 
890
 
 
891
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
892
                         of row combinations)
 
893
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
894
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
895
                  together and picking the 1st one)
 
896
 
 
897
    The join order has "duplicate-generating ranges", and every range is
 
898
    served by one strategy or a combination of FirstMatch with with some
 
899
    other strategy.
 
900
 
 
901
    "Duplicate-generating range" is defined as a range within the join order
 
902
    that contains all of the inner tables of a semi-join. All ranges must be
 
903
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
904
    are joined together, which is equivalent to converting
 
905
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
906
    to
 
907
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
908
    .
 
909
 
 
910
    Applicability conditions are as follows:
 
911
 
 
912
    DuplicateWeedout strategy
 
913
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
914
 
 
915
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
916
      +------+  +=========================+  +---+
 
917
        (1)                 (2)               (3)
 
918
 
 
919
       (1) - Prefix of OuterTables (those that participate in
 
920
             IN-equality and/or are correlated with subquery) and outer
 
921
             Noncorrelated Tables.
 
922
       (2) - The handled range. The range starts with the first sj-inner
 
923
             table, and covers all sj-inner and outer tables
 
924
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
925
             may follow in any order.
 
926
       (3) - The suffix of outer Noncorrelated tables.
 
927
 
 
928
    FirstMatch strategy
 
929
    ~~~~~~~~~~~~~~~~~~~
 
930
 
 
931
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
932
      +------+  +==================+  +---+
 
933
        (1)             (2)          (3)
 
934
 
 
935
      (1) - Prefix of outer and non-correlated tables
 
936
      (2) - The handled range, which may contain only inner and
 
937
            non-correlated tables.
 
938
      (3) - The suffix of outer Noncorrelated tables.
 
939
 
 
940
    InsideOut strategy
 
941
    ~~~~~~~~~~~~~~~~~~
 
942
 
 
943
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
944
     +--------+   +===========+ +=============+   +------+
 
945
        (1)           (2)          (3)              (4)
 
946
 
 
947
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
948
            all the non-trivially correlated outer tables. (non-trivially means
 
949
            that the correlation is not just through the IN-equality).
 
950
 
 
951
      (2) - Inner table for which the InsideOut scan is performed.
 
952
 
 
953
      (3) - The remainder of the duplicate-generating range. It is served by
 
954
            application of FirstMatch strategy, with the exception that
 
955
            outer IN-correlated tables are considered to be non-correlated.
 
956
 
 
957
      (4) - THe suffix of outer and outer non-correlated tables.
 
958
 
 
959
    If several strategies are applicable, their relative priorities are:
 
960
      1. InsideOut
 
961
      2. FirstMatch
 
962
      3. DuplicateWeedout
 
963
 
 
964
    This function walks over the join order and sets up the strategies by
 
965
    setting appropriate members in join_tab structures.
 
966
 
 
967
  RETURN
 
968
    false  OK
 
969
    true   Out of memory error
 
970
*/
 
971
 
 
972
static
 
973
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
974
{
 
975
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
976
  struct {
 
977
    /*
 
978
      0 - invalid (EOF marker),
 
979
      1 - InsideOut,
 
980
      2 - Temptable (maybe confluent),
 
981
      3 - Temptable with join buffering
 
982
    */
 
983
    uint32_t strategy;
 
984
    uint32_t start_idx; /* Left range bound */
 
985
    uint32_t end_idx;   /* Right range bound */
 
986
    /*
 
987
      For Temptable strategy: Bitmap of all outer and correlated tables from
 
988
      all involved join nests.
 
989
    */
 
990
    table_map outer_tables;
 
991
  } dups_ranges [MAX_TABLES];
 
992
 
 
993
  TableList *emb_insideout_nest= NULL;
 
994
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
995
                               tables) whose ranges we're in */
 
996
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
997
  table_map range_start_map= 0; /* table_map at current range start */
 
998
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
999
  int cur_range= 0;
 
1000
  uint32_t i;
 
1001
 
 
1002
  /*
 
1003
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
1004
  */
 
1005
  for (i=join->const_tables ; i < join->tables ; i++)
 
1006
  {
 
1007
    JOIN_TAB *tab=join->join_tab+i;
 
1008
    Table *table=tab->table;
 
1009
    cur_map |= table->map;
 
1010
 
 
1011
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
1012
    {
 
1013
      if (!emb_sj_map)
 
1014
      {
 
1015
        dups_ranges[cur_range].start_idx= i;
 
1016
        range_start_map= cur_map & ~table->map;
 
1017
        /*
 
1018
          Remember if this is a possible start of range that is covered by
 
1019
          the InsideOut strategy (the reason that it is not covered could
 
1020
          be that it overlaps with anther semi-join's range. we don't
 
1021
          support InsideOut for joined ranges)
 
1022
        */
 
1023
        if (join->best_positions[i].use_insideout_scan)
 
1024
          emb_insideout_nest= tab->emb_sj_nest;
 
1025
      }
 
1026
 
 
1027
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1028
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1029
 
 
1030
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1031
      {
 
1032
        /*
 
1033
          Two different semi-joins interleave. This cannot be handled by
 
1034
          InsideOut strategy.
 
1035
        */
 
1036
        emb_insideout_nest= NULL;
 
1037
      }
 
1038
    }
 
1039
 
 
1040
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1041
    {
 
1042
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1043
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1044
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1045
      {
 
1046
        /*
 
1047
          This table uses join buffering, which makes use of FirstMatch or
 
1048
          InsideOut strategies impossible for the current and (we assume)
 
1049
          preceding duplicate-producing ranges.
 
1050
          That is, for the join order:
 
1051
 
 
1052
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1053
                  |     |     |     |          | \
 
1054
                  +-----+     +-----+          |  join buffering use
 
1055
                     r1          r2         we're here
 
1056
 
 
1057
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1058
          strategy that spans all the tables, starting from the very 1st
 
1059
          one.
 
1060
        */
 
1061
        dealing_with_jbuf= true;
 
1062
        emb_insideout_nest= false;
 
1063
 
 
1064
        /*
 
1065
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1066
          do not matter:
 
1067
        */
 
1068
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1069
        {
 
1070
          dups_ranges[cur_range].outer_tables |=
 
1071
            dups_ranges[prev_range].outer_tables;
 
1072
        }
 
1073
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1074
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1075
        cur_range=  0;
 
1076
      }
 
1077
 
 
1078
      /*
 
1079
        Check if we are at the end of duplicate-producing range. We are if
 
1080
 
 
1081
        1. It's an InsideOut range (which presumes all correlated tables are
 
1082
           in the prefix), and all inner tables are in the join order prefix,
 
1083
           or
 
1084
        2. It's a DuplicateElimination range (possibly covering several
 
1085
           SJ-nests), and all inner, outer, and correlated tables of all
 
1086
           sj-nests are in the join order prefix.
 
1087
      */
 
1088
      bool end_of_range= false;
 
1089
      if (emb_insideout_nest &&
 
1090
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1091
      {
 
1092
        /* Save that this range is handled with InsideOut: */
 
1093
        dups_ranges[cur_range].strategy= 1;
 
1094
        end_of_range= true;
 
1095
      }
 
1096
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1097
      {
 
1098
        /*
 
1099
          This is a complete range to be handled with either DuplicateWeedout
 
1100
          or FirstMatch
 
1101
        */
 
1102
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1103
        /*
 
1104
          This will hold tables from within the range that need to be put
 
1105
          into the join buffer before we can use the FirstMatch on its tail.
 
1106
        */
 
1107
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
 
1108
                                             ~range_start_map;
 
1109
        end_of_range= true;
 
1110
      }
 
1111
 
 
1112
      if (end_of_range)
 
1113
      {
 
1114
        dups_ranges[cur_range].end_idx= i+1;
 
1115
        emb_sj_map= emb_outer_tables= 0;
 
1116
        emb_insideout_nest= NULL;
 
1117
        dealing_with_jbuf= false;
 
1118
        dups_ranges[++cur_range].strategy= 0;
 
1119
      }
 
1120
    }
 
1121
  }
 
1122
 
 
1123
  Session *session= join->session;
 
1124
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1125
  /*
 
1126
    Second pass: setup the chosen strategies
 
1127
  */
 
1128
  for (int j= 0; j < cur_range; j++)
 
1129
  {
 
1130
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1131
    JOIN_TAB *jump_to;
 
1132
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1133
    {
 
1134
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1135
      jump_to= tab++;
 
1136
    }
 
1137
    else // DuplicateWeedout strategy
 
1138
    {
 
1139
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1140
      table_map weed_cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1141
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1142
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1143
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1144
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1145
      JOIN_TAB *last_outer_tab= tab - 1;
 
1146
      /*
 
1147
        Walk through the range and remember
 
1148
         - tables that need their rowids to be put into temptable
 
1149
         - the last outer table
 
1150
      */
 
1151
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1152
      {
 
1153
        if (sj_table_is_included(join, tab))
 
1154
        {
 
1155
          last_tab->join_tab= tab;
 
1156
          last_tab->rowid_offset= jt_rowid_offset;
 
1157
          jt_rowid_offset += tab->table->file->ref_length;
 
1158
          if (tab->table->maybe_null)
 
1159
          {
 
1160
            last_tab->null_byte= jt_null_bits / 8;
 
1161
            last_tab->null_bit= jt_null_bits++;
 
1162
          }
 
1163
          last_tab++;
 
1164
          tab->table->prepare_for_position();
 
1165
          tab->rowid_keep_flags= rowid_keep_flags;
 
1166
        }
 
1167
        weed_cur_map |= tab->table->map;
 
1168
        if (!tab->emb_sj_nest && bitmap_covers(weed_cur_map,
 
1169
                                               dups_ranges[j].outer_tables))
 
1170
          last_outer_tab= tab;
 
1171
      }
 
1172
 
 
1173
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1174
      {
 
1175
        SJ_TMP_TABLE *sjtbl;
 
1176
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1177
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1178
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
 
1179
          return(true);
 
1180
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1181
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1182
        sjtbl->rowid_len= jt_rowid_offset;
 
1183
        sjtbl->null_bits= jt_null_bits;
 
1184
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1185
 
 
1186
        *next_sjtbl_ptr= sjtbl;
 
1187
        next_sjtbl_ptr= &(sjtbl->next);
 
1188
        sjtbl->next= NULL;
 
1189
 
 
1190
        sjtbl->tmp_table=
 
1191
          create_duplicate_weedout_tmp_table(session,
 
1192
                                             sjtbl->rowid_len +
 
1193
                                             sjtbl->null_bytes,
 
1194
                                             sjtbl);
 
1195
 
 
1196
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1197
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1198
      }
 
1199
      tab= last_outer_tab + 1;
 
1200
      jump_to= last_outer_tab;
 
1201
    }
 
1202
 
 
1203
    /* Create the FirstMatch tail */
 
1204
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1205
    {
 
1206
      if (tab->emb_sj_nest)
 
1207
        tab->do_firstmatch= jump_to;
 
1208
      else
 
1209
        jump_to= tab;
 
1210
    }
 
1211
  }
 
1212
  return(false);
 
1213
}
 
1214
 
 
1215
 
 
1216
static void cleanup_sj_tmp_tables(JOIN *join)
 
1217
{
 
1218
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
 
1219
       sj_tbl= sj_tbl->next)
 
1220
  {
 
1221
    if (sj_tbl->tmp_table)
 
1222
    {
 
1223
      sj_tbl->tmp_table->free_tmp_table(join->session);
 
1224
    }
 
1225
  }
 
1226
  join->sj_tmp_tables= NULL;
 
1227
}
 
1228
 
 
1229
uint32_t make_join_orderinfo(JOIN *join);
 
1230
 
 
1231
/**
 
1232
  global select optimisation.
 
1233
 
 
1234
  @note
 
1235
    error code saved in field 'error'
 
1236
 
 
1237
  @retval
 
1238
    0   success
 
1239
  @retval
 
1240
    1   error
 
1241
*/
 
1242
 
 
1243
int
 
1244
JOIN::optimize()
 
1245
{
 
1246
  // to prevent double initialization on EXPLAIN
 
1247
  if (optimized)
 
1248
    return(0);
 
1249
  optimized= 1;
 
1250
 
 
1251
  session->set_proc_info("optimizing");
 
1252
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1253
              unit->select_limit_cnt);
 
1254
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1255
  select_limit= unit->select_limit_cnt;
 
1256
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1257
    select_limit= HA_POS_ERROR;
 
1258
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1259
  // Ignore errors of execution if option IGNORE present
 
1260
  if (session->lex->ignore)
 
1261
    session->lex->current_select->no_error= 1;
 
1262
 
 
1263
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1264
  /* Add HAVING to WHERE if possible */
 
1265
  if (having && !group_list && !sum_func_count)
 
1266
  {
 
1267
    if (!conds)
 
1268
    {
 
1269
      conds= having;
 
1270
      having= 0;
 
1271
    }
 
1272
    else if ((conds=new Item_cond_and(conds,having)))
 
1273
    {
 
1274
      /*
 
1275
        Item_cond_and can't be fixed after creation, so we do not check
 
1276
        conds->fixed
 
1277
      */
 
1278
      conds->fix_fields(session, &conds);
 
1279
      conds->change_ref_to_fields(session, tables_list);
 
1280
      conds->top_level_item();
 
1281
      having= 0;
 
1282
    }
 
1283
  }
 
1284
#endif
 
1285
 
 
1286
  /* Convert all outer joins to inner joins if possible */
 
1287
  conds= simplify_joins(this, join_list, conds, true, false);
 
1288
  build_bitmap_for_nested_joins(join_list, 0);
 
1289
 
 
1290
  conds= optimize_cond(this, conds, join_list, &cond_value);
 
1291
  if (session->is_error())
 
1292
  {
 
1293
    error= 1;
 
1294
    return(1);
 
1295
  }
 
1296
 
 
1297
  {
 
1298
    having= optimize_cond(this, having, join_list, &having_value);
 
1299
    if (session->is_error())
 
1300
    {
 
1301
      error= 1;
 
1302
      return(1);
 
1303
    }
 
1304
    if (select_lex->where)
 
1305
      select_lex->cond_value= cond_value;
 
1306
    if (select_lex->having)
 
1307
      select_lex->having_value= having_value;
 
1308
 
 
1309
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
 
1310
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1311
    {                                           /* Impossible cond */
 
1312
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1313
                           "Impossible HAVING" : "Impossible WHERE";
 
1314
      error= 0;
 
1315
      return(0);
 
1316
    }
 
1317
  }
 
1318
 
 
1319
  /* Optimize count(*), cmin() and cmax() */
 
1320
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1321
  {
 
1322
    int res;
 
1323
    /*
 
1324
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1325
      to the WHERE conditions,
 
1326
      or 1 if all items were resolved,
 
1327
      or 0, or an error number HA_ERR_...
 
1328
    */
 
1329
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1330
    {
 
1331
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1332
      {
 
1333
        zero_result_cause= "No matching min/max row";
 
1334
        error=0;
 
1335
        return(0);
 
1336
      }
 
1337
      if (res > 1)
 
1338
      {
 
1339
        error= res;
 
1340
        return(1);
 
1341
      }
 
1342
      if (res < 0)
 
1343
      {
 
1344
        zero_result_cause= "No matching min/max row";
 
1345
        error=0;
 
1346
        return(0);
 
1347
      }
 
1348
      zero_result_cause= "Select tables optimized away";
 
1349
      tables_list= 0;                           // All tables resolved
 
1350
      /*
 
1351
        Extract all table-independent conditions and replace the WHERE
 
1352
        clause with them. All other conditions were computed by opt_sum_query
 
1353
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1354
        so there is no need to compute the whole WHERE clause again.
 
1355
        Notice that make_cond_for_table() will always succeed to remove all
 
1356
        computed conditions, because opt_sum_query() is applicable only to
 
1357
        conjunctions.
 
1358
        Preserve conditions for EXPLAIN.
 
1359
      */
 
1360
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
1361
      {
 
1362
        COND *table_independent_conds=
 
1363
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1364
        conds= table_independent_conds;
 
1365
      }
 
1366
    }
 
1367
  }
 
1368
  if (!tables_list)
 
1369
  {
 
1370
    error= 0;
 
1371
    return(0);
 
1372
  }
 
1373
  error= -1;                                    // Error is sent to client
 
1374
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1375
 
 
1376
  /* Calculate how to do the join */
 
1377
  session->set_proc_info("statistics");
 
1378
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1379
      session->is_fatal_error)
 
1380
  {
 
1381
    return(1);
 
1382
  }
 
1383
 
 
1384
  /* Remove distinct if only const tables */
 
1385
  select_distinct= select_distinct && (const_tables != tables);
 
1386
  session->set_proc_info("preparing");
 
1387
  if (result->initialize_tables(this))
 
1388
  {
 
1389
    return(1);                          // error == -1
 
1390
  }
 
1391
  if (const_table_map != found_const_table_map &&
 
1392
      !(select_options & SELECT_DESCRIBE) &&
 
1393
      (!conds ||
 
1394
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1395
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
1396
  {
 
1397
    zero_result_cause= "no matching row in const table";
 
1398
    error= 0;
 
1399
    return(0);
 
1400
  }
 
1401
  if (!(session->options & OPTION_BIG_SELECTS) &&
 
1402
      best_read > (double) session->variables.max_join_size &&
 
1403
      !(select_options & SELECT_DESCRIBE))
 
1404
  {                                             /* purecov: inspected */
 
1405
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1406
    error= -1;
 
1407
    return(1);
 
1408
  }
 
1409
  if (const_tables && !session->locked_tables &&
 
1410
      !(select_options & SELECT_NO_UNLOCK))
 
1411
    mysql_unlock_some_tables(session, table, const_tables);
 
1412
  if (!conds && outer_join)
 
1413
  {
 
1414
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1415
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1416
  }
 
1417
  select= make_select(*table, const_table_map,
 
1418
                      const_table_map, conds, 1, &error);
 
1419
  if (error)
 
1420
  {                                             /* purecov: inspected */
 
1421
    error= -1;                                  /* purecov: inspected */
 
1422
    return(1);
 
1423
  }
 
1424
 
 
1425
  reset_nj_counters(join_list);
 
1426
  make_outerjoin_info(this);
 
1427
 
 
1428
  /*
 
1429
    Among the equal fields belonging to the same multiple equality
 
1430
    choose the one that is to be retrieved first and substitute
 
1431
    all references to these in where condition for a reference for
 
1432
    the selected field.
 
1433
  */
 
1434
  if (conds)
 
1435
  {
 
1436
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1437
    conds->update_used_tables();
 
1438
  }
 
1439
 
 
1440
  /*
 
1441
    Permorm the the optimization on fields evaluation mentioned above
 
1442
    for all on expressions.
 
1443
  */
 
1444
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1445
  {
 
1446
    if (*tab->on_expr_ref)
 
1447
    {
 
1448
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1449
                                                         tab->cond_equal,
 
1450
                                                         map2table);
 
1451
      (*tab->on_expr_ref)->update_used_tables();
 
1452
    }
 
1453
  }
 
1454
 
 
1455
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
 
1456
      (select_options & SELECT_DESCRIBE) &&
 
1457
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
1458
  {
 
1459
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1460
  }
 
1461
  if (make_join_select(this, select, conds))
 
1462
  {
 
1463
    zero_result_cause=
 
1464
      "Impossible WHERE noticed after reading const tables";
 
1465
    return(0);                          // error == 0
 
1466
  }
 
1467
 
 
1468
  error= -1;                                    /* if goto err */
 
1469
 
 
1470
  /* Optimize distinct away if possible */
 
1471
  {
 
1472
    order_st *org_order= order;
 
1473
    order=remove_const(this, order,conds,1, &simple_order);
 
1474
    if (session->is_error())
 
1475
    {
 
1476
      error= 1;
 
1477
      return(1);
 
1478
    }
 
1479
 
 
1480
    /*
 
1481
      If we are using order_st BY NULL or order_st BY const_expression,
 
1482
      return result in any order (even if we are using a GROUP BY)
 
1483
    */
 
1484
    if (!order && org_order)
 
1485
      skip_sort_order= 1;
 
1486
  }
 
1487
  /*
 
1488
     Check if we can optimize away GROUP BY/DISTINCT.
 
1489
     We can do that if there are no aggregate functions, the
 
1490
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1491
     (if present) contain direct references to all key parts of
 
1492
     an unique index (in whatever order) and if the key parts of the
 
1493
     unique index cannot contain NULLs.
 
1494
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1495
     be the same (as long as they are unique).
 
1496
 
 
1497
     The FROM clause must contain a single non-constant table.
 
1498
  */
 
1499
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1500
      !tmp_table_param.sum_func_count &&
 
1501
      (!join_tab[const_tables].select ||
 
1502
       !join_tab[const_tables].select->quick ||
 
1503
       join_tab[const_tables].select->quick->get_type() !=
 
1504
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1505
  {
 
1506
    if (group_list &&
 
1507
       list_contains_unique_index(join_tab[const_tables].table,
 
1508
                                 find_field_in_order_list,
 
1509
                                 (void *) group_list))
 
1510
    {
 
1511
      /*
 
1512
        We have found that grouping can be removed since groups correspond to
 
1513
        only one row anyway, but we still have to guarantee correct result
 
1514
        order. The line below effectively rewrites the query from GROUP BY
 
1515
        <fields> to order_st BY <fields>. There are two exceptions:
 
1516
        - if skip_sort_order is set (see above), then we can simply skip
 
1517
          GROUP BY;
 
1518
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1519
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1520
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1521
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1522
          order_st BY fields.
 
1523
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1524
          'order' as is.
 
1525
       */
 
1526
      if (!order || test_if_subpart(group_list, order))
 
1527
          order= skip_sort_order ? 0 : group_list;
 
1528
      /*
 
1529
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
 
1530
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1531
      */
 
1532
      join_tab->table->keys_in_use_for_order_by=
 
1533
        join_tab->table->keys_in_use_for_group_by;
 
1534
      group_list= 0;
 
1535
      group= 0;
 
1536
    }
 
1537
    if (select_distinct &&
 
1538
       list_contains_unique_index(join_tab[const_tables].table,
 
1539
                                 find_field_in_item_list,
 
1540
                                 (void *) &fields_list))
 
1541
    {
 
1542
      select_distinct= 0;
 
1543
    }
 
1544
  }
 
1545
  if (group_list || tmp_table_param.sum_func_count)
 
1546
  {
 
1547
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1548
      select_distinct=0;
 
1549
  }
 
1550
  else if (select_distinct && tables - const_tables == 1)
 
1551
  {
 
1552
    /*
 
1553
      We are only using one table. In this case we change DISTINCT to a
 
1554
      GROUP BY query if:
 
1555
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1556
        BY only uses selected fields.
 
1557
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1558
      - We are scanning the whole table without LIMIT
 
1559
        This can happen if:
 
1560
        - We are using CALC_FOUND_ROWS
 
1561
        - We are using an order_st BY that can't be optimized away.
 
1562
 
 
1563
      We don't want to use this optimization when we are using LIMIT
 
1564
      because in this case we can just create a temporary table that
 
1565
      holds LIMIT rows and stop when this table is full.
 
1566
    */
 
1567
    JOIN_TAB *tab= &join_tab[const_tables];
 
1568
    bool all_order_fields_used;
 
1569
    if (order)
 
1570
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
 
1571
        &tab->table->keys_in_use_for_order_by);
 
1572
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
 
1573
                                          order, fields_list, all_fields,
 
1574
                                          &all_order_fields_used)))
 
1575
    {
 
1576
      bool skip_group= (skip_sort_order &&
 
1577
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
 
1578
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1579
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1580
      if ((skip_group && all_order_fields_used) ||
 
1581
          select_limit == HA_POS_ERROR ||
 
1582
          (order && !skip_sort_order))
 
1583
      {
 
1584
        /*  Change DISTINCT to GROUP BY */
 
1585
        select_distinct= 0;
 
1586
        no_order= !order;
 
1587
        if (all_order_fields_used)
 
1588
        {
 
1589
          if (order && skip_sort_order)
 
1590
          {
 
1591
            /*
 
1592
              Force MySQL to read the table in sorted order to get result in
 
1593
              order_st BY order.
 
1594
            */
 
1595
            tmp_table_param.quick_group=0;
 
1596
          }
 
1597
          order=0;
 
1598
        }
 
1599
        group=1;                                // For end_write_group
 
1600
      }
 
1601
      else
 
1602
        group_list= 0;
 
1603
    }
 
1604
    else if (session->is_fatal_error)                   // End of memory
 
1605
      return(1);
 
1606
  }
 
1607
  simple_group= 0;
 
1608
  {
 
1609
    order_st *old_group_list;
 
1610
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1611
                             rollup.state == ROLLUP::STATE_NONE,
 
1612
                             &simple_group);
 
1613
    if (session->is_error())
 
1614
    {
 
1615
      error= 1;
 
1616
      return(1);
 
1617
    }
 
1618
    if (old_group_list && !group_list)
 
1619
      select_distinct= 0;
 
1620
  }
 
1621
  if (!group_list && group)
 
1622
  {
 
1623
    order=0;                                    // The output has only one row
 
1624
    simple_order=1;
 
1625
    select_distinct= 0;                       // No need in distinct for 1 row
 
1626
    group_optimized_away= 1;
 
1627
  }
 
1628
 
 
1629
  calc_group_buffer(this, group_list);
 
1630
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1631
 
 
1632
  if (test_if_subpart(group_list, order) ||
 
1633
      (!group_list && tmp_table_param.sum_func_count))
 
1634
    order=0;
 
1635
 
 
1636
  // Can't use sort on head table if using row cache
 
1637
  if (full_join)
 
1638
  {
 
1639
    if (group_list)
 
1640
      simple_group=0;
 
1641
    if (order)
 
1642
      simple_order=0;
 
1643
  }
 
1644
 
 
1645
  /*
 
1646
    Check if we need to create a temporary table.
 
1647
    This has to be done if all tables are not already read (const tables)
 
1648
    and one of the following conditions holds:
 
1649
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1650
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1651
    - We are using different order_st BY and GROUP BY orders
 
1652
    - The user wants us to buffer the result.
 
1653
  */
 
1654
  need_tmp= (const_tables != tables &&
 
1655
             ((select_distinct || !simple_order || !simple_group) ||
 
1656
              (group_list && order) ||
 
1657
              test(select_options & OPTION_BUFFER_RESULT)));
 
1658
 
 
1659
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1660
  uint64_t select_opts_for_readinfo=
 
1661
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1662
 
 
1663
  sj_tmp_tables= NULL;
 
1664
  if (!select_lex->sj_nests.is_empty())
 
1665
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1666
                                    no_jbuf_after);
 
1667
 
 
1668
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1669
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1670
    return(1);
 
1671
 
 
1672
  /* Create all structures needed for materialized subquery execution. */
 
1673
  if (setup_subquery_materialization())
 
1674
    return(1);
 
1675
 
 
1676
  /*
 
1677
    is this simple IN subquery?
 
1678
  */
 
1679
  if (!group_list && !order &&
 
1680
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1681
      tables == 1 && conds &&
 
1682
      !unit->is_union())
 
1683
  {
 
1684
    if (!having)
 
1685
    {
 
1686
      Item *where= conds;
 
1687
      if (join_tab[0].type == JT_EQ_REF &&
 
1688
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1689
      {
 
1690
        remove_subq_pushed_predicates(&where);
 
1691
        save_index_subquery_explain_info(join_tab, where);
 
1692
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1693
        error= 0;
 
1694
        return(unit->item->
 
1695
                    change_engine(new
 
1696
                                  subselect_uniquesubquery_engine(session,
 
1697
                                                                  join_tab,
 
1698
                                                                  unit->item,
 
1699
                                                                  where)));
 
1700
      }
 
1701
      else if (join_tab[0].type == JT_REF &&
 
1702
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1703
      {
 
1704
        remove_subq_pushed_predicates(&where);
 
1705
        save_index_subquery_explain_info(join_tab, where);
 
1706
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1707
        error= 0;
 
1708
        return(unit->item->
 
1709
                    change_engine(new
 
1710
                                  subselect_indexsubquery_engine(session,
 
1711
                                                                 join_tab,
 
1712
                                                                 unit->item,
 
1713
                                                                 where,
 
1714
                                                                 NULL,
 
1715
                                                                 0)));
 
1716
      }
 
1717
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1718
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1719
               having->name == in_having_cond)
 
1720
    {
 
1721
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1722
      error= 0;
 
1723
      conds= remove_additional_cond(conds);
 
1724
      save_index_subquery_explain_info(join_tab, conds);
 
1725
      return(unit->item->
 
1726
                  change_engine(new subselect_indexsubquery_engine(session,
 
1727
                                                                   join_tab,
 
1728
                                                                   unit->item,
 
1729
                                                                   conds,
 
1730
                                                                   having,
 
1731
                                                                   1)));
 
1732
    }
 
1733
 
 
1734
  }
 
1735
  /*
 
1736
    Need to tell handlers that to play it safe, it should fetch all
 
1737
    columns of the primary key of the tables: this is because MySQL may
 
1738
    build row pointers for the rows, and for all columns of the primary key
 
1739
    the read set has not necessarily been set by the server code.
 
1740
  */
 
1741
  if (need_tmp || select_distinct || group_list || order)
 
1742
  {
 
1743
    for (uint32_t i = const_tables; i < tables; i++)
 
1744
      join_tab[i].table->prepare_for_position();
 
1745
  }
 
1746
 
 
1747
  if (const_tables != tables)
 
1748
  {
 
1749
    /*
 
1750
      Because filesort always does a full table scan or a quick range scan
 
1751
      we must add the removed reference to the select for the table.
 
1752
      We only need to do this when we have a simple_order or simple_group
 
1753
      as in other cases the join is done before the sort.
 
1754
    */
 
1755
    if ((order || group_list) &&
 
1756
        (join_tab[const_tables].type != JT_ALL) &&
 
1757
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1758
        ((order && simple_order) || (group_list && simple_group)))
 
1759
    {
 
1760
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
 
1761
        return(1);
 
1762
      }
 
1763
    }
 
1764
 
 
1765
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1766
        ((group_list &&
 
1767
          (!simple_group ||
 
1768
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1769
                                    unit->select_limit_cnt, 0,
 
1770
                                    &join_tab[const_tables].table->
 
1771
                                    keys_in_use_for_group_by))) ||
 
1772
         select_distinct) &&
 
1773
        tmp_table_param.quick_group)
 
1774
    {
 
1775
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1776
    }
 
1777
    if (order)
 
1778
    {
 
1779
      /*
 
1780
        Force using of tmp table if sorting by a SP or UDF function due to
 
1781
        their expensive and probably non-deterministic nature.
 
1782
      */
 
1783
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1784
      {
 
1785
        Item *item= *tmp_order->item;
 
1786
        if (item->is_expensive())
 
1787
        {
 
1788
          /* Force tmp table without sort */
 
1789
          need_tmp=1; simple_order=simple_group=0;
 
1790
          break;
 
1791
        }
 
1792
      }
 
1793
    }
 
1794
  }
 
1795
 
 
1796
  tmp_having= having;
 
1797
  if (select_options & SELECT_DESCRIBE)
 
1798
  {
 
1799
    error= 0;
 
1800
    return(0);
 
1801
  }
 
1802
  having= 0;
 
1803
 
 
1804
  /*
 
1805
    The loose index scan access method guarantees that all grouping or
 
1806
    duplicate row elimination (for distinct) is already performed
 
1807
    during data retrieval, and that all MIN/MAX functions are already
 
1808
    computed for each group. Thus all MIN/MAX functions should be
 
1809
    treated as regular functions, and there is no need to perform
 
1810
    grouping in the main execution loop.
 
1811
    Notice that currently loose index scan is applicable only for
 
1812
    single table queries, thus it is sufficient to test only the first
 
1813
    join_tab element of the plan for its access method.
 
1814
  */
 
1815
  if (join_tab->is_using_loose_index_scan())
 
1816
    tmp_table_param.precomputed_group_by= true;
 
1817
 
 
1818
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1819
  if (need_tmp)
 
1820
  {
 
1821
    session->set_proc_info("Creating tmp table");
 
1822
 
 
1823
    init_items_ref_array();
 
1824
 
 
1825
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1826
                                         fields_list.elements);
 
1827
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1828
                                                             (order_st*) 0);
 
1829
    /*
 
1830
      Pushing LIMIT to the temporary table creation is not applicable
 
1831
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1832
      there are aggregate functions, because in all these cases we need
 
1833
      all result rows.
 
1834
    */
 
1835
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1836
                             !tmp_group &&
 
1837
                             !session->lex->current_select->with_sum_func) ?
 
1838
                            select_limit : HA_POS_ERROR;
 
1839
 
 
1840
    if (!(exec_tmp_table1=
 
1841
          create_tmp_table(session, &tmp_table_param, all_fields,
 
1842
                           tmp_group,
 
1843
                           group_list ? 0 : select_distinct,
 
1844
                           group_list && simple_group,
 
1845
                           select_options,
 
1846
                           tmp_rows_limit,
 
1847
                           (char *) "")))
 
1848
                {
 
1849
      return(1);
 
1850
    }
 
1851
 
 
1852
    /*
 
1853
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1854
      - we are sorting the table and writing complete group rows to the
 
1855
        temp table.
 
1856
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1857
        on all columns.
 
1858
 
 
1859
      If having is not handled here, it will be checked before the row
 
1860
      is sent to the client.
 
1861
    */
 
1862
    if (tmp_having &&
 
1863
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1864
      having= tmp_having;
 
1865
 
 
1866
    /* if group or order on first table, sort first */
 
1867
    if (group_list && simple_group)
 
1868
    {
 
1869
      session->set_proc_info("Sorting for group");
 
1870
      if (create_sort_index(session, this, group_list,
 
1871
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1872
          alloc_group_fields(this, group_list) ||
 
1873
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1874
          setup_sum_funcs(session, sum_funcs))
 
1875
      {
 
1876
        return(1);
 
1877
      }
 
1878
      group_list=0;
 
1879
    }
 
1880
    else
 
1881
    {
 
1882
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1883
          setup_sum_funcs(session, sum_funcs))
 
1884
      {
 
1885
        return(1);
 
1886
      }
 
1887
 
 
1888
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1889
      {
 
1890
        session->set_proc_info("Sorting for order");
 
1891
        if (create_sort_index(session, this, order,
 
1892
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1893
        {
 
1894
          return(1);
 
1895
        }
 
1896
        order=0;
 
1897
      }
 
1898
    }
 
1899
 
 
1900
    /*
 
1901
      Optimize distinct when used on some of the tables
 
1902
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1903
      In this case we can stop scanning t2 when we have found one t1.a
 
1904
    */
 
1905
 
 
1906
    if (exec_tmp_table1->distinct)
 
1907
    {
 
1908
      table_map used_tables= session->used_tables;
 
1909
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1910
      do
 
1911
      {
 
1912
        if (used_tables & last_join_tab->table->map)
 
1913
          break;
 
1914
        last_join_tab->not_used_in_distinct=1;
 
1915
      } while (last_join_tab-- != join_tab);
 
1916
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1917
      if (order && skip_sort_order)
 
1918
      {
 
1919
        /* Should always succeed */
 
1920
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1921
                                    order, unit->select_limit_cnt, 0,
 
1922
                                    &join_tab[const_tables].table->
 
1923
                                      keys_in_use_for_order_by))
 
1924
          order=0;
 
1925
      }
 
1926
    }
 
1927
 
 
1928
    /*
 
1929
      If this join belongs to an uncacheable subquery save
 
1930
      the original join
 
1931
    */
 
1932
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1933
        init_save_join_tab())
 
1934
      return(-1);                         /* purecov: inspected */
 
1935
  }
 
1936
 
 
1937
  error= 0;
 
1938
  return(0);
 
1939
}
 
1940
 
 
1941
 
 
1942
/**
 
1943
  Restore values in temporary join.
 
1944
*/
 
1945
void JOIN::restore_tmp()
 
1946
{
 
1947
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1948
}
 
1949
 
 
1950
 
 
1951
int
 
1952
JOIN::reinit()
 
1953
{
 
1954
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1955
                                    select_lex->offset_limit->val_uint() :
 
1956
                                    0UL);
 
1957
 
 
1958
  first_record= 0;
 
1959
 
 
1960
  if (exec_tmp_table1)
 
1961
  {
 
1962
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1963
    exec_tmp_table1->file->ha_delete_all_rows();
 
1964
    free_io_cache(exec_tmp_table1);
 
1965
    filesort_free_buffers(exec_tmp_table1,0);
 
1966
  }
 
1967
  if (exec_tmp_table2)
 
1968
  {
 
1969
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1970
    exec_tmp_table2->file->ha_delete_all_rows();
 
1971
    free_io_cache(exec_tmp_table2);
 
1972
    filesort_free_buffers(exec_tmp_table2,0);
 
1973
  }
 
1974
  if (items0)
 
1975
    set_items_ref_array(items0);
 
1976
 
 
1977
  if (join_tab_save)
 
1978
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1979
 
 
1980
  if (tmp_join)
 
1981
    restore_tmp();
 
1982
 
 
1983
  /* Reset of sum functions */
 
1984
  if (sum_funcs)
 
1985
  {
 
1986
    Item_sum *func, **func_ptr= sum_funcs;
 
1987
    while ((func= *(func_ptr++)))
 
1988
      func->clear();
 
1989
  }
 
1990
 
 
1991
  return(0);
 
1992
}
 
1993
 
 
1994
/**
 
1995
   @brief Save the original join layout
 
1996
 
 
1997
   @details Saves the original join layout so it can be reused in
 
1998
   re-execution and for EXPLAIN.
 
1999
 
 
2000
   @return Operation status
 
2001
   @retval 0      success.
 
2002
   @retval 1      error occurred.
 
2003
*/
 
2004
 
 
2005
bool
 
2006
JOIN::init_save_join_tab()
 
2007
{
 
2008
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
 
2009
    return 1;                                  /* purecov: inspected */
 
2010
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2011
  restore_tmp();
 
2012
  return 0;
 
2013
}
 
2014
 
 
2015
 
 
2016
bool
 
2017
JOIN::save_join_tab()
 
2018
{
 
2019
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2020
  {
 
2021
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
 
2022
                                                sizeof(JOIN_TAB) * tables)))
 
2023
      return 1;
 
2024
  }
 
2025
  return 0;
 
2026
}
 
2027
 
 
2028
 
 
2029
/**
 
2030
  Exec select.
 
2031
 
 
2032
  @todo
 
2033
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2034
    finally replace sorting with index scan if there is a LIMIT clause in
 
2035
    the query.  It's never shown in EXPLAIN!
 
2036
 
 
2037
  @todo
 
2038
    When can we have here session->net.report_error not zero?
 
2039
*/
 
2040
void
 
2041
JOIN::exec()
 
2042
{
 
2043
  List<Item> *columns_list= &fields_list;
 
2044
  int      tmp_error;
 
2045
 
 
2046
  session->set_proc_info("executing");
 
2047
  error= 0;
 
2048
 
 
2049
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2050
  {                                           
 
2051
    /* Only test of functions */
 
2052
    if (select_options & SELECT_DESCRIBE)
 
2053
      select_describe(this, false, false, false,
 
2054
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2055
    else
 
2056
    {
 
2057
      result->send_fields(*columns_list,
 
2058
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2059
      /*
 
2060
        We have to test for 'conds' here as the WHERE may not be constant
 
2061
        even if we don't have any tables for prepared statements or if
 
2062
        conds uses something like 'rand()'.
 
2063
      */
 
2064
      if (cond_value != Item::COND_FALSE &&
 
2065
          (!conds || conds->val_int()) &&
 
2066
          (!having || having->val_int()))
 
2067
      {
 
2068
        if (do_send_rows && result->send_data(fields_list))
 
2069
          error= 1;
 
2070
        else
 
2071
        {
 
2072
          error= (int) result->send_eof();
 
2073
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
 
2074
        }
 
2075
      }
 
2076
      else
 
2077
      {
 
2078
        error= (int) result->send_eof();
 
2079
        send_records= 0;
 
2080
      }
 
2081
    }
 
2082
    /* Single select (without union) always returns 0 or 1 row */
 
2083
    session->limit_found_rows= send_records;
 
2084
    session->examined_row_count= 0;
 
2085
    return;
 
2086
  }
 
2087
  /*
 
2088
    Don't reset the found rows count if there're no tables as
 
2089
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2090
    It must be accumulated from all join iterations of all join parts.
 
2091
  */
 
2092
  if (tables)
 
2093
    session->limit_found_rows= 0;
 
2094
 
 
2095
  if (zero_result_cause)
 
2096
  {
 
2097
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2098
                            *columns_list,
 
2099
                            send_row_on_empty_set(),
 
2100
                            select_options,
 
2101
                            zero_result_cause,
 
2102
                            having);
 
2103
    return;
 
2104
  }
 
2105
 
 
2106
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2107
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2108
    return;
 
2109
 
 
2110
  if (select_options & SELECT_DESCRIBE)
 
2111
  {
 
2112
    /*
 
2113
      Check if we managed to optimize order_st BY away and don't use temporary
 
2114
      table to resolve order_st BY: in that case, we only may need to do
 
2115
      filesort for GROUP BY.
 
2116
    */
 
2117
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2118
    {
 
2119
      /*
 
2120
        Reset 'order' to 'group_list' and reinit variables describing
 
2121
        'order'
 
2122
      */
 
2123
      order= group_list;
 
2124
      simple_order= simple_group;
 
2125
      skip_sort_order= 0;
 
2126
    }
 
2127
    if (order &&
 
2128
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2129
        (const_tables == tables ||
 
2130
         ((simple_order || skip_sort_order) &&
 
2131
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2132
                                  select_limit, 0,
 
2133
                                  &join_tab[const_tables].table->
 
2134
                                    keys_in_use_for_query))))
 
2135
      order=0;
 
2136
    having= tmp_having;
 
2137
    select_describe(this, need_tmp,
 
2138
                    order != 0 && !skip_sort_order,
 
2139
                    select_distinct,
 
2140
                    !tables ? "No tables used" : NULL);
 
2141
    return;
 
2142
  }
 
2143
 
 
2144
  JOIN *curr_join= this;
 
2145
  List<Item> *curr_all_fields= &all_fields;
 
2146
  List<Item> *curr_fields_list= &fields_list;
 
2147
  Table *curr_tmp_table= 0;
 
2148
  /*
 
2149
    Initialize examined rows here because the values from all join parts
 
2150
    must be accumulated in examined_row_count. Hence every join
 
2151
    iteration must count from zero.
 
2152
  */
 
2153
  curr_join->examined_rows= 0;
 
2154
 
 
2155
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2156
  if (need_tmp)
 
2157
  {
 
2158
    if (tmp_join)
 
2159
    {
 
2160
      /*
 
2161
        We are in a non cacheable sub query. Get the saved join structure
 
2162
        after optimization.
 
2163
        (curr_join may have been modified during last exection and we need
 
2164
        to reset it)
 
2165
      */
 
2166
      curr_join= tmp_join;
 
2167
    }
 
2168
    curr_tmp_table= exec_tmp_table1;
 
2169
 
 
2170
    /* Copy data to the temporary table */
 
2171
    session->set_proc_info("Copying to tmp table");
 
2172
    if (!curr_join->sort_and_group &&
 
2173
        curr_join->const_tables != curr_join->tables)
 
2174
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2175
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2176
    {
 
2177
      error= tmp_error;
 
2178
      return;
 
2179
    }
 
2180
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2181
 
 
2182
    if (curr_join->having)
 
2183
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2184
 
 
2185
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2186
    curr_join->all_fields= *curr_all_fields;
 
2187
    if (!items1)
 
2188
    {
 
2189
      items1= items0 + all_fields.elements;
 
2190
      if (sort_and_group || curr_tmp_table->group)
 
2191
      {
 
2192
        if (change_to_use_tmp_fields(session, items1,
 
2193
                                     tmp_fields_list1, tmp_all_fields1,
 
2194
                                     fields_list.elements, all_fields))
 
2195
          return;
 
2196
      }
 
2197
      else
 
2198
      {
 
2199
        if (change_refs_to_tmp_fields(session, items1,
 
2200
                                      tmp_fields_list1, tmp_all_fields1,
 
2201
                                      fields_list.elements, all_fields))
 
2202
          return;
 
2203
      }
 
2204
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2205
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2206
      curr_join->items1= items1;
 
2207
    }
 
2208
    curr_all_fields= &tmp_all_fields1;
 
2209
    curr_fields_list= &tmp_fields_list1;
 
2210
    curr_join->set_items_ref_array(items1);
 
2211
 
 
2212
    if (sort_and_group || curr_tmp_table->group)
 
2213
    {
 
2214
      curr_join->tmp_table_param.field_count+=
 
2215
        curr_join->tmp_table_param.sum_func_count+
 
2216
        curr_join->tmp_table_param.func_count;
 
2217
      curr_join->tmp_table_param.sum_func_count=
 
2218
        curr_join->tmp_table_param.func_count= 0;
 
2219
    }
 
2220
    else
 
2221
    {
 
2222
      curr_join->tmp_table_param.field_count+=
 
2223
        curr_join->tmp_table_param.func_count;
 
2224
      curr_join->tmp_table_param.func_count= 0;
 
2225
    }
 
2226
 
 
2227
    if (curr_tmp_table->group)
 
2228
    {                                           // Already grouped
 
2229
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2230
        curr_join->order= curr_join->group_list;  /* order by group */
 
2231
      curr_join->group_list= 0;
 
2232
    }
 
2233
 
 
2234
    /*
 
2235
      If we have different sort & group then we must sort the data by group
 
2236
      and copy it to another tmp table
 
2237
      This code is also used if we are using distinct something
 
2238
      we haven't been able to store in the temporary table yet
 
2239
      like SEC_TO_TIME(SUM(...)).
 
2240
    */
 
2241
 
 
2242
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
2243
    {                                   /* Must copy to another table */
 
2244
      /* Free first data from old join */
 
2245
      curr_join->join_free();
 
2246
      if (make_simple_join(curr_join, curr_tmp_table))
 
2247
        return;
 
2248
      calc_group_buffer(curr_join, group_list);
 
2249
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2250
                        curr_join->tmp_all_fields1,
 
2251
                        curr_join->select_distinct && !curr_join->group_list);
 
2252
      curr_join->tmp_table_param.hidden_field_count=
 
2253
        (curr_join->tmp_all_fields1.elements-
 
2254
         curr_join->tmp_fields_list1.elements);
 
2255
 
 
2256
 
 
2257
      if (exec_tmp_table2)
 
2258
        curr_tmp_table= exec_tmp_table2;
 
2259
      else
 
2260
      {
 
2261
        /* group data to new table */
 
2262
 
 
2263
        /*
 
2264
          If the access method is loose index scan then all MIN/MAX
 
2265
          functions are precomputed, and should be treated as regular
 
2266
          functions. See extended comment in JOIN::exec.
 
2267
        */
 
2268
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2269
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2270
 
 
2271
        if (!(curr_tmp_table=
 
2272
              exec_tmp_table2= create_tmp_table(session,
 
2273
                                                &curr_join->tmp_table_param,
 
2274
                                                *curr_all_fields,
 
2275
                                                (order_st*) 0,
 
2276
                                                curr_join->select_distinct &&
 
2277
                                                !curr_join->group_list,
 
2278
                                                1, curr_join->select_options,
 
2279
                                                HA_POS_ERROR,
 
2280
                                                (char *) "")))
 
2281
          return;
 
2282
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2283
      }
 
2284
      if (curr_join->group_list)
 
2285
      {
 
2286
        session->set_proc_info("Creating sort index");
 
2287
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2288
        {
 
2289
          return;
 
2290
        }
 
2291
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
2292
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2293
            make_group_fields(this, curr_join))
 
2294
        {
 
2295
          return;
 
2296
        }
 
2297
        sortorder= curr_join->sortorder;
 
2298
      }
 
2299
 
 
2300
      session->set_proc_info("Copying to group table");
 
2301
      tmp_error= -1;
 
2302
      if (curr_join != this)
 
2303
      {
 
2304
        if (sum_funcs2)
 
2305
        {
 
2306
          curr_join->sum_funcs= sum_funcs2;
 
2307
          curr_join->sum_funcs_end= sum_funcs_end2;
 
2308
        }
 
2309
        else
 
2310
        {
 
2311
          curr_join->alloc_func_list();
 
2312
          sum_funcs2= curr_join->sum_funcs;
 
2313
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2314
        }
 
2315
      }
 
2316
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2317
                                        1, true))
 
2318
        return;
 
2319
      curr_join->group_list= 0;
 
2320
      if (!curr_join->sort_and_group &&
 
2321
          curr_join->const_tables != curr_join->tables)
 
2322
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2323
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2324
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2325
      {
 
2326
        error= tmp_error;
 
2327
        return;
 
2328
      }
 
2329
      end_read_record(&curr_join->join_tab->read_record);
 
2330
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2331
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2332
 
 
2333
      // No sum funcs anymore
 
2334
      if (!items2)
 
2335
      {
 
2336
        items2= items1 + all_fields.elements;
 
2337
        if (change_to_use_tmp_fields(session, items2,
 
2338
                                     tmp_fields_list2, tmp_all_fields2,
 
2339
                                     fields_list.elements, tmp_all_fields1))
 
2340
          return;
 
2341
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2342
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2343
      }
 
2344
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2345
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2346
      curr_join->set_items_ref_array(items2);
 
2347
      curr_join->tmp_table_param.field_count+=
 
2348
        curr_join->tmp_table_param.sum_func_count;
 
2349
      curr_join->tmp_table_param.sum_func_count= 0;
 
2350
    }
 
2351
    if (curr_tmp_table->distinct)
 
2352
      curr_join->select_distinct=0;             /* Each row is unique */
 
2353
 
 
2354
    curr_join->join_free();                     /* Free quick selects */
 
2355
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2356
    {
 
2357
      session->set_proc_info("Removing duplicates");
 
2358
      if (curr_join->tmp_having)
 
2359
        curr_join->tmp_having->update_used_tables();
 
2360
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2361
                            *curr_fields_list, curr_join->tmp_having))
 
2362
        return;
 
2363
      curr_join->tmp_having=0;
 
2364
      curr_join->select_distinct=0;
 
2365
    }
 
2366
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2367
    if (make_simple_join(curr_join, curr_tmp_table))
 
2368
      return;
 
2369
    calc_group_buffer(curr_join, curr_join->group_list);
 
2370
    count_field_types(select_lex, &curr_join->tmp_table_param,
 
2371
                      *curr_all_fields, 0);
 
2372
 
 
2373
  }
 
2374
 
 
2375
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2376
  {
 
2377
    if (make_group_fields(this, curr_join))
 
2378
    {
 
2379
      return;
 
2380
    }
 
2381
    if (!items3)
 
2382
    {
 
2383
      if (!items0)
 
2384
        init_items_ref_array();
 
2385
      items3= ref_pointer_array + (all_fields.elements*4);
 
2386
      setup_copy_fields(session, &curr_join->tmp_table_param,
 
2387
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2388
                        curr_fields_list->elements, *curr_all_fields);
 
2389
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2390
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2391
      tmp_table_param.save_copy_field_end=
 
2392
        curr_join->tmp_table_param.copy_field_end;
 
2393
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2394
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2395
    }
 
2396
    else
 
2397
    {
 
2398
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2399
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2400
      curr_join->tmp_table_param.copy_field_end=
 
2401
        tmp_table_param.save_copy_field_end;
 
2402
    }
 
2403
    curr_fields_list= &tmp_fields_list3;
 
2404
    curr_all_fields= &tmp_all_fields3;
 
2405
    curr_join->set_items_ref_array(items3);
 
2406
 
 
2407
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2408
                                      1, true) ||
 
2409
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2410
        session->is_fatal_error)
 
2411
      return;
 
2412
  }
 
2413
  if (curr_join->group_list || curr_join->order)
 
2414
  {
 
2415
    session->set_proc_info("Sorting result");
 
2416
    /* If we have already done the group, add HAVING to sorted table */
 
2417
    if (curr_join->tmp_having && ! curr_join->group_list &&
 
2418
        ! curr_join->sort_and_group)
 
2419
    {
 
2420
      // Some tables may have been const
 
2421
      curr_join->tmp_having->update_used_tables();
 
2422
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2423
      table_map used_tables= (curr_join->const_table_map |
 
2424
                              curr_table->table->map);
 
2425
 
 
2426
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2427
                                                 used_tables,
 
2428
                                                 used_tables, 0);
 
2429
      if (sort_table_cond)
 
2430
      {
 
2431
        if (!curr_table->select)
 
2432
          if (!(curr_table->select= new SQL_SELECT))
 
2433
            return;
 
2434
        if (!curr_table->select->cond)
 
2435
          curr_table->select->cond= sort_table_cond;
 
2436
        else                                    // This should never happen
 
2437
        {
 
2438
          if (!(curr_table->select->cond=
 
2439
                new Item_cond_and(curr_table->select->cond,
 
2440
                                  sort_table_cond)))
 
2441
            return;
 
2442
          /*
 
2443
            Item_cond_and do not need fix_fields for execution, its parameters
 
2444
            are fixed or do not need fix_fields, too
 
2445
          */
 
2446
          curr_table->select->cond->quick_fix_field();
 
2447
        }
 
2448
        curr_table->select_cond= curr_table->select->cond;
 
2449
        curr_table->select_cond->top_level_item();
 
2450
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2451
                                                   ~ (table_map) 0,
 
2452
                                                   ~used_tables, 0);
 
2453
      }
 
2454
    }
 
2455
    {
 
2456
      if (group)
 
2457
        curr_join->select_limit= HA_POS_ERROR;
 
2458
      else
 
2459
      {
 
2460
        /*
 
2461
          We can abort sorting after session->select_limit rows if we there is no
 
2462
          WHERE clause for any tables after the sorted one.
 
2463
        */
 
2464
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2465
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2466
        for (; curr_table < end_table ; curr_table++)
 
2467
        {
 
2468
          /*
 
2469
            table->keyuse is set in the case there was an original WHERE clause
 
2470
            on the table that was optimized away.
 
2471
          */
 
2472
          if (curr_table->select_cond ||
 
2473
              (curr_table->keyuse && !curr_table->first_inner))
 
2474
          {
 
2475
            /* We have to sort all rows */
 
2476
            curr_join->select_limit= HA_POS_ERROR;
 
2477
            break;
 
2478
          }
 
2479
        }
 
2480
      }
 
2481
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2482
      {
 
2483
        return;
 
2484
      }
 
2485
      /*
 
2486
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2487
        chose FILESORT to be faster than INDEX SCAN or there is no
 
2488
        suitable index present.
 
2489
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2490
        finally replace sorting with index scan if there is a LIMIT clause in
 
2491
        the query. XXX: it's never shown in EXPLAIN!
 
2492
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2493
      */
 
2494
      if (create_sort_index(session, curr_join,
 
2495
                            curr_join->group_list ?
 
2496
                            curr_join->group_list : curr_join->order,
 
2497
                            curr_join->select_limit,
 
2498
                            (select_options & OPTION_FOUND_ROWS ?
 
2499
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2500
                            curr_join->group_list ? true : false))
 
2501
        return;
 
2502
      sortorder= curr_join->sortorder;
 
2503
      if (curr_join->const_tables != curr_join->tables &&
 
2504
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2505
      {
 
2506
        /*
 
2507
          If no IO cache exists for the first table then we are using an
 
2508
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2509
          attribute on the INDEX SCAN.
 
2510
        */
 
2511
        skip_sort_order= 1;
 
2512
      }
 
2513
    }
 
2514
  }
 
2515
  /* XXX: When can we have here session->is_error() not zero? */
 
2516
  if (session->is_error())
 
2517
  {
 
2518
    error= session->is_error();
 
2519
    return;
 
2520
  }
 
2521
  curr_join->having= curr_join->tmp_having;
 
2522
  curr_join->fields= curr_fields_list;
 
2523
 
 
2524
  {
 
2525
    session->set_proc_info("Sending data");
 
2526
    result->send_fields(*curr_fields_list,
 
2527
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2528
    error= do_select(curr_join, curr_fields_list, NULL);
 
2529
    session->limit_found_rows= curr_join->send_records;
 
2530
  }
 
2531
 
 
2532
  /* Accumulate the counts from all join iterations of all join parts. */
 
2533
  session->examined_row_count+= curr_join->examined_rows;
 
2534
 
 
2535
  /*
 
2536
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2537
    for a derived table which is always materialized.
 
2538
    Otherwise we would not be able to print the query  correctly.
 
2539
  */
 
2540
  if (items0 &&
 
2541
      (session->lex->describe & DESCRIBE_EXTENDED) &&
 
2542
      select_lex->linkage == DERIVED_TABLE_TYPE)
 
2543
    set_items_ref_array(items0);
 
2544
 
 
2545
  return;
 
2546
}
 
2547
 
 
2548
 
 
2549
/**
 
2550
  Clean up join.
 
2551
 
 
2552
  @return
 
2553
    Return error that hold JOIN.
 
2554
*/
 
2555
 
 
2556
int
 
2557
JOIN::destroy()
 
2558
{
 
2559
  select_lex->join= 0;
 
2560
 
 
2561
  if (tmp_join)
 
2562
  {
 
2563
    if (join_tab != tmp_join->join_tab)
 
2564
    {
 
2565
      JOIN_TAB *tab, *end;
 
2566
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2567
        tab->cleanup();
 
2568
    }
 
2569
    tmp_join->tmp_join= 0;
 
2570
    tmp_table_param.copy_field=0;
 
2571
    return(tmp_join->destroy());
 
2572
  }
 
2573
  cond_equal= 0;
 
2574
 
 
2575
  cleanup(1);
 
2576
  if (exec_tmp_table1)
 
2577
    exec_tmp_table1->free_tmp_table(session);
 
2578
  if (exec_tmp_table2)
 
2579
    exec_tmp_table2->free_tmp_table(session);
 
2580
  delete select;
 
2581
  delete_dynamic(&keyuse);
 
2582
  return(error);
 
2583
}
 
2584
 
 
2585
 
 
2586
 
316
2587
/**
317
2588
  An entry point to single-unit select (a select without UNION).
318
2589
 
319
 
  @param session                  thread Cursor
 
2590
  @param session                  thread handler
320
2591
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2592
                              the top-level select_lex for this query
322
2593
  @param tables               list of all tables used in this query.
332
2603
                              for a, b and c in this list.
333
2604
  @param conds                top level item of an expression representing
334
2605
                              WHERE clause of the top level select
335
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2606
  @param og_num               total number of order_st BY and GROUP BY clauses
336
2607
                              arguments
337
 
  @param order                linked list of ORDER BY agruments
 
2608
  @param order                linked list of order_st BY agruments
338
2609
  @param group                linked list of GROUP BY arguments
339
2610
  @param having               top level item of HAVING expression
 
2611
  @param proc_param           list of PROCEDUREs
340
2612
  @param select_options       select options (BIG_RESULT, etc)
341
2613
  @param result               an instance of result set handling class.
342
2614
                              This object is responsible for send result
355
2627
  @retval
356
2628
    true   an error
357
2629
*/
358
 
bool select_query(Session *session,
359
 
                  Item ***rref_pointer_array,
360
 
                  TableList *tables, 
361
 
                  uint32_t wild_num, 
362
 
                  List<Item> &fields,
363
 
                  COND *conds, 
364
 
                  uint32_t og_num,  
365
 
                  Order *order,
366
 
                  Order *group,
367
 
                  Item *having, 
368
 
                  uint64_t select_options,
369
 
                  select_result *result, 
370
 
                  Select_Lex_Unit *unit,
371
 
                  Select_Lex *select_lex)
 
2630
 
 
2631
bool
 
2632
mysql_select(Session *session, Item ***rref_pointer_array,
 
2633
             TableList *tables, uint32_t wild_num, List<Item> &fields,
 
2634
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2635
             Item *having, order_st *proc_param, uint64_t select_options,
 
2636
             select_result *result, Select_Lex_Unit *unit,
 
2637
             Select_Lex *select_lex)
372
2638
{
373
2639
  bool err;
374
2640
  bool free_join= 1;
375
2641
 
376
2642
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2643
  JOIN *join;
378
2644
  if (select_lex->join != 0)
379
2645
  {
380
2646
    join= select_lex->join;
383
2649
      creation
384
2650
    */
385
2651
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2652
        (select_options & SELECT_DESCRIBE))
387
2653
    {
388
2654
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2655
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2656
        //here is EXPLAIN of subselect or derived table
 
2657
        if (join->change_result(result))
 
2658
        {
 
2659
          return(true);
 
2660
        }
395
2661
      }
396
2662
      else
397
2663
      {
398
2664
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
399
 
                               conds, og_num, order, group, having, select_lex, unit)))
400
 
        {
401
 
          goto err;
402
 
        }
 
2665
                               conds, og_num, order, group, having, proc_param,
 
2666
                               select_lex, unit)))
 
2667
        {
 
2668
          goto err;
 
2669
        }
403
2670
      }
404
2671
    }
405
2672
    free_join= 0;
407
2674
  }
408
2675
  else
409
2676
  {
410
 
    if (!(join= new Join(session, fields, select_options, result)))
411
 
      return(true);
 
2677
    if (!(join= new JOIN(session, fields, select_options, result)))
 
2678
        return(true);
412
2679
    session->set_proc_info("init");
413
2680
    session->used_tables=0;                         // Updated by setup_fields
414
2681
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2682
                           conds, og_num, order, group, having, proc_param,
416
2683
                           select_lex, unit)) == true)
417
2684
    {
418
2685
      goto err;
419
2686
    }
420
2687
  }
421
2688
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2689
  if (join->flatten_subqueries())
 
2690
  {
 
2691
    err= 1;
 
2692
    goto err;
 
2693
  }
 
2694
 
 
2695
  if ((err= join->optimize()))
 
2696
  {
 
2697
    goto err;                                   // 1
 
2698
  }
 
2699
 
 
2700
  if (session->lex->describe & DESCRIBE_EXTENDED)
429
2701
  {
430
2702
    join->conds_history= join->conds;
431
2703
    join->having_history= (join->having?join->having:join->tmp_having);
436
2708
 
437
2709
  join->exec();
438
2710
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2711
  if (session->lex->describe & DESCRIBE_EXTENDED)
440
2712
  {
441
2713
    select_lex->where= join->conds_history;
442
2714
    select_lex->having= join->having_history;
452
2724
  return(join->error);
453
2725
}
454
2726
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2727
 
 
2728
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
 
2729
                          Item_in_subselect* const *el2)
 
2730
{
 
2731
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
 
2732
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2733
}
 
2734
 
 
2735
 
 
2736
inline Item * and_items(Item* cond, Item *item)
456
2737
{
457
2738
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2739
}
459
2740
 
 
2741
 
 
2742
static TableList *alloc_join_nest(Session *session)
 
2743
{
 
2744
  TableList *tbl;
 
2745
  if (!(tbl= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2746
                                       sizeof(nested_join_st))))
 
2747
    return NULL;
 
2748
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl +
 
2749
                                    ALIGN_SIZE(sizeof(TableList)));
 
2750
  return tbl;
 
2751
}
 
2752
 
 
2753
 
 
2754
void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
 
2755
{
 
2756
  List_iterator<TableList> it(*tlist);
 
2757
  TableList *table;
 
2758
  while ((table= it++))
 
2759
  {
 
2760
    if (table->on_expr)
 
2761
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2762
    if (table->nested_join)
 
2763
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2764
  }
 
2765
}
 
2766
 
 
2767
 
 
2768
/*
 
2769
  Convert a subquery predicate into a TableList semi-join nest
 
2770
 
 
2771
  SYNOPSIS
 
2772
    convert_subq_to_sj()
 
2773
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON
 
2774
                    clause
 
2775
       subq_pred    Subquery predicate to be converted
 
2776
 
 
2777
  DESCRIPTION
 
2778
    Convert a subquery predicate into a TableList semi-join nest. All the
 
2779
    prerequisites are already checked, so the conversion is always successfull.
 
2780
 
 
2781
    Prepared Statements: the transformation is permanent:
 
2782
     - Changes in TableList structures are naturally permanent
 
2783
     - Item tree changes are performed on statement MEM_ROOT:
 
2784
        = we activate statement MEM_ROOT
 
2785
        = this function is called before the first fix_prepare_information
 
2786
          call.
 
2787
 
 
2788
    This is intended because the criteria for subquery-to-sj conversion remain
 
2789
    constant for the lifetime of the Prepared Statement.
 
2790
 
 
2791
  RETURN
 
2792
    false  OK
 
2793
    true   Out of memory error
 
2794
*/
 
2795
 
 
2796
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2797
{
 
2798
  Select_Lex *parent_lex= parent_join->select_lex;
 
2799
  TableList *emb_tbl_nest= NULL;
 
2800
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2801
  Session *session= parent_join->session;
 
2802
 
 
2803
  /*
 
2804
    1. Find out where to put the predicate into.
 
2805
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2806
  */
 
2807
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2808
  {
 
2809
    if (subq_pred->expr_join_nest->nested_join)
 
2810
    {
 
2811
      /*
 
2812
        We're dealing with
 
2813
 
 
2814
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2815
 
 
2816
        The sj-nest will be inserted into the brackets nest.
 
2817
      */
 
2818
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2819
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2820
    }
 
2821
    else if (!subq_pred->expr_join_nest->outer_join)
 
2822
    {
 
2823
      /*
 
2824
        We're dealing with
 
2825
 
 
2826
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2827
 
 
2828
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2829
        parent. This is ok because tblX is joined as an inner join.
 
2830
      */
 
2831
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2832
      if (emb_tbl_nest)
 
2833
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2834
    }
 
2835
    else if (!subq_pred->expr_join_nest->nested_join)
 
2836
    {
 
2837
      TableList *outer_tbl= subq_pred->expr_join_nest;
 
2838
      TableList *wrap_nest;
 
2839
      /*
 
2840
        We're dealing with
 
2841
 
 
2842
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2843
 
 
2844
        we'll need to convert it into:
 
2845
 
 
2846
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2847
                        |                      |
 
2848
                        |<----- wrap_nest ---->|
 
2849
 
 
2850
        Q:  other subqueries may be pointing to this element. What to do?
 
2851
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2852
            But we'll need to fix other pointers.
 
2853
        A2: Another way: have TableList::next_ptr so the following
 
2854
            subqueries know the table has been nested.
 
2855
        A3: changes in the TableList::outer_join will make everything work
 
2856
            automatically.
 
2857
      */
 
2858
      if (!(wrap_nest= alloc_join_nest(parent_join->session)))
 
2859
      {
 
2860
        return(true);
 
2861
      }
 
2862
      wrap_nest->embedding= outer_tbl->embedding;
 
2863
      wrap_nest->join_list= outer_tbl->join_list;
 
2864
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2865
 
 
2866
      wrap_nest->nested_join->join_list.empty();
 
2867
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2868
 
 
2869
      outer_tbl->embedding= wrap_nest;
 
2870
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2871
 
 
2872
      /*
 
2873
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2874
        and on_expr
 
2875
      */
 
2876
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2877
      outer_tbl->outer_join= 0;
 
2878
 
 
2879
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2880
      outer_tbl->on_expr= NULL;
 
2881
 
 
2882
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2883
      TableList *tbl;
 
2884
      while ((tbl= li++))
 
2885
      {
 
2886
        if (tbl == outer_tbl)
 
2887
        {
 
2888
          li.replace(wrap_nest);
 
2889
          break;
 
2890
        }
 
2891
      }
 
2892
      /*
 
2893
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
 
2894
        semi-join nest into it
 
2895
      */
 
2896
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2897
      emb_tbl_nest=  wrap_nest;
 
2898
    }
 
2899
  }
 
2900
 
 
2901
  TableList *sj_nest;
 
2902
  nested_join_st *nested_join;
 
2903
  if (!(sj_nest= alloc_join_nest(parent_join->session)))
 
2904
  {
 
2905
    return(true);
 
2906
  }
 
2907
  nested_join= sj_nest->nested_join;
 
2908
 
 
2909
  sj_nest->join_list= emb_join_list;
 
2910
  sj_nest->embedding= emb_tbl_nest;
 
2911
  sj_nest->alias= (char*) "(sj-nest)";
 
2912
  /* Nests do not participate in those 'chains', so: */
 
2913
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2914
  emb_join_list->push_back(sj_nest);
 
2915
 
 
2916
  /*
 
2917
    nested_join->used_tables and nested_join->not_null_tables are
 
2918
    initialized in simplify_joins().
 
2919
  */
 
2920
 
 
2921
  /*
 
2922
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2923
       sj-nest.
 
2924
  */
 
2925
  Select_Lex *subq_lex= subq_pred->unit->first_select();
 
2926
  nested_join->join_list.empty();
 
2927
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2928
  TableList *tl, *last_leaf;
 
2929
  while ((tl= li++))
 
2930
  {
 
2931
    tl->embedding= sj_nest;
 
2932
    tl->join_list= &nested_join->join_list;
 
2933
    nested_join->join_list.push_back(tl);
 
2934
  }
 
2935
 
 
2936
  /*
 
2937
    Reconnect the next_leaf chain.
 
2938
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2939
          Inserting them at the beginning would be a bit faster.
 
2940
    NOTE: We actually insert them at the front! That's because the order is
 
2941
          reversed in this list.
 
2942
  */
 
2943
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2944
  tl->next_leaf= subq_lex->leaf_tables;
 
2945
  last_leaf= tl;
 
2946
 
 
2947
  /*
 
2948
    Same as above for next_local chain
 
2949
    (a theory: a next_local chain always starts with ::leaf_tables
 
2950
     because view's tables are inserted after the view)
 
2951
  */
 
2952
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2953
  tl->next_local= subq_lex->leaf_tables;
 
2954
 
 
2955
  /* A theory: no need to re-connect the next_global chain */
 
2956
 
 
2957
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2958
 
 
2959
  // The subqueries were replaced for Item_int(1) earlier
 
2960
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2961
  /*TODO: also reset the 'with_subselect' there. */
 
2962
 
 
2963
  /* n. Adjust the parent_join->tables counter */
 
2964
  uint32_t table_no= parent_join->tables;
 
2965
  /* n. Walk through child's tables and adjust table->map */
 
2966
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2967
  {
 
2968
    tl->table->tablenr= table_no;
 
2969
    tl->table->map= ((table_map)1) << table_no;
 
2970
    Select_Lex *old_sl= tl->select_lex;
 
2971
    tl->select_lex= parent_join->select_lex;
 
2972
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2973
      emb->select_lex= parent_join->select_lex;
 
2974
  }
 
2975
  parent_join->tables += subq_lex->join->tables;
 
2976
 
 
2977
  /*
 
2978
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2979
    Add the subquery-induced equalities too.
 
2980
  */
 
2981
  Select_Lex *save_lex= session->lex->current_select;
 
2982
  session->lex->current_select=subq_lex;
 
2983
  if (!subq_pred->left_expr->fixed &&
 
2984
       subq_pred->left_expr->fix_fields(session, &subq_pred->left_expr))
 
2985
    return(true);
 
2986
  session->lex->current_select=save_lex;
 
2987
 
 
2988
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2989
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2990
                                        subq_pred->left_expr->used_tables();
 
2991
  sj_nest->sj_on_expr= subq_lex->where;
 
2992
 
 
2993
  /*
 
2994
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2995
    Additionally, for InsideOut strategy
 
2996
     - Record the number of IN-equalities.
 
2997
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2998
       see which of the expressions are bound and which are not (for those
 
2999
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
3000
 
 
3001
       (TODO: can we just create a list of pointers and hope the expressions
 
3002
       will not substitute themselves on fix_fields()? or we need to wrap
 
3003
       them into Item_direct_view_refs and store pointers to those. The
 
3004
       pointers to Item_direct_view_refs are guaranteed to be stable as
 
3005
       Item_direct_view_refs doesn't substitute itself with anything in
 
3006
       Item_direct_view_ref::fix_fields.
 
3007
  */
 
3008
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3009
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3010
 
 
3011
  if (subq_pred->left_expr->cols() == 1)
 
3012
  {
 
3013
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3014
 
 
3015
    Item *item_eq= new Item_func_eq(subq_pred->left_expr,
 
3016
                                    subq_lex->ref_pointer_array[0]);
 
3017
    item_eq->name= (char*)subq_sj_cond_name;
 
3018
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3019
  }
 
3020
  else
 
3021
  {
 
3022
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3023
    {
 
3024
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3025
                                                element_index(i));
 
3026
      Item *item_eq=
 
3027
        new Item_func_eq(subq_pred->left_expr->element_index(i),
 
3028
                         subq_lex->ref_pointer_array[i]);
 
3029
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3030
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3031
    }
 
3032
  }
 
3033
  /* Fix the created equality and AND */
 
3034
  sj_nest->sj_on_expr->fix_fields(parent_join->session, &sj_nest->sj_on_expr);
 
3035
 
 
3036
  /*
 
3037
    Walk through sj nest's WHERE and ON expressions and call
 
3038
    item->fix_table_changes() for all items.
 
3039
  */
 
3040
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3041
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3042
 
 
3043
 
 
3044
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3045
  subq_lex->master_unit()->exclude_level();
 
3046
 
 
3047
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3048
  if (emb_tbl_nest)
 
3049
  {
 
3050
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
 
3051
                                     sj_nest->sj_on_expr);
 
3052
    emb_tbl_nest->on_expr->fix_fields(parent_join->session, &emb_tbl_nest->on_expr);
 
3053
  }
 
3054
  else
 
3055
  {
 
3056
    /* Inject into the WHERE */
 
3057
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3058
    parent_join->conds->fix_fields(parent_join->session, &parent_join->conds);
 
3059
    parent_join->select_lex->where= parent_join->conds;
 
3060
  }
 
3061
 
 
3062
  return(false);
 
3063
}
 
3064
 
 
3065
 
 
3066
/*
 
3067
  Convert candidate subquery predicates to semi-joins
 
3068
 
 
3069
  SYNOPSIS
 
3070
    JOIN::flatten_subqueries()
 
3071
 
 
3072
  DESCRIPTION
 
3073
    Convert candidate subquery predicates to semi-joins.
 
3074
 
 
3075
  RETURN
 
3076
    false  OK
 
3077
    true   Error
 
3078
*/
 
3079
 
 
3080
bool JOIN::flatten_subqueries()
 
3081
{
 
3082
  Item_in_subselect **in_subq;
 
3083
  Item_in_subselect **in_subq_end;
 
3084
 
 
3085
  if (sj_subselects.elements() == 0)
 
3086
    return(false);
 
3087
 
 
3088
  /* 1. Fix children subqueries */
 
3089
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
 
3090
       in_subq != in_subq_end; in_subq++)
 
3091
  {
 
3092
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3093
    child_join->outer_tables = child_join->tables;
 
3094
    if (child_join->flatten_subqueries())
 
3095
      return(true);
 
3096
    (*in_subq)->sj_convert_priority=
 
3097
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3098
  }
 
3099
  
 
3100
  bool outer_join_disable_semi_join= false;
 
3101
  /*
 
3102
   * Temporary measure: disable semi-joins when they are together with outer
 
3103
   * joins.
 
3104
   *
 
3105
   * @see LP Bug #314911
 
3106
   */
 
3107
  for (TableList *tbl= select_lex->leaf_tables; tbl; tbl=tbl->next_leaf)
 
3108
  {
 
3109
    TableList *embedding= tbl->embedding;
 
3110
    if (tbl->on_expr || (tbl->embedding && !(embedding->sj_on_expr && 
 
3111
                                            !embedding->embedding)))
 
3112
    {
 
3113
      in_subq= sj_subselects.front();
 
3114
      outer_join_disable_semi_join= true;
 
3115
    }
 
3116
  }
 
3117
 
 
3118
  if (! outer_join_disable_semi_join)
 
3119
  {
 
3120
    /*
 
3121
      2. Pick which subqueries to convert:
 
3122
        sort the subquery array
 
3123
        - prefer correlated subqueries over uncorrelated;
 
3124
        - prefer subqueries that have greater number of outer tables;
 
3125
    */
 
3126
    sj_subselects.sort(subq_sj_candidate_cmp);
 
3127
    // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3128
    /* Replace all subqueries to be flattened with Item_int(1) */
 
3129
    for (in_subq= sj_subselects.front();
 
3130
        in_subq != in_subq_end &&
 
3131
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3132
        in_subq++)
 
3133
    {
 
3134
      if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3135
        return(true);
 
3136
    }
 
3137
 
 
3138
    for (in_subq= sj_subselects.front();
 
3139
        in_subq != in_subq_end &&
 
3140
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3141
        in_subq++)
 
3142
    {
 
3143
      if (convert_subq_to_sj(this, *in_subq))
 
3144
        return(true);
 
3145
    }
 
3146
  }
 
3147
 
 
3148
  /* 3. Finalize those we didn't convert */
 
3149
  for (; in_subq!= in_subq_end; in_subq++)
 
3150
  {
 
3151
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3152
    Item_subselect::trans_res res;
 
3153
    (*in_subq)->changed= 0;
 
3154
    (*in_subq)->fixed= 0;
 
3155
    res= (*in_subq)->select_transformer(child_join);
 
3156
    if (res == Item_subselect::RES_ERROR)
 
3157
      return(true);
 
3158
 
 
3159
    (*in_subq)->changed= 1;
 
3160
    (*in_subq)->fixed= 1;
 
3161
 
 
3162
    Item *substitute= (*in_subq)->substitution;
 
3163
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3164
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3165
      return(true);
 
3166
 
 
3167
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
 
3168
    //  return(true);
 
3169
  }
 
3170
  sj_subselects.clear();
 
3171
  return(false);
 
3172
}
 
3173
 
 
3174
 
 
3175
/**
 
3176
  Setup for execution all subqueries of a query, for which the optimizer
 
3177
  chose hash semi-join.
 
3178
 
 
3179
  @details Iterate over all subqueries of the query, and if they are under an
 
3180
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3181
  - try to initialize all data structures needed for the materialized execution
 
3182
    of the IN predicate,
 
3183
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3184
    previously blocked during JOIN::prepare.
 
3185
 
 
3186
  This method is part of the "code generation" query processing phase.
 
3187
 
 
3188
  This phase must be called after substitute_for_best_equal_field() because
 
3189
  that function may replace items with other items from a multiple equality,
 
3190
  and we need to reference the correct items in the index access method of the
 
3191
  IN predicate.
 
3192
 
 
3193
  @return Operation status
 
3194
  @retval false     success.
 
3195
  @retval true      error occurred.
 
3196
*/
 
3197
 
 
3198
bool JOIN::setup_subquery_materialization()
 
3199
{
 
3200
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
 
3201
       un= un->next_unit())
 
3202
  {
 
3203
    for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
 
3204
    {
 
3205
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3206
      if (subquery_predicate &&
 
3207
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3208
      {
 
3209
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3210
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3211
            in_subs->setup_engine())
 
3212
          return true;
 
3213
      }
 
3214
    }
 
3215
  }
 
3216
  return false;
 
3217
}
 
3218
 
 
3219
 
 
3220
/*
 
3221
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3222
 
 
3223
  SYNOPSIS
 
3224
    find_eq_ref_candidate()
 
3225
      table             Table to be checked
 
3226
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3227
                        count.
 
3228
 
 
3229
  DESCRIPTION
 
3230
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3231
 
 
3232
  TODO
 
3233
    Check again if it is feasible to factor common parts with constant table
 
3234
    search
 
3235
 
 
3236
  RETURN
 
3237
    true  - There exists an eq_ref(outer-tables) candidate
 
3238
    false - Otherwise
 
3239
*/
 
3240
 
 
3241
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3242
{
 
3243
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3244
  uint32_t key;
 
3245
 
 
3246
  if (keyuse)
 
3247
  {
 
3248
    while (1) /* For each key */
 
3249
    {
 
3250
      key= keyuse->key;
 
3251
      KEY *keyinfo= table->key_info + key;
 
3252
      key_part_map bound_parts= 0;
 
3253
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3254
      {
 
3255
        do  /* For all equalities on all key parts */
 
3256
        {
 
3257
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3258
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3259
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3260
          {
 
3261
            bound_parts |= 1 << keyuse->keypart;
 
3262
          }
 
3263
          keyuse++;
 
3264
        } while (keyuse->key == key && keyuse->table == table);
 
3265
 
 
3266
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3267
          return true;
 
3268
        if (keyuse->table != table)
 
3269
          return false;
 
3270
      }
 
3271
      else
 
3272
      {
 
3273
        do
 
3274
        {
 
3275
          keyuse++;
 
3276
          if (keyuse->table != table)
 
3277
            return false;
 
3278
        }
 
3279
        while (keyuse->key == key);
 
3280
      }
 
3281
    }
 
3282
  }
 
3283
  return false;
 
3284
}
 
3285
 
 
3286
 
 
3287
/*
 
3288
  Pull tables out of semi-join nests, if possible
 
3289
 
 
3290
  SYNOPSIS
 
3291
    pull_out_semijoin_tables()
 
3292
      join  The join where to do the semi-join flattening
 
3293
 
 
3294
  DESCRIPTION
 
3295
    Try to pull tables out of semi-join nests.
 
3296
 
 
3297
    PRECONDITIONS
 
3298
    When this function is called, the join may have several semi-join nests
 
3299
    (possibly within different semi-join nests), but it is guaranteed that
 
3300
    one semi-join nest does not contain another.
 
3301
 
 
3302
    ACTION
 
3303
    A table can be pulled out of the semi-join nest if
 
3304
     - It is a constant table
 
3305
     - It is accessed
 
3306
 
 
3307
    POSTCONDITIONS
 
3308
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3309
       tables)
 
3310
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3311
     * Semi-join nests TableList::sj_inner_tables
 
3312
 
 
3313
    This operation is (and should be) performed at each PS execution since
 
3314
    tables may become/cease to be constant across PS reexecutions.
 
3315
 
 
3316
  RETURN
 
3317
    0 - OK
 
3318
    1 - Out of memory error
 
3319
*/
 
3320
 
 
3321
int pull_out_semijoin_tables(JOIN *join)
 
3322
{
 
3323
  TableList *sj_nest;
 
3324
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3325
 
 
3326
  /* Try pulling out of the each of the semi-joins */
 
3327
  while ((sj_nest= sj_list_it++))
 
3328
  {
 
3329
    /* Action #1: Mark the constant tables to be pulled out */
 
3330
    table_map pulled_tables= 0;
 
3331
 
 
3332
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3333
    TableList *tbl;
 
3334
    while ((tbl= child_li++))
 
3335
    {
 
3336
      if (tbl->table)
 
3337
      {
 
3338
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3339
        if (tbl->table->map & join->const_table_map)
 
3340
        {
 
3341
          pulled_tables |= tbl->table->map;
 
3342
        }
 
3343
      }
 
3344
    }
 
3345
 
 
3346
    /*
 
3347
      Action #2: Find which tables we can pull out based on
 
3348
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3349
      us to pull out some other tables too.
 
3350
    */
 
3351
    bool pulled_a_table;
 
3352
    do
 
3353
    {
 
3354
      pulled_a_table= false;
 
3355
      child_li.rewind();
 
3356
      while ((tbl= child_li++))
 
3357
      {
 
3358
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3359
        {
 
3360
          if (find_eq_ref_candidate(tbl->table,
 
3361
                                    sj_nest->nested_join->used_tables &
 
3362
                                    ~pulled_tables))
 
3363
          {
 
3364
            pulled_a_table= true;
 
3365
            pulled_tables |= tbl->table->map;
 
3366
          }
 
3367
        }
 
3368
      }
 
3369
    } while (pulled_a_table);
 
3370
 
 
3371
    child_li.rewind();
 
3372
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3373
    {
 
3374
      (sj_nest)->sj_inner_tables= 0;
 
3375
      while ((tbl= child_li++))
 
3376
      {
 
3377
        if (tbl->table)
 
3378
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3379
      }
 
3380
    }
 
3381
    else
 
3382
    {
 
3383
      /* Record the bitmap of inner tables, mark the inner tables */
 
3384
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
 
3385
                             ~pulled_tables;
 
3386
      (sj_nest)->sj_inner_tables= inner_tables;
 
3387
      while ((tbl= child_li++))
 
3388
      {
 
3389
        if (tbl->table)
 
3390
        {
 
3391
          if (inner_tables & tbl->table->map)
 
3392
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3393
          else
 
3394
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3395
        }
 
3396
      }
 
3397
    }
 
3398
  }
 
3399
  return(0);
 
3400
}
 
3401
 
460
3402
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3403
  Create JOIN_TABS, make a guess about the table types,
462
3404
  Approximate how many records will be used in each table
463
3405
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3406
 
 
3407
 
 
3408
static ha_rows get_quick_record_count(Session *session, SQL_SELECT *select,
 
3409
                                      Table *table,
 
3410
                                      const key_map *keys,ha_rows limit)
465
3411
{
466
3412
  int error;
467
3413
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
482
3428
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3429
}
484
3430
 
 
3431
/*
 
3432
   This structure is used to collect info on potentially sargable
 
3433
   predicates in order to check whether they become sargable after
 
3434
   reading const tables.
 
3435
   We form a bitmap of indexes that can be used for sargable predicates.
 
3436
   Only such indexes are involved in range analysis.
 
3437
*/
 
3438
typedef struct st_sargable_param
 
3439
{
 
3440
  Field *field;              /* field against which to check sargability */
 
3441
  Item **arg_value;          /* values of potential keys for lookups     */
 
3442
  uint32_t num_values;           /* number of values in the above array      */
 
3443
} SARGABLE_PARAM;
 
3444
 
 
3445
/**
 
3446
  Calculate the best possible join and initialize the join structure.
 
3447
 
 
3448
  @retval
 
3449
    0   ok
 
3450
  @retval
 
3451
    1   Fatal error
 
3452
*/
 
3453
 
 
3454
static bool
 
3455
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3456
                     DYNAMIC_ARRAY *keyuse_array)
 
3457
{
 
3458
  int error;
 
3459
  Table *table;
 
3460
  uint32_t i,table_count,const_count,key;
 
3461
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3462
  key_map const_ref, eq_part;
 
3463
  Table **table_vector;
 
3464
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3465
  KEYUSE *keyuse,*start_keyuse;
 
3466
  table_map outer_join=0;
 
3467
  SARGABLE_PARAM *sargables= 0;
 
3468
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3469
 
 
3470
  table_count=join->tables;
 
3471
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
 
3472
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3473
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
3474
  if (!stat || !stat_ref || !table_vector)
 
3475
    return(1);                          // Eom /* purecov: inspected */
 
3476
 
 
3477
  join->best_ref=stat_vector;
 
3478
 
 
3479
  stat_end=stat+table_count;
 
3480
  found_const_table_map= all_table_map=0;
 
3481
  const_count=0;
 
3482
 
 
3483
  for (s= stat, i= 0;
 
3484
       tables;
 
3485
       s++, tables= tables->next_leaf, i++)
 
3486
  {
 
3487
    TableList *embedding= tables->embedding;
 
3488
    stat_vector[i]=s;
 
3489
    s->keys.init();
 
3490
    s->const_keys.init();
 
3491
    s->checked_keys.init();
 
3492
    s->needed_reg.init();
 
3493
    table_vector[i]=s->table=table=tables->table;
 
3494
    table->pos_in_table_list= tables;
 
3495
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3496
    if(error)
 
3497
    {
 
3498
        table->file->print_error(error, MYF(0));
 
3499
        return(1);
 
3500
    }
 
3501
    table->quick_keys.clear_all();
 
3502
    table->reginfo.join_tab=s;
 
3503
    table->reginfo.not_exists_optimize=0;
 
3504
    memset(table->const_key_parts, 0,
 
3505
           sizeof(key_part_map)*table->s->keys);
 
3506
    all_table_map|= table->map;
 
3507
    s->join=join;
 
3508
    s->info=0;                                  // For describe
 
3509
 
 
3510
    s->dependent= tables->dep_tables;
 
3511
    s->key_dependent= 0;
 
3512
    if (tables->schema_table)
 
3513
      table->file->stats.records= 2;
 
3514
    table->quick_condition_rows= table->file->stats.records;
 
3515
 
 
3516
    s->on_expr_ref= &tables->on_expr;
 
3517
    if (*s->on_expr_ref)
 
3518
    {
 
3519
      /* s is the only inner table of an outer join */
 
3520
      if (!table->file->stats.records && !embedding)
 
3521
      {                                         // Empty table
 
3522
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3523
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3524
        continue;
 
3525
      }
 
3526
      outer_join|= table->map;
 
3527
      s->embedding_map= 0;
 
3528
      for (;embedding; embedding= embedding->embedding)
 
3529
        s->embedding_map|= embedding->nested_join->nj_map;
 
3530
      continue;
 
3531
    }
 
3532
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3533
    {
 
3534
      /* s belongs to a nested join, maybe to several embedded joins */
 
3535
      s->embedding_map= 0;
 
3536
      do
 
3537
      {
 
3538
        nested_join_st *nested_join= embedding->nested_join;
 
3539
        s->embedding_map|=nested_join->nj_map;
 
3540
        s->dependent|= embedding->dep_tables;
 
3541
        embedding= embedding->embedding;
 
3542
        outer_join|= nested_join->used_tables;
 
3543
      }
 
3544
      while (embedding);
 
3545
      continue;
 
3546
    }
 
3547
    if ((table->file->stats.records <= 1) &&
 
3548
        !s->dependent &&
 
3549
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3550
    {
 
3551
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3552
    }
 
3553
  }
 
3554
  stat_vector[i]=0;
 
3555
  join->outer_join=outer_join;
 
3556
 
 
3557
  if (join->outer_join)
 
3558
  {
 
3559
    /*
 
3560
       Build transitive closure for relation 'to be dependent on'.
 
3561
       This will speed up the plan search for many cases with outer joins,
 
3562
       as well as allow us to catch illegal cross references/
 
3563
       Warshall's algorithm is used to build the transitive closure.
 
3564
       As we use bitmaps to represent the relation the complexity
 
3565
       of the algorithm is O((number of tables)^2).
 
3566
    */
 
3567
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3568
    {
 
3569
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3570
      {
 
3571
        table= stat[j].table;
 
3572
        if (s->dependent & table->map)
 
3573
          s->dependent |= table->reginfo.join_tab->dependent;
 
3574
      }
 
3575
      if (s->dependent)
 
3576
        s->table->maybe_null= 1;
 
3577
    }
 
3578
    /* Catch illegal cross references for outer joins */
 
3579
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3580
    {
 
3581
      if (s->dependent & s->table->map)
 
3582
      {
 
3583
        join->tables=0;                 // Don't use join->table
 
3584
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3585
        return(1);
 
3586
      }
 
3587
      s->key_dependent= s->dependent;
 
3588
    }
 
3589
  }
 
3590
 
 
3591
  if (conds || outer_join)
 
3592
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
 
3593
                            conds, join->cond_equal,
 
3594
                            ~outer_join, join->select_lex, &sargables))
 
3595
      return(1);
 
3596
 
 
3597
  /* Read tables with 0 or 1 rows (system tables) */
 
3598
  join->const_table_map= 0;
 
3599
 
 
3600
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3601
       p_pos < p_end ;
 
3602
       p_pos++)
 
3603
  {
 
3604
    int tmp;
 
3605
    s= p_pos->table;
 
3606
    s->type=JT_SYSTEM;
 
3607
    join->const_table_map|=s->table->map;
 
3608
    if ((tmp=join_read_const_table(s, p_pos)))
 
3609
    {
 
3610
      if (tmp > 0)
 
3611
        return(1);                      // Fatal error
 
3612
    }
 
3613
    else
 
3614
      found_const_table_map|= s->table->map;
 
3615
  }
 
3616
 
 
3617
  /* loop until no more const tables are found */
 
3618
  int ref_changed;
 
3619
  do
 
3620
  {
 
3621
  more_const_tables_found:
 
3622
    ref_changed = 0;
 
3623
    found_ref=0;
 
3624
 
 
3625
    /*
 
3626
      We only have to loop from stat_vector + const_count as
 
3627
      set_position() will move all const_tables first in stat_vector
 
3628
    */
 
3629
 
 
3630
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3631
    {
 
3632
      table=s->table;
 
3633
 
 
3634
      /*
 
3635
        If equi-join condition by a key is null rejecting and after a
 
3636
        substitution of a const table the key value happens to be null
 
3637
        then we can state that there are no matches for this equi-join.
 
3638
      */
 
3639
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3640
      {
 
3641
        /*
 
3642
          When performing an outer join operation if there are no matching rows
 
3643
          for the single row of the outer table all the inner tables are to be
 
3644
          null complemented and thus considered as constant tables.
 
3645
          Here we apply this consideration to the case of outer join operations
 
3646
          with a single inner table only because the case with nested tables
 
3647
          would require a more thorough analysis.
 
3648
          TODO. Apply single row substitution to null complemented inner tables
 
3649
          for nested outer join operations.
 
3650
        */
 
3651
        while (keyuse->table == table)
 
3652
        {
 
3653
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3654
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3655
          {
 
3656
            s->type= JT_CONST;
 
3657
            mark_as_null_row(table);
 
3658
            found_const_table_map|= table->map;
 
3659
            join->const_table_map|= table->map;
 
3660
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3661
            goto more_const_tables_found;
 
3662
           }
 
3663
          keyuse++;
 
3664
        }
 
3665
      }
 
3666
 
 
3667
      if (s->dependent)                         // If dependent on some table
 
3668
      {
 
3669
        // All dep. must be constants
 
3670
        if (s->dependent & ~(found_const_table_map))
 
3671
          continue;
 
3672
        if (table->file->stats.records <= 1L &&
 
3673
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3674
            !table->pos_in_table_list->embedding)
 
3675
        {                                       // system table
 
3676
          int tmp= 0;
 
3677
          s->type=JT_SYSTEM;
 
3678
          join->const_table_map|=table->map;
 
3679
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3680
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3681
          {
 
3682
            if (tmp > 0)
 
3683
              return(1);                        // Fatal error
 
3684
          }
 
3685
          else
 
3686
            found_const_table_map|= table->map;
 
3687
          continue;
 
3688
        }
 
3689
      }
 
3690
      /* check if table can be read by key or table only uses const refs */
 
3691
      if ((keyuse=s->keyuse))
 
3692
      {
 
3693
        s->type= JT_REF;
 
3694
        while (keyuse->table == table)
 
3695
        {
 
3696
          start_keyuse=keyuse;
 
3697
          key=keyuse->key;
 
3698
          s->keys.set_bit(key);               // QQ: remove this ?
 
3699
 
 
3700
          refs=0;
 
3701
          const_ref.clear_all();
 
3702
          eq_part.clear_all();
 
3703
          do
 
3704
          {
 
3705
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3706
            {
 
3707
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3708
                const_ref.set_bit(keyuse->keypart);
 
3709
              else
 
3710
                refs|=keyuse->used_tables;
 
3711
              eq_part.set_bit(keyuse->keypart);
 
3712
            }
 
3713
            keyuse++;
 
3714
          } while (keyuse->table == table && keyuse->key == key);
 
3715
 
 
3716
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3717
              !table->pos_in_table_list->embedding)
 
3718
          {
 
3719
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3720
                 == HA_NOSAME)
 
3721
            {
 
3722
              if (const_ref == eq_part)
 
3723
              {                                 // Found everything for ref.
 
3724
                int tmp;
 
3725
                ref_changed = 1;
 
3726
                s->type= JT_CONST;
 
3727
                join->const_table_map|=table->map;
 
3728
                set_position(join,const_count++,s,start_keyuse);
 
3729
                if (create_ref_for_key(join, s, start_keyuse,
 
3730
                                       found_const_table_map))
 
3731
                  return(1);
 
3732
                if ((tmp=join_read_const_table(s,
 
3733
                                               join->positions+const_count-1)))
 
3734
                {
 
3735
                  if (tmp > 0)
 
3736
                    return(1);                  // Fatal error
 
3737
                }
 
3738
                else
 
3739
                  found_const_table_map|= table->map;
 
3740
                break;
 
3741
              }
 
3742
              else
 
3743
                found_ref|= refs;      // Table is const if all refs are const
 
3744
            }
 
3745
            else if (const_ref == eq_part)
 
3746
              s->const_keys.set_bit(key);
 
3747
          }
 
3748
        }
 
3749
      }
 
3750
    }
 
3751
  } while (join->const_table_map & found_ref && ref_changed);
 
3752
 
 
3753
  /*
 
3754
    Update info on indexes that can be used for search lookups as
 
3755
    reading const tables may has added new sargable predicates.
 
3756
  */
 
3757
  if (const_count && sargables)
 
3758
  {
 
3759
    for( ; sargables->field ; sargables++)
 
3760
    {
 
3761
      Field *field= sargables->field;
 
3762
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3763
      key_map possible_keys= field->key_start;
 
3764
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3765
      bool is_const= 1;
 
3766
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3767
        is_const&= sargables->arg_value[j]->const_item();
 
3768
      if (is_const)
 
3769
        join_tab[0].const_keys.merge(possible_keys);
 
3770
    }
 
3771
  }
 
3772
 
 
3773
  if (pull_out_semijoin_tables(join))
 
3774
    return(true);
 
3775
 
 
3776
  /* Calc how many (possible) matched records in each table */
 
3777
 
 
3778
  for (s=stat ; s < stat_end ; s++)
 
3779
  {
 
3780
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3781
    {
 
3782
      /* Only one matching row */
 
3783
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3784
      continue;
 
3785
    }
 
3786
    /* Approximate found rows and time to read them */
 
3787
    s->found_records=s->records=s->table->file->stats.records;
 
3788
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3789
 
 
3790
    /*
 
3791
      Set a max range of how many seeks we can expect when using keys
 
3792
      This is can't be to high as otherwise we are likely to use
 
3793
      table scan.
 
3794
    */
 
3795
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3796
                        (double) s->read_time*3);
 
3797
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3798
      s->worst_seeks=2.0;
 
3799
 
 
3800
    /*
 
3801
      Add to stat->const_keys those indexes for which all group fields or
 
3802
      all select distinct fields participate in one index.
 
3803
    */
 
3804
    add_group_and_distinct_keys(join, s);
 
3805
 
 
3806
    if (!s->const_keys.is_clear_all() &&
 
3807
        !s->table->pos_in_table_list->embedding)
 
3808
    {
 
3809
      ha_rows records;
 
3810
      SQL_SELECT *select;
 
3811
      select= make_select(s->table, found_const_table_map,
 
3812
                          found_const_table_map,
 
3813
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3814
                          1, &error);
 
3815
      if (!select)
 
3816
        return(1);
 
3817
      records= get_quick_record_count(join->session, select, s->table,
 
3818
                                      &s->const_keys, join->row_limit);
 
3819
      s->quick=select->quick;
 
3820
      s->needed_reg=select->needed_reg;
 
3821
      select->quick=0;
 
3822
      if (records == 0 && s->table->reginfo.impossible_range)
 
3823
      {
 
3824
        /*
 
3825
          Impossible WHERE or ON expression
 
3826
          In case of ON, we mark that the we match one empty NULL row.
 
3827
          In case of WHERE, don't set found_const_table_map to get the
 
3828
          caller to abort with a zero row result.
 
3829
        */
 
3830
        join->const_table_map|= s->table->map;
 
3831
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3832
        s->type= JT_CONST;
 
3833
        if (*s->on_expr_ref)
 
3834
        {
 
3835
          /* Generate empty row */
 
3836
          s->info= "Impossible ON condition";
 
3837
          found_const_table_map|= s->table->map;
 
3838
          s->type= JT_CONST;
 
3839
          mark_as_null_row(s->table);           // All fields are NULL
 
3840
        }
 
3841
      }
 
3842
      if (records != HA_POS_ERROR)
 
3843
      {
 
3844
        s->found_records=records;
 
3845
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3846
      }
 
3847
      delete select;
 
3848
    }
 
3849
  }
 
3850
 
 
3851
  join->join_tab=stat;
 
3852
  join->map2table=stat_ref;
 
3853
  join->table= join->all_tables=table_vector;
 
3854
  join->const_tables=const_count;
 
3855
  join->found_const_table_map=found_const_table_map;
 
3856
 
 
3857
  /* Find an optimal join order of the non-constant tables. */
 
3858
  if (join->const_tables != join->tables)
 
3859
  {
 
3860
    optimize_keyuse(join, keyuse_array);
 
3861
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3862
      return(true);
 
3863
  }
 
3864
  else
 
3865
  {
 
3866
    memcpy(join->best_positions, join->positions,
 
3867
           sizeof(POSITION)*join->const_tables);
 
3868
    join->best_read=1.0;
 
3869
  }
 
3870
  /* Generate an execution plan from the found optimal join order. */
 
3871
  return(join->session->killed || get_best_combination(join));
 
3872
}
 
3873
 
 
3874
 
485
3875
/*****************************************************************************
486
3876
  Check with keys are used and with tables references with tables
487
3877
  Updates in stat:
490
3880
          keyuse     Pointer to possible keys
491
3881
*****************************************************************************/
492
3882
 
 
3883
/// Used when finding key fields
 
3884
typedef struct key_field_t {
 
3885
  Field         *field;
 
3886
  Item          *val;                   ///< May be empty if diff constant
 
3887
  uint          level;
 
3888
  uint          optimize; // KEY_OPTIMIZE_*
 
3889
  bool          eq_func;
 
3890
  /**
 
3891
    If true, the condition this struct represents will not be satisfied
 
3892
    when val IS NULL.
 
3893
  */
 
3894
  bool          null_rejecting;
 
3895
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3896
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3897
} KEY_FIELD;
 
3898
 
 
3899
/**
 
3900
  Merge new key definitions to old ones, remove those not used in both.
 
3901
 
 
3902
  This is called for OR between different levels.
 
3903
 
 
3904
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3905
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3906
  that are internally transformed to something like:.
 
3907
 
 
3908
  @code
 
3909
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
 
3910
  @endcode
 
3911
 
 
3912
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3913
  result has null_rejecting=true if it is set for both ORed references.
 
3914
  for example:
 
3915
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3916
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3917
 
 
3918
  @todo
 
3919
    The result of this is that we're missing some 'ref' accesses.
 
3920
    OptimizerTeam: Fix this
 
3921
*/
 
3922
 
 
3923
static KEY_FIELD *
 
3924
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3925
                 uint32_t and_level)
 
3926
{
 
3927
  if (start == new_fields)
 
3928
    return start;                               // Impossible or
 
3929
  if (new_fields == end)
 
3930
    return start;                               // No new fields, skip all
 
3931
 
 
3932
  KEY_FIELD *first_free=new_fields;
 
3933
 
 
3934
  /* Mark all found fields in old array */
 
3935
  for (; new_fields != end ; new_fields++)
 
3936
  {
 
3937
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3938
    {
 
3939
      if (old->field == new_fields->field)
 
3940
      {
 
3941
        /*
 
3942
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3943
          it can return false where it is feasible to make it return true.
 
3944
 
 
3945
          The cause is as follows: Some of the tables are already known to be
 
3946
          const tables (the detection code is in make_join_statistics(),
 
3947
          above the update_ref_and_keys() call), but we didn't propagate
 
3948
          information about this: Table::const_table is not set to true, and
 
3949
          Item::update_used_tables() hasn't been called for each item.
 
3950
          The result of this is that we're missing some 'ref' accesses.
 
3951
          TODO: OptimizerTeam: Fix this
 
3952
        */
 
3953
        if (!new_fields->val->const_item())
 
3954
        {
 
3955
          /*
 
3956
            If the value matches, we can use the key reference.
 
3957
            If not, we keep it until we have examined all new values
 
3958
          */
 
3959
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3960
          {
 
3961
            old->level= and_level;
 
3962
            old->optimize= ((old->optimize & new_fields->optimize &
 
3963
                             KEY_OPTIMIZE_EXISTS) |
 
3964
                            ((old->optimize | new_fields->optimize) &
 
3965
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3966
            old->null_rejecting= (old->null_rejecting &&
 
3967
                                  new_fields->null_rejecting);
 
3968
          }
 
3969
        }
 
3970
        else if (old->eq_func && new_fields->eq_func &&
 
3971
                 old->val->eq_by_collation(new_fields->val,
 
3972
                                           old->field->binary(),
 
3973
                                           old->field->charset()))
 
3974
 
 
3975
        {
 
3976
          old->level= and_level;
 
3977
          old->optimize= ((old->optimize & new_fields->optimize &
 
3978
                           KEY_OPTIMIZE_EXISTS) |
 
3979
                          ((old->optimize | new_fields->optimize) &
 
3980
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3981
          old->null_rejecting= (old->null_rejecting &&
 
3982
                                new_fields->null_rejecting);
 
3983
        }
 
3984
        else if (old->eq_func && new_fields->eq_func &&
 
3985
                 ((old->val->const_item() && old->val->is_null()) ||
 
3986
                  new_fields->val->is_null()))
 
3987
        {
 
3988
          /* field = expression OR field IS NULL */
 
3989
          old->level= and_level;
 
3990
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3991
          /*
 
3992
            Remember the NOT NULL value unless the value does not depend
 
3993
            on other tables.
 
3994
          */
 
3995
          if (!old->val->used_tables() && old->val->is_null())
 
3996
            old->val= new_fields->val;
 
3997
          /* The referred expression can be NULL: */
 
3998
          old->null_rejecting= 0;
 
3999
        }
 
4000
        else
 
4001
        {
 
4002
          /*
 
4003
            We are comparing two different const.  In this case we can't
 
4004
            use a key-lookup on this so it's better to remove the value
 
4005
            and let the range optimzier handle it
 
4006
          */
 
4007
          if (old == --first_free)              // If last item
 
4008
            break;
 
4009
          *old= *first_free;                    // Remove old value
 
4010
          old--;                                // Retry this value
 
4011
        }
 
4012
      }
 
4013
    }
 
4014
  }
 
4015
  /* Remove all not used items */
 
4016
  for (KEY_FIELD *old=start ; old != first_free ;)
 
4017
  {
 
4018
    if (old->level != and_level)
 
4019
    {                                           // Not used in all levels
 
4020
      if (old == --first_free)
 
4021
        break;
 
4022
      *old= *first_free;                        // Remove old value
 
4023
      continue;
 
4024
    }
 
4025
    old++;
 
4026
  }
 
4027
  return first_free;
 
4028
}
 
4029
 
 
4030
 
 
4031
/**
 
4032
  Add a possible key to array of possible keys if it's usable as a key
 
4033
 
 
4034
    @param key_fields      Pointer to add key, if usable
 
4035
    @param and_level       And level, to be stored in KEY_FIELD
 
4036
    @param cond            Condition predicate
 
4037
    @param field           Field used in comparision
 
4038
    @param eq_func         True if we used =, <=> or IS NULL
 
4039
    @param value           Value used for comparison with field
 
4040
    @param usable_tables   Tables which can be used for key optimization
 
4041
    @param sargables       IN/OUT Array of found sargable candidates
 
4042
 
 
4043
  @note
 
4044
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4045
    table, we store this to be able to do not exists optimization later.
 
4046
 
 
4047
  @returns
 
4048
    *key_fields is incremented if we stored a key in the array
 
4049
*/
 
4050
 
 
4051
static void
 
4052
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
 
4053
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4054
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4055
{
 
4056
  uint32_t exists_optimize= 0;
 
4057
  if (!(field->flags & PART_KEY_FLAG))
 
4058
  {
 
4059
    // Don't remove column IS NULL on a LEFT JOIN table
 
4060
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4061
        !field->table->maybe_null || field->null_ptr)
 
4062
      return;                                   // Not a key. Skip it
 
4063
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4064
    assert(num_values == 1);
 
4065
  }
 
4066
  else
 
4067
  {
 
4068
    table_map used_tables=0;
 
4069
    bool optimizable=0;
 
4070
    for (uint32_t i=0; i<num_values; i++)
 
4071
    {
 
4072
      used_tables|=(value[i])->used_tables();
 
4073
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4074
        optimizable=1;
 
4075
    }
 
4076
    if (!optimizable)
 
4077
      return;
 
4078
    if (!(usable_tables & field->table->map))
 
4079
    {
 
4080
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4081
          !field->table->maybe_null || field->null_ptr)
 
4082
        return;                                 // Can't use left join optimize
 
4083
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4084
    }
 
4085
    else
 
4086
    {
 
4087
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4088
      key_map possible_keys=field->key_start;
 
4089
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4090
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4091
 
 
4092
      /*
 
4093
        Save the following cases:
 
4094
        Field op constant
 
4095
        Field LIKE constant where constant doesn't start with a wildcard
 
4096
        Field = field2 where field2 is in a different table
 
4097
        Field op formula
 
4098
        Field IS NULL
 
4099
        Field IS NOT NULL
 
4100
         Field BETWEEN ...
 
4101
         Field IN ...
 
4102
      */
 
4103
      stat[0].key_dependent|=used_tables;
 
4104
 
 
4105
      bool is_const=1;
 
4106
      for (uint32_t i=0; i<num_values; i++)
 
4107
      {
 
4108
        if (!(is_const&= value[i]->const_item()))
 
4109
          break;
 
4110
      }
 
4111
      if (is_const)
 
4112
        stat[0].const_keys.merge(possible_keys);
 
4113
      else if (!eq_func)
 
4114
      {
 
4115
        /*
 
4116
          Save info to be able check whether this predicate can be
 
4117
          considered as sargable for range analisis after reading const tables.
 
4118
          We do not save info about equalities as update_const_equal_items
 
4119
          will take care of updating info on keys from sargable equalities.
 
4120
        */
 
4121
        (*sargables)--;
 
4122
        (*sargables)->field= field;
 
4123
        (*sargables)->arg_value= value;
 
4124
        (*sargables)->num_values= num_values;
 
4125
      }
 
4126
      /*
 
4127
        We can't always use indexes when comparing a string index to a
 
4128
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4129
        eq_func is NEVER true when num_values > 1
 
4130
       */
 
4131
      if (!eq_func)
 
4132
      {
 
4133
        /*
 
4134
          Additional optimization: if we're processing
 
4135
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4136
          "t.key = c1".
 
4137
          TODO: This is a very limited fix. A more generic fix is possible.
 
4138
          There are 2 options:
 
4139
          A) Make equality propagation code be able to handle BETWEEN
 
4140
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4141
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4142
             and use them in equality propagation process (see details in
 
4143
             OptimizerKBAndTodo)
 
4144
        */
 
4145
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4146
            ((Item_func_between*) cond)->negated ||
 
4147
            !value[0]->eq(value[1], field->binary()))
 
4148
          return;
 
4149
        eq_func= true;
 
4150
      }
 
4151
 
 
4152
      if (field->result_type() == STRING_RESULT)
 
4153
      {
 
4154
        if ((*value)->result_type() != STRING_RESULT)
 
4155
        {
 
4156
          if (field->cmp_type() != (*value)->result_type())
 
4157
            return;
 
4158
        }
 
4159
        else
 
4160
        {
 
4161
          /*
 
4162
            We can't use indexes if the effective collation
 
4163
            of the operation differ from the field collation.
 
4164
          */
 
4165
          if (field->cmp_type() == STRING_RESULT &&
 
4166
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4167
            return;
 
4168
        }
 
4169
      }
 
4170
    }
 
4171
  }
 
4172
  /*
 
4173
    For the moment eq_func is always true. This slot is reserved for future
 
4174
    extensions where we want to remembers other things than just eq comparisons
 
4175
  */
 
4176
  assert(eq_func);
 
4177
  /* Store possible eq field */
 
4178
  (*key_fields)->field=         field;
 
4179
  (*key_fields)->eq_func=       eq_func;
 
4180
  (*key_fields)->val=           *value;
 
4181
  (*key_fields)->level=         and_level;
 
4182
  (*key_fields)->optimize=      exists_optimize;
 
4183
  /*
 
4184
    If the condition has form "tbl.keypart = othertbl.field" and
 
4185
    othertbl.field can be NULL, there will be no matches if othertbl.field
 
4186
    has NULL value.
 
4187
    We use null_rejecting in add_not_null_conds() to add
 
4188
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4189
  */
 
4190
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4191
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4192
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4193
                                  ((Item_field*)*value)->field->maybe_null());
 
4194
  (*key_fields)->cond_guard= NULL;
 
4195
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
 
4196
                              cond->name < subq_sj_cond_name + 64)?
 
4197
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4198
  (*key_fields)++;
 
4199
}
 
4200
 
 
4201
/**
 
4202
  Add possible keys to array of possible keys originated from a simple
 
4203
  predicate.
 
4204
 
 
4205
    @param  key_fields     Pointer to add key, if usable
 
4206
    @param  and_level      And level, to be stored in KEY_FIELD
 
4207
    @param  cond           Condition predicate
 
4208
    @param  field          Field used in comparision
 
4209
    @param  eq_func        True if we used =, <=> or IS NULL
 
4210
    @param  value          Value used for comparison with field
 
4211
                           Is NULL for BETWEEN and IN
 
4212
    @param  usable_tables  Tables which can be used for key optimization
 
4213
    @param  sargables      IN/OUT Array of found sargable candidates
 
4214
 
 
4215
  @note
 
4216
    If field items f1 and f2 belong to the same multiple equality and
 
4217
    a key is added for f1, the the same key is added for f2.
 
4218
 
 
4219
  @returns
 
4220
    *key_fields is incremented if we stored a key in the array
 
4221
*/
 
4222
 
 
4223
static void
 
4224
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4225
                     Item_func *cond, Item_field *field_item,
 
4226
                     bool eq_func, Item **val,
 
4227
                     uint32_t num_values, table_map usable_tables,
 
4228
                     SARGABLE_PARAM **sargables)
 
4229
{
 
4230
  Field *field= field_item->field;
 
4231
  add_key_field(key_fields, and_level, cond, field,
 
4232
                eq_func, val, num_values, usable_tables, sargables);
 
4233
  Item_equal *item_equal= field_item->item_equal;
 
4234
  if (item_equal)
 
4235
  {
 
4236
    /*
 
4237
      Add to the set of possible key values every substitution of
 
4238
      the field for an equal field included into item_equal
 
4239
    */
 
4240
    Item_equal_iterator it(*item_equal);
 
4241
    Item_field *item;
 
4242
    while ((item= it++))
 
4243
    {
 
4244
      if (!field->eq(item->field))
 
4245
      {
 
4246
        add_key_field(key_fields, and_level, cond, item->field,
 
4247
                      eq_func, val, num_values, usable_tables,
 
4248
                      sargables);
 
4249
      }
 
4250
    }
 
4251
  }
 
4252
}
 
4253
 
 
4254
static void
 
4255
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4256
               COND *cond, table_map usable_tables,
 
4257
               SARGABLE_PARAM **sargables)
 
4258
{
 
4259
  if (cond->type() == Item_func::COND_ITEM)
 
4260
  {
 
4261
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4262
    KEY_FIELD *org_key_fields= *key_fields;
 
4263
 
 
4264
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4265
    {
 
4266
      Item *item;
 
4267
      while ((item=li++))
 
4268
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4269
                       sargables);
 
4270
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4271
        org_key_fields->level= *and_level;
 
4272
    }
 
4273
    else
 
4274
    {
 
4275
      (*and_level)++;
 
4276
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4277
                     sargables);
 
4278
      Item *item;
 
4279
      while ((item=li++))
 
4280
      {
 
4281
        KEY_FIELD *start_key_fields= *key_fields;
 
4282
        (*and_level)++;
 
4283
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4284
                       sargables);
 
4285
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4286
                                     *key_fields,++(*and_level));
 
4287
      }
 
4288
    }
 
4289
    return;
 
4290
  }
 
4291
 
 
4292
  /*
 
4293
    Subquery optimization: Conditions that are pushed down into subqueries
 
4294
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4295
    but need to set cond_guard for KEYUSE elements generated from it.
 
4296
  */
 
4297
  {
 
4298
    if (cond->type() == Item::FUNC_ITEM &&
 
4299
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4300
    {
 
4301
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4302
      if (!join->group_list && !join->order &&
 
4303
          join->unit->item &&
 
4304
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4305
          !join->unit->is_union())
 
4306
      {
 
4307
        KEY_FIELD *save= *key_fields;
 
4308
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4309
                       sargables);
 
4310
        // Indicate that this ref access candidate is for subquery lookup:
 
4311
        for (; save != *key_fields; save++)
 
4312
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4313
      }
 
4314
      return;
 
4315
    }
 
4316
  }
 
4317
 
 
4318
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4319
  if (cond->type() != Item::FUNC_ITEM)
 
4320
    return;
 
4321
  Item_func *cond_func= (Item_func*) cond;
 
4322
  switch (cond_func->select_optimize()) {
 
4323
  case Item_func::OPTIMIZE_NONE:
 
4324
    break;
 
4325
  case Item_func::OPTIMIZE_KEY:
 
4326
  {
 
4327
    Item **values;
 
4328
    // BETWEEN, IN, NE
 
4329
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4330
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4331
    {
 
4332
      values= cond_func->arguments()+1;
 
4333
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4334
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4335
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4336
        values--;
 
4337
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4338
                  cond_func->argument_count() != 2);
 
4339
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4340
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4341
                           0, values,
 
4342
                           cond_func->argument_count()-1,
 
4343
                           usable_tables, sargables);
 
4344
    }
 
4345
    if (cond_func->functype() == Item_func::BETWEEN)
 
4346
    {
 
4347
      values= cond_func->arguments();
 
4348
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4349
      {
 
4350
        Item_field *field_item;
 
4351
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4352
            &&
 
4353
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4354
        {
 
4355
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4356
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4357
                               field_item, 0, values, 1, usable_tables,
 
4358
                               sargables);
 
4359
        }
 
4360
      }
 
4361
    }
 
4362
    break;
 
4363
  }
 
4364
  case Item_func::OPTIMIZE_OP:
 
4365
  {
 
4366
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4367
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4368
 
 
4369
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4370
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4371
    {
 
4372
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4373
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4374
                           equal_func,
 
4375
                           cond_func->arguments()+1, 1, usable_tables,
 
4376
                           sargables);
 
4377
    }
 
4378
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4379
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4380
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4381
    {
 
4382
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4383
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4384
                           equal_func,
 
4385
                           cond_func->arguments(),1,usable_tables,
 
4386
                           sargables);
 
4387
    }
 
4388
    break;
 
4389
  }
 
4390
  case Item_func::OPTIMIZE_NULL:
 
4391
    /* column_name IS [NOT] NULL */
 
4392
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4393
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4394
    {
 
4395
      Item *tmp=new Item_null;
 
4396
      if (unlikely(!tmp))                       // Should never be true
 
4397
        return;
 
4398
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4399
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4400
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4401
                           &tmp, 1, usable_tables, sargables);
 
4402
    }
 
4403
    break;
 
4404
  case Item_func::OPTIMIZE_EQUAL:
 
4405
    Item_equal *item_equal= (Item_equal *) cond;
 
4406
    Item *const_item= item_equal->get_const();
 
4407
    Item_equal_iterator it(*item_equal);
 
4408
    Item_field *item;
 
4409
    if (const_item)
 
4410
    {
 
4411
      /*
 
4412
        For each field field1 from item_equal consider the equality
 
4413
        field1=const_item as a condition allowing an index access of the table
 
4414
        with field1 by the keys value of field1.
 
4415
      */
 
4416
      while ((item= it++))
 
4417
      {
 
4418
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4419
                      true, &const_item, 1, usable_tables, sargables);
 
4420
      }
 
4421
    }
 
4422
    else
 
4423
    {
 
4424
      /*
 
4425
        Consider all pairs of different fields included into item_equal.
 
4426
        For each of them (field1, field1) consider the equality
 
4427
        field1=field2 as a condition allowing an index access of the table
 
4428
        with field1 by the keys value of field2.
 
4429
      */
 
4430
      Item_equal_iterator fi(*item_equal);
 
4431
      while ((item= fi++))
 
4432
      {
 
4433
        Field *field= item->field;
 
4434
        while ((item= it++))
 
4435
        {
 
4436
          if (!field->eq(item->field))
 
4437
          {
 
4438
            add_key_field(key_fields, *and_level, cond_func, field,
 
4439
                          true, (Item **) &item, 1, usable_tables,
 
4440
                          sargables);
 
4441
          }
 
4442
        }
 
4443
        it.rewind();
 
4444
      }
 
4445
    }
 
4446
    break;
 
4447
  }
 
4448
}
493
4449
 
494
4450
/**
495
4451
  Add all keys with uses 'field' for some keypart.
496
4452
 
497
4453
  If field->and_level != and_level then only mark key_part as const_part.
498
4454
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4455
 
 
4456
static uint
 
4457
max_part_bit(key_part_map bits)
500
4458
{
501
4459
  uint32_t found;
502
4460
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4461
  return found;
504
4462
}
505
4463
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4464
static void
 
4465
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4466
{
 
4467
  Field *field=key_field->field;
 
4468
  Table *form= field->table;
 
4469
  KEYUSE keyuse;
 
4470
 
 
4471
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4472
  {
 
4473
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4474
    {
 
4475
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4476
        continue;
 
4477
 
 
4478
      uint32_t key_parts= (uint32_t) form->key_info[key].key_parts;
 
4479
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4480
      {
 
4481
        if (field->eq(form->key_info[key].key_part[part].field))
 
4482
        {
 
4483
          keyuse.table= field->table;
 
4484
          keyuse.val =  key_field->val;
 
4485
          keyuse.key =  key;
 
4486
          keyuse.keypart=part;
 
4487
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4488
          keyuse.used_tables=key_field->val->used_tables();
 
4489
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4490
          keyuse.null_rejecting= key_field->null_rejecting;
 
4491
          keyuse.cond_guard= key_field->cond_guard;
 
4492
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4493
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4494
        }
 
4495
      }
 
4496
    }
 
4497
  }
 
4498
}
 
4499
 
 
4500
static int
 
4501
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4502
{
508
4503
  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()));
 
4504
  if (a->table->tablenr != b->table->tablenr)
 
4505
    return (int) (a->table->tablenr - b->table->tablenr);
 
4506
  if (a->key != b->key)
 
4507
    return (int) (a->key - b->key);
 
4508
  if (a->keypart != b->keypart)
 
4509
    return (int) (a->keypart - b->keypart);
515
4510
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4511
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4512
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4513
    return res;
519
4514
  /* 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)));
 
4515
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4516
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4517
}
 
4518
 
 
4519
 
 
4520
/*
 
4521
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4522
 
 
4523
    This function populates KEY_FIELD array with entries generated from the
 
4524
    ON condition of the given nested join, and does the same for nested joins
 
4525
    contained within this nested join.
 
4526
 
 
4527
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4528
  @param[in,out]  end                 End of the key field array
 
4529
  @param[in,out]  and_level           And-level
 
4530
  @param[in,out]  sargables           Array of found sargable candidates
 
4531
 
 
4532
 
 
4533
  @note
 
4534
    We can add accesses to the tables that are direct children of this nested
 
4535
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4536
 
 
4537
    Example for #1 (outer brackets pair denotes nested join this function is
 
4538
    invoked for):
 
4539
    @code
 
4540
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4541
    @endcode
 
4542
    Example for #2:
 
4543
    @code
 
4544
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4545
    @endcode
 
4546
    In examples 1-2 for condition cond, we can add 'ref' access candidates to
 
4547
    t1 only.
 
4548
    Example #3:
 
4549
    @code
 
4550
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4551
    @endcode
 
4552
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4553
*/
 
4554
 
 
4555
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4556
                                  KEY_FIELD **end, uint32_t *and_level,
 
4557
                                  SARGABLE_PARAM **sargables)
 
4558
{
 
4559
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4560
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4561
  bool have_another = false;
 
4562
  table_map tables= 0;
 
4563
  TableList *table;
 
4564
  assert(nested_join_table->nested_join);
 
4565
 
 
4566
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4567
                                            (table= li++))))
 
4568
  {
 
4569
    if (table->nested_join)
 
4570
    {
 
4571
      if (!table->on_expr)
 
4572
      {
 
4573
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4574
        have_another= true;
 
4575
        li2= li;
 
4576
        li= List_iterator<TableList>(table->nested_join->join_list);
 
4577
      }
 
4578
      else
 
4579
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4580
    }
 
4581
    else
 
4582
      if (!table->on_expr)
 
4583
        tables |= table->table->map;
 
4584
  }
 
4585
  if (nested_join_table->on_expr)
 
4586
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4587
                   sargables);
522
4588
}
523
4589
 
524
4590
 
526
4592
  Update keyuse array with all possible keys we can use to fetch rows.
527
4593
 
528
4594
  @param       session
529
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4595
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4596
  @param       join_tab       Array in tablenr_order
531
4597
  @param       tables         Number of tables in join
532
4598
  @param       cond           WHERE condition (note that the function analyzes
535
4601
                              for which we can make ref access based the WHERE
536
4602
                              clause)
537
4603
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
 
4604
  @param[out]  sargables      Array of found sargable candidates
539
4605
 
540
4606
   @retval
541
4607
     0  OK
542
4608
   @retval
543
4609
     1  Out of memory.
544
4610
*/
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)
 
4611
 
 
4612
static bool
 
4613
update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4614
                    uint32_t tables, COND *cond, COND_EQUAL *,
 
4615
                    table_map normal_tables, Select_Lex *select_lex,
 
4616
                    SARGABLE_PARAM **sargables)
554
4617
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
 
4618
  uint  and_level,i,found_eq_constant;
 
4619
  KEY_FIELD *key_fields, *end, *field;
557
4620
  uint32_t sz;
558
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
 
4621
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
559
4622
 
560
4623
  /*
561
 
    All predicates that are used to fill arrays of KeyField
562
 
    and SargableParam classes have at most 2 arguments
 
4624
    We use the same piece of memory to store both  KEY_FIELD
 
4625
    and SARGABLE_PARAM structure.
 
4626
    KEY_FIELD values are placed at the beginning this memory
 
4627
    while  SARGABLE_PARAM values are put at the end.
 
4628
    All predicates that are used to fill arrays of KEY_FIELD
 
4629
    and SARGABLE_PARAM structures have at most 2 arguments
563
4630
    except BETWEEN predicates that have 3 arguments and
564
4631
    IN predicates.
565
4632
    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
 
4633
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4634
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4635
    can be filled as this predicate is considered as
569
4636
    saragable with respect to each of its argument.
570
4637
    An IN predicate can require at most 1 element as currently
574
4641
    can be not more than select_lex->max_equal_elems such
575
4642
    substitutions.
576
4643
  */
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;
 
4644
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4645
      (((session->lex->current_select->cond_count+1)*2 +
 
4646
        session->lex->current_select->between_count)*m+1);
 
4647
  if (!(key_fields=(KEY_FIELD*) session->alloc(sz)))
 
4648
    return true; /* purecov: inspected */
582
4649
  and_level= 0;
583
4650
  field= end= key_fields;
 
4651
  *sargables= (SARGABLE_PARAM *) key_fields +
 
4652
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4653
  /* set a barrier for the array of SARGABLE_PARAM */
 
4654
  (*sargables)[0].field= 0;
584
4655
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4656
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4657
    return true;
587
4658
  if (cond)
588
4659
  {
589
4660
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4661
                   sargables);
591
 
    for (; field != end; field++)
 
4662
    for (; field != end ; field++)
592
4663
    {
593
 
      add_key_part(keyuse, field);
 
4664
      add_key_part(keyuse,field);
594
4665
      /* 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
 
      }
 
4666
      if (field->val->type() == Item::NULL_ITEM &&
 
4667
          !field->field->real_maybe_null())
 
4668
        field->field->table->reginfo.not_exists_optimize=1;
600
4669
    }
601
4670
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4671
  for (i=0 ; i < tables ; i++)
603
4672
  {
604
4673
    /*
605
4674
      Block the creation of keys for inner tables of outer joins.
618
4687
 
619
4688
  /* Process ON conditions for the nested joins */
620
4689
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
4690
    List_iterator<TableList> li(*join_tab->join->join_list);
622
4691
    TableList *table;
623
4692
    while ((table= li++))
624
4693
    {
625
 
      if (table->getNestedJoin())
 
4694
      if (table->nested_join)
626
4695
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
627
4696
                              sargables);
628
4697
    }
643
4712
  */
644
4713
  if (keyuse->elements)
645
4714
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4715
    KEYUSE key_end,*prev,*save_pos,*use;
647
4716
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
 
4717
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4718
          (qsort_cmp) sort_keyuse);
650
4719
 
651
4720
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
4721
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
4722
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4723
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4724
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4725
    found_eq_constant=0;
 
4726
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4727
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4728
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4729
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4730
      {
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;
 
4731
        if (use->key == prev->key && use->table == prev->table)
 
4732
        {
 
4733
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4734
            continue;                           /* remove */
 
4735
        }
 
4736
        else if (use->keypart != 0)             // First found must be 0
 
4737
          continue;
 
4738
      }
672
4739
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4740
#ifdef HAVE_purify
 
4741
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4742
      if (save_pos != use)
676
4743
#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;
 
4744
        *save_pos= *use;
 
4745
      prev=use;
 
4746
      found_eq_constant= !use->used_tables;
 
4747
      /* Save ptr to first use */
 
4748
      if (!use->table->reginfo.join_tab->keyuse)
 
4749
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4750
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4751
      save_pos++;
689
4752
    }
 
4753
    i=(uint32_t) (save_pos-(KEYUSE*) keyuse->buffer);
 
4754
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4755
    keyuse->elements=i;
690
4756
  }
691
4757
  return false;
692
4758
}
694
4760
/**
695
4761
  Update some values in keyuse for faster choose_plan() loop.
696
4762
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4763
 
 
4764
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4765
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4766
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4767
 
703
4768
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4769
  {
711
4776
      Constant tables are ignored.
712
4777
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4778
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4779
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4780
    if (keyuse->used_tables &
 
4781
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4782
               ~OUTER_REF_TABLE_BIT)))
716
4783
    {
717
4784
      uint32_t tablenr;
718
4785
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4786
      if (map == 1)                     // Only one table
720
4787
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4788
        Table *tmp_table=join->all_tables[tablenr];
 
4789
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
723
4790
      }
724
4791
    }
725
4792
    /*
726
4793
      Outer reference (external field) is constant for single executing
727
4794
      of subquery
728
4795
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4796
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4797
      keyuse->ref_table_rows= 1;
731
4798
  }
732
4799
}
733
4800
 
749
4816
  @return
750
4817
    None
751
4818
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4819
 
 
4820
static void
 
4821
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4822
{
754
4823
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4824
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4825
  order_st      *cur_group;
757
4826
  Item_field *cur_item;
758
4827
  key_map possible_keys(0);
759
4828
 
766
4835
  else if (join->select_distinct)
767
4836
  { /* Collect all query fields referenced in the SELECT clause. */
768
4837
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4838
    List_iterator<Item> select_items_it(select_items);
770
4839
    Item *item;
771
4840
    while ((item= select_items_it++))
772
4841
      item->walk(&Item::collect_item_field_processor, 0,
780
4849
 
781
4850
  /* Intersect the keys of all group fields. */
782
4851
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4852
  possible_keys.merge(cur_item->field->part_of_key);
784
4853
  while ((cur_item= indexed_fields_it++))
785
4854
  {
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
 
4855
    possible_keys.intersect(cur_item->field->part_of_key);
 
4856
  }
 
4857
 
 
4858
  if (!possible_keys.is_clear_all())
 
4859
    join_tab->const_keys.merge(possible_keys);
 
4860
}
 
4861
 
 
4862
 
 
4863
/*****************************************************************************
 
4864
  Go through all combinations of not marked tables and find the one
 
4865
  which uses least records
 
4866
*****************************************************************************/
 
4867
 
 
4868
/** Save const tables first as used tables. */
 
4869
 
 
4870
static void
 
4871
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4872
{
 
4873
  join->positions[idx].table= table;
 
4874
  join->positions[idx].key=key;
 
4875
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4876
  join->positions[idx].ref_depend_map= 0;
 
4877
 
 
4878
  /* Move the const table as down as possible in best_ref */
 
4879
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4880
  JOIN_TAB *next=join->best_ref[idx];
 
4881
  for (;next != table ; pos++)
 
4882
  {
 
4883
    JOIN_TAB *tmp=pos[0];
 
4884
    pos[0]=next;
 
4885
    next=tmp;
 
4886
  }
 
4887
  join->best_ref[idx]=table;
 
4888
}
 
4889
 
 
4890
 
 
4891
/*
 
4892
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4893
 
 
4894
  SYNOPSIS
 
4895
    get_bound_sj_equalities()
 
4896
      sj_nest           Semi-join nest
 
4897
      remaining_tables  Tables that are not yet bound
 
4898
 
 
4899
  DESCRIPTION
 
4900
    Given a semi-join nest, find out which of the IN-equalities have their
 
4901
    left part expression bound (i.e. the said expression doesn't refer to
 
4902
    any of remaining_tables and can be evaluated).
 
4903
 
 
4904
  RETURN
 
4905
    Bitmap of bound IN-equalities.
 
4906
*/
 
4907
 
 
4908
uint64_t get_bound_sj_equalities(TableList *sj_nest,
 
4909
                                  table_map remaining_tables)
 
4910
{
 
4911
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4912
  Item *item;
 
4913
  uint32_t i= 0;
 
4914
  uint64_t res= 0;
 
4915
  while ((item= li++))
 
4916
  {
 
4917
    /*
 
4918
      Q: should this take into account equality propagation and how?
 
4919
      A: If e->outer_side is an Item_field, walk over the equality
 
4920
         class and see if there is an element that is bound?
 
4921
      (this is an optional feature)
 
4922
    */
 
4923
    if (!(item->used_tables() & remaining_tables))
 
4924
    {
 
4925
      res |= 1UL < i;
 
4926
    }
 
4927
  }
 
4928
  return res;
 
4929
}
 
4930
 
 
4931
 
 
4932
/**
 
4933
  Find the best access path for an extension of a partial execution
 
4934
  plan and add this path to the plan.
 
4935
 
 
4936
  The function finds the best access path to table 's' from the passed
 
4937
  partial plan where an access path is the general term for any means to
 
4938
  access the data in 's'. An access path may use either an index or a scan,
 
4939
  whichever is cheaper. The input partial plan is passed via the array
 
4940
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4941
  cost are stored in 'join->positions[idx]'.
 
4942
 
 
4943
  @param join             pointer to the structure providing all context info
 
4944
                          for the query
 
4945
  @param s                the table to be joined by the function
 
4946
  @param session              thread for the connection that submitted the query
 
4947
  @param remaining_tables set of tables not included into the partial plan yet
 
4948
  @param idx              the length of the partial plan
 
4949
  @param record_count     estimate for the number of records returned by the
 
4950
                          partial plan
 
4951
  @param read_time        the cost of the partial plan
 
4952
 
 
4953
  @return
 
4954
    None
 
4955
*/
 
4956
 
 
4957
static void
 
4958
best_access_path(JOIN      *join,
 
4959
                 JOIN_TAB  *s,
 
4960
                 Session       *session,
 
4961
                 table_map remaining_tables,
 
4962
                 uint32_t      idx,
 
4963
                 double    record_count,
 
4964
                 double)
 
4965
{
 
4966
  KEYUSE *best_key=         0;
 
4967
  uint32_t best_max_key_part=   0;
 
4968
  bool found_constraint= 0;
 
4969
  double best=              DBL_MAX;
 
4970
  double best_time=         DBL_MAX;
 
4971
  double records=           DBL_MAX;
 
4972
  table_map best_ref_depends_map= 0;
 
4973
  double tmp;
 
4974
  ha_rows rec;
 
4975
  uint32_t best_is_sj_inside_out=    0;
 
4976
 
 
4977
  if (s->keyuse)
 
4978
  {                                            /* Use key if possible */
 
4979
    Table *table= s->table;
 
4980
    KEYUSE *keyuse,*start_key=0;
 
4981
    double best_records= DBL_MAX;
 
4982
    uint32_t max_key_part=0;
 
4983
    uint64_t bound_sj_equalities= 0;
 
4984
    bool try_sj_inside_out= false;
 
4985
    /*
 
4986
      Discover the bound equalites. We need to do this, if
 
4987
        1. The next table is an SJ-inner table, and
 
4988
        2. It is the first table from that semijoin, and
 
4989
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4990
           all or none of their tables in join_table_map), except
 
4991
           s->emb_sj_nest (which we've just entered).
 
4992
        3. All correlation references from this sj-nest are bound
 
4993
    */
 
4994
    if (s->emb_sj_nest &&                                                 // (1)
 
4995
        s->emb_sj_nest->sj_in_exprs < 64 &&
 
4996
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4997
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4998
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4999
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
5000
    {
 
5001
      /* This table is an InsideOut scan candidate */
 
5002
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
 
5003
                                                   remaining_tables);
 
5004
      try_sj_inside_out= true;
 
5005
    }
 
5006
 
 
5007
    /* Test how we can use keys */
 
5008
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
5009
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
5010
    {
 
5011
      key_part_map found_part= 0;
 
5012
      table_map found_ref= 0;
 
5013
      uint32_t key= keyuse->key;
 
5014
      KEY *keyinfo= table->key_info+key;
 
5015
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
5016
      key_part_map const_part= 0;
 
5017
      /* The or-null keypart in ref-or-null access: */
 
5018
      key_part_map ref_or_null_part= 0;
 
5019
 
 
5020
      /* Calculate how many key segments of the current key we can use */
 
5021
      start_key= keyuse;
 
5022
      uint64_t handled_sj_equalities=0;
 
5023
      key_part_map sj_insideout_map= 0;
 
5024
 
 
5025
      do /* For each keypart */
 
5026
      {
 
5027
        uint32_t keypart= keyuse->keypart;
 
5028
        table_map best_part_found_ref= 0;
 
5029
        double best_prev_record_reads= DBL_MAX;
 
5030
 
 
5031
        do /* For each way to access the keypart */
 
5032
        {
 
5033
 
 
5034
          /*
 
5035
            if 1. expression doesn't refer to forward tables
 
5036
               2. we won't get two ref-or-null's
 
5037
          */
 
5038
          if (!(remaining_tables & keyuse->used_tables) &&
 
5039
              !(ref_or_null_part && (keyuse->optimize &
 
5040
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5041
          {
 
5042
            found_part|= keyuse->keypart_map;
 
5043
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5044
              const_part|= keyuse->keypart_map;
 
5045
 
 
5046
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5047
                                                      keyuse->used_tables));
 
5048
            if (tmp2 < best_prev_record_reads)
 
5049
            {
 
5050
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5051
              best_prev_record_reads= tmp2;
 
5052
            }
 
5053
            if (rec > keyuse->ref_table_rows)
 
5054
              rec= keyuse->ref_table_rows;
 
5055
            /*
 
5056
              If there is one 'key_column IS NULL' expression, we can
 
5057
              use this ref_or_null optimisation of this field
 
5058
            */
 
5059
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5060
              ref_or_null_part |= keyuse->keypart_map;
 
5061
          }
 
5062
 
 
5063
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5064
          {
 
5065
            if (!(remaining_tables & keyuse->used_tables))
 
5066
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5067
            else
 
5068
            {
 
5069
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5070
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5071
            }
 
5072
          }
 
5073
 
 
5074
          keyuse++;
 
5075
        } while (keyuse->table == table && keyuse->key == key &&
 
5076
                 keyuse->keypart == keypart);
 
5077
        found_ref|= best_part_found_ref;
 
5078
      } while (keyuse->table == table && keyuse->key == key);
 
5079
 
 
5080
      /*
 
5081
        Assume that that each key matches a proportional part of table.
 
5082
      */
 
5083
      if (!found_part && !handled_sj_equalities)
 
5084
        continue;                               // Nothing usable found
 
5085
 
 
5086
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5087
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5088
 
 
5089
      bool sj_inside_out_scan= false;
 
5090
      {
 
5091
        found_constraint= 1;
 
5092
        /*
 
5093
          Check if InsideOut scan is applicable:
 
5094
          1. All IN-equalities are either "bound" or "handled"
 
5095
          2. Index keyparts are
 
5096
             ...
 
5097
        */
 
5098
        if (try_sj_inside_out &&
 
5099
            table->covering_keys.is_set(key) &&
 
5100
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5101
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5102
        {
 
5103
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5104
          if (n_fixed_parts != keyinfo->key_parts &&
 
5105
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5106
               PREV_BITS(uint, keyinfo->key_parts))
 
5107
          {
 
5108
            /*
 
5109
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5110
              check if all of them are covered.
 
5111
            */
 
5112
            sj_inside_out_scan= true;
 
5113
            if (!n_fixed_parts)
 
5114
            {
 
5115
              /*
 
5116
                It's a confluent ref scan.
 
5117
 
 
5118
                That is, all found KEYUSE elements refer to IN-equalities,
 
5119
                and there is really no ref access because there is no
 
5120
                  t.keypart0 = {bound expression}
 
5121
 
 
5122
                Calculate the cost of complete loose index scan.
 
5123
              */
 
5124
              records= (double)s->table->file->stats.records;
 
5125
 
 
5126
              /* The cost is entire index scan cost (divided by 2) */
 
5127
              best_time= s->table->file->index_only_read_time(key, records);
 
5128
 
 
5129
              /* Now figure how many different keys we will get */
 
5130
              ulong rpc;
 
5131
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5132
                records= records / rpc;
 
5133
              start_key= NULL;
 
5134
            }
 
5135
          }
 
5136
        }
 
5137
 
 
5138
        /*
 
5139
          Check if we found full key
 
5140
        */
 
5141
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5142
            !ref_or_null_part)
 
5143
        {                                         /* use eq key */
 
5144
          max_key_part= UINT32_MAX;
 
5145
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5146
          {
 
5147
            tmp = prev_record_reads(join, idx, found_ref);
 
5148
            records=1.0;
 
5149
          }
 
5150
          else
 
5151
          {
 
5152
            if (!found_ref)
 
5153
            {                                     /* We found a const key */
 
5154
              /*
 
5155
                ReuseRangeEstimateForRef-1:
 
5156
                We get here if we've found a ref(const) (c_i are constants):
 
5157
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5158
 
 
5159
                If range optimizer was able to construct a "range"
 
5160
                access on this index, then its condition "quick_cond" was
 
5161
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5162
                from the range optimizer.
 
5163
 
 
5164
                Proof of (*): By properties of range and ref optimizers
 
5165
                quick_cond will be equal or tighther than ref_const_cond.
 
5166
                ref_const_cond already covers "smallest" possible interval -
 
5167
                a singlepoint interval over all keyparts. Therefore,
 
5168
                quick_cond is equivalent to ref_const_cond (if it was an
 
5169
                empty interval we wouldn't have got here).
 
5170
              */
 
5171
              if (table->quick_keys.is_set(key))
 
5172
                records= (double) table->quick_rows[key];
 
5173
              else
 
5174
              {
 
5175
                /* quick_range couldn't use key! */
 
5176
                records= (double) s->records/rec;
 
5177
              }
 
5178
            }
 
5179
            else
 
5180
            {
 
5181
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5182
              {                                   /* Prefer longer keys */
 
5183
                records=
 
5184
                  ((double) s->records / (double) rec *
 
5185
                   (1.0 +
 
5186
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5187
                     (double) table->s->max_key_length)));
 
5188
                if (records < 2.0)
 
5189
                  records=2.0;               /* Can't be as good as a unique */
 
5190
              }
 
5191
              /*
 
5192
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5193
                E(#rows) from range optimizer. Make another try:
 
5194
 
 
5195
                If range optimizer produced E(#rows) for a prefix of the ref
 
5196
                access we're considering, and that E(#rows) is lower then our
 
5197
                current estimate, make an adjustment. The criteria of when we
 
5198
                can make an adjustment is a special case of the criteria used
 
5199
                in ReuseRangeEstimateForRef-3.
 
5200
              */
 
5201
              if (table->quick_keys.is_set(key) &&
 
5202
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5203
                  table->quick_n_ranges[key] == 1 &&
 
5204
                  records > (double) table->quick_rows[key])
 
5205
              {
 
5206
                records= (double) table->quick_rows[key];
 
5207
              }
 
5208
            }
 
5209
            /* Limit the number of matched rows */
 
5210
            tmp= records;
 
5211
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5212
            if (table->covering_keys.is_set(key))
 
5213
            {
 
5214
              /* we can use only index tree */
 
5215
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5216
            }
 
5217
            else
 
5218
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5219
          }
 
5220
        }
 
5221
        else
 
5222
        {
 
5223
          /*
 
5224
            Use as much key-parts as possible and a uniq key is better
 
5225
            than a not unique key
 
5226
            Set tmp to (previous record count) * (records / combination)
 
5227
          */
 
5228
          if ((found_part & 1) &&
 
5229
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5230
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5231
          {
 
5232
            max_key_part= max_part_bit(found_part);
 
5233
            /*
 
5234
              ReuseRangeEstimateForRef-3:
 
5235
              We're now considering a ref[or_null] access via
 
5236
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
 
5237
              (same-as-above but with one cond replaced
 
5238
               with "t.keypart_i IS NULL")]  (**)
 
5239
 
 
5240
              Try re-using E(#rows) from "range" optimizer:
 
5241
              We can do so if "range" optimizer used the same intervals as
 
5242
              in (**). The intervals used by range optimizer may be not
 
5243
              available at this point (as "range" access might have choosen to
 
5244
              create quick select over another index), so we can't compare
 
5245
              them to (**). We'll make indirect judgements instead.
 
5246
              The sufficient conditions for re-use are:
 
5247
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5248
                   this is not satisfied we have no way to know which ranges
 
5249
                   will be actually scanned by 'ref' until we execute the
 
5250
                   join)
 
5251
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5252
                   is apparently a necessary requirement)
 
5253
 
 
5254
              We also have a property that "range optimizer produces equal or
 
5255
              tighter set of scan intervals than ref(const) optimizer". Each
 
5256
              of the intervals in (**) are "tightest possible" intervals when
 
5257
              one limits itself to using keyparts 1..K (which we do in #2).
 
5258
              From here it follows that range access used either one, or
 
5259
              both of the (I1) and (I2) intervals:
 
5260
 
 
5261
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
 
5262
               (same-as-above but with one cond replaced
 
5263
                with "t.keypart_i IS NULL")               (I2)
 
5264
 
 
5265
              The remaining part is to exclude the situation where range
 
5266
              optimizer used one interval while we're considering
 
5267
              ref-or-null and looking for estimate for two intervals. This
 
5268
              is done by last limitation:
 
5269
 
 
5270
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5271
            */
 
5272
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5273
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5274
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5275
            {
 
5276
              tmp= records= (double) table->quick_rows[key];
 
5277
            }
 
5278
            else
 
5279
            {
 
5280
              /* Check if we have statistic about the distribution */
 
5281
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5282
              {
 
5283
                /*
 
5284
                  Fix for the case where the index statistics is too
 
5285
                  optimistic: If
 
5286
                  (1) We're considering ref(const) and there is quick select
 
5287
                      on the same index,
 
5288
                  (2) and that quick select uses more keyparts (i.e. it will
 
5289
                      scan equal/smaller interval then this ref(const))
 
5290
                  (3) and E(#rows) for quick select is higher then our
 
5291
                      estimate,
 
5292
                  Then
 
5293
                    We'll use E(#rows) from quick select.
 
5294
 
 
5295
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5296
                  cheaper in some cases ?
 
5297
                  TODO: figure this out and adjust the plan choice if needed.
 
5298
                */
 
5299
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5300
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5301
                    records < (double)table->quick_rows[key])         // (3)
 
5302
                  records= (double)table->quick_rows[key];
 
5303
 
 
5304
                tmp= records;
 
5305
              }
 
5306
              else
 
5307
              {
 
5308
                /*
 
5309
                  Assume that the first key part matches 1% of the file
 
5310
                  and that the whole key matches 10 (duplicates) or 1
 
5311
                  (unique) records.
 
5312
                  Assume also that more key matches proportionally more
 
5313
                  records
 
5314
                  This gives the formula:
 
5315
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5316
 
 
5317
                  b = records matched by whole key
 
5318
                  a = records matched by first key part (1% of all records?)
 
5319
                  c = number of key parts in key
 
5320
                  x = used key parts (1 <= x <= c)
 
5321
                */
 
5322
                double rec_per_key;
 
5323
                if (!(rec_per_key=(double)
 
5324
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5325
                  rec_per_key=(double) s->records/rec+1;
 
5326
 
 
5327
                if (!s->records)
 
5328
                  tmp = 0;
 
5329
                else if (rec_per_key/(double) s->records >= 0.01)
 
5330
                  tmp = rec_per_key;
 
5331
                else
 
5332
                {
 
5333
                  double a=s->records*0.01;
 
5334
                  if (keyinfo->key_parts > 1)
 
5335
                    tmp= (max_key_part * (rec_per_key - a) +
 
5336
                          a*keyinfo->key_parts - rec_per_key)/
 
5337
                         (keyinfo->key_parts-1);
 
5338
                  else
 
5339
                    tmp= a;
 
5340
                  set_if_bigger(tmp,1.0);
 
5341
                }
 
5342
                records = (uint32_t) tmp;
 
5343
              }
 
5344
 
 
5345
              if (ref_or_null_part)
 
5346
              {
 
5347
                /* We need to do two key searches to find key */
 
5348
                tmp *= 2.0;
 
5349
                records *= 2.0;
 
5350
              }
 
5351
 
 
5352
              /*
 
5353
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5354
                E(#rows) from range optimizer. Make another try:
 
5355
 
 
5356
                If range optimizer produced E(#rows) for a prefix of the ref
 
5357
                access we're considering, and that E(#rows) is lower then our
 
5358
                current estimate, make the adjustment.
 
5359
 
 
5360
                The decision whether we can re-use the estimate from the range
 
5361
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5362
                applied to first table->quick_key_parts[key] key parts.
 
5363
              */
 
5364
              if (table->quick_keys.is_set(key) &&
 
5365
                  table->quick_key_parts[key] <= max_key_part &&
 
5366
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5367
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
5368
                                                     const_part) ? 1 : 0) &&
 
5369
                  records > (double) table->quick_rows[key])
 
5370
              {
 
5371
                tmp= records= (double) table->quick_rows[key];
 
5372
              }
 
5373
            }
 
5374
 
 
5375
            /* Limit the number of matched rows */
 
5376
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5377
            if (table->covering_keys.is_set(key))
 
5378
            {
 
5379
              /* we can use only index tree */
 
5380
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5381
            }
 
5382
            else
 
5383
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5384
          }
 
5385
          else
 
5386
            tmp= best_time;                    // Do nothing
 
5387
        }
 
5388
 
 
5389
        if (sj_inside_out_scan && !start_key)
 
5390
        {
 
5391
          tmp= tmp/2;
 
5392
          if (records)
 
5393
            records= records/2;
 
5394
        }
 
5395
 
 
5396
      }
 
5397
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5398
      {
 
5399
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5400
        best= tmp;
 
5401
        best_records= records;
 
5402
        best_key= start_key;
 
5403
        best_max_key_part= max_key_part;
 
5404
        best_ref_depends_map= found_ref;
 
5405
        best_is_sj_inside_out= sj_inside_out_scan;
 
5406
      }
 
5407
    }
 
5408
    records= best_records;
 
5409
  }
 
5410
 
 
5411
  /*
 
5412
    Don't test table scan if it can't be better.
 
5413
    Prefer key lookup if we would use the same key for scanning.
 
5414
 
 
5415
    Don't do a table scan on InnoDB tables, if we can read the used
 
5416
    parts of the row from any of the used index.
 
5417
    This is because table scans uses index and we would not win
 
5418
    anything by using a table scan.
 
5419
 
 
5420
    A word for word translation of the below if-statement in sergefp's
 
5421
    understanding: we check if we should use table scan if:
 
5422
    (1) The found 'ref' access produces more records than a table scan
 
5423
        (or index scan, or quick select), or 'ref' is more expensive than
 
5424
        any of them.
 
5425
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5426
        'range' access using index IDX, and the best way to perform 'ref'
 
5427
        access is to use the same index IDX, with the same or more key parts.
 
5428
        (note: it is not clear how this rule is/should be extended to
 
5429
        index_merge quick selects)
 
5430
    (3) See above note about InnoDB.
 
5431
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5432
             path, but there is no quick select)
 
5433
        If the condition in the above brackets holds, then the only possible
 
5434
        "table scan" access method is ALL/index (there is no quick select).
 
5435
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5436
        choose it over ALL/index, there is no need to consider a full table
 
5437
        scan.
 
5438
  */
 
5439
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5440
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5441
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5442
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5443
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5444
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5445
  {                                             // Check full join
 
5446
    ha_rows rnd_records= s->found_records;
 
5447
    /*
 
5448
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5449
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5450
      preceding this table in the join order we're now considering), then
 
5451
      assume that 25% of the rows will be filtered out by this condition.
 
5452
 
 
5453
      This heuristic is supposed to force tables used in exprZ to be before
 
5454
      this table in join order.
 
5455
    */
 
5456
    if (found_constraint)
 
5457
      rnd_records-= rnd_records/4;
 
5458
 
 
5459
    /*
 
5460
      If applicable, get a more accurate estimate. Don't use the two
 
5461
      heuristics at once.
 
5462
    */
 
5463
    if (s->table->quick_condition_rows != s->found_records)
 
5464
      rnd_records= s->table->quick_condition_rows;
 
5465
 
 
5466
    /*
 
5467
      Range optimizer never proposes a RANGE if it isn't better
 
5468
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5469
      Here we estimate its cost.
 
5470
    */
 
5471
    if (s->quick)
 
5472
    {
 
5473
      /*
 
5474
        For each record we:
 
5475
        - read record range through 'quick'
 
5476
        - skip rows which does not satisfy WHERE constraints
 
5477
        TODO:
 
5478
        We take into account possible use of join cache for ALL/index
 
5479
        access (see first else-branch below), but we don't take it into
 
5480
        account here for range/index_merge access. Find out why this is so.
 
5481
      */
 
5482
      tmp= record_count *
 
5483
        (s->quick->read_time +
 
5484
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5485
    }
 
5486
    else
 
5487
    {
 
5488
      /* Estimate cost of reading table. */
 
5489
      tmp= s->table->file->scan_time();
 
5490
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5491
      {
 
5492
        /*
 
5493
          For each record we have to:
 
5494
          - read the whole table record
 
5495
          - skip rows which does not satisfy join condition
 
5496
        */
 
5497
        tmp= record_count *
 
5498
          (tmp +
 
5499
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5500
      }
 
5501
      else
 
5502
      {
 
5503
        /* We read the table as many times as join buffer becomes full. */
 
5504
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5505
                           record_count /
 
5506
                           (double) session->variables.join_buff_size));
 
5507
        /*
 
5508
            We don't make full cartesian product between rows in the scanned
 
5509
           table and existing records because we skip all rows from the
 
5510
           scanned table, which does not satisfy join condition when
 
5511
           we read the table (see flush_cached_records for details). Here we
 
5512
           take into account cost to read and skip these records.
 
5513
        */
 
5514
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5515
      }
 
5516
    }
 
5517
 
 
5518
    /*
 
5519
      We estimate the cost of evaluating WHERE clause for found records
 
5520
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5521
      tmp give us total cost of using Table SCAN
 
5522
    */
 
5523
    if (best == DBL_MAX ||
 
5524
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5525
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5526
    {
 
5527
      /*
 
5528
        If the table has a range (s->quick is set) make_join_select()
 
5529
        will ensure that this will be used
 
5530
      */
 
5531
      best= tmp;
 
5532
      records= rows2double(rnd_records);
 
5533
      best_key= 0;
 
5534
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5535
      best_ref_depends_map= 0;
 
5536
      best_is_sj_inside_out= false;
 
5537
    }
 
5538
  }
 
5539
 
 
5540
  /* Update the cost information for the current partial plan */
 
5541
  join->positions[idx].records_read= records;
 
5542
  join->positions[idx].read_time=    best;
 
5543
  join->positions[idx].key=          best_key;
 
5544
  join->positions[idx].table=        s;
 
5545
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5546
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5547
 
 
5548
  if (!best_key &&
 
5549
      idx == join->const_tables &&
 
5550
      s->table == join->sort_by_table &&
 
5551
      join->unit->select_limit_cnt >= records)
 
5552
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5553
 
 
5554
  return;
 
5555
}
 
5556
 
 
5557
 
 
5558
/**
 
5559
  Selects and invokes a search strategy for an optimal query plan.
 
5560
 
 
5561
  The function checks user-configurable parameters that control the search
 
5562
  strategy for an optimal plan, selects the search method and then invokes
 
5563
  it. Each specific optimization procedure stores the final optimal plan in
 
5564
  the array 'join->best_positions', and the cost of the plan in
 
5565
  'join->best_read'.
 
5566
 
 
5567
  @param join         pointer to the structure providing all context info for
 
5568
                      the query
 
5569
  @param join_tables  set of the tables in the query
 
5570
 
 
5571
  @todo
 
5572
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5573
    the greedy version. Will be removed when greedy_search is approved.
 
5574
 
 
5575
  @retval
 
5576
    false       ok
 
5577
  @retval
 
5578
    true        Fatal error
 
5579
*/
 
5580
 
 
5581
static bool
 
5582
choose_plan(JOIN *join, table_map join_tables)
 
5583
{
 
5584
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
 
5585
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
 
5586
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5587
 
 
5588
  join->cur_embedding_map= 0;
 
5589
  reset_nj_counters(join->join_list);
 
5590
  /*
 
5591
    if (SELECT_STRAIGHT_JOIN option is set)
 
5592
      reorder tables so dependent tables come after tables they depend
 
5593
      on, otherwise keep tables in the order they were specified in the query
 
5594
    else
 
5595
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5596
      records accessed.
 
5597
  */
 
5598
  my_qsort(join->best_ref + join->const_tables,
 
5599
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5600
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5601
  join->cur_emb_sj_nests= 0;
 
5602
  if (straight_join)
 
5603
  {
 
5604
    optimize_straight_join(join, join_tables);
 
5605
  }
 
5606
  else
 
5607
  {
 
5608
    if (search_depth == MAX_TABLES+2)
 
5609
    { /*
 
5610
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5611
        the greedy version. Will be removed when greedy_search is approved.
 
5612
      */
 
5613
      join->best_read= DBL_MAX;
 
5614
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5615
        return(true);
 
5616
    }
 
5617
    else
 
5618
    {
 
5619
      if (search_depth == 0)
 
5620
        /* Automatically determine a reasonable value for 'search_depth' */
 
5621
        search_depth= determine_search_depth(join);
 
5622
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5623
        return(true);
 
5624
    }
 
5625
  }
 
5626
 
 
5627
  /*
 
5628
    Store the cost of this query into a user variable
 
5629
    Don't update last_query_cost for statements that are not "flat joins" :
 
5630
    i.e. they have subqueries, unions or call stored procedures.
 
5631
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5632
  */
 
5633
  if (join->session->lex->is_single_level_stmt())
 
5634
    join->session->status_var.last_query_cost= join->best_read;
 
5635
  return(false);
 
5636
}
 
5637
 
 
5638
 
 
5639
/**
 
5640
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5641
 
 
5642
  @param ptr1 pointer to first JOIN_TAB object
 
5643
  @param ptr2 pointer to second JOIN_TAB object
798
5644
 
799
5645
  NOTES
800
5646
    The order relation implemented by join_tab_cmp() is not transitive,
814
5660
  @retval
815
5661
    0  if equal
816
5662
*/
 
5663
 
817
5664
int join_tab_cmp(const void* ptr1, const void* ptr2)
818
5665
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5666
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5667
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5668
 
822
5669
  if (jt1->dependent & jt2->table->map)
823
5670
    return 1;
830
5677
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5678
}
832
5679
 
 
5680
 
833
5681
/**
834
5682
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5683
*/
 
5684
 
836
5685
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5686
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5687
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5688
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5689
 
841
5690
  if (jt1->dependent & jt2->table->map)
842
5691
    return 1;
846
5695
}
847
5696
 
848
5697
/**
 
5698
  Heuristic procedure to automatically guess a reasonable degree of
 
5699
  exhaustiveness for the greedy search procedure.
 
5700
 
 
5701
  The procedure estimates the optimization time and selects a search depth
 
5702
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5703
  find. If the number of tables in the query exceeds some constant, then
 
5704
  search_depth is set to this constant.
 
5705
 
 
5706
  @param join   pointer to the structure providing all context info for
 
5707
                the query
 
5708
 
 
5709
  @note
 
5710
    This is an extremely simplistic implementation that serves as a stub for a
 
5711
    more advanced analysis of the join. Ideally the search depth should be
 
5712
    determined by learning from previous query optimizations, because it will
 
5713
    depend on the CPU power (and other factors).
 
5714
 
 
5715
  @todo
 
5716
    this value should be determined dynamically, based on statistics:
 
5717
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5718
 
 
5719
  @todo
 
5720
    this value could be determined by some mapping of the form:
 
5721
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5722
 
 
5723
  @return
 
5724
    A positive integer that specifies the search depth (and thus the
 
5725
    exhaustiveness) of the depth-first search algorithm used by
 
5726
    'greedy_search'.
 
5727
*/
 
5728
 
 
5729
static uint
 
5730
determine_search_depth(JOIN *join)
 
5731
{
 
5732
  uint32_t table_count=  join->tables - join->const_tables;
 
5733
  uint32_t search_depth;
 
5734
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5735
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5736
 
 
5737
  if (table_count <= max_tables_for_exhaustive_opt)
 
5738
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5739
  else
 
5740
    /*
 
5741
      TODO: this value could be determined by some mapping of the form:
 
5742
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5743
    */
 
5744
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5745
 
 
5746
  return search_depth;
 
5747
}
 
5748
 
 
5749
 
 
5750
/**
 
5751
  Select the best ways to access the tables in a query without reordering them.
 
5752
 
 
5753
    Find the best access paths for each query table and compute their costs
 
5754
    according to their order in the array 'join->best_ref' (thus without
 
5755
    reordering the join tables). The function calls sequentially
 
5756
    'best_access_path' for each table in the query to select the best table
 
5757
    access method. The final optimal plan is stored in the array
 
5758
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5759
 
 
5760
  @param join          pointer to the structure providing all context info for
 
5761
                       the query
 
5762
  @param join_tables   set of the tables in the query
 
5763
 
 
5764
  @note
 
5765
    This function can be applied to:
 
5766
    - queries with STRAIGHT_JOIN
 
5767
    - internally to compute the cost of an arbitrary QEP
 
5768
  @par
 
5769
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5770
    optimization process to finalize a QEP as it is.
 
5771
*/
 
5772
 
 
5773
static void
 
5774
optimize_straight_join(JOIN *join, table_map join_tables)
 
5775
{
 
5776
  JOIN_TAB *s;
 
5777
  uint32_t idx= join->const_tables;
 
5778
  double    record_count= 1.0;
 
5779
  double    read_time=    0.0;
 
5780
 
 
5781
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5782
  {
 
5783
    /* Find the best access method from 's' to the current partial plan */
 
5784
    advance_sj_state(join_tables, s);
 
5785
    best_access_path(join, s, join->session, join_tables, idx,
 
5786
                     record_count, read_time);
 
5787
    /* compute the cost of the new plan extended with 's' */
 
5788
    record_count*= join->positions[idx].records_read;
 
5789
    read_time+=    join->positions[idx].read_time;
 
5790
    join_tables&= ~(s->table->map);
 
5791
    ++idx;
 
5792
  }
 
5793
 
 
5794
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5795
  if (join->sort_by_table &&
 
5796
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5797
    read_time+= record_count;  // We have to make a temp table
 
5798
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5799
  join->best_read= read_time;
 
5800
}
 
5801
 
 
5802
 
 
5803
/**
 
5804
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5805
 
 
5806
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5807
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5808
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5809
    selects the most promising table, and extends the current partial QEP with
 
5810
    that table.  Currenly the most 'promising' table is the one with least
 
5811
    expensive extension.\
 
5812
 
 
5813
    There are two extreme cases:
 
5814
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5815
    best complete continuation of the partial QEP. This continuation can be
 
5816
    used directly as a result of the search.
 
5817
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5818
    consideres the extension of the current QEP with each of the remaining
 
5819
    unoptimized tables.
 
5820
 
 
5821
    All other cases are in-between these two extremes. Thus the parameter
 
5822
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5823
    value, the longer the optimizaton time and possibly the better the
 
5824
    resulting plan. The lower the value, the fewer alternative plans are
 
5825
    estimated, but the more likely to get a bad QEP.
 
5826
 
 
5827
    All intermediate and final results of the procedure are stored in 'join':
 
5828
    - join->positions     : modified for every partial QEP that is explored
 
5829
    - join->best_positions: modified for the current best complete QEP
 
5830
    - join->best_read     : modified for the current best complete QEP
 
5831
    - join->best_ref      : might be partially reordered
 
5832
 
 
5833
    The final optimal plan is stored in 'join->best_positions', and its
 
5834
    corresponding cost in 'join->best_read'.
 
5835
 
 
5836
  @note
 
5837
    The following pseudocode describes the algorithm of 'greedy_search':
 
5838
 
 
5839
    @code
 
5840
    procedure greedy_search
 
5841
    input: remaining_tables
 
5842
    output: pplan;
 
5843
    {
 
5844
      pplan = <>;
 
5845
      do {
 
5846
        (t, a) = best_extension(pplan, remaining_tables);
 
5847
        pplan = concat(pplan, (t, a));
 
5848
        remaining_tables = remaining_tables - t;
 
5849
      } while (remaining_tables != {})
 
5850
      return pplan;
 
5851
    }
 
5852
 
 
5853
  @endcode
 
5854
    where 'best_extension' is a placeholder for a procedure that selects the
 
5855
    most "promising" of all tables in 'remaining_tables'.
 
5856
    Currently this estimate is performed by calling
 
5857
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5858
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5859
    mainly depends on that of 'best_extension_by_limited_search'.
 
5860
 
 
5861
  @par
 
5862
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5863
    worst-case complexity of this algorithm is <=
 
5864
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5865
    complexity of greedy_search is O(N!).
 
5866
 
 
5867
  @par
 
5868
    In the future, 'greedy_search' might be extended to support other
 
5869
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5870
 
 
5871
  @param join             pointer to the structure providing all context info
 
5872
                          for the query
 
5873
  @param remaining_tables set of tables not included into the partial plan yet
 
5874
  @param search_depth     controlls the exhaustiveness of the search
 
5875
  @param prune_level      the pruning heuristics that should be applied during
 
5876
                          search
 
5877
 
 
5878
  @retval
 
5879
    false       ok
 
5880
  @retval
 
5881
    true        Fatal error
 
5882
*/
 
5883
 
 
5884
static bool
 
5885
greedy_search(JOIN      *join,
 
5886
              table_map remaining_tables,
 
5887
              uint32_t      search_depth,
 
5888
              uint32_t      prune_level)
 
5889
{
 
5890
  double    record_count= 1.0;
 
5891
  double    read_time=    0.0;
 
5892
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
5893
  uint32_t      best_idx;
 
5894
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5895
  POSITION  best_pos;
 
5896
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5897
 
 
5898
  /* number of tables that remain to be optimized */
 
5899
  size_remain= my_count_bits(remaining_tables);
 
5900
 
 
5901
  do {
 
5902
    /* Find the extension of the current QEP with the lowest cost */
 
5903
    join->best_read= DBL_MAX;
 
5904
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5905
                                         read_time, search_depth, prune_level))
 
5906
      return(true);
 
5907
 
 
5908
    if (size_remain <= search_depth)
 
5909
    {
 
5910
      /*
 
5911
        'join->best_positions' contains a complete optimal extension of the
 
5912
        current partial QEP.
 
5913
      */
 
5914
      return(false);
 
5915
    }
 
5916
 
 
5917
    /* select the first table in the optimal extension as most promising */
 
5918
    best_pos= join->best_positions[idx];
 
5919
    best_table= best_pos.table;
 
5920
    /*
 
5921
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5922
      'join->positions' for cost estimates, therefore we have to update its
 
5923
      value.
 
5924
    */
 
5925
    join->positions[idx]= best_pos;
 
5926
 
 
5927
    /* find the position of 'best_table' in 'join->best_ref' */
 
5928
    best_idx= idx;
 
5929
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5930
    while (pos && best_table != pos)
 
5931
      pos= join->best_ref[++best_idx];
 
5932
    assert((pos != NULL)); // should always find 'best_table'
 
5933
    /* move 'best_table' at the first free position in the array of joins */
 
5934
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5935
 
 
5936
    /* compute the cost of the new plan extended with 'best_table' */
 
5937
    record_count*= join->positions[idx].records_read;
 
5938
    read_time+=    join->positions[idx].read_time;
 
5939
 
 
5940
    remaining_tables&= ~(best_table->table->map);
 
5941
    --size_remain;
 
5942
    ++idx;
 
5943
  } while (true);
 
5944
}
 
5945
 
 
5946
 
 
5947
/**
 
5948
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5949
  exhaustive search.
 
5950
 
 
5951
    The procedure searches for the optimal ordering of the query tables in set
 
5952
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5953
    each table. The choice of a table order and an access path for each table
 
5954
    constitutes a query execution plan (QEP) that fully specifies how to
 
5955
    execute the query.
 
5956
 
 
5957
    The maximal size of the found plan is controlled by the parameter
 
5958
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5959
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5960
    of only some of the query tables. Such "partial" optimal plans are useful
 
5961
    only as input to query optimization procedures, and cannot be used directly
 
5962
    to execute a query.
 
5963
 
 
5964
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5965
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5966
    evaluates the cost of the partial plan extended by all access plans for
 
5967
    each of the relations in 'remaining_tables', expands the current partial
 
5968
    plan with the access plan that results in lowest cost of the expanded
 
5969
    partial plan, and removes the corresponding relation from
 
5970
    'remaining_tables'. The algorithm continues until it either constructs a
 
5971
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5972
    search_depth.
 
5973
 
 
5974
    The final optimal plan is stored in 'join->best_positions'. The
 
5975
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5976
 
 
5977
  @note
 
5978
    The procedure uses a recursive depth-first search where the depth of the
 
5979
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5980
    parameter 'search_depth'.
 
5981
 
 
5982
  @note
 
5983
    The pseudocode below describes the algorithm of
 
5984
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5985
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5986
    the complexity of greedy_search is O(N!).
 
5987
 
 
5988
    @code
 
5989
    procedure best_extension_by_limited_search(
 
5990
      pplan in,             // in, partial plan of tables-joined-so-far
 
5991
      pplan_cost,           // in, cost of pplan
 
5992
      remaining_tables,     // in, set of tables not referenced in pplan
 
5993
      best_plan_so_far,     // in/out, best plan found so far
 
5994
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5995
      search_depth)         // in, maximum size of the plans being considered
 
5996
    {
 
5997
      for each table T from remaining_tables
 
5998
      {
 
5999
        // Calculate the cost of using table T as above
 
6000
        cost = complex-series-of-calculations;
 
6001
 
 
6002
        // Add the cost to the cost so far.
 
6003
        pplan_cost+= cost;
 
6004
 
 
6005
        if (pplan_cost >= best_plan_so_far_cost)
 
6006
          // pplan_cost already too great, stop search
 
6007
          continue;
 
6008
 
 
6009
        pplan= expand pplan by best_access_method;
 
6010
        remaining_tables= remaining_tables - table T;
 
6011
        if (remaining_tables is not an empty set
 
6012
            and
 
6013
            search_depth > 1)
 
6014
        {
 
6015
          best_extension_by_limited_search(pplan, pplan_cost,
 
6016
                                           remaining_tables,
 
6017
                                           best_plan_so_far,
 
6018
                                           best_plan_so_far_cost,
 
6019
                                           search_depth - 1);
 
6020
        }
 
6021
        else
 
6022
        {
 
6023
          best_plan_so_far_cost= pplan_cost;
 
6024
          best_plan_so_far= pplan;
 
6025
        }
 
6026
      }
 
6027
    }
 
6028
    @endcode
 
6029
 
 
6030
  @note
 
6031
    When 'best_extension_by_limited_search' is called for the first time,
 
6032
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6033
    The actual implementation provides a way to optionally use pruning
 
6034
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6035
    space by skipping some partial plans.
 
6036
 
 
6037
  @note
 
6038
    The parameter 'search_depth' provides control over the recursion
 
6039
    depth, and thus the size of the resulting optimal plan.
 
6040
 
 
6041
  @param join             pointer to the structure providing all context info
 
6042
                          for the query
 
6043
  @param remaining_tables set of tables not included into the partial plan yet
 
6044
  @param idx              length of the partial QEP in 'join->positions';
 
6045
                          since a depth-first search is used, also corresponds
 
6046
                          to the current depth of the search tree;
 
6047
                          also an index in the array 'join->best_ref';
 
6048
  @param record_count     estimate for the number of records returned by the
 
6049
                          best partial plan
 
6050
  @param read_time        the cost of the best partial plan
 
6051
  @param search_depth     maximum depth of the recursion and thus size of the
 
6052
                          found optimal plan
 
6053
                          (0 < search_depth <= join->tables+1).
 
6054
  @param prune_level      pruning heuristics that should be applied during
 
6055
                          optimization
 
6056
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6057
 
 
6058
  @retval
 
6059
    false       ok
 
6060
  @retval
 
6061
    true        Fatal error
 
6062
*/
 
6063
 
 
6064
static bool
 
6065
best_extension_by_limited_search(JOIN      *join,
 
6066
                                 table_map remaining_tables,
 
6067
                                 uint32_t      idx,
 
6068
                                 double    record_count,
 
6069
                                 double    read_time,
 
6070
                                 uint32_t      search_depth,
 
6071
                                 uint32_t      prune_level)
 
6072
{
 
6073
  Session *session= join->session;
 
6074
  if (session->killed)  // Abort
 
6075
    return(true);
 
6076
 
 
6077
  /*
 
6078
     'join' is a partial plan with lower cost than the best plan so far,
 
6079
     so continue expanding it further with the tables in 'remaining_tables'.
 
6080
  */
 
6081
  JOIN_TAB *s;
 
6082
  double best_record_count= DBL_MAX;
 
6083
  double best_read_time=    DBL_MAX;
 
6084
 
 
6085
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6086
  {
 
6087
    table_map real_table_bit= s->table->map;
 
6088
    if ((remaining_tables & real_table_bit) &&
 
6089
        !(remaining_tables & s->dependent) &&
 
6090
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6091
    {
 
6092
      double current_record_count, current_read_time;
 
6093
      advance_sj_state(remaining_tables, s);
 
6094
 
 
6095
      /*
 
6096
        psergey-insideout-todo:
 
6097
          when best_access_path() detects it could do an InsideOut scan or
 
6098
          some other scan, have it return an insideout scan and a flag that
 
6099
          requests to "fork" this loop iteration. (Q: how does that behave
 
6100
          when the depth is insufficient??)
 
6101
      */
 
6102
      /* Find the best access method from 's' to the current partial plan */
 
6103
      best_access_path(join, s, session, remaining_tables, idx,
 
6104
                       record_count, read_time);
 
6105
      /* Compute the cost of extending the plan with 's' */
 
6106
      current_record_count= record_count * join->positions[idx].records_read;
 
6107
      current_read_time=    read_time + join->positions[idx].read_time;
 
6108
 
 
6109
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6110
      if ((current_read_time +
 
6111
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6112
      {
 
6113
        restore_prev_nj_state(s);
 
6114
        restore_prev_sj_state(remaining_tables, s);
 
6115
        continue;
 
6116
      }
 
6117
 
 
6118
      /*
 
6119
        Prune some less promising partial plans. This heuristic may miss
 
6120
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6121
      */
 
6122
      if (prune_level == 1)
 
6123
      {
 
6124
        if (best_record_count > current_record_count ||
 
6125
            best_read_time > current_read_time ||
 
6126
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6127
        {
 
6128
          if (best_record_count >= current_record_count &&
 
6129
              best_read_time >= current_read_time &&
 
6130
              /* TODO: What is the reasoning behind this condition? */
 
6131
              (!(s->key_dependent & remaining_tables) ||
 
6132
               join->positions[idx].records_read < 2.0))
 
6133
          {
 
6134
            best_record_count= current_record_count;
 
6135
            best_read_time=    current_read_time;
 
6136
          }
 
6137
        }
 
6138
        else
 
6139
        {
 
6140
          restore_prev_nj_state(s);
 
6141
          restore_prev_sj_state(remaining_tables, s);
 
6142
          continue;
 
6143
        }
 
6144
      }
 
6145
 
 
6146
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6147
      { /* Recursively expand the current partial plan */
 
6148
        std::swap(join->best_ref[idx], *pos);
 
6149
        if (best_extension_by_limited_search(join,
 
6150
                                             remaining_tables & ~real_table_bit,
 
6151
                                             idx + 1,
 
6152
                                             current_record_count,
 
6153
                                             current_read_time,
 
6154
                                             search_depth - 1,
 
6155
                                             prune_level))
 
6156
          return(true);
 
6157
        std::swap(join->best_ref[idx], *pos);
 
6158
      }
 
6159
      else
 
6160
      { /*
 
6161
          'join' is either the best partial QEP with 'search_depth' relations,
 
6162
          or the best complete QEP so far, whichever is smaller.
 
6163
        */
 
6164
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6165
        if (join->sort_by_table &&
 
6166
            join->sort_by_table !=
 
6167
            join->positions[join->const_tables].table->table)
 
6168
          /* We have to make a temp table */
 
6169
          current_read_time+= current_record_count;
 
6170
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6171
        {
 
6172
          memcpy(join->best_positions, join->positions,
 
6173
                 sizeof(POSITION) * (idx + 1));
 
6174
          join->best_read= current_read_time - 0.001;
 
6175
        }
 
6176
      }
 
6177
      restore_prev_nj_state(s);
 
6178
      restore_prev_sj_state(remaining_tables, s);
 
6179
    }
 
6180
  }
 
6181
  return(false);
 
6182
}
 
6183
 
 
6184
 
 
6185
/**
 
6186
  @todo
 
6187
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6188
  tested and accepted.
 
6189
 
 
6190
  RETURN VALUES
 
6191
    false       ok
 
6192
    true        Fatal error
 
6193
*/
 
6194
static bool
 
6195
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6196
          double read_time)
 
6197
{
 
6198
  Session *session= join->session;
 
6199
  if (session->killed)
 
6200
    return(true);
 
6201
  if (!rest_tables)
 
6202
  {
 
6203
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6204
    if (join->sort_by_table &&
 
6205
        join->sort_by_table !=
 
6206
        join->positions[join->const_tables].table->table)
 
6207
      read_time+=record_count;                  // We have to make a temp table
 
6208
    if (read_time < join->best_read)
 
6209
    {
 
6210
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6211
      join->best_read= read_time - 0.001;
 
6212
    }
 
6213
    return(false);
 
6214
  }
 
6215
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6216
    return(false);                                      /* Found better before */
 
6217
 
 
6218
  JOIN_TAB *s;
 
6219
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6220
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6221
  {
 
6222
    table_map real_table_bit=s->table->map;
 
6223
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6224
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6225
    {
 
6226
      double records, best;
 
6227
      advance_sj_state(rest_tables, s);
 
6228
      best_access_path(join, s, session, rest_tables, idx, record_count,
 
6229
                       read_time);
 
6230
      records= join->positions[idx].records_read;
 
6231
      best= join->positions[idx].read_time;
 
6232
      /*
 
6233
        Go to the next level only if there hasn't been a better key on
 
6234
        this level! This will cut down the search for a lot simple cases!
 
6235
      */
 
6236
      double current_record_count=record_count*records;
 
6237
      double current_read_time=read_time+best;
 
6238
      if (best_record_count > current_record_count ||
 
6239
          best_read_time > current_read_time ||
 
6240
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6241
      {
 
6242
        if (best_record_count >= current_record_count &&
 
6243
            best_read_time >= current_read_time &&
 
6244
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6245
        {
 
6246
          best_record_count=current_record_count;
 
6247
          best_read_time=current_read_time;
 
6248
        }
 
6249
        std::swap(join->best_ref[idx], *pos);
 
6250
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6251
                      current_record_count,current_read_time))
 
6252
          return(true);
 
6253
        std::swap(join->best_ref[idx], *pos);
 
6254
      }
 
6255
      restore_prev_nj_state(s);
 
6256
      restore_prev_sj_state(rest_tables, s);
 
6257
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6258
        break;                          // Don't test all combinations
 
6259
    }
 
6260
  }
 
6261
  return(false);
 
6262
}
 
6263
 
 
6264
 
 
6265
/**
849
6266
  Find how much space the prevous read not const tables takes in cache.
850
6267
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6268
 
 
6269
static void calc_used_field_length(Session *, JOIN_TAB *join_tab)
852
6270
{
853
6271
  uint32_t null_fields,blobs,fields,rec_length;
854
6272
  Field **f_ptr,*field;
 
6273
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6274
 
856
6275
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6276
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6277
  {
859
 
    if (field->isReadSet())
 
6278
    if (bitmap_is_set(read_set, field->field_index))
860
6279
    {
861
6280
      uint32_t flags=field->flags;
862
6281
      fields++;
863
6282
      rec_length+=field->pack_length();
864
6283
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6284
        blobs++;
866
6285
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6286
        null_fields++;
868
6287
    }
869
6288
  }
870
6289
  if (null_fields)
873
6292
    rec_length+=sizeof(bool);
874
6293
  if (blobs)
875
6294
  {
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
 
6295
    uint32_t blob_length=(uint32_t) (join_tab->table->file->stats.mean_rec_length-
 
6296
                             (join_tab->table->getRecordLength()- rec_length));
 
6297
    rec_length+=(uint32_t) cmax((uint32_t)4,blob_length);
 
6298
  }
 
6299
  join_tab->used_fields=fields;
 
6300
  join_tab->used_fieldlength=rec_length;
 
6301
  join_tab->used_blobs=blobs;
 
6302
}
 
6303
 
 
6304
 
 
6305
static uint
 
6306
cache_record_length(JOIN *join,uint32_t idx)
 
6307
{
 
6308
  uint32_t length=0;
 
6309
  JOIN_TAB **pos,**end;
 
6310
  Session *session=join->session;
 
6311
 
 
6312
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6313
       pos != end ;
 
6314
       pos++)
 
6315
  {
 
6316
    JOIN_TAB *join_tab= *pos;
 
6317
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6318
      calc_used_field_length(session, join_tab);
 
6319
    length+=join_tab->used_fieldlength;
 
6320
  }
 
6321
  return length;
 
6322
}
 
6323
 
 
6324
 
 
6325
/*
 
6326
  Get the number of different row combinations for subset of partial join
 
6327
 
 
6328
  SYNOPSIS
 
6329
    prev_record_reads()
 
6330
      join       The join structure
 
6331
      idx        Number of tables in the partial join order (i.e. the
 
6332
                 partial join order is in join->positions[0..idx-1])
 
6333
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6334
                 row combinations.
 
6335
 
 
6336
  DESCRIPTION
 
6337
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6338
    tables within that join order (specified in found_ref), find out how many
 
6339
    distinct row combinations of subset tables will be in the result of the
 
6340
    partial join order.
 
6341
 
 
6342
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6343
    method. The ref access depends on current rows of tables in found_ref.
 
6344
    We want to count # of different ref accesses. We assume two ref accesses
 
6345
    will be different if at least one of access parameters is different.
 
6346
    Example: consider a query
 
6347
 
 
6348
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6349
 
 
6350
    and a join order:
 
6351
      t1,  ref access on t1.key=c1
 
6352
      t2,  ref access on t2.key=c2
 
6353
      t3,  ref access on t3.key=t1.field
 
6354
 
 
6355
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6356
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6357
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6358
            n_distinct_ref_scans = #records_read(t1)
 
6359
 
 
6360
    The reason for having this function (at least the latest version of it)
 
6361
    is that we need to account for buffering in join execution.
 
6362
 
 
6363
    An edge-case example: if we have a non-first table in join accessed via
 
6364
    ref(const) or ref(param) where there is a small number of different
 
6365
    values of param, then the access will likely hit the disk cache and will
 
6366
    not require any disk seeks.
 
6367
 
 
6368
    The proper solution would be to assume an LRU disk cache of some size,
 
6369
    calculate probability of cache hits, etc. For now we just count
 
6370
    identical ref accesses as one.
 
6371
 
 
6372
  RETURN
 
6373
    Expected number of row combinations
 
6374
*/
 
6375
 
 
6376
static double
 
6377
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6378
{
 
6379
  double found=1.0;
 
6380
  POSITION *pos_end= join->positions - 1;
 
6381
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6382
  {
 
6383
    if (pos->table->table->map & found_ref)
 
6384
    {
 
6385
      found_ref|= pos->ref_depend_map;
 
6386
      /*
 
6387
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
 
6388
        with no matching row we will get position[t2].records_read==0.
 
6389
        Actually the size of output is one null-complemented row, therefore
 
6390
        we will use value of 1 whenever we get records_read==0.
 
6391
 
 
6392
        Note
 
6393
        - the above case can't occur if inner part of outer join has more
 
6394
          than one table: table with no matches will not be marked as const.
 
6395
 
 
6396
        - Ideally we should add 1 to records_read for every possible null-
 
6397
          complemented row. We're not doing it because: 1. it will require
 
6398
          non-trivial code and add overhead. 2. The value of records_read
 
6399
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6400
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6401
      */
 
6402
      if (pos->records_read > DBL_EPSILON)
 
6403
        found*= pos->records_read;
 
6404
    }
 
6405
  }
 
6406
  return found;
 
6407
}
 
6408
 
 
6409
 
 
6410
/**
 
6411
  Set up join struct according to best position.
 
6412
*/
 
6413
 
 
6414
static bool
 
6415
get_best_combination(JOIN *join)
 
6416
{
 
6417
  uint32_t i,tablenr;
 
6418
  table_map used_tables;
 
6419
  JOIN_TAB *join_tab,*j;
 
6420
  KEYUSE *keyuse;
 
6421
  uint32_t table_count;
 
6422
  Session *session=join->session;
 
6423
 
 
6424
  table_count=join->tables;
 
6425
  if (!(join->join_tab=join_tab=
 
6426
        (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
 
6427
    return(true);
 
6428
 
 
6429
  join->full_join=0;
 
6430
 
 
6431
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6432
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
6433
  {
 
6434
    Table *form;
 
6435
    *j= *join->best_positions[tablenr].table;
 
6436
    form=join->table[tablenr]=j->table;
 
6437
    used_tables|= form->map;
 
6438
    form->reginfo.join_tab=j;
 
6439
    if (!*j->on_expr_ref)
 
6440
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6441
    if (j->type == JT_CONST)
 
6442
      continue;                                 // Handled in make_join_stat..
 
6443
 
 
6444
    j->ref.key = -1;
 
6445
    j->ref.key_parts=0;
 
6446
 
 
6447
    if (j->type == JT_SYSTEM)
 
6448
      continue;
 
6449
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
6450
    {
 
6451
      j->type=JT_ALL;
 
6452
      if (tablenr != join->const_tables)
 
6453
        join->full_join=1;
 
6454
    }
 
6455
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6456
      return(true);                        // Something went wrong
 
6457
  }
 
6458
 
 
6459
  for (i=0 ; i < table_count ; i++)
 
6460
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6461
  update_depend_map(join);
 
6462
  return(0);
 
6463
}
 
6464
 
 
6465
 
 
6466
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6467
                               table_map used_tables)
 
6468
{
 
6469
  KEYUSE *keyuse=org_keyuse;
 
6470
  Session  *session= join->session;
 
6471
  uint32_t keyparts,length,key;
 
6472
  Table *table;
 
6473
  KEY *keyinfo;
 
6474
 
 
6475
  /*  Use best key from find_best */
 
6476
  table=j->table;
 
6477
  key=keyuse->key;
 
6478
  keyinfo=table->key_info+key;
 
6479
 
 
6480
  {
 
6481
    keyparts=length=0;
 
6482
    uint32_t found_part_ref_or_null= 0;
 
6483
    /*
 
6484
      Calculate length for the used key
 
6485
      Stop if there is a missing key part or when we find second key_part
 
6486
      with KEY_OPTIMIZE_REF_OR_NULL
 
6487
    */
 
6488
    do
 
6489
    {
 
6490
      if (!(~used_tables & keyuse->used_tables))
 
6491
      {
 
6492
        if (keyparts == keyuse->keypart &&
 
6493
            !(found_part_ref_or_null & keyuse->optimize))
 
6494
        {
 
6495
          keyparts++;
 
6496
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6497
          found_part_ref_or_null|= keyuse->optimize;
 
6498
        }
 
6499
      }
 
6500
      keyuse++;
 
6501
    } while (keyuse->table == table && keyuse->key == key);
 
6502
  }
 
6503
 
 
6504
  /* set up fieldref */
 
6505
  keyinfo=table->key_info+key;
 
6506
  j->ref.key_parts=keyparts;
 
6507
  j->ref.key_length=length;
 
6508
  j->ref.key=(int) key;
 
6509
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
 
6510
      !(j->ref.key_copy= (store_key**) session->alloc((sizeof(store_key*) *
 
6511
                                                   (keyparts+1)))) ||
 
6512
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
6513
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6514
  {
 
6515
    return(true);
 
6516
  }
 
6517
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
 
6518
  j->ref.key_err=1;
 
6519
  j->ref.null_rejecting= 0;
 
6520
  j->ref.disable_cache= false;
 
6521
  keyuse=org_keyuse;
 
6522
 
 
6523
  store_key **ref_key= j->ref.key_copy;
 
6524
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6525
  bool keyuse_uses_no_tables= true;
 
6526
  {
 
6527
    uint32_t i;
 
6528
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
6529
    {
 
6530
      while (keyuse->keypart != i ||
 
6531
             ((~used_tables) & keyuse->used_tables))
 
6532
        keyuse++;                               /* Skip other parts */
 
6533
 
 
6534
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6535
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6536
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6537
      if (keyuse->null_rejecting)
 
6538
        j->ref.null_rejecting |= 1 << i;
 
6539
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6540
      if (!keyuse->used_tables &&
 
6541
          !(join->select_options & SELECT_DESCRIBE))
 
6542
      {                                 // Compare against constant
 
6543
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6544
                           key_buff + maybe_null,
 
6545
                           maybe_null ?  key_buff : 0,
 
6546
                           keyinfo->key_part[i].length, keyuse->val);
 
6547
        if (session->is_fatal_error)
 
6548
          return(true);
 
6549
        tmp.copy();
 
6550
      }
 
6551
      else
 
6552
        *ref_key++= get_store_key(session,
 
6553
                                  keyuse,join->const_table_map,
 
6554
                                  &keyinfo->key_part[i],
 
6555
                                  key_buff, maybe_null);
 
6556
      /*
 
6557
        Remember if we are going to use REF_OR_NULL
 
6558
        But only if field _really_ can be null i.e. we force JT_REF
 
6559
        instead of JT_REF_OR_NULL in case if field can't be null
 
6560
      */
 
6561
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6562
        null_ref_key= key_buff;
 
6563
      key_buff+=keyinfo->key_part[i].store_length;
 
6564
    }
 
6565
  }
 
6566
  *ref_key=0;                           // end_marker
 
6567
  if (j->type == JT_CONST)
 
6568
    j->table->const_table= 1;
 
6569
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
 
6570
           keyparts != keyinfo->key_parts || null_ref_key)
 
6571
  {
 
6572
    /* Must read with repeat */
 
6573
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
6574
    j->ref.null_ref_key= null_ref_key;
 
6575
  }
 
6576
  else if (keyuse_uses_no_tables)
 
6577
  {
 
6578
    /*
 
6579
      This happen if we are using a constant expression in the ON part
 
6580
      of an LEFT JOIN.
 
6581
      SELECT * FROM a LEFT JOIN b ON b.key=30
 
6582
      Here we should not mark the table as a 'const' as a field may
 
6583
      have a 'normal' value or a NULL value.
 
6584
    */
 
6585
    j->type=JT_CONST;
 
6586
  }
 
6587
  else
 
6588
    j->type=JT_EQ_REF;
 
6589
  return(0);
 
6590
}
 
6591
 
 
6592
 
 
6593
 
 
6594
static store_key *
 
6595
get_store_key(Session *session, KEYUSE *keyuse, table_map used_tables,
 
6596
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6597
{
 
6598
  if (!((~used_tables) & keyuse->used_tables))          // if const item
894
6599
  {
895
6600
    return new store_key_const_item(session,
896
6601
                                    key_part->field,
897
6602
                                    key_buff + maybe_null,
898
6603
                                    maybe_null ? key_buff : 0,
899
6604
                                    key_part->length,
900
 
                                    key_use_val);
 
6605
                                    keyuse->val);
901
6606
  }
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
 
  {
 
6607
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6608
           (keyuse->val->type() == Item::REF_ITEM &&
 
6609
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6610
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6611
             Item_ref::DIRECT_REF &&
 
6612
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
908
6613
    return new store_key_field(session,
909
6614
                               key_part->field,
910
6615
                               key_buff + maybe_null,
911
6616
                               maybe_null ? key_buff : 0,
912
6617
                               key_part->length,
913
 
                               ((Item_field*) key_use_val->real_item())->field,
914
 
                               key_use_val->full_name());
915
 
  }
 
6618
                               ((Item_field*) keyuse->val->real_item())->field,
 
6619
                               keyuse->val->full_name());
916
6620
  return new store_key_item(session,
917
6621
                            key_part->field,
918
6622
                            key_buff + maybe_null,
919
6623
                            maybe_null ? key_buff : 0,
920
6624
                            key_part->length,
921
 
                            key_use_val);
 
6625
                            keyuse->val);
922
6626
}
923
6627
 
924
6628
/**
927
6631
  @return
928
6632
    returns 1 if there was some conversion made when the field was stored.
929
6633
*/
930
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6634
 
 
6635
bool
 
6636
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
931
6637
{
932
6638
  bool error;
933
 
  Table *table= field->getTable();
 
6639
  Table *table= field->table;
934
6640
  Session *session= table->in_use;
935
6641
  ha_rows cuted_fields=session->cuted_fields;
936
6642
 
937
6643
  /*
938
6644
    we should restore old value of count_cuted_fields because
939
 
    store_val_in_field can be called from insert_query
 
6645
    store_val_in_field can be called from mysql_insert
940
6646
    with select_insert, which make count_cuted_fields= 1
941
6647
   */
942
6648
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
946
6652
  return error || cuted_fields != session->cuted_fields;
947
6653
}
948
6654
 
 
6655
 
 
6656
static bool
 
6657
make_simple_join(JOIN *join,Table *tmp_table)
 
6658
{
 
6659
  Table **tableptr;
 
6660
  JOIN_TAB *join_tab;
 
6661
 
 
6662
  /*
 
6663
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6664
    to this function through JOIN::exec (may happen for sub-queries).
 
6665
  */
 
6666
  if (!join->table_reexec)
 
6667
  {
 
6668
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
6669
      return(true);                        /* purecov: inspected */
 
6670
    if (join->tmp_join)
 
6671
      join->tmp_join->table_reexec= join->table_reexec;
 
6672
  }
 
6673
  if (!join->join_tab_reexec)
 
6674
  {
 
6675
    if (!(join->join_tab_reexec=
 
6676
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
 
6677
      return(true);                        /* purecov: inspected */
 
6678
    if (join->tmp_join)
 
6679
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6680
  }
 
6681
  tableptr= join->table_reexec;
 
6682
  join_tab= join->join_tab_reexec;
 
6683
 
 
6684
  join->join_tab=join_tab;
 
6685
  join->table=tableptr; tableptr[0]=tmp_table;
 
6686
  join->tables=1;
 
6687
  join->const_tables=0;
 
6688
  join->const_table_map=0;
 
6689
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6690
    join->tmp_table_param.func_count=0;
 
6691
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6692
  join->first_record=join->sort_and_group=0;
 
6693
  join->send_records=(ha_rows) 0;
 
6694
  join->group=0;
 
6695
  join->row_limit=join->unit->select_limit_cnt;
 
6696
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6697
 
 
6698
  join_tab->cache.buff=0;                       /* No caching */
 
6699
  join_tab->table=tmp_table;
 
6700
  join_tab->select=0;
 
6701
  join_tab->select_cond=0;
 
6702
  join_tab->quick=0;
 
6703
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6704
  join_tab->keys.init();
 
6705
  join_tab->keys.set_all();                     /* test everything in quick */
 
6706
  join_tab->info=0;
 
6707
  join_tab->on_expr_ref=0;
 
6708
  join_tab->last_inner= 0;
 
6709
  join_tab->first_unmatched= 0;
 
6710
  join_tab->ref.key = -1;
 
6711
  join_tab->not_used_in_distinct=0;
 
6712
  join_tab->read_first_record= join_init_read_record;
 
6713
  join_tab->join=join;
 
6714
  join_tab->ref.key_parts= 0;
 
6715
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6716
  join_tab->do_firstmatch= NULL;
 
6717
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6718
  tmp_table->status=0;
 
6719
  tmp_table->null_row=0;
 
6720
  return(false);
 
6721
}
 
6722
 
 
6723
 
949
6724
inline void add_cond_and_fix(Item **e1, Item *e2)
950
6725
{
951
6726
  if (*e1)
961
6736
    *e1= e2;
962
6737
}
963
6738
 
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
6739
 
1092
6740
/**
1093
6741
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1133
6781
 
1134
6782
    Implementation overview
1135
6783
      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.
 
6784
         predicates in in KEY_FIELD::null_rejecting
 
6785
      1.1 add_key_part saves these to KEYUSE.
 
6786
      2. create_ref_for_key copies them to TABLE_REF.
1139
6787
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6788
         appropiate JOIN_TAB members.
1141
6789
*/
1142
 
void add_not_null_conds(Join *join)
 
6790
 
 
6791
static void add_not_null_conds(JOIN *join)
1143
6792
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6793
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
1145
6794
  {
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) &&
 
6795
    JOIN_TAB *tab=join->join_tab+i;
 
6796
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
 
6797
         tab->type == JT_REF_OR_NULL) &&
1149
6798
        !tab->table->maybe_null)
1150
6799
    {
1151
6800
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1156
6805
          Item *notnull;
1157
6806
          assert(item->type() == Item::FIELD_ITEM);
1158
6807
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6808
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6809
          /*
1161
6810
            For UPDATE queries such as:
1162
6811
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1197
6846
    -  pointer to the guarded predicate, if success
1198
6847
    -  0, otherwise
1199
6848
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6849
 
 
6850
static COND*
 
6851
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6852
{
1202
6853
  COND *tmp;
1203
6854
  assert(cond != 0);
1213
6864
  return tmp;
1214
6865
}
1215
6866
 
 
6867
 
 
6868
/**
 
6869
  Fill in outer join related info for the execution plan structure.
 
6870
 
 
6871
    For each outer join operation left after simplification of the
 
6872
    original query the function set up the following pointers in the linear
 
6873
    structure join->join_tab representing the selected execution plan.
 
6874
    The first inner table t0 for the operation is set to refer to the last
 
6875
    inner table tk through the field t0->last_inner.
 
6876
    Any inner table ti for the operation are set to refer to the first
 
6877
    inner table ti->first_inner.
 
6878
    The first inner table t0 for the operation is set to refer to the
 
6879
    first inner table of the embedding outer join operation, if there is any,
 
6880
    through the field t0->first_upper.
 
6881
    The on expression for the outer join operation is attached to the
 
6882
    corresponding first inner table through the field t0->on_expr_ref.
 
6883
    Here ti are structures of the JOIN_TAB type.
 
6884
 
 
6885
  EXAMPLE. For the query:
 
6886
  @code
 
6887
        SELECT * FROM t1
 
6888
                      LEFT JOIN
 
6889
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6890
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6891
          WHERE t1.c > 5,
 
6892
  @endcode
 
6893
 
 
6894
    given the execution plan with the table order t1,t2,t3,t4
 
6895
    is selected, the following references will be set;
 
6896
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6897
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6898
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
 
6899
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6900
 
 
6901
  @param join   reference to the info fully describing the query
 
6902
 
 
6903
  @note
 
6904
    The function assumes that the simplification procedure has been
 
6905
    already applied to the join query (see simplify_joins).
 
6906
    This function can be called only after the execution plan
 
6907
    has been chosen.
 
6908
*/
 
6909
 
 
6910
static void
 
6911
make_outerjoin_info(JOIN *join)
 
6912
{
 
6913
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6914
  {
 
6915
    JOIN_TAB *tab=join->join_tab+i;
 
6916
    Table *table=tab->table;
 
6917
    TableList *tbl= table->pos_in_table_list;
 
6918
    TableList *embedding= tbl->embedding;
 
6919
 
 
6920
    if (tbl->outer_join)
 
6921
    {
 
6922
      /*
 
6923
        Table tab is the only one inner table for outer join.
 
6924
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6925
        is in the query above.)
 
6926
      */
 
6927
      tab->last_inner= tab->first_inner= tab;
 
6928
      tab->on_expr_ref= &tbl->on_expr;
 
6929
      tab->cond_equal= tbl->cond_equal;
 
6930
      if (embedding)
 
6931
        tab->first_upper= embedding->nested_join->first_nested;
 
6932
    }
 
6933
    for ( ; embedding ; embedding= embedding->embedding)
 
6934
    {
 
6935
      /* Ignore sj-nests: */
 
6936
      if (!embedding->on_expr)
 
6937
        continue;
 
6938
      nested_join_st *nested_join= embedding->nested_join;
 
6939
      if (!nested_join->counter_)
 
6940
      {
 
6941
        /*
 
6942
          Table tab is the first inner table for nested_join.
 
6943
          Save reference to it in the nested join structure.
 
6944
        */
 
6945
        nested_join->first_nested= tab;
 
6946
        tab->on_expr_ref= &embedding->on_expr;
 
6947
        tab->cond_equal= tbl->cond_equal;
 
6948
        if (embedding->embedding)
 
6949
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6950
      }
 
6951
      if (!tab->first_inner)
 
6952
        tab->first_inner= nested_join->first_nested;
 
6953
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6954
        break;
 
6955
      /* Table tab is the last inner table for nested join. */
 
6956
      nested_join->first_nested->last_inner= tab;
 
6957
    }
 
6958
  }
 
6959
  return;
 
6960
}
 
6961
 
 
6962
 
 
6963
static bool
 
6964
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6965
{
 
6966
  Session *session= join->session;
 
6967
  if (select)
 
6968
  {
 
6969
    add_not_null_conds(join);
 
6970
    table_map used_tables;
 
6971
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6972
    {                        /* there may be a select without a cond. */
 
6973
      if (join->tables > 1)
 
6974
        cond->update_used_tables();             // Tablenr may have changed
 
6975
      if (join->const_tables == join->tables &&
 
6976
          session->lex->current_select->master_unit() ==
 
6977
          &session->lex->unit)          // not upper level SELECT
 
6978
        join->const_table_map|=RAND_TABLE_BIT;
 
6979
      {                                         // Check const tables
 
6980
        COND *const_cond=
 
6981
          make_cond_for_table(cond,
 
6982
                              join->const_table_map,
 
6983
                              (table_map) 0, 1);
 
6984
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6985
             tab < join->join_tab+join->tables ; tab++)
 
6986
        {
 
6987
          if (*tab->on_expr_ref)
 
6988
          {
 
6989
            JOIN_TAB *cond_tab= tab->first_inner;
 
6990
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6991
                                           join->const_table_map,
 
6992
                                           (  table_map) 0, 0);
 
6993
            if (!tmp)
 
6994
              continue;
 
6995
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6996
            if (!tmp)
 
6997
              return(1);
 
6998
            tmp->quick_fix_field();
 
6999
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7000
                                    new Item_cond_and(cond_tab->select_cond,
 
7001
                                                      tmp);
 
7002
            if (!cond_tab->select_cond)
 
7003
              return(1);
 
7004
            cond_tab->select_cond->quick_fix_field();
 
7005
          }
 
7006
        }
 
7007
        if (const_cond && !const_cond->val_int())
 
7008
        {
 
7009
          return(1);     // Impossible const condition
 
7010
        }
 
7011
      }
 
7012
    }
 
7013
    used_tables=((select->const_tables=join->const_table_map) |
 
7014
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7015
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
7016
    {
 
7017
      JOIN_TAB *tab=join->join_tab+i;
 
7018
      /*
 
7019
        first_inner is the X in queries like:
 
7020
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7021
      */
 
7022
      JOIN_TAB *first_inner_tab= tab->first_inner;
 
7023
      table_map current_map= tab->table->map;
 
7024
      bool use_quick_range=0;
 
7025
      COND *tmp;
 
7026
 
 
7027
      /*
 
7028
        Following force including random expression in last table condition.
 
7029
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7030
      */
 
7031
      if (i == join->tables-1)
 
7032
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7033
      used_tables|=current_map;
 
7034
 
 
7035
      if (tab->type == JT_REF && tab->quick &&
 
7036
          (uint32_t) tab->ref.key == tab->quick->index &&
 
7037
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7038
      {
 
7039
        /* Range uses longer key;  Use this instead of ref on key */
 
7040
        tab->type=JT_ALL;
 
7041
        use_quick_range=1;
 
7042
        tab->use_quick=1;
 
7043
        tab->ref.key= -1;
 
7044
        tab->ref.key_parts=0;           // Don't use ref key.
 
7045
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7046
        /*
 
7047
          We will use join cache here : prevent sorting of the first
 
7048
          table only and sort at the end.
 
7049
        */
 
7050
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7051
          join->full_join= 1;
 
7052
      }
 
7053
 
 
7054
      tmp= NULL;
 
7055
      if (cond)
 
7056
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7057
      if (cond && !tmp && tab->quick)
 
7058
      {                                         // Outer join
 
7059
        if (tab->type != JT_ALL)
 
7060
        {
 
7061
          /*
 
7062
            Don't use the quick method
 
7063
            We come here in the case where we have 'key=constant' and
 
7064
            the test is removed by make_cond_for_table()
 
7065
          */
 
7066
          delete tab->quick;
 
7067
          tab->quick= 0;
 
7068
        }
 
7069
        else
 
7070
        {
 
7071
          /*
 
7072
            Hack to handle the case where we only refer to a table
 
7073
            in the ON part of an OUTER JOIN. In this case we want the code
 
7074
            below to check if we should use 'quick' instead.
 
7075
          */
 
7076
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7077
        }
 
7078
 
 
7079
      }
 
7080
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7081
          tab->type == JT_EQ_REF)
 
7082
      {
 
7083
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7084
                                       session->memdup((unsigned char*) select,
 
7085
                                                   sizeof(*select)));
 
7086
        if (!sel)
 
7087
          return(1);                    // End of memory
 
7088
        /*
 
7089
          If tab is an inner table of an outer join operation,
 
7090
          add a match guard to the pushed down predicate.
 
7091
          The guard will turn the predicate on only after
 
7092
          the first match for outer tables is encountered.
 
7093
        */
 
7094
        if (cond && tmp)
 
7095
        {
 
7096
          /*
 
7097
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7098
            a cond, so neutralize the hack above.
 
7099
          */
 
7100
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7101
            return(1);
 
7102
          tab->select_cond=sel->cond=tmp;
 
7103
          /* Push condition to storage engine if this is enabled
 
7104
             and the condition is not guarded */
 
7105
          tab->table->file->pushed_cond= NULL;
 
7106
          if (session->variables.engine_condition_pushdown)
 
7107
          {
 
7108
            COND *push_cond=
 
7109
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7110
            if (push_cond)
 
7111
            {
 
7112
              /* Push condition to handler */
 
7113
              if (!tab->table->file->cond_push(push_cond))
 
7114
                tab->table->file->pushed_cond= push_cond;
 
7115
            }
 
7116
          }
 
7117
        }
 
7118
        else
 
7119
          tab->select_cond= sel->cond= NULL;
 
7120
 
 
7121
        sel->head=tab->table;
 
7122
        if (tab->quick)
 
7123
        {
 
7124
          /* Use quick key read if it's a constant and it's not used
 
7125
             with key reading */
 
7126
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7127
              && (tab->type != JT_REF || (uint32_t) tab->ref.key == tab->quick->index))
 
7128
          {
 
7129
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7130
            sel->quick_keys.clear_all();
 
7131
            sel->needed_reg.clear_all();
 
7132
          }
 
7133
          else
 
7134
          {
 
7135
            delete tab->quick;
 
7136
          }
 
7137
          tab->quick=0;
 
7138
        }
 
7139
        uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
 
7140
        if (i == join->const_tables && ref_key)
 
7141
        {
 
7142
          if (!tab->const_keys.is_clear_all() &&
 
7143
              tab->table->reginfo.impossible_range)
 
7144
            return(1);
 
7145
        }
 
7146
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7147
        {
 
7148
          if (!tab->const_keys.is_clear_all() &&
 
7149
              tab->table->reginfo.impossible_range)
 
7150
            return(1);                          // Impossible range
 
7151
          /*
 
7152
            We plan to scan all rows.
 
7153
            Check again if we should use an index.
 
7154
            We could have used an column from a previous table in
 
7155
            the index if we are using limit and this is the first table
 
7156
          */
 
7157
 
 
7158
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7159
              (!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)))
 
7160
          {
 
7161
            /* Join with outer join condition */
 
7162
            COND *orig_cond=sel->cond;
 
7163
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7164
 
 
7165
            /*
 
7166
              We can't call sel->cond->fix_fields,
 
7167
              as it will break tab->on_expr if it's AND condition
 
7168
              (fix_fields currently removes extra AND/OR levels).
 
7169
              Yet attributes of the just built condition are not needed.
 
7170
              Thus we call sel->cond->quick_fix_field for safety.
 
7171
            */
 
7172
            if (sel->cond && !sel->cond->fixed)
 
7173
              sel->cond->quick_fix_field();
 
7174
 
 
7175
            if (sel->test_quick_select(session, tab->keys,
 
7176
                                       used_tables & ~ current_map,
 
7177
                                       (join->select_options &
 
7178
                                        OPTION_FOUND_ROWS ?
 
7179
                                        HA_POS_ERROR :
 
7180
                                        join->unit->select_limit_cnt), 0,
 
7181
                                        false) < 0)
 
7182
            {
 
7183
              /*
 
7184
                Before reporting "Impossible WHERE" for the whole query
 
7185
                we have to check isn't it only "impossible ON" instead
 
7186
              */
 
7187
              sel->cond=orig_cond;
 
7188
              if (!*tab->on_expr_ref ||
 
7189
                  sel->test_quick_select(session, tab->keys,
 
7190
                                         used_tables & ~ current_map,
 
7191
                                         (join->select_options &
 
7192
                                          OPTION_FOUND_ROWS ?
 
7193
                                          HA_POS_ERROR :
 
7194
                                          join->unit->select_limit_cnt),0,
 
7195
                                          false) < 0)
 
7196
                return(1);                      // Impossible WHERE
 
7197
            }
 
7198
            else
 
7199
              sel->cond=orig_cond;
 
7200
 
 
7201
            /* Fix for EXPLAIN */
 
7202
            if (sel->quick)
 
7203
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7204
          }
 
7205
          else
 
7206
          {
 
7207
            sel->needed_reg=tab->needed_reg;
 
7208
            sel->quick_keys.clear_all();
 
7209
          }
 
7210
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7211
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7212
          {
 
7213
            tab->keys=sel->quick_keys;
 
7214
            tab->keys.merge(sel->needed_reg);
 
7215
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7216
                             (select->quick_keys.is_clear_all() ||
 
7217
                              (select->quick &&
 
7218
                               (select->quick->records >= 100L)))) ?
 
7219
              2 : 1;
 
7220
            sel->read_tables= used_tables & ~current_map;
 
7221
          }
 
7222
          if (i != join->const_tables && tab->use_quick != 2)
 
7223
          {                                     /* Read with cache */
 
7224
            if (cond &&
 
7225
                (tmp=make_cond_for_table(cond,
 
7226
                                         join->const_table_map |
 
7227
                                         current_map,
 
7228
                                         current_map, 0)))
 
7229
            {
 
7230
              tab->cache.select=(SQL_SELECT*)
 
7231
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7232
              tab->cache.select->cond=tmp;
 
7233
              tab->cache.select->read_tables=join->const_table_map;
 
7234
            }
 
7235
          }
 
7236
        }
 
7237
      }
 
7238
 
 
7239
      /*
 
7240
        Push down conditions from all on expressions.
 
7241
        Each of these conditions are guarded by a variable
 
7242
        that turns if off just before null complemented row for
 
7243
        outer joins is formed. Thus, the condition from an
 
7244
        'on expression' are guaranteed not to be checked for
 
7245
        the null complemented row.
 
7246
      */
 
7247
 
 
7248
      /* First push down constant conditions from on expressions */
 
7249
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7250
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7251
      {
 
7252
        if (*join_tab->on_expr_ref)
 
7253
        {
 
7254
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7255
          tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7256
                                   join->const_table_map,
 
7257
                                   (table_map) 0, 0);
 
7258
          if (!tmp)
 
7259
            continue;
 
7260
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7261
          if (!tmp)
 
7262
            return(1);
 
7263
          tmp->quick_fix_field();
 
7264
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7265
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7266
          if (!cond_tab->select_cond)
 
7267
            return(1);
 
7268
          cond_tab->select_cond->quick_fix_field();
 
7269
        }
 
7270
      }
 
7271
 
 
7272
      /* Push down non-constant conditions from on expressions */
 
7273
      JOIN_TAB *last_tab= tab;
 
7274
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7275
      {
 
7276
        /*
 
7277
          Table tab is the last inner table of an outer join.
 
7278
          An on expression is always attached to it.
 
7279
        */
 
7280
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7281
 
 
7282
        table_map used_tables2= (join->const_table_map |
 
7283
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7284
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7285
        {
 
7286
          current_map= tab->table->map;
 
7287
          used_tables2|= current_map;
 
7288
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7289
                                              current_map, 0);
 
7290
          if (tmp_cond)
 
7291
          {
 
7292
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7293
            /*
 
7294
              First add the guards for match variables of
 
7295
              all embedding outer join operations.
 
7296
            */
 
7297
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7298
                                                     tmp_cond,
 
7299
                                                     first_inner_tab)))
 
7300
              return(1);
 
7301
            /*
 
7302
              Now add the guard turning the predicate off for
 
7303
              the null complemented row.
 
7304
            */
 
7305
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7306
                                              &first_inner_tab->
 
7307
                                              not_null_compl);
 
7308
            if (tmp_cond)
 
7309
              tmp_cond->quick_fix_field();
 
7310
            /* Add the predicate to other pushed down predicates */
 
7311
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7312
                                  new Item_cond_and(cond_tab->select_cond,
 
7313
                                                    tmp_cond);
 
7314
            if (!cond_tab->select_cond)
 
7315
              return(1);
 
7316
            cond_tab->select_cond->quick_fix_field();
 
7317
          }
 
7318
        }
 
7319
        first_inner_tab= first_inner_tab->first_upper;
 
7320
      }
 
7321
    }
 
7322
  }
 
7323
  return(0);
 
7324
}
 
7325
 
 
7326
 
 
7327
/*
 
7328
  Check if given expression uses only table fields covered by the given index
 
7329
 
 
7330
  SYNOPSIS
 
7331
    uses_index_fields_only()
 
7332
      item           Expression to check
 
7333
      tbl            The table having the index
 
7334
      keyno          The index number
 
7335
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7336
 
 
7337
  DESCRIPTION
 
7338
    Check if given expression only uses fields covered by index #keyno in the
 
7339
    table tbl. The expression can use any fields in any other tables.
 
7340
 
 
7341
    The expression is guaranteed not to be AND or OR - those constructs are
 
7342
    handled outside of this function.
 
7343
 
 
7344
  RETURN
 
7345
    true   Yes
 
7346
    false  No
 
7347
*/
 
7348
 
 
7349
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
 
7350
                            bool other_tbls_ok)
 
7351
{
 
7352
  if (item->const_item())
 
7353
    return true;
 
7354
 
 
7355
  /*
 
7356
    Don't push down the triggered conditions. Nested outer joins execution
 
7357
    code may need to evaluate a condition several times (both triggered and
 
7358
    untriggered), and there is no way to put thi
 
7359
    TODO: Consider cloning the triggered condition and using the copies for:
 
7360
      1. push the first copy down, to have most restrictive index condition
 
7361
         possible
 
7362
      2. Put the second copy into tab->select_cond.
 
7363
  */
 
7364
  if (item->type() == Item::FUNC_ITEM &&
 
7365
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7366
    return false;
 
7367
 
 
7368
  if (!(item->used_tables() & tbl->map))
 
7369
    return other_tbls_ok;
 
7370
 
 
7371
  Item::Type item_type= item->type();
 
7372
  switch (item_type) {
 
7373
  case Item::FUNC_ITEM:
 
7374
    {
 
7375
      /* This is a function, apply condition recursively to arguments */
 
7376
      Item_func *item_func= (Item_func*)item;
 
7377
      Item **child;
 
7378
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7379
      for (child= item_func->arguments(); child != item_end; child++)
 
7380
      {
 
7381
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7382
          return false;
 
7383
      }
 
7384
      return true;
 
7385
    }
 
7386
  case Item::COND_ITEM:
 
7387
    {
 
7388
      /* This is a function, apply condition recursively to arguments */
 
7389
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7390
      Item *list_item;
 
7391
      while ((list_item=li++))
 
7392
      {
 
7393
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7394
          return false;
 
7395
      }
 
7396
      return true;
 
7397
    }
 
7398
  case Item::FIELD_ITEM:
 
7399
    {
 
7400
      Item_field *item_field= (Item_field*)item;
 
7401
      if (item_field->field->table != tbl)
 
7402
        return true;
 
7403
      return item_field->field->part_of_key.is_set(keyno);
 
7404
    }
 
7405
  case Item::REF_ITEM:
 
7406
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7407
                                  other_tbls_ok);
 
7408
  default:
 
7409
    return false; /* Play it safe, don't push unknown non-const items */
 
7410
  }
 
7411
}
 
7412
 
 
7413
 
1216
7414
#define ICP_COND_USES_INDEX_ONLY 10
1217
7415
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
7416
/*
 
7417
  Get a part of the condition that can be checked using only index fields
 
7418
 
 
7419
  SYNOPSIS
 
7420
    make_cond_for_index()
 
7421
      cond           The source condition
 
7422
      table          The table that is partially available
 
7423
      keyno          The index in the above table. Only fields covered by the index
 
7424
                     are available
 
7425
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7426
 
 
7427
  DESCRIPTION
 
7428
    Get a part of the condition that can be checked when for the given table
 
7429
    we have values only of fields covered by some index. The condition may
 
7430
    refer to other tables, it is assumed that we have values of all of their
 
7431
    fields.
 
7432
 
 
7433
    Example:
 
7434
      make_cond_for_index(
 
7435
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7436
          t2, keyno(t2.key1))
 
7437
      will return
 
7438
        "cond(t1.field) AND cond(t2.key2)"
 
7439
 
 
7440
  RETURN
 
7441
    Index condition, or NULL if no condition could be inferred.
 
7442
*/
 
7443
 
 
7444
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7445
                          bool other_tbls_ok)
 
7446
{
 
7447
  if (!cond)
 
7448
    return NULL;
 
7449
  if (cond->type() == Item::COND_ITEM)
 
7450
  {
 
7451
    uint32_t n_marked= 0;
 
7452
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7453
    {
 
7454
      Item_cond_and *new_cond=new Item_cond_and;
 
7455
      if (!new_cond)
 
7456
        return (COND*) 0;
 
7457
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7458
      Item *item;
 
7459
      while ((item=li++))
 
7460
      {
 
7461
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7462
        if (fix)
 
7463
          new_cond->argument_list()->push_back(fix);
 
7464
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7465
      }
 
7466
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7467
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7468
      switch (new_cond->argument_list()->elements) {
 
7469
      case 0:
 
7470
        return (COND*) 0;
 
7471
      case 1:
 
7472
        return new_cond->argument_list()->head();
 
7473
      default:
 
7474
        new_cond->quick_fix_field();
 
7475
        return new_cond;
 
7476
      }
 
7477
    }
 
7478
    else /* It's OR */
 
7479
    {
 
7480
      Item_cond_or *new_cond=new Item_cond_or;
 
7481
      if (!new_cond)
 
7482
        return (COND*) 0;
 
7483
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7484
      Item *item;
 
7485
      while ((item=li++))
 
7486
      {
 
7487
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7488
        if (!fix)
 
7489
          return (COND*) 0;
 
7490
        new_cond->argument_list()->push_back(fix);
 
7491
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7492
      }
 
7493
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7494
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7495
      new_cond->quick_fix_field();
 
7496
      new_cond->top_level_item();
 
7497
      return new_cond;
 
7498
    }
 
7499
  }
 
7500
 
 
7501
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7502
    return (COND*) 0;
 
7503
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7504
  return cond;
 
7505
}
 
7506
 
 
7507
 
 
7508
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7509
{
 
7510
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7511
    return 0; /* Already checked */
 
7512
 
 
7513
  if (cond->type() == Item::COND_ITEM)
 
7514
  {
 
7515
    table_map tbl_map= 0;
 
7516
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7517
    {
 
7518
      /* Create new top level AND item */
 
7519
      Item_cond_and *new_cond=new Item_cond_and;
 
7520
      if (!new_cond)
 
7521
        return (COND*) 0;
 
7522
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7523
      Item *item;
 
7524
      while ((item=li++))
 
7525
      {
 
7526
        Item *fix= make_cond_remainder(item, exclude_index);
 
7527
        if (fix)
 
7528
        {
 
7529
          new_cond->argument_list()->push_back(fix);
 
7530
          tbl_map |= fix->used_tables();
 
7531
        }
 
7532
      }
 
7533
      switch (new_cond->argument_list()->elements) {
 
7534
      case 0:
 
7535
        return (COND*) 0;
 
7536
      case 1:
 
7537
        return new_cond->argument_list()->head();
 
7538
      default:
 
7539
        new_cond->quick_fix_field();
 
7540
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7541
        return new_cond;
 
7542
      }
 
7543
    }
 
7544
    else /* It's OR */
 
7545
    {
 
7546
      Item_cond_or *new_cond=new Item_cond_or;
 
7547
      if (!new_cond)
 
7548
        return (COND*) 0;
 
7549
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7550
      Item *item;
 
7551
      while ((item=li++))
 
7552
      {
 
7553
        Item *fix= make_cond_remainder(item, false);
 
7554
        if (!fix)
 
7555
          return (COND*) 0;
 
7556
        new_cond->argument_list()->push_back(fix);
 
7557
        tbl_map |= fix->used_tables();
 
7558
      }
 
7559
      new_cond->quick_fix_field();
 
7560
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7561
      new_cond->top_level_item();
 
7562
      return new_cond;
 
7563
    }
 
7564
  }
 
7565
  return cond;
 
7566
}
 
7567
 
 
7568
 
 
7569
/*
 
7570
  Try to extract and push the index condition
 
7571
 
 
7572
  SYNOPSIS
 
7573
    push_index_cond()
 
7574
      tab            A join tab that has tab->table->file and its condition
 
7575
                     in tab->select_cond
 
7576
      keyno          Index for which extract and push the condition
 
7577
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7578
 
 
7579
  DESCRIPTION
 
7580
    Try to extract and push the index condition down to table handler
 
7581
*/
 
7582
 
 
7583
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7584
{
 
7585
  Item *idx_cond;
 
7586
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7587
      tab->join->session->variables.engine_condition_pushdown)
 
7588
  {
 
7589
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7590
                                  other_tbls_ok);
 
7591
 
 
7592
    if (idx_cond)
 
7593
    {
 
7594
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7595
      Item *idx_remainder_cond=
 
7596
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7597
 
 
7598
      /*
 
7599
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7600
        condition.
 
7601
        TODO: This check happens to work on current ICP implementations, but
 
7602
        there may exist a compliant implementation that will not work
 
7603
        correctly with it. Sort this out when we stabilize the condition
 
7604
        pushdown APIs.
 
7605
      */
 
7606
      if (idx_remainder_cond != idx_cond)
 
7607
        tab->ref.disable_cache= true;
 
7608
 
 
7609
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7610
 
 
7611
      if (row_cond)
 
7612
      {
 
7613
        if (!idx_remainder_cond)
 
7614
          tab->select_cond= row_cond;
 
7615
        else
 
7616
        {
 
7617
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7618
          tab->select_cond->quick_fix_field();
 
7619
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
 
7620
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7621
        }
 
7622
      }
 
7623
      else
 
7624
        tab->select_cond= idx_remainder_cond;
 
7625
      if (tab->select)
 
7626
      {
 
7627
        tab->select->cond= tab->select_cond;
 
7628
      }
 
7629
    }
 
7630
  }
 
7631
  return;
 
7632
}
 
7633
 
 
7634
 
 
7635
 
 
7636
    /*
 
7637
      Determine if the set is already ordered for order_st BY, so it can
 
7638
      disable join cache because it will change the ordering of the results.
 
7639
      Code handles sort table that is at any location (not only first after
 
7640
      the const tables) despite the fact that it's currently prohibited.
 
7641
      We must disable join cache if the first non-const table alone is
 
7642
      ordered. If there is a temp table the ordering is done as a last
 
7643
      operation and doesn't prevent join cache usage.
 
7644
    */
 
7645
uint32_t make_join_orderinfo(JOIN *join)
 
7646
{
 
7647
  uint32_t i;
 
7648
  if (join->need_tmp)
 
7649
    return join->tables;
 
7650
 
 
7651
  for (i=join->const_tables ; i < join->tables ; i++)
 
7652
  {
 
7653
    JOIN_TAB *tab=join->join_tab+i;
 
7654
    Table *table=tab->table;
 
7655
    if ((table == join->sort_by_table &&
 
7656
         (!join->order || join->skip_sort_order)) ||
 
7657
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7658
    {
 
7659
      break;
 
7660
    }
 
7661
  }
 
7662
  return i;
 
7663
}
 
7664
 
 
7665
 
 
7666
/*
 
7667
  Plan refinement stage: do various set ups for the executioner
 
7668
 
 
7669
  SYNOPSIS
 
7670
    make_join_readinfo()
 
7671
      join           Join being processed
 
7672
      options        Join's options (checking for SELECT_DESCRIBE,
 
7673
                     SELECT_NO_JOIN_CACHE)
 
7674
      no_jbuf_after  Don't use join buffering after table with this number.
 
7675
 
 
7676
  DESCRIPTION
 
7677
    Plan refinement stage: do various set ups for the executioner
 
7678
      - set up use of join buffering
 
7679
      - push index conditions
 
7680
      - increment counters
 
7681
      - etc
 
7682
 
 
7683
  RETURN
 
7684
    false - OK
 
7685
    true  - Out of memory
 
7686
*/
 
7687
 
 
7688
static bool
 
7689
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7690
{
 
7691
  uint32_t i;
 
7692
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7693
  bool sorted= 1;
 
7694
 
 
7695
  for (i=join->const_tables ; i < join->tables ; i++)
 
7696
  {
 
7697
    JOIN_TAB *tab=join->join_tab+i;
 
7698
    Table *table=tab->table;
 
7699
    bool using_join_cache;
 
7700
    tab->read_record.table= table;
 
7701
    tab->read_record.file=table->file;
 
7702
    tab->next_select=sub_select;                /* normal select */
 
7703
    /*
 
7704
      TODO: don't always instruct first table's ref/range access method to
 
7705
      produce sorted output.
 
7706
    */
 
7707
    tab->sorted= sorted;
 
7708
    sorted= 0;                                  // only first must be sorted
 
7709
    if (tab->insideout_match_tab)
 
7710
    {
 
7711
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
 
7712
                                                         [tab->index].
 
7713
                                                         key_length)))
 
7714
        return true;
 
7715
    }
 
7716
    switch (tab->type) {
 
7717
    case JT_SYSTEM:                             // Only happens with left join
 
7718
      table->status=STATUS_NO_RECORD;
 
7719
      tab->read_first_record= join_read_system;
 
7720
      tab->read_record.read_record= join_no_more_records;
 
7721
      break;
 
7722
    case JT_CONST:                              // Only happens with left join
 
7723
      table->status=STATUS_NO_RECORD;
 
7724
      tab->read_first_record= join_read_const;
 
7725
      tab->read_record.read_record= join_no_more_records;
 
7726
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7727
          !table->no_keyread)
 
7728
      {
 
7729
        table->key_read=1;
 
7730
        table->file->extra(HA_EXTRA_KEYREAD);
 
7731
      }
 
7732
      break;
 
7733
    case JT_EQ_REF:
 
7734
      table->status=STATUS_NO_RECORD;
 
7735
      if (tab->select)
 
7736
      {
 
7737
        delete tab->select->quick;
 
7738
        tab->select->quick=0;
 
7739
      }
 
7740
      delete tab->quick;
 
7741
      tab->quick=0;
 
7742
      tab->read_first_record= join_read_key;
 
7743
      tab->read_record.read_record= join_no_more_records;
 
7744
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7745
          !table->no_keyread)
 
7746
      {
 
7747
        table->key_read=1;
 
7748
        table->file->extra(HA_EXTRA_KEYREAD);
 
7749
      }
 
7750
      else
 
7751
        push_index_cond(tab, tab->ref.key, true);
 
7752
      break;
 
7753
    case JT_REF_OR_NULL:
 
7754
    case JT_REF:
 
7755
      table->status=STATUS_NO_RECORD;
 
7756
      if (tab->select)
 
7757
      {
 
7758
        delete tab->select->quick;
 
7759
        tab->select->quick=0;
 
7760
      }
 
7761
      delete tab->quick;
 
7762
      tab->quick=0;
 
7763
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7764
          !table->no_keyread)
 
7765
      {
 
7766
        table->key_read=1;
 
7767
        table->file->extra(HA_EXTRA_KEYREAD);
 
7768
      }
 
7769
      else
 
7770
        push_index_cond(tab, tab->ref.key, true);
 
7771
      if (tab->type == JT_REF)
 
7772
      {
 
7773
        tab->read_first_record= join_read_always_key;
 
7774
        tab->read_record.read_record= tab->insideout_match_tab?
 
7775
           join_read_next_same_diff : join_read_next_same;
 
7776
      }
 
7777
      else
 
7778
      {
 
7779
        tab->read_first_record= join_read_always_key_or_null;
 
7780
        tab->read_record.read_record= join_read_next_same_or_null;
 
7781
      }
 
7782
      break;
 
7783
    case JT_ALL:
 
7784
      /*
 
7785
        If previous table use cache
 
7786
        If the incoming data set is already sorted don't use cache.
 
7787
      */
 
7788
      table->status=STATUS_NO_RECORD;
 
7789
      using_join_cache= false;
 
7790
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7791
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7792
          !tab->insideout_match_tab)
 
7793
      {
 
7794
        if ((options & SELECT_DESCRIBE) ||
 
7795
            !join_init_cache(join->session,join->join_tab+join->const_tables,
 
7796
                             i-join->const_tables))
 
7797
        {
 
7798
          using_join_cache= true;
 
7799
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7800
        }
 
7801
      }
 
7802
      /* These init changes read_record */
 
7803
      if (tab->use_quick == 2)
 
7804
      {
 
7805
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7806
        tab->read_first_record= join_init_quick_read_record;
 
7807
        if (statistics)
 
7808
          status_var_increment(join->session->status_var.select_range_check_count);
 
7809
      }
 
7810
      else
 
7811
      {
 
7812
        tab->read_first_record= join_init_read_record;
 
7813
        if (i == join->const_tables)
 
7814
        {
 
7815
          if (tab->select && tab->select->quick)
 
7816
          {
 
7817
            if (statistics)
 
7818
              status_var_increment(join->session->status_var.select_range_count);
 
7819
          }
 
7820
          else
 
7821
          {
 
7822
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7823
            if (statistics)
 
7824
              status_var_increment(join->session->status_var.select_scan_count);
 
7825
          }
 
7826
        }
 
7827
        else
 
7828
        {
 
7829
          if (tab->select && tab->select->quick)
 
7830
          {
 
7831
            if (statistics)
 
7832
              status_var_increment(join->session->status_var.select_full_range_join_count);
 
7833
          }
 
7834
          else
 
7835
          {
 
7836
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7837
            if (statistics)
 
7838
              status_var_increment(join->session->status_var.select_full_join_count);
 
7839
          }
 
7840
        }
 
7841
        if (!table->no_keyread)
 
7842
        {
 
7843
          if (tab->select && tab->select->quick &&
 
7844
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7845
              table->covering_keys.is_set(tab->select->quick->index))
 
7846
          {
 
7847
            table->key_read=1;
 
7848
            table->file->extra(HA_EXTRA_KEYREAD);
 
7849
          }
 
7850
          else if (!table->covering_keys.is_clear_all() &&
 
7851
                   !(tab->select && tab->select->quick))
 
7852
          {                                     // Only read index tree
 
7853
            if (!tab->insideout_match_tab)
 
7854
            {
 
7855
              /*
 
7856
                See bug #26447: "Using the clustered index for a table scan
 
7857
                is always faster than using a secondary index".
 
7858
              */
 
7859
              if (table->s->primary_key != MAX_KEY &&
 
7860
                  table->file->primary_key_is_clustered())
 
7861
                tab->index= table->s->primary_key;
 
7862
              else
 
7863
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7864
            }
 
7865
            tab->read_first_record= join_read_first;
 
7866
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7867
          }
 
7868
        }
 
7869
        if (tab->select && tab->select->quick &&
 
7870
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7871
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7872
      }
 
7873
      break;
 
7874
    default:
 
7875
      break;                                    /* purecov: deadcode */
 
7876
    case JT_UNKNOWN:
 
7877
    case JT_MAYBE_REF:
 
7878
      abort();                                  /* purecov: deadcode */
 
7879
    }
 
7880
  }
 
7881
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7882
  return(false);
 
7883
}
 
7884
 
 
7885
 
 
7886
/**
 
7887
  Give error if we some tables are done with a full join.
 
7888
 
 
7889
  This is used by multi_table_update and multi_table_delete when running
 
7890
  in safe mode.
 
7891
 
 
7892
  @param join           Join condition
 
7893
 
 
7894
  @retval
 
7895
    0   ok
 
7896
  @retval
 
7897
    1   Error (full join used)
 
7898
*/
 
7899
 
 
7900
bool error_if_full_join(JOIN *join)
 
7901
{
 
7902
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7903
       tab < end;
 
7904
       tab++)
 
7905
  {
 
7906
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7907
    {
 
7908
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7909
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7910
      return(1);
 
7911
    }
 
7912
  }
 
7913
  return(0);
 
7914
}
 
7915
 
 
7916
 
 
7917
/**
 
7918
  cleanup JOIN_TAB.
 
7919
*/
 
7920
 
 
7921
void JOIN_TAB::cleanup()
 
7922
{
 
7923
  delete select;
 
7924
  select= 0;
 
7925
  delete quick;
 
7926
  quick= 0;
1227
7927
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
7928
    free(cache.buff);
1232
 
  }
1233
7929
  cache.buff= 0;
1234
7930
  limit= 0;
1235
7931
  if (table)
1237
7933
    if (table->key_read)
1238
7934
    {
1239
7935
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7936
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7937
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7938
    table->file->ha_index_or_rnd_end();
1243
7939
    /*
1244
7940
      We need to reset this for next select
1245
7941
      (Tested in part_of_refkey)
1246
7942
    */
1247
7943
    table->reginfo.join_tab= 0;
1248
7944
  }
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:
 
7945
  end_read_record(&read_record);
 
7946
}
 
7947
 
 
7948
 
 
7949
/**
 
7950
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7951
  (table cursors), free quick selects.
 
7952
 
 
7953
    This function is called in the end of execution of a JOIN, before the used
 
7954
    tables are unlocked and closed.
 
7955
 
 
7956
    For a join that is resolved using a temporary table, the first sweep is
 
7957
    performed against actual tables and an intermediate result is inserted
 
7958
    into the temprorary table.
 
7959
    The last sweep is performed against the temporary table. Therefore,
 
7960
    the base tables and associated buffers used to fill the temporary table
 
7961
    are no longer needed, and this function is called to free them.
 
7962
 
 
7963
    For a join that is performed without a temporary table, this function
 
7964
    is called after all rows are sent, but before EOF packet is sent.
 
7965
 
 
7966
    For a simple SELECT with no subqueries this function performs a full
 
7967
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7968
    tables.
 
7969
 
 
7970
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7971
    do the full cleanup and need to do a partial cleanup only.
 
7972
    - If a JOIN is not the top level join, we must not unlock the tables
 
7973
    because the outer select may not have been evaluated yet, and we
 
7974
    can't unlock only selected tables of a query.
 
7975
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7976
    should not free quick selects and join buffers because they will be
 
7977
    needed for the next execution of the correlated subquery.
 
7978
    - However, if this is a JOIN for a [sub]select, which is not
 
7979
    a correlated subquery itself, but has subqueries, we can free it
 
7980
    fully and also free JOINs of all its subqueries. The exception
 
7981
    is a subquery in SELECT list, e.g: @n
 
7982
    SELECT a, (select cmax(b) from t1) group by c @n
 
7983
    This subquery will not be evaluated at first sweep and its value will
 
7984
    not be inserted into the temporary table. Instead, it's evaluated
 
7985
    when selecting from the temporary table. Therefore, it can't be freed
 
7986
    here even though it's not correlated.
 
7987
 
 
7988
  @todo
 
7989
    Unlock tables even if the join isn't top level select in the tree
 
7990
*/
 
7991
 
 
7992
void JOIN::join_free()
 
7993
{
 
7994
  Select_Lex_Unit *tmp_unit;
 
7995
  Select_Lex *sl;
 
7996
  /*
 
7997
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7998
    free all tables.
 
7999
  */
 
8000
  bool full= (!select_lex->uncacheable && !session->lex->describe);
 
8001
  bool can_unlock= full;
 
8002
 
 
8003
  cleanup(full);
 
8004
 
 
8005
  for (tmp_unit= select_lex->first_inner_unit();
 
8006
       tmp_unit;
 
8007
       tmp_unit= tmp_unit->next_unit())
 
8008
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
8009
    {
 
8010
      Item_subselect *subselect= sl->master_unit()->item;
 
8011
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
8012
      /*
 
8013
        If this join is evaluated, we can fully clean it up and clean up all
 
8014
        its underlying joins even if they are correlated -- they will not be
 
8015
        used any more anyway.
 
8016
        If this join is not yet evaluated, we still must clean it up to
 
8017
        close its table cursors -- it may never get evaluated, as in case of
 
8018
        ... HAVING false OR a IN (SELECT ...))
 
8019
        but all table cursors must be closed before the unlock.
 
8020
      */
 
8021
      sl->cleanup_all_joins(full_local);
 
8022
      /* Can't unlock if at least one JOIN is still needed */
 
8023
      can_unlock= can_unlock && full_local;
 
8024
    }
 
8025
 
 
8026
  /*
 
8027
    We are not using tables anymore
 
8028
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8029
  */
 
8030
  if (can_unlock && lock && session->lock &&
 
8031
      !(select_options & SELECT_NO_UNLOCK) &&
 
8032
      !select_lex->subquery_in_having &&
 
8033
      (select_lex == (session->lex->unit.fake_select_lex ?
 
8034
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
8035
  {
 
8036
    /*
 
8037
      TODO: unlock tables even if the join isn't top level select in the
 
8038
      tree.
 
8039
    */
 
8040
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
 
8041
    lock= 0;
 
8042
  }
 
8043
 
 
8044
  return;
 
8045
}
 
8046
 
 
8047
 
 
8048
/**
 
8049
  Free resources of given join.
 
8050
 
 
8051
  @param fill   true if we should free all resources, call with full==1
 
8052
                should be last, before it this function can be called with
 
8053
                full==0
 
8054
 
 
8055
  @note
 
8056
    With subquery this function definitely will be called several times,
 
8057
    but even for simple query it can be called several times.
 
8058
*/
 
8059
 
 
8060
void JOIN::cleanup(bool full)
 
8061
{
 
8062
  if (table)
 
8063
  {
 
8064
    JOIN_TAB *tab,*end;
 
8065
    /*
 
8066
      Only a sorted table may be cached.  This sorted table is always the
 
8067
      first non const table in join->table
 
8068
    */
 
8069
    if (tables > const_tables) // Test for not-const tables
 
8070
    {
 
8071
      free_io_cache(table[const_tables]);
 
8072
      filesort_free_buffers(table[const_tables],full);
 
8073
    }
 
8074
 
 
8075
    if (full)
 
8076
    {
 
8077
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8078
        tab->cleanup();
 
8079
      table= 0;
 
8080
    }
 
8081
    else
 
8082
    {
 
8083
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8084
      {
 
8085
        if (tab->table)
 
8086
          tab->table->file->ha_index_or_rnd_end();
 
8087
      }
 
8088
    }
 
8089
    cleanup_sj_tmp_tables(this);//
 
8090
  }
 
8091
  /*
 
8092
    We are not using tables anymore
 
8093
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8094
  */
 
8095
  if (full)
 
8096
  {
 
8097
    if (tmp_join)
 
8098
      tmp_table_param.copy_field= 0;
 
8099
    group_fields.delete_elements();
 
8100
    /*
 
8101
      We can't call delete_elements() on copy_funcs as this will cause
 
8102
      problems in free_elements() as some of the elements are then deleted.
 
8103
    */
 
8104
    tmp_table_param.copy_funcs.empty();
 
8105
    /*
 
8106
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8107
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8108
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8109
      be removed in tmp_table_param.cleanup().
 
8110
    */
 
8111
    if (tmp_join &&
 
8112
        tmp_join != this &&
 
8113
        tmp_join->tmp_table_param.copy_field ==
 
8114
        tmp_table_param.copy_field)
 
8115
    {
 
8116
      tmp_join->tmp_table_param.copy_field=
 
8117
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8118
    }
 
8119
    tmp_table_param.cleanup();
 
8120
  }
 
8121
  return;
 
8122
}
 
8123
 
 
8124
 
 
8125
/**
 
8126
  Remove the following expressions from order_st BY and GROUP BY:
1264
8127
  Constant expressions @n
1265
8128
  Expression that only uses tables that are of type EQ_REF and the reference
1266
8129
  is in the order_st list or if all refereed tables are of the above type.
1267
8130
 
1268
8131
  In the following, the X field can be removed:
1269
8132
  @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
 
8133
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8134
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1272
8135
  @endcode
1273
8136
 
1274
8137
  These can't be optimized:
1275
8138
  @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
 
8139
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8140
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8141
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1279
8142
  @endcode
1280
8143
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8144
 
 
8145
static bool
 
8146
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1282
8147
{
1283
8148
  if (tab->cached_eq_ref_table)                 // If cached
1284
8149
    return tab->eq_ref_table;
1285
8150
  tab->cached_eq_ref_table=1;
1286
8151
  /* 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)
 
8152
  if (tab->type == JT_CONST && !tab->first_inner)
 
8153
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8154
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8155
    return (tab->eq_ref_table=0);               // We must use this
1291
8156
  Item **ref_item=tab->ref.items;
1292
8157
  Item **end=ref_item+tab->ref.key_parts;
1297
8162
  {
1298
8163
    if (! (*ref_item)->const_item())
1299
8164
    {                                           // Not a const ref
1300
 
      Order *order;
 
8165
      order_st *order;
1301
8166
      for (order=start_order ; order ; order=order->next)
1302
8167
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8168
        if ((*ref_item)->eq(order->item[0],0))
 
8169
          break;
1305
8170
      }
1306
8171
      if (order)
1307
8172
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8173
        found++;
 
8174
        assert(!(order->used & map));
 
8175
        order->used|=map;
 
8176
        continue;                               // Used in order_st BY
1312
8177
      }
1313
8178
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8179
        return (tab->eq_ref_table=0);
1315
8180
    }
1316
8181
  }
1317
8182
  /* Check that there was no reference to table before sort order */
1323
8188
      continue;
1324
8189
    }
1325
8190
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8191
      return (tab->eq_ref_table=0);
 
8192
  }
 
8193
  return tab->eq_ref_table=1;
 
8194
}
 
8195
 
 
8196
 
 
8197
static bool
 
8198
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8199
{
 
8200
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8201
  {
 
8202
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8203
      return 0;
 
8204
  }
 
8205
  return 1;
 
8206
}
 
8207
 
 
8208
 
 
8209
/** Update the dependency map for the tables. */
 
8210
 
 
8211
static void update_depend_map(JOIN *join)
 
8212
{
 
8213
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8214
 
 
8215
  for (; join_tab != end ; join_tab++)
 
8216
  {
 
8217
    TABLE_REF *ref= &join_tab->ref;
 
8218
    table_map depend_map=0;
 
8219
    Item **item=ref->items;
 
8220
    uint32_t i;
 
8221
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8222
      depend_map|=(*item)->used_tables();
 
8223
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8224
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8225
    for (JOIN_TAB **tab=join->map2table;
 
8226
         depend_map ;
 
8227
         tab++,depend_map>>=1 )
 
8228
    {
 
8229
      if (depend_map & 1)
 
8230
        ref->depend_map|=(*tab)->ref.depend_map;
 
8231
    }
 
8232
  }
 
8233
}
 
8234
 
 
8235
 
 
8236
/** Update the dependency map for the sort order. */
 
8237
 
 
8238
static void update_depend_map(JOIN *join, order_st *order)
 
8239
{
 
8240
  for (; order ; order=order->next)
 
8241
  {
 
8242
    table_map depend_map;
 
8243
    order->item[0]->update_used_tables();
 
8244
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8245
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8246
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8247
        && !order->item[0]->with_sum_func)
 
8248
    {
 
8249
      for (JOIN_TAB **tab=join->map2table;
 
8250
           depend_map ;
 
8251
           tab++, depend_map>>=1)
 
8252
      {
 
8253
        if (depend_map & 1)
 
8254
          order->depend_map|=(*tab)->ref.depend_map;
 
8255
      }
 
8256
    }
 
8257
  }
 
8258
}
 
8259
 
 
8260
 
 
8261
/**
 
8262
  Remove all constants and check if order_st only contains simple
 
8263
  expressions.
 
8264
 
 
8265
  simple_order is set to 1 if sort_order only uses fields from head table
 
8266
  and the head table is not a LEFT JOIN table.
 
8267
 
 
8268
  @param join                   Join handler
 
8269
  @param first_order            List of SORT or GROUP order
 
8270
  @param cond                   WHERE statement
 
8271
  @param change_list            Set to 1 if we should remove things from list.
 
8272
                               If this is not set, then only simple_order is
 
8273
                               calculated.
 
8274
  @param simple_order           Set to 1 if we are only using simple expressions
 
8275
 
 
8276
  @return
 
8277
    Returns new sort order
 
8278
*/
 
8279
 
 
8280
static order_st *
 
8281
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8282
             bool change_list, bool *simple_order)
 
8283
{
 
8284
  if (join->tables == join->const_tables)
 
8285
    return change_list ? 0 : first_order;               // No need to sort
 
8286
 
 
8287
  order_st *order,**prev_ptr;
 
8288
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8289
  table_map not_const_tables= ~join->const_table_map;
 
8290
  table_map ref;
 
8291
 
 
8292
  prev_ptr= &first_order;
 
8293
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8294
 
 
8295
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8296
 
 
8297
  update_depend_map(join, first_order);
 
8298
  for (order=first_order; order ; order=order->next)
 
8299
  {
 
8300
    table_map order_tables=order->item[0]->used_tables();
 
8301
    if (order->item[0]->with_sum_func)
 
8302
      *simple_order=0;                          // Must do a temp table to sort
 
8303
    else if (!(order_tables & not_const_tables))
 
8304
    {
 
8305
      if (order->item[0]->with_subselect)
 
8306
        order->item[0]->val_str(&order->item[0]->str_value);
 
8307
      continue;                                 // skip const item
 
8308
    }
 
8309
    else
 
8310
    {
 
8311
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8312
        *simple_order=0;
 
8313
      else
 
8314
      {
 
8315
        Item *comp_item=0;
 
8316
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8317
        {
 
8318
          continue;
 
8319
        }
 
8320
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8321
        {
 
8322
          if (!(order_tables & first_table) &&
 
8323
              only_eq_ref_tables(join,first_order, ref))
 
8324
          {
 
8325
            continue;
 
8326
          }
 
8327
          *simple_order=0;                      // Must do a temp table to sort
 
8328
        }
 
8329
      }
 
8330
    }
 
8331
    if (change_list)
 
8332
      *prev_ptr= order;                         // use this entry
 
8333
    prev_ptr= &order->next;
 
8334
  }
 
8335
  if (change_list)
 
8336
    *prev_ptr=0;
 
8337
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8338
    *simple_order=1;
 
8339
  return(first_order);
 
8340
}
 
8341
 
 
8342
 
 
8343
static int
 
8344
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8345
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8346
                 const char *info, Item *having)
 
8347
{
 
8348
  if (select_options & SELECT_DESCRIBE)
 
8349
  {
 
8350
    select_describe(join, false, false, false, info);
 
8351
    return(0);
 
8352
  }
 
8353
 
 
8354
  join->join_free();
 
8355
 
 
8356
  if (send_row)
 
8357
  {
 
8358
    for (TableList *table= tables; table; table= table->next_leaf)
 
8359
      mark_as_null_row(table->table);           // All fields are NULL
 
8360
    if (having && having->val_int() == 0)
 
8361
      send_row=0;
 
8362
  }
 
8363
  if (!(result->send_fields(fields,
 
8364
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8365
  {
 
8366
    if (send_row)
 
8367
    {
 
8368
      List_iterator_fast<Item> it(fields);
 
8369
      Item *item;
 
8370
      while ((item= it++))
 
8371
        item->no_rows_in_result();
 
8372
      result->send_data(fields);
 
8373
    }
 
8374
    result->send_eof();                         // Should be safe
 
8375
  }
 
8376
  /* Update results for FOUND_ROWS */
 
8377
  join->session->limit_found_rows= join->session->examined_row_count= 0;
 
8378
  return(0);
 
8379
}
 
8380
 
 
8381
/*
 
8382
  used only in JOIN::clear
 
8383
*/
 
8384
static void clear_tables(JOIN *join)
 
8385
{
 
8386
  /*
 
8387
    must clear only the non-const tables, as const tables
 
8388
    are not re-calculated.
 
8389
  */
 
8390
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8391
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8392
}
 
8393
 
 
8394
/*****************************************************************************
 
8395
  Make som simple condition optimization:
 
8396
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8397
  Remove all dummy tests 'item = item', 'const op const'.
 
8398
  Remove all 'item is NULL', when item can never be null!
 
8399
  item->marker should be 0 for all items on entry
 
8400
  Return in cond_value false if condition is impossible (1 = 2)
 
8401
*****************************************************************************/
 
8402
 
 
8403
class COND_CMP :public ilink {
 
8404
public:
 
8405
  static void *operator new(size_t size)
 
8406
  {
 
8407
    return (void*) sql_alloc((uint32_t) size);
 
8408
  }
 
8409
  static void operator delete(void *, size_t)
 
8410
  { TRASH(ptr, size); }
 
8411
 
 
8412
  Item *and_level;
 
8413
  Item_func *cmp_func;
 
8414
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8415
};
 
8416
 
 
8417
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8418
template class I_List<COND_CMP>;
 
8419
template class I_List_iterator<COND_CMP>;
 
8420
#endif
 
8421
 
1330
8422
 
1331
8423
/**
1332
8424
  Find the multiple equality predicate containing a field.
1346
8438
    - Item_equal for the found multiple equality predicate if a success;
1347
8439
    - NULL otherwise.
1348
8440
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8441
 
 
8442
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8443
                            bool *inherited_fl)
1350
8444
{
1351
8445
  Item_equal *item= 0;
1352
8446
  bool in_upper_level= false;
1353
8447
  while (cond_equal)
1354
8448
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8449
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8450
    while ((item= li++))
1357
8451
    {
1358
8452
      if (item->contains(field))
1367
8461
  return item;
1368
8462
}
1369
8463
 
 
8464
 
1370
8465
/**
1371
8466
  Check whether an equality can be used to build multiple equalities.
1372
8467
 
1448
8543
  @retval
1449
8544
    false   otherwise
1450
8545
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8546
 
 
8547
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8548
                                  Item *item, COND_EQUAL *cond_equal)
1455
8549
{
1456
8550
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8551
      right_item->type() == Item::FIELD_ITEM &&
1517
8611
        /* Merge two multiple equalities forming a new one */
1518
8612
        left_item_equal->merge(right_item_equal);
1519
8613
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8614
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8615
        while ((li++) != right_item_equal) {};
1522
8616
        li.remove();
1523
8617
      }
1612
8706
  return false;
1613
8707
}
1614
8708
 
 
8709
 
1615
8710
/**
1616
8711
  Convert row equalities into a conjunction of regular equalities.
1617
8712
 
1637
8732
  @retval
1638
8733
    false   otherwise
1639
8734
*/
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)
 
8735
 
 
8736
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
 
8737
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
1645
8738
{
1646
8739
  uint32_t n= left_row->cols();
1647
8740
  for (uint32_t i= 0 ; i < n; i++)
1657
8750
                                       (Item_row *) right_item,
1658
8751
                                       cond_equal, eq_list);
1659
8752
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8753
        session->lex->current_select->cond_count++;
1661
8754
    }
1662
8755
    else
1663
8756
    {
1664
8757
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
 
8758
      session->lex->current_select->cond_count++;
1666
8759
    }
1667
8760
 
1668
8761
    if (!is_converted)
1678
8771
  return true;
1679
8772
}
1680
8773
 
 
8774
 
1681
8775
/**
1682
8776
  Eliminate row equalities and form multiple equalities predicates.
1683
8777
 
1707
8801
           or, if the equality is neither a simple one nor a row equality,
1708
8802
           or, if the procedure fails by a fatal error.
1709
8803
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8804
 
 
8805
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
 
8806
                           List<Item> *eq_list)
1711
8807
{
1712
8808
  if (item->type() == Item::FUNC_ITEM &&
1713
8809
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8814
    if (left_item->type() == Item::ROW_ITEM &&
1719
8815
        right_item->type() == Item::ROW_ITEM)
1720
8816
    {
1721
 
      session->getLex()->current_select->cond_count--;
 
8817
      session->lex->current_select->cond_count--;
1722
8818
      return check_row_equality(session,
1723
8819
                                (Item_row *) left_item,
1724
8820
                                (Item_row *) right_item,
1730
8826
  return false;
1731
8827
}
1732
8828
 
 
8829
 
1733
8830
/**
1734
8831
  Replace all equality predicates in a condition by multiple equality items.
1735
8832
 
1749
8846
    just an argument of a comparison predicate.
1750
8847
    The function also determines the maximum number of members in
1751
8848
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8849
    session->lex->current_select->max_equal_elems.
1753
8850
 
1754
8851
  @note
1755
8852
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1793
8890
  @return
1794
8891
    pointer to the transformed condition
1795
8892
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8893
 
 
8894
static COND *build_equal_items_for_cond(Session *session, COND *cond,
 
8895
                                        COND_EQUAL *inherited)
1797
8896
{
1798
8897
  Item_equal *item_equal;
1799
8898
  COND_EQUAL cond_equal;
1806
8905
      Item_func::COND_AND_FUNC;
1807
8906
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
8907
 
1809
 
    List<Item>::iterator li(args->begin());
 
8908
    List_iterator<Item> li(*args);
1810
8909
    Item *item;
1811
8910
 
1812
8911
    if (and_level)
1828
8927
          li.remove();
1829
8928
      }
1830
8929
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8930
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8931
      while ((item_equal= it++))
1833
8932
      {
1834
8933
        item_equal->fix_length_and_dec();
1835
8934
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8935
        set_if_bigger(session->lex->current_select->max_equal_elems,
1837
8936
                      item_equal->members());
1838
8937
      }
1839
8938
 
1844
8943
       Make replacement of equality predicates for lower levels
1845
8944
       of the condition expression.
1846
8945
    */
1847
 
    li= args->begin();
 
8946
    li.rewind();
1848
8947
    while ((item= li++))
1849
8948
    {
1850
8949
      Item *new_item;
1889
8988
        {
1890
8989
          item_equal->fix_length_and_dec();
1891
8990
          item_equal->update_used_tables();
1892
 
        }
 
8991
        }
1893
8992
        else
1894
8993
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8994
        set_if_bigger(session->lex->current_select->max_equal_elems,
1896
8995
                      item_equal->members());
1897
8996
        return item_equal;
1898
8997
      }
1901
9000
        /*
1902
9001
          Here a new AND level must be created. It can happen only
1903
9002
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
9003
        */
1905
9004
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
9005
        and_cond->quick_fix_field();
1907
9006
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
9007
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
9008
        while ((item_equal= it++))
1910
9009
        {
1911
9010
          item_equal->fix_length_and_dec();
1912
9011
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
9012
          set_if_bigger(session->lex->current_select->max_equal_elems,
1914
9013
                        item_equal->members());
1915
9014
        }
1916
9015
        and_cond->cond_equal= cond_equal;
1935
9034
  return cond;
1936
9035
}
1937
9036
 
 
9037
 
1938
9038
/**
1939
9039
  Build multiple equalities for a condition and all on expressions that
1940
9040
  inherit these multiple equalities.
1989
9089
    can get more freedom in performing join operations.
1990
9090
    Althogh we don't use this property now, it probably makes sense to use
1991
9091
    it in the future.
1992
 
  @param session                      Thread Cursor
 
9092
  @param session                      Thread handler
1993
9093
  @param cond                condition to build the multiple equalities for
1994
9094
  @param inherited           path to all inherited multiple equality items
1995
9095
  @param join_list           list of join tables to which the condition
2000
9100
  @return
2001
9101
    pointer to the transformed condition containing multiple equalities
2002
9102
*/
 
9103
 
2003
9104
static COND *build_equal_items(Session *session, COND *cond,
2004
9105
                               COND_EQUAL *inherited,
2005
9106
                               List<TableList> *join_list,
2031
9132
  if (join_list)
2032
9133
  {
2033
9134
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9135
    List_iterator<TableList> li(*join_list);
2035
9136
 
2036
9137
    while ((table= li++))
2037
9138
    {
2038
9139
      if (table->on_expr)
2039
9140
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9141
        List<TableList> *nested_join_list= table->nested_join ?
 
9142
          &table->nested_join->join_list : NULL;
2042
9143
        /*
2043
9144
          We can modify table->on_expr because its old value will
2044
9145
          be restored before re-execution of PS/SP.
2053
9154
  return cond;
2054
9155
}
2055
9156
 
 
9157
 
2056
9158
/**
2057
9159
  Compare field items by table order in the execution plan.
2058
9160
 
2072
9174
  @retval
2073
9175
    0  otherwise
2074
9176
*/
 
9177
 
2075
9178
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9179
                                  Item_field *field2,
 
9180
                                  void *table_join_idx)
2078
9181
{
2079
9182
  int cmp= 0;
2080
9183
  bool outer_ref= 0;
2090
9193
  }
2091
9194
  if (outer_ref)
2092
9195
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9196
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9197
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9198
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9199
}
2097
9200
 
 
9201
 
2098
9202
/**
2099
9203
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9204
 
2134
9238
    a pointer to the simple generated equality, if success.
2135
9239
    - 0, otherwise.
2136
9240
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9241
 
 
9242
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9243
                                  Item_equal *item_equal)
2138
9244
{
2139
9245
  List<Item> eq_list;
2140
9246
  Item_func_eq *eq_item= 0;
2204
9310
  return cond;
2205
9311
}
2206
9312
 
 
9313
 
2207
9314
/**
2208
9315
  Substitute every field reference in a condition by the best equal field
2209
9316
  and eliminate all multiple equality predicates.
2231
9338
  @return
2232
9339
    The transformed condition
2233
9340
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9341
 
 
9342
static COND* substitute_for_best_equal_field(COND *cond,
 
9343
                                             COND_EQUAL *cond_equal,
 
9344
                                             void *table_join_idx)
2235
9345
{
2236
9346
  Item_equal *item_equal;
2237
9347
 
2246
9356
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9357
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9358
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9359
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2250
9360
      while ((item_equal= it++))
2251
9361
      {
2252
9362
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9363
      }
2254
9364
    }
2255
9365
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9366
    List_iterator<Item> li(*cond_list);
2257
9367
    Item *item;
2258
9368
    while ((item= li++))
2259
9369
    {
2269
9379
 
2270
9380
    if (and_level)
2271
9381
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9382
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9383
      while ((item_equal= it++))
2274
9384
      {
2275
9385
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2299
9409
  return cond;
2300
9410
}
2301
9411
 
 
9412
 
2302
9413
/**
2303
9414
  Check appearance of new constant items in multiple equalities
2304
9415
  of a condition after reading a constant table.
2311
9422
  @param cond       condition whose multiple equalities are to be checked
2312
9423
  @param table      constant table that has been read
2313
9424
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9425
 
 
9426
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9427
{
2316
9428
  if (!(cond->used_tables() & tab->table->map))
2317
9429
    return;
2319
9431
  if (cond->type() == Item::COND_ITEM)
2320
9432
  {
2321
9433
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9434
    List_iterator_fast<Item> li(*cond_list);
2323
9435
    Item *item;
2324
9436
    while ((item= li++))
2325
9437
      update_const_equal_items(item, tab);
2338
9450
      while ((item_field= it++))
2339
9451
      {
2340
9452
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9453
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9454
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9455
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9456
        stat[0].const_keys.merge(possible_keys);
2345
9457
 
2346
9458
        /*
2347
9459
          For each field in the multiple equality (for which we know that it
2348
9460
          is a constant) we have to find its corresponding key part, and set
2349
9461
          that key part in const_key_parts.
2350
9462
        */
2351
 
        if (possible_keys.any())
 
9463
        if (!possible_keys.is_clear_all())
2352
9464
        {
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 ==
 
9465
          Table *field_tab= field->table;
 
9466
          KEYUSE *use;
 
9467
          for (use= stat->keyuse; use && use->table == field_tab; use++)
 
9468
            if (possible_keys.is_set(use->key) &&
 
9469
                field_tab->key_info[use->key].key_part[use->keypart].field ==
2358
9470
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9471
              field_tab->const_key_parts[use->key]|= use->keypart_map;
2360
9472
        }
2361
9473
      }
2362
9474
    }
2363
9475
  }
2364
9476
}
2365
9477
 
 
9478
 
2366
9479
/*
2367
9480
  change field = field to field = const for each found field = const in the
2368
9481
  and_level
2369
9482
*/
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)
 
9483
 
 
9484
static void
 
9485
change_cond_ref_to_const(Session *session, I_List<COND_CMP> *save_list,
 
9486
                         Item *and_father, Item *cond,
 
9487
                         Item *field, Item *value)
2376
9488
{
2377
9489
  if (cond->type() == Item::COND_ITEM)
2378
9490
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9491
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9492
      Item_func::COND_AND_FUNC;
 
9493
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9494
    Item *item;
2382
9495
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9496
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
 
9497
                               field, value);
2385
9498
    return;
2386
9499
  }
2387
9500
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9513
       left_item->collation.collation == value->collation.collation))
2401
9514
  {
2402
9515
    Item *tmp=value->clone_item();
 
9516
    tmp->collation.set(right_item->collation);
 
9517
 
2403
9518
    if (tmp)
2404
9519
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
9520
      session->change_item_tree(args + 1, tmp);
2407
9521
      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())
 
9522
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9523
          && and_father != cond && !left_item->const_item())
2411
9524
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9525
        cond->marker=1;
 
9526
        COND_CMP *tmp2;
 
9527
        if ((tmp2=new COND_CMP(and_father,func)))
 
9528
          save_list->push_back(tmp2);
2414
9529
      }
2415
9530
      func->set_cmp_func();
2416
9531
    }
2422
9537
            right_item->collation.collation == value->collation.collation))
2423
9538
  {
2424
9539
    Item *tmp= value->clone_item();
 
9540
    tmp->collation.set(left_item->collation);
 
9541
 
2425
9542
    if (tmp)
2426
9543
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
9544
      session->change_item_tree(args, tmp);
2429
9545
      value= tmp;
2430
9546
      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())
 
9547
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9548
          && and_father != cond && !right_item->const_item())
2434
9549
      {
2435
9550
        args[0]= args[1];                       // For easy check
2436
9551
        session->change_item_tree(args + 1, value);
2437
 
        cond->marker=1;
2438
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9552
        cond->marker=1;
 
9553
        COND_CMP *tmp2;
 
9554
        if ((tmp2=new COND_CMP(and_father,func)))
 
9555
          save_list->push_back(tmp2);
2439
9556
      }
2440
9557
      func->set_cmp_func();
2441
9558
    }
2450
9567
  @return
2451
9568
    new conditions
2452
9569
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9570
 
 
9571
static Item *remove_additional_cond(Item* conds)
2454
9572
{
2455
9573
  if (conds->name == in_additional_cond)
2456
9574
    return 0;
2457
9575
  if (conds->type() == Item::COND_ITEM)
2458
9576
  {
2459
9577
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9578
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9579
    Item *item;
2462
9580
    while ((item= li++))
2463
9581
    {
2473
9591
  return conds;
2474
9592
}
2475
9593
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9594
static void
 
9595
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
 
9596
                         COND *and_father, COND *cond)
2480
9597
{
2481
9598
  if (cond->type() == Item::COND_ITEM)
2482
9599
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9600
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9601
      Item_func::COND_AND_FUNC;
 
9602
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9603
    Item *item;
2486
 
    list<COND_CMP> save;
 
9604
    I_List<COND_CMP> save;
2487
9605
    while ((item=li++))
2488
9606
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9607
      propagate_cond_constants(session, &save,and_level ? cond : item, item);
2490
9608
    }
2491
9609
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9610
    {                                           // Handle other found items
 
9611
      I_List_iterator<COND_CMP> cond_itr(save);
 
9612
      COND_CMP *cond_cmp;
 
9613
      while ((cond_cmp=cond_itr++))
2495
9614
      {
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
 
        }
 
9615
        Item **args= cond_cmp->cmp_func->arguments();
 
9616
        if (!args[0]->const_item())
 
9617
          change_cond_ref_to_const(session, &save,cond_cmp->and_level,
 
9618
                                   cond_cmp->and_level, args[0], args[1]);
2502
9619
      }
2503
9620
    }
2504
9621
  }
2505
9622
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9623
  {
2507
9624
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9625
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9626
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9627
    {
2511
9628
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9629
      Item **args= func->arguments();
2515
9632
      if (!(left_const && right_const) &&
2516
9633
          args[0]->result_type() == args[1]->result_type())
2517
9634
      {
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
 
}
 
9635
        if (right_const)
 
9636
        {
 
9637
          resolve_const_item(session, &args[1], args[0]);
 
9638
          func->update_used_tables();
 
9639
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9640
                                   args[0], args[1]);
 
9641
        }
 
9642
        else if (left_const)
 
9643
        {
 
9644
          resolve_const_item(session, &args[0], args[1]);
 
9645
          func->update_used_tables();
 
9646
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9647
                                   args[1], args[0]);
 
9648
        }
 
9649
      }
 
9650
    }
 
9651
  }
 
9652
}
 
9653
 
 
9654
 
 
9655
/**
 
9656
  Simplify joins replacing outer joins by inner joins whenever it's
 
9657
  possible.
 
9658
 
 
9659
    The function, during a retrieval of join_list,  eliminates those
 
9660
    outer joins that can be converted into inner join, possibly nested.
 
9661
    It also moves the on expressions for the converted outer joins
 
9662
    and from inner joins to conds.
 
9663
    The function also calculates some attributes for nested joins:
 
9664
    - used_tables
 
9665
    - not_null_tables
 
9666
    - dep_tables.
 
9667
    - on_expr_dep_tables
 
9668
    The first two attributes are used to test whether an outer join can
 
9669
    be substituted for an inner join. The third attribute represents the
 
9670
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9671
    on table t1, then in any evaluated execution plan table access to
 
9672
    table t2 must precede access to table t2. This relation is used also
 
9673
    to check whether the query contains  invalid cross-references.
 
9674
    The forth attribute is an auxiliary one and is used to calculate
 
9675
    dep_tables.
 
9676
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9677
    execution plan, the dependencies required by the straight join
 
9678
    modifiers are reflected in this attribute as well.
 
9679
    The function also removes all braces that can be removed from the join
 
9680
    expression without changing its meaning.
 
9681
 
 
9682
  @note
 
9683
    An outer join can be replaced by an inner join if the where condition
 
9684
    or the on expression for an embedding nested join contains a conjunctive
 
9685
    predicate rejecting null values for some attribute of the inner tables.
 
9686
 
 
9687
    E.g. in the query:
 
9688
    @code
 
9689
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9690
    @endcode
 
9691
    the predicate t2.b < 5 rejects nulls.
 
9692
    The query is converted first to:
 
9693
    @code
 
9694
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9695
    @endcode
 
9696
    then to the equivalent form:
 
9697
    @code
 
9698
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9699
    @endcode
 
9700
 
 
9701
 
 
9702
    Similarly the following query:
 
9703
    @code
 
9704
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9705
        WHERE t2.c < 5
 
9706
    @endcode
 
9707
    is converted to:
 
9708
    @code
 
9709
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
 
9710
 
 
9711
    @endcode
 
9712
 
 
9713
    One conversion might trigger another:
 
9714
    @code
 
9715
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9716
                       LEFT JOIN t3 ON t3.b=t2.b
 
9717
        WHERE t3 IS NOT NULL =>
 
9718
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9719
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
 
9720
      SELECT * FROM t1, t2, t3
 
9721
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9722
  @endcode
 
9723
 
 
9724
    The function removes all unnecessary braces from the expression
 
9725
    produced by the conversions.
 
9726
    E.g.
 
9727
    @code
 
9728
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9729
    @endcode
 
9730
    finally is converted to:
 
9731
    @code
 
9732
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9733
 
 
9734
    @endcode
 
9735
 
 
9736
 
 
9737
    It also will remove braces from the following queries:
 
9738
    @code
 
9739
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9740
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9741
    @endcode
 
9742
 
 
9743
    The benefit of this simplification procedure is that it might return
 
9744
    a query for which the optimizer can evaluate execution plan with more
 
9745
    join orders. With a left join operation the optimizer does not
 
9746
    consider any plan where one of the inner tables is before some of outer
 
9747
    tables.
 
9748
 
 
9749
  IMPLEMENTATION
 
9750
    The function is implemented by a recursive procedure.  On the recursive
 
9751
    ascent all attributes are calculated, all outer joins that can be
 
9752
    converted are replaced and then all unnecessary braces are removed.
 
9753
    As join list contains join tables in the reverse order sequential
 
9754
    elimination of outer joins does not require extra recursive calls.
 
9755
 
 
9756
  SEMI-JOIN NOTES
 
9757
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9758
    an "ancestor" semi-join nest)
 
9759
 
 
9760
  EXAMPLES
 
9761
    Here is an example of a join query with invalid cross references:
 
9762
    @code
 
9763
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
 
9764
    @endcode
 
9765
 
 
9766
  @param join        reference to the query info
 
9767
  @param join_list   list representation of the join to be converted
 
9768
  @param conds       conditions to add on expressions for converted joins
 
9769
  @param top         true <=> conds is the where condition
 
9770
 
 
9771
  @return
 
9772
    - The new condition, if success
 
9773
    - 0, otherwise
 
9774
*/
 
9775
 
 
9776
static COND *
 
9777
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9778
               bool in_sj)
 
9779
{
 
9780
  TableList *table;
 
9781
  nested_join_st *nested_join;
 
9782
  TableList *prev_table= 0;
 
9783
  List_iterator<TableList> li(*join_list);
 
9784
 
 
9785
  /*
 
9786
    Try to simplify join operations from join_list.
 
9787
    The most outer join operation is checked for conversion first.
 
9788
  */
 
9789
  while ((table= li++))
 
9790
  {
 
9791
    table_map used_tables;
 
9792
    table_map not_null_tables= (table_map) 0;
 
9793
 
 
9794
    if ((nested_join= table->nested_join))
 
9795
    {
 
9796
      /*
 
9797
         If the element of join_list is a nested join apply
 
9798
         the procedure to its nested join list first.
 
9799
      */
 
9800
      if (table->on_expr)
 
9801
      {
 
9802
        Item *expr= table->on_expr;
 
9803
        /*
 
9804
           If an on expression E is attached to the table,
 
9805
           check all null rejected predicates in this expression.
 
9806
           If such a predicate over an attribute belonging to
 
9807
           an inner table of an embedded outer join is found,
 
9808
           the outer join is converted to an inner join and
 
9809
           the corresponding on expression is added to E.
 
9810
        */
 
9811
        expr= simplify_joins(join, &nested_join->join_list,
 
9812
                             expr, false, in_sj || table->sj_on_expr);
 
9813
 
 
9814
        if (!table->prep_on_expr || expr != table->on_expr)
 
9815
        {
 
9816
          assert(expr);
 
9817
 
 
9818
          table->on_expr= expr;
 
9819
          table->prep_on_expr= expr->copy_andor_structure(join->session);
 
9820
        }
 
9821
      }
 
9822
      nested_join->used_tables= (table_map) 0;
 
9823
      nested_join->not_null_tables=(table_map) 0;
 
9824
      conds= simplify_joins(join, &nested_join->join_list, conds, top,
 
9825
                            in_sj || table->sj_on_expr);
 
9826
      used_tables= nested_join->used_tables;
 
9827
      not_null_tables= nested_join->not_null_tables;
 
9828
    }
 
9829
    else
 
9830
    {
 
9831
      if (!table->prep_on_expr)
 
9832
        table->prep_on_expr= table->on_expr;
 
9833
      used_tables= table->table->map;
 
9834
      if (conds)
 
9835
        not_null_tables= conds->not_null_tables();
 
9836
    }
 
9837
 
 
9838
    if (table->embedding)
 
9839
    {
 
9840
      table->embedding->nested_join->used_tables|= used_tables;
 
9841
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9842
    }
 
9843
 
 
9844
    if (!table->outer_join || (used_tables & not_null_tables))
 
9845
    {
 
9846
      /*
 
9847
        For some of the inner tables there are conjunctive predicates
 
9848
        that reject nulls => the outer join can be replaced by an inner join.
 
9849
      */
 
9850
      table->outer_join= 0;
 
9851
      if (table->on_expr)
 
9852
      {
 
9853
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9854
        if (conds)
 
9855
        {
 
9856
          conds= and_conds(conds, table->on_expr);
 
9857
          conds->top_level_item();
 
9858
          /* conds is always a new item as both cond and on_expr existed */
 
9859
          assert(!conds->fixed);
 
9860
          conds->fix_fields(join->session, &conds);
 
9861
        }
 
9862
        else
 
9863
          conds= table->on_expr;
 
9864
        table->prep_on_expr= table->on_expr= 0;
 
9865
      }
 
9866
    }
 
9867
 
 
9868
    if (!top)
 
9869
      continue;
 
9870
 
 
9871
    /*
 
9872
      Only inner tables of non-convertible outer joins
 
9873
      remain with on_expr.
 
9874
    */
 
9875
    if (table->on_expr)
 
9876
    {
 
9877
      table->dep_tables|= table->on_expr->used_tables();
 
9878
      if (table->embedding)
 
9879
      {
 
9880
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
 
9881
        /*
 
9882
           Embedding table depends on tables used
 
9883
           in embedded on expressions.
 
9884
        */
 
9885
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9886
      }
 
9887
      else
 
9888
        table->dep_tables&= ~table->table->map;
 
9889
    }
 
9890
 
 
9891
    if (prev_table)
 
9892
    {
 
9893
      /* The order of tables is reverse: prev_table follows table */
 
9894
      if (prev_table->straight)
 
9895
        prev_table->dep_tables|= used_tables;
 
9896
      if (prev_table->on_expr)
 
9897
      {
 
9898
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9899
        table_map prev_used_tables= prev_table->nested_join ?
 
9900
                                    prev_table->nested_join->used_tables :
 
9901
                                    prev_table->table->map;
 
9902
        /*
 
9903
          If on expression contains only references to inner tables
 
9904
          we still make the inner tables dependent on the outer tables.
 
9905
          It would be enough to set dependency only on one outer table
 
9906
          for them. Yet this is really a rare case.
 
9907
        */
 
9908
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9909
          prev_table->dep_tables|= used_tables;
 
9910
      }
 
9911
    }
 
9912
    prev_table= table;
 
9913
  }
 
9914
 
 
9915
  /*
 
9916
    Flatten nested joins that can be flattened.
 
9917
    no ON expression and not a semi-join => can be flattened.
 
9918
  */
 
9919
  li.rewind();
 
9920
  while ((table= li++))
 
9921
  {
 
9922
    nested_join= table->nested_join;
 
9923
    if (table->sj_on_expr && !in_sj)
 
9924
    {
 
9925
       /*
 
9926
         If this is a semi-join that is not contained within another semi-join,
 
9927
         leave it intact (otherwise it is flattened)
 
9928
       */
 
9929
      join->select_lex->sj_nests.push_back(table);
 
9930
    }
 
9931
    else if (nested_join && !table->on_expr)
 
9932
    {
 
9933
      TableList *tbl;
 
9934
      List_iterator<TableList> it(nested_join->join_list);
 
9935
      while ((tbl= it++))
 
9936
      {
 
9937
        tbl->embedding= table->embedding;
 
9938
        tbl->join_list= table->join_list;
 
9939
      }
 
9940
      li.replace(nested_join->join_list);
 
9941
    }
 
9942
  }
 
9943
  return(conds);
 
9944
}
 
9945
 
 
9946
 
 
9947
/**
 
9948
  Assign each nested join structure a bit in nested_join_map.
 
9949
 
 
9950
    Assign each nested join structure (except "confluent" ones - those that
 
9951
    embed only one element) a bit in nested_join_map.
 
9952
 
 
9953
  @param join          Join being processed
 
9954
  @param join_list     List of tables
 
9955
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9956
                       call
 
9957
 
 
9958
  @note
 
9959
    This function is called after simplify_joins(), when there are no
 
9960
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9961
    we will not run out of bits in nested_join_map.
 
9962
 
 
9963
  @return
 
9964
    First unused bit in nested_join_map after the call.
 
9965
*/
 
9966
 
 
9967
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
9968
                                          uint32_t first_unused)
 
9969
{
 
9970
  List_iterator<TableList> li(*join_list);
 
9971
  TableList *table;
 
9972
  while ((table= li++))
 
9973
  {
 
9974
    nested_join_st *nested_join;
 
9975
    if ((nested_join= table->nested_join))
 
9976
    {
 
9977
      /*
 
9978
        It is guaranteed by simplify_joins() function that a nested join
 
9979
        that has only one child is either
 
9980
         - a single-table view (the child is the underlying table), or
 
9981
         - a single-table semi-join nest
 
9982
 
 
9983
        We don't assign bits to such sj-nests because
 
9984
        1. it is redundant (a "sequence" of one table cannot be interleaved
 
9985
            with anything)
 
9986
        2. we could run out bits in nested_join_map otherwise.
 
9987
      */
 
9988
      if (nested_join->join_list.elements != 1)
 
9989
      {
 
9990
        /* Don't assign bits to sj-nests */
 
9991
        if (table->on_expr)
 
9992
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9993
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9994
                                                    first_unused);
 
9995
      }
 
9996
    }
 
9997
  }
 
9998
  return(first_unused);
 
9999
}
 
10000
 
 
10001
 
 
10002
/**
 
10003
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
10004
 
 
10005
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
10006
    the passed join_list.
 
10007
 
 
10008
  @param join_list  List of nested joins to process. It may also contain base
 
10009
                    tables which will be ignored.
 
10010
*/
 
10011
 
 
10012
static void reset_nj_counters(List<TableList> *join_list)
 
10013
{
 
10014
  List_iterator<TableList> li(*join_list);
 
10015
  TableList *table;
 
10016
  while ((table= li++))
 
10017
  {
 
10018
    nested_join_st *nested_join;
 
10019
    if ((nested_join= table->nested_join))
 
10020
    {
 
10021
      nested_join->counter_= 0;
 
10022
      reset_nj_counters(&nested_join->join_list);
 
10023
    }
 
10024
  }
 
10025
  return;
 
10026
}
 
10027
 
2536
10028
 
2537
10029
/**
2538
10030
  Check interleaving with an inner tables of an outer join for
2610
10102
         position:
2611
10103
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10104
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10105
          2. {each nested_join_st structure not simplified away}->counter - number
2614
10106
             of this nested join's children that have already been added to to
2615
10107
             the partial join order.
2616
10108
  @endverbatim
2617
10109
 
2618
10110
  @param join       Join being processed
 
10111
  @param last_tab   Last table in current partial join order (this function is
 
10112
                    not called for empty partial join orders)
2619
10113
  @param next_tab   Table we're going to extend the current partial join with
2620
10114
 
2621
10115
  @retval
2624
10118
  @retval
2625
10119
    true   Requested join order extension not allowed.
2626
10120
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10121
 
 
10122
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10123
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10124
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10125
  JOIN *join= last_tab->join;
2631
10126
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10127
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10128
  {
2634
10129
    /*
2635
10130
      next_tab is outside of the "pair of brackets" we're currently in.
2642
10137
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10138
    X,Y,Z in the above picture)
2644
10139
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10140
  for (;next_emb; next_emb= next_emb->embedding)
2646
10141
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10142
    next_emb->nested_join->counter_++;
 
10143
    if (next_emb->nested_join->counter_ == 1)
2649
10144
    {
2650
10145
      /*
2651
10146
        next_emb is the first table inside a nested join we've "entered". In
2652
10147
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10148
        X bracket might have Y pair bracket.
2654
10149
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10150
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10151
    }
2657
10152
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10153
    if (next_emb->nested_join->join_list.elements !=
 
10154
        next_emb->nested_join->counter_)
2660
10155
      break;
2661
10156
 
2662
10157
    /*
2663
10158
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10159
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10160
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10161
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10162
  }
2668
10163
  return false;
2669
10164
}
2670
10165
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
 
10166
 
 
10167
/**
 
10168
  Nested joins perspective: Remove the last table from the join order.
 
10169
 
 
10170
    Remove the last table from the partial join order and update the nested
 
10171
    joins counters and join->cur_embedding_map. It is ok to call this
 
10172
    function for the first table in join order (for which
 
10173
    check_interleaving_with_nj has not been called)
 
10174
 
 
10175
  @param last  join table to remove, it is assumed to be the last in current
 
10176
               partial join order.
 
10177
*/
 
10178
 
 
10179
static void restore_prev_nj_state(JOIN_TAB *last)
 
10180
{
 
10181
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10182
  JOIN *join= last->join;
 
10183
  while (last_emb)
 
10184
  {
 
10185
    if (last_emb->on_expr)
 
10186
    {
 
10187
      if (!(--last_emb->nested_join->counter_))
 
10188
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10189
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10190
               last_emb->nested_join->counter_)
 
10191
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10192
      else
 
10193
        break;
 
10194
    }
 
10195
    last_emb= last_emb->embedding;
 
10196
  }
 
10197
}
 
10198
 
 
10199
 
 
10200
 
 
10201
static
 
10202
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10203
{
 
10204
  TableList *emb_sj_nest;
 
10205
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10206
  {
 
10207
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10208
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10209
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10210
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10211
  }
 
10212
}
 
10213
 
 
10214
 
 
10215
/*
 
10216
  we assume remaining_tables doesnt contain @tab.
 
10217
*/
 
10218
 
 
10219
static void restore_prev_sj_state(const table_map remaining_tables,
 
10220
                                  const JOIN_TAB *tab)
 
10221
{
 
10222
  TableList *emb_sj_nest;
 
10223
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10224
  {
 
10225
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10226
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) ==
 
10227
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10228
    {
 
10229
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10230
    }
 
10231
  }
 
10232
}
 
10233
 
 
10234
 
 
10235
static COND *
 
10236
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10237
              Item::cond_result *cond_value)
2672
10238
{
2673
10239
  Session *session= join->session;
2674
10240
 
2688
10254
                             &join->cond_equal);
2689
10255
 
2690
10256
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10257
    propagate_cond_constants(session, (I_List<COND_CMP> *) 0, conds, conds);
2693
10258
    /*
2694
10259
      Remove all instances of item == item
2695
10260
      Remove all and-levels where CONST item != CONST item
2699
10264
  return(conds);
2700
10265
}
2701
10266
 
 
10267
 
2702
10268
/**
2703
10269
  Remove const and eq items.
2704
10270
 
2709
10275
    - COND_TRUE   : always true ( 1 = 1 )
2710
10276
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10277
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10278
 
 
10279
COND *
 
10280
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
2713
10281
{
2714
10282
  if (cond->type() == Item::COND_ITEM)
2715
10283
  {
2716
10284
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
10285
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10286
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10287
    Item::cond_result tmp_cond_value;
2720
10288
    bool should_fix_fields= false;
2721
10289
 
2755
10323
          }
2756
10324
          break;
2757
10325
        case Item::COND_UNDEF:                  /* Impossible */
2758
 
          break;
 
10326
          break; /* purecov: deadcode */
2759
10327
      }
2760
10328
    }
2761
10329
 
2769
10337
    {                                           
2770
10338
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
2771
10339
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10340
      ((Item_cond*) cond)->argument_list()->empty();
2773
10341
      return item;
2774
10342
    }
2775
10343
  }
2791
10359
    {
2792
10360
      Field *field= ((Item_field*) args[0])->field;
2793
10361
      if (field->flags & AUTO_INCREMENT_FLAG 
2794
 
          && ! field->getTable()->maybe_null 
 
10362
          && ! field->table->maybe_null 
2795
10363
          && session->options & OPTION_AUTO_IS_NULL
2796
10364
          && (
2797
10365
            session->first_successful_insert_id_in_prev_stmt > 0 
2847
10415
  }
2848
10416
  else if (cond->const_item() && !cond->is_expensive())
2849
10417
  /*
2850
 
    @todo
 
10418
    TODO:
2851
10419
    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
 
 
 
10420
    materialized IN because it is created later than this phase, and cannot be
 
10421
    evaluated at this point.
 
10422
    The condition should be something as (need to fix member access):
 
10423
      !(cond->type() == Item::FUNC_ITEM &&
 
10424
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10425
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10426
  */
2859
10427
  {
2860
10428
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2898
10466
    true    can be used
2899
10467
    false   cannot be used
2900
10468
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10469
static bool
 
10470
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10471
{
2903
10472
  return r->const_item() &&
2904
10473
    /* elements must be compared as dates */
2913
10482
/**
2914
10483
  Return true if the item is a const value in all the WHERE clause.
2915
10484
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10485
 
 
10486
static bool
 
10487
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10488
{
2918
10489
  if (cond->type() == Item::COND_ITEM)
2919
10490
  {
2920
10491
    bool and_level= (((Item_cond*) cond)->functype()
2921
10492
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10493
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10494
    Item *item;
2924
10495
    while ((item=li++))
2925
10496
    {
2926
10497
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10498
      if (res)                                  // Is a const value
2928
10499
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10500
        if (and_level)
 
10501
          return 1;
2931
10502
      }
2932
10503
      else if (!and_level)
2933
 
        return 0;
 
10504
        return 0;
2934
10505
    }
2935
10506
    return and_level ? 0 : 1;
2936
10507
  }
2938
10509
  {                                             // boolan compare function
2939
10510
    Item_func* func= (Item_func*) cond;
2940
10511
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10512
        func->functype() != Item_func::EQ_FUNC)
2942
10513
      return 0;
2943
10514
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10515
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10517
    {
2947
10518
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10519
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10520
        if (*const_item)
 
10521
          return right_item->eq(*const_item, 1);
 
10522
        *const_item=right_item;
 
10523
        return 1;
2953
10524
      }
2954
10525
    }
2955
10526
    else if (right_item->eq(comp_item,1))
2956
10527
    {
2957
10528
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10529
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
 
10530
        if (*const_item)
 
10531
          return left_item->eq(*const_item, 1);
 
10532
        *const_item=left_item;
 
10533
        return 1;
2963
10534
      }
2964
10535
    }
2965
10536
  }
2966
10537
  return 0;
2967
10538
}
2968
10539
 
 
10540
 
2969
10541
/**
2970
10542
  @details
2971
10543
  Rows produced by a join sweep may end up in a temporary table or be sent
2977
10549
  @return
2978
10550
    end_select function to use. This function can't fail.
2979
10551
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
10552
 
 
10553
Next_select_func setup_end_select_func(JOIN *join)
2981
10554
{
2982
10555
  Table *table= join->tmp_table;
2983
10556
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
2989
10562
    if (table->group && tmp_tbl->sum_func_count &&
2990
10563
        !tmp_tbl->precomputed_group_by)
2991
10564
    {
2992
 
      if (table->getShare()->sizeKeys())
 
10565
      if (table->s->keys)
2993
10566
      {
2994
 
        end_select= end_update;
 
10567
        end_select=end_update;
2995
10568
      }
2996
10569
      else
2997
10570
      {
2998
 
        end_select= end_unique_update;
 
10571
        end_select=end_unique_update;
2999
10572
      }
3000
10573
    }
3001
10574
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
10575
    {
3003
 
      end_select= end_write_group;
 
10576
      end_select=end_write_group;
3004
10577
    }
3005
10578
    else
3006
10579
    {
3007
 
      end_select= end_write;
 
10580
      end_select=end_write;
3008
10581
      if (tmp_tbl->precomputed_group_by)
3009
10582
      {
3010
10583
        /*
3032
10605
  return end_select;
3033
10606
}
3034
10607
 
 
10608
 
3035
10609
/**
3036
10610
  Make a join of all tables and write it on socket or to table.
3037
10611
 
3042
10616
  @retval
3043
10617
    -1  if error should be sent
3044
10618
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10619
 
 
10620
static int
 
10621
do_select(JOIN *join,List<Item> *fields,Table *table)
3046
10622
{
3047
10623
  int rc= 0;
3048
10624
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
 
10625
  JOIN_TAB *join_tab= NULL;
3050
10626
 
3051
10627
  join->tmp_table= table;                       /* Save for easy recursion */
3052
10628
  join->fields= fields;
3053
10629
 
3054
10630
  if (table)
3055
10631
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
10632
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10633
    empty_record(table);
3058
10634
    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
 
    }
 
10635
        table->s->keys && !table->file->inited)
 
10636
      table->file->ha_index_init(0, 0);
3069
10637
  }
3070
10638
  /* Set up select_end */
3071
10639
  Next_select_func end_select= setup_end_select_func(join);
3086
10654
    {
3087
10655
      error= (*end_select)(join, 0, 0);
3088
10656
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
10657
        error= (*end_select)(join, 0, 1);
3090
10658
 
3091
10659
      /*
3092
10660
        If we don't go through evaluate_join_record(), do the counting
3124
10692
    if (!table)                                 // If sending data to client
3125
10693
    {
3126
10694
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
10695
        The following will unlock all cursors if the command wasn't an
 
10696
        update command
3129
10697
      */
3130
10698
      join->join_free();                        // Unlock all cursors
3131
10699
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
10700
        rc= 1;                                  // Don't send error
3133
10701
    }
3134
10702
  }
3135
10703
  else
3137
10705
  if (table)
3138
10706
  {
3139
10707
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10708
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
10709
    {
3142
10710
      new_errno= tmp;
3143
10711
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10712
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
10713
    {
3146
10714
      new_errno= tmp;
3147
10715
    }
3148
10716
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
10717
      table->file->print_error(new_errno,MYF(0));
3150
10718
  }
3151
10719
  return(join->session->is_error() ? -1 : rc);
3152
10720
}
3153
10721
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10722
 
 
10723
enum_nested_loop_state
 
10724
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
10725
{
3156
10726
  enum_nested_loop_state rc;
3157
10727
 
3162
10732
      rc= sub_select(join,join_tab,end_of_records);
3163
10733
    return rc;
3164
10734
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
10735
  if (join->session->killed)            // If aborted by user
3166
10736
  {
3167
10737
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
10738
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
10739
  }
3170
10740
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
10741
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
10742
    if (!store_record_in_cache(&join_tab->cache))
3173
10743
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
10744
    return flush_cached_records(join,join_tab,false);
3175
10745
  }
3298
10868
  @return
3299
10869
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
10870
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10871
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10872
 
 
10873
enum_nested_loop_state
 
10874
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
10875
{
3303
10876
  join_tab->table->null_row=0;
3304
10877
  if (end_of_records)
3306
10879
 
3307
10880
  int error;
3308
10881
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
10882
  READ_RECORD *info= &join_tab->read_record;
 
10883
 
 
10884
  if (join_tab->flush_weedout_table)
 
10885
  {
 
10886
    do_sj_reset(join_tab->flush_weedout_table);
 
10887
  }
3310
10888
 
3311
10889
  if (join->resume_nested_loop)
3312
10890
  {
3359
10937
  return rc;
3360
10938
}
3361
10939
 
3362
 
int safe_index_read(JoinTable *tab)
 
10940
 
 
10941
 
 
10942
 
 
10943
/*
 
10944
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10945
 
 
10946
  SYNPOSIS
 
10947
    do_sj_dups_weedout()
 
10948
 
 
10949
  RETURN
 
10950
    -1  Error
 
10951
    1   The row combination is a duplicate (discard it)
 
10952
    0   The row combination is not a duplicate (continue)
 
10953
*/
 
10954
 
 
10955
int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
 
10956
{
 
10957
  int error;
 
10958
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10959
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10960
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
10961
  unsigned char *nulls_ptr= ptr;
 
10962
 
 
10963
  /* Put the the rowids tuple into table->record[0]: */
 
10964
 
 
10965
  // 1. Store the length
 
10966
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10967
  {
 
10968
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10969
    ptr++;
 
10970
  }
 
10971
  else
 
10972
  {
 
10973
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10974
    ptr += 2;
 
10975
  }
 
10976
 
 
10977
  // 2. Zero the null bytes
 
10978
  if (sjtbl->null_bytes)
 
10979
  {
 
10980
    memset(ptr, 0, sjtbl->null_bytes);
 
10981
    ptr += sjtbl->null_bytes;
 
10982
  }
 
10983
 
 
10984
  // 3. Put the rowids
 
10985
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10986
  {
 
10987
    handler *h= tab->join_tab->table->file;
 
10988
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10989
    {
 
10990
      /* It's a NULL-complemented row */
 
10991
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10992
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10993
    }
 
10994
    else
 
10995
    {
 
10996
      /* Copy the rowid value */
 
10997
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10998
        h->position(tab->join_tab->table->record[0]);
 
10999
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
11000
    }
 
11001
  }
 
11002
 
 
11003
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
11004
  if (error)
 
11005
  {
 
11006
    /* create_myisam_from_heap will generate error if needed */
 
11007
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
11008
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
 
11009
                                &sjtbl->recinfo, error, 1))
 
11010
      return -1;
 
11011
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
11012
    return 1;
 
11013
  }
 
11014
  return 0;
 
11015
}
 
11016
 
 
11017
 
 
11018
/*
 
11019
  SemiJoinDuplicateElimination: Reset the temporary table
 
11020
*/
 
11021
 
 
11022
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
11023
{
 
11024
  if (sj_tbl->tmp_table)
 
11025
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
11026
  return 0;
 
11027
}
 
11028
 
 
11029
/*
 
11030
  Process one record of the nested loop join.
 
11031
 
 
11032
    This function will evaluate parts of WHERE/ON clauses that are
 
11033
    applicable to the partial record on hand and in case of success
 
11034
    submit this record to the next level of the nested loop.
 
11035
*/
 
11036
 
 
11037
static enum_nested_loop_state
 
11038
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11039
                     int error)
 
11040
{
 
11041
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11042
  ha_rows found_records=join->found_records;
 
11043
  COND *select_cond= join_tab->select_cond;
 
11044
 
 
11045
  if (error > 0 || (join->session->is_error()))     // Fatal error
 
11046
    return NESTED_LOOP_ERROR;
 
11047
  if (error < 0)
 
11048
    return NESTED_LOOP_NO_MORE_ROWS;
 
11049
  if (join->session->killed)                    // Aborted by user
 
11050
  {
 
11051
    join->session->send_kill_message();
 
11052
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11053
  }
 
11054
  if (!select_cond || select_cond->val_int())
 
11055
  {
 
11056
    /*
 
11057
      There is no select condition or the attached pushed down
 
11058
      condition is true => a match is found.
 
11059
    */
 
11060
    bool found= 1;
 
11061
    while (join_tab->first_unmatched && found)
 
11062
    {
 
11063
      /*
 
11064
        The while condition is always false if join_tab is not
 
11065
        the last inner join table of an outer join operation.
 
11066
      */
 
11067
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11068
      /*
 
11069
        Mark that a match for current outer table is found.
 
11070
        This activates push down conditional predicates attached
 
11071
        to the all inner tables of the outer join.
 
11072
      */
 
11073
      first_unmatched->found= 1;
 
11074
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11075
      {
 
11076
        if (tab->table->reginfo.not_exists_optimize)
 
11077
          return NESTED_LOOP_NO_MORE_ROWS;
 
11078
        /* Check all predicates that has just been activated. */
 
11079
        /*
 
11080
          Actually all predicates non-guarded by first_unmatched->found
 
11081
          will be re-evaluated again. It could be fixed, but, probably,
 
11082
          it's not worth doing now.
 
11083
        */
 
11084
        if (tab->select_cond && !tab->select_cond->val_int())
 
11085
        {
 
11086
          /* The condition attached to table tab is false */
 
11087
          if (tab == join_tab)
 
11088
            found= 0;
 
11089
          else
 
11090
          {
 
11091
            /*
 
11092
              Set a return point if rejected predicate is attached
 
11093
              not to the last table of the current nest level.
 
11094
            */
 
11095
            join->return_tab= tab;
 
11096
            return NESTED_LOOP_OK;
 
11097
          }
 
11098
        }
 
11099
      }
 
11100
      /*
 
11101
        Check whether join_tab is not the last inner table
 
11102
        for another embedding outer join.
 
11103
      */
 
11104
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11105
          first_unmatched->last_inner != join_tab)
 
11106
        first_unmatched= 0;
 
11107
      join_tab->first_unmatched= first_unmatched;
 
11108
    }
 
11109
 
 
11110
    JOIN_TAB *return_tab= join->return_tab;
 
11111
    join_tab->found_match= true;
 
11112
    if (join_tab->check_weed_out_table)
 
11113
    {
 
11114
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
 
11115
      if (res == -1)
 
11116
        return NESTED_LOOP_ERROR;
 
11117
      if (res == 1)
 
11118
        return NESTED_LOOP_OK;
 
11119
    }
 
11120
    else if (join_tab->do_firstmatch)
 
11121
    {
 
11122
      /*
 
11123
        We should return to the join_tab->do_firstmatch after we have
 
11124
        enumerated all the suffixes for current prefix row combination
 
11125
      */
 
11126
      return_tab= join_tab->do_firstmatch;
 
11127
    }
 
11128
 
 
11129
    /*
 
11130
      It was not just a return to lower loop level when one
 
11131
      of the newly activated predicates is evaluated as false
 
11132
      (See above join->return_tab= tab).
 
11133
    */
 
11134
    join->examined_rows++;
 
11135
    join->session->row_count++;
 
11136
 
 
11137
    if (found)
 
11138
    {
 
11139
      enum enum_nested_loop_state rc;
 
11140
      /* A match from join_tab is found for the current partial join. */
 
11141
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11142
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11143
        return rc;
 
11144
      if (return_tab < join->return_tab)
 
11145
        join->return_tab= return_tab;
 
11146
 
 
11147
      if (join->return_tab < join_tab)
 
11148
        return NESTED_LOOP_OK;
 
11149
      /*
 
11150
        Test if this was a SELECT DISTINCT query on a table that
 
11151
        was not in the field list;  In this case we can abort if
 
11152
        we found a row, as no new rows can be added to the result.
 
11153
      */
 
11154
      if (not_used_in_distinct && found_records != join->found_records)
 
11155
        return NESTED_LOOP_NO_MORE_ROWS;
 
11156
    }
 
11157
    else
 
11158
      join_tab->read_record.file->unlock_row();
 
11159
  }
 
11160
  else
 
11161
  {
 
11162
    /*
 
11163
      The condition pushed down to the table join_tab rejects all rows
 
11164
      with the beginning coinciding with the current partial join.
 
11165
    */
 
11166
    join->examined_rows++;
 
11167
    join->session->row_count++;
 
11168
    join_tab->read_record.file->unlock_row();
 
11169
  }
 
11170
  return NESTED_LOOP_OK;
 
11171
}
 
11172
 
 
11173
 
 
11174
/**
 
11175
 
 
11176
  @details
 
11177
    Construct a NULL complimented partial join record and feed it to the next
 
11178
    level of the nested loop. This function is used in case we have
 
11179
    an OUTER join and no matching record was found.
 
11180
*/
 
11181
 
 
11182
static enum_nested_loop_state
 
11183
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11184
{
 
11185
  /*
 
11186
    The table join_tab is the first inner table of a outer join operation
 
11187
    and no matches has been found for the current outer row.
 
11188
  */
 
11189
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11190
  /* Cache variables for faster loop */
 
11191
  COND *select_cond;
 
11192
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11193
  {
 
11194
    /* Change the the values of guard predicate variables. */
 
11195
    join_tab->found= 1;
 
11196
    join_tab->not_null_compl= 0;
 
11197
    /* The outer row is complemented by nulls for each inner tables */
 
11198
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11199
    mark_as_null_row(join_tab->table);       // For group by without error
 
11200
    select_cond= join_tab->select_cond;
 
11201
    /* Check all attached conditions for inner table rows. */
 
11202
    if (select_cond && !select_cond->val_int())
 
11203
      return NESTED_LOOP_OK;
 
11204
  }
 
11205
  join_tab--;
 
11206
  /*
 
11207
    The row complemented by nulls might be the first row
 
11208
    of embedding outer joins.
 
11209
    If so, perform the same actions as in the code
 
11210
    for the first regular outer join row above.
 
11211
  */
 
11212
  for ( ; ; )
 
11213
  {
 
11214
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11215
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11216
        first_unmatched->last_inner != join_tab)
 
11217
      first_unmatched= 0;
 
11218
    join_tab->first_unmatched= first_unmatched;
 
11219
    if (!first_unmatched)
 
11220
      break;
 
11221
    first_unmatched->found= 1;
 
11222
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11223
    {
 
11224
      if (tab->select_cond && !tab->select_cond->val_int())
 
11225
      {
 
11226
        join->return_tab= tab;
 
11227
        return NESTED_LOOP_OK;
 
11228
      }
 
11229
    }
 
11230
  }
 
11231
  /*
 
11232
    The row complemented by nulls satisfies all conditions
 
11233
    attached to inner tables.
 
11234
    Send the row complemented by nulls to be joined with the
 
11235
    remaining tables.
 
11236
  */
 
11237
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11238
}
 
11239
 
 
11240
 
 
11241
static enum_nested_loop_state
 
11242
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11243
{
 
11244
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11245
  int error;
 
11246
  READ_RECORD *info;
 
11247
 
 
11248
  join_tab->table->null_row= 0;
 
11249
  if (!join_tab->cache.records)
 
11250
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11251
  if (skip_last)
 
11252
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11253
  if (join_tab->use_quick == 2)
 
11254
  {
 
11255
    if (join_tab->select->quick)
 
11256
    {                                   /* Used quick select last. reset it */
 
11257
      delete join_tab->select->quick;
 
11258
      join_tab->select->quick=0;
 
11259
    }
 
11260
  }
 
11261
 /* read through all records */
 
11262
  if ((error=join_init_read_record(join_tab)))
 
11263
  {
 
11264
    reset_cache_write(&join_tab->cache);
 
11265
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11266
  }
 
11267
 
 
11268
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11269
  {
 
11270
    tmp->status=tmp->table->status;
 
11271
    tmp->table->status=0;
 
11272
  }
 
11273
 
 
11274
  info= &join_tab->read_record;
 
11275
  do
 
11276
  {
 
11277
    if (join->session->killed)
 
11278
    {
 
11279
      join->session->send_kill_message();
 
11280
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11281
    }
 
11282
    SQL_SELECT *select=join_tab->select;
 
11283
    if (rc == NESTED_LOOP_OK &&
 
11284
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11285
    {
 
11286
      uint32_t i;
 
11287
      reset_cache_read(&join_tab->cache);
 
11288
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11289
      {
 
11290
        read_cached_record(join_tab);
 
11291
        if (!select || !select->skip_record())
 
11292
        {
 
11293
          int res= 0;
 
11294
          if (!join_tab->check_weed_out_table ||
 
11295
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
 
11296
          {
 
11297
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11298
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11299
            {
 
11300
              reset_cache_write(&join_tab->cache);
 
11301
              return rc;
 
11302
            }
 
11303
          }
 
11304
          if (res == -1)
 
11305
            return NESTED_LOOP_ERROR;
 
11306
        }
 
11307
      }
 
11308
    }
 
11309
  } while (!(error=info->read_record(info)));
 
11310
 
 
11311
  if (skip_last)
 
11312
    read_cached_record(join_tab);               // Restore current record
 
11313
  reset_cache_write(&join_tab->cache);
 
11314
  if (error > 0)                                // Fatal error
 
11315
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11316
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11317
    tmp2->table->status=tmp2->status;
 
11318
  return NESTED_LOOP_OK;
 
11319
}
 
11320
 
 
11321
int safe_index_read(JOIN_TAB *tab)
3363
11322
{
3364
11323
  int error;
3365
11324
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11325
  if ((error=table->file->index_read_map(table->record[0],
3367
11326
                                         tab->ref.key_buff,
3368
11327
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
11328
                                         HA_READ_KEY_EXACT)))
3371
11330
  return 0;
3372
11331
}
3373
11332
 
 
11333
 
 
11334
static int
 
11335
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
11336
{
 
11337
  int error;
 
11338
  Table *table=tab->table;
 
11339
  table->const_table=1;
 
11340
  table->null_row=0;
 
11341
  table->status=STATUS_NO_RECORD;
 
11342
 
 
11343
  if (tab->type == JT_SYSTEM)
 
11344
  {
 
11345
    if ((error=join_read_system(tab)))
 
11346
    {                                           // Info for DESCRIBE
 
11347
      tab->info="const row not found";
 
11348
      /* Mark for EXPLAIN that the row was not found */
 
11349
      pos->records_read=0.0;
 
11350
      pos->ref_depend_map= 0;
 
11351
      if (!table->maybe_null || error > 0)
 
11352
        return(error);
 
11353
    }
 
11354
  }
 
11355
  else
 
11356
  {
 
11357
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11358
        !table->no_keyread &&
 
11359
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
11360
    {
 
11361
      table->key_read=1;
 
11362
      table->file->extra(HA_EXTRA_KEYREAD);
 
11363
      tab->index= tab->ref.key;
 
11364
    }
 
11365
    error=join_read_const(tab);
 
11366
    if (table->key_read)
 
11367
    {
 
11368
      table->key_read=0;
 
11369
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
11370
    }
 
11371
    if (error)
 
11372
    {
 
11373
      tab->info="unique row not found";
 
11374
      /* Mark for EXPLAIN that the row was not found */
 
11375
      pos->records_read=0.0;
 
11376
      pos->ref_depend_map= 0;
 
11377
      if (!table->maybe_null || error > 0)
 
11378
        return(error);
 
11379
    }
 
11380
  }
 
11381
  if (*tab->on_expr_ref && !table->null_row)
 
11382
  {
 
11383
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
11384
      mark_as_null_row(table);
 
11385
  }
 
11386
  if (!table->null_row)
 
11387
    table->maybe_null=0;
 
11388
 
 
11389
  /* Check appearance of new constant items in Item_equal objects */
 
11390
  JOIN *join= tab->join;
 
11391
  if (join->conds)
 
11392
    update_const_equal_items(join->conds, tab);
 
11393
  TableList *tbl;
 
11394
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
11395
  {
 
11396
    TableList *embedded;
 
11397
    TableList *embedding= tbl;
 
11398
    do
 
11399
    {
 
11400
      embedded= embedding;
 
11401
      if (embedded->on_expr)
 
11402
         update_const_equal_items(embedded->on_expr, tab);
 
11403
      embedding= embedded->embedding;
 
11404
    }
 
11405
    while (embedding &&
 
11406
           embedding->nested_join->join_list.head() == embedded);
 
11407
  }
 
11408
 
 
11409
  return(0);
 
11410
}
 
11411
 
 
11412
 
 
11413
static int
 
11414
join_read_system(JOIN_TAB *tab)
 
11415
{
 
11416
  Table *table= tab->table;
 
11417
  int error;
 
11418
  if (table->status & STATUS_GARBAGE)           // If first read
 
11419
  {
 
11420
    if ((error=table->file->read_first_row(table->record[0],
 
11421
                                           table->s->primary_key)))
 
11422
    {
 
11423
      if (error != HA_ERR_END_OF_FILE)
 
11424
        return table->report_error(error);
 
11425
      mark_as_null_row(tab->table);
 
11426
      empty_record(table);                      // Make empty record
 
11427
      return -1;
 
11428
    }
 
11429
    update_virtual_fields_marked_for_write(table);
 
11430
    store_record(table,record[1]);
 
11431
  }
 
11432
  else if (!table->status)                      // Only happens with left join
 
11433
    restore_record(table,record[1]);                    // restore old record
 
11434
  table->null_row=0;
 
11435
  return table->status ? -1 : 0;
 
11436
}
 
11437
 
 
11438
 
3374
11439
/**
3375
11440
  Read a (constant) table when there is at most one matching row.
3376
11441
 
3383
11448
  @retval
3384
11449
    1   Got an error (other than row not found) during read
3385
11450
*/
3386
 
int join_read_const(JoinTable *tab)
 
11451
 
 
11452
static int
 
11453
join_read_const(JOIN_TAB *tab)
3387
11454
{
3388
11455
  int error;
3389
11456
  Table *table= tab->table;
3391
11458
  {
3392
11459
    table->status= 0;
3393
11460
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11461
      error=HA_ERR_KEY_NOT_FOUND;
3395
11462
    else
3396
11463
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
11464
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
3398
11465
                                            (unsigned char*) tab->ref.key_buff,
3399
11466
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
11467
                                            HA_READ_KEY_EXACT);
3402
11469
    if (error)
3403
11470
    {
3404
11471
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
11472
      mark_as_null_row(tab->table);
 
11473
      empty_record(table);
3407
11474
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
11475
        return table->report_error(error);
3409
11476
      return -1;
3410
11477
    }
3411
 
    table->storeRecord();
 
11478
    update_virtual_fields_marked_for_write(table);
 
11479
    store_record(table,record[1]);
3412
11480
  }
3413
11481
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
11482
  {
3415
11483
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
11484
    restore_record(table,record[1]);                    // restore old record
3417
11485
  }
3418
11486
  table->null_row=0;
3419
11487
  return table->status ? -1 : 0;
3420
11488
}
3421
11489
 
 
11490
 
3422
11491
/*
3423
11492
  eq_ref access method implementation: "read_first" function
3424
11493
 
3425
11494
  SYNOPSIS
3426
11495
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
11496
      tab  JOIN_TAB of the accessed table
3428
11497
 
3429
11498
  DESCRIPTION
3430
11499
    This is "read_fist" function for the "ref" access method. The difference
3435
11504
   -1  - Row not found
3436
11505
    1  - Error
3437
11506
*/
3438
 
int join_read_key(JoinTable *tab)
 
11507
 
 
11508
static int
 
11509
join_read_key(JOIN_TAB *tab)
3439
11510
{
3440
11511
  int error;
3441
11512
  Table *table= tab->table;
3442
11513
 
3443
 
  if (!table->cursor->inited)
 
11514
  if (!table->file->inited)
3444
11515
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
11516
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
11517
  }
3451
11518
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
11519
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
11520
  if (cmp_buffer_with_ref(tab) ||
3454
11521
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
11522
  {
3458
11525
      table->status=STATUS_NOT_FOUND;
3459
11526
      return -1;
3460
11527
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11528
    error=table->file->index_read_map(table->record[0],
3462
11529
                                      tab->ref.key_buff,
3463
11530
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
11531
                                      HA_READ_KEY_EXACT);
3469
11536
  return table->status ? -1 : 0;
3470
11537
}
3471
11538
 
 
11539
 
3472
11540
/*
3473
11541
  ref access method implementation: "read_first" function
3474
11542
 
3475
11543
  SYNOPSIS
3476
11544
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
11545
      tab  JOIN_TAB of the accessed table
3478
11546
 
3479
11547
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
 
11548
    This is "read_fist" function for the "ref" access method.
3481
11549
 
3482
11550
    The functon must leave the index initialized when it returns.
3483
11551
    ref_or_null access implementation depends on that.
3487
11555
   -1  - Row not found
3488
11556
    1  - Error
3489
11557
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
11558
 
 
11559
static int
 
11560
join_read_always_key(JOIN_TAB *tab)
3491
11561
{
3492
11562
  int error;
3493
11563
  Table *table= tab->table;
3494
11564
 
3495
11565
  /* 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
 
  }
 
11566
  if (!table->file->inited)
 
11567
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3502
11568
 
3503
11569
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
11570
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3509
11575
 
3510
11576
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3511
11577
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11578
  if ((error=table->file->index_read_map(table->record[0],
3513
11579
                                         tab->ref.key_buff,
3514
11580
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
11581
                                         HA_READ_KEY_EXACT)))
3516
11582
  {
3517
11583
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
11584
      return table->report_error(error);
3519
 
    return -1;
 
11585
    return -1; /* purecov: inspected */
3520
11586
  }
3521
 
 
 
11587
  update_virtual_fields_marked_for_write(table);
3522
11588
  return 0;
3523
11589
}
3524
11590
 
 
11591
 
3525
11592
/**
3526
 
  This function is used when optimizing away ORDER BY in
3527
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11593
  This function is used when optimizing away order_st BY in
 
11594
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3528
11595
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
11596
 
 
11597
static int
 
11598
join_read_last_key(JOIN_TAB *tab)
3530
11599
{
3531
11600
  int error;
3532
11601
  Table *table= tab->table;
3533
11602
 
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
 
  }
 
11603
  if (!table->file->inited)
 
11604
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3540
11605
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3541
11606
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11607
  if ((error=table->file->index_read_last_map(table->record[0],
3543
11608
                                              tab->ref.key_buff,
3544
11609
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
11610
  {
3546
11611
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
11612
      return table->report_error(error);
3548
 
    return -1;
 
11613
    return -1; /* purecov: inspected */
3549
11614
  }
3550
11615
  return 0;
3551
11616
}
3552
11617
 
3553
 
int join_no_more_records(ReadRecord *)
 
11618
 
 
11619
        /* ARGSUSED */
 
11620
static int
 
11621
join_no_more_records(READ_RECORD *)
3554
11622
{
3555
11623
  return -1;
3556
11624
}
3557
11625
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
11626
static int
 
11627
join_read_next_same_diff(READ_RECORD *info)
3559
11628
{
3560
11629
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
11630
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
11631
  if (tab->insideout_match_tab->found_match)
3563
11632
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11633
    KEY *key= tab->table->key_info + tab->index;
3565
11634
    do
3566
11635
    {
3567
11636
      int error;
3568
11637
      /* Save index tuple from record to the buffer */
3569
11638
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
11639
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11640
      if ((error=table->file->index_next_same(table->record[0],
3572
11641
                                              tab->ref.key_buff,
3573
11642
                                              tab->ref.key_length)))
3574
11643
      {
3586
11655
    return join_read_next_same(info);
3587
11656
}
3588
11657
 
3589
 
int join_read_next_same(ReadRecord *info)
 
11658
static int
 
11659
join_read_next_same(READ_RECORD *info)
3590
11660
{
3591
11661
  int error;
3592
11662
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
11663
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
11664
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11665
  if ((error=table->file->index_next_same(table->record[0],
3596
11666
                                          tab->ref.key_buff,
3597
11667
                                          tab->ref.key_length)))
3598
11668
  {
3601
11671
    table->status= STATUS_GARBAGE;
3602
11672
    return -1;
3603
11673
  }
3604
 
 
 
11674
  update_virtual_fields_marked_for_write(table);
3605
11675
  return 0;
3606
11676
}
3607
11677
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
11678
 
 
11679
static int
 
11680
join_read_prev_same(READ_RECORD *info)
3609
11681
{
3610
11682
  int error;
3611
11683
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
11684
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
11685
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11686
  if ((error=table->file->index_prev(table->record[0])))
3615
11687
    return table->report_error(error);
3616
11688
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
11689
                      tab->ref.key_length))
3619
11691
    table->status=STATUS_NOT_FOUND;
3620
11692
    error= -1;
3621
11693
  }
 
11694
  update_virtual_fields_marked_for_write(table);
3622
11695
  return error;
3623
11696
}
3624
11697
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
11698
 
 
11699
static int
 
11700
join_init_quick_read_record(JOIN_TAB *tab)
3626
11701
{
3627
11702
  if (test_if_quick_select(tab) == -1)
3628
11703
    return -1;                                  /* No possible records */
3629
11704
  return join_init_read_record(tab);
3630
11705
}
3631
11706
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
11707
 
 
11708
int rr_sequential(READ_RECORD *info);
 
11709
int init_read_record_seq(JOIN_TAB *tab)
3633
11710
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
11711
  tab->read_record.read_record= rr_sequential;
 
11712
  if (tab->read_record.file->ha_rnd_init(1))
3637
11713
    return 1;
3638
11714
  return (*tab->read_record.read_record)(&tab->read_record);
3639
11715
}
3640
11716
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
11717
static int
 
11718
test_if_quick_select(JOIN_TAB *tab)
3642
11719
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
11720
  delete tab->select->quick;
 
11721
  tab->select->quick=0;
3645
11722
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11723
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11724
                                        false);
3647
11725
}
3648
11726
 
3649
 
int join_init_read_record(JoinTable *tab)
 
11727
 
 
11728
static int
 
11729
join_init_read_record(JOIN_TAB *tab)
3650
11730
{
3651
11731
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
11732
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
11733
  init_read_record(&tab->read_record, tab->join->session, tab->table,
 
11734
                   tab->select,1,1);
3657
11735
  return (*tab->read_record.read_record)(&tab->read_record);
3658
11736
}
3659
11737
 
3660
 
int join_read_first(JoinTable *tab)
 
11738
 
 
11739
static int
 
11740
join_read_first(JOIN_TAB *tab)
3661
11741
{
3662
11742
  int error;
3663
11743
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11744
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
11745
      !table->no_keyread)
3666
11746
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11747
    table->key_read=1;
 
11748
    table->file->extra(HA_EXTRA_KEYREAD);
3669
11749
  }
3670
 
  tab->table->status= 0;
 
11750
  tab->table->status=0;
3671
11751
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
11752
  tab->read_record.file=table->file;
3673
11753
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
11754
  tab->read_record.record=table->record[0];
3675
11755
  if (tab->insideout_match_tab)
3676
11756
  {
3677
11757
    tab->read_record.do_insideout_scan= tab;
3684
11764
    tab->read_record.do_insideout_scan= 0;
3685
11765
  }
3686
11766
 
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())))
 
11767
  if (!table->file->inited)
 
11768
    table->file->ha_index_init(tab->index, tab->sorted);
 
11769
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
11770
  {
3698
11771
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
11772
      table->report_error(error);
3700
11773
    return -1;
3701
11774
  }
3702
 
 
 
11775
  if (not error)
 
11776
    update_virtual_fields_marked_for_write(tab->table);
3703
11777
  return 0;
3704
11778
}
3705
11779
 
3706
 
int join_read_next_different(ReadRecord *info)
 
11780
 
 
11781
static int
 
11782
join_read_next_different(READ_RECORD *info)
3707
11783
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
11784
  JOIN_TAB *tab= info->do_insideout_scan;
3709
11785
  if (tab->insideout_match_tab->found_match)
3710
11786
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11787
    KEY *key= tab->table->key_info + tab->index;
3712
11788
    do
3713
11789
    {
3714
11790
      int error;
3715
11791
      /* Save index tuple from record to the buffer */
3716
11792
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
11793
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
 
11794
      if ((error=info->file->index_next(info->record)))
3719
11795
        return info->table->report_error(error);
 
11796
      if (not error)
 
11797
        update_virtual_fields_marked_for_write(tab->table);
3720
11798
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3721
11799
                      tab->insideout_buf, key->key_length));
3722
11800
    tab->insideout_match_tab->found_match= 0;
3726
11804
    return join_read_next(info);
3727
11805
}
3728
11806
 
3729
 
int join_read_next(ReadRecord *info)
 
11807
 
 
11808
static int
 
11809
join_read_next(READ_RECORD *info)
3730
11810
{
3731
11811
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
 
11812
  if ((error=info->file->index_next(info->record)))
3733
11813
    return info->table->report_error(error);
 
11814
  if (not error)
 
11815
    update_virtual_fields_marked_for_write(info->table);
3734
11816
  return 0;
3735
11817
}
3736
11818
 
3737
 
int join_read_last(JoinTable *tab)
 
11819
 
 
11820
static int
 
11821
join_read_last(JOIN_TAB *tab)
3738
11822
{
3739
11823
  Table *table=tab->table;
3740
11824
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11825
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
11826
      !table->no_keyread)
3743
11827
  {
3744
11828
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11829
    table->file->extra(HA_EXTRA_KEYREAD);
3746
11830
  }
3747
11831
  tab->table->status=0;
3748
11832
  tab->read_record.read_record=join_read_prev;
3749
11833
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
11834
  tab->read_record.file=table->file;
3751
11835
  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())))
 
11836
  tab->read_record.record=table->record[0];
 
11837
  if (!table->file->inited)
 
11838
    table->file->ha_index_init(tab->index, 1);
 
11839
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3760
11840
    return table->report_error(error);
3761
 
 
 
11841
  if (not error)
 
11842
    update_virtual_fields_marked_for_write(tab->table);
3762
11843
  return 0;
3763
11844
}
3764
11845
 
3765
 
int join_read_prev(ReadRecord *info)
 
11846
 
 
11847
static int
 
11848
join_read_prev(READ_RECORD *info)
3766
11849
{
3767
11850
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
 
11851
  if ((error= info->file->index_prev(info->record)))
3769
11852
    return info->table->report_error(error);
3770
 
 
 
11853
  if (not error)
 
11854
    update_virtual_fields_marked_for_write(info->table);
3771
11855
  return 0;
3772
11856
}
3773
11857
 
3774
11858
/**
3775
11859
  Reading of key with key reference and one part that may be NULL.
3776
11860
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
11861
 
 
11862
int
 
11863
join_read_always_key_or_null(JOIN_TAB *tab)
3778
11864
{
3779
11865
  int res;
3780
11866
 
3788
11874
  return safe_index_read(tab);
3789
11875
}
3790
11876
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
11877
 
 
11878
int
 
11879
join_read_next_same_or_null(READ_RECORD *info)
3792
11880
{
3793
11881
  int error;
3794
11882
  if ((error= join_read_next_same(info)) >= 0)
3795
11883
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11884
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
11885
 
3798
11886
  /* Test if we have already done a read after null key */
3799
11887
  if (*tab->ref.null_ref_key)
3802
11890
  return safe_index_read(tab);                  // then read null keys
3803
11891
}
3804
11892
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11893
 
 
11894
/*****************************************************************************
 
11895
  DESCRIPTION
 
11896
    Functions that end one nested loop iteration. Different functions
 
11897
    are used to support GROUP BY clause and to redirect records
 
11898
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11899
    network client.
 
11900
 
 
11901
  RETURN VALUES
 
11902
    NESTED_LOOP_OK           - the record has been successfully handled
 
11903
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11904
                               was detected
 
11905
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11906
                               the record
 
11907
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11908
                               additionally, the nested loop produced the
 
11909
                               number of rows specified in the LIMIT clause
 
11910
                               for the query
 
11911
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11912
                               additionally, there is a cursor and the nested
 
11913
                               loop algorithm produced the number of rows
 
11914
                               that is specified for current cursor fetch
 
11915
                               operation.
 
11916
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11917
*****************************************************************************/
 
11918
 
 
11919
/* ARGSUSED */
 
11920
static enum_nested_loop_state
 
11921
end_send(JOIN *join, JOIN_TAB *,
 
11922
         bool end_of_records)
 
11923
{
 
11924
  if (!end_of_records)
 
11925
  {
 
11926
    int error;
 
11927
    if (join->having && join->having->val_int() == 0)
 
11928
      return(NESTED_LOOP_OK);               // Didn't match having
 
11929
    error=0;
 
11930
    if (join->do_send_rows)
 
11931
      error=join->result->send_data(*join->fields);
 
11932
    if (error)
 
11933
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11934
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11935
        join->do_send_rows)
 
11936
    {
 
11937
      if (join->select_options & OPTION_FOUND_ROWS)
 
11938
      {
 
11939
        JOIN_TAB *jt=join->join_tab;
 
11940
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11941
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11942
            !(jt->select && jt->select->quick) &&
 
11943
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11944
            (jt->ref.key < 0))
 
11945
        {
 
11946
          /* Join over all rows in table;  Return number of found rows */
 
11947
          Table *table=jt->table;
 
11948
 
 
11949
          join->select_options ^= OPTION_FOUND_ROWS;
 
11950
          if (table->sort.record_pointers ||
 
11951
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11952
          {
 
11953
            /* Using filesort */
 
11954
            join->send_records= table->sort.found_records;
 
11955
          }
 
11956
          else
 
11957
          {
 
11958
            table->file->info(HA_STATUS_VARIABLE);
 
11959
            join->send_records= table->file->stats.records;
 
11960
          }
 
11961
        }
 
11962
        else
 
11963
        {
 
11964
          join->do_send_rows= 0;
 
11965
          if (join->unit->fake_select_lex)
 
11966
            join->unit->fake_select_lex->select_limit= 0;
 
11967
          return(NESTED_LOOP_OK);
 
11968
        }
 
11969
      }
 
11970
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11971
    }
 
11972
    else if (join->send_records >= join->fetch_limit)
 
11973
    {
 
11974
      /*
 
11975
        There is a server side cursor and all rows for
 
11976
        this fetch request are sent.
 
11977
      */
 
11978
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11979
    }
 
11980
  }
 
11981
 
 
11982
  return(NESTED_LOOP_OK);
 
11983
}
 
11984
 
 
11985
 
 
11986
/* ARGSUSED */
 
11987
enum_nested_loop_state
 
11988
end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
3806
11989
{
3807
11990
  int idx= -1;
3808
11991
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3815
11998
    {
3816
11999
      if (idx < (int) join->send_group_parts)
3817
12000
      {
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();
 
12001
        int error=0;
 
12002
        {
 
12003
          if (!join->first_record)
 
12004
          {
 
12005
            List_iterator_fast<Item> it(*join->fields);
 
12006
            Item *item;
 
12007
            /* No matching rows for group function */
 
12008
            join->clear();
3826
12009
 
3827
12010
            while ((item= it++))
3828
12011
              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;
 
12012
          }
 
12013
          if (join->having && join->having->val_int() == 0)
 
12014
            error= -1;                          // Didn't satisfy having
 
12015
          else
 
12016
          {
 
12017
            if (join->do_send_rows)
 
12018
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
12019
            join->send_records++;
 
12020
          }
 
12021
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
12022
          {
 
12023
            if (join->rollup_send_data((uint32_t) (idx+1)))
 
12024
              error= 1;
 
12025
          }
 
12026
        }
 
12027
        if (error > 0)
 
12028
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
12029
        if (end_of_records)
 
12030
          return(NESTED_LOOP_OK);
 
12031
        if (join->send_records >= join->unit->select_limit_cnt &&
 
12032
            join->do_send_rows)
 
12033
        {
 
12034
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
12035
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
12036
          join->do_send_rows=0;
 
12037
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
12038
        }
3856
12039
        else if (join->send_records >= join->fetch_limit)
3857
12040
        {
3870
12053
    else
3871
12054
    {
3872
12055
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
12056
        return(NESTED_LOOP_OK);
3874
12057
      join->first_record=1;
3875
12058
      test_if_item_cache_changed(join->group_fields);
3876
12059
    }
3882
12065
      */
3883
12066
      copy_fields(&join->tmp_table_param);
3884
12067
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
12068
        return(NESTED_LOOP_ERROR);
3886
12069
      return(ok_code);
3887
12070
    }
3888
12071
  }
3891
12074
  return(NESTED_LOOP_OK);
3892
12075
}
3893
12076
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12077
 
 
12078
/* ARGSUSED */
 
12079
enum_nested_loop_state
 
12080
end_write(JOIN *join, JOIN_TAB *,
 
12081
          bool end_of_records)
 
12082
{
 
12083
  Table *table=join->tmp_table;
 
12084
 
 
12085
  if (join->session->killed)                    // Aborted by user
 
12086
  {
 
12087
    join->session->send_kill_message();
 
12088
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12089
  }
 
12090
  if (!end_of_records)
 
12091
  {
 
12092
    copy_fields(&join->tmp_table_param);
 
12093
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12094
    if (!join->having || join->having->val_int())
 
12095
    {
 
12096
      int error;
 
12097
      join->found_records++;
 
12098
      if ((error=table->file->ha_write_row(table->record[0])))
 
12099
      {
 
12100
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12101
          goto end;
 
12102
        if (create_myisam_from_heap(join->session, table,
 
12103
                                    join->tmp_table_param.start_recinfo,
 
12104
                                    &join->tmp_table_param.recinfo,
 
12105
                                    error, 1))
 
12106
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12107
        table->s->uniques=0;                    // To ensure rows are the same
 
12108
      }
 
12109
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12110
          join->do_send_rows)
 
12111
      {
 
12112
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12113
          return(NESTED_LOOP_QUERY_LIMIT);
 
12114
        join->do_send_rows=0;
 
12115
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12116
        return(NESTED_LOOP_OK);
 
12117
      }
 
12118
    }
 
12119
  }
 
12120
end:
 
12121
  return(NESTED_LOOP_OK);
 
12122
}
 
12123
 
 
12124
/* ARGSUSED */
 
12125
/** Group by searching after group record and updating it if possible. */
 
12126
 
 
12127
static enum_nested_loop_state
 
12128
end_update(JOIN *join, JOIN_TAB *,
 
12129
           bool end_of_records)
 
12130
{
 
12131
  Table *table=join->tmp_table;
 
12132
  order_st   *group;
 
12133
  int     error;
 
12134
 
 
12135
  if (end_of_records)
 
12136
    return(NESTED_LOOP_OK);
 
12137
  if (join->session->killed)                    // Aborted by user
 
12138
  {
 
12139
    join->session->send_kill_message();
 
12140
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12141
  }
 
12142
 
 
12143
  join->found_records++;
 
12144
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12145
  /* Make a key of group index */
 
12146
  for (group=table->group ; group ; group=group->next)
 
12147
  {
 
12148
    Item *item= *group->item;
 
12149
    item->save_org_in_field(group->field);
 
12150
    /* Store in the used key if the field was 0 */
 
12151
    if (item->maybe_null)
 
12152
      group->buff[-1]= (char) group->field->is_null();
 
12153
  }
 
12154
  if (!table->file->index_read_map(table->record[1],
 
12155
                                   join->tmp_table_param.group_buff,
 
12156
                                   HA_WHOLE_KEY,
 
12157
                                   HA_READ_KEY_EXACT))
 
12158
  {                                             /* Update old record */
 
12159
    restore_record(table,record[1]);
 
12160
    update_tmptable_sum_func(join->sum_funcs,table);
 
12161
    if ((error=table->file->ha_update_row(table->record[1],
 
12162
                                          table->record[0])))
 
12163
    {
 
12164
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12165
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12166
    }
 
12167
    return(NESTED_LOOP_OK);
 
12168
  }
 
12169
 
 
12170
  /*
 
12171
    Copy null bits from group key to table
 
12172
    We can't copy all data as the key may have different format
 
12173
    as the row data (for example as with VARCHAR keys)
 
12174
  */
 
12175
  KEY_PART_INFO *key_part;
 
12176
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12177
       group ;
 
12178
       group=group->next,key_part++)
 
12179
  {
 
12180
    if (key_part->null_bit)
 
12181
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12182
  }
 
12183
  init_tmptable_sum_functions(join->sum_funcs);
 
12184
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12185
  if ((error=table->file->ha_write_row(table->record[0])))
 
12186
  {
 
12187
    if (create_myisam_from_heap(join->session, table,
 
12188
                                join->tmp_table_param.start_recinfo,
 
12189
                                &join->tmp_table_param.recinfo,
 
12190
                                error, 0))
 
12191
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12192
    /* Change method to update rows */
 
12193
    table->file->ha_index_init(0, 0);
 
12194
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12195
  }
 
12196
  join->send_records++;
 
12197
  return(NESTED_LOOP_OK);
 
12198
}
 
12199
 
 
12200
 
 
12201
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12202
 
 
12203
static enum_nested_loop_state
 
12204
end_unique_update(JOIN *join, JOIN_TAB *,
 
12205
                  bool end_of_records)
 
12206
{
 
12207
  Table *table=join->tmp_table;
 
12208
  int     error;
 
12209
 
 
12210
  if (end_of_records)
 
12211
    return(NESTED_LOOP_OK);
 
12212
  if (join->session->killed)                    // Aborted by user
 
12213
  {
 
12214
    join->session->send_kill_message();
 
12215
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12216
  }
 
12217
 
 
12218
  init_tmptable_sum_functions(join->sum_funcs);
 
12219
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12220
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12221
 
 
12222
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12223
    join->send_records++;                       // New group
 
12224
  else
 
12225
  {
 
12226
    if ((int) table->file->get_dup_key(error) < 0)
 
12227
    {
 
12228
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12229
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12230
    }
 
12231
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12232
    {
 
12233
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12234
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12235
    }
 
12236
    restore_record(table,record[1]);
 
12237
    update_tmptable_sum_func(join->sum_funcs,table);
 
12238
    if ((error=table->file->ha_update_row(table->record[1],
 
12239
                                          table->record[0])))
 
12240
    {
 
12241
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12242
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12243
    }
 
12244
  }
 
12245
  return(NESTED_LOOP_OK);
 
12246
}
 
12247
 
 
12248
 
 
12249
/* ARGSUSED */
 
12250
enum_nested_loop_state
 
12251
end_write_group(JOIN *join, JOIN_TAB *,
 
12252
                bool end_of_records)
3895
12253
{
3896
12254
  Table *table=join->tmp_table;
3897
12255
  int     idx= -1;
3898
12256
 
3899
 
  if (join->session->getKilled())
 
12257
  if (join->session->killed)
3900
12258
  {                                             // Aborted by user
3901
12259
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
12260
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
12261
  }
3904
12262
  if (!join->first_record || end_of_records ||
3905
12263
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
12267
      int send_group_parts= join->send_group_parts;
3910
12268
      if (idx < send_group_parts)
3911
12269
      {
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;
 
12270
        if (!join->first_record)
 
12271
        {
 
12272
          /* No matching rows for group function */
 
12273
          join->clear();
 
12274
        }
 
12275
        copy_sum_funcs(join->sum_funcs,
 
12276
                       join->sum_funcs_end[send_group_parts]);
 
12277
        if (!join->having || join->having->val_int())
 
12278
        {
 
12279
          int error= table->file->ha_write_row(table->record[0]);
 
12280
          if (error && create_myisam_from_heap(join->session, table,
 
12281
                                               join->tmp_table_param.start_recinfo,
 
12282
                                                &join->tmp_table_param.recinfo,
 
12283
                                               error, 0))
 
12284
            return(NESTED_LOOP_ERROR);
 
12285
        }
 
12286
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
12287
        {
 
12288
          if (join->rollup_write_data((uint32_t) (idx+1), table))
 
12289
            return(NESTED_LOOP_ERROR);
 
12290
        }
 
12291
        if (end_of_records)
 
12292
          return(NESTED_LOOP_OK);
3935
12293
      }
3936
12294
    }
3937
12295
    else
3938
12296
    {
3939
12297
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
12298
        return(NESTED_LOOP_OK);
3941
12299
      join->first_record=1;
3942
12300
      test_if_item_cache_changed(join->group_fields);
3943
12301
    }
3944
12302
    if (idx < (int) join->send_group_parts)
3945
12303
    {
3946
12304
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
12305
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
12306
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
12307
        return(NESTED_LOOP_ERROR);
 
12308
      return(NESTED_LOOP_OK);
3952
12309
    }
3953
12310
  }
3954
12311
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
12312
    return(NESTED_LOOP_ERROR);
 
12313
  return(NESTED_LOOP_OK);
3957
12314
}
3958
12315
 
 
12316
 
3959
12317
/*****************************************************************************
3960
12318
  Remove calculation with tables that aren't yet read. Remove also tests
3961
12319
  against fields that are read through key where the table is not a
3962
12320
  outer join table.
3963
12321
  We can't remove tests that are made against columns which are stored
3964
12322
  in sorted order.
 
12323
*****************************************************************************/
 
12324
 
 
12325
/**
3965
12326
  @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)
 
12327
    1 if right_item is used removable reference key on left_item
 
12328
*/
 
12329
 
 
12330
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
12331
{
3971
12332
  Field *field=left_item->field;
3972
12333
  // 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)
 
12334
  if (!field->table->const_table && !field->table->maybe_null)
3974
12335
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12336
    Item *ref_item=part_of_refkey(field->table,field);
3976
12337
    if (ref_item && ref_item->eq(right_item,1))
3977
12338
    {
3978
12339
      right_item= right_item->real_item();
3979
12340
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12341
        return (field->eq_def(((Item_field *) right_item)->field));
3981
12342
      /* remove equalities injected by IN->EXISTS transformation */
3982
12343
      else if (right_item->type() == Item::CACHE_ITEM)
3983
12344
        return ((Item_cache *)right_item)->eq_def (field);
3984
12345
      if (right_item->const_item() && !(right_item->is_null()))
3985
12346
      {
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
 
12347
        /*
 
12348
          We can remove binary fields and numerical fields except float,
 
12349
          as float comparison isn't 100 % secure
 
12350
          We have to keep normal strings to be able to check for end spaces
3990
12351
 
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
 
        }
 
12352
          sergefp: the above seems to be too restrictive. Counterexample:
 
12353
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12354
            insert into t100 values ('a'),('a ');
 
12355
            explain select * from t100 where v='a';
 
12356
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12357
          rows, so it seems there are no problems with endspace in the most
 
12358
          frequent case?
 
12359
        */
 
12360
        if (field->binary() &&
 
12361
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12362
            field->decimals() == 0)
 
12363
        {
 
12364
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12365
        }
4005
12366
      }
4006
12367
    }
4007
12368
  }
4008
 
  return 0;
 
12369
  return 0;                                     // keep test
 
12370
}
 
12371
 
 
12372
/**
 
12373
   @brief Replaces an expression destructively inside the expression tree of
 
12374
   the WHERE clase.
 
12375
 
 
12376
   @note Because of current requirements for semijoin flattening, we do not
 
12377
   need to recurse here, hence this function will only examine the top-level
 
12378
   AND conditions. (see JOIN::prepare, comment above the line
 
12379
   'if (do_materialize)'
 
12380
 
 
12381
   @param join The top-level query.
 
12382
   @param old_cond The expression to be replaced.
 
12383
   @param new_cond The expression to be substituted.
 
12384
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
 
12385
   the new expression.
 
12386
   @return <code>true</code> if there was an error, <code>false</code> if
 
12387
   successful.
 
12388
*/
 
12389
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
12390
                                       Item *new_cond, bool do_fix_fields)
 
12391
{
 
12392
  if (join->conds == old_cond) {
 
12393
    join->conds= new_cond;
 
12394
    if (do_fix_fields)
 
12395
      new_cond->fix_fields(join->session, &join->conds);
 
12396
    return false;
 
12397
  }
 
12398
 
 
12399
  if (join->conds->type() == Item::COND_ITEM) {
 
12400
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12401
    Item *item;
 
12402
    while ((item= li++))
 
12403
      if (item == old_cond)
 
12404
      {
 
12405
        li.replace(new_cond);
 
12406
        if (do_fix_fields)
 
12407
          new_cond->fix_fields(join->session, li.ref());
 
12408
        return false;
 
12409
      }
 
12410
  }
 
12411
 
 
12412
  return true;
4009
12413
}
4010
12414
 
4011
12415
/*
4039
12443
  RETURN
4040
12444
    Extracted condition
4041
12445
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12446
 
 
12447
static COND *
 
12448
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12449
                    bool exclude_expensive_cond)
4043
12450
{
4044
12451
  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()))
 
12452
      /*
 
12453
        Exclude constant conditions not checked at optimization time if
 
12454
        the table we are pushing conditions to is the first one.
 
12455
        As a result, such conditions are not considered as already checked
 
12456
        and will be checked at execution time, attached to the first table.
 
12457
      */
 
12458
      !((used_table & 1) && cond->is_expensive()))
4052
12459
    return (COND*) 0;                           // Already checked
4053
12460
  if (cond->type() == Item::COND_ITEM)
4054
12461
  {
4057
12464
      /* Create new top level AND item */
4058
12465
      Item_cond_and *new_cond=new Item_cond_and;
4059
12466
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12467
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12468
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
12469
      Item *item;
4063
12470
      while ((item=li++))
4064
12471
      {
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);
 
12472
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12473
                                      exclude_expensive_cond);
 
12474
        if (fix)
 
12475
          new_cond->argument_list()->push_back(fix);
4069
12476
      }
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;
 
12477
      switch (new_cond->argument_list()->elements) {
 
12478
      case 0:
 
12479
        return (COND*) 0;                       // Always true
 
12480
      case 1:
 
12481
        return new_cond->argument_list()->head();
 
12482
      default:
 
12483
        /*
 
12484
          Item_cond_and do not need fix_fields for execution, its parameters
 
12485
          are fixed or do not need fix_fields, too
 
12486
        */
 
12487
        new_cond->quick_fix_field();
 
12488
        new_cond->used_tables_cache=
 
12489
          ((Item_cond_and*) cond)->used_tables_cache &
 
12490
          tables;
 
12491
        return new_cond;
4084
12492
      }
4085
12493
    }
4086
12494
    else
4087
12495
    {                                           // Or list
4088
12496
      Item_cond_or *new_cond=new Item_cond_or;
4089
12497
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12498
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12499
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
12500
      Item *item;
4093
12501
      while ((item=li++))
4094
12502
      {
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);
 
12503
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12504
        if (!fix)
 
12505
          return (COND*) 0;                     // Always true
 
12506
        new_cond->argument_list()->push_back(fix);
4099
12507
      }
4100
12508
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
12509
        Item_cond_and do not need fix_fields for execution, its parameters
 
12510
        are fixed or do not need fix_fields, too
4103
12511
      */
4104
12512
      new_cond->quick_fix_field();
4105
12513
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4132
12540
  {
4133
12541
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
12542
    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))
 
12543
    if (left_item->type() == Item::FIELD_ITEM &&
 
12544
        test_if_ref((Item_field*) left_item,right_item))
4136
12545
    {
4137
12546
      cond->marker=3;                   // Checked when read
4138
12547
      return (COND*) 0;
4139
12548
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12549
    if (right_item->type() == Item::FIELD_ITEM &&
 
12550
        test_if_ref((Item_field*) right_item,left_item))
4141
12551
    {
4142
12552
      cond->marker=3;                   // Checked when read
4143
12553
      return (COND*) 0;
4147
12557
  return cond;
4148
12558
}
4149
12559
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
12560
 
 
12561
static Item *
 
12562
part_of_refkey(Table *table,Field *field)
4151
12563
{
4152
12564
  if (!table->reginfo.join_tab)
4153
12565
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4155
12567
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
12568
  if (ref_parts)
4157
12569
  {
4158
 
    KeyPartInfo *key_part=
 
12570
    KEY_PART_INFO *key_part=
4159
12571
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
12572
    uint32_t part;
4161
12573
 
4166
12578
    }
4167
12579
 
4168
12580
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
12581
      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
 
      {
 
12582
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
12583
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
12584
  }
4180
12585
  return (Item*) 0;
4181
12586
}
4182
12587
 
 
12588
 
4183
12589
/**
4184
12590
  Test if one can use the key to resolve order_st BY.
4185
12591
 
4200
12606
  @retval
4201
12607
    -1   Reverse key can be used
4202
12608
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12609
 
 
12610
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12611
                                uint32_t *used_key_parts)
4204
12612
{
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;
 
12613
  KEY_PART_INFO *key_part,*key_part_end;
 
12614
  key_part=table->key_info[idx].key_part;
 
12615
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
12616
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
12617
  int reverse=0;
4211
12618
  bool on_primary_key= false;
4212
12619
 
4213
12620
  for (; order ; order=order->next, const_key_parts>>=1)
4217
12624
 
4218
12625
    /*
4219
12626
      Skip key parts that are constants in the WHERE clause.
4220
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12627
      These are already skipped in the order_st BY by const_expression_in_where()
4221
12628
    */
4222
12629
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
12630
      key_part++;
4230
12637
        the primary key as a suffix.
4231
12638
      */
4232
12639
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
12640
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12641
          table->s->primary_key != MAX_KEY)
4235
12642
      {
4236
12643
        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()];
 
12644
        key_part= table->key_info[table->s->primary_key].key_part;
 
12645
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12646
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
12647
 
4241
12648
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
12649
          key_part++;
4264
12671
  }
4265
12672
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4266
12673
    (uint32_t) (key_part - table->key_info[idx].key_part);
4267
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
12674
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
12675
                         HA_READ_PREV))
4269
12676
    reverse= 0;                                 // Index can't be used
4270
12677
  return(reverse);
4271
12678
}
4272
12679
 
 
12680
 
4273
12681
/**
4274
12682
  Test if a second key is the subkey of the first one.
4275
12683
 
4285
12693
  @retval
4286
12694
    0   no sub key
4287
12695
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
12696
 
 
12697
inline bool
 
12698
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12699
          KEY_PART_INFO *ref_key_part_end)
4291
12700
{
4292
12701
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
12702
    if (!key_part->field->eq(ref_key_part->field))
4294
12703
      return 0;
4295
12704
  return 1;
4296
12705
}
4306
12715
    - MAX_KEY                   If we can't use other key
4307
12716
    - the number of found key   Otherwise
4308
12717
*/
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)
 
12718
 
 
12719
static uint
 
12720
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12721
               const key_map *usable_keys)
4314
12722
{
4315
12723
  uint32_t nr;
4316
12724
  uint32_t min_length= UINT32_MAX;
4317
12725
  uint32_t best= MAX_KEY;
4318
12726
  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;
 
12727
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12728
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
12729
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12730
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
12731
  {
4324
 
    if (usable_keys->test(nr) &&
 
12732
    if (usable_keys->is_set(nr) &&
4325
12733
        table->key_info[nr].key_length < min_length &&
4326
12734
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
12735
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
12743
  return best;
4336
12744
}
4337
12745
 
 
12746
 
4338
12747
/**
4339
12748
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
12749
  already known to be distinct.
4366
12775
  @retval
4367
12776
    0                    not found.
4368
12777
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12778
 
 
12779
static bool
 
12780
list_contains_unique_index(Table *table,
 
12781
                          bool (*find_func) (Field *, void *), void *data)
4370
12782
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12783
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
4372
12784
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12785
    if (keynr == table->s->primary_key ||
4374
12786
         (table->key_info[keynr].flags & HA_NOSAME))
4375
12787
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
12788
      KEY *keyinfo= table->key_info + keynr;
 
12789
      KEY_PART_INFO *key_part, *key_part_end;
4379
12790
 
4380
12791
      for (key_part=keyinfo->key_part,
4381
12792
           key_part_end=key_part+ keyinfo->key_parts;
4383
12794
           key_part++)
4384
12795
      {
4385
12796
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
12797
            !find_func(key_part->field, data))
4387
12798
          break;
4388
12799
      }
4389
12800
      if (key_part == key_part_end)
4393
12804
  return 0;
4394
12805
}
4395
12806
 
 
12807
 
4396
12808
/**
4397
12809
  Helper function for list_contains_unique_index.
4398
12810
  Find a field reference in a list of order_st structures.
4406
12818
  @retval
4407
12819
    0                    not found.
4408
12820
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
12821
 
 
12822
static bool
 
12823
find_field_in_order_list (Field *field, void *data)
4410
12824
{
4411
 
  Order *group= (Order *) data;
 
12825
  order_st *group= (order_st *) data;
4412
12826
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12827
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
12828
  {
4415
12829
    Item *item= (*tmp_group->item)->real_item();
4416
12830
    if (item->type() == Item::FIELD_ITEM &&
4423
12837
  return part_found;
4424
12838
}
4425
12839
 
 
12840
 
4426
12841
/**
4427
12842
  Helper function for list_contains_unique_index.
4428
12843
  Find a field reference in a dynamic list of Items.
4436
12851
  @retval
4437
12852
    0                    not found.
4438
12853
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
12854
 
 
12855
static bool
 
12856
find_field_in_item_list (Field *field, void *data)
4440
12857
{
4441
12858
  List<Item> *fields= (List<Item> *) data;
4442
12859
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
12860
  List_iterator<Item> li(*fields);
4444
12861
  Item *item;
4445
12862
 
4446
12863
  while ((item= li++))
4455
12872
  return part_found;
4456
12873
}
4457
12874
 
 
12875
 
4458
12876
/**
4459
 
  Test if we can skip the ORDER BY by using an index.
 
12877
  Test if we can skip the order_st BY by using an index.
4460
12878
 
4461
12879
  SYNOPSIS
4462
12880
    test_if_skip_sort_order()
4466
12884
      no_changes
4467
12885
      map
4468
12886
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
12887
  If we can use an index, the JOIN_TAB / tab->select struct
4470
12888
  is changed to use the index.
4471
12889
 
4472
12890
  The index must cover all fields in <order>, or it will not be considered.
4480
12898
  @retval
4481
12899
    1    We can use an index.
4482
12900
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12901
 
 
12902
static bool
 
12903
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12904
                        bool no_changes, const key_map *map)
4484
12905
{
4485
12906
  int32_t ref_key;
4486
12907
  uint32_t ref_key_parts;
4487
12908
  int order_direction;
4488
12909
  uint32_t used_key_parts;
4489
12910
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
12911
  SQL_SELECT *select=tab->select;
4491
12912
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12913
  QUICK_SELECT_I *save_quick= 0;
4493
12914
 
4494
12915
  /*
4495
12916
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4497
12918
  */
4498
12919
  usable_keys= *map;
4499
12920
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12921
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
12922
  {
4502
12923
    Item *item= (*tmp_order->item)->real_item();
4503
12924
    if (item->type() != Item::FIELD_ITEM)
4504
12925
    {
4505
 
      usable_keys.reset();
 
12926
      usable_keys.clear_all();
4506
12927
      return(0);
4507
12928
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
12929
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12930
    if (usable_keys.is_clear_all())
4510
12931
      return(0);                                        // No usable keys
4511
12932
  }
4512
12933
 
4516
12937
  {
4517
12938
    ref_key=       tab->ref.key;
4518
12939
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
12940
    if (tab->type == JT_REF_OR_NULL)
4520
12941
      return(0);
4521
12942
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12943
  else if (select && select->quick)             // Range found by opt_range
4523
12944
  {
4524
12945
    int quick_type= select->quick->get_type();
4525
12946
    save_quick= select->quick;
4526
12947
    /*
4527
12948
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
12949
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
12950
      by clustered PK values.
4530
12951
    */
4531
12952
 
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)
 
12953
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
12954
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
12955
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
12956
      return(0);
4536
12957
    ref_key=       select->quick->index;
4537
12958
    ref_key_parts= select->quick->used_key_parts;
4542
12963
    /*
4543
12964
      We come here when there is a REF key.
4544
12965
    */
4545
 
    if (! usable_keys.test(ref_key))
 
12966
    if (!usable_keys.is_set(ref_key))
4546
12967
    {
4547
12968
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
12969
        We come here when ref_key is not among usable_keys
4549
12970
      */
4550
12971
      uint32_t new_ref_key;
4551
12972
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
12973
        If using index only read, only consider other possible index only
 
12974
        keys
4554
12975
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
12976
      if (table->covering_keys.is_set(ref_key))
 
12977
        usable_keys.intersect(table->covering_keys);
4557
12978
      if (tab->pre_idx_push_select_cond)
4558
12979
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
12980
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
12981
                                       &usable_keys)) < MAX_KEY)
4561
12982
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
12983
        /* Found key that can be used to retrieve data in sorted order */
 
12984
        if (tab->ref.key >= 0)
 
12985
        {
4565
12986
          /*
4566
12987
            We'll use ref access method on key new_ref_key. In general case
4567
12988
            the index search tuple for new_ref_key will be different (e.g.
4570
12991
            "part1 = const1 AND part2=const2".
4571
12992
            So we build tab->ref from scratch here.
4572
12993
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12994
          KEYUSE *keyuse= tab->keyuse;
 
12995
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
12996
            keyuse++;
4576
12997
 
4577
12998
          if (create_ref_for_key(tab->join, tab, keyuse,
4578
12999
                                 tab->join->const_table_map))
4579
13000
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
13001
        }
 
13002
        else
 
13003
        {
4583
13004
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
13005
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
13006
            we want to use instead new_ref_key as the index. We can't
4586
13007
            just change the index of the quick select, because this may
4587
13008
            result in an incosistent QUICK_SELECT object. Below we
4589
13010
            parameres are set correctly by the range optimizer.
4590
13011
           */
4591
13012
          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.
 
13013
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
13014
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
13015
 
4595
13016
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
4596
13017
                                        (tab->join->select_options &
4600
13021
                                        true) <=
4601
13022
              0)
4602
13023
            return(0);
4603
 
        }
 
13024
        }
4604
13025
        ref_key= new_ref_key;
4605
13026
      }
4606
13027
    }
4607
13028
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
13029
    if (usable_keys.is_set(ref_key) &&
4609
13030
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
13031
                                               &used_key_parts)))
4611
13032
      goto check_reverse_order;
4626
13047
    int best_key= -1;
4627
13048
    bool is_best_covering= false;
4628
13049
    double fanout= 1;
4629
 
    Join *join= tab->join;
 
13050
    JOIN *join= tab->join;
4630
13051
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
13052
    ha_rows table_records= table->file->stats.records;
4632
13053
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
13054
 
4635
13055
    /*
4636
13056
      If not used with LIMIT, only use keys if the whole query can be
4643
13063
        filesort() and join cache are usually faster than reading in
4644
13064
        index order and not using join cache
4645
13065
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13066
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
13067
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
13068
      keys= *table->file->keys_to_use_for_scanning();
 
13069
      keys.merge(table->covering_keys);
4650
13070
 
4651
13071
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
 
13072
        We are adding here also the index specified in FORCE INDEX clause,
 
13073
        if any.
4654
13074
        This is to allow users to use index in order_st BY.
4655
13075
      */
4656
13076
      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;
 
13077
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13078
                           table->keys_in_use_for_order_by);
 
13079
      keys.intersect(usable_keys);
4660
13080
    }
4661
13081
    else
4662
13082
      keys= usable_keys;
4663
13083
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
 
13084
    read_time= join->best_positions[tablenr].read_time;
4666
13085
    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
 
    }
 
13086
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
13087
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13088
    for (nr=0; nr < table->s->keys ; nr++)
4673
13089
    {
4674
13090
      int direction;
4675
 
      if (keys.test(nr) &&
 
13091
      if (keys.is_set(nr) &&
4676
13092
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
13093
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
 
13094
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
4679
13095
 
4680
13096
        /*
4681
 
          Don't use an index scan with ORDER BY without limit.
 
13097
          Don't use an index scan with order_st BY without limit.
4682
13098
          For GROUP BY without limit always use index scan
4683
13099
          if there is a suitable index.
4684
13100
          Why we hold to this asymmetry hardly can be explained
4685
13101
          rationally. It's easy to demonstrate that using
4686
13102
          temporary table + filesort could be cheaper for grouping
4687
13103
          queries too.
4688
 
        */
 
13104
        */
4689
13105
        if (is_covering ||
4690
13106
            select_limit != HA_POS_ERROR ||
4691
13107
            (ref_key < 0 && (group || table->force_index)))
4692
13108
        {
4693
13109
          double rec_per_key;
4694
13110
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13111
          KEY *keyinfo= tab->table->key_info+nr;
4696
13112
          if (select_limit == HA_POS_ERROR)
4697
13113
            select_limit= table_records;
4698
13114
          if (group)
4699
13115
          {
4700
13116
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
13117
            set_if_bigger(rec_per_key, 1);
4702
13118
            /*
4703
13119
              With a grouping query each group containing on average
4704
13120
              rec_per_key records produces only one row that will
4705
13121
              be included into the result set.
4706
 
            */
 
13122
            */
4707
13123
            if (select_limit > table_records/rec_per_key)
4708
13124
                select_limit= table_records;
4709
13125
            else
4718
13134
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
13135
            and as result we'll choose an index scan when using ref/range
4720
13136
            access + filesort will be cheaper.
4721
 
          */
 
13137
          */
4722
13138
          select_limit= (ha_rows) (select_limit < fanout ?
4723
13139
                                   1 : select_limit/fanout);
4724
13140
          /*
4738
13154
                                     (double) table_records /
4739
13155
                                      table->quick_condition_rows);
4740
13156
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
13157
          set_if_bigger(rec_per_key, 1);
4742
13158
          /*
4743
13159
            Here we take into account the fact that rows are
4744
13160
            accessed in sequences rec_per_key records in each.
4745
13161
            Rows in such a sequence are supposed to be ordered
4746
13162
            by rowid/primary key. When reading the data
4747
13163
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
13164
            table file contains.
4749
13165
            TODO. Use the formula for a disk sweep sequential access
4750
13166
            to calculate the cost of accessing data rows for one
4751
13167
            index entry.
4752
 
          */
 
13168
          */
4753
13169
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
13170
                           cmin(rec_per_key, table->file->scan_time());
4755
13171
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
13172
              index_scan_time < read_time)
4757
13173
          {
4758
13174
            ha_rows quick_records= table_records;
4759
13175
            if (is_best_covering && !is_covering)
4760
13176
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
13177
            if (table->quick_keys.is_set(nr))
4762
13178
              quick_records= table->quick_rows[nr];
4763
13179
            if (best_key < 0 ||
4764
 
                (select_limit <= min(quick_records,best_records) ?
 
13180
                (select_limit <= cmin(quick_records,best_records) ?
4765
13181
                 keyinfo->key_parts < best_key_parts :
4766
13182
                 quick_records < best_records))
4767
13183
            {
4772
13188
              best_key_direction= direction;
4773
13189
            }
4774
13190
          }
4775
 
        }
 
13191
        }
4776
13192
      }
4777
13193
    }
4778
13194
    if (best_key >= 0)
4779
13195
    {
4780
13196
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13197
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
13198
      {
4783
13199
        key_map test_map;
4784
 
        test_map.reset();       // Force the creation of quick select
4785
 
        test_map.set(best_key); // only best_key.
 
13200
        test_map.clear_all();       // Force the creation of quick select
 
13201
        test_map.set_bit(best_key); // only best_key.
4786
13202
        quick_created=
4787
13203
          select->test_quick_select(join->session, test_map, 0,
4788
13204
                                    join->select_options & OPTION_FOUND_ROWS ?
4793
13209
      if (!no_changes)
4794
13210
      {
4795
13211
        if (!quick_created)
4796
 
        {
 
13212
        {
4797
13213
          tab->index= best_key;
4798
13214
          tab->read_first_record= best_key_direction > 0 ?
4799
13215
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13216
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
13217
          if (select && select->quick)
4802
13218
          {
4803
 
            safe_delete(select->quick);
 
13219
            delete select->quick;
 
13220
            select->quick= 0;
4804
13221
          }
4805
 
          if (table->covering_keys.test(best_key))
 
13222
          if (table->covering_keys.is_set(best_key))
4806
13223
          {
4807
13224
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13225
            table->file->extra(HA_EXTRA_KEYREAD);
4809
13226
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
13227
          table->file->ha_index_or_rnd_end();
4811
13228
          if (join->select_options & SELECT_DESCRIBE)
4812
13229
          {
4813
13230
            tab->ref.key= -1;
4816
13233
              tab->limit= select_limit;
4817
13234
          }
4818
13235
        }
4819
 
        else if (tab->type != AM_ALL)
 
13236
        else if (tab->type != JT_ALL)
4820
13237
        {
4821
13238
          /*
4822
13239
            We're about to use a quick access to the table.
4824
13241
            method is actually used.
4825
13242
          */
4826
13243
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
13244
          tab->type=JT_ALL;
4828
13245
          tab->use_quick=1;
4829
13246
          tab->ref.key= -1;
4830
13247
          tab->ref.key_parts=0;         // Don't use ref key.
4831
13248
          tab->read_first_record= join_init_read_record;
 
13249
          /*
 
13250
            TODO: update the number of records in join->best_positions[tablenr]
 
13251
          */
4832
13252
        }
4833
13253
      }
4834
13254
      used_key_parts= best_key_parts;
4839
13259
  }
4840
13260
 
4841
13261
check_reverse_order:
4842
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13262
  if (order_direction == -1)            // If order_st BY ... DESC
4843
13263
  {
4844
13264
    if (select && select->quick)
4845
13265
    {
4846
13266
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
13267
        Don't reverse the sort order, if it's already done.
4848
13268
        (In some cases test_if_order_by_key() can be called multiple times
4849
13269
      */
4850
 
      if (! select->quick->reverse_sorted())
 
13270
      if (!select->quick->reverse_sorted())
4851
13271
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13272
        QUICK_SELECT_DESC *tmp;
4853
13273
        bool error= false;
4854
13274
        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)
 
13275
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13276
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13277
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13278
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4859
13279
        {
4860
13280
          tab->limit= 0;
4861
13281
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
 
13282
          return(0);                   // Use filesort
4863
13283
        }
4864
13284
 
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;
 
13285
        /* order_st BY range_key DESC */
 
13286
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13287
                                    used_key_parts, &error);
 
13288
        if (!tmp || error)
 
13289
        {
 
13290
          delete tmp;
4872
13291
          select->quick= save_quick;
4873
13292
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
13293
          return(0);            // Reverse sort not supported
 
13294
        }
 
13295
        select->quick=tmp;
4877
13296
      }
4878
13297
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
13298
    else if (tab->type != JT_NEXT &&
4880
13299
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
13300
    {
4882
13301
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13302
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4884
13303
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
13304
        Use a traversal function that starts by reading the last row
 
13305
        with key part (A) and then traverse the index backwards.
4887
13306
      */
4888
13307
      tab->read_first_record= join_read_last_key;
4889
13308
      tab->read_record.read_record= join_read_prev_same;
4891
13310
  }
4892
13311
  else if (select && select->quick)
4893
13312
    select->quick->sorted= 1;
4894
 
  return 1;
 
13313
  return(1);
4895
13314
}
4896
13315
 
 
13316
 
4897
13317
/*
4898
13318
  If not selecting by given key, create an index how records should be read
4899
13319
 
4900
13320
  SYNOPSIS
4901
13321
   create_sort_index()
4902
 
     session            Thread Cursor
 
13322
     session            Thread handler
4903
13323
     tab                Table to sort (in join structure)
4904
13324
     order              How table should be sorted
4905
13325
     filesort_limit     Max number of rows that needs to be sorted
4912
13332
  IMPLEMENTATION
4913
13333
   - If there is an index that can be used, 'tab' is modified to use
4914
13334
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
13335
   - If no index, create with filesort() an index file that can be used to
4916
13336
     retrieve rows in order (should be done with 'read_record').
4917
13337
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
13338
     free_io_cache(tab->table).
4919
13339
 
4920
13340
  RETURN VALUES
4921
13341
    0           ok
4922
13342
    -1          Some fatal error
4923
13343
    1           No records
4924
13344
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13345
 
 
13346
static int
 
13347
create_sort_index(Session *session, JOIN *join, order_st *order,
 
13348
                  ha_rows filesort_limit, ha_rows select_limit,
 
13349
                  bool is_order_by)
4926
13350
{
4927
13351
  uint32_t length= 0;
4928
13352
  ha_rows examined_rows;
4929
13353
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
13354
  SQL_SELECT *select;
 
13355
  JOIN_TAB *tab;
4932
13356
 
4933
13357
  if (join->tables == join->const_tables)
4934
13358
    return(0);                          // One row, no need to sort
4944
13368
  */
4945
13369
  if ((order != join->group_list ||
4946
13370
       !(join->select_options & SELECT_BIG_RESULT) ||
4947
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
 
13371
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
4948
13372
      test_if_skip_sort_order(tab,order,select_limit,0,
4949
13373
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
13374
                              &table->keys_in_use_for_group_by))
4951
13375
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13376
  for (order_st *ord= join->order; ord; ord= ord->next)
4953
13377
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
13378
  if (!(join->sortorder=
 
13379
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13380
    goto err;                           /* purecov: inspected */
4958
13381
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13382
  table->sort.io_cache= new IO_CACHE;
 
13383
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
4960
13384
  table->status=0;                              // May be wrong if quick_select
4961
13385
 
4962
13386
  // If table has a range, move it to select
4972
13396
      */
4973
13397
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
4974
13398
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13399
        table->key_read=0;
 
13400
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
13401
      }
4978
13402
    }
4979
13403
    else
4980
13404
    {
4981
13405
      /*
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.
 
13406
        We have a ref on a const;  Change this to a range that filesort
 
13407
        can use.
 
13408
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13409
        field, quick will contain an empty record set.
4986
13410
      */
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
 
      }
 
13411
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
 
13412
                                                     tab->found_records))))
 
13413
        goto err;
4994
13414
    }
4995
13415
  }
4996
13416
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13417
  /* Fill schema tables with data before filesort if it's necessary */
 
13418
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13419
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13420
    goto err;
4999
13421
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
13422
  if (table->s->tmp_table)
 
13423
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13424
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
13425
                                     select, filesort_limit, 0,
 
13426
                                     &examined_rows);
5004
13427
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
13428
  if (select)
5006
13429
  {
5010
13433
  tab->select_cond=0;
5011
13434
  tab->last_inner= 0;
5012
13435
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13436
  tab->type=JT_ALL;                             // Read with normal read_record
5014
13437
  tab->read_first_record= join_init_read_record;
5015
13438
  tab->join->examined_rows+=examined_rows;
5016
13439
  if (table->key_read)                          // Restore if we used indexes
5017
13440
  {
5018
13441
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13442
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
13443
  }
5021
 
 
5022
13444
  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;
 
13445
err:
 
13446
  return(-1);
 
13447
}
 
13448
 
 
13449
static bool copy_blobs(Field **ptr)
 
13450
{
 
13451
  for (; *ptr ; ptr++)
 
13452
  {
 
13453
    if ((*ptr)->flags & BLOB_FLAG)
 
13454
      if (((Field_blob *) (*ptr))->copy())
 
13455
        return 1;                               // Error
 
13456
  }
 
13457
  return 0;
 
13458
}
 
13459
 
 
13460
static void free_blobs(Field **ptr)
 
13461
{
 
13462
  for (; *ptr ; ptr++)
 
13463
  {
 
13464
    if ((*ptr)->flags & BLOB_FLAG)
 
13465
      ((Field_blob *) (*ptr))->free();
 
13466
  }
 
13467
}
 
13468
 
 
13469
 
 
13470
static int
 
13471
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13472
{
 
13473
  int error;
 
13474
  uint32_t reclength,offset;
 
13475
  uint32_t field_count;
 
13476
  Session *session= join->session;
 
13477
 
 
13478
  entry->reginfo.lock_type=TL_WRITE;
 
13479
 
 
13480
  /* Calculate how many saved fields there is in list */
 
13481
  field_count=0;
 
13482
  List_iterator<Item> it(fields);
 
13483
  Item *item;
 
13484
  while ((item=it++))
 
13485
  {
 
13486
    if (item->get_tmp_table_field() && ! item->const_item())
 
13487
      field_count++;
 
13488
  }
 
13489
 
 
13490
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
 
13491
  {                    // only const items with no OPTION_FOUND_ROWS
 
13492
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13493
    return(0);
 
13494
  }
 
13495
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13496
  offset= (field_count ?
 
13497
           entry->field[entry->s->fields - field_count]->
 
13498
           offset(entry->record[0]) : 0);
 
13499
  reclength= entry->s->reclength-offset;
 
13500
 
 
13501
  free_io_cache(entry);                         // Safety
 
13502
  entry->file->info(HA_STATUS_VARIABLE);
 
13503
  if (entry->s->db_type() == heap_hton ||
 
13504
      (!entry->s->blob_fields &&
 
13505
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13506
        session->variables.sortbuff_size)))
 
13507
    error= remove_dup_with_hash_index(join->session, entry,
 
13508
                                     field_count, first_field,
 
13509
                                     reclength, having);
 
13510
  else
 
13511
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
 
13512
                                  having);
 
13513
 
 
13514
  free_blobs(first_field);
 
13515
  return(error);
 
13516
}
 
13517
 
 
13518
 
 
13519
static int remove_dup_with_compare(Session *session, Table *table, Field **first_field,
 
13520
                                   uint32_t offset, Item *having)
 
13521
{
 
13522
  handler *file=table->file;
5028
13523
  char *org_record,*new_record;
5029
13524
  unsigned char *record;
5030
13525
  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);
 
13526
  uint32_t reclength= table->s->reclength-offset;
 
13527
 
 
13528
  org_record=(char*) (record=table->record[0])+offset;
 
13529
  new_record=(char*) table->record[1]+offset;
 
13530
 
 
13531
  file->ha_rnd_init(1);
 
13532
  error=file->rnd_next(record);
5040
13533
  for (;;)
5041
13534
  {
5042
 
    if (session->getKilled())
 
13535
    if (session->killed)
5043
13536
    {
5044
13537
      session->send_kill_message();
5045
13538
      error=0;
5048
13541
    if (error)
5049
13542
    {
5050
13543
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
13544
        continue;
5052
13545
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
13546
        break;
5054
13547
      goto err;
5055
13548
    }
5056
13549
    if (having && !having->val_int())
5057
13550
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
13551
      if ((error=file->ha_delete_row(record)))
 
13552
        goto err;
 
13553
      error=file->rnd_next(record);
5061
13554
      continue;
5062
13555
    }
5063
13556
    if (copy_blobs(first_field))
5068
13561
    }
5069
13562
    memcpy(new_record,org_record,reclength);
5070
13563
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13564
    /* Read through rest of file and mark duplicated rows deleted */
5072
13565
    bool found=0;
5073
13566
    for (;;)
5074
13567
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
13568
      if ((error=file->rnd_next(record)))
5076
13569
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
13570
        if (error == HA_ERR_RECORD_DELETED)
 
13571
          continue;
 
13572
        if (error == HA_ERR_END_OF_FILE)
 
13573
          break;
 
13574
        goto err;
5082
13575
      }
5083
13576
      if (table->compare_record(first_field) == 0)
5084
13577
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
13578
        if ((error=file->ha_delete_row(record)))
 
13579
          goto err;
5087
13580
      }
5088
13581
      else if (!found)
5089
13582
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
13583
        found=1;
 
13584
        file->position(record); // Remember position
5092
13585
      }
5093
13586
    }
5094
13587
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13588
      break;                                    // End of file
 
13589
    /* Restart search on next row */
 
13590
    error=file->restart_rnd_next(record,file->ref);
5098
13591
  }
5099
13592
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13593
  file->extra(HA_EXTRA_NO_CACHE);
5101
13594
  return(0);
5102
13595
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13596
  file->extra(HA_EXTRA_NO_CACHE);
5104
13597
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
13598
    file->print_error(error,MYF(0));
5106
13599
  return(1);
5107
13600
}
5108
13601
 
 
13602
 
5109
13603
/**
5110
13604
  Generate a hash index for each row to quickly find duplicate rows.
5111
13605
 
5112
13606
  @note
5113
13607
    Note that this will not work on tables with blobs!
5114
13608
*/
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)
 
13609
 
 
13610
static int remove_dup_with_hash_index(Session *session, Table *table,
 
13611
                                      uint32_t field_count,
 
13612
                                      Field **first_field,
 
13613
                                      uint32_t key_length,
 
13614
                                      Item *having)
5121
13615
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13616
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
5123
13617
  int error;
5124
 
  Cursor *cursor= table->cursor;
 
13618
  handler *file= table->file;
5125
13619
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
13620
  uint32_t *field_lengths,*field_length;
5127
13621
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
13622
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
13623
  if (!my_multi_malloc(MYF(MY_WME),
 
13624
                       &key_buffer,
 
13625
                       (uint32_t) ((key_length + extra_length) *
 
13626
                               (long) file->stats.records),
 
13627
                       &field_lengths,
 
13628
                       (uint32_t) (field_count*sizeof(*field_lengths)),
 
13629
                       NULL))
 
13630
    return(1);
5133
13631
 
5134
13632
  {
5135
13633
    Field **ptr;
5136
13634
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13635
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
13636
    {
5140
13637
      uint32_t length= (*ptr)->sort_length();
5141
13638
      (*field_length++)= length;
5146
13643
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
13644
  }
5148
13645
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13646
  if (hash_init(&hash, &my_charset_bin, (uint32_t) file->stats.records, 0,
5150
13647
                key_length, (hash_get_key) 0, 0, 0))
5151
13648
  {
 
13649
    free((char*) key_buffer);
5152
13650
    return(1);
5153
13651
  }
5154
13652
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
13653
  file->ha_rnd_init(1);
 
13654
  key_pos=key_buffer;
5159
13655
  for (;;)
5160
13656
  {
5161
13657
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
13658
    if (session->killed)
5163
13659
    {
5164
13660
      session->send_kill_message();
5165
13661
      error=0;
5166
13662
      goto err;
5167
13663
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
13664
    if ((error=file->rnd_next(record)))
5169
13665
    {
5170
13666
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
13667
        continue;
5172
13668
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
13669
        break;
5174
13670
      goto err;
5175
13671
    }
5176
13672
    if (having && !having->val_int())
5177
13673
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
13674
      if ((error=file->ha_delete_row(record)))
 
13675
        goto err;
5180
13676
      continue;
5181
13677
    }
5182
13678
 
5183
13679
    /* copy fields to key buffer */
5184
13680
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
13681
    field_length=field_lengths;
5186
13682
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
13683
    {
5188
13684
      (*ptr)->sort_string(key_pos,*field_length);
5192
13688
    if (hash_search(&hash, org_key_pos, key_length))
5193
13689
    {
5194
13690
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
13691
      if ((error=file->ha_delete_row(record)))
 
13692
        goto err;
5197
13693
    }
5198
13694
    else
5199
13695
      (void) my_hash_insert(&hash, org_key_pos);
5200
13696
    key_pos+=extra_length;
5201
13697
  }
 
13698
  free((char*) key_buffer);
5202
13699
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
13700
  file->extra(HA_EXTRA_NO_CACHE);
 
13701
  (void) file->ha_rnd_end();
5205
13702
  return(0);
5206
13703
 
5207
13704
err:
 
13705
  free((char*) key_buffer);
5208
13706
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
13707
  file->extra(HA_EXTRA_NO_CACHE);
 
13708
  (void) file->ha_rnd_end();
5211
13709
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
13710
    file->print_error(error,MYF(0));
5213
13711
  return(1);
5214
13712
}
5215
13713
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13714
 
 
13715
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13716
                                  SORT_FIELD *sortorder)
5217
13717
{
5218
13718
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
13719
  SORT_FIELD *sort,*pos;
5220
13720
 
5221
13721
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13722
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5223
13723
    count++;
5224
13724
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5226
 
                                       (max(count, *length) + 1));
 
13725
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
13726
                                       (cmax(count, *length) + 1));
5227
13727
  pos= sort= sortorder;
5228
13728
 
5229
13729
  if (!pos)
5249
13749
  return(sort);
5250
13750
}
5251
13751
 
 
13752
 
 
13753
/*****************************************************************************
 
13754
  Fill join cache with packed records
 
13755
  Records are stored in tab->cache.buffer and last record in
 
13756
  last record is stored with pointers to blobs to support very big
 
13757
  records
 
13758
******************************************************************************/
 
13759
 
 
13760
static int
 
13761
join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count)
 
13762
{
 
13763
  register unsigned int i;
 
13764
  unsigned int length, blobs;
 
13765
  size_t size;
 
13766
  CACHE_FIELD *copy,**blob_ptr;
 
13767
  JOIN_CACHE  *cache;
 
13768
  JOIN_TAB *join_tab;
 
13769
 
 
13770
  cache= &tables[table_count].cache;
 
13771
  cache->fields=blobs=0;
 
13772
 
 
13773
  join_tab=tables;
 
13774
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13775
  {
 
13776
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13777
      calc_used_field_length(session, join_tab);
 
13778
    cache->fields+=join_tab->used_fields;
 
13779
    blobs+=join_tab->used_blobs;
 
13780
 
 
13781
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13782
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13783
    {
 
13784
      cache->fields++;
 
13785
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13786
    }
 
13787
  }
 
13788
  if (!(cache->field=(CACHE_FIELD*)
 
13789
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13790
 
 
13791
                  sizeof(CACHE_FIELD*))))
 
13792
  {
 
13793
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13794
    cache->buff=0;                              /* purecov: inspected */
 
13795
    return(1);                          /* purecov: inspected */
 
13796
  }
 
13797
  copy=cache->field;
 
13798
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13799
    (cache->field+cache->fields+table_count*2);
 
13800
 
 
13801
  length=0;
 
13802
  for (i=0 ; i < table_count ; i++)
 
13803
  {
 
13804
    uint32_t null_fields=0, used_fields;
 
13805
    Field **f_ptr,*field;
 
13806
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13807
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13808
         used_fields ;
 
13809
         f_ptr++)
 
13810
    {
 
13811
      field= *f_ptr;
 
13812
      if (bitmap_is_set(read_set, field->field_index))
 
13813
      {
 
13814
        used_fields--;
 
13815
        length+=field->fill_cache_field(copy);
 
13816
        if (copy->blob_field)
 
13817
          (*blob_ptr++)=copy;
 
13818
        if (field->maybe_null())
 
13819
          null_fields++;
 
13820
        copy->get_rowid= NULL;
 
13821
        copy++;
 
13822
      }
 
13823
    }
 
13824
    /* Copy null bits from table */
 
13825
    if (null_fields && tables[i].table->getNullFields())
 
13826
    {                                           /* must copy null bits */
 
13827
      copy->str= tables[i].table->null_flags;
 
13828
      copy->length= tables[i].table->s->null_bytes;
 
13829
      copy->strip=0;
 
13830
      copy->blob_field=0;
 
13831
      copy->get_rowid= NULL;
 
13832
      length+=copy->length;
 
13833
      copy++;
 
13834
      cache->fields++;
 
13835
    }
 
13836
    /* If outer join table, copy null_row flag */
 
13837
    if (tables[i].table->maybe_null)
 
13838
    {
 
13839
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13840
      copy->length=sizeof(tables[i].table->null_row);
 
13841
      copy->strip=0;
 
13842
      copy->blob_field=0;
 
13843
      copy->get_rowid= NULL;
 
13844
      length+=copy->length;
 
13845
      copy++;
 
13846
      cache->fields++;
 
13847
    }
 
13848
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13849
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13850
    {
 
13851
      copy->str= tables[i].table->file->ref;
 
13852
      copy->length= tables[i].table->file->ref_length;
 
13853
      copy->strip=0;
 
13854
      copy->blob_field=0;
 
13855
      copy->get_rowid= NULL;
 
13856
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13857
      {
 
13858
        /* We will need to call h->position(): */
 
13859
        copy->get_rowid= tables[i].table;
 
13860
        /* And those after us won't have to: */
 
13861
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13862
      }
 
13863
      copy++;
 
13864
    }
 
13865
  }
 
13866
 
 
13867
  cache->length=length+blobs*sizeof(char*);
 
13868
  cache->blobs=blobs;
 
13869
  *blob_ptr= NULL;                                      /* End sequentel */
 
13870
  size= max((size_t)session->variables.join_buff_size,
 
13871
            (size_t)cache->length);
 
13872
  if (!(cache->buff=(unsigned char*) malloc(size)))
 
13873
    return 1;                           /* Don't use cache */ /* purecov: inspected */
 
13874
  cache->end=cache->buff+size;
 
13875
  reset_cache_write(cache);
 
13876
  return 0;
 
13877
}
 
13878
 
 
13879
 
 
13880
static uint32_t used_blob_length(CACHE_FIELD **ptr)
 
13881
{
 
13882
  uint32_t length,blob_length;
 
13883
  for (length=0 ; *ptr ; ptr++)
 
13884
  {
 
13885
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13886
    length+=blob_length;
 
13887
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13888
  }
 
13889
  return length;
 
13890
}
 
13891
 
 
13892
 
 
13893
static bool
 
13894
store_record_in_cache(JOIN_CACHE *cache)
 
13895
{
 
13896
  uint32_t length;
 
13897
  unsigned char *pos;
 
13898
  CACHE_FIELD *copy,*end_field;
 
13899
  bool last_record;
 
13900
 
 
13901
  pos=cache->pos;
 
13902
  end_field=cache->field+cache->fields;
 
13903
 
 
13904
  length=cache->length;
 
13905
  if (cache->blobs)
 
13906
    length+= used_blob_length(cache->blob_ptr);
 
13907
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13908
    cache->ptr_record=cache->records;
 
13909
  /*
 
13910
    There is room in cache. Put record there
 
13911
  */
 
13912
  cache->records++;
 
13913
  for (copy=cache->field ; copy < end_field; copy++)
 
13914
  {
 
13915
    if (copy->blob_field)
 
13916
    {
 
13917
      if (last_record)
 
13918
      {
 
13919
        copy->blob_field->get_image(pos, copy->length+sizeof(char*),
 
13920
                                    copy->blob_field->charset());
 
13921
        pos+=copy->length+sizeof(char*);
 
13922
      }
 
13923
      else
 
13924
      {
 
13925
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13926
                                    copy->blob_field->charset());
 
13927
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13928
        pos+=copy->length+copy->blob_length;
 
13929
      }
 
13930
    }
 
13931
    else
 
13932
    {
 
13933
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13934
      if (copy->get_rowid)
 
13935
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13936
 
 
13937
      if (copy->strip)
 
13938
      {
 
13939
        unsigned char *str,*end;
 
13940
        for (str=copy->str,end= str+copy->length;
 
13941
             end > str && end[-1] == ' ' ;
 
13942
             end--) ;
 
13943
        length=(uint32_t) (end-str);
 
13944
        memcpy(pos+2, str, length);
 
13945
        int2store(pos, length);
 
13946
        pos+= length+2;
 
13947
      }
 
13948
      else
 
13949
      {
 
13950
        memcpy(pos,copy->str,copy->length);
 
13951
        pos+=copy->length;
 
13952
      }
 
13953
    }
 
13954
  }
 
13955
  cache->pos=pos;
 
13956
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13957
}
 
13958
 
 
13959
 
 
13960
static void
 
13961
reset_cache_read(JOIN_CACHE *cache)
 
13962
{
 
13963
  cache->record_nr=0;
 
13964
  cache->pos=cache->buff;
 
13965
}
 
13966
 
 
13967
 
 
13968
static void reset_cache_write(JOIN_CACHE *cache)
 
13969
{
 
13970
  reset_cache_read(cache);
 
13971
  cache->records= 0;
 
13972
  cache->ptr_record= UINT32_MAX;
 
13973
}
 
13974
 
 
13975
 
 
13976
static void
 
13977
read_cached_record(JOIN_TAB *tab)
 
13978
{
 
13979
  unsigned char *pos;
 
13980
  uint32_t length;
 
13981
  bool last_record;
 
13982
  CACHE_FIELD *copy,*end_field;
 
13983
 
 
13984
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13985
  pos=tab->cache.pos;
 
13986
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13987
       copy < end_field;
 
13988
       copy++)
 
13989
  {
 
13990
    if (copy->blob_field)
 
13991
    {
 
13992
      if (last_record)
 
13993
      {
 
13994
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13995
                                    copy->blob_field->charset());
 
13996
        pos+=copy->length+sizeof(char*);
 
13997
      }
 
13998
      else
 
13999
      {
 
14000
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
14001
        pos+=copy->length+copy->blob_field->get_length();
 
14002
      }
 
14003
    }
 
14004
    else
 
14005
    {
 
14006
      if (copy->strip)
 
14007
      {
 
14008
        length= uint2korr(pos);
 
14009
        memcpy(copy->str, pos+2, length);
 
14010
        memset(copy->str+length, ' ', copy->length-length);
 
14011
        pos+= 2 + length;
 
14012
      }
 
14013
      else
 
14014
      {
 
14015
        memcpy(copy->str,pos,copy->length);
 
14016
        pos+=copy->length;
 
14017
      }
 
14018
    }
 
14019
  }
 
14020
  tab->cache.pos=pos;
 
14021
  return;
 
14022
}
 
14023
 
 
14024
 
5252
14025
/*
5253
14026
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
14027
 
5267
14040
    false  The created key is the same as the previous one (and the record
5268
14041
           is already in table->record)
5269
14042
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14043
 
 
14044
static bool
 
14045
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
14046
{
5272
14047
  bool no_prev_key;
5273
14048
  if (!tab->ref.disable_cache)
5287
14062
    != 0;
5288
14063
}
5289
14064
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14065
 
 
14066
bool
 
14067
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
5291
14068
{
5292
14069
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
14070
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
5294
14071
  bool result= 0;
5295
14072
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14073
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
14074
  {
5298
14075
    if ((*copy)->copy() & 1)
5299
14076
    {
5305
14082
  return result;
5306
14083
}
5307
14084
 
 
14085
 
5308
14086
/*****************************************************************************
5309
14087
  Group and order functions
5310
14088
*****************************************************************************/
5311
14089
 
5312
14090
/**
5313
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14091
  Resolve an order_st BY or GROUP BY column reference.
5314
14092
 
5315
14093
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5316
14094
  BY clause, find the actual column it represents. If the column being
5317
14095
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
14096
  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.
 
14097
  the order_st BY clause, only the SELECT list is being searched.
5320
14098
 
5321
14099
  If 'order' is resolved to an Item, then order->item is set to the found
5322
14100
  Item. If there is no item for the found column (that is, it was resolved
5341
14119
  @retval
5342
14120
    true  if error occurred
5343
14121
*/
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)
 
14122
 
 
14123
static bool
 
14124
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
 
14125
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14126
                   bool is_group_field)
5351
14127
{
5352
14128
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5353
14129
  Item::Type order_item_type;
5366
14142
    if (!count || count > fields.elements)
5367
14143
    {
5368
14144
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
14145
               order_item->full_name(), session->where);
5370
14146
      return true;
5371
14147
    }
5372
14148
    order->item= ref_pointer_array + count - 1;
5376
14152
    return false;
5377
14153
  }
5378
14154
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14155
  select_item= find_item_in_list(order_item, fields, &counter,
5380
14156
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
14157
  if (!select_item)
5382
14158
    return true; /* The item is not unique, or some other error occured. */
5402
14178
        order_item_type == Item::REF_ITEM)
5403
14179
    {
5404
14180
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14181
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14182
                                       false);
5406
14183
      if (!from_field)
5407
14184
        from_field= (Field*) not_found_field;
5408
14185
    }
5443
14220
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
14221
                          ER(ER_NON_UNIQ_ERROR),
5445
14222
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
14223
                          current_session->where);
5447
14224
    }
5448
14225
  }
5449
14226
 
5472
14249
  return false;
5473
14250
}
5474
14251
 
 
14252
 
5475
14253
/**
5476
14254
  Change order to point at item in select list.
5477
14255
 
5478
14256
  If item isn't a number and doesn't exits in the select list, add it the
5479
14257
  the field list.
5480
14258
*/
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)
 
14259
 
 
14260
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
 
14261
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5487
14262
{
5488
 
  session->setWhere("order clause");
 
14263
  session->where="order clause";
5489
14264
  for (; order; order=order->next)
5490
14265
  {
5491
14266
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5495
14270
  return 0;
5496
14271
}
5497
14272
 
 
14273
 
5498
14274
/**
5499
14275
  Intitialize the GROUP BY list.
5500
14276
 
5501
 
  @param session                        Thread Cursor
 
14277
  @param session                        Thread handler
5502
14278
  @param ref_pointer_array      We store references to all fields that was
5503
14279
                               not in 'fields' here.
5504
14280
  @param fields         All fields in the select part. Any item in
5520
14296
  @retval
5521
14297
    1  error (probably out of memory)
5522
14298
*/
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)
 
14299
 
 
14300
int
 
14301
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
 
14302
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14303
            bool *hidden_group_fields)
5530
14304
{
5531
14305
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
14306
  order_st *ord;
5533
14307
 
5534
14308
  if (!order)
5535
14309
    return 0;                           /* Everything is ok */
5536
14310
 
5537
14311
  uint32_t org_fields=all_fields.elements;
5538
14312
 
5539
 
  session->setWhere("group statement");
 
14313
  session->where="group statement";
5540
14314
  for (ord= order; ord; ord= ord->next)
5541
14315
  {
5542
14316
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5569
14343
    Item *item;
5570
14344
    Item_field *field;
5571
14345
    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());
 
14346
    List_iterator<Item> li(fields);
 
14347
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
14348
 
5575
14349
    field= naf_it++;
5576
14350
    while (field && (item=li++))
5596
14370
            if ((*ord->item)->eq((Item*)field, 0))
5597
14371
              goto next_field;
5598
14372
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
14373
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
14374
            ER_NON_GROUPING_FIELD_USED
5600
14375
          */
5601
14376
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
14377
          return 1;
5618
14393
  Try to use the fields in the order given by 'order' to allow one to
5619
14394
  optimize away 'order by'.
5620
14395
*/
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)
 
14396
 
 
14397
static order_st *
 
14398
create_distinct_group(Session *session, Item **ref_pointer_array,
 
14399
                      order_st *order_list, List<Item> &fields,
 
14400
                      List<Item> &, bool *all_order_by_fields_used)
5627
14401
{
5628
 
  List<Item>::iterator li(fields.begin());
 
14402
  List_iterator<Item> li(fields);
5629
14403
  Item *item;
5630
 
  Order *order,*group,**prev;
 
14404
  order_st *order,*group,**prev;
5631
14405
 
5632
14406
  *all_order_by_fields_used= 1;
5633
14407
  while ((item=li++))
5638
14412
  {
5639
14413
    if (order->in_field_list)
5640
14414
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
14415
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5642
14416
      if (!ord)
5643
 
        return 0;
 
14417
        return 0;
5644
14418
      *prev=ord;
5645
14419
      prev= &ord->next;
5646
14420
      (*ord->item)->marker=1;
5649
14423
      *all_order_by_fields_used= 0;
5650
14424
  }
5651
14425
 
5652
 
  li= fields.begin();
 
14426
  li.rewind();
5653
14427
  while ((item=li++))
5654
14428
  {
5655
14429
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5658
14432
        Don't put duplicate columns from the SELECT list into the
5659
14433
        GROUP BY list.
5660
14434
      */
5661
 
      Order *ord_iter;
 
14435
      order_st *ord_iter;
5662
14436
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
14437
        if ((*ord_iter->item)->eq(item, 1))
5664
14438
          goto next_item;
5665
14439
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14440
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5667
14441
      if (!ord)
5668
 
        return 0;
 
14442
        return 0;
5669
14443
 
5670
14444
      /*
5671
14445
        We have here only field_list (not all_field_list), so we can use
5684
14458
  return group;
5685
14459
}
5686
14460
 
 
14461
 
5687
14462
/**
5688
14463
  Update join with count of the different type of fields.
5689
14464
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14465
 
 
14466
void
 
14467
count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
 
14468
                  List<Item> &fields, bool reset_with_sum_func)
5691
14469
{
5692
 
  List<Item>::iterator li(fields.begin());
 
14470
  List_iterator<Item> li(fields);
5693
14471
  Item *field;
5694
14472
 
5695
14473
  param->field_count=param->sum_func_count=param->func_count=
5704
14482
    {
5705
14483
      if (! field->const_item())
5706
14484
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14485
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
14486
        if (!sum_item->depended_from() ||
5709
14487
            sum_item->depended_from() == select_lex)
5710
14488
        {
5727
14505
    {
5728
14506
      param->func_count++;
5729
14507
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
14508
        field->with_sum_func=0;
 
14509
    }
 
14510
  }
 
14511
}
 
14512
 
 
14513
 
 
14514
/**
 
14515
  Return 1 if second is a subpart of first argument.
 
14516
 
 
14517
  If first parts has different direction, change it to second part
 
14518
  (group is sorted like order)
 
14519
*/
 
14520
 
 
14521
static bool
 
14522
test_if_subpart(order_st *a,order_st *b)
 
14523
{
 
14524
  for (; a && b; a=a->next,b=b->next)
 
14525
  {
 
14526
    if ((*a->item)->eq(*b->item,1))
 
14527
      a->asc=b->asc;
 
14528
    else
 
14529
      return 0;
 
14530
  }
 
14531
  return test(!b);
 
14532
}
 
14533
 
 
14534
/**
 
14535
  Return table number if there is only one table in sort order
 
14536
  and group and order is compatible, else return 0.
 
14537
*/
 
14538
 
 
14539
static Table *
 
14540
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14541
{
 
14542
  table_map map= (table_map) 0;
 
14543
 
 
14544
  if (!a)
 
14545
    a=b;                                        // Only one need to be given
 
14546
  else if (!b)
 
14547
    b=a;
 
14548
 
 
14549
  for (; a && b; a=a->next,b=b->next)
 
14550
  {
 
14551
    if (!(*a->item)->eq(*b->item,1))
 
14552
      return(0);
 
14553
    map|=a->item[0]->used_tables();
 
14554
  }
 
14555
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14556
    return(0);
 
14557
 
 
14558
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14559
  if (map != tables->table->map)
 
14560
    return(0);                          // More than one table
 
14561
  return(tables->table);
 
14562
}
 
14563
 
 
14564
 
 
14565
/**
 
14566
  calc how big buffer we need for comparing group entries.
 
14567
*/
 
14568
 
 
14569
static void
 
14570
calc_group_buffer(JOIN *join,order_st *group)
 
14571
{
 
14572
  uint32_t key_length=0, parts=0, null_parts=0;
 
14573
 
 
14574
  if (group)
 
14575
    join->group= 1;
 
14576
  for (; group ; group=group->next)
 
14577
  {
 
14578
    Item *group_item= *group->item;
 
14579
    Field *field= group_item->get_tmp_table_field();
 
14580
    if (field)
 
14581
    {
 
14582
      enum_field_types type;
 
14583
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14584
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14585
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14586
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14587
      else
 
14588
        key_length+= field->pack_length();
 
14589
    }
 
14590
    else
 
14591
    {
 
14592
      switch (group_item->result_type()) {
 
14593
      case REAL_RESULT:
 
14594
        key_length+= sizeof(double);
 
14595
        break;
 
14596
      case INT_RESULT:
 
14597
        key_length+= sizeof(int64_t);
 
14598
        break;
 
14599
      case DECIMAL_RESULT:
 
14600
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
14601
                                                (group_item->decimals ? 1 : 0),
 
14602
                                                group_item->decimals);
 
14603
        break;
 
14604
      case STRING_RESULT:
 
14605
      {
 
14606
        enum enum_field_types type= group_item->field_type();
 
14607
        /*
 
14608
          As items represented as DATE/TIME fields in the group buffer
 
14609
          have STRING_RESULT result type, we increase the length
 
14610
          by 8 as maximum pack length of such fields.
 
14611
        */
 
14612
        if (type == DRIZZLE_TYPE_DATE ||
 
14613
            type == DRIZZLE_TYPE_DATETIME ||
 
14614
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14615
        {
 
14616
          key_length+= 8;
 
14617
        }
 
14618
        else
 
14619
        {
 
14620
          /*
 
14621
            Group strings are taken as varstrings and require an length field.
 
14622
            A field is not yet created by create_tmp_field()
 
14623
            and the sizes should match up.
 
14624
          */
 
14625
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14626
        }
 
14627
        break;
 
14628
      }
 
14629
      default:
 
14630
        /* This case should never be choosen */
 
14631
        assert(0);
 
14632
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14633
      }
 
14634
    }
 
14635
    parts++;
 
14636
    if (group_item->maybe_null)
 
14637
      null_parts++;
 
14638
  }
 
14639
  join->tmp_table_param.group_length=key_length+null_parts;
 
14640
  join->tmp_table_param.group_parts=parts;
 
14641
  join->tmp_table_param.group_null_parts=null_parts;
 
14642
}
 
14643
 
 
14644
 
 
14645
/**
 
14646
  allocate group fields or take prepared (cached).
 
14647
 
 
14648
  @param main_join   join of current select
 
14649
  @param curr_join   current join (join of current select or temporary copy
 
14650
                     of it)
 
14651
 
 
14652
  @retval
 
14653
    0   ok
 
14654
  @retval
 
14655
    1   failed
 
14656
*/
 
14657
 
 
14658
static bool
 
14659
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14660
{
 
14661
  if (main_join->group_fields_cache.elements)
 
14662
  {
 
14663
    curr_join->group_fields= main_join->group_fields_cache;
 
14664
    curr_join->sort_and_group= 1;
 
14665
  }
 
14666
  else
 
14667
  {
 
14668
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14669
      return (1);
 
14670
    main_join->group_fields_cache= curr_join->group_fields;
 
14671
  }
 
14672
  return (0);
 
14673
}
 
14674
 
 
14675
 
 
14676
/**
 
14677
  Get a list of buffers for saveing last group.
 
14678
 
 
14679
  Groups are saved in reverse order for easyer check loop.
 
14680
*/
 
14681
 
 
14682
static bool
 
14683
alloc_group_fields(JOIN *join,order_st *group)
 
14684
{
 
14685
  if (group)
 
14686
  {
 
14687
    for (; group ; group=group->next)
 
14688
    {
 
14689
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
 
14690
      if (!tmp || join->group_fields.push_front(tmp))
 
14691
        return true;
 
14692
    }
 
14693
  }
 
14694
  join->sort_and_group=1;                       /* Mark for do_select */
 
14695
  return false;
 
14696
}
 
14697
 
5734
14698
 
5735
14699
/*
5736
14700
  Test if a single-row cache of items changed, and update the cache.
5744
14708
  @return -1 if no item changed
5745
14709
  @return index of the first item that changed
5746
14710
*/
 
14711
 
5747
14712
int test_if_item_cache_changed(List<Cached_item> &list)
5748
14713
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
14714
  List_iterator<Cached_item> li(list);
5750
14715
  int idx= -1,i;
5751
14716
  Cached_item *buff;
5752
14717
 
5758
14723
  return(idx);
5759
14724
}
5760
14725
 
 
14726
 
5761
14727
/**
5762
14728
  Setup copy_fields to save fields at start of new group.
5763
14729
 
5786
14752
  @retval
5787
14753
    !=0   error
5788
14754
*/
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)
 
14755
 
 
14756
bool
 
14757
setup_copy_fields(Session *session, Tmp_Table_Param *param,
 
14758
                  Item **ref_pointer_array,
 
14759
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14760
                  uint32_t elements, List<Item> &all_fields)
5796
14761
{
5797
14762
  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());
 
14763
  List_iterator_fast<Item> li(all_fields);
 
14764
  Copy_field *copy= NULL;
 
14765
  res_selected_fields.empty();
 
14766
  res_all_fields.empty();
 
14767
  List_iterator_fast<Item> itr(res_all_fields);
5803
14768
  List<Item> extra_funcs;
5804
14769
  uint32_t i, border= all_fields.elements - elements;
5805
14770
 
5806
14771
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14772
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
14773
    goto err2;
5809
14774
 
5810
 
  param->copy_funcs.clear();
 
14775
  param->copy_funcs.empty();
5811
14776
  for (i= 0; (pos= li++); i++)
5812
14777
  {
5813
14778
    Field *field;
5817
14782
    {
5818
14783
      Item_field *item;
5819
14784
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
14785
        goto err;
5821
14786
      if (pos->type() == Item::REF_ITEM)
5822
14787
      {
5823
14788
        /* preserve the names of the ref when dereferncing */
5829
14794
      pos= item;
5830
14795
      if (item->field->flags & BLOB_FLAG)
5831
14796
      {
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;
 
14797
        if (!(pos= new Item_copy_string(pos)))
 
14798
          goto err;
 
14799
       /*
 
14800
         Item_copy_string::copy for function can call
 
14801
         Item_copy_string::val_int for blob via Item_ref.
 
14802
         But if Item_copy_string::copy for blob isn't called before,
 
14803
         it's value will be wrong
 
14804
         so let's insert Item_copy_string for blobs in the beginning of
 
14805
         copy_funcs
 
14806
         (to see full test case look at having.test, BUG #4358)
 
14807
       */
 
14808
        if (param->copy_funcs.push_front(pos))
 
14809
          goto err;
5845
14810
      }
5846
14811
      else
5847
14812
      {
 
14813
        /*
 
14814
           set up save buffer and change result_field to point at
 
14815
           saved value
 
14816
        */
 
14817
        field= item->field;
 
14818
        item->result_field=field->new_field(session->mem_root,field->table, 1);
5848
14819
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
14820
          We need to allocate one extra byte for null handling and
 
14821
          another extra byte to not get warnings from purify in
 
14822
          Field_varstring::val_int
5851
14823
        */
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;
 
14824
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14825
          goto err;
5861
14826
        if (copy)
5862
14827
        {
5863
14828
          copy->set(tmp, item->result_field);
5864
14829
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
14830
#ifdef HAVE_purify
5866
14831
          copy->to_ptr[copy->from_length]= 0;
5867
14832
#endif
5868
14833
          copy++;
5876
14841
             !real_pos->with_sum_func)
5877
14842
    {                                           // Save for send fields
5878
14843
      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(*))
 
14844
      /* TODO:
 
14845
         In most cases this result will be sent to the user.
 
14846
         This should be changed to use copy_int or copy_real depending
 
14847
         on how the value is to be used: In some cases this may be an
 
14848
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
14849
      */
5885
14850
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
 
14851
        goto err;
5887
14852
      if (i < border)                           // HAVING, order_st and GROUP BY
5888
14853
      {
5889
14854
        if (extra_funcs.push_back(pos))
5890
14855
          goto err;
5891
14856
      }
5892
14857
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
14858
        goto err;
5894
14859
    }
5895
14860
    res_all_fields.push_back(pos);
5896
14861
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5902
14867
    itr++;
5903
14868
  itr.sublist(res_selected_fields, elements);
5904
14869
  /*
5905
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14870
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5906
14871
    reference used in these will resolve to a item that is already calculated
5907
14872
  */
5908
14873
  param->copy_funcs.concat(&extra_funcs);
5909
14874
 
5910
14875
  return(0);
5911
14876
 
5912
 
err:
 
14877
 err:
5913
14878
  if (copy)
5914
14879
    delete [] param->copy_field;                        // This is never 0
5915
14880
  param->copy_field=0;
5917
14882
  return(true);
5918
14883
}
5919
14884
 
 
14885
 
5920
14886
/**
5921
14887
  Make a copy of all simple SELECT'ed items.
5922
14888
 
5923
14889
  This is done at the start of a new group so that we can retrieve
5924
14890
  these later when the group changes.
5925
14891
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
14892
 
 
14893
void
 
14894
copy_fields(Tmp_Table_Param *param)
5927
14895
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
14896
  Copy_field *ptr=param->copy_field;
 
14897
  Copy_field *end=param->copy_field_end;
5930
14898
 
5931
14899
  for (; ptr != end; ptr++)
5932
14900
    (*ptr->do_copy)(ptr);
5933
14901
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
14902
  List_iterator_fast<Item> it(param->copy_funcs);
5935
14903
  Item_copy_string *item;
5936
14904
  while ((item = (Item_copy_string*) it++))
5937
14905
    item->copy();
5938
14906
}
5939
14907
 
 
14908
 
 
14909
/**
 
14910
  Make an array of pointers to sum_functions to speed up
 
14911
  sum_func calculation.
 
14912
 
 
14913
  @retval
 
14914
    0   ok
 
14915
  @retval
 
14916
    1   Error
 
14917
*/
 
14918
 
 
14919
bool JOIN::alloc_func_list()
 
14920
{
 
14921
  uint32_t func_count, group_parts;
 
14922
 
 
14923
  func_count= tmp_table_param.sum_func_count;
 
14924
  /*
 
14925
    If we are using rollup, we need a copy of the summary functions for
 
14926
    each level
 
14927
  */
 
14928
  if (rollup.state != ROLLUP::STATE_NONE)
 
14929
    func_count*= (send_group_parts+1);
 
14930
 
 
14931
  group_parts= send_group_parts;
 
14932
  /*
 
14933
    If distinct, reserve memory for possible
 
14934
    disctinct->group_by optimization
 
14935
  */
 
14936
  if (select_distinct)
 
14937
  {
 
14938
    group_parts+= fields_list.elements;
 
14939
    /*
 
14940
      If the order_st clause is specified then it's possible that
 
14941
      it also will be optimized, so reserve space for it too
 
14942
    */
 
14943
    if (order)
 
14944
    {
 
14945
      order_st *ord;
 
14946
      for (ord= order; ord; ord= ord->next)
 
14947
        group_parts++;
 
14948
    }
 
14949
  }
 
14950
 
 
14951
  /* This must use calloc() as rollup_make_fields depends on this */
 
14952
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14953
                                      sizeof(Item_sum***) * (group_parts+1));
 
14954
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14955
  return(sum_funcs == 0);
 
14956
}
 
14957
 
 
14958
 
 
14959
/**
 
14960
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14961
 
 
14962
  @param field_list        All items
 
14963
  @param send_fields       Items in select list
 
14964
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14965
  @param recompute         Set to true if sum_funcs must be recomputed
 
14966
 
 
14967
  @retval
 
14968
    0  ok
 
14969
  @retval
 
14970
    1  error
 
14971
*/
 
14972
 
 
14973
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14974
                              bool before_group_by, bool recompute)
 
14975
{
 
14976
  List_iterator_fast<Item> it(field_list);
 
14977
  Item_sum **func;
 
14978
  Item *item;
 
14979
 
 
14980
  if (*sum_funcs && !recompute)
 
14981
    return(false); /* We have already initialized sum_funcs. */
 
14982
 
 
14983
  func= sum_funcs;
 
14984
  while ((item=it++))
 
14985
  {
 
14986
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14987
        (!((Item_sum*) item)->depended_from() ||
 
14988
         ((Item_sum *)item)->depended_from() == select_lex))
 
14989
      *func++= (Item_sum*) item;
 
14990
  }
 
14991
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14992
  {
 
14993
    rollup.state= ROLLUP::STATE_READY;
 
14994
    if (rollup_make_fields(field_list, send_fields, &func))
 
14995
      return(true);                     // Should never happen
 
14996
  }
 
14997
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14998
  {
 
14999
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
15000
      sum_funcs_end[i]= func;
 
15001
  }
 
15002
  else if (rollup.state == ROLLUP::STATE_READY)
 
15003
    return(false);                         // Don't put end marker
 
15004
  *func=0;                                      // End marker
 
15005
  return(false);
 
15006
}
 
15007
 
 
15008
 
5940
15009
/**
5941
15010
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
15011
  new list of all items.
5953
15022
  @retval
5954
15023
    !=0   error
5955
15024
*/
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)
 
15025
 
 
15026
static bool
 
15027
change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
 
15028
                         List<Item> &res_selected_fields,
 
15029
                         List<Item> &res_all_fields,
 
15030
                         uint32_t elements, List<Item> &all_fields)
5962
15031
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
15032
  List_iterator_fast<Item> it(all_fields);
5964
15033
  Item *item_field,*item;
5965
15034
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
15035
  res_selected_fields.empty();
 
15036
  res_all_fields.empty();
5968
15037
 
5969
15038
  uint32_t i, border= all_fields.elements - elements;
5970
15039
  for (i= 0; (item= it++); i++)
5979
15048
    {
5980
15049
      if (item->type() == Item::FIELD_ITEM)
5981
15050
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
15051
        item_field= item->get_tmp_table_item(session);
5983
15052
      }
5984
15053
      else if ((field= item->get_tmp_table_field()))
5985
15054
      {
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
 
15055
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15056
          item_field= ((Item_sum*) item)->result_item(field);
 
15057
        else
 
15058
          item_field= (Item*) new Item_field(field);
 
15059
        if (!item_field)
 
15060
          return(true);                    // Fatal error
5992
15061
 
5993
15062
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
15063
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
15064
        item_field->name= item->name;
5996
15065
        if (item->type() == Item::REF_ITEM)
5997
15066
        {
5998
15067
          Item_field *ifield= (Item_field *) item_field;
6002
15071
        }
6003
15072
      }
6004
15073
      else
6005
 
        item_field= item;
 
15074
        item_field= item;
6006
15075
    }
6007
15076
    res_all_fields.push_back(item_field);
6008
15077
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
15078
      item_field;
6010
15079
  }
6011
15080
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15081
  List_iterator_fast<Item> itr(res_all_fields);
6013
15082
  for (i= 0; i < border; i++)
6014
15083
    itr++;
6015
15084
  itr.sublist(res_selected_fields, elements);
6016
15085
  return(false);
6017
15086
}
6018
15087
 
 
15088
 
6019
15089
/**
6020
15090
  Change all sum_func refs to fields to point at fields in tmp table.
6021
15091
  Change all funcs to be fields in tmp table.
6032
15102
  @retval
6033
15103
    1   error
6034
15104
*/
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)
 
15105
 
 
15106
static bool
 
15107
change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
15108
                          List<Item> &res_selected_fields,
 
15109
                          List<Item> &res_all_fields, uint32_t elements,
 
15110
                          List<Item> &all_fields)
6041
15111
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
15112
  List_iterator_fast<Item> it(all_fields);
6043
15113
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
15114
  res_selected_fields.empty();
 
15115
  res_all_fields.empty();
6046
15116
 
6047
15117
  uint32_t i, border= all_fields.elements - elements;
6048
15118
  for (i= 0; (item= it++); i++)
6052
15122
      new_item;
6053
15123
  }
6054
15124
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15125
  List_iterator_fast<Item> itr(res_all_fields);
6056
15126
  for (i= 0; i < border; i++)
6057
15127
    itr++;
6058
15128
  itr.sublist(res_selected_fields, elements);
6060
15130
  return session->is_fatal_error;
6061
15131
}
6062
15132
 
 
15133
 
 
15134
 
6063
15135
/******************************************************************************
6064
15136
  Code for calculating functions
6065
15137
******************************************************************************/
6066
15138
 
 
15139
 
6067
15140
/**
6068
15141
  Call ::setup for all sum functions.
6069
15142
 
6070
 
  @param session           thread Cursor
 
15143
  @param session           thread handler
6071
15144
  @param func_ptr      sum function list
6072
15145
 
6073
15146
  @retval
6075
15148
  @retval
6076
15149
    true   error
6077
15150
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15151
 
 
15152
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
6079
15153
{
6080
15154
  Item_sum *func;
6081
15155
  while ((func= *(func_ptr++)))
6086
15160
  return(false);
6087
15161
}
6088
15162
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15163
 
 
15164
static void
 
15165
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
15166
{
6091
15167
  Item_sum *func;
6092
15168
  while ((func= *(func_ptr++)))
6093
15169
    func->reset_field();
6094
15170
}
6095
15171
 
 
15172
 
6096
15173
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15174
 
 
15175
static void
 
15176
update_tmptable_sum_func(Item_sum **func_ptr, Table *)
6098
15177
{
6099
15178
  Item_sum *func;
6100
15179
  while ((func= *(func_ptr++)))
6101
15180
    func->update_field();
6102
15181
}
6103
15182
 
 
15183
 
6104
15184
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15185
 
 
15186
static void
 
15187
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
15188
{
6107
15189
  for (; func_ptr != end_ptr ; func_ptr++)
6108
15190
    (void) (*func_ptr)->save_in_result_field(1);
6109
15191
  return;
6110
15192
}
6111
15193
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15194
 
 
15195
static bool
 
15196
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
15197
{
6114
15198
  for (; func_ptr != end_ptr ;func_ptr++)
6115
15199
  {
6125
15209
  return 0;
6126
15210
}
6127
15211
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
15212
 
 
15213
static bool
 
15214
update_sum_func(Item_sum **func_ptr)
6129
15215
{
6130
15216
  Item_sum *func;
6131
15217
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
15221
}
6136
15222
 
6137
15223
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
15224
 
 
15225
void
 
15226
copy_funcs(Item **func_ptr)
6139
15227
{
6140
15228
  Item *func;
6141
15229
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
15230
    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
 
}
 
15231
}
 
15232
 
 
15233
 
 
15234
/**
 
15235
  Create a condition for a const reference and add this to the
 
15236
  currenct select for the table.
 
15237
*/
 
15238
 
 
15239
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
 
15240
{
 
15241
  if (!join_tab->ref.key_parts)
 
15242
    return(false);
 
15243
 
 
15244
  Item_cond_and *cond=new Item_cond_and();
 
15245
  Table *table=join_tab->table;
 
15246
  int error;
 
15247
  if (!cond)
 
15248
    return(true);
 
15249
 
 
15250
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15251
  {
 
15252
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15253
                              fieldnr-1];
 
15254
    Item *value=join_tab->ref.items[i];
 
15255
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15256
  }
 
15257
  if (session->is_fatal_error)
 
15258
    return(true);
 
15259
 
 
15260
  if (!cond->fixed)
 
15261
    cond->fix_fields(session, (Item**)&cond);
 
15262
  if (join_tab->select)
 
15263
  {
 
15264
    error=(int) cond->add(join_tab->select->cond);
 
15265
    join_tab->select_cond=join_tab->select->cond=cond;
 
15266
  }
 
15267
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15268
                                          &error)))
 
15269
    join_tab->select_cond=cond;
 
15270
 
 
15271
  return(error ? true : false);
 
15272
}
 
15273
 
6155
15274
 
6156
15275
/**
6157
15276
  Free joins of subselect of this select.
6159
15278
  @param session      Session pointer
6160
15279
  @param select   pointer to Select_Lex which subselects joins we will free
6161
15280
*/
 
15281
 
6162
15282
void free_underlaid_joins(Session *, Select_Lex *select)
6163
15283
{
6164
15284
  for (Select_Lex_Unit *unit= select->first_inner_unit();
6202
15322
  @param changed        out:  returns 1 if item contains a replaced field item
6203
15323
 
6204
15324
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
15325
    - TODO: Some functions are not null-preserving. For those functions
6206
15326
    updating of the maybe_null attribute is an overkill.
6207
15327
 
6208
15328
  @retval
6210
15330
  @retval
6211
15331
    1   on error
6212
15332
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15333
 
 
15334
static bool change_group_ref(Session *session, Item_func *expr, order_st *group_list,
 
15335
                             bool *changed)
6214
15336
{
6215
15337
  if (expr->arg_count)
6216
15338
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
15339
    Name_resolution_context *context= &session->lex->current_select->context;
6218
15340
    Item **arg,**arg_end;
6219
15341
    bool arg_changed= false;
6220
15342
    for (arg= expr->arguments(),
6224
15346
      Item *item= *arg;
6225
15347
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
15348
      {
6227
 
        Order *group_tmp;
 
15349
        order_st *group_tmp;
6228
15350
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
15351
        {
6230
15352
          if (item->eq(*group_tmp->item,0))
6254
15376
}
6255
15377
 
6256
15378
 
 
15379
/** Allocate memory needed for other rollup functions. */
 
15380
 
 
15381
bool JOIN::rollup_init()
 
15382
{
 
15383
  uint32_t i,j;
 
15384
  Item **ref_array;
 
15385
 
 
15386
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15387
  rollup.state= ROLLUP::STATE_INITED;
 
15388
 
 
15389
  /*
 
15390
    Create pointers to the different sum function groups
 
15391
    These are updated by rollup_make_fields()
 
15392
  */
 
15393
  tmp_table_param.group_parts= send_group_parts;
 
15394
 
 
15395
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
15396
                                                sizeof(Item**) +
 
15397
                                                sizeof(List<Item>) +
 
15398
                                                ref_pointer_array_size)
 
15399
                                                * send_group_parts )))
 
15400
    return 1;
 
15401
 
 
15402
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15403
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15404
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15405
 
 
15406
  /*
 
15407
    Prepare space for field list for the different levels
 
15408
    These will be filled up in rollup_make_fields()
 
15409
  */
 
15410
  for (i= 0 ; i < send_group_parts ; i++)
 
15411
  {
 
15412
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
15413
    List<Item> *rollup_fields= &rollup.fields[i];
 
15414
    rollup_fields->empty();
 
15415
    rollup.ref_pointer_arrays[i]= ref_array;
 
15416
    ref_array+= all_fields.elements;
 
15417
  }
 
15418
  for (i= 0 ; i < send_group_parts; i++)
 
15419
  {
 
15420
    for (j=0 ; j < fields_list.elements ; j++)
 
15421
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15422
  }
 
15423
  List_iterator<Item> it(all_fields);
 
15424
  Item *item;
 
15425
  while ((item= it++))
 
15426
  {
 
15427
    order_st *group_tmp;
 
15428
    bool found_in_group= 0;
 
15429
 
 
15430
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15431
    {
 
15432
      if (*group_tmp->item == item)
 
15433
      {
 
15434
        item->maybe_null= 1;
 
15435
        found_in_group= 1;
 
15436
        if (item->const_item())
 
15437
        {
 
15438
          /*
 
15439
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15440
            is wrapped up into an Item_func object yielding the same value
 
15441
            as the constant item. The objects of the wrapper class are never
 
15442
            considered as constant items and besides they inherit all
 
15443
            properties of the Item_result_field class.
 
15444
            This wrapping allows us to ensure writing constant items
 
15445
            into temporary tables whenever the result of the ROLLUP
 
15446
            operation has to be written into a temporary table, e.g. when
 
15447
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15448
            Usually when creating temporary tables for a intermidiate
 
15449
            result we do not include fields for constant expressions.
 
15450
          */
 
15451
          Item* new_item= new Item_func_rollup_const(item);
 
15452
          if (!new_item)
 
15453
            return 1;
 
15454
          new_item->fix_fields(session, (Item **) 0);
 
15455
          session->change_item_tree(it.ref(), new_item);
 
15456
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15457
          {
 
15458
            if (*tmp->item == item)
 
15459
              session->change_item_tree(tmp->item, new_item);
 
15460
          }
 
15461
        }
 
15462
      }
 
15463
    }
 
15464
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15465
    {
 
15466
      bool changed= false;
 
15467
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
 
15468
        return 1;
 
15469
      /*
 
15470
        We have to prevent creation of a field in a temporary table for
 
15471
        an expression that contains GROUP BY attributes.
 
15472
        Marking the expression item as 'with_sum_func' will ensure this.
 
15473
      */
 
15474
      if (changed)
 
15475
        item->with_sum_func= 1;
 
15476
    }
 
15477
  }
 
15478
  return 0;
 
15479
}
 
15480
 
 
15481
 
 
15482
/**
 
15483
  Fill up rollup structures with pointers to fields to use.
 
15484
 
 
15485
  Creates copies of item_sum items for each sum level.
 
15486
 
 
15487
  @param fields_arg             List of all fields (hidden and real ones)
 
15488
  @param sel_fields             Pointer to selected fields
 
15489
  @param func                   Store here a pointer to all fields
 
15490
 
 
15491
  @retval
 
15492
    0   if ok;
 
15493
    In this case func is pointing to next not used element.
 
15494
  @retval
 
15495
    1    on error
 
15496
*/
 
15497
 
 
15498
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15499
                              Item_sum ***func)
 
15500
{
 
15501
  List_iterator_fast<Item> it(fields_arg);
 
15502
  Item *first_field= sel_fields.head();
 
15503
  uint32_t level;
 
15504
 
 
15505
  /*
 
15506
    Create field lists for the different levels
 
15507
 
 
15508
    The idea here is to have a separate field list for each rollup level to
 
15509
    avoid all runtime checks of which columns should be NULL.
 
15510
 
 
15511
    The list is stored in reverse order to get sum function in such an order
 
15512
    in func that it makes it easy to reset them with init_sum_functions()
 
15513
 
 
15514
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15515
 
 
15516
    rollup.fields[0] will contain list where a,b,c is NULL
 
15517
    rollup.fields[1] will contain list where b,c is NULL
 
15518
    ...
 
15519
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15520
    ...
 
15521
    sum_funcs_end[0] points to all sum functions
 
15522
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15523
    ...
 
15524
  */
 
15525
 
 
15526
  for (level=0 ; level < send_group_parts ; level++)
 
15527
  {
 
15528
    uint32_t i;
 
15529
    uint32_t pos= send_group_parts - level -1;
 
15530
    bool real_fields= 0;
 
15531
    Item *item;
 
15532
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15533
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15534
    order_st *start_group;
 
15535
 
 
15536
    /* Point to first hidden field */
 
15537
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15538
 
 
15539
    /* Remember where the sum functions ends for the previous level */
 
15540
    sum_funcs_end[pos+1]= *func;
 
15541
 
 
15542
    /* Find the start of the group for this level */
 
15543
    for (i= 0, start_group= group_list ;
 
15544
         i++ < pos ;
 
15545
         start_group= start_group->next)
 
15546
      ;
 
15547
 
 
15548
    it.rewind();
 
15549
    while ((item= it++))
 
15550
    {
 
15551
      if (item == first_field)
 
15552
      {
 
15553
        real_fields= 1;                         // End of hidden fields
 
15554
        ref_array= ref_array_start;
 
15555
      }
 
15556
 
 
15557
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15558
          (!((Item_sum*) item)->depended_from() ||
 
15559
           ((Item_sum *)item)->depended_from() == select_lex))
 
15560
 
 
15561
      {
 
15562
        /*
 
15563
          This is a top level summary function that must be replaced with
 
15564
          a sum function that is reset for this level.
 
15565
 
 
15566
          NOTE: This code creates an object which is not that nice in a
 
15567
          sub select.  Fortunately it's not common to have rollup in
 
15568
          sub selects.
 
15569
        */
 
15570
        item= item->copy_or_same(session);
 
15571
        ((Item_sum*) item)->make_unique();
 
15572
        *(*func)= (Item_sum*) item;
 
15573
        (*func)++;
 
15574
      }
 
15575
      else
 
15576
      {
 
15577
        /* Check if this is something that is part of this group by */
 
15578
        order_st *group_tmp;
 
15579
        for (group_tmp= start_group, i= pos ;
 
15580
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15581
        {
 
15582
          if (*group_tmp->item == item)
 
15583
          {
 
15584
            /*
 
15585
              This is an element that is used by the GROUP BY and should be
 
15586
              set to NULL in this level
 
15587
            */
 
15588
            Item_null_result *null_item= new (session->mem_root) Item_null_result();
 
15589
            if (!null_item)
 
15590
              return 1;
 
15591
            item->maybe_null= 1;                // Value will be null sometimes
 
15592
            null_item->result_field= item->get_tmp_table_field();
 
15593
            item= null_item;
 
15594
            break;
 
15595
          }
 
15596
        }
 
15597
      }
 
15598
      *ref_array= item;
 
15599
      if (real_fields)
 
15600
      {
 
15601
        (void) new_it++;                        // Point to next item
 
15602
        new_it.replace(item);                   // Replace previous
 
15603
        ref_array++;
 
15604
      }
 
15605
      else
 
15606
        ref_array--;
 
15607
    }
 
15608
  }
 
15609
  sum_funcs_end[0]= *func;                      // Point to last function
 
15610
  return 0;
 
15611
}
 
15612
 
 
15613
/**
 
15614
  Send all rollup levels higher than the current one to the client.
 
15615
 
 
15616
  @b SAMPLE
 
15617
    @code
 
15618
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15619
  @endcode
 
15620
 
 
15621
  @param idx            Level we are on:
 
15622
                        - 0 = Total sum level
 
15623
                        - 1 = First group changed  (a)
 
15624
                        - 2 = Second group changed (a,b)
 
15625
 
 
15626
  @retval
 
15627
    0   ok
 
15628
  @retval
 
15629
    1   If send_data_failed()
 
15630
*/
 
15631
 
 
15632
int JOIN::rollup_send_data(uint32_t idx)
 
15633
{
 
15634
  uint32_t i;
 
15635
  for (i= send_group_parts ; i-- > idx ; )
 
15636
  {
 
15637
    /* Get reference pointers to sum functions in place */
 
15638
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15639
           ref_pointer_array_size);
 
15640
    if ((!having || having->val_int()))
 
15641
    {
 
15642
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15643
          result->send_data(rollup.fields[i]))
 
15644
        return 1;
 
15645
      send_records++;
 
15646
    }
 
15647
  }
 
15648
  /* Restore ref_pointer_array */
 
15649
  set_items_ref_array(current_ref_pointer_array);
 
15650
  return 0;
 
15651
}
 
15652
 
 
15653
/**
 
15654
  Write all rollup levels higher than the current one to a temp table.
 
15655
 
 
15656
  @b SAMPLE
 
15657
    @code
 
15658
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15659
  @endcode
 
15660
 
 
15661
  @param idx                 Level we are on:
 
15662
                               - 0 = Total sum level
 
15663
                               - 1 = First group changed  (a)
 
15664
                               - 2 = Second group changed (a,b)
 
15665
  @param table               reference to temp table
 
15666
 
 
15667
  @retval
 
15668
    0   ok
 
15669
  @retval
 
15670
    1   if write_data_failed()
 
15671
*/
 
15672
 
 
15673
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15674
{
 
15675
  uint32_t i;
 
15676
  for (i= send_group_parts ; i-- > idx ; )
 
15677
  {
 
15678
    /* Get reference pointers to sum functions in place */
 
15679
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15680
           ref_pointer_array_size);
 
15681
    if ((!having || having->val_int()))
 
15682
    {
 
15683
      int write_error;
 
15684
      Item *item;
 
15685
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15686
      while ((item= it++))
 
15687
      {
 
15688
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15689
          item->save_in_result_field(1);
 
15690
      }
 
15691
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15692
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15693
      {
 
15694
        if (create_myisam_from_heap(session, table_arg,
 
15695
                                    tmp_table_param.start_recinfo,
 
15696
                                    &tmp_table_param.recinfo,
 
15697
                                    write_error, 0))
 
15698
          return 1;
 
15699
      }
 
15700
    }
 
15701
  }
 
15702
  /* Restore ref_pointer_array */
 
15703
  set_items_ref_array(current_ref_pointer_array);
 
15704
  return 0;
 
15705
}
 
15706
 
 
15707
/**
 
15708
  clear results if there are not rows found for group
 
15709
  (end_send_group/end_write_group)
 
15710
*/
 
15711
 
 
15712
void JOIN::clear()
 
15713
{
 
15714
  clear_tables(this);
 
15715
  copy_fields(&tmp_table_param);
 
15716
 
 
15717
  if (sum_funcs)
 
15718
  {
 
15719
    Item_sum *func, **func_ptr= sum_funcs;
 
15720
    while ((func= *(func_ptr++)))
 
15721
      func->clear();
 
15722
  }
 
15723
}
 
15724
 
 
15725
/**
 
15726
  EXPLAIN handling.
 
15727
 
 
15728
  Send a description about what how the select will be done to stdout.
 
15729
*/
 
15730
 
 
15731
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15732
                     bool distinct,const char *message)
 
15733
{
 
15734
  List<Item> field_list;
 
15735
  List<Item> item_list;
 
15736
  Session *session=join->session;
 
15737
  select_result *result=join->result;
 
15738
  Item *item_null= new Item_null();
 
15739
  const CHARSET_INFO * const cs= system_charset_info;
 
15740
  int quick_type;
 
15741
  /* Don't log this into the slow query log */
 
15742
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15743
  join->unit->offset_limit_cnt= 0;
 
15744
 
 
15745
  /*
 
15746
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15747
    EXPLAIN column types as they're "defined" in Session::send_explain_fields()
 
15748
  */
 
15749
  if (message)
 
15750
  {
 
15751
    item_list.push_back(new Item_int((int32_t)
 
15752
                                     join->select_lex->select_number));
 
15753
    item_list.push_back(new Item_string(join->select_lex->type,
 
15754
                                        strlen(join->select_lex->type), cs));
 
15755
    for (uint32_t i=0 ; i < 7; i++)
 
15756
      item_list.push_back(item_null);
 
15757
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15758
      item_list.push_back(item_null);
 
15759
 
 
15760
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15761
    if (result->send_data(item_list))
 
15762
      join->error= 1;
 
15763
  }
 
15764
  else if (join->select_lex == join->unit->fake_select_lex)
 
15765
  {
 
15766
    /*
 
15767
      here we assume that the query will return at least two rows, so we
 
15768
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15769
      and no filesort will be actually done, but executing all selects in
 
15770
      the UNION to provide precise EXPLAIN information will hardly be
 
15771
      appreciated :)
 
15772
    */
 
15773
    char table_name_buffer[NAME_LEN];
 
15774
    item_list.empty();
 
15775
    /* id */
 
15776
    item_list.push_back(new Item_null);
 
15777
    /* select_type */
 
15778
    item_list.push_back(new Item_string(join->select_lex->type,
 
15779
                                        strlen(join->select_lex->type),
 
15780
                                        cs));
 
15781
    /* table */
 
15782
    {
 
15783
      Select_Lex *sl= join->unit->first_select();
 
15784
      uint32_t len= 6, lastop= 0;
 
15785
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15786
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15787
      {
 
15788
        len+= lastop;
 
15789
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15790
                         "%u,", sl->select_number);
 
15791
      }
 
15792
      if (sl || len + lastop >= NAME_LEN)
 
15793
      {
 
15794
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15795
        len+= 4;
 
15796
      }
 
15797
      else
 
15798
      {
 
15799
        len+= lastop;
 
15800
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15801
      }
 
15802
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15803
    }
 
15804
    /* type */
 
15805
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15806
                                          strlen(join_type_str[JT_ALL]),
 
15807
                                          cs));
 
15808
    /* possible_keys */
 
15809
    item_list.push_back(item_null);
 
15810
    /* key*/
 
15811
    item_list.push_back(item_null);
 
15812
    /* key_len */
 
15813
    item_list.push_back(item_null);
 
15814
    /* ref */
 
15815
    item_list.push_back(item_null);
 
15816
    /* in_rows */
 
15817
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15818
      item_list.push_back(item_null);
 
15819
    /* rows */
 
15820
    item_list.push_back(item_null);
 
15821
    /* extra */
 
15822
    if (join->unit->global_parameters->order_list.first)
 
15823
      item_list.push_back(new Item_string("Using filesort",
 
15824
                                          14, cs));
 
15825
    else
 
15826
      item_list.push_back(new Item_string("", 0, cs));
 
15827
 
 
15828
    if (result->send_data(item_list))
 
15829
      join->error= 1;
 
15830
  }
 
15831
  else
 
15832
  {
 
15833
    table_map used_tables=0;
 
15834
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15835
    {
 
15836
      JOIN_TAB *tab=join->join_tab+i;
 
15837
      Table *table=tab->table;
 
15838
      TableList *table_list= tab->table->pos_in_table_list;
 
15839
      char buff[512];
 
15840
      char buff1[512], buff2[512], buff3[512];
 
15841
      char keylen_str_buf[64];
 
15842
      String extra(buff, sizeof(buff),cs);
 
15843
      char table_name_buffer[NAME_LEN];
 
15844
      String tmp1(buff1,sizeof(buff1),cs);
 
15845
      String tmp2(buff2,sizeof(buff2),cs);
 
15846
      String tmp3(buff3,sizeof(buff3),cs);
 
15847
      extra.length(0);
 
15848
      tmp1.length(0);
 
15849
      tmp2.length(0);
 
15850
      tmp3.length(0);
 
15851
 
 
15852
      quick_type= -1;
 
15853
      item_list.empty();
 
15854
      /* id */
 
15855
      item_list.push_back(new Item_uint((uint32_t)
 
15856
                                       join->select_lex->select_number));
 
15857
      /* select_type */
 
15858
      item_list.push_back(new Item_string(join->select_lex->type,
 
15859
                                          strlen(join->select_lex->type),
 
15860
                                          cs));
 
15861
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15862
      {
 
15863
        quick_type= tab->select->quick->get_type();
 
15864
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15865
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15866
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15867
          tab->type = JT_INDEX_MERGE;
 
15868
        else
 
15869
          tab->type = JT_RANGE;
 
15870
      }
 
15871
      /* table */
 
15872
      if (table->derived_select_number)
 
15873
      {
 
15874
        /* Derived table name generation */
 
15875
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15876
                          "<derived%u>",
 
15877
                          table->derived_select_number);
 
15878
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15879
      }
 
15880
      else
 
15881
      {
 
15882
        TableList *real_table= table->pos_in_table_list;
 
15883
        item_list.push_back(new Item_string(real_table->alias,
 
15884
                                            strlen(real_table->alias),
 
15885
                                            cs));
 
15886
      }
 
15887
      /* "type" column */
 
15888
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15889
                                          strlen(join_type_str[tab->type]),
 
15890
                                          cs));
 
15891
      /* Build "possible_keys" value and add it to item_list */
 
15892
      if (!tab->keys.is_clear_all())
 
15893
      {
 
15894
        uint32_t j;
 
15895
        for (j=0 ; j < table->s->keys ; j++)
 
15896
        {
 
15897
          if (tab->keys.is_set(j))
 
15898
          {
 
15899
            if (tmp1.length())
 
15900
              tmp1.append(',');
 
15901
            tmp1.append(table->key_info[j].name,
 
15902
                        strlen(table->key_info[j].name),
 
15903
                        system_charset_info);
 
15904
          }
 
15905
        }
 
15906
      }
 
15907
      if (tmp1.length())
 
15908
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15909
      else
 
15910
        item_list.push_back(item_null);
 
15911
 
 
15912
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15913
      if (tab->ref.key_parts)
 
15914
      {
 
15915
        KEY *key_info=table->key_info+ tab->ref.key;
 
15916
        register uint32_t length;
 
15917
        item_list.push_back(new Item_string(key_info->name,
 
15918
                                            strlen(key_info->name),
 
15919
                                            system_charset_info));
 
15920
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
 
15921
                keylen_str_buf;
 
15922
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15923
                                            system_charset_info));
 
15924
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15925
        {
 
15926
          if (tmp2.length())
 
15927
            tmp2.append(',');
 
15928
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15929
                      system_charset_info);
 
15930
        }
 
15931
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15932
      }
 
15933
      else if (tab->type == JT_NEXT)
 
15934
      {
 
15935
        KEY *key_info=table->key_info+ tab->index;
 
15936
        register uint32_t length;
 
15937
        item_list.push_back(new Item_string(key_info->name,
 
15938
                                            strlen(key_info->name),cs));
 
15939
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
 
15940
                keylen_str_buf;
 
15941
        item_list.push_back(new Item_string(keylen_str_buf,
 
15942
                                            length,
 
15943
                                            system_charset_info));
 
15944
        item_list.push_back(item_null);
 
15945
      }
 
15946
      else if (tab->select && tab->select->quick)
 
15947
      {
 
15948
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15949
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15950
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15951
        item_list.push_back(item_null);
 
15952
      }
 
15953
      else
 
15954
      {
 
15955
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15956
        {
 
15957
          const char *tmp_buff;
 
15958
          int f_idx;
 
15959
          if (table_list->has_db_lookup_value)
 
15960
          {
 
15961
            f_idx= table_list->schema_table->idx_field1;
 
15962
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15963
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15964
          }
 
15965
          if (table_list->has_table_lookup_value)
 
15966
          {
 
15967
            if (table_list->has_db_lookup_value)
 
15968
              tmp2.append(',');
 
15969
            f_idx= table_list->schema_table->idx_field2;
 
15970
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15971
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15972
          }
 
15973
          if (tmp2.length())
 
15974
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15975
          else
 
15976
            item_list.push_back(item_null);
 
15977
        }
 
15978
        else
 
15979
          item_list.push_back(item_null);
 
15980
        item_list.push_back(item_null);
 
15981
        item_list.push_back(item_null);
 
15982
      }
 
15983
 
 
15984
      /* Add "rows" field to item_list. */
 
15985
      if (table_list->schema_table)
 
15986
      {
 
15987
        /* in_rows */
 
15988
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15989
          item_list.push_back(item_null);
 
15990
        /* rows */
 
15991
        item_list.push_back(item_null);
 
15992
      }
 
15993
      else
 
15994
      {
 
15995
        double examined_rows;
 
15996
        if (tab->select && tab->select->quick)
 
15997
          examined_rows= rows2double(tab->select->quick->records);
 
15998
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15999
          examined_rows= rows2double(tab->limit ? tab->limit :
 
16000
                                     tab->table->file->records());
 
16001
        else
 
16002
          examined_rows= join->best_positions[i].records_read;
 
16003
 
 
16004
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
 
16005
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
16006
 
 
16007
        /* Add "filtered" field to item_list. */
 
16008
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
16009
        {
 
16010
          float f= 0.0;
 
16011
          if (examined_rows)
 
16012
            f= (float) (100.0 * join->best_positions[i].records_read /
 
16013
                        examined_rows);
 
16014
          item_list.push_back(new Item_float(f, 2));
 
16015
        }
 
16016
      }
 
16017
 
 
16018
      /* Build "Extra" field and add it to item_list. */
 
16019
      bool key_read=table->key_read;
 
16020
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
16021
          table->covering_keys.is_set(tab->index))
 
16022
        key_read=1;
 
16023
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
16024
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
16025
        key_read=1;
 
16026
 
 
16027
      if (tab->info)
 
16028
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16029
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16030
      {
 
16031
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16032
          extra.append(STRING_WITH_LEN("; Using index"));
 
16033
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16034
          extra.append(STRING_WITH_LEN("; Using where"));
 
16035
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16036
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16037
        /* Skip initial "; "*/
 
16038
        const char *str= extra.ptr();
 
16039
        uint32_t len= extra.length();
 
16040
        if (len)
 
16041
        {
 
16042
          str += 2;
 
16043
          len -= 2;
 
16044
        }
 
16045
        item_list.push_back(new Item_string(str, len, cs));
 
16046
      }
 
16047
      else
 
16048
      {
 
16049
        uint32_t keyno= MAX_KEY;
 
16050
        if (tab->ref.key_parts)
 
16051
          keyno= tab->ref.key;
 
16052
        else if (tab->select && tab->select->quick)
 
16053
          keyno = tab->select->quick->index;
 
16054
 
 
16055
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16056
            table->file->pushed_idx_cond)
 
16057
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16058
 
 
16059
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
16060
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16061
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16062
        {
 
16063
          extra.append(STRING_WITH_LEN("; Using "));
 
16064
          tab->select->quick->add_info_string(&extra);
 
16065
        }
 
16066
          if (tab->select)
 
16067
        {
 
16068
          if (tab->use_quick == 2)
 
16069
          {
 
16070
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16071
            char buf[MAX_KEY / 4 + 1];
 
16072
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16073
                                         "record (index map: 0x"));
 
16074
            extra.append(tab->keys.print(buf));
 
16075
            extra.append(')');
 
16076
          }
 
16077
          else if (tab->select->cond)
 
16078
          {
 
16079
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16080
 
 
16081
            if (session->variables.engine_condition_pushdown && pushed_cond)
 
16082
            {
 
16083
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16084
                                           "condition"));
 
16085
              if (session->lex->describe & DESCRIBE_EXTENDED)
 
16086
              {
 
16087
                extra.append(STRING_WITH_LEN(": "));
 
16088
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16089
              }
 
16090
            }
 
16091
            else
 
16092
              extra.append(STRING_WITH_LEN("; Using where"));
 
16093
          }
 
16094
        }
 
16095
        if (key_read)
 
16096
        {
 
16097
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16098
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16099
          else
 
16100
            extra.append(STRING_WITH_LEN("; Using index"));
 
16101
        }
 
16102
        if (table->reginfo.not_exists_optimize)
 
16103
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16104
 
 
16105
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16106
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16107
             HA_MRR_USE_DEFAULT_IMPL))
 
16108
        {
 
16109
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16110
        }
 
16111
 
 
16112
        if (table_list->schema_table &&
 
16113
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16114
        {
 
16115
          if (!table_list->table_open_method)
 
16116
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16117
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16118
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16119
          else
 
16120
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16121
          if (table_list->has_db_lookup_value &&
 
16122
              table_list->has_table_lookup_value)
 
16123
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16124
          else if (table_list->has_db_lookup_value ||
 
16125
                   table_list->has_table_lookup_value)
 
16126
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16127
          else
 
16128
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16129
        }
 
16130
        if (need_tmp_table)
 
16131
        {
 
16132
          need_tmp_table=0;
 
16133
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16134
        }
 
16135
        if (need_order)
 
16136
        {
 
16137
          need_order=0;
 
16138
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16139
        }
 
16140
        if (distinct & test_all_bits(used_tables,session->used_tables))
 
16141
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16142
 
 
16143
        if (tab->insideout_match_tab)
 
16144
        {
 
16145
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16146
        }
 
16147
 
 
16148
        if (tab->flush_weedout_table)
 
16149
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16150
        else if (tab->check_weed_out_table)
 
16151
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16152
        else if (tab->do_firstmatch)
 
16153
        {
 
16154
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16155
          Table *prev_table=tab->do_firstmatch->table;
 
16156
          if (prev_table->derived_select_number)
 
16157
          {
 
16158
            char namebuf[NAME_LEN];
 
16159
            /* Derived table name generation */
 
16160
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16161
                              "<derived%u>",
 
16162
                              prev_table->derived_select_number);
 
16163
            extra.append(namebuf, len);
 
16164
          }
 
16165
          else
 
16166
            extra.append(prev_table->pos_in_table_list->alias);
 
16167
          extra.append(STRING_WITH_LEN(")"));
 
16168
        }
 
16169
 
 
16170
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16171
        {
 
16172
          if (tab->ref.cond_guards[part])
 
16173
          {
 
16174
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16175
            break;
 
16176
          }
 
16177
        }
 
16178
 
 
16179
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16180
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16181
 
 
16182
        /* Skip initial "; "*/
 
16183
        const char *str= extra.ptr();
 
16184
        uint32_t len= extra.length();
 
16185
        if (len)
 
16186
        {
 
16187
          str += 2;
 
16188
          len -= 2;
 
16189
        }
 
16190
        item_list.push_back(new Item_string(str, len, cs));
 
16191
      }
 
16192
      // For next iteration
 
16193
      used_tables|=table->map;
 
16194
      if (result->send_data(item_list))
 
16195
        join->error= 1;
 
16196
    }
 
16197
  }
 
16198
  for (Select_Lex_Unit *unit= join->select_lex->first_inner_unit();
 
16199
       unit;
 
16200
       unit= unit->next_unit())
 
16201
  {
 
16202
    if (mysql_explain_union(session, unit, result))
 
16203
      return;
 
16204
  }
 
16205
  return;
 
16206
}
 
16207
 
 
16208
 
 
16209
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit, select_result *result)
 
16210
{
 
16211
  bool res= false;
 
16212
  Select_Lex *first= unit->first_select();
 
16213
 
 
16214
  for (Select_Lex *sl= first;
 
16215
       sl;
 
16216
       sl= sl->next_select())
 
16217
  {
 
16218
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16219
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16220
    sl->type= (((&session->lex->select_lex)==sl)?
 
16221
               (sl->first_inner_unit() || sl->next_select() ?
 
16222
                "PRIMARY" : "SIMPLE"):
 
16223
               ((sl == first)?
 
16224
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16225
                 "DERIVED":
 
16226
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16227
                  "DEPENDENT SUBQUERY":
 
16228
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16229
                   "SUBQUERY"))):
 
16230
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16231
                 "DEPENDENT UNION":
 
16232
                 uncacheable?"UNCACHEABLE UNION":
 
16233
                 "UNION")));
 
16234
    sl->options|= SELECT_DESCRIBE;
 
16235
  }
 
16236
  if (unit->is_union())
 
16237
  {
 
16238
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16239
    unit->fake_select_lex->type= "UNION RESULT";
 
16240
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16241
    if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16242
      res= unit->exec();
 
16243
    res|= unit->cleanup();
 
16244
  }
 
16245
  else
 
16246
  {
 
16247
    session->lex->current_select= first;
 
16248
    unit->set_limit(unit->global_parameters);
 
16249
    res= mysql_select(session, &first->ref_pointer_array,
 
16250
                        (TableList*) first->table_list.first,
 
16251
                        first->with_wild, first->item_list,
 
16252
                        first->where,
 
16253
                        first->order_list.elements +
 
16254
                        first->group_list.elements,
 
16255
                        (order_st*) first->order_list.first,
 
16256
                        (order_st*) first->group_list.first,
 
16257
                        first->having,
 
16258
                        (order_st*) session->lex->proc_list.first,
 
16259
                        first->options | session->options | SELECT_DESCRIBE,
 
16260
                        result, unit, first);
 
16261
  }
 
16262
  return(res || session->is_error());
 
16263
}
 
16264
 
 
16265
 
6257
16266
static void print_table_array(Session *session, String *str, TableList **table,
6258
16267
                              TableList **end)
6259
16268
{
6269
16278
    }
6270
16279
    else if (curr->straight)
6271
16280
      str->append(STRING_WITH_LEN(" straight_join "));
 
16281
    else if (curr->sj_inner_tables)
 
16282
      str->append(STRING_WITH_LEN(" semi join "));
6272
16283
    else
6273
16284
      str->append(STRING_WITH_LEN(" join "));
6274
16285
    curr->print(session, str, QT_ORDINARY);
6281
16292
  }
6282
16293
}
6283
16294
 
 
16295
 
6284
16296
/**
6285
16297
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
16298
  @param session     thread handler
6287
16299
  @param str     string where table should be printed
6288
16300
  @param tables  list of tables in join
6289
16301
  @query_type    type of the query is being generated
6290
16302
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
16303
 
 
16304
static void print_join(Session *session, String *str,
 
16305
                       List<TableList> *tables, enum_query_type)
6293
16306
{
6294
16307
  /* 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*) *
 
16308
  List_iterator_fast<TableList> ti(*tables);
 
16309
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
16310
                                                tables->elements);
6298
16311
  if (table == 0)
6299
16312
    return;  // out of memory
6300
16313
 
6301
16314
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6302
16315
    *t= ti++;
 
16316
 
 
16317
  /*
 
16318
    If the first table is a semi-join nest, swap it with something that is
 
16319
    not a semi-join nest.
 
16320
  */
 
16321
  if ((*table)->sj_inner_tables)
 
16322
  {
 
16323
    TableList **end= table + tables->elements;
 
16324
    for (TableList **t2= table; t2!=end; t2++)
 
16325
    {
 
16326
      if (!(*t2)->sj_inner_tables)
 
16327
      {
 
16328
        TableList *tmp= *t2;
 
16329
        *t2= *table;
 
16330
        *table= tmp;
 
16331
        break;
 
16332
      }
 
16333
    }
 
16334
  }
6303
16335
  assert(tables->elements >= 1);
6304
16336
  print_table_array(session, str, table, table + tables->elements);
6305
16337
}
6306
16338
 
 
16339
 
 
16340
/**
 
16341
  @brief Print an index hint
 
16342
 
 
16343
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16344
 
 
16345
  @param      session         the current thread
 
16346
  @param[out] str         appends the index hint here
 
16347
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16348
                          "FORCE INDEX"|"IGNORE INDEX")
 
16349
  @param      hint_length the length of the string in 'hint'
 
16350
  @param      indexes     a list of index names for the hint
 
16351
*/
 
16352
 
 
16353
void
 
16354
Index_hint::print(Session *session, String *str)
 
16355
{
 
16356
  switch (type)
 
16357
  {
 
16358
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16359
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16360
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16361
  }
 
16362
  str->append (STRING_WITH_LEN(" ("));
 
16363
  if (key_name.length)
 
16364
  {
 
16365
    if (session && is_primary_key_name(key_name.str))
 
16366
      str->append(is_primary_key_name(key_name.str));
 
16367
    else
 
16368
      str->append_identifier(key_name.str, key_name.length);
 
16369
  }
 
16370
  str->append(')');
 
16371
}
 
16372
 
 
16373
 
 
16374
/**
 
16375
  Print table as it should be in join list.
 
16376
 
 
16377
  @param str   string where table should be printed
 
16378
*/
 
16379
 
 
16380
void TableList::print(Session *session, String *str, enum_query_type query_type)
 
16381
{
 
16382
  if (nested_join)
 
16383
  {
 
16384
    str->append('(');
 
16385
    print_join(session, str, &nested_join->join_list, query_type);
 
16386
    str->append(')');
 
16387
  }
 
16388
  else
 
16389
  {
 
16390
    const char *cmp_name;                         // Name to compare with alias
 
16391
    if (derived)
 
16392
    {
 
16393
      // A derived table
 
16394
      str->append('(');
 
16395
      derived->print(str, query_type);
 
16396
      str->append(')');
 
16397
      cmp_name= "";                               // Force printing of alias
 
16398
    }
 
16399
    else
 
16400
    {
 
16401
      // A normal table
 
16402
      {
 
16403
        str->append_identifier(db, db_length);
 
16404
        str->append('.');
 
16405
      }
 
16406
      if (schema_table)
 
16407
      {
 
16408
        str->append_identifier(schema_table_name, strlen(schema_table_name));
 
16409
        cmp_name= schema_table_name;
 
16410
      }
 
16411
      else
 
16412
      {
 
16413
        str->append_identifier(table_name, table_name_length);
 
16414
        cmp_name= table_name;
 
16415
      }
 
16416
    }
 
16417
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16418
    {
 
16419
 
 
16420
      if (alias && alias[0])
 
16421
      {
 
16422
        str->append(' ');
 
16423
 
 
16424
        string t_alias(alias);
 
16425
        if (lower_case_table_names== 1)
 
16426
          transform(t_alias.begin(), t_alias.end(),
 
16427
                    t_alias.begin(), ::tolower);
 
16428
 
 
16429
        str->append_identifier(t_alias.c_str(), t_alias.length());
 
16430
      }
 
16431
 
 
16432
    }
 
16433
 
 
16434
    if (index_hints)
 
16435
    {
 
16436
      List_iterator<Index_hint> it(*index_hints);
 
16437
      Index_hint *hint;
 
16438
 
 
16439
      while ((hint= it++))
 
16440
      {
 
16441
        str->append (STRING_WITH_LEN(" "));
 
16442
        hint->print (session, str);
 
16443
      }
 
16444
    }
 
16445
  }
 
16446
}
 
16447
 
 
16448
 
6307
16449
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6308
16450
{
6309
16451
  /* QQ: session may not be set for sub queries, but this should be fixed */
6310
 
  if(not session)
 
16452
  if (!session)
6311
16453
    session= current_session;
6312
16454
 
6313
 
 
6314
16455
  str->append(STRING_WITH_LEN("select "));
6315
16456
 
6316
16457
  /* First add options */
6317
16458
  if (options & SELECT_STRAIGHT_JOIN)
6318
16459
    str->append(STRING_WITH_LEN("straight_join "));
 
16460
  if ((session->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16461
      (this == &session->lex->select_lex))
 
16462
    str->append(STRING_WITH_LEN("high_priority "));
6319
16463
  if (options & SELECT_DISTINCT)
6320
16464
    str->append(STRING_WITH_LEN("distinct "));
6321
16465
  if (options & SELECT_SMALL_RESULT)
6329
16473
 
6330
16474
  //Item List
6331
16475
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
16476
  List_iterator_fast<Item> it(item_list);
6333
16477
  Item *item;
6334
16478
  while ((item= it++))
6335
16479
  {
6342
16486
 
6343
16487
  /*
6344
16488
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
16489
    TODO: support USING/FORCE/IGNORE index
6346
16490
  */
6347
16491
  if (table_list.elements)
6348
16492
  {
6376
16520
  if (group_list.elements)
6377
16521
  {
6378
16522
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
16523
    print_order(str, (order_st *) group_list.first, query_type);
6380
16524
    switch (olap)
6381
16525
    {
6382
16526
      case CUBE_TYPE:
6407
16551
  if (order_list.elements)
6408
16552
  {
6409
16553
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
16554
    print_order(str, (order_st *) order_list.first, query_type);
6411
16555
  }
6412
16556
 
6413
16557
  // limit
6416
16560
  // PROCEDURE unsupported here
6417
16561
}
6418
16562
 
 
16563
 
 
16564
/**
 
16565
  change select_result object of JOIN.
 
16566
 
 
16567
  @param res            new select_result object
 
16568
 
 
16569
  @retval
 
16570
    false   OK
 
16571
  @retval
 
16572
    true    error
 
16573
*/
 
16574
 
 
16575
bool JOIN::change_result(select_result *res)
 
16576
{
 
16577
  result= res;
 
16578
  if (result->prepare(fields_list, select_lex->master_unit()))
 
16579
  {
 
16580
    return(true);
 
16581
  }
 
16582
  return(false);
 
16583
}
 
16584
 
6419
16585
/**
6420
16586
  @} (end of group Query_Optimizer)
6421
16587
*/
6422
 
 
6423
 
} /* namespace drizzled */