~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Jay Pipes
  • Date: 2008-12-18 15:55:03 UTC
  • mto: This revision was merged to the branch mainline in revision 717.
  • Revision ID: jpipes@serialcoder-20081218155503-u45ygyunrdyyvquq
Fix for Bug#308457.  Gave UTF8 enclosure and escape character on LOAD DATA INFILE and changed the error message to be more descriptive

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>
40
38
#include <drizzled/show.h>
41
39
#include <drizzled/item/cache.h>
42
40
#include <drizzled/item/cmpfunc.h>
43
 
#include <drizzled/item/copy_string.h>
44
41
#include <drizzled/item/uint.h>
45
42
#include <drizzled/cached_item.h>
46
43
#include <drizzled/sql_base.h>
48
45
#include <drizzled/check_stack_overrun.h>
49
46
#include <drizzled/lock.h>
50
47
#include <drizzled/item/outer_ref.h>
51
 
#include <drizzled/index_hint.h>
52
 
#include <drizzled/records.h>
53
 
#include <drizzled/internal/iocache.h>
54
 
#include <drizzled/drizzled.h>
55
 
#include <drizzled/plugin/storage_engine.h>
56
 
 
57
 
#include <drizzled/sql_union.h>
58
 
#include <drizzled/optimizer/key_field.h>
59
 
#include <drizzled/optimizer/position.h>
60
 
#include <drizzled/optimizer/sargable_param.h>
61
 
#include <drizzled/optimizer/key_use.h>
62
 
#include <drizzled/optimizer/range.h>
63
 
#include <drizzled/optimizer/quick_range_select.h>
64
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
65
 
 
66
 
#include <drizzled/filesort.h>
67
 
#include <drizzled/sql_lex.h>
68
 
#include <drizzled/session.h>
69
 
#include <drizzled/sort_field.h>
70
 
#include <drizzled/select_result.h>
71
 
 
72
 
using namespace std;
73
 
 
74
 
namespace drizzled
75
 
{
76
 
 
77
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
 
48
 
 
49
 
 
50
#include CMATH_H
 
51
 
 
52
#if defined(CMATH_NAMESPACE)
 
53
using namespace CMATH_NAMESPACE;
 
54
#endif
 
55
 
 
56
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
57
                              "MAYBE_REF","ALL","range","index",
 
58
                              "ref_or_null","unique_subquery","index_subquery",
 
59
                              "index_merge"
 
60
};
 
61
 
 
62
struct st_sargable_param;
 
63
 
 
64
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
65
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
66
                                 DYNAMIC_ARRAY *keyuse);
 
67
static bool update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,
 
68
                                JOIN_TAB *join_tab,
 
69
                                uint32_t tables, COND *conds,
 
70
                                COND_EQUAL *cond_equal,
 
71
                                table_map table_map, SELECT_LEX *select_lex,
 
72
                                st_sargable_param **sargables);
 
73
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
74
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
75
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
76
                               table_map used_tables);
 
77
static bool choose_plan(JOIN *join,table_map join_tables);
 
78
 
 
79
static void best_access_path(JOIN *join, JOIN_TAB *s, Session *session,
 
80
                             table_map remaining_tables, uint32_t idx,
 
81
                             double record_count, double read_time);
 
82
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
83
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
84
                             uint32_t depth, uint32_t prune_level);
 
85
static bool best_extension_by_limited_search(JOIN *join,
 
86
                                             table_map remaining_tables,
 
87
                                             uint32_t idx, double record_count,
 
88
                                             double read_time, uint32_t depth,
 
89
                                             uint32_t prune_level);
 
90
static uint32_t determine_search_depth(JOIN* join);
 
91
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
92
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
93
/*
 
94
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
95
  tested and approved.
 
96
*/
 
97
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
98
                      double record_count,double read_time);
 
99
static uint32_t cache_record_length(JOIN *join,uint32_t index);
 
100
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
101
static bool get_best_combination(JOIN *join);
 
102
static store_key *get_store_key(Session *session,
 
103
                                KEYUSE *keyuse, table_map used_tables,
 
104
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
 
105
                                uint32_t maybe_null);
 
106
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
107
static void make_outerjoin_info(JOIN *join);
 
108
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
109
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
110
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
111
static void update_depend_map(JOIN *join);
 
112
static void update_depend_map(JOIN *join, order_st *order);
 
113
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
114
                           bool change_list, bool *simple_order);
 
115
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
116
                            List<Item> &fields, bool send_row,
 
117
                            uint64_t select_options, const char *info,
 
118
                            Item *having);
78
119
static COND *build_equal_items(Session *session, COND *cond,
79
120
                               COND_EQUAL *inherited,
80
121
                               List<TableList> *join_list,
81
122
                               COND_EQUAL **cond_equal_ref);
82
 
 
 
123
static COND* substitute_for_best_equal_field(COND *cond,
 
124
                                             COND_EQUAL *cond_equal,
 
125
                                             void *table_join_idx);
 
126
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
127
                            COND *conds, bool top, bool in_sj);
 
128
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
129
static void restore_prev_nj_state(JOIN_TAB *last);
 
130
static void reset_nj_counters(List<TableList> *join_list);
 
131
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
132
                                          uint32_t first_unused);
 
133
 
 
134
static
 
135
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
136
static void restore_prev_sj_state(const table_map remaining_tables,
 
137
                                  const JOIN_TAB *tab);
 
138
 
 
139
static COND *optimize_cond(JOIN *join, COND *conds,
 
140
                           List<TableList> *join_list,
 
141
                           Item::cond_result *cond_value);
 
142
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
143
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
144
 
 
145
static enum_nested_loop_state
 
146
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
147
                     int error);
 
148
static enum_nested_loop_state
 
149
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
150
static enum_nested_loop_state
 
151
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
152
static enum_nested_loop_state
 
153
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
154
static enum_nested_loop_state
 
155
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
156
static enum_nested_loop_state
 
157
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
158
static enum_nested_loop_state
 
159
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
160
 
 
161
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
162
static int join_read_system(JOIN_TAB *tab);
 
163
static int join_read_const(JOIN_TAB *tab);
 
164
static int join_read_key(JOIN_TAB *tab);
 
165
static int join_read_always_key(JOIN_TAB *tab);
 
166
static int join_read_last_key(JOIN_TAB *tab);
 
167
static int join_no_more_records(READ_RECORD *info);
 
168
static int join_read_next(READ_RECORD *info);
 
169
static int join_read_next_different(READ_RECORD *info);
 
170
static int join_init_quick_read_record(JOIN_TAB *tab);
 
171
static int test_if_quick_select(JOIN_TAB *tab);
 
172
static int join_init_read_record(JOIN_TAB *tab);
 
173
static int join_read_first(JOIN_TAB *tab);
 
174
static int join_read_next_same(READ_RECORD *info);
 
175
static int join_read_next_same_diff(READ_RECORD *info);
 
176
static int join_read_last(JOIN_TAB *tab);
 
177
static int join_read_prev_same(READ_RECORD *info);
 
178
static int join_read_prev(READ_RECORD *info);
 
179
int join_read_always_key_or_null(JOIN_TAB *tab);
 
180
int join_read_next_same_or_null(READ_RECORD *info);
 
181
static COND *make_cond_for_table(COND *cond,table_map table,
 
182
                                 table_map used_table,
 
183
                                 bool exclude_expensive_cond);
83
184
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);
 
185
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
186
                                    ha_rows select_limit, bool no_changes,
 
187
                                    const key_map *map);
 
188
static bool list_contains_unique_index(Table *table,
 
189
                          bool (*find_func) (Field *, void *), void *data);
 
190
static bool find_field_in_item_list (Field *field, void *data);
 
191
static bool find_field_in_order_list (Field *field, void *data);
 
192
static int create_sort_index(Session *session, JOIN *join, order_st *order,
 
193
                             ha_rows filesort_limit, ha_rows select_limit,
 
194
                             bool is_order_by);
 
195
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
196
                             Item *having);
 
197
static int remove_dup_with_compare(Session *session, Table *entry, Field **field,
 
198
                                   uint32_t offset, Item *having);
 
199
static int remove_dup_with_hash_index(Session *session,Table *table,
 
200
                                      uint32_t field_count, Field **first_field,
 
201
                                      uint32_t key_length, Item *having);
 
202
static int join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count);
 
203
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
204
static bool store_record_in_cache(JOIN_CACHE *cache);
 
205
static void reset_cache_read(JOIN_CACHE *cache);
 
206
static void reset_cache_write(JOIN_CACHE *cache);
 
207
static void read_cached_record(JOIN_TAB *tab);
 
208
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
209
static order_st *create_distinct_group(Session *session, Item **ref_pointer_array,
 
210
                                    order_st *order, List<Item> &fields,
 
211
                                    List<Item> &all_fields,
 
212
                                    bool *all_order_by_fields_used);
 
213
static bool test_if_subpart(order_st *a,order_st *b);
 
214
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
215
static void calc_group_buffer(JOIN *join,order_st *group);
 
216
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
217
static bool alloc_group_fields(JOIN *join,order_st *group);
 
218
// Create list for using with tempory table
 
219
static bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
 
220
                                     List<Item> &new_list1,
 
221
                                     List<Item> &new_list2,
 
222
                                     uint32_t elements, List<Item> &items);
 
223
// Create list for using with tempory table
 
224
static bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
225
                                      List<Item> &new_list1,
 
226
                                      List<Item> &new_list2,
 
227
                                      uint32_t elements, List<Item> &items);
 
228
static void init_tmptable_sum_functions(Item_sum **func);
 
229
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
230
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
231
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
 
232
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
 
233
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
234
static bool update_sum_func(Item_sum **func);
 
235
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
236
                            bool distinct, const char *message=NULL);
 
237
static Item *remove_additional_cond(Item* conds);
 
238
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
239
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
240
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
241
                                       Item *new_cond, bool fix_fields);
92
242
 
93
243
static bool eval_const_cond(COND *cond)
94
244
{
95
245
    return ((Item_func*) cond)->val_int() ? true : false;
96
246
}
97
247
 
 
248
 
98
249
/*
99
250
  This is used to mark equalities that were made from i-th IN-equality.
100
251
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
103
254
const char *subq_sj_cond_name=
104
255
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
105
256
 
106
 
static bool copy_blobs(Field **ptr)
 
257
static bool bitmap_covers(const table_map x, const table_map y)
107
258
{
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;
 
259
  return !test(y & ~x);
115
260
}
116
261
 
117
262
/**
118
263
  This handles SELECT with and without UNION.
119
264
*/
 
265
 
120
266
bool handle_select(Session *session, LEX *lex, select_result *result,
121
 
                   uint64_t setup_tables_done_option)
 
267
                   ulong setup_tables_done_option)
122
268
{
123
269
  bool res;
124
 
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
270
  register SELECT_LEX *select_lex = &lex->select_lex;
 
271
  DRIZZLE_SELECT_START();
126
272
 
127
273
  if (select_lex->master_unit()->is_union() ||
128
274
      select_lex->master_unit()->fake_select_lex)
129
 
  {
130
 
    res= drizzle_union(session, lex, result, &lex->unit,
131
 
                       setup_tables_done_option);
132
 
  }
 
275
    res= mysql_union(session, lex, result, &lex->unit, setup_tables_done_option);
133
276
  else
134
277
  {
135
 
    Select_Lex_Unit *unit= &lex->unit;
 
278
    SELECT_LEX_UNIT *unit= &lex->unit;
136
279
    unit->set_limit(unit->global_parameters);
137
280
    session->session_marker= 0;
138
281
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
 
282
      'options' of mysql_select will be set in JOIN, as far as JOIN for
140
283
      every PS/SP execution new, we will not need reset this flag if
141
284
      setup_tables_done_option changed for next rexecution
142
285
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
 
286
    res= mysql_select(session, &select_lex->ref_pointer_array,
145
287
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
288
                      select_lex->with_wild, select_lex->item_list,
148
289
                      select_lex->where,
149
290
                      select_lex->order_list.elements +
150
291
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
292
                      (order_st*) select_lex->order_list.first,
 
293
                      (order_st*) select_lex->group_list.first,
153
294
                      select_lex->having,
 
295
                      (order_st*) lex->proc_list.first,
154
296
                      select_lex->options | session->options |
155
297
                      setup_tables_done_option,
156
298
                      result, unit, select_lex);
159
301
  if (unlikely(res))
160
302
    result->abort();
161
303
 
162
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
163
 
  return res;
 
304
  DRIZZLE_SELECT_END();
 
305
  return(res);
164
306
}
165
307
 
 
308
 
166
309
/*
167
310
  Fix fields referenced from inner selects.
168
311
 
203
346
    true  an error occured
204
347
    false ok
205
348
*/
206
 
bool fix_inner_refs(Session *session, 
207
 
                    List<Item> &all_fields, 
208
 
                    Select_Lex *select, 
209
 
                    Item **ref_pointer_array)
 
349
 
 
350
bool
 
351
fix_inner_refs(Session *session, List<Item> &all_fields, SELECT_LEX *select,
 
352
                 Item **ref_pointer_array)
210
353
{
211
354
  Item_outer_ref *ref;
212
355
  bool res= false;
213
356
  bool direct_ref= false;
214
357
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
358
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
359
  while ((ref= ref_it++))
217
360
  {
218
361
    Item *item= ref->outer_ref;
219
362
    Item **item_ref= ref->ref;
220
363
    Item_ref *new_ref;
221
364
    /*
222
 
      @todo this field item already might be present in the select list.
 
365
      TODO: this field item already might be present in the select list.
223
366
      In this case instead of adding new field item we could use an
224
367
      existing one. The change will lead to less operations for copying fields,
225
368
      smaller temporary tables and less data passed through filesort.
273
416
  return res;
274
417
}
275
418
 
 
419
/**
 
420
  Function to setup clauses without sum functions.
 
421
*/
 
422
inline int setup_without_group(Session *session, Item **ref_pointer_array,
 
423
                               TableList *tables,
 
424
                               TableList *leaves,
 
425
                               List<Item> &fields,
 
426
                               List<Item> &all_fields,
 
427
                               COND **conds,
 
428
                               order_st *order,
 
429
                               order_st *group, bool *hidden_group_fields)
 
430
{
 
431
  int res;
 
432
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
433
 
 
434
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
435
  res= setup_conds(session, tables, leaves, conds);
 
436
 
 
437
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
438
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
 
439
                          order);
 
440
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
441
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
 
442
                          group, hidden_group_fields);
 
443
  session->lex->allow_sum_func= save_allow_sum_func;
 
444
  return(res);
 
445
}
 
446
 
276
447
/*****************************************************************************
277
448
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
449
  mysql_select assumes that all tables are already opened
279
450
*****************************************************************************/
280
451
 
 
452
/**
 
453
  Prepare of whole select (including sub queries in future).
 
454
 
 
455
  @todo
 
456
    Add check of calculation of GROUP functions and fields:
 
457
    SELECT COUNT(*)+table.col1 from table1;
 
458
 
 
459
  @retval
 
460
    -1   on error
 
461
  @retval
 
462
    0   on success
 
463
*/
 
464
int
 
465
JOIN::prepare(Item ***rref_pointer_array,
 
466
              TableList *tables_init,
 
467
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
468
              order_st *order_init, order_st *group_init,
 
469
              Item *having_init,
 
470
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
471
              SELECT_LEX_UNIT *unit_arg)
 
472
{
 
473
  // to prevent double initialization on EXPLAIN
 
474
  if (optimized)
 
475
    return(0);
 
476
 
 
477
  conds= conds_init;
 
478
  order= order_init;
 
479
  group_list= group_init;
 
480
  having= having_init;
 
481
  proc_param= proc_param_init;
 
482
  tables_list= tables_init;
 
483
  select_lex= select_lex_arg;
 
484
  select_lex->join= this;
 
485
  join_list= &select_lex->top_join_list;
 
486
  union_part= unit_arg->is_union();
 
487
 
 
488
  session->lex->current_select->is_item_list_lookup= 1;
 
489
  /*
 
490
    If we have already executed SELECT, then it have not sense to prevent
 
491
    its table from update (see unique_table())
 
492
  */
 
493
  if (session->derived_tables_processing)
 
494
    select_lex->exclude_from_table_unique_test= true;
 
495
 
 
496
  /* Check that all tables, fields, conds and order are ok */
 
497
 
 
498
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
499
      setup_tables_and_check_access(session, &select_lex->context, join_list,
 
500
                                    tables_list, &select_lex->leaf_tables,
 
501
                                    false))
 
502
      return(-1);
 
503
 
 
504
  TableList *table_ptr;
 
505
  for (table_ptr= select_lex->leaf_tables;
 
506
       table_ptr;
 
507
       table_ptr= table_ptr->next_leaf)
 
508
    tables++;
 
509
 
 
510
  if (setup_wild(session, tables_list, fields_list, &all_fields, wild_num) ||
 
511
      select_lex->setup_ref_array(session, og_num) ||
 
512
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
513
                   &all_fields, 1) ||
 
514
      setup_without_group(session, (*rref_pointer_array), tables_list,
 
515
                          select_lex->leaf_tables, fields_list,
 
516
                          all_fields, &conds, order, group_list,
 
517
                          &hidden_group_fields))
 
518
    return(-1);                         /* purecov: inspected */
 
519
 
 
520
  ref_pointer_array= *rref_pointer_array;
 
521
 
 
522
  if (having)
 
523
  {
 
524
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
525
    session->where="having clause";
 
526
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
527
    select_lex->having_fix_field= 1;
 
528
    bool having_fix_rc= (!having->fixed &&
 
529
                         (having->fix_fields(session, &having) ||
 
530
                          having->check_cols(1)));
 
531
    select_lex->having_fix_field= 0;
 
532
    if (having_fix_rc || session->is_error())
 
533
      return(-1);                               /* purecov: inspected */
 
534
    session->lex->allow_sum_func= save_allow_sum_func;
 
535
  }
 
536
 
 
537
  {
 
538
    Item_subselect *subselect;
 
539
    Item_in_subselect *in_subs= NULL;
 
540
    /*
 
541
      Are we in a subquery predicate?
 
542
      TODO: the block below will be executed for every PS execution without need.
 
543
    */
 
544
    if ((subselect= select_lex->master_unit()->item))
 
545
    {
 
546
      bool do_semijoin= !test(session->variables.optimizer_switch &
 
547
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
548
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
549
        in_subs= (Item_in_subselect*)subselect;
 
550
 
 
551
      /*
 
552
        Check if we're in subquery that is a candidate for flattening into a
 
553
        semi-join (which is done done in flatten_subqueries()). The
 
554
        requirements are:
 
555
          1. Subquery predicate is an IN/=ANY subq predicate
 
556
          2. Subquery is a single SELECT (not a UNION)
 
557
          3. Subquery does not have GROUP BY or order_st BY
 
558
          4. Subquery does not use aggregate functions or HAVING
 
559
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
560
          6. No execution method was already chosen (by a prepared statement).
 
561
 
 
562
          (*). We are not in a subquery of a single table UPDATE/DELETE that
 
563
               doesn't have a JOIN (TODO: We should handle this at some
 
564
               point by switching to multi-table UPDATE/DELETE)
 
565
 
 
566
          (**). We're not in a confluent table-less subquery, like
 
567
                "SELECT 1".
 
568
      */
 
569
      if (in_subs &&                                                    // 1
 
570
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
571
          !select_lex->group_list.elements && !order &&                 // 3
 
572
          !having && !select_lex->with_sum_func &&                      // 4
 
573
          session->session_marker &&                                            // 5
 
574
          select_lex->outer_select()->join &&                           // (*)
 
575
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
 
576
          do_semijoin &&
 
577
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
578
      {
 
579
        {
 
580
          if (!in_subs->left_expr->fixed &&
 
581
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
 
582
          {
 
583
            return(-1);
 
584
          }
 
585
          /*
 
586
            Check that the right part of the subselect contains no more than one
 
587
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
588
          */
 
589
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
590
             (select_lex->item_list.elements !=
 
591
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
592
          {
 
593
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
594
            return(-1);
 
595
          }
 
596
        }
 
597
 
 
598
        /* Register the subquery for further processing */
 
599
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
 
600
        in_subs->expr_join_nest= (TableList*)session->session_marker;
 
601
      }
 
602
      else
 
603
      {
 
604
        bool do_materialize= !test(session->variables.optimizer_switch &
 
605
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
606
        /*
 
607
          Check if the subquery predicate can be executed via materialization.
 
608
          The required conditions are:
 
609
          1. Subquery predicate is an IN/=ANY subq predicate
 
610
          2. Subquery is a single SELECT (not a UNION)
 
611
          3. Subquery is not a table-less query. In this case there is no
 
612
             point in materializing.
 
613
          4. Subquery predicate is a top-level predicate
 
614
             (this implies it is not negated)
 
615
             TODO: this is a limitation that should be lifeted once we
 
616
             implement correct NULL semantics (WL#3830)
 
617
          5. Subquery is non-correlated
 
618
             TODO:
 
619
             This is an overly restrictive condition. It can be extended to:
 
620
             (Subquery is non-correlated ||
 
621
              Subquery is correlated to any query outer to IN predicate ||
 
622
              (Subquery is correlated to the immediate outer query &&
 
623
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
624
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
625
          6. No execution method was already chosen (by a prepared statement).
 
626
 
 
627
          (*) The subquery must be part of a SELECT statement. The current
 
628
               condition also excludes multi-table update statements.
 
629
 
 
630
          We have to determine whether we will perform subquery materialization
 
631
          before calling the IN=>EXISTS transformation, so that we know whether to
 
632
          perform the whole transformation or only that part of it which wraps
 
633
          Item_in_subselect in an Item_in_optimizer.
 
634
        */
 
635
        if (do_materialize &&
 
636
            in_subs  &&                                                   // 1
 
637
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
638
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
639
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
640
        {
 
641
          if (in_subs->is_top_level_item() &&                             // 4
 
642
              !in_subs->is_correlated &&                                  // 5
 
643
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
644
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
645
        }
 
646
 
 
647
        Item_subselect::trans_res trans_res;
 
648
        if ((trans_res= subselect->select_transformer(this)) !=
 
649
            Item_subselect::RES_OK)
 
650
        {
 
651
          return((trans_res == Item_subselect::RES_ERROR));
 
652
        }
 
653
      }
 
654
    }
 
655
  }
 
656
 
 
657
  if (order)
 
658
  {
 
659
    order_st *ord;
 
660
    for (ord= order; ord; ord= ord->next)
 
661
    {
 
662
      Item *item= *ord->item;
 
663
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
664
        item->split_sum_func(session, ref_pointer_array, all_fields);
 
665
    }
 
666
  }
 
667
 
 
668
  if (having && having->with_sum_func)
 
669
    having->split_sum_func(session, ref_pointer_array, all_fields,
 
670
                           &having, true);
 
671
  if (select_lex->inner_sum_func_list)
 
672
  {
 
673
    Item_sum *end=select_lex->inner_sum_func_list;
 
674
    Item_sum *item_sum= end;
 
675
    do
 
676
    {
 
677
      item_sum= item_sum->next;
 
678
      item_sum->split_sum_func(session, ref_pointer_array,
 
679
                               all_fields, item_sum->ref_by, false);
 
680
    } while (item_sum != end);
 
681
  }
 
682
 
 
683
  if (select_lex->inner_refs_list.elements &&
 
684
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
 
685
    return(-1);
 
686
 
 
687
  /*
 
688
    Check if there are references to un-aggregated columns when computing
 
689
    aggregate functions with implicit grouping (there is no GROUP BY).
 
690
 
 
691
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
692
  */
 
693
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
694
  {
 
695
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
696
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
697
    return(-1);
 
698
  }
 
699
  {
 
700
    /* Caclulate the number of groups */
 
701
    send_group_parts= 0;
 
702
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
703
      send_group_parts++;
 
704
  }
 
705
 
 
706
  if (error)
 
707
    goto err;                                   /* purecov: inspected */
 
708
 
 
709
  if (result && result->prepare(fields_list, unit_arg))
 
710
    goto err;                                   /* purecov: inspected */
 
711
 
 
712
  /* Init join struct */
 
713
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
714
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
715
  this->group= group_list != 0;
 
716
  unit= unit_arg;
 
717
 
 
718
#ifdef RESTRICTED_GROUP
 
719
  if (sum_func_count && !group_list && (func_count || field_count))
 
720
  {
 
721
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
722
    goto err;
 
723
  }
 
724
#endif
 
725
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
726
    goto err;
 
727
  if (alloc_func_list())
 
728
    goto err;
 
729
 
 
730
  return(0); // All OK
 
731
 
 
732
err:
 
733
  return(-1);                           /* purecov: inspected */
 
734
}
 
735
 
 
736
 
 
737
/*
 
738
  Remove the predicates pushed down into the subquery
 
739
 
 
740
  SYNOPSIS
 
741
    JOIN::remove_subq_pushed_predicates()
 
742
      where   IN  Must be NULL
 
743
              OUT The remaining WHERE condition, or NULL
 
744
 
 
745
  DESCRIPTION
 
746
    Given that this join will be executed using (unique|index)_subquery,
 
747
    without "checking NULL", remove the predicates that were pushed down
 
748
    into the subquery.
 
749
 
 
750
    If the subquery compares scalar values, we can remove the condition that
 
751
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
752
    engine)
 
753
 
 
754
    If the subquery compares row values, we need to keep the wrapped
 
755
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
756
    and non-NULL values, we'll do a full table scan and will rely on the
 
757
    equalities corresponding to non-NULL parts of left tuple to filter out
 
758
    non-matching records.
 
759
 
 
760
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
761
    fact that subquery engine will be using index lookup. This must be done only
 
762
    for cases where there are no conversion errors of significance, e.g. 257
 
763
    that is searched in a byte. But this requires homogenization of the return
 
764
    codes of all Field*::store() methods.
 
765
*/
 
766
 
 
767
void JOIN::remove_subq_pushed_predicates(Item **where)
 
768
{
 
769
  if (conds->type() == Item::FUNC_ITEM &&
 
770
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
771
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
772
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
773
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
774
                   ((Item_func *)conds)->arguments()[0]))
 
775
  {
 
776
    *where= 0;
 
777
    return;
 
778
  }
 
779
}
 
780
 
 
781
 
281
782
/*
282
783
  Index lookup-based subquery: save some flags for EXPLAIN output
283
784
 
296
797
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
297
798
    and set appropriate flags in join_tab->packed_info.
298
799
*/
299
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
800
 
 
801
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
300
802
{
301
803
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
302
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
804
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
303
805
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
304
806
  if (where)
305
807
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
313
815
  }
314
816
}
315
817
 
 
818
 
 
819
 
 
820
 
 
821
/*
 
822
  Check if the table's rowid is included in the temptable
 
823
 
 
824
  SYNOPSIS
 
825
    sj_table_is_included()
 
826
      join      The join
 
827
      join_tab  The table to be checked
 
828
 
 
829
  DESCRIPTION
 
830
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
831
    in the temptable. This is so if
 
832
 
 
833
    1. The table is not embedded within some semi-join nest
 
834
    2. The has been pulled out of a semi-join nest, or
 
835
 
 
836
    3. The table is functionally dependent on some previous table
 
837
 
 
838
    [4. This is also true for constant tables that can't be
 
839
        NULL-complemented but this function is not called for such tables]
 
840
 
 
841
  RETURN
 
842
    true  - Include table's rowid
 
843
    false - Don't
 
844
*/
 
845
 
 
846
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
847
{
 
848
  if (join_tab->emb_sj_nest)
 
849
    return false;
 
850
 
 
851
  /* Check if this table is functionally dependent on the tables that
 
852
     are within the same outer join nest
 
853
  */
 
854
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
855
  if (join_tab->type == JT_EQ_REF)
 
856
  {
 
857
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
858
    uint32_t idx;
 
859
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
860
    {
 
861
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
862
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
863
        return true;
 
864
    }
 
865
    /* Ok, functionally dependent */
 
866
    return false;
 
867
  }
 
868
  /* Not functionally dependent => need to include*/
 
869
  return true;
 
870
}
 
871
 
 
872
 
 
873
/*
 
874
  Setup the strategies to eliminate semi-join duplicates.
 
875
 
 
876
  SYNOPSIS
 
877
    setup_semijoin_dups_elimination()
 
878
      join           Join to process
 
879
      options        Join options (needed to see if join buffering will be
 
880
                     used or not)
 
881
      no_jbuf_after  Another bit of information re where join buffering will
 
882
                     be used.
 
883
 
 
884
  DESCRIPTION
 
885
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
886
    strategies:
 
887
 
 
888
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
889
                         of row combinations)
 
890
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
891
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
892
                  together and picking the 1st one)
 
893
 
 
894
    The join order has "duplicate-generating ranges", and every range is
 
895
    served by one strategy or a combination of FirstMatch with with some
 
896
    other strategy.
 
897
 
 
898
    "Duplicate-generating range" is defined as a range within the join order
 
899
    that contains all of the inner tables of a semi-join. All ranges must be
 
900
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
901
    are joined together, which is equivalent to converting
 
902
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
903
    to
 
904
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
905
    .
 
906
 
 
907
    Applicability conditions are as follows:
 
908
 
 
909
    DuplicateWeedout strategy
 
910
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
911
 
 
912
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
913
      +------+  +=========================+  +---+
 
914
        (1)                 (2)               (3)
 
915
 
 
916
       (1) - Prefix of OuterTables (those that participate in
 
917
             IN-equality and/or are correlated with subquery) and outer
 
918
             Noncorrelated Tables.
 
919
       (2) - The handled range. The range starts with the first sj-inner
 
920
             table, and covers all sj-inner and outer tables
 
921
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
922
             may follow in any order.
 
923
       (3) - The suffix of outer Noncorrelated tables.
 
924
 
 
925
    FirstMatch strategy
 
926
    ~~~~~~~~~~~~~~~~~~~
 
927
 
 
928
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
929
      +------+  +==================+  +---+
 
930
        (1)             (2)          (3)
 
931
 
 
932
      (1) - Prefix of outer and non-correlated tables
 
933
      (2) - The handled range, which may contain only inner and
 
934
            non-correlated tables.
 
935
      (3) - The suffix of outer Noncorrelated tables.
 
936
 
 
937
    InsideOut strategy
 
938
    ~~~~~~~~~~~~~~~~~~
 
939
 
 
940
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
941
     +--------+   +===========+ +=============+   +------+
 
942
        (1)           (2)          (3)              (4)
 
943
 
 
944
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
945
            all the non-trivially correlated outer tables. (non-trivially means
 
946
            that the correlation is not just through the IN-equality).
 
947
 
 
948
      (2) - Inner table for which the InsideOut scan is performed.
 
949
 
 
950
      (3) - The remainder of the duplicate-generating range. It is served by
 
951
            application of FirstMatch strategy, with the exception that
 
952
            outer IN-correlated tables are considered to be non-correlated.
 
953
 
 
954
      (4) - THe suffix of outer and outer non-correlated tables.
 
955
 
 
956
    If several strategies are applicable, their relative priorities are:
 
957
      1. InsideOut
 
958
      2. FirstMatch
 
959
      3. DuplicateWeedout
 
960
 
 
961
    This function walks over the join order and sets up the strategies by
 
962
    setting appropriate members in join_tab structures.
 
963
 
 
964
  RETURN
 
965
    false  OK
 
966
    true   Out of memory error
 
967
*/
 
968
 
 
969
static
 
970
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
971
{
 
972
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
973
  struct {
 
974
    /*
 
975
      0 - invalid (EOF marker),
 
976
      1 - InsideOut,
 
977
      2 - Temptable (maybe confluent),
 
978
      3 - Temptable with join buffering
 
979
    */
 
980
    uint32_t strategy;
 
981
    uint32_t start_idx; /* Left range bound */
 
982
    uint32_t end_idx;   /* Right range bound */
 
983
    /*
 
984
      For Temptable strategy: Bitmap of all outer and correlated tables from
 
985
      all involved join nests.
 
986
    */
 
987
    table_map outer_tables;
 
988
  } dups_ranges [MAX_TABLES];
 
989
 
 
990
  TableList *emb_insideout_nest= NULL;
 
991
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
992
                               tables) whose ranges we're in */
 
993
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
994
  table_map range_start_map= 0; /* table_map at current range start */
 
995
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
996
  int cur_range= 0;
 
997
  uint32_t i;
 
998
 
 
999
  /*
 
1000
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
1001
  */
 
1002
  for (i=join->const_tables ; i < join->tables ; i++)
 
1003
  {
 
1004
    JOIN_TAB *tab=join->join_tab+i;
 
1005
    Table *table=tab->table;
 
1006
    cur_map |= table->map;
 
1007
 
 
1008
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
1009
    {
 
1010
      if (!emb_sj_map)
 
1011
      {
 
1012
        dups_ranges[cur_range].start_idx= i;
 
1013
        range_start_map= cur_map & ~table->map;
 
1014
        /*
 
1015
          Remember if this is a possible start of range that is covered by
 
1016
          the InsideOut strategy (the reason that it is not covered could
 
1017
          be that it overlaps with anther semi-join's range. we don't
 
1018
          support InsideOut for joined ranges)
 
1019
        */
 
1020
        if (join->best_positions[i].use_insideout_scan)
 
1021
          emb_insideout_nest= tab->emb_sj_nest;
 
1022
      }
 
1023
 
 
1024
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1025
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1026
 
 
1027
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1028
      {
 
1029
        /*
 
1030
          Two different semi-joins interleave. This cannot be handled by
 
1031
          InsideOut strategy.
 
1032
        */
 
1033
        emb_insideout_nest= NULL;
 
1034
      }
 
1035
    }
 
1036
 
 
1037
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1038
    {
 
1039
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1040
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1041
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1042
      {
 
1043
        /*
 
1044
          This table uses join buffering, which makes use of FirstMatch or
 
1045
          InsideOut strategies impossible for the current and (we assume)
 
1046
          preceding duplicate-producing ranges.
 
1047
          That is, for the join order:
 
1048
 
 
1049
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1050
                  |     |     |     |          | \
 
1051
                  +-----+     +-----+          |  join buffering use
 
1052
                     r1          r2         we're here
 
1053
 
 
1054
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1055
          strategy that spans all the tables, starting from the very 1st
 
1056
          one.
 
1057
        */
 
1058
        dealing_with_jbuf= true;
 
1059
        emb_insideout_nest= false;
 
1060
 
 
1061
        /*
 
1062
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1063
          do not matter:
 
1064
        */
 
1065
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1066
        {
 
1067
          dups_ranges[cur_range].outer_tables |=
 
1068
            dups_ranges[prev_range].outer_tables;
 
1069
        }
 
1070
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1071
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1072
        cur_range=  0;
 
1073
      }
 
1074
 
 
1075
      /*
 
1076
        Check if we are at the end of duplicate-producing range. We are if
 
1077
 
 
1078
        1. It's an InsideOut range (which presumes all correlated tables are
 
1079
           in the prefix), and all inner tables are in the join order prefix,
 
1080
           or
 
1081
        2. It's a DuplicateElimination range (possibly covering several
 
1082
           SJ-nests), and all inner, outer, and correlated tables of all
 
1083
           sj-nests are in the join order prefix.
 
1084
      */
 
1085
      bool end_of_range= false;
 
1086
      if (emb_insideout_nest &&
 
1087
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1088
      {
 
1089
        /* Save that this range is handled with InsideOut: */
 
1090
        dups_ranges[cur_range].strategy= 1;
 
1091
        end_of_range= true;
 
1092
      }
 
1093
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1094
      {
 
1095
        /*
 
1096
          This is a complete range to be handled with either DuplicateWeedout
 
1097
          or FirstMatch
 
1098
        */
 
1099
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1100
        /*
 
1101
          This will hold tables from within the range that need to be put
 
1102
          into the join buffer before we can use the FirstMatch on its tail.
 
1103
        */
 
1104
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
 
1105
                                             ~range_start_map;
 
1106
        end_of_range= true;
 
1107
      }
 
1108
 
 
1109
      if (end_of_range)
 
1110
      {
 
1111
        dups_ranges[cur_range].end_idx= i+1;
 
1112
        emb_sj_map= emb_outer_tables= 0;
 
1113
        emb_insideout_nest= NULL;
 
1114
        dealing_with_jbuf= false;
 
1115
        dups_ranges[++cur_range].strategy= 0;
 
1116
      }
 
1117
    }
 
1118
  }
 
1119
 
 
1120
  Session *session= join->session;
 
1121
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1122
  /*
 
1123
    Second pass: setup the chosen strategies
 
1124
  */
 
1125
  for (int j= 0; j < cur_range; j++)
 
1126
  {
 
1127
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1128
    JOIN_TAB *jump_to;
 
1129
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1130
    {
 
1131
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1132
      jump_to= tab++;
 
1133
    }
 
1134
    else // DuplicateWeedout strategy
 
1135
    {
 
1136
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1137
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1138
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1139
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1140
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1141
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1142
      JOIN_TAB *last_outer_tab= tab - 1;
 
1143
      /*
 
1144
        Walk through the range and remember
 
1145
         - tables that need their rowids to be put into temptable
 
1146
         - the last outer table
 
1147
      */
 
1148
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1149
      {
 
1150
        if (sj_table_is_included(join, tab))
 
1151
        {
 
1152
          last_tab->join_tab= tab;
 
1153
          last_tab->rowid_offset= jt_rowid_offset;
 
1154
          jt_rowid_offset += tab->table->file->ref_length;
 
1155
          if (tab->table->maybe_null)
 
1156
          {
 
1157
            last_tab->null_byte= jt_null_bits / 8;
 
1158
            last_tab->null_bit= jt_null_bits++;
 
1159
          }
 
1160
          last_tab++;
 
1161
          tab->table->prepare_for_position();
 
1162
          tab->rowid_keep_flags= rowid_keep_flags;
 
1163
        }
 
1164
        cur_map |= tab->table->map;
 
1165
        if (!tab->emb_sj_nest && bitmap_covers(cur_map,
 
1166
                                               dups_ranges[j].outer_tables))
 
1167
          last_outer_tab= tab;
 
1168
      }
 
1169
 
 
1170
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1171
      {
 
1172
        SJ_TMP_TABLE *sjtbl;
 
1173
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1174
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1175
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
 
1176
          return(true);
 
1177
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1178
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1179
        sjtbl->rowid_len= jt_rowid_offset;
 
1180
        sjtbl->null_bits= jt_null_bits;
 
1181
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1182
 
 
1183
        *next_sjtbl_ptr= sjtbl;
 
1184
        next_sjtbl_ptr= &(sjtbl->next);
 
1185
        sjtbl->next= NULL;
 
1186
 
 
1187
        sjtbl->tmp_table=
 
1188
          create_duplicate_weedout_tmp_table(session,
 
1189
                                             sjtbl->rowid_len +
 
1190
                                             sjtbl->null_bytes,
 
1191
                                             sjtbl);
 
1192
 
 
1193
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1194
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1195
      }
 
1196
      tab= last_outer_tab + 1;
 
1197
      jump_to= last_outer_tab;
 
1198
    }
 
1199
 
 
1200
    /* Create the FirstMatch tail */
 
1201
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1202
    {
 
1203
      if (tab->emb_sj_nest)
 
1204
        tab->do_firstmatch= jump_to;
 
1205
      else
 
1206
        jump_to= tab;
 
1207
    }
 
1208
  }
 
1209
  return(false);
 
1210
}
 
1211
 
 
1212
 
 
1213
static void cleanup_sj_tmp_tables(JOIN *join)
 
1214
{
 
1215
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
 
1216
       sj_tbl= sj_tbl->next)
 
1217
  {
 
1218
    if (sj_tbl->tmp_table)
 
1219
    {
 
1220
      sj_tbl->tmp_table->free_tmp_table(join->session);
 
1221
    }
 
1222
  }
 
1223
  join->sj_tmp_tables= NULL;
 
1224
}
 
1225
 
 
1226
uint32_t make_join_orderinfo(JOIN *join);
 
1227
 
 
1228
/**
 
1229
  global select optimisation.
 
1230
 
 
1231
  @note
 
1232
    error code saved in field 'error'
 
1233
 
 
1234
  @retval
 
1235
    0   success
 
1236
  @retval
 
1237
    1   error
 
1238
*/
 
1239
 
 
1240
int
 
1241
JOIN::optimize()
 
1242
{
 
1243
  // to prevent double initialization on EXPLAIN
 
1244
  if (optimized)
 
1245
    return(0);
 
1246
  optimized= 1;
 
1247
 
 
1248
  session->set_proc_info("optimizing");
 
1249
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1250
              unit->select_limit_cnt);
 
1251
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1252
  select_limit= unit->select_limit_cnt;
 
1253
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1254
    select_limit= HA_POS_ERROR;
 
1255
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1256
  // Ignore errors of execution if option IGNORE present
 
1257
  if (session->lex->ignore)
 
1258
    session->lex->current_select->no_error= 1;
 
1259
 
 
1260
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1261
  /* Add HAVING to WHERE if possible */
 
1262
  if (having && !group_list && !sum_func_count)
 
1263
  {
 
1264
    if (!conds)
 
1265
    {
 
1266
      conds= having;
 
1267
      having= 0;
 
1268
    }
 
1269
    else if ((conds=new Item_cond_and(conds,having)))
 
1270
    {
 
1271
      /*
 
1272
        Item_cond_and can't be fixed after creation, so we do not check
 
1273
        conds->fixed
 
1274
      */
 
1275
      conds->fix_fields(session, &conds);
 
1276
      conds->change_ref_to_fields(session, tables_list);
 
1277
      conds->top_level_item();
 
1278
      having= 0;
 
1279
    }
 
1280
  }
 
1281
#endif
 
1282
 
 
1283
  /* Convert all outer joins to inner joins if possible */
 
1284
  conds= simplify_joins(this, join_list, conds, true, false);
 
1285
  build_bitmap_for_nested_joins(join_list, 0);
 
1286
 
 
1287
  conds= optimize_cond(this, conds, join_list, &cond_value);
 
1288
  if (session->is_error())
 
1289
  {
 
1290
    error= 1;
 
1291
    return(1);
 
1292
  }
 
1293
 
 
1294
  {
 
1295
    having= optimize_cond(this, having, join_list, &having_value);
 
1296
    if (session->is_error())
 
1297
    {
 
1298
      error= 1;
 
1299
      return(1);
 
1300
    }
 
1301
    if (select_lex->where)
 
1302
      select_lex->cond_value= cond_value;
 
1303
    if (select_lex->having)
 
1304
      select_lex->having_value= having_value;
 
1305
 
 
1306
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
 
1307
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1308
    {                                           /* Impossible cond */
 
1309
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1310
                           "Impossible HAVING" : "Impossible WHERE";
 
1311
      error= 0;
 
1312
      return(0);
 
1313
    }
 
1314
  }
 
1315
 
 
1316
  /* Optimize count(*), cmin() and cmax() */
 
1317
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1318
  {
 
1319
    int res;
 
1320
    /*
 
1321
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1322
      to the WHERE conditions,
 
1323
      or 1 if all items were resolved,
 
1324
      or 0, or an error number HA_ERR_...
 
1325
    */
 
1326
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1327
    {
 
1328
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1329
      {
 
1330
        zero_result_cause= "No matching min/max row";
 
1331
        error=0;
 
1332
        return(0);
 
1333
      }
 
1334
      if (res > 1)
 
1335
      {
 
1336
        error= res;
 
1337
        return(1);
 
1338
      }
 
1339
      if (res < 0)
 
1340
      {
 
1341
        zero_result_cause= "No matching min/max row";
 
1342
        error=0;
 
1343
        return(0);
 
1344
      }
 
1345
      zero_result_cause= "Select tables optimized away";
 
1346
      tables_list= 0;                           // All tables resolved
 
1347
      /*
 
1348
        Extract all table-independent conditions and replace the WHERE
 
1349
        clause with them. All other conditions were computed by opt_sum_query
 
1350
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1351
        so there is no need to compute the whole WHERE clause again.
 
1352
        Notice that make_cond_for_table() will always succeed to remove all
 
1353
        computed conditions, because opt_sum_query() is applicable only to
 
1354
        conjunctions.
 
1355
        Preserve conditions for EXPLAIN.
 
1356
      */
 
1357
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
1358
      {
 
1359
        COND *table_independent_conds=
 
1360
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1361
        conds= table_independent_conds;
 
1362
      }
 
1363
    }
 
1364
  }
 
1365
  if (!tables_list)
 
1366
  {
 
1367
    error= 0;
 
1368
    return(0);
 
1369
  }
 
1370
  error= -1;                                    // Error is sent to client
 
1371
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1372
 
 
1373
  /* Calculate how to do the join */
 
1374
  session->set_proc_info("statistics");
 
1375
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1376
      session->is_fatal_error)
 
1377
  {
 
1378
    return(1);
 
1379
  }
 
1380
 
 
1381
  /* Remove distinct if only const tables */
 
1382
  select_distinct= select_distinct && (const_tables != tables);
 
1383
  session->set_proc_info("preparing");
 
1384
  if (result->initialize_tables(this))
 
1385
  {
 
1386
    return(1);                          // error == -1
 
1387
  }
 
1388
  if (const_table_map != found_const_table_map &&
 
1389
      !(select_options & SELECT_DESCRIBE) &&
 
1390
      (!conds ||
 
1391
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1392
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
1393
  {
 
1394
    zero_result_cause= "no matching row in const table";
 
1395
    error= 0;
 
1396
    return(0);
 
1397
  }
 
1398
  if (!(session->options & OPTION_BIG_SELECTS) &&
 
1399
      best_read > (double) session->variables.max_join_size &&
 
1400
      !(select_options & SELECT_DESCRIBE))
 
1401
  {                                             /* purecov: inspected */
 
1402
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1403
    error= -1;
 
1404
    return(1);
 
1405
  }
 
1406
  if (const_tables && !session->locked_tables &&
 
1407
      !(select_options & SELECT_NO_UNLOCK))
 
1408
    mysql_unlock_some_tables(session, table, const_tables);
 
1409
  if (!conds && outer_join)
 
1410
  {
 
1411
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1412
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1413
  }
 
1414
  select= make_select(*table, const_table_map,
 
1415
                      const_table_map, conds, 1, &error);
 
1416
  if (error)
 
1417
  {                                             /* purecov: inspected */
 
1418
    error= -1;                                  /* purecov: inspected */
 
1419
    return(1);
 
1420
  }
 
1421
 
 
1422
  reset_nj_counters(join_list);
 
1423
  make_outerjoin_info(this);
 
1424
 
 
1425
  /*
 
1426
    Among the equal fields belonging to the same multiple equality
 
1427
    choose the one that is to be retrieved first and substitute
 
1428
    all references to these in where condition for a reference for
 
1429
    the selected field.
 
1430
  */
 
1431
  if (conds)
 
1432
  {
 
1433
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1434
    conds->update_used_tables();
 
1435
  }
 
1436
 
 
1437
  /*
 
1438
    Permorm the the optimization on fields evaluation mentioned above
 
1439
    for all on expressions.
 
1440
  */
 
1441
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1442
  {
 
1443
    if (*tab->on_expr_ref)
 
1444
    {
 
1445
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1446
                                                         tab->cond_equal,
 
1447
                                                         map2table);
 
1448
      (*tab->on_expr_ref)->update_used_tables();
 
1449
    }
 
1450
  }
 
1451
 
 
1452
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
 
1453
      (select_options & SELECT_DESCRIBE) &&
 
1454
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
1455
  {
 
1456
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1457
  }
 
1458
  if (make_join_select(this, select, conds))
 
1459
  {
 
1460
    zero_result_cause=
 
1461
      "Impossible WHERE noticed after reading const tables";
 
1462
    return(0);                          // error == 0
 
1463
  }
 
1464
 
 
1465
  error= -1;                                    /* if goto err */
 
1466
 
 
1467
  /* Optimize distinct away if possible */
 
1468
  {
 
1469
    order_st *org_order= order;
 
1470
    order=remove_const(this, order,conds,1, &simple_order);
 
1471
    if (session->is_error())
 
1472
    {
 
1473
      error= 1;
 
1474
      return(1);
 
1475
    }
 
1476
 
 
1477
    /*
 
1478
      If we are using order_st BY NULL or order_st BY const_expression,
 
1479
      return result in any order (even if we are using a GROUP BY)
 
1480
    */
 
1481
    if (!order && org_order)
 
1482
      skip_sort_order= 1;
 
1483
  }
 
1484
  /*
 
1485
     Check if we can optimize away GROUP BY/DISTINCT.
 
1486
     We can do that if there are no aggregate functions, the
 
1487
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1488
     (if present) contain direct references to all key parts of
 
1489
     an unique index (in whatever order) and if the key parts of the
 
1490
     unique index cannot contain NULLs.
 
1491
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1492
     be the same (as long as they are unique).
 
1493
 
 
1494
     The FROM clause must contain a single non-constant table.
 
1495
  */
 
1496
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1497
      !tmp_table_param.sum_func_count &&
 
1498
      (!join_tab[const_tables].select ||
 
1499
       !join_tab[const_tables].select->quick ||
 
1500
       join_tab[const_tables].select->quick->get_type() !=
 
1501
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1502
  {
 
1503
    if (group_list &&
 
1504
       list_contains_unique_index(join_tab[const_tables].table,
 
1505
                                 find_field_in_order_list,
 
1506
                                 (void *) group_list))
 
1507
    {
 
1508
      /*
 
1509
        We have found that grouping can be removed since groups correspond to
 
1510
        only one row anyway, but we still have to guarantee correct result
 
1511
        order. The line below effectively rewrites the query from GROUP BY
 
1512
        <fields> to order_st BY <fields>. There are two exceptions:
 
1513
        - if skip_sort_order is set (see above), then we can simply skip
 
1514
          GROUP BY;
 
1515
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1516
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1517
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1518
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1519
          order_st BY fields.
 
1520
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1521
          'order' as is.
 
1522
       */
 
1523
      if (!order || test_if_subpart(group_list, order))
 
1524
          order= skip_sort_order ? 0 : group_list;
 
1525
      /*
 
1526
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
 
1527
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1528
      */
 
1529
      join_tab->table->keys_in_use_for_order_by=
 
1530
        join_tab->table->keys_in_use_for_group_by;
 
1531
      group_list= 0;
 
1532
      group= 0;
 
1533
    }
 
1534
    if (select_distinct &&
 
1535
       list_contains_unique_index(join_tab[const_tables].table,
 
1536
                                 find_field_in_item_list,
 
1537
                                 (void *) &fields_list))
 
1538
    {
 
1539
      select_distinct= 0;
 
1540
    }
 
1541
  }
 
1542
  if (group_list || tmp_table_param.sum_func_count)
 
1543
  {
 
1544
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1545
      select_distinct=0;
 
1546
  }
 
1547
  else if (select_distinct && tables - const_tables == 1)
 
1548
  {
 
1549
    /*
 
1550
      We are only using one table. In this case we change DISTINCT to a
 
1551
      GROUP BY query if:
 
1552
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1553
        BY only uses selected fields.
 
1554
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1555
      - We are scanning the whole table without LIMIT
 
1556
        This can happen if:
 
1557
        - We are using CALC_FOUND_ROWS
 
1558
        - We are using an order_st BY that can't be optimized away.
 
1559
 
 
1560
      We don't want to use this optimization when we are using LIMIT
 
1561
      because in this case we can just create a temporary table that
 
1562
      holds LIMIT rows and stop when this table is full.
 
1563
    */
 
1564
    JOIN_TAB *tab= &join_tab[const_tables];
 
1565
    bool all_order_fields_used;
 
1566
    if (order)
 
1567
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
 
1568
        &tab->table->keys_in_use_for_order_by);
 
1569
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
 
1570
                                          order, fields_list, all_fields,
 
1571
                                          &all_order_fields_used)))
 
1572
    {
 
1573
      bool skip_group= (skip_sort_order &&
 
1574
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
 
1575
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1576
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1577
      if ((skip_group && all_order_fields_used) ||
 
1578
          select_limit == HA_POS_ERROR ||
 
1579
          (order && !skip_sort_order))
 
1580
      {
 
1581
        /*  Change DISTINCT to GROUP BY */
 
1582
        select_distinct= 0;
 
1583
        no_order= !order;
 
1584
        if (all_order_fields_used)
 
1585
        {
 
1586
          if (order && skip_sort_order)
 
1587
          {
 
1588
            /*
 
1589
              Force MySQL to read the table in sorted order to get result in
 
1590
              order_st BY order.
 
1591
            */
 
1592
            tmp_table_param.quick_group=0;
 
1593
          }
 
1594
          order=0;
 
1595
        }
 
1596
        group=1;                                // For end_write_group
 
1597
      }
 
1598
      else
 
1599
        group_list= 0;
 
1600
    }
 
1601
    else if (session->is_fatal_error)                   // End of memory
 
1602
      return(1);
 
1603
  }
 
1604
  simple_group= 0;
 
1605
  {
 
1606
    order_st *old_group_list;
 
1607
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1608
                             rollup.state == ROLLUP::STATE_NONE,
 
1609
                             &simple_group);
 
1610
    if (session->is_error())
 
1611
    {
 
1612
      error= 1;
 
1613
      return(1);
 
1614
    }
 
1615
    if (old_group_list && !group_list)
 
1616
      select_distinct= 0;
 
1617
  }
 
1618
  if (!group_list && group)
 
1619
  {
 
1620
    order=0;                                    // The output has only one row
 
1621
    simple_order=1;
 
1622
    select_distinct= 0;                       // No need in distinct for 1 row
 
1623
    group_optimized_away= 1;
 
1624
  }
 
1625
 
 
1626
  calc_group_buffer(this, group_list);
 
1627
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1628
 
 
1629
  if (test_if_subpart(group_list, order) ||
 
1630
      (!group_list && tmp_table_param.sum_func_count))
 
1631
    order=0;
 
1632
 
 
1633
  // Can't use sort on head table if using row cache
 
1634
  if (full_join)
 
1635
  {
 
1636
    if (group_list)
 
1637
      simple_group=0;
 
1638
    if (order)
 
1639
      simple_order=0;
 
1640
  }
 
1641
 
 
1642
  /*
 
1643
    Check if we need to create a temporary table.
 
1644
    This has to be done if all tables are not already read (const tables)
 
1645
    and one of the following conditions holds:
 
1646
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1647
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1648
    - We are using different order_st BY and GROUP BY orders
 
1649
    - The user wants us to buffer the result.
 
1650
  */
 
1651
  need_tmp= (const_tables != tables &&
 
1652
             ((select_distinct || !simple_order || !simple_group) ||
 
1653
              (group_list && order) ||
 
1654
              test(select_options & OPTION_BUFFER_RESULT)));
 
1655
 
 
1656
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1657
  uint64_t select_opts_for_readinfo=
 
1658
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1659
 
 
1660
  sj_tmp_tables= NULL;
 
1661
  if (!select_lex->sj_nests.is_empty())
 
1662
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1663
                                    no_jbuf_after);
 
1664
 
 
1665
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1666
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1667
    return(1);
 
1668
 
 
1669
  /* Create all structures needed for materialized subquery execution. */
 
1670
  if (setup_subquery_materialization())
 
1671
    return(1);
 
1672
 
 
1673
  /*
 
1674
    is this simple IN subquery?
 
1675
  */
 
1676
  if (!group_list && !order &&
 
1677
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1678
      tables == 1 && conds &&
 
1679
      !unit->is_union())
 
1680
  {
 
1681
    if (!having)
 
1682
    {
 
1683
      Item *where= conds;
 
1684
      if (join_tab[0].type == JT_EQ_REF &&
 
1685
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1686
      {
 
1687
        remove_subq_pushed_predicates(&where);
 
1688
        save_index_subquery_explain_info(join_tab, where);
 
1689
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1690
        error= 0;
 
1691
        return(unit->item->
 
1692
                    change_engine(new
 
1693
                                  subselect_uniquesubquery_engine(session,
 
1694
                                                                  join_tab,
 
1695
                                                                  unit->item,
 
1696
                                                                  where)));
 
1697
      }
 
1698
      else if (join_tab[0].type == JT_REF &&
 
1699
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1700
      {
 
1701
        remove_subq_pushed_predicates(&where);
 
1702
        save_index_subquery_explain_info(join_tab, where);
 
1703
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1704
        error= 0;
 
1705
        return(unit->item->
 
1706
                    change_engine(new
 
1707
                                  subselect_indexsubquery_engine(session,
 
1708
                                                                 join_tab,
 
1709
                                                                 unit->item,
 
1710
                                                                 where,
 
1711
                                                                 NULL,
 
1712
                                                                 0)));
 
1713
      }
 
1714
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1715
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1716
               having->name == in_having_cond)
 
1717
    {
 
1718
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1719
      error= 0;
 
1720
      conds= remove_additional_cond(conds);
 
1721
      save_index_subquery_explain_info(join_tab, conds);
 
1722
      return(unit->item->
 
1723
                  change_engine(new subselect_indexsubquery_engine(session,
 
1724
                                                                   join_tab,
 
1725
                                                                   unit->item,
 
1726
                                                                   conds,
 
1727
                                                                   having,
 
1728
                                                                   1)));
 
1729
    }
 
1730
 
 
1731
  }
 
1732
  /*
 
1733
    Need to tell handlers that to play it safe, it should fetch all
 
1734
    columns of the primary key of the tables: this is because MySQL may
 
1735
    build row pointers for the rows, and for all columns of the primary key
 
1736
    the read set has not necessarily been set by the server code.
 
1737
  */
 
1738
  if (need_tmp || select_distinct || group_list || order)
 
1739
  {
 
1740
    for (uint32_t i = const_tables; i < tables; i++)
 
1741
      join_tab[i].table->prepare_for_position();
 
1742
  }
 
1743
 
 
1744
  if (const_tables != tables)
 
1745
  {
 
1746
    /*
 
1747
      Because filesort always does a full table scan or a quick range scan
 
1748
      we must add the removed reference to the select for the table.
 
1749
      We only need to do this when we have a simple_order or simple_group
 
1750
      as in other cases the join is done before the sort.
 
1751
    */
 
1752
    if ((order || group_list) &&
 
1753
        (join_tab[const_tables].type != JT_ALL) &&
 
1754
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1755
        ((order && simple_order) || (group_list && simple_group)))
 
1756
    {
 
1757
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
 
1758
        return(1);
 
1759
      }
 
1760
    }
 
1761
 
 
1762
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1763
        ((group_list &&
 
1764
          (!simple_group ||
 
1765
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1766
                                    unit->select_limit_cnt, 0,
 
1767
                                    &join_tab[const_tables].table->
 
1768
                                    keys_in_use_for_group_by))) ||
 
1769
         select_distinct) &&
 
1770
        tmp_table_param.quick_group)
 
1771
    {
 
1772
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1773
    }
 
1774
    if (order)
 
1775
    {
 
1776
      /*
 
1777
        Force using of tmp table if sorting by a SP or UDF function due to
 
1778
        their expensive and probably non-deterministic nature.
 
1779
      */
 
1780
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1781
      {
 
1782
        Item *item= *tmp_order->item;
 
1783
        if (item->is_expensive())
 
1784
        {
 
1785
          /* Force tmp table without sort */
 
1786
          need_tmp=1; simple_order=simple_group=0;
 
1787
          break;
 
1788
        }
 
1789
      }
 
1790
    }
 
1791
  }
 
1792
 
 
1793
  tmp_having= having;
 
1794
  if (select_options & SELECT_DESCRIBE)
 
1795
  {
 
1796
    error= 0;
 
1797
    return(0);
 
1798
  }
 
1799
  having= 0;
 
1800
 
 
1801
  /*
 
1802
    The loose index scan access method guarantees that all grouping or
 
1803
    duplicate row elimination (for distinct) is already performed
 
1804
    during data retrieval, and that all MIN/MAX functions are already
 
1805
    computed for each group. Thus all MIN/MAX functions should be
 
1806
    treated as regular functions, and there is no need to perform
 
1807
    grouping in the main execution loop.
 
1808
    Notice that currently loose index scan is applicable only for
 
1809
    single table queries, thus it is sufficient to test only the first
 
1810
    join_tab element of the plan for its access method.
 
1811
  */
 
1812
  if (join_tab->is_using_loose_index_scan())
 
1813
    tmp_table_param.precomputed_group_by= true;
 
1814
 
 
1815
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1816
  if (need_tmp)
 
1817
  {
 
1818
    session->set_proc_info("Creating tmp table");
 
1819
 
 
1820
    init_items_ref_array();
 
1821
 
 
1822
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1823
                                         fields_list.elements);
 
1824
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1825
                                                             (order_st*) 0);
 
1826
    /*
 
1827
      Pushing LIMIT to the temporary table creation is not applicable
 
1828
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1829
      there are aggregate functions, because in all these cases we need
 
1830
      all result rows.
 
1831
    */
 
1832
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1833
                             !tmp_group &&
 
1834
                             !session->lex->current_select->with_sum_func) ?
 
1835
                            select_limit : HA_POS_ERROR;
 
1836
 
 
1837
    if (!(exec_tmp_table1=
 
1838
          create_tmp_table(session, &tmp_table_param, all_fields,
 
1839
                           tmp_group,
 
1840
                           group_list ? 0 : select_distinct,
 
1841
                           group_list && simple_group,
 
1842
                           select_options,
 
1843
                           tmp_rows_limit,
 
1844
                           (char *) "")))
 
1845
                {
 
1846
      return(1);
 
1847
    }
 
1848
 
 
1849
    /*
 
1850
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1851
      - we are sorting the table and writing complete group rows to the
 
1852
        temp table.
 
1853
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1854
        on all columns.
 
1855
 
 
1856
      If having is not handled here, it will be checked before the row
 
1857
      is sent to the client.
 
1858
    */
 
1859
    if (tmp_having &&
 
1860
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1861
      having= tmp_having;
 
1862
 
 
1863
    /* if group or order on first table, sort first */
 
1864
    if (group_list && simple_group)
 
1865
    {
 
1866
      session->set_proc_info("Sorting for group");
 
1867
      if (create_sort_index(session, this, group_list,
 
1868
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1869
          alloc_group_fields(this, group_list) ||
 
1870
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1871
          setup_sum_funcs(session, sum_funcs))
 
1872
      {
 
1873
        return(1);
 
1874
      }
 
1875
      group_list=0;
 
1876
    }
 
1877
    else
 
1878
    {
 
1879
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1880
          setup_sum_funcs(session, sum_funcs))
 
1881
      {
 
1882
        return(1);
 
1883
      }
 
1884
 
 
1885
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1886
      {
 
1887
        session->set_proc_info("Sorting for order");
 
1888
        if (create_sort_index(session, this, order,
 
1889
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1890
        {
 
1891
          return(1);
 
1892
        }
 
1893
        order=0;
 
1894
      }
 
1895
    }
 
1896
 
 
1897
    /*
 
1898
      Optimize distinct when used on some of the tables
 
1899
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1900
      In this case we can stop scanning t2 when we have found one t1.a
 
1901
    */
 
1902
 
 
1903
    if (exec_tmp_table1->distinct)
 
1904
    {
 
1905
      table_map used_tables= session->used_tables;
 
1906
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1907
      do
 
1908
      {
 
1909
        if (used_tables & last_join_tab->table->map)
 
1910
          break;
 
1911
        last_join_tab->not_used_in_distinct=1;
 
1912
      } while (last_join_tab-- != join_tab);
 
1913
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1914
      if (order && skip_sort_order)
 
1915
      {
 
1916
        /* Should always succeed */
 
1917
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1918
                                    order, unit->select_limit_cnt, 0,
 
1919
                                    &join_tab[const_tables].table->
 
1920
                                      keys_in_use_for_order_by))
 
1921
          order=0;
 
1922
      }
 
1923
    }
 
1924
 
 
1925
    /*
 
1926
      If this join belongs to an uncacheable subquery save
 
1927
      the original join
 
1928
    */
 
1929
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1930
        init_save_join_tab())
 
1931
      return(-1);                         /* purecov: inspected */
 
1932
  }
 
1933
 
 
1934
  error= 0;
 
1935
  return(0);
 
1936
}
 
1937
 
 
1938
 
 
1939
/**
 
1940
  Restore values in temporary join.
 
1941
*/
 
1942
void JOIN::restore_tmp()
 
1943
{
 
1944
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1945
}
 
1946
 
 
1947
 
 
1948
int
 
1949
JOIN::reinit()
 
1950
{
 
1951
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1952
                                    select_lex->offset_limit->val_uint() :
 
1953
                                    0UL);
 
1954
 
 
1955
  first_record= 0;
 
1956
 
 
1957
  if (exec_tmp_table1)
 
1958
  {
 
1959
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1960
    exec_tmp_table1->file->ha_delete_all_rows();
 
1961
    free_io_cache(exec_tmp_table1);
 
1962
    filesort_free_buffers(exec_tmp_table1,0);
 
1963
  }
 
1964
  if (exec_tmp_table2)
 
1965
  {
 
1966
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1967
    exec_tmp_table2->file->ha_delete_all_rows();
 
1968
    free_io_cache(exec_tmp_table2);
 
1969
    filesort_free_buffers(exec_tmp_table2,0);
 
1970
  }
 
1971
  if (items0)
 
1972
    set_items_ref_array(items0);
 
1973
 
 
1974
  if (join_tab_save)
 
1975
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1976
 
 
1977
  if (tmp_join)
 
1978
    restore_tmp();
 
1979
 
 
1980
  /* Reset of sum functions */
 
1981
  if (sum_funcs)
 
1982
  {
 
1983
    Item_sum *func, **func_ptr= sum_funcs;
 
1984
    while ((func= *(func_ptr++)))
 
1985
      func->clear();
 
1986
  }
 
1987
 
 
1988
  return(0);
 
1989
}
 
1990
 
 
1991
/**
 
1992
   @brief Save the original join layout
 
1993
 
 
1994
   @details Saves the original join layout so it can be reused in
 
1995
   re-execution and for EXPLAIN.
 
1996
 
 
1997
   @return Operation status
 
1998
   @retval 0      success.
 
1999
   @retval 1      error occurred.
 
2000
*/
 
2001
 
 
2002
bool
 
2003
JOIN::init_save_join_tab()
 
2004
{
 
2005
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
 
2006
    return 1;                                  /* purecov: inspected */
 
2007
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2008
  restore_tmp();
 
2009
  return 0;
 
2010
}
 
2011
 
 
2012
 
 
2013
bool
 
2014
JOIN::save_join_tab()
 
2015
{
 
2016
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2017
  {
 
2018
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
 
2019
                                                sizeof(JOIN_TAB) * tables)))
 
2020
      return 1;
 
2021
  }
 
2022
  return 0;
 
2023
}
 
2024
 
 
2025
 
 
2026
/**
 
2027
  Exec select.
 
2028
 
 
2029
  @todo
 
2030
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2031
    finally replace sorting with index scan if there is a LIMIT clause in
 
2032
    the query.  It's never shown in EXPLAIN!
 
2033
 
 
2034
  @todo
 
2035
    When can we have here session->net.report_error not zero?
 
2036
*/
 
2037
void
 
2038
JOIN::exec()
 
2039
{
 
2040
  List<Item> *columns_list= &fields_list;
 
2041
  int      tmp_error;
 
2042
 
 
2043
  session->set_proc_info("executing");
 
2044
  error= 0;
 
2045
  (void) result->prepare2(); // Currently, this cannot fail.
 
2046
 
 
2047
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2048
  {                                           // Only test of functions
 
2049
    if (select_options & SELECT_DESCRIBE)
 
2050
      select_describe(this, false, false, false,
 
2051
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2052
    else
 
2053
    {
 
2054
      result->send_fields(*columns_list,
 
2055
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2056
      /*
 
2057
        We have to test for 'conds' here as the WHERE may not be constant
 
2058
        even if we don't have any tables for prepared statements or if
 
2059
        conds uses something like 'rand()'.
 
2060
      */
 
2061
      if (cond_value != Item::COND_FALSE &&
 
2062
          (!conds || conds->val_int()) &&
 
2063
          (!having || having->val_int()))
 
2064
      {
 
2065
        if (do_send_rows && result->send_data(fields_list))
 
2066
          error= 1;
 
2067
        else
 
2068
        {
 
2069
          error= (int) result->send_eof();
 
2070
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2071
                         session->sent_row_count);
 
2072
        }
 
2073
      }
 
2074
      else
 
2075
      {
 
2076
        error=(int) result->send_eof();
 
2077
        send_records= 0;
 
2078
      }
 
2079
    }
 
2080
    /* Single select (without union) always returns 0 or 1 row */
 
2081
    session->limit_found_rows= send_records;
 
2082
    session->examined_row_count= 0;
 
2083
    return;
 
2084
  }
 
2085
  /*
 
2086
    Don't reset the found rows count if there're no tables as
 
2087
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2088
    It must be accumulated from all join iterations of all join parts.
 
2089
  */
 
2090
  if (tables)
 
2091
    session->limit_found_rows= 0;
 
2092
 
 
2093
  if (zero_result_cause)
 
2094
  {
 
2095
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2096
                            *columns_list,
 
2097
                            send_row_on_empty_set(),
 
2098
                            select_options,
 
2099
                            zero_result_cause,
 
2100
                            having);
 
2101
    return;
 
2102
  }
 
2103
 
 
2104
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2105
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2106
    return;
 
2107
 
 
2108
  if (select_options & SELECT_DESCRIBE)
 
2109
  {
 
2110
    /*
 
2111
      Check if we managed to optimize order_st BY away and don't use temporary
 
2112
      table to resolve order_st BY: in that case, we only may need to do
 
2113
      filesort for GROUP BY.
 
2114
    */
 
2115
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2116
    {
 
2117
      /*
 
2118
        Reset 'order' to 'group_list' and reinit variables describing
 
2119
        'order'
 
2120
      */
 
2121
      order= group_list;
 
2122
      simple_order= simple_group;
 
2123
      skip_sort_order= 0;
 
2124
    }
 
2125
    if (order &&
 
2126
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2127
        (const_tables == tables ||
 
2128
         ((simple_order || skip_sort_order) &&
 
2129
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2130
                                  select_limit, 0,
 
2131
                                  &join_tab[const_tables].table->
 
2132
                                    keys_in_use_for_query))))
 
2133
      order=0;
 
2134
    having= tmp_having;
 
2135
    select_describe(this, need_tmp,
 
2136
                    order != 0 && !skip_sort_order,
 
2137
                    select_distinct,
 
2138
                    !tables ? "No tables used" : NULL);
 
2139
    return;
 
2140
  }
 
2141
 
 
2142
  JOIN *curr_join= this;
 
2143
  List<Item> *curr_all_fields= &all_fields;
 
2144
  List<Item> *curr_fields_list= &fields_list;
 
2145
  Table *curr_tmp_table= 0;
 
2146
  /*
 
2147
    Initialize examined rows here because the values from all join parts
 
2148
    must be accumulated in examined_row_count. Hence every join
 
2149
    iteration must count from zero.
 
2150
  */
 
2151
  curr_join->examined_rows= 0;
 
2152
 
 
2153
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2154
  if (need_tmp)
 
2155
  {
 
2156
    if (tmp_join)
 
2157
    {
 
2158
      /*
 
2159
        We are in a non cacheable sub query. Get the saved join structure
 
2160
        after optimization.
 
2161
        (curr_join may have been modified during last exection and we need
 
2162
        to reset it)
 
2163
      */
 
2164
      curr_join= tmp_join;
 
2165
    }
 
2166
    curr_tmp_table= exec_tmp_table1;
 
2167
 
 
2168
    /* Copy data to the temporary table */
 
2169
    session->set_proc_info("Copying to tmp table");
 
2170
    if (!curr_join->sort_and_group &&
 
2171
        curr_join->const_tables != curr_join->tables)
 
2172
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2173
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2174
    {
 
2175
      error= tmp_error;
 
2176
      return;
 
2177
    }
 
2178
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2179
 
 
2180
    if (curr_join->having)
 
2181
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2182
 
 
2183
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2184
    curr_join->all_fields= *curr_all_fields;
 
2185
    if (!items1)
 
2186
    {
 
2187
      items1= items0 + all_fields.elements;
 
2188
      if (sort_and_group || curr_tmp_table->group)
 
2189
      {
 
2190
        if (change_to_use_tmp_fields(session, items1,
 
2191
                                     tmp_fields_list1, tmp_all_fields1,
 
2192
                                     fields_list.elements, all_fields))
 
2193
          return;
 
2194
      }
 
2195
      else
 
2196
      {
 
2197
        if (change_refs_to_tmp_fields(session, items1,
 
2198
                                      tmp_fields_list1, tmp_all_fields1,
 
2199
                                      fields_list.elements, all_fields))
 
2200
          return;
 
2201
      }
 
2202
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2203
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2204
      curr_join->items1= items1;
 
2205
    }
 
2206
    curr_all_fields= &tmp_all_fields1;
 
2207
    curr_fields_list= &tmp_fields_list1;
 
2208
    curr_join->set_items_ref_array(items1);
 
2209
 
 
2210
    if (sort_and_group || curr_tmp_table->group)
 
2211
    {
 
2212
      curr_join->tmp_table_param.field_count+=
 
2213
        curr_join->tmp_table_param.sum_func_count+
 
2214
        curr_join->tmp_table_param.func_count;
 
2215
      curr_join->tmp_table_param.sum_func_count=
 
2216
        curr_join->tmp_table_param.func_count= 0;
 
2217
    }
 
2218
    else
 
2219
    {
 
2220
      curr_join->tmp_table_param.field_count+=
 
2221
        curr_join->tmp_table_param.func_count;
 
2222
      curr_join->tmp_table_param.func_count= 0;
 
2223
    }
 
2224
 
 
2225
    if (curr_tmp_table->group)
 
2226
    {                                           // Already grouped
 
2227
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2228
        curr_join->order= curr_join->group_list;  /* order by group */
 
2229
      curr_join->group_list= 0;
 
2230
    }
 
2231
 
 
2232
    /*
 
2233
      If we have different sort & group then we must sort the data by group
 
2234
      and copy it to another tmp table
 
2235
      This code is also used if we are using distinct something
 
2236
      we haven't been able to store in the temporary table yet
 
2237
      like SEC_TO_TIME(SUM(...)).
 
2238
    */
 
2239
 
 
2240
    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))
 
2241
    {                                   /* Must copy to another table */
 
2242
      /* Free first data from old join */
 
2243
      curr_join->join_free();
 
2244
      if (make_simple_join(curr_join, curr_tmp_table))
 
2245
        return;
 
2246
      calc_group_buffer(curr_join, group_list);
 
2247
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2248
                        curr_join->tmp_all_fields1,
 
2249
                        curr_join->select_distinct && !curr_join->group_list);
 
2250
      curr_join->tmp_table_param.hidden_field_count=
 
2251
        (curr_join->tmp_all_fields1.elements-
 
2252
         curr_join->tmp_fields_list1.elements);
 
2253
 
 
2254
 
 
2255
      if (exec_tmp_table2)
 
2256
        curr_tmp_table= exec_tmp_table2;
 
2257
      else
 
2258
      {
 
2259
        /* group data to new table */
 
2260
 
 
2261
        /*
 
2262
          If the access method is loose index scan then all MIN/MAX
 
2263
          functions are precomputed, and should be treated as regular
 
2264
          functions. See extended comment in JOIN::exec.
 
2265
        */
 
2266
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2267
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2268
 
 
2269
        if (!(curr_tmp_table=
 
2270
              exec_tmp_table2= create_tmp_table(session,
 
2271
                                                &curr_join->tmp_table_param,
 
2272
                                                *curr_all_fields,
 
2273
                                                (order_st*) 0,
 
2274
                                                curr_join->select_distinct &&
 
2275
                                                !curr_join->group_list,
 
2276
                                                1, curr_join->select_options,
 
2277
                                                HA_POS_ERROR,
 
2278
                                                (char *) "")))
 
2279
          return;
 
2280
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2281
      }
 
2282
      if (curr_join->group_list)
 
2283
      {
 
2284
        session->set_proc_info("Creating sort index");
 
2285
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2286
        {
 
2287
          return;
 
2288
        }
 
2289
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
2290
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2291
            make_group_fields(this, curr_join))
 
2292
        {
 
2293
          return;
 
2294
        }
 
2295
        sortorder= curr_join->sortorder;
 
2296
      }
 
2297
 
 
2298
      session->set_proc_info("Copying to group table");
 
2299
      tmp_error= -1;
 
2300
      if (curr_join != this)
 
2301
      {
 
2302
        if (sum_funcs2)
 
2303
        {
 
2304
          curr_join->sum_funcs= sum_funcs2;
 
2305
          curr_join->sum_funcs_end= sum_funcs_end2;
 
2306
        }
 
2307
        else
 
2308
        {
 
2309
          curr_join->alloc_func_list();
 
2310
          sum_funcs2= curr_join->sum_funcs;
 
2311
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2312
        }
 
2313
      }
 
2314
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2315
                                        1, true))
 
2316
        return;
 
2317
      curr_join->group_list= 0;
 
2318
      if (!curr_join->sort_and_group &&
 
2319
          curr_join->const_tables != curr_join->tables)
 
2320
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2321
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2322
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2323
      {
 
2324
        error= tmp_error;
 
2325
        return;
 
2326
      }
 
2327
      end_read_record(&curr_join->join_tab->read_record);
 
2328
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2329
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2330
 
 
2331
      // No sum funcs anymore
 
2332
      if (!items2)
 
2333
      {
 
2334
        items2= items1 + all_fields.elements;
 
2335
        if (change_to_use_tmp_fields(session, items2,
 
2336
                                     tmp_fields_list2, tmp_all_fields2,
 
2337
                                     fields_list.elements, tmp_all_fields1))
 
2338
          return;
 
2339
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2340
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2341
      }
 
2342
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2343
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2344
      curr_join->set_items_ref_array(items2);
 
2345
      curr_join->tmp_table_param.field_count+=
 
2346
        curr_join->tmp_table_param.sum_func_count;
 
2347
      curr_join->tmp_table_param.sum_func_count= 0;
 
2348
    }
 
2349
    if (curr_tmp_table->distinct)
 
2350
      curr_join->select_distinct=0;             /* Each row is unique */
 
2351
 
 
2352
    curr_join->join_free();                     /* Free quick selects */
 
2353
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2354
    {
 
2355
      session->set_proc_info("Removing duplicates");
 
2356
      if (curr_join->tmp_having)
 
2357
        curr_join->tmp_having->update_used_tables();
 
2358
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2359
                            *curr_fields_list, curr_join->tmp_having))
 
2360
        return;
 
2361
      curr_join->tmp_having=0;
 
2362
      curr_join->select_distinct=0;
 
2363
    }
 
2364
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2365
    if (make_simple_join(curr_join, curr_tmp_table))
 
2366
      return;
 
2367
    calc_group_buffer(curr_join, curr_join->group_list);
 
2368
    count_field_types(select_lex, &curr_join->tmp_table_param,
 
2369
                      *curr_all_fields, 0);
 
2370
 
 
2371
  }
 
2372
 
 
2373
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2374
  {
 
2375
    if (make_group_fields(this, curr_join))
 
2376
    {
 
2377
      return;
 
2378
    }
 
2379
    if (!items3)
 
2380
    {
 
2381
      if (!items0)
 
2382
        init_items_ref_array();
 
2383
      items3= ref_pointer_array + (all_fields.elements*4);
 
2384
      setup_copy_fields(session, &curr_join->tmp_table_param,
 
2385
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2386
                        curr_fields_list->elements, *curr_all_fields);
 
2387
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2388
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2389
      tmp_table_param.save_copy_field_end=
 
2390
        curr_join->tmp_table_param.copy_field_end;
 
2391
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2392
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2393
    }
 
2394
    else
 
2395
    {
 
2396
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2397
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2398
      curr_join->tmp_table_param.copy_field_end=
 
2399
        tmp_table_param.save_copy_field_end;
 
2400
    }
 
2401
    curr_fields_list= &tmp_fields_list3;
 
2402
    curr_all_fields= &tmp_all_fields3;
 
2403
    curr_join->set_items_ref_array(items3);
 
2404
 
 
2405
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2406
                                      1, true) ||
 
2407
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
2408
        session->is_fatal_error)
 
2409
      return;
 
2410
  }
 
2411
  if (curr_join->group_list || curr_join->order)
 
2412
  {
 
2413
    session->set_proc_info("Sorting result");
 
2414
    /* If we have already done the group, add HAVING to sorted table */
 
2415
    if (curr_join->tmp_having && ! curr_join->group_list &&
 
2416
        ! curr_join->sort_and_group)
 
2417
    {
 
2418
      // Some tables may have been const
 
2419
      curr_join->tmp_having->update_used_tables();
 
2420
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2421
      table_map used_tables= (curr_join->const_table_map |
 
2422
                              curr_table->table->map);
 
2423
 
 
2424
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2425
                                                 used_tables,
 
2426
                                                 used_tables, 0);
 
2427
      if (sort_table_cond)
 
2428
      {
 
2429
        if (!curr_table->select)
 
2430
          if (!(curr_table->select= new SQL_SELECT))
 
2431
            return;
 
2432
        if (!curr_table->select->cond)
 
2433
          curr_table->select->cond= sort_table_cond;
 
2434
        else                                    // This should never happen
 
2435
        {
 
2436
          if (!(curr_table->select->cond=
 
2437
                new Item_cond_and(curr_table->select->cond,
 
2438
                                  sort_table_cond)))
 
2439
            return;
 
2440
          /*
 
2441
            Item_cond_and do not need fix_fields for execution, its parameters
 
2442
            are fixed or do not need fix_fields, too
 
2443
          */
 
2444
          curr_table->select->cond->quick_fix_field();
 
2445
        }
 
2446
        curr_table->select_cond= curr_table->select->cond;
 
2447
        curr_table->select_cond->top_level_item();
 
2448
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2449
                                                   ~ (table_map) 0,
 
2450
                                                   ~used_tables, 0);
 
2451
      }
 
2452
    }
 
2453
    {
 
2454
      if (group)
 
2455
        curr_join->select_limit= HA_POS_ERROR;
 
2456
      else
 
2457
      {
 
2458
        /*
 
2459
          We can abort sorting after session->select_limit rows if we there is no
 
2460
          WHERE clause for any tables after the sorted one.
 
2461
        */
 
2462
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2463
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2464
        for (; curr_table < end_table ; curr_table++)
 
2465
        {
 
2466
          /*
 
2467
            table->keyuse is set in the case there was an original WHERE clause
 
2468
            on the table that was optimized away.
 
2469
          */
 
2470
          if (curr_table->select_cond ||
 
2471
              (curr_table->keyuse && !curr_table->first_inner))
 
2472
          {
 
2473
            /* We have to sort all rows */
 
2474
            curr_join->select_limit= HA_POS_ERROR;
 
2475
            break;
 
2476
          }
 
2477
        }
 
2478
      }
 
2479
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2480
      {
 
2481
        return;
 
2482
      }
 
2483
      /*
 
2484
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2485
        chose FILESORT to be faster than INDEX SCAN or there is no
 
2486
        suitable index present.
 
2487
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2488
        finally replace sorting with index scan if there is a LIMIT clause in
 
2489
        the query. XXX: it's never shown in EXPLAIN!
 
2490
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2491
      */
 
2492
      if (create_sort_index(session, curr_join,
 
2493
                            curr_join->group_list ?
 
2494
                            curr_join->group_list : curr_join->order,
 
2495
                            curr_join->select_limit,
 
2496
                            (select_options & OPTION_FOUND_ROWS ?
 
2497
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2498
                            curr_join->group_list ? true : false))
 
2499
        return;
 
2500
      sortorder= curr_join->sortorder;
 
2501
      if (curr_join->const_tables != curr_join->tables &&
 
2502
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2503
      {
 
2504
        /*
 
2505
          If no IO cache exists for the first table then we are using an
 
2506
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2507
          attribute on the INDEX SCAN.
 
2508
        */
 
2509
        skip_sort_order= 1;
 
2510
      }
 
2511
    }
 
2512
  }
 
2513
  /* XXX: When can we have here session->is_error() not zero? */
 
2514
  if (session->is_error())
 
2515
  {
 
2516
    error= session->is_error();
 
2517
    return;
 
2518
  }
 
2519
  curr_join->having= curr_join->tmp_having;
 
2520
  curr_join->fields= curr_fields_list;
 
2521
 
 
2522
  {
 
2523
    session->set_proc_info("Sending data");
 
2524
    result->send_fields(*curr_fields_list,
 
2525
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2526
    error= do_select(curr_join, curr_fields_list, NULL);
 
2527
    session->limit_found_rows= curr_join->send_records;
 
2528
  }
 
2529
 
 
2530
  /* Accumulate the counts from all join iterations of all join parts. */
 
2531
  session->examined_row_count+= curr_join->examined_rows;
 
2532
 
 
2533
  /*
 
2534
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2535
    for a derived table which is always materialized.
 
2536
    Otherwise we would not be able to print the query  correctly.
 
2537
  */
 
2538
  if (items0 &&
 
2539
      (session->lex->describe & DESCRIBE_EXTENDED) &&
 
2540
      select_lex->linkage == DERIVED_TABLE_TYPE)
 
2541
    set_items_ref_array(items0);
 
2542
 
 
2543
  return;
 
2544
}
 
2545
 
 
2546
 
 
2547
/**
 
2548
  Clean up join.
 
2549
 
 
2550
  @return
 
2551
    Return error that hold JOIN.
 
2552
*/
 
2553
 
 
2554
int
 
2555
JOIN::destroy()
 
2556
{
 
2557
  select_lex->join= 0;
 
2558
 
 
2559
  if (tmp_join)
 
2560
  {
 
2561
    if (join_tab != tmp_join->join_tab)
 
2562
    {
 
2563
      JOIN_TAB *tab, *end;
 
2564
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2565
        tab->cleanup();
 
2566
    }
 
2567
    tmp_join->tmp_join= 0;
 
2568
    tmp_table_param.copy_field=0;
 
2569
    return(tmp_join->destroy());
 
2570
  }
 
2571
  cond_equal= 0;
 
2572
 
 
2573
  cleanup(1);
 
2574
  if (exec_tmp_table1)
 
2575
    exec_tmp_table1->free_tmp_table(session);
 
2576
  if (exec_tmp_table2)
 
2577
    exec_tmp_table2->free_tmp_table(session);
 
2578
  delete select;
 
2579
  delete_dynamic(&keyuse);
 
2580
  return(error);
 
2581
}
 
2582
 
 
2583
 
 
2584
 
316
2585
/**
317
2586
  An entry point to single-unit select (a select without UNION).
318
2587
 
319
 
  @param session                  thread Cursor
 
2588
  @param session                  thread handler
320
2589
  @param rref_pointer_array   a reference to ref_pointer_array of
321
2590
                              the top-level select_lex for this query
322
2591
  @param tables               list of all tables used in this query.
332
2601
                              for a, b and c in this list.
333
2602
  @param conds                top level item of an expression representing
334
2603
                              WHERE clause of the top level select
335
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2604
  @param og_num               total number of order_st BY and GROUP BY clauses
336
2605
                              arguments
337
 
  @param order                linked list of ORDER BY agruments
 
2606
  @param order                linked list of order_st BY agruments
338
2607
  @param group                linked list of GROUP BY arguments
339
2608
  @param having               top level item of HAVING expression
 
2609
  @param proc_param           list of PROCEDUREs
340
2610
  @param select_options       select options (BIG_RESULT, etc)
341
2611
  @param result               an instance of result set handling class.
342
2612
                              This object is responsible for send result
343
2613
                              set rows to the client or inserting them
344
2614
                              into a table.
345
 
  @param select_lex           the only Select_Lex of this query
 
2615
  @param select_lex           the only SELECT_LEX of this query
346
2616
  @param unit                 top-level UNIT of this query
347
2617
                              UNIT is an artificial object created by the
348
2618
                              parser for every SELECT clause.
355
2625
  @retval
356
2626
    true   an error
357
2627
*/
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)
 
2628
 
 
2629
bool
 
2630
mysql_select(Session *session, Item ***rref_pointer_array,
 
2631
             TableList *tables, uint32_t wild_num, List<Item> &fields,
 
2632
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2633
             Item *having, order_st *proc_param, uint64_t select_options,
 
2634
             select_result *result, SELECT_LEX_UNIT *unit,
 
2635
             SELECT_LEX *select_lex)
372
2636
{
373
2637
  bool err;
374
2638
  bool free_join= 1;
375
2639
 
376
2640
  select_lex->context.resolve_in_select_list= true;
377
 
  Join *join;
 
2641
  JOIN *join;
378
2642
  if (select_lex->join != 0)
379
2643
  {
380
2644
    join= select_lex->join;
383
2647
      creation
384
2648
    */
385
2649
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
386
 
        (select_options & SELECT_DESCRIBE))
 
2650
        (select_options & SELECT_DESCRIBE))
387
2651
    {
388
2652
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
389
2653
      {
390
 
        //here is EXPLAIN of subselect or derived table
391
 
        if (join->change_result(result))
392
 
        {
393
 
          return(true);
394
 
        }
 
2654
        //here is EXPLAIN of subselect or derived table
 
2655
        if (join->change_result(result))
 
2656
        {
 
2657
          return(true);
 
2658
        }
395
2659
      }
396
2660
      else
397
2661
      {
398
2662
        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
 
        }
 
2663
                               conds, og_num, order, group, having, proc_param,
 
2664
                               select_lex, unit)))
 
2665
        {
 
2666
          goto err;
 
2667
        }
403
2668
      }
404
2669
    }
405
2670
    free_join= 0;
407
2672
  }
408
2673
  else
409
2674
  {
410
 
    if (!(join= new Join(session, fields, select_options, result)))
411
 
      return(true);
 
2675
    if (!(join= new JOIN(session, fields, select_options, result)))
 
2676
        return(true);
412
2677
    session->set_proc_info("init");
413
2678
    session->used_tables=0;                         // Updated by setup_fields
414
2679
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
415
 
                           conds, og_num, order, group, having,
 
2680
                           conds, og_num, order, group, having, proc_param,
416
2681
                           select_lex, unit)) == true)
417
2682
    {
418
2683
      goto err;
419
2684
    }
420
2685
  }
421
2686
 
422
 
  err= join->optimize();
423
 
  if (err)
424
 
  {
425
 
    goto err; // 1
426
 
  }
427
 
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2687
  if (join->flatten_subqueries())
 
2688
  {
 
2689
    err= 1;
 
2690
    goto err;
 
2691
  }
 
2692
 
 
2693
  if ((err= join->optimize()))
 
2694
  {
 
2695
    goto err;                                   // 1
 
2696
  }
 
2697
 
 
2698
  if (session->lex->describe & DESCRIBE_EXTENDED)
429
2699
  {
430
2700
    join->conds_history= join->conds;
431
2701
    join->having_history= (join->having?join->having:join->tmp_having);
436
2706
 
437
2707
  join->exec();
438
2708
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
2709
  if (session->lex->describe & DESCRIBE_EXTENDED)
440
2710
  {
441
2711
    select_lex->where= join->conds_history;
442
2712
    select_lex->having= join->having_history;
452
2722
  return(join->error);
453
2723
}
454
2724
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2725
 
 
2726
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
 
2727
                          Item_in_subselect* const *el2)
 
2728
{
 
2729
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
 
2730
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2731
}
 
2732
 
 
2733
 
 
2734
inline Item * and_items(Item* cond, Item *item)
456
2735
{
457
2736
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2737
}
459
2738
 
 
2739
 
 
2740
static TableList *alloc_join_nest(Session *session)
 
2741
{
 
2742
  TableList *tbl;
 
2743
  if (!(tbl= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2744
                                       sizeof(nested_join_st))))
 
2745
    return NULL;
 
2746
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl +
 
2747
                                    ALIGN_SIZE(sizeof(TableList)));
 
2748
  return tbl;
 
2749
}
 
2750
 
 
2751
 
 
2752
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2753
{
 
2754
  List_iterator<TableList> it(*tlist);
 
2755
  TableList *table;
 
2756
  while ((table= it++))
 
2757
  {
 
2758
    if (table->on_expr)
 
2759
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2760
    if (table->nested_join)
 
2761
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2762
  }
 
2763
}
 
2764
 
 
2765
 
 
2766
/*
 
2767
  Convert a subquery predicate into a TableList semi-join nest
 
2768
 
 
2769
  SYNOPSIS
 
2770
    convert_subq_to_sj()
 
2771
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON
 
2772
                    clause
 
2773
       subq_pred    Subquery predicate to be converted
 
2774
 
 
2775
  DESCRIPTION
 
2776
    Convert a subquery predicate into a TableList semi-join nest. All the
 
2777
    prerequisites are already checked, so the conversion is always successfull.
 
2778
 
 
2779
    Prepared Statements: the transformation is permanent:
 
2780
     - Changes in TableList structures are naturally permanent
 
2781
     - Item tree changes are performed on statement MEM_ROOT:
 
2782
        = we activate statement MEM_ROOT
 
2783
        = this function is called before the first fix_prepare_information
 
2784
          call.
 
2785
 
 
2786
    This is intended because the criteria for subquery-to-sj conversion remain
 
2787
    constant for the lifetime of the Prepared Statement.
 
2788
 
 
2789
  RETURN
 
2790
    false  OK
 
2791
    true   Out of memory error
 
2792
*/
 
2793
 
 
2794
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2795
{
 
2796
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2797
  TableList *emb_tbl_nest= NULL;
 
2798
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2799
  Session *session= parent_join->session;
 
2800
 
 
2801
  /*
 
2802
    1. Find out where to put the predicate into.
 
2803
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2804
  */
 
2805
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2806
  {
 
2807
    if (subq_pred->expr_join_nest->nested_join)
 
2808
    {
 
2809
      /*
 
2810
        We're dealing with
 
2811
 
 
2812
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2813
 
 
2814
        The sj-nest will be inserted into the brackets nest.
 
2815
      */
 
2816
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2817
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2818
    }
 
2819
    else if (!subq_pred->expr_join_nest->outer_join)
 
2820
    {
 
2821
      /*
 
2822
        We're dealing with
 
2823
 
 
2824
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2825
 
 
2826
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2827
        parent. This is ok because tblX is joined as an inner join.
 
2828
      */
 
2829
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2830
      if (emb_tbl_nest)
 
2831
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2832
    }
 
2833
    else if (!subq_pred->expr_join_nest->nested_join)
 
2834
    {
 
2835
      TableList *outer_tbl= subq_pred->expr_join_nest;
 
2836
      TableList *wrap_nest;
 
2837
      /*
 
2838
        We're dealing with
 
2839
 
 
2840
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2841
 
 
2842
        we'll need to convert it into:
 
2843
 
 
2844
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2845
                        |                      |
 
2846
                        |<----- wrap_nest ---->|
 
2847
 
 
2848
        Q:  other subqueries may be pointing to this element. What to do?
 
2849
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2850
            But we'll need to fix other pointers.
 
2851
        A2: Another way: have TableList::next_ptr so the following
 
2852
            subqueries know the table has been nested.
 
2853
        A3: changes in the TableList::outer_join will make everything work
 
2854
            automatically.
 
2855
      */
 
2856
      if (!(wrap_nest= alloc_join_nest(parent_join->session)))
 
2857
      {
 
2858
        return(true);
 
2859
      }
 
2860
      wrap_nest->embedding= outer_tbl->embedding;
 
2861
      wrap_nest->join_list= outer_tbl->join_list;
 
2862
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2863
 
 
2864
      wrap_nest->nested_join->join_list.empty();
 
2865
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2866
 
 
2867
      outer_tbl->embedding= wrap_nest;
 
2868
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2869
 
 
2870
      /*
 
2871
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2872
        and on_expr
 
2873
      */
 
2874
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2875
      outer_tbl->outer_join= 0;
 
2876
 
 
2877
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2878
      outer_tbl->on_expr= NULL;
 
2879
 
 
2880
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2881
      TableList *tbl;
 
2882
      while ((tbl= li++))
 
2883
      {
 
2884
        if (tbl == outer_tbl)
 
2885
        {
 
2886
          li.replace(wrap_nest);
 
2887
          break;
 
2888
        }
 
2889
      }
 
2890
      /*
 
2891
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
 
2892
        semi-join nest into it
 
2893
      */
 
2894
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2895
      emb_tbl_nest=  wrap_nest;
 
2896
    }
 
2897
  }
 
2898
 
 
2899
  TableList *sj_nest;
 
2900
  nested_join_st *nested_join;
 
2901
  if (!(sj_nest= alloc_join_nest(parent_join->session)))
 
2902
  {
 
2903
    return(true);
 
2904
  }
 
2905
  nested_join= sj_nest->nested_join;
 
2906
 
 
2907
  sj_nest->join_list= emb_join_list;
 
2908
  sj_nest->embedding= emb_tbl_nest;
 
2909
  sj_nest->alias= (char*) "(sj-nest)";
 
2910
  /* Nests do not participate in those 'chains', so: */
 
2911
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2912
  emb_join_list->push_back(sj_nest);
 
2913
 
 
2914
  /*
 
2915
    nested_join->used_tables and nested_join->not_null_tables are
 
2916
    initialized in simplify_joins().
 
2917
  */
 
2918
 
 
2919
  /*
 
2920
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2921
       sj-nest.
 
2922
  */
 
2923
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2924
  nested_join->join_list.empty();
 
2925
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2926
  TableList *tl, *last_leaf;
 
2927
  while ((tl= li++))
 
2928
  {
 
2929
    tl->embedding= sj_nest;
 
2930
    tl->join_list= &nested_join->join_list;
 
2931
    nested_join->join_list.push_back(tl);
 
2932
  }
 
2933
 
 
2934
  /*
 
2935
    Reconnect the next_leaf chain.
 
2936
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2937
          Inserting them at the beginning would be a bit faster.
 
2938
    NOTE: We actually insert them at the front! That's because the order is
 
2939
          reversed in this list.
 
2940
  */
 
2941
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2942
  tl->next_leaf= subq_lex->leaf_tables;
 
2943
  last_leaf= tl;
 
2944
 
 
2945
  /*
 
2946
    Same as above for next_local chain
 
2947
    (a theory: a next_local chain always starts with ::leaf_tables
 
2948
     because view's tables are inserted after the view)
 
2949
  */
 
2950
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2951
  tl->next_local= subq_lex->leaf_tables;
 
2952
 
 
2953
  /* A theory: no need to re-connect the next_global chain */
 
2954
 
 
2955
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2956
 
 
2957
  // The subqueries were replaced for Item_int(1) earlier
 
2958
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2959
  /*TODO: also reset the 'with_subselect' there. */
 
2960
 
 
2961
  /* n. Adjust the parent_join->tables counter */
 
2962
  uint32_t table_no= parent_join->tables;
 
2963
  /* n. Walk through child's tables and adjust table->map */
 
2964
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2965
  {
 
2966
    tl->table->tablenr= table_no;
 
2967
    tl->table->map= ((table_map)1) << table_no;
 
2968
    SELECT_LEX *old_sl= tl->select_lex;
 
2969
    tl->select_lex= parent_join->select_lex;
 
2970
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2971
      emb->select_lex= parent_join->select_lex;
 
2972
  }
 
2973
  parent_join->tables += subq_lex->join->tables;
 
2974
 
 
2975
  /*
 
2976
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2977
    Add the subquery-induced equalities too.
 
2978
  */
 
2979
  SELECT_LEX *save_lex= session->lex->current_select;
 
2980
  session->lex->current_select=subq_lex;
 
2981
  if (!subq_pred->left_expr->fixed &&
 
2982
       subq_pred->left_expr->fix_fields(session, &subq_pred->left_expr))
 
2983
    return(true);
 
2984
  session->lex->current_select=save_lex;
 
2985
 
 
2986
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2987
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2988
                                        subq_pred->left_expr->used_tables();
 
2989
  sj_nest->sj_on_expr= subq_lex->where;
 
2990
 
 
2991
  /*
 
2992
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2993
    Additionally, for InsideOut strategy
 
2994
     - Record the number of IN-equalities.
 
2995
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2996
       see which of the expressions are bound and which are not (for those
 
2997
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2998
 
 
2999
       (TODO: can we just create a list of pointers and hope the expressions
 
3000
       will not substitute themselves on fix_fields()? or we need to wrap
 
3001
       them into Item_direct_view_refs and store pointers to those. The
 
3002
       pointers to Item_direct_view_refs are guaranteed to be stable as
 
3003
       Item_direct_view_refs doesn't substitute itself with anything in
 
3004
       Item_direct_view_ref::fix_fields.
 
3005
  */
 
3006
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3007
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3008
 
 
3009
  if (subq_pred->left_expr->cols() == 1)
 
3010
  {
 
3011
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3012
 
 
3013
    Item *item_eq= new Item_func_eq(subq_pred->left_expr,
 
3014
                                    subq_lex->ref_pointer_array[0]);
 
3015
    item_eq->name= (char*)subq_sj_cond_name;
 
3016
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3017
  }
 
3018
  else
 
3019
  {
 
3020
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
3021
    {
 
3022
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3023
                                                element_index(i));
 
3024
      Item *item_eq=
 
3025
        new Item_func_eq(subq_pred->left_expr->element_index(i),
 
3026
                         subq_lex->ref_pointer_array[i]);
 
3027
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3028
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3029
    }
 
3030
  }
 
3031
  /* Fix the created equality and AND */
 
3032
  sj_nest->sj_on_expr->fix_fields(parent_join->session, &sj_nest->sj_on_expr);
 
3033
 
 
3034
  /*
 
3035
    Walk through sj nest's WHERE and ON expressions and call
 
3036
    item->fix_table_changes() for all items.
 
3037
  */
 
3038
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3039
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3040
 
 
3041
 
 
3042
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3043
  subq_lex->master_unit()->exclude_level();
 
3044
 
 
3045
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3046
  if (emb_tbl_nest)
 
3047
  {
 
3048
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
 
3049
                                     sj_nest->sj_on_expr);
 
3050
    emb_tbl_nest->on_expr->fix_fields(parent_join->session, &emb_tbl_nest->on_expr);
 
3051
  }
 
3052
  else
 
3053
  {
 
3054
    /* Inject into the WHERE */
 
3055
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3056
    parent_join->conds->fix_fields(parent_join->session, &parent_join->conds);
 
3057
    parent_join->select_lex->where= parent_join->conds;
 
3058
  }
 
3059
 
 
3060
  return(false);
 
3061
}
 
3062
 
 
3063
 
 
3064
/*
 
3065
  Convert candidate subquery predicates to semi-joins
 
3066
 
 
3067
  SYNOPSIS
 
3068
    JOIN::flatten_subqueries()
 
3069
 
 
3070
  DESCRIPTION
 
3071
    Convert candidate subquery predicates to semi-joins.
 
3072
 
 
3073
  RETURN
 
3074
    false  OK
 
3075
    true   Error
 
3076
*/
 
3077
 
 
3078
bool JOIN::flatten_subqueries()
 
3079
{
 
3080
  Item_in_subselect **in_subq;
 
3081
  Item_in_subselect **in_subq_end;
 
3082
 
 
3083
  if (sj_subselects.elements() == 0)
 
3084
    return(false);
 
3085
 
 
3086
  /* 1. Fix children subqueries */
 
3087
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
 
3088
       in_subq != in_subq_end; in_subq++)
 
3089
  {
 
3090
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3091
    child_join->outer_tables = child_join->tables;
 
3092
    if (child_join->flatten_subqueries())
 
3093
      return(true);
 
3094
    (*in_subq)->sj_convert_priority=
 
3095
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3096
  }
 
3097
 
 
3098
  /*
 
3099
    2. Pick which subqueries to convert:
 
3100
      sort the subquery array
 
3101
      - prefer correlated subqueries over uncorrelated;
 
3102
      - prefer subqueries that have greater number of outer tables;
 
3103
  */
 
3104
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3105
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3106
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3107
  for (in_subq= sj_subselects.front();
 
3108
       in_subq != in_subq_end &&
 
3109
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3110
       in_subq++)
 
3111
  {
 
3112
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3113
      return(true);
 
3114
  }
 
3115
 
 
3116
  for (in_subq= sj_subselects.front();
 
3117
       in_subq != in_subq_end &&
 
3118
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3119
       in_subq++)
 
3120
  {
 
3121
    if (convert_subq_to_sj(this, *in_subq))
 
3122
      return(true);
 
3123
  }
 
3124
 
 
3125
  /* 3. Finalize those we didn't convert */
 
3126
  for (; in_subq!= in_subq_end; in_subq++)
 
3127
  {
 
3128
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3129
    Item_subselect::trans_res res;
 
3130
    (*in_subq)->changed= 0;
 
3131
    (*in_subq)->fixed= 0;
 
3132
    res= (*in_subq)->select_transformer(child_join);
 
3133
    if (res == Item_subselect::RES_ERROR)
 
3134
      return(true);
 
3135
 
 
3136
    (*in_subq)->changed= 1;
 
3137
    (*in_subq)->fixed= 1;
 
3138
 
 
3139
    Item *substitute= (*in_subq)->substitution;
 
3140
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3141
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3142
      return(true);
 
3143
 
 
3144
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
 
3145
    //  return(true);
 
3146
  }
 
3147
  sj_subselects.clear();
 
3148
  return(false);
 
3149
}
 
3150
 
 
3151
 
 
3152
/**
 
3153
  Setup for execution all subqueries of a query, for which the optimizer
 
3154
  chose hash semi-join.
 
3155
 
 
3156
  @details Iterate over all subqueries of the query, and if they are under an
 
3157
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3158
  - try to initialize all data structures needed for the materialized execution
 
3159
    of the IN predicate,
 
3160
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3161
    previously blocked during JOIN::prepare.
 
3162
 
 
3163
  This method is part of the "code generation" query processing phase.
 
3164
 
 
3165
  This phase must be called after substitute_for_best_equal_field() because
 
3166
  that function may replace items with other items from a multiple equality,
 
3167
  and we need to reference the correct items in the index access method of the
 
3168
  IN predicate.
 
3169
 
 
3170
  @return Operation status
 
3171
  @retval false     success.
 
3172
  @retval true      error occurred.
 
3173
*/
 
3174
 
 
3175
bool JOIN::setup_subquery_materialization()
 
3176
{
 
3177
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3178
       un= un->next_unit())
 
3179
  {
 
3180
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3181
    {
 
3182
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3183
      if (subquery_predicate &&
 
3184
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3185
      {
 
3186
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3187
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3188
            in_subs->setup_engine())
 
3189
          return true;
 
3190
      }
 
3191
    }
 
3192
  }
 
3193
  return false;
 
3194
}
 
3195
 
 
3196
 
 
3197
/*
 
3198
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3199
 
 
3200
  SYNOPSIS
 
3201
    find_eq_ref_candidate()
 
3202
      table             Table to be checked
 
3203
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3204
                        count.
 
3205
 
 
3206
  DESCRIPTION
 
3207
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3208
 
 
3209
  TODO
 
3210
    Check again if it is feasible to factor common parts with constant table
 
3211
    search
 
3212
 
 
3213
  RETURN
 
3214
    true  - There exists an eq_ref(outer-tables) candidate
 
3215
    false - Otherwise
 
3216
*/
 
3217
 
 
3218
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3219
{
 
3220
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3221
  uint32_t key;
 
3222
 
 
3223
  if (keyuse)
 
3224
  {
 
3225
    while (1) /* For each key */
 
3226
    {
 
3227
      key= keyuse->key;
 
3228
      KEY *keyinfo= table->key_info + key;
 
3229
      key_part_map bound_parts= 0;
 
3230
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3231
      {
 
3232
        do  /* For all equalities on all key parts */
 
3233
        {
 
3234
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3235
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3236
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3237
          {
 
3238
            bound_parts |= 1 << keyuse->keypart;
 
3239
          }
 
3240
          keyuse++;
 
3241
        } while (keyuse->key == key && keyuse->table == table);
 
3242
 
 
3243
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3244
          return true;
 
3245
        if (keyuse->table != table)
 
3246
          return false;
 
3247
      }
 
3248
      else
 
3249
      {
 
3250
        do
 
3251
        {
 
3252
          keyuse++;
 
3253
          if (keyuse->table != table)
 
3254
            return false;
 
3255
        }
 
3256
        while (keyuse->key == key);
 
3257
      }
 
3258
    }
 
3259
  }
 
3260
  return false;
 
3261
}
 
3262
 
 
3263
 
 
3264
/*
 
3265
  Pull tables out of semi-join nests, if possible
 
3266
 
 
3267
  SYNOPSIS
 
3268
    pull_out_semijoin_tables()
 
3269
      join  The join where to do the semi-join flattening
 
3270
 
 
3271
  DESCRIPTION
 
3272
    Try to pull tables out of semi-join nests.
 
3273
 
 
3274
    PRECONDITIONS
 
3275
    When this function is called, the join may have several semi-join nests
 
3276
    (possibly within different semi-join nests), but it is guaranteed that
 
3277
    one semi-join nest does not contain another.
 
3278
 
 
3279
    ACTION
 
3280
    A table can be pulled out of the semi-join nest if
 
3281
     - It is a constant table
 
3282
     - It is accessed
 
3283
 
 
3284
    POSTCONDITIONS
 
3285
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3286
       tables)
 
3287
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3288
     * Semi-join nests TableList::sj_inner_tables
 
3289
 
 
3290
    This operation is (and should be) performed at each PS execution since
 
3291
    tables may become/cease to be constant across PS reexecutions.
 
3292
 
 
3293
  RETURN
 
3294
    0 - OK
 
3295
    1 - Out of memory error
 
3296
*/
 
3297
 
 
3298
int pull_out_semijoin_tables(JOIN *join)
 
3299
{
 
3300
  TableList *sj_nest;
 
3301
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3302
 
 
3303
  /* Try pulling out of the each of the semi-joins */
 
3304
  while ((sj_nest= sj_list_it++))
 
3305
  {
 
3306
    /* Action #1: Mark the constant tables to be pulled out */
 
3307
    table_map pulled_tables= 0;
 
3308
 
 
3309
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3310
    TableList *tbl;
 
3311
    while ((tbl= child_li++))
 
3312
    {
 
3313
      if (tbl->table)
 
3314
      {
 
3315
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3316
        if (tbl->table->map & join->const_table_map)
 
3317
        {
 
3318
          pulled_tables |= tbl->table->map;
 
3319
        }
 
3320
      }
 
3321
    }
 
3322
 
 
3323
    /*
 
3324
      Action #2: Find which tables we can pull out based on
 
3325
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3326
      us to pull out some other tables too.
 
3327
    */
 
3328
    bool pulled_a_table;
 
3329
    do
 
3330
    {
 
3331
      pulled_a_table= false;
 
3332
      child_li.rewind();
 
3333
      while ((tbl= child_li++))
 
3334
      {
 
3335
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3336
        {
 
3337
          if (find_eq_ref_candidate(tbl->table,
 
3338
                                    sj_nest->nested_join->used_tables &
 
3339
                                    ~pulled_tables))
 
3340
          {
 
3341
            pulled_a_table= true;
 
3342
            pulled_tables |= tbl->table->map;
 
3343
          }
 
3344
        }
 
3345
      }
 
3346
    } while (pulled_a_table);
 
3347
 
 
3348
    child_li.rewind();
 
3349
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3350
    {
 
3351
      (sj_nest)->sj_inner_tables= 0;
 
3352
      while ((tbl= child_li++))
 
3353
      {
 
3354
        if (tbl->table)
 
3355
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3356
      }
 
3357
    }
 
3358
    else
 
3359
    {
 
3360
      /* Record the bitmap of inner tables, mark the inner tables */
 
3361
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
 
3362
                             ~pulled_tables;
 
3363
      (sj_nest)->sj_inner_tables= inner_tables;
 
3364
      while ((tbl= child_li++))
 
3365
      {
 
3366
        if (tbl->table)
 
3367
        {
 
3368
          if (inner_tables & tbl->table->map)
 
3369
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3370
          else
 
3371
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3372
        }
 
3373
      }
 
3374
    }
 
3375
  }
 
3376
  return(0);
 
3377
}
 
3378
 
460
3379
/*****************************************************************************
461
 
  Create JoinTableS, make a guess about the table types,
 
3380
  Create JOIN_TABS, make a guess about the table types,
462
3381
  Approximate how many records will be used in each table
463
3382
*****************************************************************************/
464
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3383
 
 
3384
 
 
3385
static ha_rows get_quick_record_count(Session *session, SQL_SELECT *select,
 
3386
                                      Table *table,
 
3387
                                      const key_map *keys,ha_rows limit)
465
3388
{
466
3389
  int error;
467
3390
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
482
3405
  return(HA_POS_ERROR);                 /* This shouldn't happend */
483
3406
}
484
3407
 
 
3408
/*
 
3409
   This structure is used to collect info on potentially sargable
 
3410
   predicates in order to check whether they become sargable after
 
3411
   reading const tables.
 
3412
   We form a bitmap of indexes that can be used for sargable predicates.
 
3413
   Only such indexes are involved in range analysis.
 
3414
*/
 
3415
typedef struct st_sargable_param
 
3416
{
 
3417
  Field *field;              /* field against which to check sargability */
 
3418
  Item **arg_value;          /* values of potential keys for lookups     */
 
3419
  uint32_t num_values;           /* number of values in the above array      */
 
3420
} SARGABLE_PARAM;
 
3421
 
 
3422
/**
 
3423
  Calculate the best possible join and initialize the join structure.
 
3424
 
 
3425
  @retval
 
3426
    0   ok
 
3427
  @retval
 
3428
    1   Fatal error
 
3429
*/
 
3430
 
 
3431
static bool
 
3432
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3433
                     DYNAMIC_ARRAY *keyuse_array)
 
3434
{
 
3435
  int error;
 
3436
  Table *table;
 
3437
  uint32_t i,table_count,const_count,key;
 
3438
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3439
  key_map const_ref, eq_part;
 
3440
  Table **table_vector;
 
3441
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3442
  KEYUSE *keyuse,*start_keyuse;
 
3443
  table_map outer_join=0;
 
3444
  SARGABLE_PARAM *sargables= 0;
 
3445
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3446
 
 
3447
  table_count=join->tables;
 
3448
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
 
3449
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3450
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
3451
  if (!stat || !stat_ref || !table_vector)
 
3452
    return(1);                          // Eom /* purecov: inspected */
 
3453
 
 
3454
  join->best_ref=stat_vector;
 
3455
 
 
3456
  stat_end=stat+table_count;
 
3457
  found_const_table_map= all_table_map=0;
 
3458
  const_count=0;
 
3459
 
 
3460
  for (s= stat, i= 0;
 
3461
       tables;
 
3462
       s++, tables= tables->next_leaf, i++)
 
3463
  {
 
3464
    TableList *embedding= tables->embedding;
 
3465
    stat_vector[i]=s;
 
3466
    s->keys.init();
 
3467
    s->const_keys.init();
 
3468
    s->checked_keys.init();
 
3469
    s->needed_reg.init();
 
3470
    table_vector[i]=s->table=table=tables->table;
 
3471
    table->pos_in_table_list= tables;
 
3472
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3473
    if(error)
 
3474
    {
 
3475
        table->file->print_error(error, MYF(0));
 
3476
        return(1);
 
3477
    }
 
3478
    table->quick_keys.clear_all();
 
3479
    table->reginfo.join_tab=s;
 
3480
    table->reginfo.not_exists_optimize=0;
 
3481
    memset(table->const_key_parts, 0,
 
3482
           sizeof(key_part_map)*table->s->keys);
 
3483
    all_table_map|= table->map;
 
3484
    s->join=join;
 
3485
    s->info=0;                                  // For describe
 
3486
 
 
3487
    s->dependent= tables->dep_tables;
 
3488
    s->key_dependent= 0;
 
3489
    if (tables->schema_table)
 
3490
      table->file->stats.records= 2;
 
3491
    table->quick_condition_rows= table->file->stats.records;
 
3492
 
 
3493
    s->on_expr_ref= &tables->on_expr;
 
3494
    if (*s->on_expr_ref)
 
3495
    {
 
3496
      /* s is the only inner table of an outer join */
 
3497
      if (!table->file->stats.records && !embedding)
 
3498
      {                                         // Empty table
 
3499
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3500
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3501
        continue;
 
3502
      }
 
3503
      outer_join|= table->map;
 
3504
      s->embedding_map= 0;
 
3505
      for (;embedding; embedding= embedding->embedding)
 
3506
        s->embedding_map|= embedding->nested_join->nj_map;
 
3507
      continue;
 
3508
    }
 
3509
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3510
    {
 
3511
      /* s belongs to a nested join, maybe to several embedded joins */
 
3512
      s->embedding_map= 0;
 
3513
      do
 
3514
      {
 
3515
        nested_join_st *nested_join= embedding->nested_join;
 
3516
        s->embedding_map|=nested_join->nj_map;
 
3517
        s->dependent|= embedding->dep_tables;
 
3518
        embedding= embedding->embedding;
 
3519
        outer_join|= nested_join->used_tables;
 
3520
      }
 
3521
      while (embedding);
 
3522
      continue;
 
3523
    }
 
3524
    if ((table->file->stats.records <= 1) &&
 
3525
        !s->dependent &&
 
3526
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3527
    {
 
3528
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3529
    }
 
3530
  }
 
3531
  stat_vector[i]=0;
 
3532
  join->outer_join=outer_join;
 
3533
 
 
3534
  if (join->outer_join)
 
3535
  {
 
3536
    /*
 
3537
       Build transitive closure for relation 'to be dependent on'.
 
3538
       This will speed up the plan search for many cases with outer joins,
 
3539
       as well as allow us to catch illegal cross references/
 
3540
       Warshall's algorithm is used to build the transitive closure.
 
3541
       As we use bitmaps to represent the relation the complexity
 
3542
       of the algorithm is O((number of tables)^2).
 
3543
    */
 
3544
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3545
    {
 
3546
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3547
      {
 
3548
        table= stat[j].table;
 
3549
        if (s->dependent & table->map)
 
3550
          s->dependent |= table->reginfo.join_tab->dependent;
 
3551
      }
 
3552
      if (s->dependent)
 
3553
        s->table->maybe_null= 1;
 
3554
    }
 
3555
    /* Catch illegal cross references for outer joins */
 
3556
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3557
    {
 
3558
      if (s->dependent & s->table->map)
 
3559
      {
 
3560
        join->tables=0;                 // Don't use join->table
 
3561
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3562
        return(1);
 
3563
      }
 
3564
      s->key_dependent= s->dependent;
 
3565
    }
 
3566
  }
 
3567
 
 
3568
  if (conds || outer_join)
 
3569
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
 
3570
                            conds, join->cond_equal,
 
3571
                            ~outer_join, join->select_lex, &sargables))
 
3572
      return(1);
 
3573
 
 
3574
  /* Read tables with 0 or 1 rows (system tables) */
 
3575
  join->const_table_map= 0;
 
3576
 
 
3577
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3578
       p_pos < p_end ;
 
3579
       p_pos++)
 
3580
  {
 
3581
    int tmp;
 
3582
    s= p_pos->table;
 
3583
    s->type=JT_SYSTEM;
 
3584
    join->const_table_map|=s->table->map;
 
3585
    if ((tmp=join_read_const_table(s, p_pos)))
 
3586
    {
 
3587
      if (tmp > 0)
 
3588
        return(1);                      // Fatal error
 
3589
    }
 
3590
    else
 
3591
      found_const_table_map|= s->table->map;
 
3592
  }
 
3593
 
 
3594
  /* loop until no more const tables are found */
 
3595
  int ref_changed;
 
3596
  do
 
3597
  {
 
3598
  more_const_tables_found:
 
3599
    ref_changed = 0;
 
3600
    found_ref=0;
 
3601
 
 
3602
    /*
 
3603
      We only have to loop from stat_vector + const_count as
 
3604
      set_position() will move all const_tables first in stat_vector
 
3605
    */
 
3606
 
 
3607
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3608
    {
 
3609
      table=s->table;
 
3610
 
 
3611
      /*
 
3612
        If equi-join condition by a key is null rejecting and after a
 
3613
        substitution of a const table the key value happens to be null
 
3614
        then we can state that there are no matches for this equi-join.
 
3615
      */
 
3616
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3617
      {
 
3618
        /*
 
3619
          When performing an outer join operation if there are no matching rows
 
3620
          for the single row of the outer table all the inner tables are to be
 
3621
          null complemented and thus considered as constant tables.
 
3622
          Here we apply this consideration to the case of outer join operations
 
3623
          with a single inner table only because the case with nested tables
 
3624
          would require a more thorough analysis.
 
3625
          TODO. Apply single row substitution to null complemented inner tables
 
3626
          for nested outer join operations.
 
3627
        */
 
3628
        while (keyuse->table == table)
 
3629
        {
 
3630
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3631
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3632
          {
 
3633
            s->type= JT_CONST;
 
3634
            mark_as_null_row(table);
 
3635
            found_const_table_map|= table->map;
 
3636
            join->const_table_map|= table->map;
 
3637
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3638
            goto more_const_tables_found;
 
3639
           }
 
3640
          keyuse++;
 
3641
        }
 
3642
      }
 
3643
 
 
3644
      if (s->dependent)                         // If dependent on some table
 
3645
      {
 
3646
        // All dep. must be constants
 
3647
        if (s->dependent & ~(found_const_table_map))
 
3648
          continue;
 
3649
        if (table->file->stats.records <= 1L &&
 
3650
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3651
            !table->pos_in_table_list->embedding)
 
3652
        {                                       // system table
 
3653
          int tmp= 0;
 
3654
          s->type=JT_SYSTEM;
 
3655
          join->const_table_map|=table->map;
 
3656
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3657
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3658
          {
 
3659
            if (tmp > 0)
 
3660
              return(1);                        // Fatal error
 
3661
          }
 
3662
          else
 
3663
            found_const_table_map|= table->map;
 
3664
          continue;
 
3665
        }
 
3666
      }
 
3667
      /* check if table can be read by key or table only uses const refs */
 
3668
      if ((keyuse=s->keyuse))
 
3669
      {
 
3670
        s->type= JT_REF;
 
3671
        while (keyuse->table == table)
 
3672
        {
 
3673
          start_keyuse=keyuse;
 
3674
          key=keyuse->key;
 
3675
          s->keys.set_bit(key);               // QQ: remove this ?
 
3676
 
 
3677
          refs=0;
 
3678
          const_ref.clear_all();
 
3679
          eq_part.clear_all();
 
3680
          do
 
3681
          {
 
3682
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3683
            {
 
3684
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3685
                const_ref.set_bit(keyuse->keypart);
 
3686
              else
 
3687
                refs|=keyuse->used_tables;
 
3688
              eq_part.set_bit(keyuse->keypart);
 
3689
            }
 
3690
            keyuse++;
 
3691
          } while (keyuse->table == table && keyuse->key == key);
 
3692
 
 
3693
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3694
              !table->pos_in_table_list->embedding)
 
3695
          {
 
3696
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3697
                 == HA_NOSAME)
 
3698
            {
 
3699
              if (const_ref == eq_part)
 
3700
              {                                 // Found everything for ref.
 
3701
                int tmp;
 
3702
                ref_changed = 1;
 
3703
                s->type= JT_CONST;
 
3704
                join->const_table_map|=table->map;
 
3705
                set_position(join,const_count++,s,start_keyuse);
 
3706
                if (create_ref_for_key(join, s, start_keyuse,
 
3707
                                       found_const_table_map))
 
3708
                  return(1);
 
3709
                if ((tmp=join_read_const_table(s,
 
3710
                                               join->positions+const_count-1)))
 
3711
                {
 
3712
                  if (tmp > 0)
 
3713
                    return(1);                  // Fatal error
 
3714
                }
 
3715
                else
 
3716
                  found_const_table_map|= table->map;
 
3717
                break;
 
3718
              }
 
3719
              else
 
3720
                found_ref|= refs;      // Table is const if all refs are const
 
3721
            }
 
3722
            else if (const_ref == eq_part)
 
3723
              s->const_keys.set_bit(key);
 
3724
          }
 
3725
        }
 
3726
      }
 
3727
    }
 
3728
  } while (join->const_table_map & found_ref && ref_changed);
 
3729
 
 
3730
  /*
 
3731
    Update info on indexes that can be used for search lookups as
 
3732
    reading const tables may has added new sargable predicates.
 
3733
  */
 
3734
  if (const_count && sargables)
 
3735
  {
 
3736
    for( ; sargables->field ; sargables++)
 
3737
    {
 
3738
      Field *field= sargables->field;
 
3739
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3740
      key_map possible_keys= field->key_start;
 
3741
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3742
      bool is_const= 1;
 
3743
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3744
        is_const&= sargables->arg_value[j]->const_item();
 
3745
      if (is_const)
 
3746
        join_tab[0].const_keys.merge(possible_keys);
 
3747
    }
 
3748
  }
 
3749
 
 
3750
  if (pull_out_semijoin_tables(join))
 
3751
    return(true);
 
3752
 
 
3753
  /* Calc how many (possible) matched records in each table */
 
3754
 
 
3755
  for (s=stat ; s < stat_end ; s++)
 
3756
  {
 
3757
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3758
    {
 
3759
      /* Only one matching row */
 
3760
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3761
      continue;
 
3762
    }
 
3763
    /* Approximate found rows and time to read them */
 
3764
    s->found_records=s->records=s->table->file->stats.records;
 
3765
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3766
 
 
3767
    /*
 
3768
      Set a max range of how many seeks we can expect when using keys
 
3769
      This is can't be to high as otherwise we are likely to use
 
3770
      table scan.
 
3771
    */
 
3772
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3773
                        (double) s->read_time*3);
 
3774
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3775
      s->worst_seeks=2.0;
 
3776
 
 
3777
    /*
 
3778
      Add to stat->const_keys those indexes for which all group fields or
 
3779
      all select distinct fields participate in one index.
 
3780
    */
 
3781
    add_group_and_distinct_keys(join, s);
 
3782
 
 
3783
    if (!s->const_keys.is_clear_all() &&
 
3784
        !s->table->pos_in_table_list->embedding)
 
3785
    {
 
3786
      ha_rows records;
 
3787
      SQL_SELECT *select;
 
3788
      select= make_select(s->table, found_const_table_map,
 
3789
                          found_const_table_map,
 
3790
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3791
                          1, &error);
 
3792
      if (!select)
 
3793
        return(1);
 
3794
      records= get_quick_record_count(join->session, select, s->table,
 
3795
                                      &s->const_keys, join->row_limit);
 
3796
      s->quick=select->quick;
 
3797
      s->needed_reg=select->needed_reg;
 
3798
      select->quick=0;
 
3799
      if (records == 0 && s->table->reginfo.impossible_range)
 
3800
      {
 
3801
        /*
 
3802
          Impossible WHERE or ON expression
 
3803
          In case of ON, we mark that the we match one empty NULL row.
 
3804
          In case of WHERE, don't set found_const_table_map to get the
 
3805
          caller to abort with a zero row result.
 
3806
        */
 
3807
        join->const_table_map|= s->table->map;
 
3808
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3809
        s->type= JT_CONST;
 
3810
        if (*s->on_expr_ref)
 
3811
        {
 
3812
          /* Generate empty row */
 
3813
          s->info= "Impossible ON condition";
 
3814
          found_const_table_map|= s->table->map;
 
3815
          s->type= JT_CONST;
 
3816
          mark_as_null_row(s->table);           // All fields are NULL
 
3817
        }
 
3818
      }
 
3819
      if (records != HA_POS_ERROR)
 
3820
      {
 
3821
        s->found_records=records;
 
3822
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3823
      }
 
3824
      delete select;
 
3825
    }
 
3826
  }
 
3827
 
 
3828
  join->join_tab=stat;
 
3829
  join->map2table=stat_ref;
 
3830
  join->table= join->all_tables=table_vector;
 
3831
  join->const_tables=const_count;
 
3832
  join->found_const_table_map=found_const_table_map;
 
3833
 
 
3834
  /* Find an optimal join order of the non-constant tables. */
 
3835
  if (join->const_tables != join->tables)
 
3836
  {
 
3837
    optimize_keyuse(join, keyuse_array);
 
3838
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3839
      return(true);
 
3840
  }
 
3841
  else
 
3842
  {
 
3843
    memcpy(join->best_positions, join->positions,
 
3844
           sizeof(POSITION)*join->const_tables);
 
3845
    join->best_read=1.0;
 
3846
  }
 
3847
  /* Generate an execution plan from the found optimal join order. */
 
3848
  return(join->session->killed || get_best_combination(join));
 
3849
}
 
3850
 
 
3851
 
485
3852
/*****************************************************************************
486
3853
  Check with keys are used and with tables references with tables
487
3854
  Updates in stat:
490
3857
          keyuse     Pointer to possible keys
491
3858
*****************************************************************************/
492
3859
 
 
3860
/// Used when finding key fields
 
3861
typedef struct key_field_t {
 
3862
  Field         *field;
 
3863
  Item          *val;                   ///< May be empty if diff constant
 
3864
  uint          level;
 
3865
  uint          optimize; // KEY_OPTIMIZE_*
 
3866
  bool          eq_func;
 
3867
  /**
 
3868
    If true, the condition this struct represents will not be satisfied
 
3869
    when val IS NULL.
 
3870
  */
 
3871
  bool          null_rejecting;
 
3872
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3873
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3874
} KEY_FIELD;
 
3875
 
 
3876
/**
 
3877
  Merge new key definitions to old ones, remove those not used in both.
 
3878
 
 
3879
  This is called for OR between different levels.
 
3880
 
 
3881
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3882
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3883
  that are internally transformed to something like:.
 
3884
 
 
3885
  @code
 
3886
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
 
3887
  @endcode
 
3888
 
 
3889
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3890
  result has null_rejecting=true if it is set for both ORed references.
 
3891
  for example:
 
3892
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3893
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3894
 
 
3895
  @todo
 
3896
    The result of this is that we're missing some 'ref' accesses.
 
3897
    OptimizerTeam: Fix this
 
3898
*/
 
3899
 
 
3900
static KEY_FIELD *
 
3901
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3902
                 uint32_t and_level)
 
3903
{
 
3904
  if (start == new_fields)
 
3905
    return start;                               // Impossible or
 
3906
  if (new_fields == end)
 
3907
    return start;                               // No new fields, skip all
 
3908
 
 
3909
  KEY_FIELD *first_free=new_fields;
 
3910
 
 
3911
  /* Mark all found fields in old array */
 
3912
  for (; new_fields != end ; new_fields++)
 
3913
  {
 
3914
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3915
    {
 
3916
      if (old->field == new_fields->field)
 
3917
      {
 
3918
        /*
 
3919
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3920
          it can return false where it is feasible to make it return true.
 
3921
 
 
3922
          The cause is as follows: Some of the tables are already known to be
 
3923
          const tables (the detection code is in make_join_statistics(),
 
3924
          above the update_ref_and_keys() call), but we didn't propagate
 
3925
          information about this: Table::const_table is not set to true, and
 
3926
          Item::update_used_tables() hasn't been called for each item.
 
3927
          The result of this is that we're missing some 'ref' accesses.
 
3928
          TODO: OptimizerTeam: Fix this
 
3929
        */
 
3930
        if (!new_fields->val->const_item())
 
3931
        {
 
3932
          /*
 
3933
            If the value matches, we can use the key reference.
 
3934
            If not, we keep it until we have examined all new values
 
3935
          */
 
3936
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3937
          {
 
3938
            old->level= and_level;
 
3939
            old->optimize= ((old->optimize & new_fields->optimize &
 
3940
                             KEY_OPTIMIZE_EXISTS) |
 
3941
                            ((old->optimize | new_fields->optimize) &
 
3942
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3943
            old->null_rejecting= (old->null_rejecting &&
 
3944
                                  new_fields->null_rejecting);
 
3945
          }
 
3946
        }
 
3947
        else if (old->eq_func && new_fields->eq_func &&
 
3948
                 old->val->eq_by_collation(new_fields->val,
 
3949
                                           old->field->binary(),
 
3950
                                           old->field->charset()))
 
3951
 
 
3952
        {
 
3953
          old->level= and_level;
 
3954
          old->optimize= ((old->optimize & new_fields->optimize &
 
3955
                           KEY_OPTIMIZE_EXISTS) |
 
3956
                          ((old->optimize | new_fields->optimize) &
 
3957
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3958
          old->null_rejecting= (old->null_rejecting &&
 
3959
                                new_fields->null_rejecting);
 
3960
        }
 
3961
        else if (old->eq_func && new_fields->eq_func &&
 
3962
                 ((old->val->const_item() && old->val->is_null()) ||
 
3963
                  new_fields->val->is_null()))
 
3964
        {
 
3965
          /* field = expression OR field IS NULL */
 
3966
          old->level= and_level;
 
3967
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3968
          /*
 
3969
            Remember the NOT NULL value unless the value does not depend
 
3970
            on other tables.
 
3971
          */
 
3972
          if (!old->val->used_tables() && old->val->is_null())
 
3973
            old->val= new_fields->val;
 
3974
          /* The referred expression can be NULL: */
 
3975
          old->null_rejecting= 0;
 
3976
        }
 
3977
        else
 
3978
        {
 
3979
          /*
 
3980
            We are comparing two different const.  In this case we can't
 
3981
            use a key-lookup on this so it's better to remove the value
 
3982
            and let the range optimzier handle it
 
3983
          */
 
3984
          if (old == --first_free)              // If last item
 
3985
            break;
 
3986
          *old= *first_free;                    // Remove old value
 
3987
          old--;                                // Retry this value
 
3988
        }
 
3989
      }
 
3990
    }
 
3991
  }
 
3992
  /* Remove all not used items */
 
3993
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3994
  {
 
3995
    if (old->level != and_level)
 
3996
    {                                           // Not used in all levels
 
3997
      if (old == --first_free)
 
3998
        break;
 
3999
      *old= *first_free;                        // Remove old value
 
4000
      continue;
 
4001
    }
 
4002
    old++;
 
4003
  }
 
4004
  return first_free;
 
4005
}
 
4006
 
 
4007
 
 
4008
/**
 
4009
  Add a possible key to array of possible keys if it's usable as a key
 
4010
 
 
4011
    @param key_fields      Pointer to add key, if usable
 
4012
    @param and_level       And level, to be stored in KEY_FIELD
 
4013
    @param cond            Condition predicate
 
4014
    @param field           Field used in comparision
 
4015
    @param eq_func         True if we used =, <=> or IS NULL
 
4016
    @param value           Value used for comparison with field
 
4017
    @param usable_tables   Tables which can be used for key optimization
 
4018
    @param sargables       IN/OUT Array of found sargable candidates
 
4019
 
 
4020
  @note
 
4021
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4022
    table, we store this to be able to do not exists optimization later.
 
4023
 
 
4024
  @returns
 
4025
    *key_fields is incremented if we stored a key in the array
 
4026
*/
 
4027
 
 
4028
static void
 
4029
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
 
4030
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4031
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4032
{
 
4033
  uint32_t exists_optimize= 0;
 
4034
  if (!(field->flags & PART_KEY_FLAG))
 
4035
  {
 
4036
    // Don't remove column IS NULL on a LEFT JOIN table
 
4037
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4038
        !field->table->maybe_null || field->null_ptr)
 
4039
      return;                                   // Not a key. Skip it
 
4040
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4041
    assert(num_values == 1);
 
4042
  }
 
4043
  else
 
4044
  {
 
4045
    table_map used_tables=0;
 
4046
    bool optimizable=0;
 
4047
    for (uint32_t i=0; i<num_values; i++)
 
4048
    {
 
4049
      used_tables|=(value[i])->used_tables();
 
4050
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4051
        optimizable=1;
 
4052
    }
 
4053
    if (!optimizable)
 
4054
      return;
 
4055
    if (!(usable_tables & field->table->map))
 
4056
    {
 
4057
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4058
          !field->table->maybe_null || field->null_ptr)
 
4059
        return;                                 // Can't use left join optimize
 
4060
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4061
    }
 
4062
    else
 
4063
    {
 
4064
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4065
      key_map possible_keys=field->key_start;
 
4066
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4067
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4068
 
 
4069
      /*
 
4070
        Save the following cases:
 
4071
        Field op constant
 
4072
        Field LIKE constant where constant doesn't start with a wildcard
 
4073
        Field = field2 where field2 is in a different table
 
4074
        Field op formula
 
4075
        Field IS NULL
 
4076
        Field IS NOT NULL
 
4077
         Field BETWEEN ...
 
4078
         Field IN ...
 
4079
      */
 
4080
      stat[0].key_dependent|=used_tables;
 
4081
 
 
4082
      bool is_const=1;
 
4083
      for (uint32_t i=0; i<num_values; i++)
 
4084
      {
 
4085
        if (!(is_const&= value[i]->const_item()))
 
4086
          break;
 
4087
      }
 
4088
      if (is_const)
 
4089
        stat[0].const_keys.merge(possible_keys);
 
4090
      else if (!eq_func)
 
4091
      {
 
4092
        /*
 
4093
          Save info to be able check whether this predicate can be
 
4094
          considered as sargable for range analisis after reading const tables.
 
4095
          We do not save info about equalities as update_const_equal_items
 
4096
          will take care of updating info on keys from sargable equalities.
 
4097
        */
 
4098
        (*sargables)--;
 
4099
        (*sargables)->field= field;
 
4100
        (*sargables)->arg_value= value;
 
4101
        (*sargables)->num_values= num_values;
 
4102
      }
 
4103
      /*
 
4104
        We can't always use indexes when comparing a string index to a
 
4105
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4106
        eq_func is NEVER true when num_values > 1
 
4107
       */
 
4108
      if (!eq_func)
 
4109
      {
 
4110
        /*
 
4111
          Additional optimization: if we're processing
 
4112
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4113
          "t.key = c1".
 
4114
          TODO: This is a very limited fix. A more generic fix is possible.
 
4115
          There are 2 options:
 
4116
          A) Make equality propagation code be able to handle BETWEEN
 
4117
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4118
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4119
             and use them in equality propagation process (see details in
 
4120
             OptimizerKBAndTodo)
 
4121
        */
 
4122
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4123
            ((Item_func_between*) cond)->negated ||
 
4124
            !value[0]->eq(value[1], field->binary()))
 
4125
          return;
 
4126
        eq_func= true;
 
4127
      }
 
4128
 
 
4129
      if (field->result_type() == STRING_RESULT)
 
4130
      {
 
4131
        if ((*value)->result_type() != STRING_RESULT)
 
4132
        {
 
4133
          if (field->cmp_type() != (*value)->result_type())
 
4134
            return;
 
4135
        }
 
4136
        else
 
4137
        {
 
4138
          /*
 
4139
            We can't use indexes if the effective collation
 
4140
            of the operation differ from the field collation.
 
4141
          */
 
4142
          if (field->cmp_type() == STRING_RESULT &&
 
4143
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4144
            return;
 
4145
        }
 
4146
      }
 
4147
    }
 
4148
  }
 
4149
  /*
 
4150
    For the moment eq_func is always true. This slot is reserved for future
 
4151
    extensions where we want to remembers other things than just eq comparisons
 
4152
  */
 
4153
  assert(eq_func);
 
4154
  /* Store possible eq field */
 
4155
  (*key_fields)->field=         field;
 
4156
  (*key_fields)->eq_func=       eq_func;
 
4157
  (*key_fields)->val=           *value;
 
4158
  (*key_fields)->level=         and_level;
 
4159
  (*key_fields)->optimize=      exists_optimize;
 
4160
  /*
 
4161
    If the condition has form "tbl.keypart = othertbl.field" and
 
4162
    othertbl.field can be NULL, there will be no matches if othertbl.field
 
4163
    has NULL value.
 
4164
    We use null_rejecting in add_not_null_conds() to add
 
4165
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4166
  */
 
4167
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4168
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4169
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4170
                                  ((Item_field*)*value)->field->maybe_null());
 
4171
  (*key_fields)->cond_guard= NULL;
 
4172
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
 
4173
                              cond->name < subq_sj_cond_name + 64)?
 
4174
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4175
  (*key_fields)++;
 
4176
}
 
4177
 
 
4178
/**
 
4179
  Add possible keys to array of possible keys originated from a simple
 
4180
  predicate.
 
4181
 
 
4182
    @param  key_fields     Pointer to add key, if usable
 
4183
    @param  and_level      And level, to be stored in KEY_FIELD
 
4184
    @param  cond           Condition predicate
 
4185
    @param  field          Field used in comparision
 
4186
    @param  eq_func        True if we used =, <=> or IS NULL
 
4187
    @param  value          Value used for comparison with field
 
4188
                           Is NULL for BETWEEN and IN
 
4189
    @param  usable_tables  Tables which can be used for key optimization
 
4190
    @param  sargables      IN/OUT Array of found sargable candidates
 
4191
 
 
4192
  @note
 
4193
    If field items f1 and f2 belong to the same multiple equality and
 
4194
    a key is added for f1, the the same key is added for f2.
 
4195
 
 
4196
  @returns
 
4197
    *key_fields is incremented if we stored a key in the array
 
4198
*/
 
4199
 
 
4200
static void
 
4201
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4202
                     Item_func *cond, Item_field *field_item,
 
4203
                     bool eq_func, Item **val,
 
4204
                     uint32_t num_values, table_map usable_tables,
 
4205
                     SARGABLE_PARAM **sargables)
 
4206
{
 
4207
  Field *field= field_item->field;
 
4208
  add_key_field(key_fields, and_level, cond, field,
 
4209
                eq_func, val, num_values, usable_tables, sargables);
 
4210
  Item_equal *item_equal= field_item->item_equal;
 
4211
  if (item_equal)
 
4212
  {
 
4213
    /*
 
4214
      Add to the set of possible key values every substitution of
 
4215
      the field for an equal field included into item_equal
 
4216
    */
 
4217
    Item_equal_iterator it(*item_equal);
 
4218
    Item_field *item;
 
4219
    while ((item= it++))
 
4220
    {
 
4221
      if (!field->eq(item->field))
 
4222
      {
 
4223
        add_key_field(key_fields, and_level, cond, item->field,
 
4224
                      eq_func, val, num_values, usable_tables,
 
4225
                      sargables);
 
4226
      }
 
4227
    }
 
4228
  }
 
4229
}
 
4230
 
 
4231
static void
 
4232
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4233
               COND *cond, table_map usable_tables,
 
4234
               SARGABLE_PARAM **sargables)
 
4235
{
 
4236
  if (cond->type() == Item_func::COND_ITEM)
 
4237
  {
 
4238
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4239
    KEY_FIELD *org_key_fields= *key_fields;
 
4240
 
 
4241
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4242
    {
 
4243
      Item *item;
 
4244
      while ((item=li++))
 
4245
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4246
                       sargables);
 
4247
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4248
        org_key_fields->level= *and_level;
 
4249
    }
 
4250
    else
 
4251
    {
 
4252
      (*and_level)++;
 
4253
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4254
                     sargables);
 
4255
      Item *item;
 
4256
      while ((item=li++))
 
4257
      {
 
4258
        KEY_FIELD *start_key_fields= *key_fields;
 
4259
        (*and_level)++;
 
4260
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4261
                       sargables);
 
4262
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4263
                                     *key_fields,++(*and_level));
 
4264
      }
 
4265
    }
 
4266
    return;
 
4267
  }
 
4268
 
 
4269
  /*
 
4270
    Subquery optimization: Conditions that are pushed down into subqueries
 
4271
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4272
    but need to set cond_guard for KEYUSE elements generated from it.
 
4273
  */
 
4274
  {
 
4275
    if (cond->type() == Item::FUNC_ITEM &&
 
4276
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4277
    {
 
4278
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4279
      if (!join->group_list && !join->order &&
 
4280
          join->unit->item &&
 
4281
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4282
          !join->unit->is_union())
 
4283
      {
 
4284
        KEY_FIELD *save= *key_fields;
 
4285
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4286
                       sargables);
 
4287
        // Indicate that this ref access candidate is for subquery lookup:
 
4288
        for (; save != *key_fields; save++)
 
4289
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4290
      }
 
4291
      return;
 
4292
    }
 
4293
  }
 
4294
 
 
4295
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4296
  if (cond->type() != Item::FUNC_ITEM)
 
4297
    return;
 
4298
  Item_func *cond_func= (Item_func*) cond;
 
4299
  switch (cond_func->select_optimize()) {
 
4300
  case Item_func::OPTIMIZE_NONE:
 
4301
    break;
 
4302
  case Item_func::OPTIMIZE_KEY:
 
4303
  {
 
4304
    Item **values;
 
4305
    // BETWEEN, IN, NE
 
4306
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4307
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4308
    {
 
4309
      values= cond_func->arguments()+1;
 
4310
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4311
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4312
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4313
        values--;
 
4314
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4315
                  cond_func->argument_count() != 2);
 
4316
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4317
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4318
                           0, values,
 
4319
                           cond_func->argument_count()-1,
 
4320
                           usable_tables, sargables);
 
4321
    }
 
4322
    if (cond_func->functype() == Item_func::BETWEEN)
 
4323
    {
 
4324
      values= cond_func->arguments();
 
4325
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4326
      {
 
4327
        Item_field *field_item;
 
4328
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4329
            &&
 
4330
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4331
        {
 
4332
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4333
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4334
                               field_item, 0, values, 1, usable_tables,
 
4335
                               sargables);
 
4336
        }
 
4337
      }
 
4338
    }
 
4339
    break;
 
4340
  }
 
4341
  case Item_func::OPTIMIZE_OP:
 
4342
  {
 
4343
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4344
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4345
 
 
4346
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4347
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4348
    {
 
4349
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4350
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4351
                           equal_func,
 
4352
                           cond_func->arguments()+1, 1, usable_tables,
 
4353
                           sargables);
 
4354
    }
 
4355
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4356
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4357
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4358
    {
 
4359
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4360
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4361
                           equal_func,
 
4362
                           cond_func->arguments(),1,usable_tables,
 
4363
                           sargables);
 
4364
    }
 
4365
    break;
 
4366
  }
 
4367
  case Item_func::OPTIMIZE_NULL:
 
4368
    /* column_name IS [NOT] NULL */
 
4369
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4370
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4371
    {
 
4372
      Item *tmp=new Item_null;
 
4373
      if (unlikely(!tmp))                       // Should never be true
 
4374
        return;
 
4375
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4376
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4377
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4378
                           &tmp, 1, usable_tables, sargables);
 
4379
    }
 
4380
    break;
 
4381
  case Item_func::OPTIMIZE_EQUAL:
 
4382
    Item_equal *item_equal= (Item_equal *) cond;
 
4383
    Item *const_item= item_equal->get_const();
 
4384
    Item_equal_iterator it(*item_equal);
 
4385
    Item_field *item;
 
4386
    if (const_item)
 
4387
    {
 
4388
      /*
 
4389
        For each field field1 from item_equal consider the equality
 
4390
        field1=const_item as a condition allowing an index access of the table
 
4391
        with field1 by the keys value of field1.
 
4392
      */
 
4393
      while ((item= it++))
 
4394
      {
 
4395
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4396
                      true, &const_item, 1, usable_tables, sargables);
 
4397
      }
 
4398
    }
 
4399
    else
 
4400
    {
 
4401
      /*
 
4402
        Consider all pairs of different fields included into item_equal.
 
4403
        For each of them (field1, field1) consider the equality
 
4404
        field1=field2 as a condition allowing an index access of the table
 
4405
        with field1 by the keys value of field2.
 
4406
      */
 
4407
      Item_equal_iterator fi(*item_equal);
 
4408
      while ((item= fi++))
 
4409
      {
 
4410
        Field *field= item->field;
 
4411
        while ((item= it++))
 
4412
        {
 
4413
          if (!field->eq(item->field))
 
4414
          {
 
4415
            add_key_field(key_fields, *and_level, cond_func, field,
 
4416
                          true, (Item **) &item, 1, usable_tables,
 
4417
                          sargables);
 
4418
          }
 
4419
        }
 
4420
        it.rewind();
 
4421
      }
 
4422
    }
 
4423
    break;
 
4424
  }
 
4425
}
493
4426
 
494
4427
/**
495
4428
  Add all keys with uses 'field' for some keypart.
496
4429
 
497
4430
  If field->and_level != and_level then only mark key_part as const_part.
498
4431
*/
499
 
uint32_t max_part_bit(key_part_map bits)
 
4432
 
 
4433
static uint
 
4434
max_part_bit(key_part_map bits)
500
4435
{
501
4436
  uint32_t found;
502
4437
  for (found=0; bits & 1 ; found++,bits>>=1) ;
503
4438
  return found;
504
4439
}
505
4440
 
506
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4441
static void
 
4442
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4443
{
 
4444
  Field *field=key_field->field;
 
4445
  Table *form= field->table;
 
4446
  KEYUSE keyuse;
 
4447
 
 
4448
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4449
  {
 
4450
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4451
    {
 
4452
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4453
        continue;
 
4454
 
 
4455
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
 
4456
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4457
      {
 
4458
        if (field->eq(form->key_info[key].key_part[part].field))
 
4459
        {
 
4460
          keyuse.table= field->table;
 
4461
          keyuse.val =  key_field->val;
 
4462
          keyuse.key =  key;
 
4463
          keyuse.keypart=part;
 
4464
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4465
          keyuse.used_tables=key_field->val->used_tables();
 
4466
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4467
          keyuse.null_rejecting= key_field->null_rejecting;
 
4468
          keyuse.cond_guard= key_field->cond_guard;
 
4469
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4470
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4471
        }
 
4472
      }
 
4473
    }
 
4474
  }
 
4475
}
 
4476
 
 
4477
static int
 
4478
sort_keyuse(KEYUSE *a,KEYUSE *b)
507
4479
{
508
4480
  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()));
 
4481
  if (a->table->tablenr != b->table->tablenr)
 
4482
    return (int) (a->table->tablenr - b->table->tablenr);
 
4483
  if (a->key != b->key)
 
4484
    return (int) (a->key - b->key);
 
4485
  if (a->keypart != b->keypart)
 
4486
    return (int) (a->keypart - b->keypart);
515
4487
  // Place const values before other ones
516
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4488
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4489
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
4490
    return res;
519
4491
  /* 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)));
 
4492
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4493
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4494
}
 
4495
 
 
4496
 
 
4497
/*
 
4498
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4499
 
 
4500
    This function populates KEY_FIELD array with entries generated from the
 
4501
    ON condition of the given nested join, and does the same for nested joins
 
4502
    contained within this nested join.
 
4503
 
 
4504
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4505
  @param[in,out]  end                 End of the key field array
 
4506
  @param[in,out]  and_level           And-level
 
4507
  @param[in,out]  sargables           Array of found sargable candidates
 
4508
 
 
4509
 
 
4510
  @note
 
4511
    We can add accesses to the tables that are direct children of this nested
 
4512
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4513
 
 
4514
    Example for #1 (outer brackets pair denotes nested join this function is
 
4515
    invoked for):
 
4516
    @code
 
4517
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4518
    @endcode
 
4519
    Example for #2:
 
4520
    @code
 
4521
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4522
    @endcode
 
4523
    In examples 1-2 for condition cond, we can add 'ref' access candidates to
 
4524
    t1 only.
 
4525
    Example #3:
 
4526
    @code
 
4527
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4528
    @endcode
 
4529
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4530
*/
 
4531
 
 
4532
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4533
                                  KEY_FIELD **end, uint32_t *and_level,
 
4534
                                  SARGABLE_PARAM **sargables)
 
4535
{
 
4536
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4537
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4538
  bool have_another = false;
 
4539
  table_map tables= 0;
 
4540
  TableList *table;
 
4541
  assert(nested_join_table->nested_join);
 
4542
 
 
4543
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4544
                                            (table= li++))))
 
4545
  {
 
4546
    if (table->nested_join)
 
4547
    {
 
4548
      if (!table->on_expr)
 
4549
      {
 
4550
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4551
        have_another= true;
 
4552
        li2= li;
 
4553
        li= List_iterator<TableList>(table->nested_join->join_list);
 
4554
      }
 
4555
      else
 
4556
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4557
    }
 
4558
    else
 
4559
      if (!table->on_expr)
 
4560
        tables |= table->table->map;
 
4561
  }
 
4562
  if (nested_join_table->on_expr)
 
4563
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4564
                   sargables);
522
4565
}
523
4566
 
524
4567
 
526
4569
  Update keyuse array with all possible keys we can use to fetch rows.
527
4570
 
528
4571
  @param       session
529
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4572
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
530
4573
  @param       join_tab       Array in tablenr_order
531
4574
  @param       tables         Number of tables in join
532
4575
  @param       cond           WHERE condition (note that the function analyzes
535
4578
                              for which we can make ref access based the WHERE
536
4579
                              clause)
537
4580
  @param       select_lex     current SELECT
538
 
  @param[out]  sargables      std::vector of found sargable candidates
 
4581
  @param[out]  sargables      Array of found sargable candidates
539
4582
 
540
4583
   @retval
541
4584
     0  OK
542
4585
   @retval
543
4586
     1  Out of memory.
544
4587
*/
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)
 
4588
 
 
4589
static bool
 
4590
update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4591
                    uint32_t tables, COND *cond, COND_EQUAL *,
 
4592
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4593
                    SARGABLE_PARAM **sargables)
554
4594
{
555
 
  uint  and_level,found_eq_constant;
556
 
  optimizer::KeyField *key_fields, *end, *field;
 
4595
  uint  and_level,i,found_eq_constant;
 
4596
  KEY_FIELD *key_fields, *end, *field;
557
4597
  uint32_t sz;
558
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
 
4598
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
559
4599
 
560
4600
  /*
561
 
    All predicates that are used to fill arrays of KeyField
562
 
    and SargableParam classes have at most 2 arguments
 
4601
    We use the same piece of memory to store both  KEY_FIELD
 
4602
    and SARGABLE_PARAM structure.
 
4603
    KEY_FIELD values are placed at the beginning this memory
 
4604
    while  SARGABLE_PARAM values are put at the end.
 
4605
    All predicates that are used to fill arrays of KEY_FIELD
 
4606
    and SARGABLE_PARAM structures have at most 2 arguments
563
4607
    except BETWEEN predicates that have 3 arguments and
564
4608
    IN predicates.
565
4609
    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
 
4610
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4611
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
568
4612
    can be filled as this predicate is considered as
569
4613
    saragable with respect to each of its argument.
570
4614
    An IN predicate can require at most 1 element as currently
574
4618
    can be not more than select_lex->max_equal_elems such
575
4619
    substitutions.
576
4620
  */
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;
 
4621
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4622
      (((session->lex->current_select->cond_count+1)*2 +
 
4623
        session->lex->current_select->between_count)*m+1);
 
4624
  if (!(key_fields=(KEY_FIELD*) session->alloc(sz)))
 
4625
    return true; /* purecov: inspected */
582
4626
  and_level= 0;
583
4627
  field= end= key_fields;
 
4628
  *sargables= (SARGABLE_PARAM *) key_fields +
 
4629
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4630
  /* set a barrier for the array of SARGABLE_PARAM */
 
4631
  (*sargables)[0].field= 0;
584
4632
 
585
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4633
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
586
4634
    return true;
587
4635
  if (cond)
588
4636
  {
589
4637
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
590
4638
                   sargables);
591
 
    for (; field != end; field++)
 
4639
    for (; field != end ; field++)
592
4640
    {
593
 
      add_key_part(keyuse, field);
 
4641
      add_key_part(keyuse,field);
594
4642
      /* Mark that we can optimize LEFT JOIN */
595
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
596
 
          ! field->getField()->real_maybe_null())
597
 
      {
598
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
599
 
      }
 
4643
      if (field->val->type() == Item::NULL_ITEM &&
 
4644
          !field->field->real_maybe_null())
 
4645
        field->field->table->reginfo.not_exists_optimize=1;
600
4646
    }
601
4647
  }
602
 
  for (uint32_t i= 0; i < tables; i++)
 
4648
  for (i=0 ; i < tables ; i++)
603
4649
  {
604
4650
    /*
605
4651
      Block the creation of keys for inner tables of outer joins.
618
4664
 
619
4665
  /* Process ON conditions for the nested joins */
620
4666
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
4667
    List_iterator<TableList> li(*join_tab->join->join_list);
622
4668
    TableList *table;
623
4669
    while ((table= li++))
624
4670
    {
625
 
      if (table->getNestedJoin())
 
4671
      if (table->nested_join)
626
4672
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
627
4673
                              sargables);
628
4674
    }
643
4689
  */
644
4690
  if (keyuse->elements)
645
4691
  {
646
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4692
    KEYUSE key_end,*prev,*save_pos,*use;
647
4693
 
648
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
649
 
                       (qsort_cmp) sort_keyuse);
 
4694
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4695
          (qsort_cmp) sort_keyuse);
650
4696
 
651
4697
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
652
4698
    insert_dynamic(keyuse,(unsigned char*) &key_end);
653
4699
 
654
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4700
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
655
4701
    prev= &key_end;
656
 
    found_eq_constant= 0;
 
4702
    found_eq_constant=0;
 
4703
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
657
4704
    {
658
 
      uint32_t i;
659
 
 
660
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4705
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4706
        use->table->const_key_parts[use->key]|= use->keypart_map;
661
4707
      {
662
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
663
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
664
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
665
 
        {
666
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
667
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
668
 
            continue;                           /* remove */
669
 
        }
670
 
        else if (use->getKeypart() != 0)                // First found must be 0
671
 
          continue;
 
4708
        if (use->key == prev->key && use->table == prev->table)
 
4709
        {
 
4710
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4711
            continue;                           /* remove */
 
4712
        }
 
4713
        else if (use->keypart != 0)             // First found must be 0
 
4714
          continue;
 
4715
      }
672
4716
 
673
 
#ifdef HAVE_VALGRIND
674
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
 
        if (save_pos != use)
 
4717
#ifdef HAVE_purify
 
4718
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4719
      if (save_pos != use)
676
4720
#endif
677
 
          *save_pos= *use;
678
 
        prev=use;
679
 
        found_eq_constant= ! use->getUsedTables();
680
 
        /* Save ptr to first use */
681
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
682
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
683
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
684
 
        save_pos++;
685
 
      }
686
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
687
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
688
 
      keyuse->elements= i;
 
4721
        *save_pos= *use;
 
4722
      prev=use;
 
4723
      found_eq_constant= !use->used_tables;
 
4724
      /* Save ptr to first use */
 
4725
      if (!use->table->reginfo.join_tab->keyuse)
 
4726
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4727
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4728
      save_pos++;
689
4729
    }
 
4730
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4731
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4732
    keyuse->elements=i;
690
4733
  }
691
4734
  return false;
692
4735
}
694
4737
/**
695
4738
  Update some values in keyuse for faster choose_plan() loop.
696
4739
*/
697
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4740
 
 
4741
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
698
4742
{
699
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
700
 
                                                  0, 
701
 
                                                  optimizer::KeyUse*);
 
4743
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
702
4744
 
703
4745
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
704
4746
  {
711
4753
      Constant tables are ignored.
712
4754
      To avoid bad matches, we don't make ref_table_rows less than 100.
713
4755
    */
714
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
715
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4756
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4757
    if (keyuse->used_tables &
 
4758
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4759
               ~OUTER_REF_TABLE_BIT)))
716
4760
    {
717
4761
      uint32_t tablenr;
718
4762
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
719
4763
      if (map == 1)                     // Only one table
720
4764
      {
721
 
        Table *tmp_table=join->all_tables[tablenr];
722
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4765
        Table *tmp_table=join->all_tables[tablenr];
 
4766
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
723
4767
      }
724
4768
    }
725
4769
    /*
726
4770
      Outer reference (external field) is constant for single executing
727
4771
      of subquery
728
4772
    */
729
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
730
 
      keyuse->setTableRows(1);
 
4773
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4774
      keyuse->ref_table_rows= 1;
731
4775
  }
732
4776
}
733
4777
 
749
4793
  @return
750
4794
    None
751
4795
*/
752
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4796
 
 
4797
static void
 
4798
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
753
4799
{
754
4800
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
4801
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
4802
  order_st      *cur_group;
757
4803
  Item_field *cur_item;
758
4804
  key_map possible_keys(0);
759
4805
 
766
4812
  else if (join->select_distinct)
767
4813
  { /* Collect all query fields referenced in the SELECT clause. */
768
4814
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
4815
    List_iterator<Item> select_items_it(select_items);
770
4816
    Item *item;
771
4817
    while ((item= select_items_it++))
772
4818
      item->walk(&Item::collect_item_field_processor, 0,
780
4826
 
781
4827
  /* Intersect the keys of all group fields. */
782
4828
  cur_item= indexed_fields_it++;
783
 
  possible_keys|= cur_item->field->part_of_key;
 
4829
  possible_keys.merge(cur_item->field->part_of_key);
784
4830
  while ((cur_item= indexed_fields_it++))
785
4831
  {
786
 
    possible_keys&= cur_item->field->part_of_key;
787
 
  }
788
 
 
789
 
  if (possible_keys.any())
790
 
    join_tab->const_keys|= possible_keys;
791
 
}
792
 
 
793
 
/**
794
 
  Compare two JoinTable objects based on the number of accessed records.
795
 
 
796
 
  @param ptr1 pointer to first JoinTable object
797
 
  @param ptr2 pointer to second JoinTable object
 
4832
    possible_keys.intersect(cur_item->field->part_of_key);
 
4833
  }
 
4834
 
 
4835
  if (!possible_keys.is_clear_all())
 
4836
    join_tab->const_keys.merge(possible_keys);
 
4837
}
 
4838
 
 
4839
 
 
4840
/*****************************************************************************
 
4841
  Go through all combinations of not marked tables and find the one
 
4842
  which uses least records
 
4843
*****************************************************************************/
 
4844
 
 
4845
/** Save const tables first as used tables. */
 
4846
 
 
4847
static void
 
4848
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4849
{
 
4850
  join->positions[idx].table= table;
 
4851
  join->positions[idx].key=key;
 
4852
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4853
  join->positions[idx].ref_depend_map= 0;
 
4854
 
 
4855
  /* Move the const table as down as possible in best_ref */
 
4856
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4857
  JOIN_TAB *next=join->best_ref[idx];
 
4858
  for (;next != table ; pos++)
 
4859
  {
 
4860
    JOIN_TAB *tmp=pos[0];
 
4861
    pos[0]=next;
 
4862
    next=tmp;
 
4863
  }
 
4864
  join->best_ref[idx]=table;
 
4865
}
 
4866
 
 
4867
 
 
4868
/*
 
4869
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4870
 
 
4871
  SYNOPSIS
 
4872
    get_bound_sj_equalities()
 
4873
      sj_nest           Semi-join nest
 
4874
      remaining_tables  Tables that are not yet bound
 
4875
 
 
4876
  DESCRIPTION
 
4877
    Given a semi-join nest, find out which of the IN-equalities have their
 
4878
    left part expression bound (i.e. the said expression doesn't refer to
 
4879
    any of remaining_tables and can be evaluated).
 
4880
 
 
4881
  RETURN
 
4882
    Bitmap of bound IN-equalities.
 
4883
*/
 
4884
 
 
4885
uint64_t get_bound_sj_equalities(TableList *sj_nest,
 
4886
                                  table_map remaining_tables)
 
4887
{
 
4888
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4889
  Item *item;
 
4890
  uint32_t i= 0;
 
4891
  uint64_t res= 0;
 
4892
  while ((item= li++))
 
4893
  {
 
4894
    /*
 
4895
      Q: should this take into account equality propagation and how?
 
4896
      A: If e->outer_side is an Item_field, walk over the equality
 
4897
         class and see if there is an element that is bound?
 
4898
      (this is an optional feature)
 
4899
    */
 
4900
    if (!(item->used_tables() & remaining_tables))
 
4901
    {
 
4902
      res |= 1UL < i;
 
4903
    }
 
4904
  }
 
4905
  return res;
 
4906
}
 
4907
 
 
4908
 
 
4909
/**
 
4910
  Find the best access path for an extension of a partial execution
 
4911
  plan and add this path to the plan.
 
4912
 
 
4913
  The function finds the best access path to table 's' from the passed
 
4914
  partial plan where an access path is the general term for any means to
 
4915
  access the data in 's'. An access path may use either an index or a scan,
 
4916
  whichever is cheaper. The input partial plan is passed via the array
 
4917
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4918
  cost are stored in 'join->positions[idx]'.
 
4919
 
 
4920
  @param join             pointer to the structure providing all context info
 
4921
                          for the query
 
4922
  @param s                the table to be joined by the function
 
4923
  @param session              thread for the connection that submitted the query
 
4924
  @param remaining_tables set of tables not included into the partial plan yet
 
4925
  @param idx              the length of the partial plan
 
4926
  @param record_count     estimate for the number of records returned by the
 
4927
                          partial plan
 
4928
  @param read_time        the cost of the partial plan
 
4929
 
 
4930
  @return
 
4931
    None
 
4932
*/
 
4933
 
 
4934
static void
 
4935
best_access_path(JOIN      *join,
 
4936
                 JOIN_TAB  *s,
 
4937
                 Session       *session,
 
4938
                 table_map remaining_tables,
 
4939
                 uint32_t      idx,
 
4940
                 double    record_count,
 
4941
                 double)
 
4942
{
 
4943
  KEYUSE *best_key=         0;
 
4944
  uint32_t best_max_key_part=   0;
 
4945
  bool found_constraint= 0;
 
4946
  double best=              DBL_MAX;
 
4947
  double best_time=         DBL_MAX;
 
4948
  double records=           DBL_MAX;
 
4949
  table_map best_ref_depends_map= 0;
 
4950
  double tmp;
 
4951
  ha_rows rec;
 
4952
  uint32_t best_is_sj_inside_out=    0;
 
4953
 
 
4954
  if (s->keyuse)
 
4955
  {                                            /* Use key if possible */
 
4956
    Table *table= s->table;
 
4957
    KEYUSE *keyuse,*start_key=0;
 
4958
    double best_records= DBL_MAX;
 
4959
    uint32_t max_key_part=0;
 
4960
    uint64_t bound_sj_equalities= 0;
 
4961
    bool try_sj_inside_out= false;
 
4962
    /*
 
4963
      Discover the bound equalites. We need to do this, if
 
4964
        1. The next table is an SJ-inner table, and
 
4965
        2. It is the first table from that semijoin, and
 
4966
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4967
           all or none of their tables in join_table_map), except
 
4968
           s->emb_sj_nest (which we've just entered).
 
4969
        3. All correlation references from this sj-nest are bound
 
4970
    */
 
4971
    if (s->emb_sj_nest &&                                                 // (1)
 
4972
        s->emb_sj_nest->sj_in_exprs < 64 &&
 
4973
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4974
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4975
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4976
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4977
    {
 
4978
      /* This table is an InsideOut scan candidate */
 
4979
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
 
4980
                                                   remaining_tables);
 
4981
      try_sj_inside_out= true;
 
4982
    }
 
4983
 
 
4984
    /* Test how we can use keys */
 
4985
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4986
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4987
    {
 
4988
      key_part_map found_part= 0;
 
4989
      table_map found_ref= 0;
 
4990
      uint32_t key= keyuse->key;
 
4991
      KEY *keyinfo= table->key_info+key;
 
4992
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4993
      key_part_map const_part= 0;
 
4994
      /* The or-null keypart in ref-or-null access: */
 
4995
      key_part_map ref_or_null_part= 0;
 
4996
 
 
4997
      /* Calculate how many key segments of the current key we can use */
 
4998
      start_key= keyuse;
 
4999
      uint64_t handled_sj_equalities=0;
 
5000
      key_part_map sj_insideout_map= 0;
 
5001
 
 
5002
      do /* For each keypart */
 
5003
      {
 
5004
        uint32_t keypart= keyuse->keypart;
 
5005
        table_map best_part_found_ref= 0;
 
5006
        double best_prev_record_reads= DBL_MAX;
 
5007
 
 
5008
        do /* For each way to access the keypart */
 
5009
        {
 
5010
 
 
5011
          /*
 
5012
            if 1. expression doesn't refer to forward tables
 
5013
               2. we won't get two ref-or-null's
 
5014
          */
 
5015
          if (!(remaining_tables & keyuse->used_tables) &&
 
5016
              !(ref_or_null_part && (keyuse->optimize &
 
5017
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5018
          {
 
5019
            found_part|= keyuse->keypart_map;
 
5020
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5021
              const_part|= keyuse->keypart_map;
 
5022
 
 
5023
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5024
                                                      keyuse->used_tables));
 
5025
            if (tmp2 < best_prev_record_reads)
 
5026
            {
 
5027
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5028
              best_prev_record_reads= tmp2;
 
5029
            }
 
5030
            if (rec > keyuse->ref_table_rows)
 
5031
              rec= keyuse->ref_table_rows;
 
5032
            /*
 
5033
              If there is one 'key_column IS NULL' expression, we can
 
5034
              use this ref_or_null optimisation of this field
 
5035
            */
 
5036
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5037
              ref_or_null_part |= keyuse->keypart_map;
 
5038
          }
 
5039
 
 
5040
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5041
          {
 
5042
            if (!(remaining_tables & keyuse->used_tables))
 
5043
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5044
            else
 
5045
            {
 
5046
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5047
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5048
            }
 
5049
          }
 
5050
 
 
5051
          keyuse++;
 
5052
        } while (keyuse->table == table && keyuse->key == key &&
 
5053
                 keyuse->keypart == keypart);
 
5054
        found_ref|= best_part_found_ref;
 
5055
      } while (keyuse->table == table && keyuse->key == key);
 
5056
 
 
5057
      /*
 
5058
        Assume that that each key matches a proportional part of table.
 
5059
      */
 
5060
      if (!found_part && !handled_sj_equalities)
 
5061
        continue;                               // Nothing usable found
 
5062
 
 
5063
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5064
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5065
 
 
5066
      bool sj_inside_out_scan= false;
 
5067
      {
 
5068
        found_constraint= 1;
 
5069
        /*
 
5070
          Check if InsideOut scan is applicable:
 
5071
          1. All IN-equalities are either "bound" or "handled"
 
5072
          2. Index keyparts are
 
5073
             ...
 
5074
        */
 
5075
        if (try_sj_inside_out &&
 
5076
            table->covering_keys.is_set(key) &&
 
5077
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5078
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5079
        {
 
5080
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5081
          if (n_fixed_parts != keyinfo->key_parts &&
 
5082
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5083
               PREV_BITS(uint, keyinfo->key_parts))
 
5084
          {
 
5085
            /*
 
5086
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5087
              check if all of them are covered.
 
5088
            */
 
5089
            sj_inside_out_scan= true;
 
5090
            if (!n_fixed_parts)
 
5091
            {
 
5092
              /*
 
5093
                It's a confluent ref scan.
 
5094
 
 
5095
                That is, all found KEYUSE elements refer to IN-equalities,
 
5096
                and there is really no ref access because there is no
 
5097
                  t.keypart0 = {bound expression}
 
5098
 
 
5099
                Calculate the cost of complete loose index scan.
 
5100
              */
 
5101
              records= (double)s->table->file->stats.records;
 
5102
 
 
5103
              /* The cost is entire index scan cost (divided by 2) */
 
5104
              best_time= s->table->file->index_only_read_time(key, records);
 
5105
 
 
5106
              /* Now figure how many different keys we will get */
 
5107
              ulong rpc;
 
5108
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5109
                records= records / rpc;
 
5110
              start_key= NULL;
 
5111
            }
 
5112
          }
 
5113
        }
 
5114
 
 
5115
        /*
 
5116
          Check if we found full key
 
5117
        */
 
5118
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5119
            !ref_or_null_part)
 
5120
        {                                         /* use eq key */
 
5121
          max_key_part= UINT32_MAX;
 
5122
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5123
          {
 
5124
            tmp = prev_record_reads(join, idx, found_ref);
 
5125
            records=1.0;
 
5126
          }
 
5127
          else
 
5128
          {
 
5129
            if (!found_ref)
 
5130
            {                                     /* We found a const key */
 
5131
              /*
 
5132
                ReuseRangeEstimateForRef-1:
 
5133
                We get here if we've found a ref(const) (c_i are constants):
 
5134
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5135
 
 
5136
                If range optimizer was able to construct a "range"
 
5137
                access on this index, then its condition "quick_cond" was
 
5138
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5139
                from the range optimizer.
 
5140
 
 
5141
                Proof of (*): By properties of range and ref optimizers
 
5142
                quick_cond will be equal or tighther than ref_const_cond.
 
5143
                ref_const_cond already covers "smallest" possible interval -
 
5144
                a singlepoint interval over all keyparts. Therefore,
 
5145
                quick_cond is equivalent to ref_const_cond (if it was an
 
5146
                empty interval we wouldn't have got here).
 
5147
              */
 
5148
              if (table->quick_keys.is_set(key))
 
5149
                records= (double) table->quick_rows[key];
 
5150
              else
 
5151
              {
 
5152
                /* quick_range couldn't use key! */
 
5153
                records= (double) s->records/rec;
 
5154
              }
 
5155
            }
 
5156
            else
 
5157
            {
 
5158
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5159
              {                                   /* Prefer longer keys */
 
5160
                records=
 
5161
                  ((double) s->records / (double) rec *
 
5162
                   (1.0 +
 
5163
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5164
                     (double) table->s->max_key_length)));
 
5165
                if (records < 2.0)
 
5166
                  records=2.0;               /* Can't be as good as a unique */
 
5167
              }
 
5168
              /*
 
5169
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5170
                E(#rows) from range optimizer. Make another try:
 
5171
 
 
5172
                If range optimizer produced E(#rows) for a prefix of the ref
 
5173
                access we're considering, and that E(#rows) is lower then our
 
5174
                current estimate, make an adjustment. The criteria of when we
 
5175
                can make an adjustment is a special case of the criteria used
 
5176
                in ReuseRangeEstimateForRef-3.
 
5177
              */
 
5178
              if (table->quick_keys.is_set(key) &&
 
5179
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5180
                  table->quick_n_ranges[key] == 1 &&
 
5181
                  records > (double) table->quick_rows[key])
 
5182
              {
 
5183
                records= (double) table->quick_rows[key];
 
5184
              }
 
5185
            }
 
5186
            /* Limit the number of matched rows */
 
5187
            tmp= records;
 
5188
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5189
            if (table->covering_keys.is_set(key))
 
5190
            {
 
5191
              /* we can use only index tree */
 
5192
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5193
            }
 
5194
            else
 
5195
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5196
          }
 
5197
        }
 
5198
        else
 
5199
        {
 
5200
          /*
 
5201
            Use as much key-parts as possible and a uniq key is better
 
5202
            than a not unique key
 
5203
            Set tmp to (previous record count) * (records / combination)
 
5204
          */
 
5205
          if ((found_part & 1) &&
 
5206
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5207
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5208
          {
 
5209
            max_key_part= max_part_bit(found_part);
 
5210
            /*
 
5211
              ReuseRangeEstimateForRef-3:
 
5212
              We're now considering a ref[or_null] access via
 
5213
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
 
5214
              (same-as-above but with one cond replaced
 
5215
               with "t.keypart_i IS NULL")]  (**)
 
5216
 
 
5217
              Try re-using E(#rows) from "range" optimizer:
 
5218
              We can do so if "range" optimizer used the same intervals as
 
5219
              in (**). The intervals used by range optimizer may be not
 
5220
              available at this point (as "range" access might have choosen to
 
5221
              create quick select over another index), so we can't compare
 
5222
              them to (**). We'll make indirect judgements instead.
 
5223
              The sufficient conditions for re-use are:
 
5224
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5225
                   this is not satisfied we have no way to know which ranges
 
5226
                   will be actually scanned by 'ref' until we execute the
 
5227
                   join)
 
5228
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5229
                   is apparently a necessary requirement)
 
5230
 
 
5231
              We also have a property that "range optimizer produces equal or
 
5232
              tighter set of scan intervals than ref(const) optimizer". Each
 
5233
              of the intervals in (**) are "tightest possible" intervals when
 
5234
              one limits itself to using keyparts 1..K (which we do in #2).
 
5235
              From here it follows that range access used either one, or
 
5236
              both of the (I1) and (I2) intervals:
 
5237
 
 
5238
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
 
5239
               (same-as-above but with one cond replaced
 
5240
                with "t.keypart_i IS NULL")               (I2)
 
5241
 
 
5242
              The remaining part is to exclude the situation where range
 
5243
              optimizer used one interval while we're considering
 
5244
              ref-or-null and looking for estimate for two intervals. This
 
5245
              is done by last limitation:
 
5246
 
 
5247
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5248
            */
 
5249
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5250
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5251
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5252
            {
 
5253
              tmp= records= (double) table->quick_rows[key];
 
5254
            }
 
5255
            else
 
5256
            {
 
5257
              /* Check if we have statistic about the distribution */
 
5258
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5259
              {
 
5260
                /*
 
5261
                  Fix for the case where the index statistics is too
 
5262
                  optimistic: If
 
5263
                  (1) We're considering ref(const) and there is quick select
 
5264
                      on the same index,
 
5265
                  (2) and that quick select uses more keyparts (i.e. it will
 
5266
                      scan equal/smaller interval then this ref(const))
 
5267
                  (3) and E(#rows) for quick select is higher then our
 
5268
                      estimate,
 
5269
                  Then
 
5270
                    We'll use E(#rows) from quick select.
 
5271
 
 
5272
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5273
                  cheaper in some cases ?
 
5274
                  TODO: figure this out and adjust the plan choice if needed.
 
5275
                */
 
5276
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5277
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5278
                    records < (double)table->quick_rows[key])         // (3)
 
5279
                  records= (double)table->quick_rows[key];
 
5280
 
 
5281
                tmp= records;
 
5282
              }
 
5283
              else
 
5284
              {
 
5285
                /*
 
5286
                  Assume that the first key part matches 1% of the file
 
5287
                  and that the whole key matches 10 (duplicates) or 1
 
5288
                  (unique) records.
 
5289
                  Assume also that more key matches proportionally more
 
5290
                  records
 
5291
                  This gives the formula:
 
5292
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5293
 
 
5294
                  b = records matched by whole key
 
5295
                  a = records matched by first key part (1% of all records?)
 
5296
                  c = number of key parts in key
 
5297
                  x = used key parts (1 <= x <= c)
 
5298
                */
 
5299
                double rec_per_key;
 
5300
                if (!(rec_per_key=(double)
 
5301
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5302
                  rec_per_key=(double) s->records/rec+1;
 
5303
 
 
5304
                if (!s->records)
 
5305
                  tmp = 0;
 
5306
                else if (rec_per_key/(double) s->records >= 0.01)
 
5307
                  tmp = rec_per_key;
 
5308
                else
 
5309
                {
 
5310
                  double a=s->records*0.01;
 
5311
                  if (keyinfo->key_parts > 1)
 
5312
                    tmp= (max_key_part * (rec_per_key - a) +
 
5313
                          a*keyinfo->key_parts - rec_per_key)/
 
5314
                         (keyinfo->key_parts-1);
 
5315
                  else
 
5316
                    tmp= a;
 
5317
                  set_if_bigger(tmp,1.0);
 
5318
                }
 
5319
                records = (uint32_t) tmp;
 
5320
              }
 
5321
 
 
5322
              if (ref_or_null_part)
 
5323
              {
 
5324
                /* We need to do two key searches to find key */
 
5325
                tmp *= 2.0;
 
5326
                records *= 2.0;
 
5327
              }
 
5328
 
 
5329
              /*
 
5330
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5331
                E(#rows) from range optimizer. Make another try:
 
5332
 
 
5333
                If range optimizer produced E(#rows) for a prefix of the ref
 
5334
                access we're considering, and that E(#rows) is lower then our
 
5335
                current estimate, make the adjustment.
 
5336
 
 
5337
                The decision whether we can re-use the estimate from the range
 
5338
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5339
                applied to first table->quick_key_parts[key] key parts.
 
5340
              */
 
5341
              if (table->quick_keys.is_set(key) &&
 
5342
                  table->quick_key_parts[key] <= max_key_part &&
 
5343
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5344
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
5345
                                                     const_part) ? 1 : 0) &&
 
5346
                  records > (double) table->quick_rows[key])
 
5347
              {
 
5348
                tmp= records= (double) table->quick_rows[key];
 
5349
              }
 
5350
            }
 
5351
 
 
5352
            /* Limit the number of matched rows */
 
5353
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
5354
            if (table->covering_keys.is_set(key))
 
5355
            {
 
5356
              /* we can use only index tree */
 
5357
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5358
            }
 
5359
            else
 
5360
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5361
          }
 
5362
          else
 
5363
            tmp= best_time;                    // Do nothing
 
5364
        }
 
5365
 
 
5366
        if (sj_inside_out_scan && !start_key)
 
5367
        {
 
5368
          tmp= tmp/2;
 
5369
          if (records)
 
5370
            records= records/2;
 
5371
        }
 
5372
 
 
5373
      }
 
5374
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5375
      {
 
5376
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5377
        best= tmp;
 
5378
        best_records= records;
 
5379
        best_key= start_key;
 
5380
        best_max_key_part= max_key_part;
 
5381
        best_ref_depends_map= found_ref;
 
5382
        best_is_sj_inside_out= sj_inside_out_scan;
 
5383
      }
 
5384
    }
 
5385
    records= best_records;
 
5386
  }
 
5387
 
 
5388
  /*
 
5389
    Don't test table scan if it can't be better.
 
5390
    Prefer key lookup if we would use the same key for scanning.
 
5391
 
 
5392
    Don't do a table scan on InnoDB tables, if we can read the used
 
5393
    parts of the row from any of the used index.
 
5394
    This is because table scans uses index and we would not win
 
5395
    anything by using a table scan.
 
5396
 
 
5397
    A word for word translation of the below if-statement in sergefp's
 
5398
    understanding: we check if we should use table scan if:
 
5399
    (1) The found 'ref' access produces more records than a table scan
 
5400
        (or index scan, or quick select), or 'ref' is more expensive than
 
5401
        any of them.
 
5402
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5403
        'range' access using index IDX, and the best way to perform 'ref'
 
5404
        access is to use the same index IDX, with the same or more key parts.
 
5405
        (note: it is not clear how this rule is/should be extended to
 
5406
        index_merge quick selects)
 
5407
    (3) See above note about InnoDB.
 
5408
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5409
             path, but there is no quick select)
 
5410
        If the condition in the above brackets holds, then the only possible
 
5411
        "table scan" access method is ALL/index (there is no quick select).
 
5412
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5413
        choose it over ALL/index, there is no need to consider a full table
 
5414
        scan.
 
5415
  */
 
5416
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5417
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5418
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5419
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5420
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5421
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5422
  {                                             // Check full join
 
5423
    ha_rows rnd_records= s->found_records;
 
5424
    /*
 
5425
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5426
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5427
      preceding this table in the join order we're now considering), then
 
5428
      assume that 25% of the rows will be filtered out by this condition.
 
5429
 
 
5430
      This heuristic is supposed to force tables used in exprZ to be before
 
5431
      this table in join order.
 
5432
    */
 
5433
    if (found_constraint)
 
5434
      rnd_records-= rnd_records/4;
 
5435
 
 
5436
    /*
 
5437
      If applicable, get a more accurate estimate. Don't use the two
 
5438
      heuristics at once.
 
5439
    */
 
5440
    if (s->table->quick_condition_rows != s->found_records)
 
5441
      rnd_records= s->table->quick_condition_rows;
 
5442
 
 
5443
    /*
 
5444
      Range optimizer never proposes a RANGE if it isn't better
 
5445
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5446
      Here we estimate its cost.
 
5447
    */
 
5448
    if (s->quick)
 
5449
    {
 
5450
      /*
 
5451
        For each record we:
 
5452
        - read record range through 'quick'
 
5453
        - skip rows which does not satisfy WHERE constraints
 
5454
        TODO:
 
5455
        We take into account possible use of join cache for ALL/index
 
5456
        access (see first else-branch below), but we don't take it into
 
5457
        account here for range/index_merge access. Find out why this is so.
 
5458
      */
 
5459
      tmp= record_count *
 
5460
        (s->quick->read_time +
 
5461
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5462
    }
 
5463
    else
 
5464
    {
 
5465
      /* Estimate cost of reading table. */
 
5466
      tmp= s->table->file->scan_time();
 
5467
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5468
      {
 
5469
        /*
 
5470
          For each record we have to:
 
5471
          - read the whole table record
 
5472
          - skip rows which does not satisfy join condition
 
5473
        */
 
5474
        tmp= record_count *
 
5475
          (tmp +
 
5476
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5477
      }
 
5478
      else
 
5479
      {
 
5480
        /* We read the table as many times as join buffer becomes full. */
 
5481
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5482
                           record_count /
 
5483
                           (double) session->variables.join_buff_size));
 
5484
        /*
 
5485
            We don't make full cartesian product between rows in the scanned
 
5486
           table and existing records because we skip all rows from the
 
5487
           scanned table, which does not satisfy join condition when
 
5488
           we read the table (see flush_cached_records for details). Here we
 
5489
           take into account cost to read and skip these records.
 
5490
        */
 
5491
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5492
      }
 
5493
    }
 
5494
 
 
5495
    /*
 
5496
      We estimate the cost of evaluating WHERE clause for found records
 
5497
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5498
      tmp give us total cost of using Table SCAN
 
5499
    */
 
5500
    if (best == DBL_MAX ||
 
5501
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5502
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5503
    {
 
5504
      /*
 
5505
        If the table has a range (s->quick is set) make_join_select()
 
5506
        will ensure that this will be used
 
5507
      */
 
5508
      best= tmp;
 
5509
      records= rows2double(rnd_records);
 
5510
      best_key= 0;
 
5511
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5512
      best_ref_depends_map= 0;
 
5513
      best_is_sj_inside_out= false;
 
5514
    }
 
5515
  }
 
5516
 
 
5517
  /* Update the cost information for the current partial plan */
 
5518
  join->positions[idx].records_read= records;
 
5519
  join->positions[idx].read_time=    best;
 
5520
  join->positions[idx].key=          best_key;
 
5521
  join->positions[idx].table=        s;
 
5522
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5523
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5524
 
 
5525
  if (!best_key &&
 
5526
      idx == join->const_tables &&
 
5527
      s->table == join->sort_by_table &&
 
5528
      join->unit->select_limit_cnt >= records)
 
5529
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5530
 
 
5531
  return;
 
5532
}
 
5533
 
 
5534
 
 
5535
/**
 
5536
  Selects and invokes a search strategy for an optimal query plan.
 
5537
 
 
5538
  The function checks user-configurable parameters that control the search
 
5539
  strategy for an optimal plan, selects the search method and then invokes
 
5540
  it. Each specific optimization procedure stores the final optimal plan in
 
5541
  the array 'join->best_positions', and the cost of the plan in
 
5542
  'join->best_read'.
 
5543
 
 
5544
  @param join         pointer to the structure providing all context info for
 
5545
                      the query
 
5546
  @param join_tables  set of the tables in the query
 
5547
 
 
5548
  @todo
 
5549
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5550
    the greedy version. Will be removed when greedy_search is approved.
 
5551
 
 
5552
  @retval
 
5553
    false       ok
 
5554
  @retval
 
5555
    true        Fatal error
 
5556
*/
 
5557
 
 
5558
static bool
 
5559
choose_plan(JOIN *join, table_map join_tables)
 
5560
{
 
5561
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
 
5562
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
 
5563
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5564
 
 
5565
  join->cur_embedding_map= 0;
 
5566
  reset_nj_counters(join->join_list);
 
5567
  /*
 
5568
    if (SELECT_STRAIGHT_JOIN option is set)
 
5569
      reorder tables so dependent tables come after tables they depend
 
5570
      on, otherwise keep tables in the order they were specified in the query
 
5571
    else
 
5572
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5573
      records accessed.
 
5574
  */
 
5575
  my_qsort(join->best_ref + join->const_tables,
 
5576
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5577
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5578
  join->cur_emb_sj_nests= 0;
 
5579
  if (straight_join)
 
5580
  {
 
5581
    optimize_straight_join(join, join_tables);
 
5582
  }
 
5583
  else
 
5584
  {
 
5585
    if (search_depth == MAX_TABLES+2)
 
5586
    { /*
 
5587
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5588
        the greedy version. Will be removed when greedy_search is approved.
 
5589
      */
 
5590
      join->best_read= DBL_MAX;
 
5591
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5592
        return(true);
 
5593
    }
 
5594
    else
 
5595
    {
 
5596
      if (search_depth == 0)
 
5597
        /* Automatically determine a reasonable value for 'search_depth' */
 
5598
        search_depth= determine_search_depth(join);
 
5599
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5600
        return(true);
 
5601
    }
 
5602
  }
 
5603
 
 
5604
  /*
 
5605
    Store the cost of this query into a user variable
 
5606
    Don't update last_query_cost for statements that are not "flat joins" :
 
5607
    i.e. they have subqueries, unions or call stored procedures.
 
5608
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5609
  */
 
5610
  if (join->session->lex->is_single_level_stmt())
 
5611
    join->session->status_var.last_query_cost= join->best_read;
 
5612
  return(false);
 
5613
}
 
5614
 
 
5615
 
 
5616
/**
 
5617
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5618
 
 
5619
  @param ptr1 pointer to first JOIN_TAB object
 
5620
  @param ptr2 pointer to second JOIN_TAB object
798
5621
 
799
5622
  NOTES
800
5623
    The order relation implemented by join_tab_cmp() is not transitive,
814
5637
  @retval
815
5638
    0  if equal
816
5639
*/
817
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5640
 
 
5641
static int
 
5642
join_tab_cmp(const void* ptr1, const void* ptr2)
818
5643
{
819
 
  JoinTable *jt1= *(JoinTable**) ptr1;
820
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5644
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5645
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
821
5646
 
822
5647
  if (jt1->dependent & jt2->table->map)
823
5648
    return 1;
830
5655
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
831
5656
}
832
5657
 
 
5658
 
833
5659
/**
834
5660
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
835
5661
*/
836
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5662
 
 
5663
static int
 
5664
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
837
5665
{
838
 
  JoinTable *jt1= *(JoinTable**) ptr1;
839
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5666
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5667
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
840
5668
 
841
5669
  if (jt1->dependent & jt2->table->map)
842
5670
    return 1;
846
5674
}
847
5675
 
848
5676
/**
 
5677
  Heuristic procedure to automatically guess a reasonable degree of
 
5678
  exhaustiveness for the greedy search procedure.
 
5679
 
 
5680
  The procedure estimates the optimization time and selects a search depth
 
5681
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5682
  find. If the number of tables in the query exceeds some constant, then
 
5683
  search_depth is set to this constant.
 
5684
 
 
5685
  @param join   pointer to the structure providing all context info for
 
5686
                the query
 
5687
 
 
5688
  @note
 
5689
    This is an extremely simplistic implementation that serves as a stub for a
 
5690
    more advanced analysis of the join. Ideally the search depth should be
 
5691
    determined by learning from previous query optimizations, because it will
 
5692
    depend on the CPU power (and other factors).
 
5693
 
 
5694
  @todo
 
5695
    this value should be determined dynamically, based on statistics:
 
5696
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5697
 
 
5698
  @todo
 
5699
    this value could be determined by some mapping of the form:
 
5700
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5701
 
 
5702
  @return
 
5703
    A positive integer that specifies the search depth (and thus the
 
5704
    exhaustiveness) of the depth-first search algorithm used by
 
5705
    'greedy_search'.
 
5706
*/
 
5707
 
 
5708
static uint
 
5709
determine_search_depth(JOIN *join)
 
5710
{
 
5711
  uint32_t table_count=  join->tables - join->const_tables;
 
5712
  uint32_t search_depth;
 
5713
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5714
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5715
 
 
5716
  if (table_count <= max_tables_for_exhaustive_opt)
 
5717
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5718
  else
 
5719
    /*
 
5720
      TODO: this value could be determined by some mapping of the form:
 
5721
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5722
    */
 
5723
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5724
 
 
5725
  return search_depth;
 
5726
}
 
5727
 
 
5728
 
 
5729
/**
 
5730
  Select the best ways to access the tables in a query without reordering them.
 
5731
 
 
5732
    Find the best access paths for each query table and compute their costs
 
5733
    according to their order in the array 'join->best_ref' (thus without
 
5734
    reordering the join tables). The function calls sequentially
 
5735
    'best_access_path' for each table in the query to select the best table
 
5736
    access method. The final optimal plan is stored in the array
 
5737
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5738
 
 
5739
  @param join          pointer to the structure providing all context info for
 
5740
                       the query
 
5741
  @param join_tables   set of the tables in the query
 
5742
 
 
5743
  @note
 
5744
    This function can be applied to:
 
5745
    - queries with STRAIGHT_JOIN
 
5746
    - internally to compute the cost of an arbitrary QEP
 
5747
  @par
 
5748
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5749
    optimization process to finalize a QEP as it is.
 
5750
*/
 
5751
 
 
5752
static void
 
5753
optimize_straight_join(JOIN *join, table_map join_tables)
 
5754
{
 
5755
  JOIN_TAB *s;
 
5756
  uint32_t idx= join->const_tables;
 
5757
  double    record_count= 1.0;
 
5758
  double    read_time=    0.0;
 
5759
 
 
5760
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5761
  {
 
5762
    /* Find the best access method from 's' to the current partial plan */
 
5763
    advance_sj_state(join_tables, s);
 
5764
    best_access_path(join, s, join->session, join_tables, idx,
 
5765
                     record_count, read_time);
 
5766
    /* compute the cost of the new plan extended with 's' */
 
5767
    record_count*= join->positions[idx].records_read;
 
5768
    read_time+=    join->positions[idx].read_time;
 
5769
    join_tables&= ~(s->table->map);
 
5770
    ++idx;
 
5771
  }
 
5772
 
 
5773
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5774
  if (join->sort_by_table &&
 
5775
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5776
    read_time+= record_count;  // We have to make a temp table
 
5777
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5778
  join->best_read= read_time;
 
5779
}
 
5780
 
 
5781
 
 
5782
/**
 
5783
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5784
 
 
5785
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5786
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5787
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5788
    selects the most promising table, and extends the current partial QEP with
 
5789
    that table.  Currenly the most 'promising' table is the one with least
 
5790
    expensive extension.\
 
5791
 
 
5792
    There are two extreme cases:
 
5793
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5794
    best complete continuation of the partial QEP. This continuation can be
 
5795
    used directly as a result of the search.
 
5796
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5797
    consideres the extension of the current QEP with each of the remaining
 
5798
    unoptimized tables.
 
5799
 
 
5800
    All other cases are in-between these two extremes. Thus the parameter
 
5801
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5802
    value, the longer the optimizaton time and possibly the better the
 
5803
    resulting plan. The lower the value, the fewer alternative plans are
 
5804
    estimated, but the more likely to get a bad QEP.
 
5805
 
 
5806
    All intermediate and final results of the procedure are stored in 'join':
 
5807
    - join->positions     : modified for every partial QEP that is explored
 
5808
    - join->best_positions: modified for the current best complete QEP
 
5809
    - join->best_read     : modified for the current best complete QEP
 
5810
    - join->best_ref      : might be partially reordered
 
5811
 
 
5812
    The final optimal plan is stored in 'join->best_positions', and its
 
5813
    corresponding cost in 'join->best_read'.
 
5814
 
 
5815
  @note
 
5816
    The following pseudocode describes the algorithm of 'greedy_search':
 
5817
 
 
5818
    @code
 
5819
    procedure greedy_search
 
5820
    input: remaining_tables
 
5821
    output: pplan;
 
5822
    {
 
5823
      pplan = <>;
 
5824
      do {
 
5825
        (t, a) = best_extension(pplan, remaining_tables);
 
5826
        pplan = concat(pplan, (t, a));
 
5827
        remaining_tables = remaining_tables - t;
 
5828
      } while (remaining_tables != {})
 
5829
      return pplan;
 
5830
    }
 
5831
 
 
5832
  @endcode
 
5833
    where 'best_extension' is a placeholder for a procedure that selects the
 
5834
    most "promising" of all tables in 'remaining_tables'.
 
5835
    Currently this estimate is performed by calling
 
5836
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5837
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5838
    mainly depends on that of 'best_extension_by_limited_search'.
 
5839
 
 
5840
  @par
 
5841
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5842
    worst-case complexity of this algorithm is <=
 
5843
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5844
    complexity of greedy_search is O(N!).
 
5845
 
 
5846
  @par
 
5847
    In the future, 'greedy_search' might be extended to support other
 
5848
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5849
 
 
5850
  @param join             pointer to the structure providing all context info
 
5851
                          for the query
 
5852
  @param remaining_tables set of tables not included into the partial plan yet
 
5853
  @param search_depth     controlls the exhaustiveness of the search
 
5854
  @param prune_level      the pruning heuristics that should be applied during
 
5855
                          search
 
5856
 
 
5857
  @retval
 
5858
    false       ok
 
5859
  @retval
 
5860
    true        Fatal error
 
5861
*/
 
5862
 
 
5863
static bool
 
5864
greedy_search(JOIN      *join,
 
5865
              table_map remaining_tables,
 
5866
              uint32_t      search_depth,
 
5867
              uint32_t      prune_level)
 
5868
{
 
5869
  double    record_count= 1.0;
 
5870
  double    read_time=    0.0;
 
5871
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
5872
  uint32_t      best_idx;
 
5873
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5874
  POSITION  best_pos;
 
5875
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5876
 
 
5877
  /* number of tables that remain to be optimized */
 
5878
  size_remain= my_count_bits(remaining_tables);
 
5879
 
 
5880
  do {
 
5881
    /* Find the extension of the current QEP with the lowest cost */
 
5882
    join->best_read= DBL_MAX;
 
5883
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5884
                                         read_time, search_depth, prune_level))
 
5885
      return(true);
 
5886
 
 
5887
    if (size_remain <= search_depth)
 
5888
    {
 
5889
      /*
 
5890
        'join->best_positions' contains a complete optimal extension of the
 
5891
        current partial QEP.
 
5892
      */
 
5893
      return(false);
 
5894
    }
 
5895
 
 
5896
    /* select the first table in the optimal extension as most promising */
 
5897
    best_pos= join->best_positions[idx];
 
5898
    best_table= best_pos.table;
 
5899
    /*
 
5900
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5901
      'join->positions' for cost estimates, therefore we have to update its
 
5902
      value.
 
5903
    */
 
5904
    join->positions[idx]= best_pos;
 
5905
 
 
5906
    /* find the position of 'best_table' in 'join->best_ref' */
 
5907
    best_idx= idx;
 
5908
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5909
    while (pos && best_table != pos)
 
5910
      pos= join->best_ref[++best_idx];
 
5911
    assert((pos != NULL)); // should always find 'best_table'
 
5912
    /* move 'best_table' at the first free position in the array of joins */
 
5913
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5914
 
 
5915
    /* compute the cost of the new plan extended with 'best_table' */
 
5916
    record_count*= join->positions[idx].records_read;
 
5917
    read_time+=    join->positions[idx].read_time;
 
5918
 
 
5919
    remaining_tables&= ~(best_table->table->map);
 
5920
    --size_remain;
 
5921
    ++idx;
 
5922
  } while (true);
 
5923
}
 
5924
 
 
5925
 
 
5926
/**
 
5927
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5928
  exhaustive search.
 
5929
 
 
5930
    The procedure searches for the optimal ordering of the query tables in set
 
5931
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5932
    each table. The choice of a table order and an access path for each table
 
5933
    constitutes a query execution plan (QEP) that fully specifies how to
 
5934
    execute the query.
 
5935
 
 
5936
    The maximal size of the found plan is controlled by the parameter
 
5937
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5938
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5939
    of only some of the query tables. Such "partial" optimal plans are useful
 
5940
    only as input to query optimization procedures, and cannot be used directly
 
5941
    to execute a query.
 
5942
 
 
5943
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5944
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5945
    evaluates the cost of the partial plan extended by all access plans for
 
5946
    each of the relations in 'remaining_tables', expands the current partial
 
5947
    plan with the access plan that results in lowest cost of the expanded
 
5948
    partial plan, and removes the corresponding relation from
 
5949
    'remaining_tables'. The algorithm continues until it either constructs a
 
5950
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5951
    search_depth.
 
5952
 
 
5953
    The final optimal plan is stored in 'join->best_positions'. The
 
5954
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5955
 
 
5956
  @note
 
5957
    The procedure uses a recursive depth-first search where the depth of the
 
5958
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5959
    parameter 'search_depth'.
 
5960
 
 
5961
  @note
 
5962
    The pseudocode below describes the algorithm of
 
5963
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5964
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5965
    the complexity of greedy_search is O(N!).
 
5966
 
 
5967
    @code
 
5968
    procedure best_extension_by_limited_search(
 
5969
      pplan in,             // in, partial plan of tables-joined-so-far
 
5970
      pplan_cost,           // in, cost of pplan
 
5971
      remaining_tables,     // in, set of tables not referenced in pplan
 
5972
      best_plan_so_far,     // in/out, best plan found so far
 
5973
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5974
      search_depth)         // in, maximum size of the plans being considered
 
5975
    {
 
5976
      for each table T from remaining_tables
 
5977
      {
 
5978
        // Calculate the cost of using table T as above
 
5979
        cost = complex-series-of-calculations;
 
5980
 
 
5981
        // Add the cost to the cost so far.
 
5982
        pplan_cost+= cost;
 
5983
 
 
5984
        if (pplan_cost >= best_plan_so_far_cost)
 
5985
          // pplan_cost already too great, stop search
 
5986
          continue;
 
5987
 
 
5988
        pplan= expand pplan by best_access_method;
 
5989
        remaining_tables= remaining_tables - table T;
 
5990
        if (remaining_tables is not an empty set
 
5991
            and
 
5992
            search_depth > 1)
 
5993
        {
 
5994
          best_extension_by_limited_search(pplan, pplan_cost,
 
5995
                                           remaining_tables,
 
5996
                                           best_plan_so_far,
 
5997
                                           best_plan_so_far_cost,
 
5998
                                           search_depth - 1);
 
5999
        }
 
6000
        else
 
6001
        {
 
6002
          best_plan_so_far_cost= pplan_cost;
 
6003
          best_plan_so_far= pplan;
 
6004
        }
 
6005
      }
 
6006
    }
 
6007
    @endcode
 
6008
 
 
6009
  @note
 
6010
    When 'best_extension_by_limited_search' is called for the first time,
 
6011
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6012
    The actual implementation provides a way to optionally use pruning
 
6013
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6014
    space by skipping some partial plans.
 
6015
 
 
6016
  @note
 
6017
    The parameter 'search_depth' provides control over the recursion
 
6018
    depth, and thus the size of the resulting optimal plan.
 
6019
 
 
6020
  @param join             pointer to the structure providing all context info
 
6021
                          for the query
 
6022
  @param remaining_tables set of tables not included into the partial plan yet
 
6023
  @param idx              length of the partial QEP in 'join->positions';
 
6024
                          since a depth-first search is used, also corresponds
 
6025
                          to the current depth of the search tree;
 
6026
                          also an index in the array 'join->best_ref';
 
6027
  @param record_count     estimate for the number of records returned by the
 
6028
                          best partial plan
 
6029
  @param read_time        the cost of the best partial plan
 
6030
  @param search_depth     maximum depth of the recursion and thus size of the
 
6031
                          found optimal plan
 
6032
                          (0 < search_depth <= join->tables+1).
 
6033
  @param prune_level      pruning heuristics that should be applied during
 
6034
                          optimization
 
6035
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6036
 
 
6037
  @retval
 
6038
    false       ok
 
6039
  @retval
 
6040
    true        Fatal error
 
6041
*/
 
6042
 
 
6043
static bool
 
6044
best_extension_by_limited_search(JOIN      *join,
 
6045
                                 table_map remaining_tables,
 
6046
                                 uint32_t      idx,
 
6047
                                 double    record_count,
 
6048
                                 double    read_time,
 
6049
                                 uint32_t      search_depth,
 
6050
                                 uint32_t      prune_level)
 
6051
{
 
6052
  Session *session= join->session;
 
6053
  if (session->killed)  // Abort
 
6054
    return(true);
 
6055
 
 
6056
  /*
 
6057
     'join' is a partial plan with lower cost than the best plan so far,
 
6058
     so continue expanding it further with the tables in 'remaining_tables'.
 
6059
  */
 
6060
  JOIN_TAB *s;
 
6061
  double best_record_count= DBL_MAX;
 
6062
  double best_read_time=    DBL_MAX;
 
6063
 
 
6064
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6065
  {
 
6066
    table_map real_table_bit= s->table->map;
 
6067
    if ((remaining_tables & real_table_bit) &&
 
6068
        !(remaining_tables & s->dependent) &&
 
6069
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6070
    {
 
6071
      double current_record_count, current_read_time;
 
6072
      advance_sj_state(remaining_tables, s);
 
6073
 
 
6074
      /*
 
6075
        psergey-insideout-todo:
 
6076
          when best_access_path() detects it could do an InsideOut scan or
 
6077
          some other scan, have it return an insideout scan and a flag that
 
6078
          requests to "fork" this loop iteration. (Q: how does that behave
 
6079
          when the depth is insufficient??)
 
6080
      */
 
6081
      /* Find the best access method from 's' to the current partial plan */
 
6082
      best_access_path(join, s, session, remaining_tables, idx,
 
6083
                       record_count, read_time);
 
6084
      /* Compute the cost of extending the plan with 's' */
 
6085
      current_record_count= record_count * join->positions[idx].records_read;
 
6086
      current_read_time=    read_time + join->positions[idx].read_time;
 
6087
 
 
6088
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6089
      if ((current_read_time +
 
6090
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6091
      {
 
6092
        restore_prev_nj_state(s);
 
6093
        restore_prev_sj_state(remaining_tables, s);
 
6094
        continue;
 
6095
      }
 
6096
 
 
6097
      /*
 
6098
        Prune some less promising partial plans. This heuristic may miss
 
6099
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6100
      */
 
6101
      if (prune_level == 1)
 
6102
      {
 
6103
        if (best_record_count > current_record_count ||
 
6104
            best_read_time > current_read_time ||
 
6105
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6106
        {
 
6107
          if (best_record_count >= current_record_count &&
 
6108
              best_read_time >= current_read_time &&
 
6109
              /* TODO: What is the reasoning behind this condition? */
 
6110
              (!(s->key_dependent & remaining_tables) ||
 
6111
               join->positions[idx].records_read < 2.0))
 
6112
          {
 
6113
            best_record_count= current_record_count;
 
6114
            best_read_time=    current_read_time;
 
6115
          }
 
6116
        }
 
6117
        else
 
6118
        {
 
6119
          restore_prev_nj_state(s);
 
6120
          restore_prev_sj_state(remaining_tables, s);
 
6121
          continue;
 
6122
        }
 
6123
      }
 
6124
 
 
6125
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6126
      { /* Recursively expand the current partial plan */
 
6127
        std::swap(join->best_ref[idx], *pos);
 
6128
        if (best_extension_by_limited_search(join,
 
6129
                                             remaining_tables & ~real_table_bit,
 
6130
                                             idx + 1,
 
6131
                                             current_record_count,
 
6132
                                             current_read_time,
 
6133
                                             search_depth - 1,
 
6134
                                             prune_level))
 
6135
          return(true);
 
6136
        std::swap(join->best_ref[idx], *pos);
 
6137
      }
 
6138
      else
 
6139
      { /*
 
6140
          'join' is either the best partial QEP with 'search_depth' relations,
 
6141
          or the best complete QEP so far, whichever is smaller.
 
6142
        */
 
6143
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6144
        if (join->sort_by_table &&
 
6145
            join->sort_by_table !=
 
6146
            join->positions[join->const_tables].table->table)
 
6147
          /* We have to make a temp table */
 
6148
          current_read_time+= current_record_count;
 
6149
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6150
        {
 
6151
          memcpy(join->best_positions, join->positions,
 
6152
                 sizeof(POSITION) * (idx + 1));
 
6153
          join->best_read= current_read_time - 0.001;
 
6154
        }
 
6155
      }
 
6156
      restore_prev_nj_state(s);
 
6157
      restore_prev_sj_state(remaining_tables, s);
 
6158
    }
 
6159
  }
 
6160
  return(false);
 
6161
}
 
6162
 
 
6163
 
 
6164
/**
 
6165
  @todo
 
6166
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6167
  tested and accepted.
 
6168
 
 
6169
  RETURN VALUES
 
6170
    false       ok
 
6171
    true        Fatal error
 
6172
*/
 
6173
static bool
 
6174
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6175
          double read_time)
 
6176
{
 
6177
  Session *session= join->session;
 
6178
  if (session->killed)
 
6179
    return(true);
 
6180
  if (!rest_tables)
 
6181
  {
 
6182
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6183
    if (join->sort_by_table &&
 
6184
        join->sort_by_table !=
 
6185
        join->positions[join->const_tables].table->table)
 
6186
      read_time+=record_count;                  // We have to make a temp table
 
6187
    if (read_time < join->best_read)
 
6188
    {
 
6189
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6190
      join->best_read= read_time - 0.001;
 
6191
    }
 
6192
    return(false);
 
6193
  }
 
6194
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6195
    return(false);                                      /* Found better before */
 
6196
 
 
6197
  JOIN_TAB *s;
 
6198
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6199
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6200
  {
 
6201
    table_map real_table_bit=s->table->map;
 
6202
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6203
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6204
    {
 
6205
      double records, best;
 
6206
      advance_sj_state(rest_tables, s);
 
6207
      best_access_path(join, s, session, rest_tables, idx, record_count,
 
6208
                       read_time);
 
6209
      records= join->positions[idx].records_read;
 
6210
      best= join->positions[idx].read_time;
 
6211
      /*
 
6212
        Go to the next level only if there hasn't been a better key on
 
6213
        this level! This will cut down the search for a lot simple cases!
 
6214
      */
 
6215
      double current_record_count=record_count*records;
 
6216
      double current_read_time=read_time+best;
 
6217
      if (best_record_count > current_record_count ||
 
6218
          best_read_time > current_read_time ||
 
6219
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6220
      {
 
6221
        if (best_record_count >= current_record_count &&
 
6222
            best_read_time >= current_read_time &&
 
6223
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6224
        {
 
6225
          best_record_count=current_record_count;
 
6226
          best_read_time=current_read_time;
 
6227
        }
 
6228
        std::swap(join->best_ref[idx], *pos);
 
6229
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6230
                      current_record_count,current_read_time))
 
6231
          return(true);
 
6232
        std::swap(join->best_ref[idx], *pos);
 
6233
      }
 
6234
      restore_prev_nj_state(s);
 
6235
      restore_prev_sj_state(rest_tables, s);
 
6236
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6237
        break;                          // Don't test all combinations
 
6238
    }
 
6239
  }
 
6240
  return(false);
 
6241
}
 
6242
 
 
6243
 
 
6244
/**
849
6245
  Find how much space the prevous read not const tables takes in cache.
850
6246
*/
851
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6247
 
 
6248
static void calc_used_field_length(Session *, JOIN_TAB *join_tab)
852
6249
{
853
6250
  uint32_t null_fields,blobs,fields,rec_length;
854
6251
  Field **f_ptr,*field;
 
6252
  MY_BITMAP *read_set= join_tab->table->read_set;;
855
6253
 
856
6254
  null_fields= blobs= fields= rec_length=0;
857
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6255
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
858
6256
  {
859
 
    if (field->isReadSet())
 
6257
    if (bitmap_is_set(read_set, field->field_index))
860
6258
    {
861
6259
      uint32_t flags=field->flags;
862
6260
      fields++;
863
6261
      rec_length+=field->pack_length();
864
6262
      if (flags & BLOB_FLAG)
865
 
        blobs++;
 
6263
        blobs++;
866
6264
      if (!(flags & NOT_NULL_FLAG))
867
 
        null_fields++;
 
6265
        null_fields++;
868
6266
    }
869
6267
  }
870
6268
  if (null_fields)
873
6271
    rec_length+=sizeof(bool);
874
6272
  if (blobs)
875
6273
  {
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
 
6274
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6275
                             (join_tab->table->getRecordLength()- rec_length));
 
6276
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6277
  }
 
6278
  join_tab->used_fields=fields;
 
6279
  join_tab->used_fieldlength=rec_length;
 
6280
  join_tab->used_blobs=blobs;
 
6281
}
 
6282
 
 
6283
 
 
6284
static uint
 
6285
cache_record_length(JOIN *join,uint32_t idx)
 
6286
{
 
6287
  uint32_t length=0;
 
6288
  JOIN_TAB **pos,**end;
 
6289
  Session *session=join->session;
 
6290
 
 
6291
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6292
       pos != end ;
 
6293
       pos++)
 
6294
  {
 
6295
    JOIN_TAB *join_tab= *pos;
 
6296
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6297
      calc_used_field_length(session, join_tab);
 
6298
    length+=join_tab->used_fieldlength;
 
6299
  }
 
6300
  return length;
 
6301
}
 
6302
 
 
6303
 
 
6304
/*
 
6305
  Get the number of different row combinations for subset of partial join
 
6306
 
 
6307
  SYNOPSIS
 
6308
    prev_record_reads()
 
6309
      join       The join structure
 
6310
      idx        Number of tables in the partial join order (i.e. the
 
6311
                 partial join order is in join->positions[0..idx-1])
 
6312
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6313
                 row combinations.
 
6314
 
 
6315
  DESCRIPTION
 
6316
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6317
    tables within that join order (specified in found_ref), find out how many
 
6318
    distinct row combinations of subset tables will be in the result of the
 
6319
    partial join order.
 
6320
 
 
6321
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6322
    method. The ref access depends on current rows of tables in found_ref.
 
6323
    We want to count # of different ref accesses. We assume two ref accesses
 
6324
    will be different if at least one of access parameters is different.
 
6325
    Example: consider a query
 
6326
 
 
6327
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6328
 
 
6329
    and a join order:
 
6330
      t1,  ref access on t1.key=c1
 
6331
      t2,  ref access on t2.key=c2
 
6332
      t3,  ref access on t3.key=t1.field
 
6333
 
 
6334
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6335
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6336
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6337
            n_distinct_ref_scans = #records_read(t1)
 
6338
 
 
6339
    The reason for having this function (at least the latest version of it)
 
6340
    is that we need to account for buffering in join execution.
 
6341
 
 
6342
    An edge-case example: if we have a non-first table in join accessed via
 
6343
    ref(const) or ref(param) where there is a small number of different
 
6344
    values of param, then the access will likely hit the disk cache and will
 
6345
    not require any disk seeks.
 
6346
 
 
6347
    The proper solution would be to assume an LRU disk cache of some size,
 
6348
    calculate probability of cache hits, etc. For now we just count
 
6349
    identical ref accesses as one.
 
6350
 
 
6351
  RETURN
 
6352
    Expected number of row combinations
 
6353
*/
 
6354
 
 
6355
static double
 
6356
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6357
{
 
6358
  double found=1.0;
 
6359
  POSITION *pos_end= join->positions - 1;
 
6360
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6361
  {
 
6362
    if (pos->table->table->map & found_ref)
 
6363
    {
 
6364
      found_ref|= pos->ref_depend_map;
 
6365
      /*
 
6366
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
 
6367
        with no matching row we will get position[t2].records_read==0.
 
6368
        Actually the size of output is one null-complemented row, therefore
 
6369
        we will use value of 1 whenever we get records_read==0.
 
6370
 
 
6371
        Note
 
6372
        - the above case can't occur if inner part of outer join has more
 
6373
          than one table: table with no matches will not be marked as const.
 
6374
 
 
6375
        - Ideally we should add 1 to records_read for every possible null-
 
6376
          complemented row. We're not doing it because: 1. it will require
 
6377
          non-trivial code and add overhead. 2. The value of records_read
 
6378
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6379
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6380
      */
 
6381
      if (pos->records_read > DBL_EPSILON)
 
6382
        found*= pos->records_read;
 
6383
    }
 
6384
  }
 
6385
  return found;
 
6386
}
 
6387
 
 
6388
 
 
6389
/**
 
6390
  Set up join struct according to best position.
 
6391
*/
 
6392
 
 
6393
static bool
 
6394
get_best_combination(JOIN *join)
 
6395
{
 
6396
  uint32_t i,tablenr;
 
6397
  table_map used_tables;
 
6398
  JOIN_TAB *join_tab,*j;
 
6399
  KEYUSE *keyuse;
 
6400
  uint32_t table_count;
 
6401
  Session *session=join->session;
 
6402
 
 
6403
  table_count=join->tables;
 
6404
  if (!(join->join_tab=join_tab=
 
6405
        (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
 
6406
    return(true);
 
6407
 
 
6408
  join->full_join=0;
 
6409
 
 
6410
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6411
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
6412
  {
 
6413
    Table *form;
 
6414
    *j= *join->best_positions[tablenr].table;
 
6415
    form=join->table[tablenr]=j->table;
 
6416
    used_tables|= form->map;
 
6417
    form->reginfo.join_tab=j;
 
6418
    if (!*j->on_expr_ref)
 
6419
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6420
    if (j->type == JT_CONST)
 
6421
      continue;                                 // Handled in make_join_stat..
 
6422
 
 
6423
    j->ref.key = -1;
 
6424
    j->ref.key_parts=0;
 
6425
 
 
6426
    if (j->type == JT_SYSTEM)
 
6427
      continue;
 
6428
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
6429
    {
 
6430
      j->type=JT_ALL;
 
6431
      if (tablenr != join->const_tables)
 
6432
        join->full_join=1;
 
6433
    }
 
6434
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6435
      return(true);                        // Something went wrong
 
6436
  }
 
6437
 
 
6438
  for (i=0 ; i < table_count ; i++)
 
6439
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6440
  update_depend_map(join);
 
6441
  return(0);
 
6442
}
 
6443
 
 
6444
 
 
6445
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6446
                               table_map used_tables)
 
6447
{
 
6448
  KEYUSE *keyuse=org_keyuse;
 
6449
  Session  *session= join->session;
 
6450
  uint32_t keyparts,length,key;
 
6451
  Table *table;
 
6452
  KEY *keyinfo;
 
6453
 
 
6454
  /*  Use best key from find_best */
 
6455
  table=j->table;
 
6456
  key=keyuse->key;
 
6457
  keyinfo=table->key_info+key;
 
6458
 
 
6459
  {
 
6460
    keyparts=length=0;
 
6461
    uint32_t found_part_ref_or_null= 0;
 
6462
    /*
 
6463
      Calculate length for the used key
 
6464
      Stop if there is a missing key part or when we find second key_part
 
6465
      with KEY_OPTIMIZE_REF_OR_NULL
 
6466
    */
 
6467
    do
 
6468
    {
 
6469
      if (!(~used_tables & keyuse->used_tables))
 
6470
      {
 
6471
        if (keyparts == keyuse->keypart &&
 
6472
            !(found_part_ref_or_null & keyuse->optimize))
 
6473
        {
 
6474
          keyparts++;
 
6475
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6476
          found_part_ref_or_null|= keyuse->optimize;
 
6477
        }
 
6478
      }
 
6479
      keyuse++;
 
6480
    } while (keyuse->table == table && keyuse->key == key);
 
6481
  }
 
6482
 
 
6483
  /* set up fieldref */
 
6484
  keyinfo=table->key_info+key;
 
6485
  j->ref.key_parts=keyparts;
 
6486
  j->ref.key_length=length;
 
6487
  j->ref.key=(int) key;
 
6488
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
 
6489
      !(j->ref.key_copy= (store_key**) session->alloc((sizeof(store_key*) *
 
6490
                                                   (keyparts+1)))) ||
 
6491
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
6492
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6493
  {
 
6494
    return(true);
 
6495
  }
 
6496
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
 
6497
  j->ref.key_err=1;
 
6498
  j->ref.null_rejecting= 0;
 
6499
  j->ref.disable_cache= false;
 
6500
  keyuse=org_keyuse;
 
6501
 
 
6502
  store_key **ref_key= j->ref.key_copy;
 
6503
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
6504
  bool keyuse_uses_no_tables= true;
 
6505
  {
 
6506
    uint32_t i;
 
6507
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
6508
    {
 
6509
      while (keyuse->keypart != i ||
 
6510
             ((~used_tables) & keyuse->used_tables))
 
6511
        keyuse++;                               /* Skip other parts */
 
6512
 
 
6513
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
6514
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6515
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6516
      if (keyuse->null_rejecting)
 
6517
        j->ref.null_rejecting |= 1 << i;
 
6518
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6519
      if (!keyuse->used_tables &&
 
6520
          !(join->select_options & SELECT_DESCRIBE))
 
6521
      {                                 // Compare against constant
 
6522
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6523
                           key_buff + maybe_null,
 
6524
                           maybe_null ?  key_buff : 0,
 
6525
                           keyinfo->key_part[i].length, keyuse->val);
 
6526
        if (session->is_fatal_error)
 
6527
          return(true);
 
6528
        tmp.copy();
 
6529
      }
 
6530
      else
 
6531
        *ref_key++= get_store_key(session,
 
6532
                                  keyuse,join->const_table_map,
 
6533
                                  &keyinfo->key_part[i],
 
6534
                                  key_buff, maybe_null);
 
6535
      /*
 
6536
        Remember if we are going to use REF_OR_NULL
 
6537
        But only if field _really_ can be null i.e. we force JT_REF
 
6538
        instead of JT_REF_OR_NULL in case if field can't be null
 
6539
      */
 
6540
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6541
        null_ref_key= key_buff;
 
6542
      key_buff+=keyinfo->key_part[i].store_length;
 
6543
    }
 
6544
  }
 
6545
  *ref_key=0;                           // end_marker
 
6546
  if (j->type == JT_CONST)
 
6547
    j->table->const_table= 1;
 
6548
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
 
6549
           keyparts != keyinfo->key_parts || null_ref_key)
 
6550
  {
 
6551
    /* Must read with repeat */
 
6552
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
6553
    j->ref.null_ref_key= null_ref_key;
 
6554
  }
 
6555
  else if (keyuse_uses_no_tables)
 
6556
  {
 
6557
    /*
 
6558
      This happen if we are using a constant expression in the ON part
 
6559
      of an LEFT JOIN.
 
6560
      SELECT * FROM a LEFT JOIN b ON b.key=30
 
6561
      Here we should not mark the table as a 'const' as a field may
 
6562
      have a 'normal' value or a NULL value.
 
6563
    */
 
6564
    j->type=JT_CONST;
 
6565
  }
 
6566
  else
 
6567
    j->type=JT_EQ_REF;
 
6568
  return(0);
 
6569
}
 
6570
 
 
6571
 
 
6572
 
 
6573
static store_key *
 
6574
get_store_key(Session *session, KEYUSE *keyuse, table_map used_tables,
 
6575
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6576
{
 
6577
  if (!((~used_tables) & keyuse->used_tables))          // if const item
894
6578
  {
895
6579
    return new store_key_const_item(session,
896
6580
                                    key_part->field,
897
6581
                                    key_buff + maybe_null,
898
6582
                                    maybe_null ? key_buff : 0,
899
6583
                                    key_part->length,
900
 
                                    key_use_val);
 
6584
                                    keyuse->val);
901
6585
  }
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
 
  {
 
6586
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6587
           (keyuse->val->type() == Item::REF_ITEM &&
 
6588
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6589
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6590
             Item_ref::DIRECT_REF &&
 
6591
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
908
6592
    return new store_key_field(session,
909
6593
                               key_part->field,
910
6594
                               key_buff + maybe_null,
911
6595
                               maybe_null ? key_buff : 0,
912
6596
                               key_part->length,
913
 
                               ((Item_field*) key_use_val->real_item())->field,
914
 
                               key_use_val->full_name());
915
 
  }
 
6597
                               ((Item_field*) keyuse->val->real_item())->field,
 
6598
                               keyuse->val->full_name());
916
6599
  return new store_key_item(session,
917
6600
                            key_part->field,
918
6601
                            key_buff + maybe_null,
919
6602
                            maybe_null ? key_buff : 0,
920
6603
                            key_part->length,
921
 
                            key_use_val);
 
6604
                            keyuse->val);
922
6605
}
923
6606
 
924
6607
/**
927
6610
  @return
928
6611
    returns 1 if there was some conversion made when the field was stored.
929
6612
*/
930
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6613
 
 
6614
bool
 
6615
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
931
6616
{
932
6617
  bool error;
933
 
  Table *table= field->getTable();
 
6618
  Table *table= field->table;
934
6619
  Session *session= table->in_use;
935
6620
  ha_rows cuted_fields=session->cuted_fields;
936
6621
 
937
6622
  /*
938
6623
    we should restore old value of count_cuted_fields because
939
 
    store_val_in_field can be called from insert_query
 
6624
    store_val_in_field can be called from mysql_insert
940
6625
    with select_insert, which make count_cuted_fields= 1
941
6626
   */
942
6627
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
946
6631
  return error || cuted_fields != session->cuted_fields;
947
6632
}
948
6633
 
 
6634
 
 
6635
static bool
 
6636
make_simple_join(JOIN *join,Table *tmp_table)
 
6637
{
 
6638
  Table **tableptr;
 
6639
  JOIN_TAB *join_tab;
 
6640
 
 
6641
  /*
 
6642
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6643
    to this function through JOIN::exec (may happen for sub-queries).
 
6644
  */
 
6645
  if (!join->table_reexec)
 
6646
  {
 
6647
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
6648
      return(true);                        /* purecov: inspected */
 
6649
    if (join->tmp_join)
 
6650
      join->tmp_join->table_reexec= join->table_reexec;
 
6651
  }
 
6652
  if (!join->join_tab_reexec)
 
6653
  {
 
6654
    if (!(join->join_tab_reexec=
 
6655
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
 
6656
      return(true);                        /* purecov: inspected */
 
6657
    if (join->tmp_join)
 
6658
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6659
  }
 
6660
  tableptr= join->table_reexec;
 
6661
  join_tab= join->join_tab_reexec;
 
6662
 
 
6663
  join->join_tab=join_tab;
 
6664
  join->table=tableptr; tableptr[0]=tmp_table;
 
6665
  join->tables=1;
 
6666
  join->const_tables=0;
 
6667
  join->const_table_map=0;
 
6668
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6669
    join->tmp_table_param.func_count=0;
 
6670
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6671
  join->first_record=join->sort_and_group=0;
 
6672
  join->send_records=(ha_rows) 0;
 
6673
  join->group=0;
 
6674
  join->row_limit=join->unit->select_limit_cnt;
 
6675
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6676
 
 
6677
  join_tab->cache.buff=0;                       /* No caching */
 
6678
  join_tab->table=tmp_table;
 
6679
  join_tab->select=0;
 
6680
  join_tab->select_cond=0;
 
6681
  join_tab->quick=0;
 
6682
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6683
  join_tab->keys.init();
 
6684
  join_tab->keys.set_all();                     /* test everything in quick */
 
6685
  join_tab->info=0;
 
6686
  join_tab->on_expr_ref=0;
 
6687
  join_tab->last_inner= 0;
 
6688
  join_tab->first_unmatched= 0;
 
6689
  join_tab->ref.key = -1;
 
6690
  join_tab->not_used_in_distinct=0;
 
6691
  join_tab->read_first_record= join_init_read_record;
 
6692
  join_tab->join=join;
 
6693
  join_tab->ref.key_parts= 0;
 
6694
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6695
  join_tab->do_firstmatch= NULL;
 
6696
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6697
  tmp_table->status=0;
 
6698
  tmp_table->null_row=0;
 
6699
  return(false);
 
6700
}
 
6701
 
 
6702
 
949
6703
inline void add_cond_and_fix(Item **e1, Item *e2)
950
6704
{
951
6705
  if (*e1)
961
6715
    *e1= e2;
962
6716
}
963
6717
 
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
6718
 
1092
6719
/**
1093
6720
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1133
6760
 
1134
6761
    Implementation overview
1135
6762
      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.
 
6763
         predicates in in KEY_FIELD::null_rejecting
 
6764
      1.1 add_key_part saves these to KEYUSE.
 
6765
      2. create_ref_for_key copies them to TABLE_REF.
1139
6766
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1140
 
         appropiate JoinTable members.
 
6767
         appropiate JOIN_TAB members.
1141
6768
*/
1142
 
void add_not_null_conds(Join *join)
 
6769
 
 
6770
static void add_not_null_conds(JOIN *join)
1143
6771
{
1144
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6772
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
1145
6773
  {
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) &&
 
6774
    JOIN_TAB *tab=join->join_tab+i;
 
6775
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
 
6776
         tab->type == JT_REF_OR_NULL) &&
1149
6777
        !tab->table->maybe_null)
1150
6778
    {
1151
6779
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1156
6784
          Item *notnull;
1157
6785
          assert(item->type() == Item::FIELD_ITEM);
1158
6786
          Item_field *not_null_item= (Item_field*)item;
1159
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6787
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1160
6788
          /*
1161
6789
            For UPDATE queries such as:
1162
6790
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1197
6825
    -  pointer to the guarded predicate, if success
1198
6826
    -  0, otherwise
1199
6827
*/
1200
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6828
 
 
6829
static COND*
 
6830
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1201
6831
{
1202
6832
  COND *tmp;
1203
6833
  assert(cond != 0);
1213
6843
  return tmp;
1214
6844
}
1215
6845
 
 
6846
 
 
6847
/**
 
6848
  Fill in outer join related info for the execution plan structure.
 
6849
 
 
6850
    For each outer join operation left after simplification of the
 
6851
    original query the function set up the following pointers in the linear
 
6852
    structure join->join_tab representing the selected execution plan.
 
6853
    The first inner table t0 for the operation is set to refer to the last
 
6854
    inner table tk through the field t0->last_inner.
 
6855
    Any inner table ti for the operation are set to refer to the first
 
6856
    inner table ti->first_inner.
 
6857
    The first inner table t0 for the operation is set to refer to the
 
6858
    first inner table of the embedding outer join operation, if there is any,
 
6859
    through the field t0->first_upper.
 
6860
    The on expression for the outer join operation is attached to the
 
6861
    corresponding first inner table through the field t0->on_expr_ref.
 
6862
    Here ti are structures of the JOIN_TAB type.
 
6863
 
 
6864
  EXAMPLE. For the query:
 
6865
  @code
 
6866
        SELECT * FROM t1
 
6867
                      LEFT JOIN
 
6868
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6869
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6870
          WHERE t1.c > 5,
 
6871
  @endcode
 
6872
 
 
6873
    given the execution plan with the table order t1,t2,t3,t4
 
6874
    is selected, the following references will be set;
 
6875
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6876
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6877
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
 
6878
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6879
 
 
6880
  @param join   reference to the info fully describing the query
 
6881
 
 
6882
  @note
 
6883
    The function assumes that the simplification procedure has been
 
6884
    already applied to the join query (see simplify_joins).
 
6885
    This function can be called only after the execution plan
 
6886
    has been chosen.
 
6887
*/
 
6888
 
 
6889
static void
 
6890
make_outerjoin_info(JOIN *join)
 
6891
{
 
6892
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6893
  {
 
6894
    JOIN_TAB *tab=join->join_tab+i;
 
6895
    Table *table=tab->table;
 
6896
    TableList *tbl= table->pos_in_table_list;
 
6897
    TableList *embedding= tbl->embedding;
 
6898
 
 
6899
    if (tbl->outer_join)
 
6900
    {
 
6901
      /*
 
6902
        Table tab is the only one inner table for outer join.
 
6903
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6904
        is in the query above.)
 
6905
      */
 
6906
      tab->last_inner= tab->first_inner= tab;
 
6907
      tab->on_expr_ref= &tbl->on_expr;
 
6908
      tab->cond_equal= tbl->cond_equal;
 
6909
      if (embedding)
 
6910
        tab->first_upper= embedding->nested_join->first_nested;
 
6911
    }
 
6912
    for ( ; embedding ; embedding= embedding->embedding)
 
6913
    {
 
6914
      /* Ignore sj-nests: */
 
6915
      if (!embedding->on_expr)
 
6916
        continue;
 
6917
      nested_join_st *nested_join= embedding->nested_join;
 
6918
      if (!nested_join->counter_)
 
6919
      {
 
6920
        /*
 
6921
          Table tab is the first inner table for nested_join.
 
6922
          Save reference to it in the nested join structure.
 
6923
        */
 
6924
        nested_join->first_nested= tab;
 
6925
        tab->on_expr_ref= &embedding->on_expr;
 
6926
        tab->cond_equal= tbl->cond_equal;
 
6927
        if (embedding->embedding)
 
6928
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6929
      }
 
6930
      if (!tab->first_inner)
 
6931
        tab->first_inner= nested_join->first_nested;
 
6932
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6933
        break;
 
6934
      /* Table tab is the last inner table for nested join. */
 
6935
      nested_join->first_nested->last_inner= tab;
 
6936
    }
 
6937
  }
 
6938
  return;
 
6939
}
 
6940
 
 
6941
 
 
6942
static bool
 
6943
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6944
{
 
6945
  Session *session= join->session;
 
6946
  if (select)
 
6947
  {
 
6948
    add_not_null_conds(join);
 
6949
    table_map used_tables;
 
6950
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6951
    {                        /* there may be a select without a cond. */
 
6952
      if (join->tables > 1)
 
6953
        cond->update_used_tables();             // Tablenr may have changed
 
6954
      if (join->const_tables == join->tables &&
 
6955
          session->lex->current_select->master_unit() ==
 
6956
          &session->lex->unit)          // not upper level SELECT
 
6957
        join->const_table_map|=RAND_TABLE_BIT;
 
6958
      {                                         // Check const tables
 
6959
        COND *const_cond=
 
6960
          make_cond_for_table(cond,
 
6961
                              join->const_table_map,
 
6962
                              (table_map) 0, 1);
 
6963
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6964
             tab < join->join_tab+join->tables ; tab++)
 
6965
        {
 
6966
          if (*tab->on_expr_ref)
 
6967
          {
 
6968
            JOIN_TAB *cond_tab= tab->first_inner;
 
6969
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6970
                                           join->const_table_map,
 
6971
                                           (  table_map) 0, 0);
 
6972
            if (!tmp)
 
6973
              continue;
 
6974
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6975
            if (!tmp)
 
6976
              return(1);
 
6977
            tmp->quick_fix_field();
 
6978
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6979
                                    new Item_cond_and(cond_tab->select_cond,
 
6980
                                                      tmp);
 
6981
            if (!cond_tab->select_cond)
 
6982
              return(1);
 
6983
            cond_tab->select_cond->quick_fix_field();
 
6984
          }
 
6985
        }
 
6986
        if (const_cond && !const_cond->val_int())
 
6987
        {
 
6988
          return(1);     // Impossible const condition
 
6989
        }
 
6990
      }
 
6991
    }
 
6992
    used_tables=((select->const_tables=join->const_table_map) |
 
6993
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
6994
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6995
    {
 
6996
      JOIN_TAB *tab=join->join_tab+i;
 
6997
      /*
 
6998
        first_inner is the X in queries like:
 
6999
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7000
      */
 
7001
      JOIN_TAB *first_inner_tab= tab->first_inner;
 
7002
      table_map current_map= tab->table->map;
 
7003
      bool use_quick_range=0;
 
7004
      COND *tmp;
 
7005
 
 
7006
      /*
 
7007
        Following force including random expression in last table condition.
 
7008
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7009
      */
 
7010
      if (i == join->tables-1)
 
7011
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7012
      used_tables|=current_map;
 
7013
 
 
7014
      if (tab->type == JT_REF && tab->quick &&
 
7015
          (uint) tab->ref.key == tab->quick->index &&
 
7016
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7017
      {
 
7018
        /* Range uses longer key;  Use this instead of ref on key */
 
7019
        tab->type=JT_ALL;
 
7020
        use_quick_range=1;
 
7021
        tab->use_quick=1;
 
7022
        tab->ref.key= -1;
 
7023
        tab->ref.key_parts=0;           // Don't use ref key.
 
7024
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7025
        /*
 
7026
          We will use join cache here : prevent sorting of the first
 
7027
          table only and sort at the end.
 
7028
        */
 
7029
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7030
          join->full_join= 1;
 
7031
      }
 
7032
 
 
7033
      tmp= NULL;
 
7034
      if (cond)
 
7035
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7036
      if (cond && !tmp && tab->quick)
 
7037
      {                                         // Outer join
 
7038
        if (tab->type != JT_ALL)
 
7039
        {
 
7040
          /*
 
7041
            Don't use the quick method
 
7042
            We come here in the case where we have 'key=constant' and
 
7043
            the test is removed by make_cond_for_table()
 
7044
          */
 
7045
          delete tab->quick;
 
7046
          tab->quick= 0;
 
7047
        }
 
7048
        else
 
7049
        {
 
7050
          /*
 
7051
            Hack to handle the case where we only refer to a table
 
7052
            in the ON part of an OUTER JOIN. In this case we want the code
 
7053
            below to check if we should use 'quick' instead.
 
7054
          */
 
7055
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7056
        }
 
7057
 
 
7058
      }
 
7059
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7060
          tab->type == JT_EQ_REF)
 
7061
      {
 
7062
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7063
                                       session->memdup((unsigned char*) select,
 
7064
                                                   sizeof(*select)));
 
7065
        if (!sel)
 
7066
          return(1);                    // End of memory
 
7067
        /*
 
7068
          If tab is an inner table of an outer join operation,
 
7069
          add a match guard to the pushed down predicate.
 
7070
          The guard will turn the predicate on only after
 
7071
          the first match for outer tables is encountered.
 
7072
        */
 
7073
        if (cond && tmp)
 
7074
        {
 
7075
          /*
 
7076
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7077
            a cond, so neutralize the hack above.
 
7078
          */
 
7079
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7080
            return(1);
 
7081
          tab->select_cond=sel->cond=tmp;
 
7082
          /* Push condition to storage engine if this is enabled
 
7083
             and the condition is not guarded */
 
7084
          tab->table->file->pushed_cond= NULL;
 
7085
          if (session->variables.engine_condition_pushdown)
 
7086
          {
 
7087
            COND *push_cond=
 
7088
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7089
            if (push_cond)
 
7090
            {
 
7091
              /* Push condition to handler */
 
7092
              if (!tab->table->file->cond_push(push_cond))
 
7093
                tab->table->file->pushed_cond= push_cond;
 
7094
            }
 
7095
          }
 
7096
        }
 
7097
        else
 
7098
          tab->select_cond= sel->cond= NULL;
 
7099
 
 
7100
        sel->head=tab->table;
 
7101
        if (tab->quick)
 
7102
        {
 
7103
          /* Use quick key read if it's a constant and it's not used
 
7104
             with key reading */
 
7105
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7106
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7107
          {
 
7108
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7109
            sel->quick_keys.clear_all();
 
7110
            sel->needed_reg.clear_all();
 
7111
          }
 
7112
          else
 
7113
          {
 
7114
            delete tab->quick;
 
7115
          }
 
7116
          tab->quick=0;
 
7117
        }
 
7118
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7119
        if (i == join->const_tables && ref_key)
 
7120
        {
 
7121
          if (!tab->const_keys.is_clear_all() &&
 
7122
              tab->table->reginfo.impossible_range)
 
7123
            return(1);
 
7124
        }
 
7125
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7126
        {
 
7127
          if (!tab->const_keys.is_clear_all() &&
 
7128
              tab->table->reginfo.impossible_range)
 
7129
            return(1);                          // Impossible range
 
7130
          /*
 
7131
            We plan to scan all rows.
 
7132
            Check again if we should use an index.
 
7133
            We could have used an column from a previous table in
 
7134
            the index if we are using limit and this is the first table
 
7135
          */
 
7136
 
 
7137
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7138
              (!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)))
 
7139
          {
 
7140
            /* Join with outer join condition */
 
7141
            COND *orig_cond=sel->cond;
 
7142
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7143
 
 
7144
            /*
 
7145
              We can't call sel->cond->fix_fields,
 
7146
              as it will break tab->on_expr if it's AND condition
 
7147
              (fix_fields currently removes extra AND/OR levels).
 
7148
              Yet attributes of the just built condition are not needed.
 
7149
              Thus we call sel->cond->quick_fix_field for safety.
 
7150
            */
 
7151
            if (sel->cond && !sel->cond->fixed)
 
7152
              sel->cond->quick_fix_field();
 
7153
 
 
7154
            if (sel->test_quick_select(session, tab->keys,
 
7155
                                       used_tables & ~ current_map,
 
7156
                                       (join->select_options &
 
7157
                                        OPTION_FOUND_ROWS ?
 
7158
                                        HA_POS_ERROR :
 
7159
                                        join->unit->select_limit_cnt), 0,
 
7160
                                        false) < 0)
 
7161
            {
 
7162
              /*
 
7163
                Before reporting "Impossible WHERE" for the whole query
 
7164
                we have to check isn't it only "impossible ON" instead
 
7165
              */
 
7166
              sel->cond=orig_cond;
 
7167
              if (!*tab->on_expr_ref ||
 
7168
                  sel->test_quick_select(session, tab->keys,
 
7169
                                         used_tables & ~ current_map,
 
7170
                                         (join->select_options &
 
7171
                                          OPTION_FOUND_ROWS ?
 
7172
                                          HA_POS_ERROR :
 
7173
                                          join->unit->select_limit_cnt),0,
 
7174
                                          false) < 0)
 
7175
                return(1);                      // Impossible WHERE
 
7176
            }
 
7177
            else
 
7178
              sel->cond=orig_cond;
 
7179
 
 
7180
            /* Fix for EXPLAIN */
 
7181
            if (sel->quick)
 
7182
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7183
          }
 
7184
          else
 
7185
          {
 
7186
            sel->needed_reg=tab->needed_reg;
 
7187
            sel->quick_keys.clear_all();
 
7188
          }
 
7189
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7190
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7191
          {
 
7192
            tab->keys=sel->quick_keys;
 
7193
            tab->keys.merge(sel->needed_reg);
 
7194
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7195
                             (select->quick_keys.is_clear_all() ||
 
7196
                              (select->quick &&
 
7197
                               (select->quick->records >= 100L)))) ?
 
7198
              2 : 1;
 
7199
            sel->read_tables= used_tables & ~current_map;
 
7200
          }
 
7201
          if (i != join->const_tables && tab->use_quick != 2)
 
7202
          {                                     /* Read with cache */
 
7203
            if (cond &&
 
7204
                (tmp=make_cond_for_table(cond,
 
7205
                                         join->const_table_map |
 
7206
                                         current_map,
 
7207
                                         current_map, 0)))
 
7208
            {
 
7209
              tab->cache.select=(SQL_SELECT*)
 
7210
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7211
              tab->cache.select->cond=tmp;
 
7212
              tab->cache.select->read_tables=join->const_table_map;
 
7213
            }
 
7214
          }
 
7215
        }
 
7216
      }
 
7217
 
 
7218
      /*
 
7219
        Push down conditions from all on expressions.
 
7220
        Each of these conditions are guarded by a variable
 
7221
        that turns if off just before null complemented row for
 
7222
        outer joins is formed. Thus, the condition from an
 
7223
        'on expression' are guaranteed not to be checked for
 
7224
        the null complemented row.
 
7225
      */
 
7226
 
 
7227
      /* First push down constant conditions from on expressions */
 
7228
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7229
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7230
      {
 
7231
        if (*join_tab->on_expr_ref)
 
7232
        {
 
7233
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7234
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7235
                                         join->const_table_map,
 
7236
                                         (table_map) 0, 0);
 
7237
          if (!tmp)
 
7238
            continue;
 
7239
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7240
          if (!tmp)
 
7241
            return(1);
 
7242
          tmp->quick_fix_field();
 
7243
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7244
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7245
          if (!cond_tab->select_cond)
 
7246
            return(1);
 
7247
          cond_tab->select_cond->quick_fix_field();
 
7248
        }
 
7249
      }
 
7250
 
 
7251
      /* Push down non-constant conditions from on expressions */
 
7252
      JOIN_TAB *last_tab= tab;
 
7253
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7254
      {
 
7255
        /*
 
7256
          Table tab is the last inner table of an outer join.
 
7257
          An on expression is always attached to it.
 
7258
        */
 
7259
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7260
 
 
7261
        table_map used_tables2= (join->const_table_map |
 
7262
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7263
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7264
        {
 
7265
          current_map= tab->table->map;
 
7266
          used_tables2|= current_map;
 
7267
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7268
                                              current_map, 0);
 
7269
          if (tmp_cond)
 
7270
          {
 
7271
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7272
            /*
 
7273
              First add the guards for match variables of
 
7274
              all embedding outer join operations.
 
7275
            */
 
7276
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7277
                                                     tmp_cond,
 
7278
                                                     first_inner_tab)))
 
7279
              return(1);
 
7280
            /*
 
7281
              Now add the guard turning the predicate off for
 
7282
              the null complemented row.
 
7283
            */
 
7284
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7285
                                              &first_inner_tab->
 
7286
                                              not_null_compl);
 
7287
            if (tmp_cond)
 
7288
              tmp_cond->quick_fix_field();
 
7289
            /* Add the predicate to other pushed down predicates */
 
7290
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7291
                                  new Item_cond_and(cond_tab->select_cond,
 
7292
                                                    tmp_cond);
 
7293
            if (!cond_tab->select_cond)
 
7294
              return(1);
 
7295
            cond_tab->select_cond->quick_fix_field();
 
7296
          }
 
7297
        }
 
7298
        first_inner_tab= first_inner_tab->first_upper;
 
7299
      }
 
7300
    }
 
7301
  }
 
7302
  return(0);
 
7303
}
 
7304
 
 
7305
 
 
7306
/*
 
7307
  Check if given expression uses only table fields covered by the given index
 
7308
 
 
7309
  SYNOPSIS
 
7310
    uses_index_fields_only()
 
7311
      item           Expression to check
 
7312
      tbl            The table having the index
 
7313
      keyno          The index number
 
7314
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7315
 
 
7316
  DESCRIPTION
 
7317
    Check if given expression only uses fields covered by index #keyno in the
 
7318
    table tbl. The expression can use any fields in any other tables.
 
7319
 
 
7320
    The expression is guaranteed not to be AND or OR - those constructs are
 
7321
    handled outside of this function.
 
7322
 
 
7323
  RETURN
 
7324
    true   Yes
 
7325
    false  No
 
7326
*/
 
7327
 
 
7328
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
 
7329
                            bool other_tbls_ok)
 
7330
{
 
7331
  if (item->const_item())
 
7332
    return true;
 
7333
 
 
7334
  /*
 
7335
    Don't push down the triggered conditions. Nested outer joins execution
 
7336
    code may need to evaluate a condition several times (both triggered and
 
7337
    untriggered), and there is no way to put thi
 
7338
    TODO: Consider cloning the triggered condition and using the copies for:
 
7339
      1. push the first copy down, to have most restrictive index condition
 
7340
         possible
 
7341
      2. Put the second copy into tab->select_cond.
 
7342
  */
 
7343
  if (item->type() == Item::FUNC_ITEM &&
 
7344
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7345
    return false;
 
7346
 
 
7347
  if (!(item->used_tables() & tbl->map))
 
7348
    return other_tbls_ok;
 
7349
 
 
7350
  Item::Type item_type= item->type();
 
7351
  switch (item_type) {
 
7352
  case Item::FUNC_ITEM:
 
7353
    {
 
7354
      /* This is a function, apply condition recursively to arguments */
 
7355
      Item_func *item_func= (Item_func*)item;
 
7356
      Item **child;
 
7357
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7358
      for (child= item_func->arguments(); child != item_end; child++)
 
7359
      {
 
7360
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7361
          return false;
 
7362
      }
 
7363
      return true;
 
7364
    }
 
7365
  case Item::COND_ITEM:
 
7366
    {
 
7367
      /* This is a function, apply condition recursively to arguments */
 
7368
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7369
      Item *item;
 
7370
      while ((item=li++))
 
7371
      {
 
7372
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7373
          return false;
 
7374
      }
 
7375
      return true;
 
7376
    }
 
7377
  case Item::FIELD_ITEM:
 
7378
    {
 
7379
      Item_field *item_field= (Item_field*)item;
 
7380
      if (item_field->field->table != tbl)
 
7381
        return true;
 
7382
      return item_field->field->part_of_key.is_set(keyno);
 
7383
    }
 
7384
  case Item::REF_ITEM:
 
7385
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7386
                                  other_tbls_ok);
 
7387
  default:
 
7388
    return false; /* Play it safe, don't push unknown non-const items */
 
7389
  }
 
7390
}
 
7391
 
 
7392
 
1216
7393
#define ICP_COND_USES_INDEX_ONLY 10
1217
7394
 
1218
 
 
1219
 
/**
1220
 
  cleanup JoinTable.
1221
 
*/
1222
 
void JoinTable::cleanup()
1223
 
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
7395
/*
 
7396
  Get a part of the condition that can be checked using only index fields
 
7397
 
 
7398
  SYNOPSIS
 
7399
    make_cond_for_index()
 
7400
      cond           The source condition
 
7401
      table          The table that is partially available
 
7402
      keyno          The index in the above table. Only fields covered by the index
 
7403
                     are available
 
7404
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7405
 
 
7406
  DESCRIPTION
 
7407
    Get a part of the condition that can be checked when for the given table
 
7408
    we have values only of fields covered by some index. The condition may
 
7409
    refer to other tables, it is assumed that we have values of all of their
 
7410
    fields.
 
7411
 
 
7412
    Example:
 
7413
      make_cond_for_index(
 
7414
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7415
          t2, keyno(t2.key1))
 
7416
      will return
 
7417
        "cond(t1.field) AND cond(t2.key2)"
 
7418
 
 
7419
  RETURN
 
7420
    Index condition, or NULL if no condition could be inferred.
 
7421
*/
 
7422
 
 
7423
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7424
                          bool other_tbls_ok)
 
7425
{
 
7426
  if (!cond)
 
7427
    return NULL;
 
7428
  if (cond->type() == Item::COND_ITEM)
 
7429
  {
 
7430
    uint32_t n_marked= 0;
 
7431
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7432
    {
 
7433
      Item_cond_and *new_cond=new Item_cond_and;
 
7434
      if (!new_cond)
 
7435
        return (COND*) 0;
 
7436
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7437
      Item *item;
 
7438
      while ((item=li++))
 
7439
      {
 
7440
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7441
        if (fix)
 
7442
          new_cond->argument_list()->push_back(fix);
 
7443
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7444
      }
 
7445
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7446
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7447
      switch (new_cond->argument_list()->elements) {
 
7448
      case 0:
 
7449
        return (COND*) 0;
 
7450
      case 1:
 
7451
        return new_cond->argument_list()->head();
 
7452
      default:
 
7453
        new_cond->quick_fix_field();
 
7454
        return new_cond;
 
7455
      }
 
7456
    }
 
7457
    else /* It's OR */
 
7458
    {
 
7459
      Item_cond_or *new_cond=new Item_cond_or;
 
7460
      if (!new_cond)
 
7461
        return (COND*) 0;
 
7462
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7463
      Item *item;
 
7464
      while ((item=li++))
 
7465
      {
 
7466
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7467
        if (!fix)
 
7468
          return (COND*) 0;
 
7469
        new_cond->argument_list()->push_back(fix);
 
7470
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7471
      }
 
7472
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7473
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7474
      new_cond->quick_fix_field();
 
7475
      new_cond->top_level_item();
 
7476
      return new_cond;
 
7477
    }
 
7478
  }
 
7479
 
 
7480
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7481
    return (COND*) 0;
 
7482
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7483
  return cond;
 
7484
}
 
7485
 
 
7486
 
 
7487
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7488
{
 
7489
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7490
    return 0; /* Already checked */
 
7491
 
 
7492
  if (cond->type() == Item::COND_ITEM)
 
7493
  {
 
7494
    table_map tbl_map= 0;
 
7495
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7496
    {
 
7497
      /* Create new top level AND item */
 
7498
      Item_cond_and *new_cond=new Item_cond_and;
 
7499
      if (!new_cond)
 
7500
        return (COND*) 0;
 
7501
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7502
      Item *item;
 
7503
      while ((item=li++))
 
7504
      {
 
7505
        Item *fix= make_cond_remainder(item, exclude_index);
 
7506
        if (fix)
 
7507
        {
 
7508
          new_cond->argument_list()->push_back(fix);
 
7509
          tbl_map |= fix->used_tables();
 
7510
        }
 
7511
      }
 
7512
      switch (new_cond->argument_list()->elements) {
 
7513
      case 0:
 
7514
        return (COND*) 0;
 
7515
      case 1:
 
7516
        return new_cond->argument_list()->head();
 
7517
      default:
 
7518
        new_cond->quick_fix_field();
 
7519
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7520
        return new_cond;
 
7521
      }
 
7522
    }
 
7523
    else /* It's OR */
 
7524
    {
 
7525
      Item_cond_or *new_cond=new Item_cond_or;
 
7526
      if (!new_cond)
 
7527
        return (COND*) 0;
 
7528
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7529
      Item *item;
 
7530
      while ((item=li++))
 
7531
      {
 
7532
        Item *fix= make_cond_remainder(item, false);
 
7533
        if (!fix)
 
7534
          return (COND*) 0;
 
7535
        new_cond->argument_list()->push_back(fix);
 
7536
        tbl_map |= fix->used_tables();
 
7537
      }
 
7538
      new_cond->quick_fix_field();
 
7539
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7540
      new_cond->top_level_item();
 
7541
      return new_cond;
 
7542
    }
 
7543
  }
 
7544
  return cond;
 
7545
}
 
7546
 
 
7547
 
 
7548
/*
 
7549
  Try to extract and push the index condition
 
7550
 
 
7551
  SYNOPSIS
 
7552
    push_index_cond()
 
7553
      tab            A join tab that has tab->table->file and its condition
 
7554
                     in tab->select_cond
 
7555
      keyno          Index for which extract and push the condition
 
7556
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7557
 
 
7558
  DESCRIPTION
 
7559
    Try to extract and push the index condition down to table handler
 
7560
*/
 
7561
 
 
7562
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7563
{
 
7564
  Item *idx_cond;
 
7565
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7566
      tab->join->session->variables.engine_condition_pushdown)
 
7567
  {
 
7568
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7569
                                  other_tbls_ok);
 
7570
 
 
7571
    if (idx_cond)
 
7572
    {
 
7573
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7574
      Item *idx_remainder_cond=
 
7575
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7576
 
 
7577
      /*
 
7578
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7579
        condition.
 
7580
        TODO: This check happens to work on current ICP implementations, but
 
7581
        there may exist a compliant implementation that will not work
 
7582
        correctly with it. Sort this out when we stabilize the condition
 
7583
        pushdown APIs.
 
7584
      */
 
7585
      if (idx_remainder_cond != idx_cond)
 
7586
        tab->ref.disable_cache= true;
 
7587
 
 
7588
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7589
 
 
7590
      if (row_cond)
 
7591
      {
 
7592
        if (!idx_remainder_cond)
 
7593
          tab->select_cond= row_cond;
 
7594
        else
 
7595
        {
 
7596
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7597
          tab->select_cond->quick_fix_field();
 
7598
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
 
7599
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7600
        }
 
7601
      }
 
7602
      else
 
7603
        tab->select_cond= idx_remainder_cond;
 
7604
      if (tab->select)
 
7605
      {
 
7606
        tab->select->cond= tab->select_cond;
 
7607
      }
 
7608
    }
 
7609
  }
 
7610
  return;
 
7611
}
 
7612
 
 
7613
 
 
7614
 
 
7615
    /*
 
7616
      Determine if the set is already ordered for order_st BY, so it can
 
7617
      disable join cache because it will change the ordering of the results.
 
7618
      Code handles sort table that is at any location (not only first after
 
7619
      the const tables) despite the fact that it's currently prohibited.
 
7620
      We must disable join cache if the first non-const table alone is
 
7621
      ordered. If there is a temp table the ordering is done as a last
 
7622
      operation and doesn't prevent join cache usage.
 
7623
    */
 
7624
uint32_t make_join_orderinfo(JOIN *join)
 
7625
{
 
7626
  uint32_t i;
 
7627
  if (join->need_tmp)
 
7628
    return join->tables;
 
7629
 
 
7630
  for (i=join->const_tables ; i < join->tables ; i++)
 
7631
  {
 
7632
    JOIN_TAB *tab=join->join_tab+i;
 
7633
    Table *table=tab->table;
 
7634
    if ((table == join->sort_by_table &&
 
7635
         (!join->order || join->skip_sort_order)) ||
 
7636
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7637
    {
 
7638
      break;
 
7639
    }
 
7640
  }
 
7641
  return i;
 
7642
}
 
7643
 
 
7644
 
 
7645
/*
 
7646
  Plan refinement stage: do various set ups for the executioner
 
7647
 
 
7648
  SYNOPSIS
 
7649
    make_join_readinfo()
 
7650
      join           Join being processed
 
7651
      options        Join's options (checking for SELECT_DESCRIBE,
 
7652
                     SELECT_NO_JOIN_CACHE)
 
7653
      no_jbuf_after  Don't use join buffering after table with this number.
 
7654
 
 
7655
  DESCRIPTION
 
7656
    Plan refinement stage: do various set ups for the executioner
 
7657
      - set up use of join buffering
 
7658
      - push index conditions
 
7659
      - increment counters
 
7660
      - etc
 
7661
 
 
7662
  RETURN
 
7663
    false - OK
 
7664
    true  - Out of memory
 
7665
*/
 
7666
 
 
7667
static bool
 
7668
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7669
{
 
7670
  uint32_t i;
 
7671
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7672
  bool sorted= 1;
 
7673
 
 
7674
  for (i=join->const_tables ; i < join->tables ; i++)
 
7675
  {
 
7676
    JOIN_TAB *tab=join->join_tab+i;
 
7677
    Table *table=tab->table;
 
7678
    bool using_join_cache;
 
7679
    tab->read_record.table= table;
 
7680
    tab->read_record.file=table->file;
 
7681
    tab->next_select=sub_select;                /* normal select */
 
7682
    /*
 
7683
      TODO: don't always instruct first table's ref/range access method to
 
7684
      produce sorted output.
 
7685
    */
 
7686
    tab->sorted= sorted;
 
7687
    sorted= 0;                                  // only first must be sorted
 
7688
    if (tab->insideout_match_tab)
 
7689
    {
 
7690
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
 
7691
                                                         [tab->index].
 
7692
                                                         key_length)))
 
7693
        return true;
 
7694
    }
 
7695
    switch (tab->type) {
 
7696
    case JT_SYSTEM:                             // Only happens with left join
 
7697
      table->status=STATUS_NO_RECORD;
 
7698
      tab->read_first_record= join_read_system;
 
7699
      tab->read_record.read_record= join_no_more_records;
 
7700
      break;
 
7701
    case JT_CONST:                              // Only happens with left join
 
7702
      table->status=STATUS_NO_RECORD;
 
7703
      tab->read_first_record= join_read_const;
 
7704
      tab->read_record.read_record= join_no_more_records;
 
7705
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7706
          !table->no_keyread)
 
7707
      {
 
7708
        table->key_read=1;
 
7709
        table->file->extra(HA_EXTRA_KEYREAD);
 
7710
      }
 
7711
      break;
 
7712
    case JT_EQ_REF:
 
7713
      table->status=STATUS_NO_RECORD;
 
7714
      if (tab->select)
 
7715
      {
 
7716
        delete tab->select->quick;
 
7717
        tab->select->quick=0;
 
7718
      }
 
7719
      delete tab->quick;
 
7720
      tab->quick=0;
 
7721
      tab->read_first_record= join_read_key;
 
7722
      tab->read_record.read_record= join_no_more_records;
 
7723
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7724
          !table->no_keyread)
 
7725
      {
 
7726
        table->key_read=1;
 
7727
        table->file->extra(HA_EXTRA_KEYREAD);
 
7728
      }
 
7729
      else
 
7730
        push_index_cond(tab, tab->ref.key, true);
 
7731
      break;
 
7732
    case JT_REF_OR_NULL:
 
7733
    case JT_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
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7743
          !table->no_keyread)
 
7744
      {
 
7745
        table->key_read=1;
 
7746
        table->file->extra(HA_EXTRA_KEYREAD);
 
7747
      }
 
7748
      else
 
7749
        push_index_cond(tab, tab->ref.key, true);
 
7750
      if (tab->type == JT_REF)
 
7751
      {
 
7752
        tab->read_first_record= join_read_always_key;
 
7753
        tab->read_record.read_record= tab->insideout_match_tab?
 
7754
           join_read_next_same_diff : join_read_next_same;
 
7755
      }
 
7756
      else
 
7757
      {
 
7758
        tab->read_first_record= join_read_always_key_or_null;
 
7759
        tab->read_record.read_record= join_read_next_same_or_null;
 
7760
      }
 
7761
      break;
 
7762
    case JT_ALL:
 
7763
      /*
 
7764
        If previous table use cache
 
7765
        If the incoming data set is already sorted don't use cache.
 
7766
      */
 
7767
      table->status=STATUS_NO_RECORD;
 
7768
      using_join_cache= false;
 
7769
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7770
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7771
          !tab->insideout_match_tab)
 
7772
      {
 
7773
        if ((options & SELECT_DESCRIBE) ||
 
7774
            !join_init_cache(join->session,join->join_tab+join->const_tables,
 
7775
                             i-join->const_tables))
 
7776
        {
 
7777
          using_join_cache= true;
 
7778
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7779
        }
 
7780
      }
 
7781
      /* These init changes read_record */
 
7782
      if (tab->use_quick == 2)
 
7783
      {
 
7784
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7785
        tab->read_first_record= join_init_quick_read_record;
 
7786
        if (statistics)
 
7787
          status_var_increment(join->session->status_var.select_range_check_count);
 
7788
      }
 
7789
      else
 
7790
      {
 
7791
        tab->read_first_record= join_init_read_record;
 
7792
        if (i == join->const_tables)
 
7793
        {
 
7794
          if (tab->select && tab->select->quick)
 
7795
          {
 
7796
            if (statistics)
 
7797
              status_var_increment(join->session->status_var.select_range_count);
 
7798
          }
 
7799
          else
 
7800
          {
 
7801
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7802
            if (statistics)
 
7803
              status_var_increment(join->session->status_var.select_scan_count);
 
7804
          }
 
7805
        }
 
7806
        else
 
7807
        {
 
7808
          if (tab->select && tab->select->quick)
 
7809
          {
 
7810
            if (statistics)
 
7811
              status_var_increment(join->session->status_var.select_full_range_join_count);
 
7812
          }
 
7813
          else
 
7814
          {
 
7815
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7816
            if (statistics)
 
7817
              status_var_increment(join->session->status_var.select_full_join_count);
 
7818
          }
 
7819
        }
 
7820
        if (!table->no_keyread)
 
7821
        {
 
7822
          if (tab->select && tab->select->quick &&
 
7823
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7824
              table->covering_keys.is_set(tab->select->quick->index))
 
7825
          {
 
7826
            table->key_read=1;
 
7827
            table->file->extra(HA_EXTRA_KEYREAD);
 
7828
          }
 
7829
          else if (!table->covering_keys.is_clear_all() &&
 
7830
                   !(tab->select && tab->select->quick))
 
7831
          {                                     // Only read index tree
 
7832
            if (!tab->insideout_match_tab)
 
7833
            {
 
7834
              /*
 
7835
                See bug #26447: "Using the clustered index for a table scan
 
7836
                is always faster than using a secondary index".
 
7837
              */
 
7838
              if (table->s->primary_key != MAX_KEY &&
 
7839
                  table->file->primary_key_is_clustered())
 
7840
                tab->index= table->s->primary_key;
 
7841
              else
 
7842
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7843
            }
 
7844
            tab->read_first_record= join_read_first;
 
7845
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7846
          }
 
7847
        }
 
7848
        if (tab->select && tab->select->quick &&
 
7849
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7850
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7851
      }
 
7852
      break;
 
7853
    default:
 
7854
      break;                                    /* purecov: deadcode */
 
7855
    case JT_UNKNOWN:
 
7856
    case JT_MAYBE_REF:
 
7857
      abort();                                  /* purecov: deadcode */
 
7858
    }
 
7859
  }
 
7860
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7861
  return(false);
 
7862
}
 
7863
 
 
7864
 
 
7865
/**
 
7866
  Give error if we some tables are done with a full join.
 
7867
 
 
7868
  This is used by multi_table_update and multi_table_delete when running
 
7869
  in safe mode.
 
7870
 
 
7871
  @param join           Join condition
 
7872
 
 
7873
  @retval
 
7874
    0   ok
 
7875
  @retval
 
7876
    1   Error (full join used)
 
7877
*/
 
7878
 
 
7879
bool error_if_full_join(JOIN *join)
 
7880
{
 
7881
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7882
       tab < end;
 
7883
       tab++)
 
7884
  {
 
7885
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7886
    {
 
7887
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7888
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7889
      return(1);
 
7890
    }
 
7891
  }
 
7892
  return(0);
 
7893
}
 
7894
 
 
7895
 
 
7896
/**
 
7897
  cleanup JOIN_TAB.
 
7898
*/
 
7899
 
 
7900
void JOIN_TAB::cleanup()
 
7901
{
 
7902
  delete select;
 
7903
  select= 0;
 
7904
  delete quick;
 
7905
  quick= 0;
1227
7906
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
7907
    free(cache.buff);
1232
 
  }
1233
7908
  cache.buff= 0;
1234
7909
  limit= 0;
1235
7910
  if (table)
1237
7912
    if (table->key_read)
1238
7913
    {
1239
7914
      table->key_read= 0;
1240
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7915
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1241
7916
    }
1242
 
    table->cursor->ha_index_or_rnd_end();
 
7917
    table->file->ha_index_or_rnd_end();
1243
7918
    /*
1244
7919
      We need to reset this for next select
1245
7920
      (Tested in part_of_refkey)
1246
7921
    */
1247
7922
    table->reginfo.join_tab= 0;
1248
7923
  }
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:
 
7924
  end_read_record(&read_record);
 
7925
}
 
7926
 
 
7927
 
 
7928
/**
 
7929
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7930
  (table cursors), free quick selects.
 
7931
 
 
7932
    This function is called in the end of execution of a JOIN, before the used
 
7933
    tables are unlocked and closed.
 
7934
 
 
7935
    For a join that is resolved using a temporary table, the first sweep is
 
7936
    performed against actual tables and an intermediate result is inserted
 
7937
    into the temprorary table.
 
7938
    The last sweep is performed against the temporary table. Therefore,
 
7939
    the base tables and associated buffers used to fill the temporary table
 
7940
    are no longer needed, and this function is called to free them.
 
7941
 
 
7942
    For a join that is performed without a temporary table, this function
 
7943
    is called after all rows are sent, but before EOF packet is sent.
 
7944
 
 
7945
    For a simple SELECT with no subqueries this function performs a full
 
7946
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7947
    tables.
 
7948
 
 
7949
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7950
    do the full cleanup and need to do a partial cleanup only.
 
7951
    - If a JOIN is not the top level join, we must not unlock the tables
 
7952
    because the outer select may not have been evaluated yet, and we
 
7953
    can't unlock only selected tables of a query.
 
7954
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7955
    should not free quick selects and join buffers because they will be
 
7956
    needed for the next execution of the correlated subquery.
 
7957
    - However, if this is a JOIN for a [sub]select, which is not
 
7958
    a correlated subquery itself, but has subqueries, we can free it
 
7959
    fully and also free JOINs of all its subqueries. The exception
 
7960
    is a subquery in SELECT list, e.g: @n
 
7961
    SELECT a, (select cmax(b) from t1) group by c @n
 
7962
    This subquery will not be evaluated at first sweep and its value will
 
7963
    not be inserted into the temporary table. Instead, it's evaluated
 
7964
    when selecting from the temporary table. Therefore, it can't be freed
 
7965
    here even though it's not correlated.
 
7966
 
 
7967
  @todo
 
7968
    Unlock tables even if the join isn't top level select in the tree
 
7969
*/
 
7970
 
 
7971
void JOIN::join_free()
 
7972
{
 
7973
  SELECT_LEX_UNIT *tmp_unit;
 
7974
  SELECT_LEX *sl;
 
7975
  /*
 
7976
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7977
    free all tables.
 
7978
  */
 
7979
  bool full= (!select_lex->uncacheable && !session->lex->describe);
 
7980
  bool can_unlock= full;
 
7981
 
 
7982
  cleanup(full);
 
7983
 
 
7984
  for (tmp_unit= select_lex->first_inner_unit();
 
7985
       tmp_unit;
 
7986
       tmp_unit= tmp_unit->next_unit())
 
7987
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7988
    {
 
7989
      Item_subselect *subselect= sl->master_unit()->item;
 
7990
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7991
      /*
 
7992
        If this join is evaluated, we can fully clean it up and clean up all
 
7993
        its underlying joins even if they are correlated -- they will not be
 
7994
        used any more anyway.
 
7995
        If this join is not yet evaluated, we still must clean it up to
 
7996
        close its table cursors -- it may never get evaluated, as in case of
 
7997
        ... HAVING false OR a IN (SELECT ...))
 
7998
        but all table cursors must be closed before the unlock.
 
7999
      */
 
8000
      sl->cleanup_all_joins(full_local);
 
8001
      /* Can't unlock if at least one JOIN is still needed */
 
8002
      can_unlock= can_unlock && full_local;
 
8003
    }
 
8004
 
 
8005
  /*
 
8006
    We are not using tables anymore
 
8007
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8008
  */
 
8009
  if (can_unlock && lock && session->lock &&
 
8010
      !(select_options & SELECT_NO_UNLOCK) &&
 
8011
      !select_lex->subquery_in_having &&
 
8012
      (select_lex == (session->lex->unit.fake_select_lex ?
 
8013
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
8014
  {
 
8015
    /*
 
8016
      TODO: unlock tables even if the join isn't top level select in the
 
8017
      tree.
 
8018
    */
 
8019
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
 
8020
    lock= 0;
 
8021
  }
 
8022
 
 
8023
  return;
 
8024
}
 
8025
 
 
8026
 
 
8027
/**
 
8028
  Free resources of given join.
 
8029
 
 
8030
  @param fill   true if we should free all resources, call with full==1
 
8031
                should be last, before it this function can be called with
 
8032
                full==0
 
8033
 
 
8034
  @note
 
8035
    With subquery this function definitely will be called several times,
 
8036
    but even for simple query it can be called several times.
 
8037
*/
 
8038
 
 
8039
void JOIN::cleanup(bool full)
 
8040
{
 
8041
  if (table)
 
8042
  {
 
8043
    JOIN_TAB *tab,*end;
 
8044
    /*
 
8045
      Only a sorted table may be cached.  This sorted table is always the
 
8046
      first non const table in join->table
 
8047
    */
 
8048
    if (tables > const_tables) // Test for not-const tables
 
8049
    {
 
8050
      free_io_cache(table[const_tables]);
 
8051
      filesort_free_buffers(table[const_tables],full);
 
8052
    }
 
8053
 
 
8054
    if (full)
 
8055
    {
 
8056
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8057
        tab->cleanup();
 
8058
      table= 0;
 
8059
    }
 
8060
    else
 
8061
    {
 
8062
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8063
      {
 
8064
        if (tab->table)
 
8065
          tab->table->file->ha_index_or_rnd_end();
 
8066
      }
 
8067
    }
 
8068
    cleanup_sj_tmp_tables(this);//
 
8069
  }
 
8070
  /*
 
8071
    We are not using tables anymore
 
8072
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8073
  */
 
8074
  if (full)
 
8075
  {
 
8076
    if (tmp_join)
 
8077
      tmp_table_param.copy_field= 0;
 
8078
    group_fields.delete_elements();
 
8079
    /*
 
8080
      We can't call delete_elements() on copy_funcs as this will cause
 
8081
      problems in free_elements() as some of the elements are then deleted.
 
8082
    */
 
8083
    tmp_table_param.copy_funcs.empty();
 
8084
    /*
 
8085
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8086
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8087
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8088
      be removed in tmp_table_param.cleanup().
 
8089
    */
 
8090
    if (tmp_join &&
 
8091
        tmp_join != this &&
 
8092
        tmp_join->tmp_table_param.copy_field ==
 
8093
        tmp_table_param.copy_field)
 
8094
    {
 
8095
      tmp_join->tmp_table_param.copy_field=
 
8096
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8097
    }
 
8098
    tmp_table_param.cleanup();
 
8099
  }
 
8100
  return;
 
8101
}
 
8102
 
 
8103
 
 
8104
/**
 
8105
  Remove the following expressions from order_st BY and GROUP BY:
1264
8106
  Constant expressions @n
1265
8107
  Expression that only uses tables that are of type EQ_REF and the reference
1266
8108
  is in the order_st list or if all refereed tables are of the above type.
1267
8109
 
1268
8110
  In the following, the X field can be removed:
1269
8111
  @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
 
8112
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8113
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1272
8114
  @endcode
1273
8115
 
1274
8116
  These can't be optimized:
1275
8117
  @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
 
8118
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8119
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8120
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1279
8121
  @endcode
1280
8122
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8123
 
 
8124
static bool
 
8125
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1282
8126
{
1283
8127
  if (tab->cached_eq_ref_table)                 // If cached
1284
8128
    return tab->eq_ref_table;
1285
8129
  tab->cached_eq_ref_table=1;
1286
8130
  /* 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)
 
8131
  if (tab->type == JT_CONST && !tab->first_inner)
 
8132
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8133
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1290
8134
    return (tab->eq_ref_table=0);               // We must use this
1291
8135
  Item **ref_item=tab->ref.items;
1292
8136
  Item **end=ref_item+tab->ref.key_parts;
1297
8141
  {
1298
8142
    if (! (*ref_item)->const_item())
1299
8143
    {                                           // Not a const ref
1300
 
      Order *order;
 
8144
      order_st *order;
1301
8145
      for (order=start_order ; order ; order=order->next)
1302
8146
      {
1303
 
        if ((*ref_item)->eq(order->item[0],0))
1304
 
          break;
 
8147
        if ((*ref_item)->eq(order->item[0],0))
 
8148
          break;
1305
8149
      }
1306
8150
      if (order)
1307
8151
      {
1308
 
        found++;
1309
 
        assert(!(order->used & map));
1310
 
        order->used|=map;
1311
 
        continue;                               // Used in order_st BY
 
8152
        found++;
 
8153
        assert(!(order->used & map));
 
8154
        order->used|=map;
 
8155
        continue;                               // Used in order_st BY
1312
8156
      }
1313
8157
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1314
 
        return (tab->eq_ref_table= 0);
 
8158
        return (tab->eq_ref_table=0);
1315
8159
    }
1316
8160
  }
1317
8161
  /* Check that there was no reference to table before sort order */
1323
8167
      continue;
1324
8168
    }
1325
8169
    if (start_order->depend_map & map)
1326
 
      return (tab->eq_ref_table= 0);
1327
 
  }
1328
 
  return tab->eq_ref_table= 1;
1329
 
}
 
8170
      return (tab->eq_ref_table=0);
 
8171
  }
 
8172
  return tab->eq_ref_table=1;
 
8173
}
 
8174
 
 
8175
 
 
8176
static bool
 
8177
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8178
{
 
8179
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8180
  {
 
8181
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8182
      return 0;
 
8183
  }
 
8184
  return 1;
 
8185
}
 
8186
 
 
8187
 
 
8188
/** Update the dependency map for the tables. */
 
8189
 
 
8190
static void update_depend_map(JOIN *join)
 
8191
{
 
8192
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8193
 
 
8194
  for (; join_tab != end ; join_tab++)
 
8195
  {
 
8196
    TABLE_REF *ref= &join_tab->ref;
 
8197
    table_map depend_map=0;
 
8198
    Item **item=ref->items;
 
8199
    uint32_t i;
 
8200
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8201
      depend_map|=(*item)->used_tables();
 
8202
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8203
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8204
    for (JOIN_TAB **tab=join->map2table;
 
8205
         depend_map ;
 
8206
         tab++,depend_map>>=1 )
 
8207
    {
 
8208
      if (depend_map & 1)
 
8209
        ref->depend_map|=(*tab)->ref.depend_map;
 
8210
    }
 
8211
  }
 
8212
}
 
8213
 
 
8214
 
 
8215
/** Update the dependency map for the sort order. */
 
8216
 
 
8217
static void update_depend_map(JOIN *join, order_st *order)
 
8218
{
 
8219
  for (; order ; order=order->next)
 
8220
  {
 
8221
    table_map depend_map;
 
8222
    order->item[0]->update_used_tables();
 
8223
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8224
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8225
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8226
        && !order->item[0]->with_sum_func)
 
8227
    {
 
8228
      for (JOIN_TAB **tab=join->map2table;
 
8229
           depend_map ;
 
8230
           tab++, depend_map>>=1)
 
8231
      {
 
8232
        if (depend_map & 1)
 
8233
          order->depend_map|=(*tab)->ref.depend_map;
 
8234
      }
 
8235
    }
 
8236
  }
 
8237
}
 
8238
 
 
8239
 
 
8240
/**
 
8241
  Remove all constants and check if order_st only contains simple
 
8242
  expressions.
 
8243
 
 
8244
  simple_order is set to 1 if sort_order only uses fields from head table
 
8245
  and the head table is not a LEFT JOIN table.
 
8246
 
 
8247
  @param join                   Join handler
 
8248
  @param first_order            List of SORT or GROUP order
 
8249
  @param cond                   WHERE statement
 
8250
  @param change_list            Set to 1 if we should remove things from list.
 
8251
                               If this is not set, then only simple_order is
 
8252
                               calculated.
 
8253
  @param simple_order           Set to 1 if we are only using simple expressions
 
8254
 
 
8255
  @return
 
8256
    Returns new sort order
 
8257
*/
 
8258
 
 
8259
static order_st *
 
8260
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8261
             bool change_list, bool *simple_order)
 
8262
{
 
8263
  if (join->tables == join->const_tables)
 
8264
    return change_list ? 0 : first_order;               // No need to sort
 
8265
 
 
8266
  order_st *order,**prev_ptr;
 
8267
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8268
  table_map not_const_tables= ~join->const_table_map;
 
8269
  table_map ref;
 
8270
 
 
8271
  prev_ptr= &first_order;
 
8272
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8273
 
 
8274
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8275
 
 
8276
  update_depend_map(join, first_order);
 
8277
  for (order=first_order; order ; order=order->next)
 
8278
  {
 
8279
    table_map order_tables=order->item[0]->used_tables();
 
8280
    if (order->item[0]->with_sum_func)
 
8281
      *simple_order=0;                          // Must do a temp table to sort
 
8282
    else if (!(order_tables & not_const_tables))
 
8283
    {
 
8284
      if (order->item[0]->with_subselect)
 
8285
        order->item[0]->val_str(&order->item[0]->str_value);
 
8286
      continue;                                 // skip const item
 
8287
    }
 
8288
    else
 
8289
    {
 
8290
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8291
        *simple_order=0;
 
8292
      else
 
8293
      {
 
8294
        Item *comp_item=0;
 
8295
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8296
        {
 
8297
          continue;
 
8298
        }
 
8299
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8300
        {
 
8301
          if (!(order_tables & first_table) &&
 
8302
              only_eq_ref_tables(join,first_order, ref))
 
8303
          {
 
8304
            continue;
 
8305
          }
 
8306
          *simple_order=0;                      // Must do a temp table to sort
 
8307
        }
 
8308
      }
 
8309
    }
 
8310
    if (change_list)
 
8311
      *prev_ptr= order;                         // use this entry
 
8312
    prev_ptr= &order->next;
 
8313
  }
 
8314
  if (change_list)
 
8315
    *prev_ptr=0;
 
8316
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8317
    *simple_order=1;
 
8318
  return(first_order);
 
8319
}
 
8320
 
 
8321
 
 
8322
static int
 
8323
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8324
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8325
                 const char *info, Item *having)
 
8326
{
 
8327
  if (select_options & SELECT_DESCRIBE)
 
8328
  {
 
8329
    select_describe(join, false, false, false, info);
 
8330
    return(0);
 
8331
  }
 
8332
 
 
8333
  join->join_free();
 
8334
 
 
8335
  if (send_row)
 
8336
  {
 
8337
    for (TableList *table= tables; table; table= table->next_leaf)
 
8338
      mark_as_null_row(table->table);           // All fields are NULL
 
8339
    if (having && having->val_int() == 0)
 
8340
      send_row=0;
 
8341
  }
 
8342
  if (!(result->send_fields(fields,
 
8343
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8344
  {
 
8345
    if (send_row)
 
8346
    {
 
8347
      List_iterator_fast<Item> it(fields);
 
8348
      Item *item;
 
8349
      while ((item= it++))
 
8350
        item->no_rows_in_result();
 
8351
      result->send_data(fields);
 
8352
    }
 
8353
    result->send_eof();                         // Should be safe
 
8354
  }
 
8355
  /* Update results for FOUND_ROWS */
 
8356
  join->session->limit_found_rows= join->session->examined_row_count= 0;
 
8357
  return(0);
 
8358
}
 
8359
 
 
8360
/*
 
8361
  used only in JOIN::clear
 
8362
*/
 
8363
static void clear_tables(JOIN *join)
 
8364
{
 
8365
  /*
 
8366
    must clear only the non-const tables, as const tables
 
8367
    are not re-calculated.
 
8368
  */
 
8369
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8370
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8371
}
 
8372
 
 
8373
/*****************************************************************************
 
8374
  Make som simple condition optimization:
 
8375
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8376
  Remove all dummy tests 'item = item', 'const op const'.
 
8377
  Remove all 'item is NULL', when item can never be null!
 
8378
  item->marker should be 0 for all items on entry
 
8379
  Return in cond_value false if condition is impossible (1 = 2)
 
8380
*****************************************************************************/
 
8381
 
 
8382
class COND_CMP :public ilink {
 
8383
public:
 
8384
  static void *operator new(size_t size)
 
8385
  {
 
8386
    return (void*) sql_alloc((uint) size);
 
8387
  }
 
8388
  static void operator delete(void *, size_t)
 
8389
  { TRASH(ptr, size); }
 
8390
 
 
8391
  Item *and_level;
 
8392
  Item_func *cmp_func;
 
8393
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8394
};
 
8395
 
 
8396
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8397
template class I_List<COND_CMP>;
 
8398
template class I_List_iterator<COND_CMP>;
 
8399
#endif
 
8400
 
1330
8401
 
1331
8402
/**
1332
8403
  Find the multiple equality predicate containing a field.
1346
8417
    - Item_equal for the found multiple equality predicate if a success;
1347
8418
    - NULL otherwise.
1348
8419
*/
1349
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8420
 
 
8421
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8422
                            bool *inherited_fl)
1350
8423
{
1351
8424
  Item_equal *item= 0;
1352
8425
  bool in_upper_level= false;
1353
8426
  while (cond_equal)
1354
8427
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8428
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
8429
    while ((item= li++))
1357
8430
    {
1358
8431
      if (item->contains(field))
1367
8440
  return item;
1368
8441
}
1369
8442
 
 
8443
 
1370
8444
/**
1371
8445
  Check whether an equality can be used to build multiple equalities.
1372
8446
 
1448
8522
  @retval
1449
8523
    false   otherwise
1450
8524
*/
1451
 
static bool check_simple_equality(Item *left_item,
1452
 
                                  Item *right_item,
1453
 
                                  Item *item,
1454
 
                                  COND_EQUAL *cond_equal)
 
8525
 
 
8526
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8527
                                  Item *item, COND_EQUAL *cond_equal)
1455
8528
{
1456
8529
  if (left_item->type() == Item::FIELD_ITEM &&
1457
8530
      right_item->type() == Item::FIELD_ITEM &&
1517
8590
        /* Merge two multiple equalities forming a new one */
1518
8591
        left_item_equal->merge(right_item_equal);
1519
8592
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
8593
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
8594
        while ((li++) != right_item_equal) {};
1522
8595
        li.remove();
1523
8596
      }
1612
8685
  return false;
1613
8686
}
1614
8687
 
 
8688
 
1615
8689
/**
1616
8690
  Convert row equalities into a conjunction of regular equalities.
1617
8691
 
1637
8711
  @retval
1638
8712
    false   otherwise
1639
8713
*/
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)
 
8714
 
 
8715
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
 
8716
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
1645
8717
{
1646
8718
  uint32_t n= left_row->cols();
1647
8719
  for (uint32_t i= 0 ; i < n; i++)
1657
8729
                                       (Item_row *) right_item,
1658
8730
                                       cond_equal, eq_list);
1659
8731
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
8732
        session->lex->current_select->cond_count++;
1661
8733
    }
1662
8734
    else
1663
8735
    {
1664
8736
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
 
8737
      session->lex->current_select->cond_count++;
1666
8738
    }
1667
8739
 
1668
8740
    if (!is_converted)
1678
8750
  return true;
1679
8751
}
1680
8752
 
 
8753
 
1681
8754
/**
1682
8755
  Eliminate row equalities and form multiple equalities predicates.
1683
8756
 
1707
8780
           or, if the equality is neither a simple one nor a row equality,
1708
8781
           or, if the procedure fails by a fatal error.
1709
8782
*/
1710
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8783
 
 
8784
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
 
8785
                           List<Item> *eq_list)
1711
8786
{
1712
8787
  if (item->type() == Item::FUNC_ITEM &&
1713
8788
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1718
8793
    if (left_item->type() == Item::ROW_ITEM &&
1719
8794
        right_item->type() == Item::ROW_ITEM)
1720
8795
    {
1721
 
      session->getLex()->current_select->cond_count--;
 
8796
      session->lex->current_select->cond_count--;
1722
8797
      return check_row_equality(session,
1723
8798
                                (Item_row *) left_item,
1724
8799
                                (Item_row *) right_item,
1730
8805
  return false;
1731
8806
}
1732
8807
 
 
8808
 
1733
8809
/**
1734
8810
  Replace all equality predicates in a condition by multiple equality items.
1735
8811
 
1749
8825
    just an argument of a comparison predicate.
1750
8826
    The function also determines the maximum number of members in
1751
8827
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
8828
    session->lex->current_select->max_equal_elems.
1753
8829
 
1754
8830
  @note
1755
8831
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1793
8869
  @return
1794
8870
    pointer to the transformed condition
1795
8871
*/
1796
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8872
 
 
8873
static COND *build_equal_items_for_cond(Session *session, COND *cond,
 
8874
                                        COND_EQUAL *inherited)
1797
8875
{
1798
8876
  Item_equal *item_equal;
1799
8877
  COND_EQUAL cond_equal;
1806
8884
      Item_func::COND_AND_FUNC;
1807
8885
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
8886
 
1809
 
    List<Item>::iterator li(args->begin());
 
8887
    List_iterator<Item> li(*args);
1810
8888
    Item *item;
1811
8889
 
1812
8890
    if (and_level)
1828
8906
          li.remove();
1829
8907
      }
1830
8908
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8909
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
8910
      while ((item_equal= it++))
1833
8911
      {
1834
8912
        item_equal->fix_length_and_dec();
1835
8913
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8914
        set_if_bigger(session->lex->current_select->max_equal_elems,
1837
8915
                      item_equal->members());
1838
8916
      }
1839
8917
 
1844
8922
       Make replacement of equality predicates for lower levels
1845
8923
       of the condition expression.
1846
8924
    */
1847
 
    li= args->begin();
 
8925
    li.rewind();
1848
8926
    while ((item= li++))
1849
8927
    {
1850
8928
      Item *new_item;
1889
8967
        {
1890
8968
          item_equal->fix_length_and_dec();
1891
8969
          item_equal->update_used_tables();
1892
 
        }
 
8970
        }
1893
8971
        else
1894
8972
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8973
        set_if_bigger(session->lex->current_select->max_equal_elems,
1896
8974
                      item_equal->members());
1897
8975
        return item_equal;
1898
8976
      }
1901
8979
        /*
1902
8980
          Here a new AND level must be created. It can happen only
1903
8981
          when a row equality is processed as a standalone predicate.
1904
 
        */
 
8982
        */
1905
8983
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
8984
        and_cond->quick_fix_field();
1907
8985
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
8986
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
8987
        while ((item_equal= it++))
1910
8988
        {
1911
8989
          item_equal->fix_length_and_dec();
1912
8990
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
8991
          set_if_bigger(session->lex->current_select->max_equal_elems,
1914
8992
                        item_equal->members());
1915
8993
        }
1916
8994
        and_cond->cond_equal= cond_equal;
1935
9013
  return cond;
1936
9014
}
1937
9015
 
 
9016
 
1938
9017
/**
1939
9018
  Build multiple equalities for a condition and all on expressions that
1940
9019
  inherit these multiple equalities.
1989
9068
    can get more freedom in performing join operations.
1990
9069
    Althogh we don't use this property now, it probably makes sense to use
1991
9070
    it in the future.
1992
 
  @param session                      Thread Cursor
 
9071
  @param session                      Thread handler
1993
9072
  @param cond                condition to build the multiple equalities for
1994
9073
  @param inherited           path to all inherited multiple equality items
1995
9074
  @param join_list           list of join tables to which the condition
2000
9079
  @return
2001
9080
    pointer to the transformed condition containing multiple equalities
2002
9081
*/
 
9082
 
2003
9083
static COND *build_equal_items(Session *session, COND *cond,
2004
9084
                               COND_EQUAL *inherited,
2005
9085
                               List<TableList> *join_list,
2031
9111
  if (join_list)
2032
9112
  {
2033
9113
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
9114
    List_iterator<TableList> li(*join_list);
2035
9115
 
2036
9116
    while ((table= li++))
2037
9117
    {
2038
9118
      if (table->on_expr)
2039
9119
      {
2040
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2041
 
          &table->getNestedJoin()->join_list : NULL;
 
9120
        List<TableList> *nested_join_list= table->nested_join ?
 
9121
          &table->nested_join->join_list : NULL;
2042
9122
        /*
2043
9123
          We can modify table->on_expr because its old value will
2044
9124
          be restored before re-execution of PS/SP.
2053
9133
  return cond;
2054
9134
}
2055
9135
 
 
9136
 
2056
9137
/**
2057
9138
  Compare field items by table order in the execution plan.
2058
9139
 
2072
9153
  @retval
2073
9154
    0  otherwise
2074
9155
*/
 
9156
 
2075
9157
static int compare_fields_by_table_order(Item_field *field1,
2076
 
                                         Item_field *field2,
2077
 
                                         void *table_join_idx)
 
9158
                                  Item_field *field2,
 
9159
                                  void *table_join_idx)
2078
9160
{
2079
9161
  int cmp= 0;
2080
9162
  bool outer_ref= 0;
2090
9172
  }
2091
9173
  if (outer_ref)
2092
9174
    return cmp;
2093
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2094
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9175
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9176
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2095
9177
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2096
9178
}
2097
9179
 
 
9180
 
2098
9181
/**
2099
9182
  Generate minimal set of simple equalities equivalent to a multiple equality.
2100
9183
 
2134
9217
    a pointer to the simple generated equality, if success.
2135
9218
    - 0, otherwise.
2136
9219
*/
2137
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9220
 
 
9221
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9222
                                  Item_equal *item_equal)
2138
9223
{
2139
9224
  List<Item> eq_list;
2140
9225
  Item_func_eq *eq_item= 0;
2204
9289
  return cond;
2205
9290
}
2206
9291
 
 
9292
 
2207
9293
/**
2208
9294
  Substitute every field reference in a condition by the best equal field
2209
9295
  and eliminate all multiple equality predicates.
2231
9317
  @return
2232
9318
    The transformed condition
2233
9319
*/
2234
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9320
 
 
9321
static COND* substitute_for_best_equal_field(COND *cond,
 
9322
                                             COND_EQUAL *cond_equal,
 
9323
                                             void *table_join_idx)
2235
9324
{
2236
9325
  Item_equal *item_equal;
2237
9326
 
2246
9335
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
9336
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
9337
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9338
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2250
9339
      while ((item_equal= it++))
2251
9340
      {
2252
9341
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
9342
      }
2254
9343
    }
2255
9344
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
9345
    List_iterator<Item> li(*cond_list);
2257
9346
    Item *item;
2258
9347
    while ((item= li++))
2259
9348
    {
2269
9358
 
2270
9359
    if (and_level)
2271
9360
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
9361
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
9362
      while ((item_equal= it++))
2274
9363
      {
2275
9364
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2299
9388
  return cond;
2300
9389
}
2301
9390
 
 
9391
 
2302
9392
/**
2303
9393
  Check appearance of new constant items in multiple equalities
2304
9394
  of a condition after reading a constant table.
2311
9401
  @param cond       condition whose multiple equalities are to be checked
2312
9402
  @param table      constant table that has been read
2313
9403
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
9404
 
 
9405
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2315
9406
{
2316
9407
  if (!(cond->used_tables() & tab->table->map))
2317
9408
    return;
2319
9410
  if (cond->type() == Item::COND_ITEM)
2320
9411
  {
2321
9412
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
9413
    List_iterator_fast<Item> li(*cond_list);
2323
9414
    Item *item;
2324
9415
    while ((item= li++))
2325
9416
      update_const_equal_items(item, tab);
2338
9429
      while ((item_field= it++))
2339
9430
      {
2340
9431
        Field *field= item_field->field;
2341
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9432
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2342
9433
        key_map possible_keys= field->key_start;
2343
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2344
 
        stat[0].const_keys|= possible_keys;
 
9434
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9435
        stat[0].const_keys.merge(possible_keys);
2345
9436
 
2346
9437
        /*
2347
9438
          For each field in the multiple equality (for which we know that it
2348
9439
          is a constant) we have to find its corresponding key part, and set
2349
9440
          that key part in const_key_parts.
2350
9441
        */
2351
 
        if (possible_keys.any())
 
9442
        if (!possible_keys.is_clear_all())
2352
9443
        {
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 ==
 
9444
          Table *tab= field->table;
 
9445
          KEYUSE *use;
 
9446
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9447
            if (possible_keys.is_set(use->key) &&
 
9448
                tab->key_info[use->key].key_part[use->keypart].field ==
2358
9449
                field)
2359
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9450
              tab->const_key_parts[use->key]|= use->keypart_map;
2360
9451
        }
2361
9452
      }
2362
9453
    }
2363
9454
  }
2364
9455
}
2365
9456
 
 
9457
 
2366
9458
/*
2367
9459
  change field = field to field = const for each found field = const in the
2368
9460
  and_level
2369
9461
*/
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)
 
9462
 
 
9463
static void
 
9464
change_cond_ref_to_const(Session *session, I_List<COND_CMP> *save_list,
 
9465
                         Item *and_father, Item *cond,
 
9466
                         Item *field, Item *value)
2376
9467
{
2377
9468
  if (cond->type() == Item::COND_ITEM)
2378
9469
  {
2379
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9470
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9471
      Item_func::COND_AND_FUNC;
 
9472
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
9473
    Item *item;
2382
9474
    while ((item=li++))
2383
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
 
9475
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
 
9476
                               field, value);
2385
9477
    return;
2386
9478
  }
2387
9479
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
9492
       left_item->collation.collation == value->collation.collation))
2401
9493
  {
2402
9494
    Item *tmp=value->clone_item();
 
9495
    tmp->collation.set(right_item->collation);
 
9496
 
2403
9497
    if (tmp)
2404
9498
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
9499
      session->change_item_tree(args + 1, tmp);
2407
9500
      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())
 
9501
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9502
          && and_father != cond && !left_item->const_item())
2411
9503
      {
2412
 
        cond->marker=1;
2413
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9504
        cond->marker=1;
 
9505
        COND_CMP *tmp2;
 
9506
        if ((tmp2=new COND_CMP(and_father,func)))
 
9507
          save_list->push_back(tmp2);
2414
9508
      }
2415
9509
      func->set_cmp_func();
2416
9510
    }
2422
9516
            right_item->collation.collation == value->collation.collation))
2423
9517
  {
2424
9518
    Item *tmp= value->clone_item();
 
9519
    tmp->collation.set(left_item->collation);
 
9520
 
2425
9521
    if (tmp)
2426
9522
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
9523
      session->change_item_tree(args, tmp);
2429
9524
      value= tmp;
2430
9525
      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())
 
9526
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9527
          && and_father != cond && !right_item->const_item())
2434
9528
      {
2435
9529
        args[0]= args[1];                       // For easy check
2436
9530
        session->change_item_tree(args + 1, value);
2437
 
        cond->marker=1;
2438
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9531
        cond->marker=1;
 
9532
        COND_CMP *tmp2;
 
9533
        if ((tmp2=new COND_CMP(and_father,func)))
 
9534
          save_list->push_back(tmp2);
2439
9535
      }
2440
9536
      func->set_cmp_func();
2441
9537
    }
2450
9546
  @return
2451
9547
    new conditions
2452
9548
*/
2453
 
Item *remove_additional_cond(Item* conds)
 
9549
 
 
9550
static Item *remove_additional_cond(Item* conds)
2454
9551
{
2455
9552
  if (conds->name == in_additional_cond)
2456
9553
    return 0;
2457
9554
  if (conds->type() == Item::COND_ITEM)
2458
9555
  {
2459
9556
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
9557
    List_iterator<Item> li(*(cnd->argument_list()));
2461
9558
    Item *item;
2462
9559
    while ((item= li++))
2463
9560
    {
2473
9570
  return conds;
2474
9571
}
2475
9572
 
2476
 
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
2478
 
                                     COND *and_father, 
2479
 
                                     COND *cond)
 
9573
static void
 
9574
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
 
9575
                         COND *and_father, COND *cond)
2480
9576
{
2481
9577
  if (cond->type() == Item::COND_ITEM)
2482
9578
  {
2483
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
9579
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9580
      Item_func::COND_AND_FUNC;
 
9581
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
9582
    Item *item;
2486
 
    list<COND_CMP> save;
 
9583
    I_List<COND_CMP> save;
2487
9584
    while ((item=li++))
2488
9585
    {
2489
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9586
      propagate_cond_constants(session, &save,and_level ? cond : item, item);
2490
9587
    }
2491
9588
    if (and_level)
2492
 
    {
2493
 
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9589
    {                                           // Handle other found items
 
9590
      I_List_iterator<COND_CMP> cond_itr(save);
 
9591
      COND_CMP *cond_cmp;
 
9592
      while ((cond_cmp=cond_itr++))
2495
9593
      {
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
 
        }
 
9594
        Item **args= cond_cmp->cmp_func->arguments();
 
9595
        if (!args[0]->const_item())
 
9596
          change_cond_ref_to_const(session, &save,cond_cmp->and_level,
 
9597
                                   cond_cmp->and_level, args[0], args[1]);
2502
9598
      }
2503
9599
    }
2504
9600
  }
2505
9601
  else if (and_father != cond && !cond->marker)         // In a AND group
2506
9602
  {
2507
9603
    if (cond->type() == Item::FUNC_ITEM &&
2508
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2509
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9604
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9605
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2510
9606
    {
2511
9607
      Item_func_eq *func=(Item_func_eq*) cond;
2512
9608
      Item **args= func->arguments();
2515
9611
      if (!(left_const && right_const) &&
2516
9612
          args[0]->result_type() == args[1]->result_type())
2517
9613
      {
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
 
}
 
9614
        if (right_const)
 
9615
        {
 
9616
          resolve_const_item(session, &args[1], args[0]);
 
9617
          func->update_used_tables();
 
9618
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9619
                                   args[0], args[1]);
 
9620
        }
 
9621
        else if (left_const)
 
9622
        {
 
9623
          resolve_const_item(session, &args[0], args[1]);
 
9624
          func->update_used_tables();
 
9625
          change_cond_ref_to_const(session, save_list, and_father, and_father,
 
9626
                                   args[1], args[0]);
 
9627
        }
 
9628
      }
 
9629
    }
 
9630
  }
 
9631
}
 
9632
 
 
9633
 
 
9634
/**
 
9635
  Simplify joins replacing outer joins by inner joins whenever it's
 
9636
  possible.
 
9637
 
 
9638
    The function, during a retrieval of join_list,  eliminates those
 
9639
    outer joins that can be converted into inner join, possibly nested.
 
9640
    It also moves the on expressions for the converted outer joins
 
9641
    and from inner joins to conds.
 
9642
    The function also calculates some attributes for nested joins:
 
9643
    - used_tables
 
9644
    - not_null_tables
 
9645
    - dep_tables.
 
9646
    - on_expr_dep_tables
 
9647
    The first two attributes are used to test whether an outer join can
 
9648
    be substituted for an inner join. The third attribute represents the
 
9649
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9650
    on table t1, then in any evaluated execution plan table access to
 
9651
    table t2 must precede access to table t2. This relation is used also
 
9652
    to check whether the query contains  invalid cross-references.
 
9653
    The forth attribute is an auxiliary one and is used to calculate
 
9654
    dep_tables.
 
9655
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9656
    execution plan, the dependencies required by the straight join
 
9657
    modifiers are reflected in this attribute as well.
 
9658
    The function also removes all braces that can be removed from the join
 
9659
    expression without changing its meaning.
 
9660
 
 
9661
  @note
 
9662
    An outer join can be replaced by an inner join if the where condition
 
9663
    or the on expression for an embedding nested join contains a conjunctive
 
9664
    predicate rejecting null values for some attribute of the inner tables.
 
9665
 
 
9666
    E.g. in the query:
 
9667
    @code
 
9668
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9669
    @endcode
 
9670
    the predicate t2.b < 5 rejects nulls.
 
9671
    The query is converted first to:
 
9672
    @code
 
9673
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9674
    @endcode
 
9675
    then to the equivalent form:
 
9676
    @code
 
9677
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9678
    @endcode
 
9679
 
 
9680
 
 
9681
    Similarly the following query:
 
9682
    @code
 
9683
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9684
        WHERE t2.c < 5
 
9685
    @endcode
 
9686
    is converted to:
 
9687
    @code
 
9688
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
 
9689
 
 
9690
    @endcode
 
9691
 
 
9692
    One conversion might trigger another:
 
9693
    @code
 
9694
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9695
                       LEFT JOIN t3 ON t3.b=t2.b
 
9696
        WHERE t3 IS NOT NULL =>
 
9697
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9698
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
 
9699
      SELECT * FROM t1, t2, t3
 
9700
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9701
  @endcode
 
9702
 
 
9703
    The function removes all unnecessary braces from the expression
 
9704
    produced by the conversions.
 
9705
    E.g.
 
9706
    @code
 
9707
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9708
    @endcode
 
9709
    finally is converted to:
 
9710
    @code
 
9711
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9712
 
 
9713
    @endcode
 
9714
 
 
9715
 
 
9716
    It also will remove braces from the following queries:
 
9717
    @code
 
9718
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9719
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9720
    @endcode
 
9721
 
 
9722
    The benefit of this simplification procedure is that it might return
 
9723
    a query for which the optimizer can evaluate execution plan with more
 
9724
    join orders. With a left join operation the optimizer does not
 
9725
    consider any plan where one of the inner tables is before some of outer
 
9726
    tables.
 
9727
 
 
9728
  IMPLEMENTATION
 
9729
    The function is implemented by a recursive procedure.  On the recursive
 
9730
    ascent all attributes are calculated, all outer joins that can be
 
9731
    converted are replaced and then all unnecessary braces are removed.
 
9732
    As join list contains join tables in the reverse order sequential
 
9733
    elimination of outer joins does not require extra recursive calls.
 
9734
 
 
9735
  SEMI-JOIN NOTES
 
9736
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9737
    an "ancestor" semi-join nest)
 
9738
 
 
9739
  EXAMPLES
 
9740
    Here is an example of a join query with invalid cross references:
 
9741
    @code
 
9742
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
 
9743
    @endcode
 
9744
 
 
9745
  @param join        reference to the query info
 
9746
  @param join_list   list representation of the join to be converted
 
9747
  @param conds       conditions to add on expressions for converted joins
 
9748
  @param top         true <=> conds is the where condition
 
9749
 
 
9750
  @return
 
9751
    - The new condition, if success
 
9752
    - 0, otherwise
 
9753
*/
 
9754
 
 
9755
static COND *
 
9756
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9757
               bool in_sj)
 
9758
{
 
9759
  TableList *table;
 
9760
  nested_join_st *nested_join;
 
9761
  TableList *prev_table= 0;
 
9762
  List_iterator<TableList> li(*join_list);
 
9763
 
 
9764
  /*
 
9765
    Try to simplify join operations from join_list.
 
9766
    The most outer join operation is checked for conversion first.
 
9767
  */
 
9768
  while ((table= li++))
 
9769
  {
 
9770
    table_map used_tables;
 
9771
    table_map not_null_tables= (table_map) 0;
 
9772
 
 
9773
    if ((nested_join= table->nested_join))
 
9774
    {
 
9775
      /*
 
9776
         If the element of join_list is a nested join apply
 
9777
         the procedure to its nested join list first.
 
9778
      */
 
9779
      if (table->on_expr)
 
9780
      {
 
9781
        Item *expr= table->on_expr;
 
9782
        /*
 
9783
           If an on expression E is attached to the table,
 
9784
           check all null rejected predicates in this expression.
 
9785
           If such a predicate over an attribute belonging to
 
9786
           an inner table of an embedded outer join is found,
 
9787
           the outer join is converted to an inner join and
 
9788
           the corresponding on expression is added to E.
 
9789
        */
 
9790
        expr= simplify_joins(join, &nested_join->join_list,
 
9791
                             expr, false, in_sj || table->sj_on_expr);
 
9792
 
 
9793
        if (!table->prep_on_expr || expr != table->on_expr)
 
9794
        {
 
9795
          assert(expr);
 
9796
 
 
9797
          table->on_expr= expr;
 
9798
          table->prep_on_expr= expr->copy_andor_structure(join->session);
 
9799
        }
 
9800
      }
 
9801
      nested_join->used_tables= (table_map) 0;
 
9802
      nested_join->not_null_tables=(table_map) 0;
 
9803
      conds= simplify_joins(join, &nested_join->join_list, conds, top,
 
9804
                            in_sj || table->sj_on_expr);
 
9805
      used_tables= nested_join->used_tables;
 
9806
      not_null_tables= nested_join->not_null_tables;
 
9807
    }
 
9808
    else
 
9809
    {
 
9810
      if (!table->prep_on_expr)
 
9811
        table->prep_on_expr= table->on_expr;
 
9812
      used_tables= table->table->map;
 
9813
      if (conds)
 
9814
        not_null_tables= conds->not_null_tables();
 
9815
    }
 
9816
 
 
9817
    if (table->embedding)
 
9818
    {
 
9819
      table->embedding->nested_join->used_tables|= used_tables;
 
9820
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9821
    }
 
9822
 
 
9823
    if (!table->outer_join || (used_tables & not_null_tables))
 
9824
    {
 
9825
      /*
 
9826
        For some of the inner tables there are conjunctive predicates
 
9827
        that reject nulls => the outer join can be replaced by an inner join.
 
9828
      */
 
9829
      table->outer_join= 0;
 
9830
      if (table->on_expr)
 
9831
      {
 
9832
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9833
        if (conds)
 
9834
        {
 
9835
          conds= and_conds(conds, table->on_expr);
 
9836
          conds->top_level_item();
 
9837
          /* conds is always a new item as both cond and on_expr existed */
 
9838
          assert(!conds->fixed);
 
9839
          conds->fix_fields(join->session, &conds);
 
9840
        }
 
9841
        else
 
9842
          conds= table->on_expr;
 
9843
        table->prep_on_expr= table->on_expr= 0;
 
9844
      }
 
9845
    }
 
9846
 
 
9847
    if (!top)
 
9848
      continue;
 
9849
 
 
9850
    /*
 
9851
      Only inner tables of non-convertible outer joins
 
9852
      remain with on_expr.
 
9853
    */
 
9854
    if (table->on_expr)
 
9855
    {
 
9856
      table->dep_tables|= table->on_expr->used_tables();
 
9857
      if (table->embedding)
 
9858
      {
 
9859
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
 
9860
        /*
 
9861
           Embedding table depends on tables used
 
9862
           in embedded on expressions.
 
9863
        */
 
9864
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9865
      }
 
9866
      else
 
9867
        table->dep_tables&= ~table->table->map;
 
9868
    }
 
9869
 
 
9870
    if (prev_table)
 
9871
    {
 
9872
      /* The order of tables is reverse: prev_table follows table */
 
9873
      if (prev_table->straight)
 
9874
        prev_table->dep_tables|= used_tables;
 
9875
      if (prev_table->on_expr)
 
9876
      {
 
9877
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9878
        table_map prev_used_tables= prev_table->nested_join ?
 
9879
                                    prev_table->nested_join->used_tables :
 
9880
                                    prev_table->table->map;
 
9881
        /*
 
9882
          If on expression contains only references to inner tables
 
9883
          we still make the inner tables dependent on the outer tables.
 
9884
          It would be enough to set dependency only on one outer table
 
9885
          for them. Yet this is really a rare case.
 
9886
        */
 
9887
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9888
          prev_table->dep_tables|= used_tables;
 
9889
      }
 
9890
    }
 
9891
    prev_table= table;
 
9892
  }
 
9893
 
 
9894
  /*
 
9895
    Flatten nested joins that can be flattened.
 
9896
    no ON expression and not a semi-join => can be flattened.
 
9897
  */
 
9898
  li.rewind();
 
9899
  while ((table= li++))
 
9900
  {
 
9901
    nested_join= table->nested_join;
 
9902
    if (table->sj_on_expr && !in_sj)
 
9903
    {
 
9904
       /*
 
9905
         If this is a semi-join that is not contained within another semi-join,
 
9906
         leave it intact (otherwise it is flattened)
 
9907
       */
 
9908
      join->select_lex->sj_nests.push_back(table);
 
9909
    }
 
9910
    else if (nested_join && !table->on_expr)
 
9911
    {
 
9912
      TableList *tbl;
 
9913
      List_iterator<TableList> it(nested_join->join_list);
 
9914
      while ((tbl= it++))
 
9915
      {
 
9916
        tbl->embedding= table->embedding;
 
9917
        tbl->join_list= table->join_list;
 
9918
      }
 
9919
      li.replace(nested_join->join_list);
 
9920
    }
 
9921
  }
 
9922
  return(conds);
 
9923
}
 
9924
 
 
9925
 
 
9926
/**
 
9927
  Assign each nested join structure a bit in nested_join_map.
 
9928
 
 
9929
    Assign each nested join structure (except "confluent" ones - those that
 
9930
    embed only one element) a bit in nested_join_map.
 
9931
 
 
9932
  @param join          Join being processed
 
9933
  @param join_list     List of tables
 
9934
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9935
                       call
 
9936
 
 
9937
  @note
 
9938
    This function is called after simplify_joins(), when there are no
 
9939
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9940
    we will not run out of bits in nested_join_map.
 
9941
 
 
9942
  @return
 
9943
    First unused bit in nested_join_map after the call.
 
9944
*/
 
9945
 
 
9946
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
9947
                                          uint32_t first_unused)
 
9948
{
 
9949
  List_iterator<TableList> li(*join_list);
 
9950
  TableList *table;
 
9951
  while ((table= li++))
 
9952
  {
 
9953
    nested_join_st *nested_join;
 
9954
    if ((nested_join= table->nested_join))
 
9955
    {
 
9956
      /*
 
9957
        It is guaranteed by simplify_joins() function that a nested join
 
9958
        that has only one child is either
 
9959
         - a single-table view (the child is the underlying table), or
 
9960
         - a single-table semi-join nest
 
9961
 
 
9962
        We don't assign bits to such sj-nests because
 
9963
        1. it is redundant (a "sequence" of one table cannot be interleaved
 
9964
            with anything)
 
9965
        2. we could run out bits in nested_join_map otherwise.
 
9966
      */
 
9967
      if (nested_join->join_list.elements != 1)
 
9968
      {
 
9969
        /* Don't assign bits to sj-nests */
 
9970
        if (table->on_expr)
 
9971
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9972
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9973
                                                    first_unused);
 
9974
      }
 
9975
    }
 
9976
  }
 
9977
  return(first_unused);
 
9978
}
 
9979
 
 
9980
 
 
9981
/**
 
9982
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
9983
 
 
9984
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
9985
    the passed join_list.
 
9986
 
 
9987
  @param join_list  List of nested joins to process. It may also contain base
 
9988
                    tables which will be ignored.
 
9989
*/
 
9990
 
 
9991
static void reset_nj_counters(List<TableList> *join_list)
 
9992
{
 
9993
  List_iterator<TableList> li(*join_list);
 
9994
  TableList *table;
 
9995
  while ((table= li++))
 
9996
  {
 
9997
    nested_join_st *nested_join;
 
9998
    if ((nested_join= table->nested_join))
 
9999
    {
 
10000
      nested_join->counter_= 0;
 
10001
      reset_nj_counters(&nested_join->join_list);
 
10002
    }
 
10003
  }
 
10004
  return;
 
10005
}
 
10006
 
2536
10007
 
2537
10008
/**
2538
10009
  Check interleaving with an inner tables of an outer join for
2610
10081
         position:
2611
10082
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
10083
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
10084
          2. {each nested_join_st structure not simplified away}->counter - number
2614
10085
             of this nested join's children that have already been added to to
2615
10086
             the partial join order.
2616
10087
  @endverbatim
2617
10088
 
2618
10089
  @param join       Join being processed
 
10090
  @param last_tab   Last table in current partial join order (this function is
 
10091
                    not called for empty partial join orders)
2619
10092
  @param next_tab   Table we're going to extend the current partial join with
2620
10093
 
2621
10094
  @retval
2624
10097
  @retval
2625
10098
    true   Requested join order extension not allowed.
2626
10099
*/
2627
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10100
 
 
10101
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2628
10102
{
2629
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2630
 
  Join *join= next_tab->join;
 
10103
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10104
  JOIN *join= last_tab->join;
2631
10105
 
2632
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10106
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2633
10107
  {
2634
10108
    /*
2635
10109
      next_tab is outside of the "pair of brackets" we're currently in.
2642
10116
    Do update counters for "pairs of brackets" that we've left (marked as
2643
10117
    X,Y,Z in the above picture)
2644
10118
  */
2645
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10119
  for (;next_emb; next_emb= next_emb->embedding)
2646
10120
  {
2647
 
    next_emb->getNestedJoin()->counter_++;
2648
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10121
    next_emb->nested_join->counter_++;
 
10122
    if (next_emb->nested_join->counter_ == 1)
2649
10123
    {
2650
10124
      /*
2651
10125
        next_emb is the first table inside a nested join we've "entered". In
2652
10126
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2653
10127
        X bracket might have Y pair bracket.
2654
10128
      */
2655
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10129
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2656
10130
    }
2657
10131
 
2658
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2659
 
        next_emb->getNestedJoin()->counter_)
 
10132
    if (next_emb->nested_join->join_list.elements !=
 
10133
        next_emb->nested_join->counter_)
2660
10134
      break;
2661
10135
 
2662
10136
    /*
2663
10137
      We're currently at Y or Z-bracket as depicted in the above picture.
2664
10138
      Mark that we've left it and continue walking up the brackets hierarchy.
2665
10139
    */
2666
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10140
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
10141
  }
2668
10142
  return false;
2669
10143
}
2670
10144
 
2671
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
 
10145
 
 
10146
/**
 
10147
  Nested joins perspective: Remove the last table from the join order.
 
10148
 
 
10149
    Remove the last table from the partial join order and update the nested
 
10150
    joins counters and join->cur_embedding_map. It is ok to call this
 
10151
    function for the first table in join order (for which
 
10152
    check_interleaving_with_nj has not been called)
 
10153
 
 
10154
  @param last  join table to remove, it is assumed to be the last in current
 
10155
               partial join order.
 
10156
*/
 
10157
 
 
10158
static void restore_prev_nj_state(JOIN_TAB *last)
 
10159
{
 
10160
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10161
  JOIN *join= last->join;
 
10162
  while (last_emb)
 
10163
  {
 
10164
    if (last_emb->on_expr)
 
10165
    {
 
10166
      if (!(--last_emb->nested_join->counter_))
 
10167
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10168
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10169
               last_emb->nested_join->counter_)
 
10170
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10171
      else
 
10172
        break;
 
10173
    }
 
10174
    last_emb= last_emb->embedding;
 
10175
  }
 
10176
}
 
10177
 
 
10178
 
 
10179
 
 
10180
static
 
10181
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10182
{
 
10183
  TableList *emb_sj_nest;
 
10184
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10185
  {
 
10186
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10187
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10188
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10189
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10190
  }
 
10191
}
 
10192
 
 
10193
 
 
10194
/*
 
10195
  we assume remaining_tables doesnt contain @tab.
 
10196
*/
 
10197
 
 
10198
static void restore_prev_sj_state(const table_map remaining_tables,
 
10199
                                  const JOIN_TAB *tab)
 
10200
{
 
10201
  TableList *emb_sj_nest;
 
10202
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10203
  {
 
10204
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10205
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) ==
 
10206
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10207
    {
 
10208
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10209
    }
 
10210
  }
 
10211
}
 
10212
 
 
10213
 
 
10214
static COND *
 
10215
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10216
              Item::cond_result *cond_value)
2672
10217
{
2673
10218
  Session *session= join->session;
2674
10219
 
2688
10233
                             &join->cond_equal);
2689
10234
 
2690
10235
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
2692
 
    propagate_cond_constants(session, temp, conds, conds);
 
10236
    propagate_cond_constants(session, (I_List<COND_CMP> *) 0, conds, conds);
2693
10237
    /*
2694
10238
      Remove all instances of item == item
2695
10239
      Remove all and-levels where CONST item != CONST item
2699
10243
  return(conds);
2700
10244
}
2701
10245
 
 
10246
 
2702
10247
/**
2703
10248
  Remove const and eq items.
2704
10249
 
2709
10254
    - COND_TRUE   : always true ( 1 = 1 )
2710
10255
    - COND_FALSE  : always false        ( 1 = 2 )
2711
10256
*/
2712
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10257
 
 
10258
COND *
 
10259
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
2713
10260
{
2714
10261
  if (cond->type() == Item::COND_ITEM)
2715
10262
  {
2716
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
 
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10263
    bool and_level= ((Item_cond*) cond)->functype()
 
10264
      == Item_func::COND_AND_FUNC;
 
10265
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
10266
    Item::cond_result tmp_cond_value;
2720
 
    bool should_fix_fields= false;
 
10267
    bool should_fix_fields=0;
2721
10268
 
2722
 
    *cond_value= Item::COND_UNDEF;
 
10269
    *cond_value=Item::COND_UNDEF;
2723
10270
    Item *item;
2724
 
    while ((item= li++))
 
10271
    while ((item=li++))
2725
10272
    {
2726
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2727
 
      if (! new_item)
2728
 
              li.remove();
 
10273
      Item *new_item=remove_eq_conds(session, item, &tmp_cond_value);
 
10274
      if (!new_item)
 
10275
        li.remove();
2729
10276
      else if (item != new_item)
2730
10277
      {
2731
 
        li.replace(new_item);
2732
 
        should_fix_fields= true;
 
10278
        li.replace(new_item);
 
10279
        should_fix_fields=1;
2733
10280
      }
2734
10281
      if (*cond_value == Item::COND_UNDEF)
2735
 
              *cond_value= tmp_cond_value;
2736
 
 
2737
 
      switch (tmp_cond_value) 
2738
 
      {
2739
 
        case Item::COND_OK:                     /* Not true or false */
2740
 
          if (and_level || (*cond_value == Item::COND_FALSE))
2741
 
            *cond_value= tmp_cond_value;
2742
 
          break;
2743
 
        case Item::COND_FALSE:
2744
 
          if (and_level)
2745
 
          {
2746
 
            *cond_value= tmp_cond_value;
2747
 
            return (COND *) NULL;                       /* Always false */
2748
 
          }
2749
 
          break;
2750
 
        case Item::COND_TRUE:
2751
 
          if (! and_level)
2752
 
          {
2753
 
            *cond_value= tmp_cond_value;
2754
 
            return (COND *) NULL;                       /* Always true */
2755
 
          }
2756
 
          break;
2757
 
        case Item::COND_UNDEF:                  /* Impossible */
2758
 
          break;
 
10282
        *cond_value=tmp_cond_value;
 
10283
      switch (tmp_cond_value) {
 
10284
      case Item::COND_OK:                       // Not true or false
 
10285
        if (and_level || *cond_value == Item::COND_FALSE)
 
10286
          *cond_value=tmp_cond_value;
 
10287
        break;
 
10288
      case Item::COND_FALSE:
 
10289
        if (and_level)
 
10290
        {
 
10291
          *cond_value=tmp_cond_value;
 
10292
          return (COND*) 0;                     // Always false
 
10293
        }
 
10294
        break;
 
10295
      case Item::COND_TRUE:
 
10296
        if (!and_level)
 
10297
        {
 
10298
          *cond_value= tmp_cond_value;
 
10299
          return (COND*) 0;                     // Always true
 
10300
        }
 
10301
        break;
 
10302
      case Item::COND_UNDEF:                    // Impossible
 
10303
        break; /* purecov: deadcode */
2759
10304
      }
2760
10305
    }
2761
 
 
2762
10306
    if (should_fix_fields)
2763
10307
      cond->update_used_tables();
2764
10308
 
2765
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2766
 
      return (COND*) NULL;
2767
 
 
 
10309
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10310
        *cond_value != Item::COND_OK)
 
10311
      return (COND*) 0;
2768
10312
    if (((Item_cond*) cond)->argument_list()->elements == 1)
2769
 
    {                                           
2770
 
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
10313
    {                                           // Remove list
2771
10314
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
10315
      ((Item_cond*) cond)->argument_list()->empty();
2773
10316
      return item;
2774
10317
    }
2775
10318
  }
2776
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10319
  else if (cond->type() == Item::FUNC_ITEM &&
 
10320
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2777
10321
  {
2778
10322
    /*
2779
10323
      Handles this special case for some ODBC applications:
2785
10329
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2786
10330
    */
2787
10331
 
2788
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10332
    Item_func_isnull *func=(Item_func_isnull*) cond;
2789
10333
    Item **args= func->arguments();
2790
10334
    if (args[0]->type() == Item::FIELD_ITEM)
2791
10335
    {
2792
 
      Field *field= ((Item_field*) args[0])->field;
2793
 
      if (field->flags & AUTO_INCREMENT_FLAG 
2794
 
          && ! field->getTable()->maybe_null 
2795
 
          && session->options & OPTION_AUTO_IS_NULL
2796
 
          && (
2797
 
            session->first_successful_insert_id_in_prev_stmt > 0 
2798
 
            && session->substitute_null_with_insert_id
2799
 
            )
2800
 
          )
 
10336
      Field *field=((Item_field*) args[0])->field;
 
10337
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10338
          (session->options & OPTION_AUTO_IS_NULL) &&
 
10339
          (session->first_successful_insert_id_in_prev_stmt > 0 &&
 
10340
           session->substitute_null_with_insert_id))
2801
10341
      {
2802
 
        COND *new_cond;
2803
 
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
2804
 
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
2805
 
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
2806
 
        {
2807
 
          cond= new_cond;
 
10342
        COND *new_cond;
 
10343
        if ((new_cond= new Item_func_eq(args[0],
 
10344
                                        new Item_int("last_insert_id()",
 
10345
                                                     session->read_first_successful_insert_id_in_prev_stmt(),
 
10346
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10347
        {
 
10348
          cond=new_cond;
2808
10349
          /*
2809
10350
            Item_func_eq can't be fixed after creation so we do not check
2810
10351
            cond->fixed, also it do not need tables so we use 0 as second
2811
10352
            argument.
2812
10353
          */
2813
 
          cond->fix_fields(session, &cond);
2814
 
        }
 
10354
          cond->fix_fields(session, &cond);
 
10355
        }
2815
10356
        /*
2816
10357
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2817
10358
          clear for next row
2818
10359
        */
2819
10360
        session->substitute_null_with_insert_id= false;
2820
10361
      }
2821
 
#ifdef NOTDEFINED
2822
10362
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
2823
 
      else if (
2824
 
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
2825
 
          && (field->flags & NOT_NULL_FLAG) 
2826
 
          && ! field->table->maybe_null)
 
10363
      else if (((field->type() == DRIZZLE_TYPE_DATE) ||
 
10364
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10365
                (field->flags & NOT_NULL_FLAG) &&
 
10366
               !field->table->maybe_null)
2827
10367
      {
2828
 
        COND *new_cond;
2829
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
2830
 
        {
2831
 
          cond= new_cond;
 
10368
        COND *new_cond;
 
10369
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10370
        {
 
10371
          cond=new_cond;
2832
10372
          /*
2833
10373
            Item_func_eq can't be fixed after creation so we do not check
2834
10374
            cond->fixed, also it do not need tables so we use 0 as second
2835
10375
            argument.
2836
10376
          */
2837
 
          cond->fix_fields(session, &cond);
2838
 
        }
 
10377
          cond->fix_fields(session, &cond);
 
10378
        }
2839
10379
      }
2840
 
#endif /* NOTDEFINED */
2841
10380
    }
2842
10381
    if (cond->const_item())
2843
10382
    {
2844
10383
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2845
 
      return (COND *) NULL;
 
10384
      return (COND*) 0;
2846
10385
    }
2847
10386
  }
2848
10387
  else if (cond->const_item() && !cond->is_expensive())
2849
10388
  /*
2850
 
    @todo
 
10389
    TODO:
2851
10390
    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
 
 
 
10391
    materialized IN because it is created later than this phase, and cannot be
 
10392
    evaluated at this point.
 
10393
    The condition should be something as (need to fix member access):
 
10394
      !(cond->type() == Item::FUNC_ITEM &&
 
10395
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10396
        ((Item_in_optimizer*)cond)->is_expensive()))
2858
10397
  */
2859
10398
  {
2860
10399
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2861
 
    return (COND *) NULL;
 
10400
    return (COND*) 0;
2862
10401
  }
2863
10402
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2864
 
  {                                             
2865
 
    /* boolan compare function */
 
10403
  {                                             // boolan compare function
2866
10404
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2867
10405
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2868
10406
    if (left_item->eq(right_item,1))
2869
10407
    {
2870
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2871
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10408
      if (!left_item->maybe_null ||
 
10409
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10410
        return (COND*) 0;                       // Compare of identical items
2872
10411
    }
2873
10412
  }
2874
 
  *cond_value= Item::COND_OK;
2875
 
  return cond;                                  /* Point at next and return into recursion */
 
10413
  *cond_value=Item::COND_OK;
 
10414
  return cond;                                  // Point at next and level
2876
10415
}
2877
10416
 
2878
10417
/*
2898
10437
    true    can be used
2899
10438
    false   cannot be used
2900
10439
*/
2901
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10440
static bool
 
10441
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2902
10442
{
2903
10443
  return r->const_item() &&
2904
10444
    /* elements must be compared as dates */
2913
10453
/**
2914
10454
  Return true if the item is a const value in all the WHERE clause.
2915
10455
*/
2916
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10456
 
 
10457
static bool
 
10458
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2917
10459
{
2918
10460
  if (cond->type() == Item::COND_ITEM)
2919
10461
  {
2920
10462
    bool and_level= (((Item_cond*) cond)->functype()
2921
10463
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
10464
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
10465
    Item *item;
2924
10466
    while ((item=li++))
2925
10467
    {
2926
10468
      bool res=const_expression_in_where(item, comp_item, const_item);
2927
10469
      if (res)                                  // Is a const value
2928
10470
      {
2929
 
        if (and_level)
2930
 
          return 1;
 
10471
        if (and_level)
 
10472
          return 1;
2931
10473
      }
2932
10474
      else if (!and_level)
2933
 
        return 0;
 
10475
        return 0;
2934
10476
    }
2935
10477
    return and_level ? 0 : 1;
2936
10478
  }
2938
10480
  {                                             // boolan compare function
2939
10481
    Item_func* func= (Item_func*) cond;
2940
10482
    if (func->functype() != Item_func::EQUAL_FUNC &&
2941
 
              func->functype() != Item_func::EQ_FUNC)
 
10483
        func->functype() != Item_func::EQ_FUNC)
2942
10484
      return 0;
2943
10485
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2944
10486
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2946
10488
    {
2947
10489
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2948
10490
      {
2949
 
        if (*const_item)
2950
 
          return right_item->eq(*const_item, 1);
2951
 
        *const_item=right_item;
2952
 
        return 1;
 
10491
        if (*const_item)
 
10492
          return right_item->eq(*const_item, 1);
 
10493
        *const_item=right_item;
 
10494
        return 1;
2953
10495
      }
2954
10496
    }
2955
10497
    else if (right_item->eq(comp_item,1))
2956
10498
    {
2957
10499
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2958
10500
      {
2959
 
        if (*const_item)
2960
 
          return left_item->eq(*const_item, 1);
2961
 
        *const_item=left_item;
2962
 
        return 1;
 
10501
        if (*const_item)
 
10502
          return left_item->eq(*const_item, 1);
 
10503
        *const_item=left_item;
 
10504
        return 1;
2963
10505
      }
2964
10506
    }
2965
10507
  }
2966
10508
  return 0;
2967
10509
}
2968
10510
 
 
10511
 
2969
10512
/**
2970
10513
  @details
2971
10514
  Rows produced by a join sweep may end up in a temporary table or be sent
2977
10520
  @return
2978
10521
    end_select function to use. This function can't fail.
2979
10522
*/
2980
 
Next_select_func setup_end_select_func(Join *join)
 
10523
 
 
10524
Next_select_func setup_end_select_func(JOIN *join)
2981
10525
{
2982
10526
  Table *table= join->tmp_table;
2983
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
10527
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2984
10528
  Next_select_func end_select;
2985
10529
 
2986
10530
  /* Set up select_end */
2989
10533
    if (table->group && tmp_tbl->sum_func_count &&
2990
10534
        !tmp_tbl->precomputed_group_by)
2991
10535
    {
2992
 
      if (table->getShare()->sizeKeys())
 
10536
      if (table->s->keys)
2993
10537
      {
2994
 
        end_select= end_update;
 
10538
        end_select=end_update;
2995
10539
      }
2996
10540
      else
2997
10541
      {
2998
 
        end_select= end_unique_update;
 
10542
        end_select=end_unique_update;
2999
10543
      }
3000
10544
    }
3001
10545
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3002
10546
    {
3003
 
      end_select= end_write_group;
 
10547
      end_select=end_write_group;
3004
10548
    }
3005
10549
    else
3006
10550
    {
3007
 
      end_select= end_write;
 
10551
      end_select=end_write;
3008
10552
      if (tmp_tbl->precomputed_group_by)
3009
10553
      {
3010
10554
        /*
3011
10555
          A preceding call to create_tmp_table in the case when loose
3012
10556
          index scan is used guarantees that
3013
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
10557
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3014
10558
          by functions. It is OK here to use memcpy since we copy
3015
10559
          Item_sum pointers into an array of Item pointers.
3016
10560
        */
3032
10576
  return end_select;
3033
10577
}
3034
10578
 
 
10579
 
3035
10580
/**
3036
10581
  Make a join of all tables and write it on socket or to table.
3037
10582
 
3042
10587
  @retval
3043
10588
    -1  if error should be sent
3044
10589
*/
3045
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10590
 
 
10591
static int
 
10592
do_select(JOIN *join,List<Item> *fields,Table *table)
3046
10593
{
3047
10594
  int rc= 0;
3048
10595
  enum_nested_loop_state error= NESTED_LOOP_OK;
3049
 
  JoinTable *join_tab= NULL;
 
10596
  JOIN_TAB *join_tab= NULL;
3050
10597
 
3051
10598
  join->tmp_table= table;                       /* Save for easy recursion */
3052
10599
  join->fields= fields;
3053
10600
 
3054
10601
  if (table)
3055
10602
  {
3056
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3057
 
    table->emptyRecord();
 
10603
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10604
    empty_record(table);
3058
10605
    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
 
    }
 
10606
        table->s->keys && !table->file->inited)
 
10607
      table->file->ha_index_init(0, 0);
3069
10608
  }
3070
10609
  /* Set up select_end */
3071
10610
  Next_select_func end_select= setup_end_select_func(join);
3086
10625
    {
3087
10626
      error= (*end_select)(join, 0, 0);
3088
10627
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3089
 
              error= (*end_select)(join, 0, 1);
 
10628
        error= (*end_select)(join, 0, 1);
3090
10629
 
3091
10630
      /*
3092
10631
        If we don't go through evaluate_join_record(), do the counting
3124
10663
    if (!table)                                 // If sending data to client
3125
10664
    {
3126
10665
      /*
3127
 
        The following will unlock all cursors if the command wasn't an
3128
 
        update command
 
10666
        The following will unlock all cursors if the command wasn't an
 
10667
        update command
3129
10668
      */
3130
10669
      join->join_free();                        // Unlock all cursors
3131
10670
      if (join->result->send_eof())
3132
 
        rc= 1;                                  // Don't send error
 
10671
        rc= 1;                                  // Don't send error
3133
10672
    }
3134
10673
  }
3135
10674
  else
3137
10676
  if (table)
3138
10677
  {
3139
10678
    int tmp, new_errno= 0;
3140
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10679
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3141
10680
    {
3142
10681
      new_errno= tmp;
3143
10682
    }
3144
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10683
    if ((tmp=table->file->ha_index_or_rnd_end()))
3145
10684
    {
3146
10685
      new_errno= tmp;
3147
10686
    }
3148
10687
    if (new_errno)
3149
 
      table->print_error(new_errno,MYF(0));
 
10688
      table->file->print_error(new_errno,MYF(0));
3150
10689
  }
3151
10690
  return(join->session->is_error() ? -1 : rc);
3152
10691
}
3153
10692
 
3154
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10693
 
 
10694
enum_nested_loop_state
 
10695
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3155
10696
{
3156
10697
  enum_nested_loop_state rc;
3157
10698
 
3162
10703
      rc= sub_select(join,join_tab,end_of_records);
3163
10704
    return rc;
3164
10705
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
10706
  if (join->session->killed)            // If aborted by user
3166
10707
  {
3167
10708
    join->session->send_kill_message();
3168
 
    return NESTED_LOOP_KILLED;
 
10709
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3169
10710
  }
3170
10711
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3171
10712
  {
3172
 
    if (! join_tab->cache.store_record_in_cache())
 
10713
    if (!store_record_in_cache(&join_tab->cache))
3173
10714
      return NESTED_LOOP_OK;                     // There is more room in cache
3174
10715
    return flush_cached_records(join,join_tab,false);
3175
10716
  }
3298
10839
  @return
3299
10840
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3300
10841
*/
3301
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10842
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10843
 
 
10844
enum_nested_loop_state
 
10845
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3302
10846
{
3303
10847
  join_tab->table->null_row=0;
3304
10848
  if (end_of_records)
3306
10850
 
3307
10851
  int error;
3308
10852
  enum_nested_loop_state rc;
3309
 
  ReadRecord *info= &join_tab->read_record;
 
10853
  READ_RECORD *info= &join_tab->read_record;
 
10854
 
 
10855
  if (join_tab->flush_weedout_table)
 
10856
  {
 
10857
    do_sj_reset(join_tab->flush_weedout_table);
 
10858
  }
3310
10859
 
3311
10860
  if (join->resume_nested_loop)
3312
10861
  {
3359
10908
  return rc;
3360
10909
}
3361
10910
 
3362
 
int safe_index_read(JoinTable *tab)
 
10911
 
 
10912
 
 
10913
 
 
10914
/*
 
10915
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10916
 
 
10917
  SYNPOSIS
 
10918
    do_sj_dups_weedout()
 
10919
 
 
10920
  RETURN
 
10921
    -1  Error
 
10922
    1   The row combination is a duplicate (discard it)
 
10923
    0   The row combination is not a duplicate (continue)
 
10924
*/
 
10925
 
 
10926
int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
 
10927
{
 
10928
  int error;
 
10929
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10930
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10931
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
10932
  unsigned char *nulls_ptr= ptr;
 
10933
 
 
10934
  /* Put the the rowids tuple into table->record[0]: */
 
10935
 
 
10936
  // 1. Store the length
 
10937
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10938
  {
 
10939
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10940
    ptr++;
 
10941
  }
 
10942
  else
 
10943
  {
 
10944
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10945
    ptr += 2;
 
10946
  }
 
10947
 
 
10948
  // 2. Zero the null bytes
 
10949
  if (sjtbl->null_bytes)
 
10950
  {
 
10951
    memset(ptr, 0, sjtbl->null_bytes);
 
10952
    ptr += sjtbl->null_bytes;
 
10953
  }
 
10954
 
 
10955
  // 3. Put the rowids
 
10956
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10957
  {
 
10958
    handler *h= tab->join_tab->table->file;
 
10959
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10960
    {
 
10961
      /* It's a NULL-complemented row */
 
10962
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10963
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10964
    }
 
10965
    else
 
10966
    {
 
10967
      /* Copy the rowid value */
 
10968
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10969
        h->position(tab->join_tab->table->record[0]);
 
10970
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10971
    }
 
10972
  }
 
10973
 
 
10974
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10975
  if (error)
 
10976
  {
 
10977
    /* create_myisam_from_heap will generate error if needed */
 
10978
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
10979
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
 
10980
                                &sjtbl->recinfo, error, 1))
 
10981
      return -1;
 
10982
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
10983
    return 1;
 
10984
  }
 
10985
  return 0;
 
10986
}
 
10987
 
 
10988
 
 
10989
/*
 
10990
  SemiJoinDuplicateElimination: Reset the temporary table
 
10991
*/
 
10992
 
 
10993
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
10994
{
 
10995
  if (sj_tbl->tmp_table)
 
10996
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
10997
  return 0;
 
10998
}
 
10999
 
 
11000
/*
 
11001
  Process one record of the nested loop join.
 
11002
 
 
11003
    This function will evaluate parts of WHERE/ON clauses that are
 
11004
    applicable to the partial record on hand and in case of success
 
11005
    submit this record to the next level of the nested loop.
 
11006
*/
 
11007
 
 
11008
static enum_nested_loop_state
 
11009
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11010
                     int error)
 
11011
{
 
11012
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11013
  ha_rows found_records=join->found_records;
 
11014
  COND *select_cond= join_tab->select_cond;
 
11015
 
 
11016
  if (error > 0 || (join->session->is_error()))     // Fatal error
 
11017
    return NESTED_LOOP_ERROR;
 
11018
  if (error < 0)
 
11019
    return NESTED_LOOP_NO_MORE_ROWS;
 
11020
  if (join->session->killed)                    // Aborted by user
 
11021
  {
 
11022
    join->session->send_kill_message();
 
11023
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11024
  }
 
11025
  if (!select_cond || select_cond->val_int())
 
11026
  {
 
11027
    /*
 
11028
      There is no select condition or the attached pushed down
 
11029
      condition is true => a match is found.
 
11030
    */
 
11031
    bool found= 1;
 
11032
    while (join_tab->first_unmatched && found)
 
11033
    {
 
11034
      /*
 
11035
        The while condition is always false if join_tab is not
 
11036
        the last inner join table of an outer join operation.
 
11037
      */
 
11038
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11039
      /*
 
11040
        Mark that a match for current outer table is found.
 
11041
        This activates push down conditional predicates attached
 
11042
        to the all inner tables of the outer join.
 
11043
      */
 
11044
      first_unmatched->found= 1;
 
11045
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11046
      {
 
11047
        if (tab->table->reginfo.not_exists_optimize)
 
11048
          return NESTED_LOOP_NO_MORE_ROWS;
 
11049
        /* Check all predicates that has just been activated. */
 
11050
        /*
 
11051
          Actually all predicates non-guarded by first_unmatched->found
 
11052
          will be re-evaluated again. It could be fixed, but, probably,
 
11053
          it's not worth doing now.
 
11054
        */
 
11055
        if (tab->select_cond && !tab->select_cond->val_int())
 
11056
        {
 
11057
          /* The condition attached to table tab is false */
 
11058
          if (tab == join_tab)
 
11059
            found= 0;
 
11060
          else
 
11061
          {
 
11062
            /*
 
11063
              Set a return point if rejected predicate is attached
 
11064
              not to the last table of the current nest level.
 
11065
            */
 
11066
            join->return_tab= tab;
 
11067
            return NESTED_LOOP_OK;
 
11068
          }
 
11069
        }
 
11070
      }
 
11071
      /*
 
11072
        Check whether join_tab is not the last inner table
 
11073
        for another embedding outer join.
 
11074
      */
 
11075
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11076
          first_unmatched->last_inner != join_tab)
 
11077
        first_unmatched= 0;
 
11078
      join_tab->first_unmatched= first_unmatched;
 
11079
    }
 
11080
 
 
11081
    JOIN_TAB *return_tab= join->return_tab;
 
11082
    join_tab->found_match= true;
 
11083
    if (join_tab->check_weed_out_table)
 
11084
    {
 
11085
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
 
11086
      if (res == -1)
 
11087
        return NESTED_LOOP_ERROR;
 
11088
      if (res == 1)
 
11089
        return NESTED_LOOP_OK;
 
11090
    }
 
11091
    else if (join_tab->do_firstmatch)
 
11092
    {
 
11093
      /*
 
11094
        We should return to the join_tab->do_firstmatch after we have
 
11095
        enumerated all the suffixes for current prefix row combination
 
11096
      */
 
11097
      return_tab= join_tab->do_firstmatch;
 
11098
    }
 
11099
 
 
11100
    /*
 
11101
      It was not just a return to lower loop level when one
 
11102
      of the newly activated predicates is evaluated as false
 
11103
      (See above join->return_tab= tab).
 
11104
    */
 
11105
    join->examined_rows++;
 
11106
    join->session->row_count++;
 
11107
 
 
11108
    if (found)
 
11109
    {
 
11110
      enum enum_nested_loop_state rc;
 
11111
      /* A match from join_tab is found for the current partial join. */
 
11112
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11113
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11114
        return rc;
 
11115
      if (return_tab < join->return_tab)
 
11116
        join->return_tab= return_tab;
 
11117
 
 
11118
      if (join->return_tab < join_tab)
 
11119
        return NESTED_LOOP_OK;
 
11120
      /*
 
11121
        Test if this was a SELECT DISTINCT query on a table that
 
11122
        was not in the field list;  In this case we can abort if
 
11123
        we found a row, as no new rows can be added to the result.
 
11124
      */
 
11125
      if (not_used_in_distinct && found_records != join->found_records)
 
11126
        return NESTED_LOOP_NO_MORE_ROWS;
 
11127
    }
 
11128
    else
 
11129
      join_tab->read_record.file->unlock_row();
 
11130
  }
 
11131
  else
 
11132
  {
 
11133
    /*
 
11134
      The condition pushed down to the table join_tab rejects all rows
 
11135
      with the beginning coinciding with the current partial join.
 
11136
    */
 
11137
    join->examined_rows++;
 
11138
    join->session->row_count++;
 
11139
    join_tab->read_record.file->unlock_row();
 
11140
  }
 
11141
  return NESTED_LOOP_OK;
 
11142
}
 
11143
 
 
11144
 
 
11145
/**
 
11146
 
 
11147
  @details
 
11148
    Construct a NULL complimented partial join record and feed it to the next
 
11149
    level of the nested loop. This function is used in case we have
 
11150
    an OUTER join and no matching record was found.
 
11151
*/
 
11152
 
 
11153
static enum_nested_loop_state
 
11154
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11155
{
 
11156
  /*
 
11157
    The table join_tab is the first inner table of a outer join operation
 
11158
    and no matches has been found for the current outer row.
 
11159
  */
 
11160
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11161
  /* Cache variables for faster loop */
 
11162
  COND *select_cond;
 
11163
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11164
  {
 
11165
    /* Change the the values of guard predicate variables. */
 
11166
    join_tab->found= 1;
 
11167
    join_tab->not_null_compl= 0;
 
11168
    /* The outer row is complemented by nulls for each inner tables */
 
11169
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11170
    mark_as_null_row(join_tab->table);       // For group by without error
 
11171
    select_cond= join_tab->select_cond;
 
11172
    /* Check all attached conditions for inner table rows. */
 
11173
    if (select_cond && !select_cond->val_int())
 
11174
      return NESTED_LOOP_OK;
 
11175
  }
 
11176
  join_tab--;
 
11177
  /*
 
11178
    The row complemented by nulls might be the first row
 
11179
    of embedding outer joins.
 
11180
    If so, perform the same actions as in the code
 
11181
    for the first regular outer join row above.
 
11182
  */
 
11183
  for ( ; ; )
 
11184
  {
 
11185
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11186
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11187
        first_unmatched->last_inner != join_tab)
 
11188
      first_unmatched= 0;
 
11189
    join_tab->first_unmatched= first_unmatched;
 
11190
    if (!first_unmatched)
 
11191
      break;
 
11192
    first_unmatched->found= 1;
 
11193
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11194
    {
 
11195
      if (tab->select_cond && !tab->select_cond->val_int())
 
11196
      {
 
11197
        join->return_tab= tab;
 
11198
        return NESTED_LOOP_OK;
 
11199
      }
 
11200
    }
 
11201
  }
 
11202
  /*
 
11203
    The row complemented by nulls satisfies all conditions
 
11204
    attached to inner tables.
 
11205
    Send the row complemented by nulls to be joined with the
 
11206
    remaining tables.
 
11207
  */
 
11208
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11209
}
 
11210
 
 
11211
 
 
11212
static enum_nested_loop_state
 
11213
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11214
{
 
11215
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11216
  int error;
 
11217
  READ_RECORD *info;
 
11218
 
 
11219
  join_tab->table->null_row= 0;
 
11220
  if (!join_tab->cache.records)
 
11221
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11222
  if (skip_last)
 
11223
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11224
  if (join_tab->use_quick == 2)
 
11225
  {
 
11226
    if (join_tab->select->quick)
 
11227
    {                                   /* Used quick select last. reset it */
 
11228
      delete join_tab->select->quick;
 
11229
      join_tab->select->quick=0;
 
11230
    }
 
11231
  }
 
11232
 /* read through all records */
 
11233
  if ((error=join_init_read_record(join_tab)))
 
11234
  {
 
11235
    reset_cache_write(&join_tab->cache);
 
11236
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11237
  }
 
11238
 
 
11239
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11240
  {
 
11241
    tmp->status=tmp->table->status;
 
11242
    tmp->table->status=0;
 
11243
  }
 
11244
 
 
11245
  info= &join_tab->read_record;
 
11246
  do
 
11247
  {
 
11248
    if (join->session->killed)
 
11249
    {
 
11250
      join->session->send_kill_message();
 
11251
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11252
    }
 
11253
    SQL_SELECT *select=join_tab->select;
 
11254
    if (rc == NESTED_LOOP_OK &&
 
11255
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11256
    {
 
11257
      uint32_t i;
 
11258
      reset_cache_read(&join_tab->cache);
 
11259
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11260
      {
 
11261
        read_cached_record(join_tab);
 
11262
        if (!select || !select->skip_record())
 
11263
        {
 
11264
          int res= 0;
 
11265
          if (!join_tab->check_weed_out_table ||
 
11266
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
 
11267
          {
 
11268
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11269
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11270
            {
 
11271
              reset_cache_write(&join_tab->cache);
 
11272
              return rc;
 
11273
            }
 
11274
          }
 
11275
          if (res == -1)
 
11276
            return NESTED_LOOP_ERROR;
 
11277
        }
 
11278
      }
 
11279
    }
 
11280
  } while (!(error=info->read_record(info)));
 
11281
 
 
11282
  if (skip_last)
 
11283
    read_cached_record(join_tab);               // Restore current record
 
11284
  reset_cache_write(&join_tab->cache);
 
11285
  if (error > 0)                                // Fatal error
 
11286
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11287
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11288
    tmp2->table->status=tmp2->status;
 
11289
  return NESTED_LOOP_OK;
 
11290
}
 
11291
 
 
11292
int safe_index_read(JOIN_TAB *tab)
3363
11293
{
3364
11294
  int error;
3365
11295
  Table *table= tab->table;
3366
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11296
  if ((error=table->file->index_read_map(table->record[0],
3367
11297
                                         tab->ref.key_buff,
3368
11298
                                         make_prev_keypart_map(tab->ref.key_parts),
3369
11299
                                         HA_READ_KEY_EXACT)))
3371
11301
  return 0;
3372
11302
}
3373
11303
 
 
11304
 
 
11305
static int
 
11306
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
11307
{
 
11308
  int error;
 
11309
  Table *table=tab->table;
 
11310
  table->const_table=1;
 
11311
  table->null_row=0;
 
11312
  table->status=STATUS_NO_RECORD;
 
11313
 
 
11314
  if (tab->type == JT_SYSTEM)
 
11315
  {
 
11316
    if ((error=join_read_system(tab)))
 
11317
    {                                           // Info for DESCRIBE
 
11318
      tab->info="const row not found";
 
11319
      /* Mark for EXPLAIN that the row was not found */
 
11320
      pos->records_read=0.0;
 
11321
      pos->ref_depend_map= 0;
 
11322
      if (!table->maybe_null || error > 0)
 
11323
        return(error);
 
11324
    }
 
11325
  }
 
11326
  else
 
11327
  {
 
11328
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11329
        !table->no_keyread &&
 
11330
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
11331
    {
 
11332
      table->key_read=1;
 
11333
      table->file->extra(HA_EXTRA_KEYREAD);
 
11334
      tab->index= tab->ref.key;
 
11335
    }
 
11336
    error=join_read_const(tab);
 
11337
    if (table->key_read)
 
11338
    {
 
11339
      table->key_read=0;
 
11340
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
11341
    }
 
11342
    if (error)
 
11343
    {
 
11344
      tab->info="unique row not found";
 
11345
      /* Mark for EXPLAIN that the row was not found */
 
11346
      pos->records_read=0.0;
 
11347
      pos->ref_depend_map= 0;
 
11348
      if (!table->maybe_null || error > 0)
 
11349
        return(error);
 
11350
    }
 
11351
  }
 
11352
  if (*tab->on_expr_ref && !table->null_row)
 
11353
  {
 
11354
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
11355
      mark_as_null_row(table);
 
11356
  }
 
11357
  if (!table->null_row)
 
11358
    table->maybe_null=0;
 
11359
 
 
11360
  /* Check appearance of new constant items in Item_equal objects */
 
11361
  JOIN *join= tab->join;
 
11362
  if (join->conds)
 
11363
    update_const_equal_items(join->conds, tab);
 
11364
  TableList *tbl;
 
11365
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
11366
  {
 
11367
    TableList *embedded;
 
11368
    TableList *embedding= tbl;
 
11369
    do
 
11370
    {
 
11371
      embedded= embedding;
 
11372
      if (embedded->on_expr)
 
11373
         update_const_equal_items(embedded->on_expr, tab);
 
11374
      embedding= embedded->embedding;
 
11375
    }
 
11376
    while (embedding &&
 
11377
           embedding->nested_join->join_list.head() == embedded);
 
11378
  }
 
11379
 
 
11380
  return(0);
 
11381
}
 
11382
 
 
11383
 
 
11384
static int
 
11385
join_read_system(JOIN_TAB *tab)
 
11386
{
 
11387
  Table *table= tab->table;
 
11388
  int error;
 
11389
  if (table->status & STATUS_GARBAGE)           // If first read
 
11390
  {
 
11391
    if ((error=table->file->read_first_row(table->record[0],
 
11392
                                           table->s->primary_key)))
 
11393
    {
 
11394
      if (error != HA_ERR_END_OF_FILE)
 
11395
        return table->report_error(error);
 
11396
      mark_as_null_row(tab->table);
 
11397
      empty_record(table);                      // Make empty record
 
11398
      return -1;
 
11399
    }
 
11400
    update_virtual_fields_marked_for_write(table);
 
11401
    store_record(table,record[1]);
 
11402
  }
 
11403
  else if (!table->status)                      // Only happens with left join
 
11404
    restore_record(table,record[1]);                    // restore old record
 
11405
  table->null_row=0;
 
11406
  return table->status ? -1 : 0;
 
11407
}
 
11408
 
 
11409
 
3374
11410
/**
3375
11411
  Read a (constant) table when there is at most one matching row.
3376
11412
 
3383
11419
  @retval
3384
11420
    1   Got an error (other than row not found) during read
3385
11421
*/
3386
 
int join_read_const(JoinTable *tab)
 
11422
 
 
11423
static int
 
11424
join_read_const(JOIN_TAB *tab)
3387
11425
{
3388
11426
  int error;
3389
11427
  Table *table= tab->table;
3391
11429
  {
3392
11430
    table->status= 0;
3393
11431
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3394
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11432
      error=HA_ERR_KEY_NOT_FOUND;
3395
11433
    else
3396
11434
    {
3397
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
11435
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
3398
11436
                                            (unsigned char*) tab->ref.key_buff,
3399
11437
                                            make_prev_keypart_map(tab->ref.key_parts),
3400
11438
                                            HA_READ_KEY_EXACT);
3402
11440
    if (error)
3403
11441
    {
3404
11442
      table->status= STATUS_NOT_FOUND;
3405
 
      tab->table->mark_as_null_row();
3406
 
      table->emptyRecord();
 
11443
      mark_as_null_row(tab->table);
 
11444
      empty_record(table);
3407
11445
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3408
 
        return table->report_error(error);
 
11446
        return table->report_error(error);
3409
11447
      return -1;
3410
11448
    }
3411
 
    table->storeRecord();
 
11449
    update_virtual_fields_marked_for_write(table);
 
11450
    store_record(table,record[1]);
3412
11451
  }
3413
11452
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3414
11453
  {
3415
11454
    table->status=0;
3416
 
    table->restoreRecord();                     // restore old record
 
11455
    restore_record(table,record[1]);                    // restore old record
3417
11456
  }
3418
11457
  table->null_row=0;
3419
11458
  return table->status ? -1 : 0;
3420
11459
}
3421
11460
 
 
11461
 
3422
11462
/*
3423
11463
  eq_ref access method implementation: "read_first" function
3424
11464
 
3425
11465
  SYNOPSIS
3426
11466
    join_read_key()
3427
 
      tab  JoinTable of the accessed table
 
11467
      tab  JOIN_TAB of the accessed table
3428
11468
 
3429
11469
  DESCRIPTION
3430
11470
    This is "read_fist" function for the "ref" access method. The difference
3435
11475
   -1  - Row not found
3436
11476
    1  - Error
3437
11477
*/
3438
 
int join_read_key(JoinTable *tab)
 
11478
 
 
11479
static int
 
11480
join_read_key(JOIN_TAB *tab)
3439
11481
{
3440
11482
  int error;
3441
11483
  Table *table= tab->table;
3442
11484
 
3443
 
  if (!table->cursor->inited)
 
11485
  if (!table->file->inited)
3444
11486
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
11487
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3450
11488
  }
3451
11489
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
11490
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
11491
  if (cmp_buffer_with_ref(tab) ||
3454
11492
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
11493
  {
3458
11496
      table->status=STATUS_NOT_FOUND;
3459
11497
      return -1;
3460
11498
    }
3461
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11499
    error=table->file->index_read_map(table->record[0],
3462
11500
                                      tab->ref.key_buff,
3463
11501
                                      make_prev_keypart_map(tab->ref.key_parts),
3464
11502
                                      HA_READ_KEY_EXACT);
3469
11507
  return table->status ? -1 : 0;
3470
11508
}
3471
11509
 
 
11510
 
3472
11511
/*
3473
11512
  ref access method implementation: "read_first" function
3474
11513
 
3475
11514
  SYNOPSIS
3476
11515
    join_read_always_key()
3477
 
      tab  JoinTable of the accessed table
 
11516
      tab  JOIN_TAB of the accessed table
3478
11517
 
3479
11518
  DESCRIPTION
3480
 
    This is "read_first" function for the "ref" access method.
 
11519
    This is "read_fist" function for the "ref" access method.
3481
11520
 
3482
11521
    The functon must leave the index initialized when it returns.
3483
11522
    ref_or_null access implementation depends on that.
3487
11526
   -1  - Row not found
3488
11527
    1  - Error
3489
11528
*/
3490
 
int join_read_always_key(JoinTable *tab)
 
11529
 
 
11530
static int
 
11531
join_read_always_key(JOIN_TAB *tab)
3491
11532
{
3492
11533
  int error;
3493
11534
  Table *table= tab->table;
3494
11535
 
3495
11536
  /* 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
 
  }
 
11537
  if (!table->file->inited)
 
11538
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3502
11539
 
3503
11540
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
11541
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3509
11546
 
3510
11547
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3511
11548
    return -1;
3512
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11549
  if ((error=table->file->index_read_map(table->record[0],
3513
11550
                                         tab->ref.key_buff,
3514
11551
                                         make_prev_keypart_map(tab->ref.key_parts),
3515
11552
                                         HA_READ_KEY_EXACT)))
3516
11553
  {
3517
11554
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3518
11555
      return table->report_error(error);
3519
 
    return -1;
 
11556
    return -1; /* purecov: inspected */
3520
11557
  }
3521
 
 
 
11558
  update_virtual_fields_marked_for_write(table);
3522
11559
  return 0;
3523
11560
}
3524
11561
 
 
11562
 
3525
11563
/**
3526
 
  This function is used when optimizing away ORDER BY in
3527
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11564
  This function is used when optimizing away order_st BY in
 
11565
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3528
11566
*/
3529
 
int join_read_last_key(JoinTable *tab)
 
11567
 
 
11568
static int
 
11569
join_read_last_key(JOIN_TAB *tab)
3530
11570
{
3531
11571
  int error;
3532
11572
  Table *table= tab->table;
3533
11573
 
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
 
  }
 
11574
  if (!table->file->inited)
 
11575
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3540
11576
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3541
11577
    return -1;
3542
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11578
  if ((error=table->file->index_read_last_map(table->record[0],
3543
11579
                                              tab->ref.key_buff,
3544
11580
                                              make_prev_keypart_map(tab->ref.key_parts))))
3545
11581
  {
3546
11582
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3547
11583
      return table->report_error(error);
3548
 
    return -1;
 
11584
    return -1; /* purecov: inspected */
3549
11585
  }
3550
11586
  return 0;
3551
11587
}
3552
11588
 
3553
 
int join_no_more_records(ReadRecord *)
 
11589
 
 
11590
        /* ARGSUSED */
 
11591
static int
 
11592
join_no_more_records(READ_RECORD *)
3554
11593
{
3555
11594
  return -1;
3556
11595
}
3557
11596
 
3558
 
int join_read_next_same_diff(ReadRecord *info)
 
11597
static int
 
11598
join_read_next_same_diff(READ_RECORD *info)
3559
11599
{
3560
11600
  Table *table= info->table;
3561
 
  JoinTable *tab=table->reginfo.join_tab;
 
11601
  JOIN_TAB *tab=table->reginfo.join_tab;
3562
11602
  if (tab->insideout_match_tab->found_match)
3563
11603
  {
3564
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11604
    KEY *key= tab->table->key_info + tab->index;
3565
11605
    do
3566
11606
    {
3567
11607
      int error;
3568
11608
      /* Save index tuple from record to the buffer */
3569
11609
      key_copy(tab->insideout_buf, info->record, key, 0);
3570
11610
 
3571
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11611
      if ((error=table->file->index_next_same(table->record[0],
3572
11612
                                              tab->ref.key_buff,
3573
11613
                                              tab->ref.key_length)))
3574
11614
      {
3586
11626
    return join_read_next_same(info);
3587
11627
}
3588
11628
 
3589
 
int join_read_next_same(ReadRecord *info)
 
11629
static int
 
11630
join_read_next_same(READ_RECORD *info)
3590
11631
{
3591
11632
  int error;
3592
11633
  Table *table= info->table;
3593
 
  JoinTable *tab=table->reginfo.join_tab;
 
11634
  JOIN_TAB *tab=table->reginfo.join_tab;
3594
11635
 
3595
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11636
  if ((error=table->file->index_next_same(table->record[0],
3596
11637
                                          tab->ref.key_buff,
3597
11638
                                          tab->ref.key_length)))
3598
11639
  {
3601
11642
    table->status= STATUS_GARBAGE;
3602
11643
    return -1;
3603
11644
  }
3604
 
 
 
11645
  update_virtual_fields_marked_for_write(table);
3605
11646
  return 0;
3606
11647
}
3607
11648
 
3608
 
int join_read_prev_same(ReadRecord *info)
 
11649
 
 
11650
static int
 
11651
join_read_prev_same(READ_RECORD *info)
3609
11652
{
3610
11653
  int error;
3611
11654
  Table *table= info->table;
3612
 
  JoinTable *tab=table->reginfo.join_tab;
 
11655
  JOIN_TAB *tab=table->reginfo.join_tab;
3613
11656
 
3614
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11657
  if ((error=table->file->index_prev(table->record[0])))
3615
11658
    return table->report_error(error);
3616
11659
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3617
11660
                      tab->ref.key_length))
3619
11662
    table->status=STATUS_NOT_FOUND;
3620
11663
    error= -1;
3621
11664
  }
 
11665
  update_virtual_fields_marked_for_write(table);
3622
11666
  return error;
3623
11667
}
3624
11668
 
3625
 
int join_init_quick_read_record(JoinTable *tab)
 
11669
 
 
11670
static int
 
11671
join_init_quick_read_record(JOIN_TAB *tab)
3626
11672
{
3627
11673
  if (test_if_quick_select(tab) == -1)
3628
11674
    return -1;                                  /* No possible records */
3629
11675
  return join_init_read_record(tab);
3630
11676
}
3631
11677
 
3632
 
int init_read_record_seq(JoinTable *tab)
 
11678
 
 
11679
int rr_sequential(READ_RECORD *info);
 
11680
int init_read_record_seq(JOIN_TAB *tab)
3633
11681
{
3634
 
  tab->read_record.init_reard_record_sequential();
3635
 
 
3636
 
  if (tab->read_record.cursor->startTableScan(1))
 
11682
  tab->read_record.read_record= rr_sequential;
 
11683
  if (tab->read_record.file->ha_rnd_init(1))
3637
11684
    return 1;
3638
11685
  return (*tab->read_record.read_record)(&tab->read_record);
3639
11686
}
3640
11687
 
3641
 
int test_if_quick_select(JoinTable *tab)
 
11688
static int
 
11689
test_if_quick_select(JOIN_TAB *tab)
3642
11690
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
11691
  delete tab->select->quick;
 
11692
  tab->select->quick=0;
3645
11693
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11694
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11695
                                        false);
3647
11696
}
3648
11697
 
3649
 
int join_init_read_record(JoinTable *tab)
 
11698
 
 
11699
static int
 
11700
join_init_read_record(JOIN_TAB *tab)
3650
11701
{
3651
11702
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
11703
    return 1;
3653
 
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
3656
 
 
 
11704
  init_read_record(&tab->read_record, tab->join->session, tab->table,
 
11705
                   tab->select,1,1);
3657
11706
  return (*tab->read_record.read_record)(&tab->read_record);
3658
11707
}
3659
11708
 
3660
 
int join_read_first(JoinTable *tab)
 
11709
 
 
11710
static int
 
11711
join_read_first(JOIN_TAB *tab)
3661
11712
{
3662
11713
  int error;
3663
11714
  Table *table=tab->table;
3664
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11715
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3665
11716
      !table->no_keyread)
3666
11717
  {
3667
 
    table->key_read= 1;
3668
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11718
    table->key_read=1;
 
11719
    table->file->extra(HA_EXTRA_KEYREAD);
3669
11720
  }
3670
 
  tab->table->status= 0;
 
11721
  tab->table->status=0;
3671
11722
  tab->read_record.table=table;
3672
 
  tab->read_record.cursor=table->cursor;
 
11723
  tab->read_record.file=table->file;
3673
11724
  tab->read_record.index=tab->index;
3674
 
  tab->read_record.record=table->getInsertRecord();
 
11725
  tab->read_record.record=table->record[0];
3675
11726
  if (tab->insideout_match_tab)
3676
11727
  {
3677
11728
    tab->read_record.do_insideout_scan= tab;
3684
11735
    tab->read_record.do_insideout_scan= 0;
3685
11736
  }
3686
11737
 
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())))
 
11738
  if (!table->file->inited)
 
11739
    table->file->ha_index_init(tab->index, tab->sorted);
 
11740
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3697
11741
  {
3698
11742
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3699
11743
      table->report_error(error);
3700
11744
    return -1;
3701
11745
  }
3702
 
 
 
11746
  if (not error)
 
11747
    update_virtual_fields_marked_for_write(tab->table);
3703
11748
  return 0;
3704
11749
}
3705
11750
 
3706
 
int join_read_next_different(ReadRecord *info)
 
11751
 
 
11752
static int
 
11753
join_read_next_different(READ_RECORD *info)
3707
11754
{
3708
 
  JoinTable *tab= info->do_insideout_scan;
 
11755
  JOIN_TAB *tab= info->do_insideout_scan;
3709
11756
  if (tab->insideout_match_tab->found_match)
3710
11757
  {
3711
 
    KeyInfo *key= tab->table->key_info + tab->index;
 
11758
    KEY *key= tab->table->key_info + tab->index;
3712
11759
    do
3713
11760
    {
3714
11761
      int error;
3715
11762
      /* Save index tuple from record to the buffer */
3716
11763
      key_copy(tab->insideout_buf, info->record, key, 0);
3717
11764
 
3718
 
      if ((error=info->cursor->index_next(info->record)))
 
11765
      if ((error=info->file->index_next(info->record)))
3719
11766
        return info->table->report_error(error);
 
11767
      if (not error)
 
11768
        update_virtual_fields_marked_for_write(tab->table);
3720
11769
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3721
11770
                      tab->insideout_buf, key->key_length));
3722
11771
    tab->insideout_match_tab->found_match= 0;
3726
11775
    return join_read_next(info);
3727
11776
}
3728
11777
 
3729
 
int join_read_next(ReadRecord *info)
 
11778
 
 
11779
static int
 
11780
join_read_next(READ_RECORD *info)
3730
11781
{
3731
11782
  int error;
3732
 
  if ((error=info->cursor->index_next(info->record)))
 
11783
  if ((error=info->file->index_next(info->record)))
3733
11784
    return info->table->report_error(error);
 
11785
  if (not error)
 
11786
    update_virtual_fields_marked_for_write(info->table);
3734
11787
  return 0;
3735
11788
}
3736
11789
 
3737
 
int join_read_last(JoinTable *tab)
 
11790
 
 
11791
static int
 
11792
join_read_last(JOIN_TAB *tab)
3738
11793
{
3739
11794
  Table *table=tab->table;
3740
11795
  int error;
3741
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11796
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3742
11797
      !table->no_keyread)
3743
11798
  {
3744
11799
    table->key_read=1;
3745
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11800
    table->file->extra(HA_EXTRA_KEYREAD);
3746
11801
  }
3747
11802
  tab->table->status=0;
3748
11803
  tab->read_record.read_record=join_read_prev;
3749
11804
  tab->read_record.table=table;
3750
 
  tab->read_record.cursor=table->cursor;
 
11805
  tab->read_record.file=table->file;
3751
11806
  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())))
 
11807
  tab->read_record.record=table->record[0];
 
11808
  if (!table->file->inited)
 
11809
    table->file->ha_index_init(tab->index, 1);
 
11810
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3760
11811
    return table->report_error(error);
3761
 
 
 
11812
  if (not error)
 
11813
    update_virtual_fields_marked_for_write(tab->table);
3762
11814
  return 0;
3763
11815
}
3764
11816
 
3765
 
int join_read_prev(ReadRecord *info)
 
11817
 
 
11818
static int
 
11819
join_read_prev(READ_RECORD *info)
3766
11820
{
3767
11821
  int error;
3768
 
  if ((error= info->cursor->index_prev(info->record)))
 
11822
  if ((error= info->file->index_prev(info->record)))
3769
11823
    return info->table->report_error(error);
3770
 
 
 
11824
  if (not error)
 
11825
    update_virtual_fields_marked_for_write(info->table);
3771
11826
  return 0;
3772
11827
}
3773
11828
 
3774
11829
/**
3775
11830
  Reading of key with key reference and one part that may be NULL.
3776
11831
*/
3777
 
int join_read_always_key_or_null(JoinTable *tab)
 
11832
 
 
11833
int
 
11834
join_read_always_key_or_null(JOIN_TAB *tab)
3778
11835
{
3779
11836
  int res;
3780
11837
 
3788
11845
  return safe_index_read(tab);
3789
11846
}
3790
11847
 
3791
 
int join_read_next_same_or_null(ReadRecord *info)
 
11848
 
 
11849
int
 
11850
join_read_next_same_or_null(READ_RECORD *info)
3792
11851
{
3793
11852
  int error;
3794
11853
  if ((error= join_read_next_same(info)) >= 0)
3795
11854
    return error;
3796
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11855
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3797
11856
 
3798
11857
  /* Test if we have already done a read after null key */
3799
11858
  if (*tab->ref.null_ref_key)
3802
11861
  return safe_index_read(tab);                  // then read null keys
3803
11862
}
3804
11863
 
3805
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11864
 
 
11865
/*****************************************************************************
 
11866
  DESCRIPTION
 
11867
    Functions that end one nested loop iteration. Different functions
 
11868
    are used to support GROUP BY clause and to redirect records
 
11869
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11870
    network client.
 
11871
 
 
11872
  RETURN VALUES
 
11873
    NESTED_LOOP_OK           - the record has been successfully handled
 
11874
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11875
                               was detected
 
11876
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11877
                               the record
 
11878
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11879
                               additionally, the nested loop produced the
 
11880
                               number of rows specified in the LIMIT clause
 
11881
                               for the query
 
11882
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11883
                               additionally, there is a cursor and the nested
 
11884
                               loop algorithm produced the number of rows
 
11885
                               that is specified for current cursor fetch
 
11886
                               operation.
 
11887
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11888
*****************************************************************************/
 
11889
 
 
11890
/* ARGSUSED */
 
11891
static enum_nested_loop_state
 
11892
end_send(JOIN *join, JOIN_TAB *,
 
11893
         bool end_of_records)
 
11894
{
 
11895
  if (!end_of_records)
 
11896
  {
 
11897
    int error;
 
11898
    if (join->having && join->having->val_int() == 0)
 
11899
      return(NESTED_LOOP_OK);               // Didn't match having
 
11900
    error=0;
 
11901
    if (join->do_send_rows)
 
11902
      error=join->result->send_data(*join->fields);
 
11903
    if (error)
 
11904
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11905
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11906
        join->do_send_rows)
 
11907
    {
 
11908
      if (join->select_options & OPTION_FOUND_ROWS)
 
11909
      {
 
11910
        JOIN_TAB *jt=join->join_tab;
 
11911
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11912
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11913
            !(jt->select && jt->select->quick) &&
 
11914
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11915
            (jt->ref.key < 0))
 
11916
        {
 
11917
          /* Join over all rows in table;  Return number of found rows */
 
11918
          Table *table=jt->table;
 
11919
 
 
11920
          join->select_options ^= OPTION_FOUND_ROWS;
 
11921
          if (table->sort.record_pointers ||
 
11922
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11923
          {
 
11924
            /* Using filesort */
 
11925
            join->send_records= table->sort.found_records;
 
11926
          }
 
11927
          else
 
11928
          {
 
11929
            table->file->info(HA_STATUS_VARIABLE);
 
11930
            join->send_records= table->file->stats.records;
 
11931
          }
 
11932
        }
 
11933
        else
 
11934
        {
 
11935
          join->do_send_rows= 0;
 
11936
          if (join->unit->fake_select_lex)
 
11937
            join->unit->fake_select_lex->select_limit= 0;
 
11938
          return(NESTED_LOOP_OK);
 
11939
        }
 
11940
      }
 
11941
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11942
    }
 
11943
    else if (join->send_records >= join->fetch_limit)
 
11944
    {
 
11945
      /*
 
11946
        There is a server side cursor and all rows for
 
11947
        this fetch request are sent.
 
11948
      */
 
11949
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11950
    }
 
11951
  }
 
11952
 
 
11953
  return(NESTED_LOOP_OK);
 
11954
}
 
11955
 
 
11956
 
 
11957
/* ARGSUSED */
 
11958
enum_nested_loop_state
 
11959
end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
3806
11960
{
3807
11961
  int idx= -1;
3808
11962
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3815
11969
    {
3816
11970
      if (idx < (int) join->send_group_parts)
3817
11971
      {
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();
 
11972
        int error=0;
 
11973
        {
 
11974
          if (!join->first_record)
 
11975
          {
 
11976
            List_iterator_fast<Item> it(*join->fields);
 
11977
            Item *item;
 
11978
            /* No matching rows for group function */
 
11979
            join->clear();
3826
11980
 
3827
11981
            while ((item= it++))
3828
11982
              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;
 
11983
          }
 
11984
          if (join->having && join->having->val_int() == 0)
 
11985
            error= -1;                          // Didn't satisfy having
 
11986
          else
 
11987
          {
 
11988
            if (join->do_send_rows)
 
11989
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
11990
            join->send_records++;
 
11991
          }
 
11992
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
11993
          {
 
11994
            if (join->rollup_send_data((uint) (idx+1)))
 
11995
              error= 1;
 
11996
          }
 
11997
        }
 
11998
        if (error > 0)
 
11999
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
12000
        if (end_of_records)
 
12001
          return(NESTED_LOOP_OK);
 
12002
        if (join->send_records >= join->unit->select_limit_cnt &&
 
12003
            join->do_send_rows)
 
12004
        {
 
12005
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
12006
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
12007
          join->do_send_rows=0;
 
12008
          join->unit->select_limit_cnt = HA_POS_ERROR;
3855
12009
        }
3856
12010
        else if (join->send_records >= join->fetch_limit)
3857
12011
        {
3870
12024
    else
3871
12025
    {
3872
12026
      if (end_of_records)
3873
 
        return(NESTED_LOOP_OK);
 
12027
        return(NESTED_LOOP_OK);
3874
12028
      join->first_record=1;
3875
12029
      test_if_item_cache_changed(join->group_fields);
3876
12030
    }
3882
12036
      */
3883
12037
      copy_fields(&join->tmp_table_param);
3884
12038
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3885
 
        return(NESTED_LOOP_ERROR);
 
12039
        return(NESTED_LOOP_ERROR);
3886
12040
      return(ok_code);
3887
12041
    }
3888
12042
  }
3891
12045
  return(NESTED_LOOP_OK);
3892
12046
}
3893
12047
 
3894
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12048
 
 
12049
/* ARGSUSED */
 
12050
enum_nested_loop_state
 
12051
end_write(JOIN *join, JOIN_TAB *,
 
12052
          bool end_of_records)
 
12053
{
 
12054
  Table *table=join->tmp_table;
 
12055
 
 
12056
  if (join->session->killed)                    // Aborted by user
 
12057
  {
 
12058
    join->session->send_kill_message();
 
12059
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12060
  }
 
12061
  if (!end_of_records)
 
12062
  {
 
12063
    copy_fields(&join->tmp_table_param);
 
12064
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12065
    if (!join->having || join->having->val_int())
 
12066
    {
 
12067
      int error;
 
12068
      join->found_records++;
 
12069
      if ((error=table->file->ha_write_row(table->record[0])))
 
12070
      {
 
12071
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12072
          goto end;
 
12073
        if (create_myisam_from_heap(join->session, table,
 
12074
                                    join->tmp_table_param.start_recinfo,
 
12075
                                    &join->tmp_table_param.recinfo,
 
12076
                                    error, 1))
 
12077
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12078
        table->s->uniques=0;                    // To ensure rows are the same
 
12079
      }
 
12080
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12081
          join->do_send_rows)
 
12082
      {
 
12083
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12084
          return(NESTED_LOOP_QUERY_LIMIT);
 
12085
        join->do_send_rows=0;
 
12086
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12087
        return(NESTED_LOOP_OK);
 
12088
      }
 
12089
    }
 
12090
  }
 
12091
end:
 
12092
  return(NESTED_LOOP_OK);
 
12093
}
 
12094
 
 
12095
/* ARGSUSED */
 
12096
/** Group by searching after group record and updating it if possible. */
 
12097
 
 
12098
static enum_nested_loop_state
 
12099
end_update(JOIN *join, JOIN_TAB *,
 
12100
           bool end_of_records)
 
12101
{
 
12102
  Table *table=join->tmp_table;
 
12103
  order_st   *group;
 
12104
  int     error;
 
12105
 
 
12106
  if (end_of_records)
 
12107
    return(NESTED_LOOP_OK);
 
12108
  if (join->session->killed)                    // Aborted by user
 
12109
  {
 
12110
    join->session->send_kill_message();
 
12111
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12112
  }
 
12113
 
 
12114
  join->found_records++;
 
12115
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12116
  /* Make a key of group index */
 
12117
  for (group=table->group ; group ; group=group->next)
 
12118
  {
 
12119
    Item *item= *group->item;
 
12120
    item->save_org_in_field(group->field);
 
12121
    /* Store in the used key if the field was 0 */
 
12122
    if (item->maybe_null)
 
12123
      group->buff[-1]= (char) group->field->is_null();
 
12124
  }
 
12125
  if (!table->file->index_read_map(table->record[1],
 
12126
                                   join->tmp_table_param.group_buff,
 
12127
                                   HA_WHOLE_KEY,
 
12128
                                   HA_READ_KEY_EXACT))
 
12129
  {                                             /* Update old record */
 
12130
    restore_record(table,record[1]);
 
12131
    update_tmptable_sum_func(join->sum_funcs,table);
 
12132
    if ((error=table->file->ha_update_row(table->record[1],
 
12133
                                          table->record[0])))
 
12134
    {
 
12135
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12136
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12137
    }
 
12138
    return(NESTED_LOOP_OK);
 
12139
  }
 
12140
 
 
12141
  /*
 
12142
    Copy null bits from group key to table
 
12143
    We can't copy all data as the key may have different format
 
12144
    as the row data (for example as with VARCHAR keys)
 
12145
  */
 
12146
  KEY_PART_INFO *key_part;
 
12147
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12148
       group ;
 
12149
       group=group->next,key_part++)
 
12150
  {
 
12151
    if (key_part->null_bit)
 
12152
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12153
  }
 
12154
  init_tmptable_sum_functions(join->sum_funcs);
 
12155
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12156
  if ((error=table->file->ha_write_row(table->record[0])))
 
12157
  {
 
12158
    if (create_myisam_from_heap(join->session, table,
 
12159
                                join->tmp_table_param.start_recinfo,
 
12160
                                &join->tmp_table_param.recinfo,
 
12161
                                error, 0))
 
12162
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12163
    /* Change method to update rows */
 
12164
    table->file->ha_index_init(0, 0);
 
12165
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12166
  }
 
12167
  join->send_records++;
 
12168
  return(NESTED_LOOP_OK);
 
12169
}
 
12170
 
 
12171
 
 
12172
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12173
 
 
12174
static enum_nested_loop_state
 
12175
end_unique_update(JOIN *join, JOIN_TAB *,
 
12176
                  bool end_of_records)
 
12177
{
 
12178
  Table *table=join->tmp_table;
 
12179
  int     error;
 
12180
 
 
12181
  if (end_of_records)
 
12182
    return(NESTED_LOOP_OK);
 
12183
  if (join->session->killed)                    // Aborted by user
 
12184
  {
 
12185
    join->session->send_kill_message();
 
12186
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12187
  }
 
12188
 
 
12189
  init_tmptable_sum_functions(join->sum_funcs);
 
12190
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12191
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12192
 
 
12193
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12194
    join->send_records++;                       // New group
 
12195
  else
 
12196
  {
 
12197
    if ((int) table->file->get_dup_key(error) < 0)
 
12198
    {
 
12199
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12200
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12201
    }
 
12202
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12203
    {
 
12204
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12205
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12206
    }
 
12207
    restore_record(table,record[1]);
 
12208
    update_tmptable_sum_func(join->sum_funcs,table);
 
12209
    if ((error=table->file->ha_update_row(table->record[1],
 
12210
                                          table->record[0])))
 
12211
    {
 
12212
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12213
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12214
    }
 
12215
  }
 
12216
  return(NESTED_LOOP_OK);
 
12217
}
 
12218
 
 
12219
 
 
12220
/* ARGSUSED */
 
12221
enum_nested_loop_state
 
12222
end_write_group(JOIN *join, JOIN_TAB *,
 
12223
                bool end_of_records)
3895
12224
{
3896
12225
  Table *table=join->tmp_table;
3897
12226
  int     idx= -1;
3898
12227
 
3899
 
  if (join->session->getKilled())
 
12228
  if (join->session->killed)
3900
12229
  {                                             // Aborted by user
3901
12230
    join->session->send_kill_message();
3902
 
    return NESTED_LOOP_KILLED;
 
12231
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3903
12232
  }
3904
12233
  if (!join->first_record || end_of_records ||
3905
12234
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3909
12238
      int send_group_parts= join->send_group_parts;
3910
12239
      if (idx < send_group_parts)
3911
12240
      {
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;
 
12241
        if (!join->first_record)
 
12242
        {
 
12243
          /* No matching rows for group function */
 
12244
          join->clear();
 
12245
        }
 
12246
        copy_sum_funcs(join->sum_funcs,
 
12247
                       join->sum_funcs_end[send_group_parts]);
 
12248
        if (!join->having || join->having->val_int())
 
12249
        {
 
12250
          int error= table->file->ha_write_row(table->record[0]);
 
12251
          if (error && create_myisam_from_heap(join->session, table,
 
12252
                                               join->tmp_table_param.start_recinfo,
 
12253
                                                &join->tmp_table_param.recinfo,
 
12254
                                               error, 0))
 
12255
            return(NESTED_LOOP_ERROR);
 
12256
        }
 
12257
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
12258
        {
 
12259
          if (join->rollup_write_data((uint) (idx+1), table))
 
12260
            return(NESTED_LOOP_ERROR);
 
12261
        }
 
12262
        if (end_of_records)
 
12263
          return(NESTED_LOOP_OK);
3935
12264
      }
3936
12265
    }
3937
12266
    else
3938
12267
    {
3939
12268
      if (end_of_records)
3940
 
        return NESTED_LOOP_OK;
 
12269
        return(NESTED_LOOP_OK);
3941
12270
      join->first_record=1;
3942
12271
      test_if_item_cache_changed(join->group_fields);
3943
12272
    }
3944
12273
    if (idx < (int) join->send_group_parts)
3945
12274
    {
3946
12275
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
12276
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
12277
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return NESTED_LOOP_ERROR;
3951
 
      return NESTED_LOOP_OK;
 
12278
        return(NESTED_LOOP_ERROR);
 
12279
      return(NESTED_LOOP_OK);
3952
12280
    }
3953
12281
  }
3954
12282
  if (update_sum_func(join->sum_funcs))
3955
 
    return NESTED_LOOP_ERROR;
3956
 
  return NESTED_LOOP_OK;
 
12283
    return(NESTED_LOOP_ERROR);
 
12284
  return(NESTED_LOOP_OK);
3957
12285
}
3958
12286
 
 
12287
 
3959
12288
/*****************************************************************************
3960
12289
  Remove calculation with tables that aren't yet read. Remove also tests
3961
12290
  against fields that are read through key where the table is not a
3962
12291
  outer join table.
3963
12292
  We can't remove tests that are made against columns which are stored
3964
12293
  in sorted order.
 
12294
*****************************************************************************/
 
12295
 
 
12296
/**
3965
12297
  @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)
 
12298
    1 if right_item is used removable reference key on left_item
 
12299
*/
 
12300
 
 
12301
static bool test_if_ref(Item_field *left_item,Item *right_item)
3970
12302
{
3971
12303
  Field *field=left_item->field;
3972
12304
  // 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)
 
12305
  if (!field->table->const_table && !field->table->maybe_null)
3974
12306
  {
3975
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12307
    Item *ref_item=part_of_refkey(field->table,field);
3976
12308
    if (ref_item && ref_item->eq(right_item,1))
3977
12309
    {
3978
12310
      right_item= right_item->real_item();
3979
12311
      if (right_item->type() == Item::FIELD_ITEM)
3980
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12312
        return (field->eq_def(((Item_field *) right_item)->field));
3981
12313
      /* remove equalities injected by IN->EXISTS transformation */
3982
12314
      else if (right_item->type() == Item::CACHE_ITEM)
3983
12315
        return ((Item_cache *)right_item)->eq_def (field);
3984
12316
      if (right_item->const_item() && !(right_item->is_null()))
3985
12317
      {
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
 
12318
        /*
 
12319
          We can remove binary fields and numerical fields except float,
 
12320
          as float comparison isn't 100 % secure
 
12321
          We have to keep normal strings to be able to check for end spaces
3990
12322
 
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
 
        }
 
12323
          sergefp: the above seems to be too restrictive. Counterexample:
 
12324
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12325
            insert into t100 values ('a'),('a ');
 
12326
            explain select * from t100 where v='a';
 
12327
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12328
          rows, so it seems there are no problems with endspace in the most
 
12329
          frequent case?
 
12330
        */
 
12331
        if (field->binary() &&
 
12332
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12333
            field->decimals() == 0)
 
12334
        {
 
12335
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12336
        }
4005
12337
      }
4006
12338
    }
4007
12339
  }
4008
 
  return 0;
 
12340
  return 0;                                     // keep test
 
12341
}
 
12342
 
 
12343
/**
 
12344
   @brief Replaces an expression destructively inside the expression tree of
 
12345
   the WHERE clase.
 
12346
 
 
12347
   @note Because of current requirements for semijoin flattening, we do not
 
12348
   need to recurse here, hence this function will only examine the top-level
 
12349
   AND conditions. (see JOIN::prepare, comment above the line
 
12350
   'if (do_materialize)'
 
12351
 
 
12352
   @param join The top-level query.
 
12353
   @param old_cond The expression to be replaced.
 
12354
   @param new_cond The expression to be substituted.
 
12355
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
 
12356
   the new expression.
 
12357
   @return <code>true</code> if there was an error, <code>false</code> if
 
12358
   successful.
 
12359
*/
 
12360
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
12361
                                       Item *new_cond, bool do_fix_fields)
 
12362
{
 
12363
  if (join->conds == old_cond) {
 
12364
    join->conds= new_cond;
 
12365
    if (do_fix_fields)
 
12366
      new_cond->fix_fields(join->session, &join->conds);
 
12367
    return false;
 
12368
  }
 
12369
 
 
12370
  if (join->conds->type() == Item::COND_ITEM) {
 
12371
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12372
    Item *item;
 
12373
    while ((item= li++))
 
12374
      if (item == old_cond)
 
12375
      {
 
12376
        li.replace(new_cond);
 
12377
        if (do_fix_fields)
 
12378
          new_cond->fix_fields(join->session, li.ref());
 
12379
        return false;
 
12380
      }
 
12381
  }
 
12382
 
 
12383
  return true;
4009
12384
}
4010
12385
 
4011
12386
/*
4039
12414
  RETURN
4040
12415
    Extracted condition
4041
12416
*/
4042
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12417
 
 
12418
static COND *
 
12419
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12420
                    bool exclude_expensive_cond)
4043
12421
{
4044
12422
  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()))
 
12423
      /*
 
12424
        Exclude constant conditions not checked at optimization time if
 
12425
        the table we are pushing conditions to is the first one.
 
12426
        As a result, such conditions are not considered as already checked
 
12427
        and will be checked at execution time, attached to the first table.
 
12428
      */
 
12429
      !((used_table & 1) && cond->is_expensive()))
4052
12430
    return (COND*) 0;                           // Already checked
4053
12431
  if (cond->type() == Item::COND_ITEM)
4054
12432
  {
4057
12435
      /* Create new top level AND item */
4058
12436
      Item_cond_and *new_cond=new Item_cond_and;
4059
12437
      if (!new_cond)
4060
 
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12438
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12439
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
12440
      Item *item;
4063
12441
      while ((item=li++))
4064
12442
      {
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);
 
12443
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12444
                                      exclude_expensive_cond);
 
12445
        if (fix)
 
12446
          new_cond->argument_list()->push_back(fix);
4069
12447
      }
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;
 
12448
      switch (new_cond->argument_list()->elements) {
 
12449
      case 0:
 
12450
        return (COND*) 0;                       // Always true
 
12451
      case 1:
 
12452
        return new_cond->argument_list()->head();
 
12453
      default:
 
12454
        /*
 
12455
          Item_cond_and do not need fix_fields for execution, its parameters
 
12456
          are fixed or do not need fix_fields, too
 
12457
        */
 
12458
        new_cond->quick_fix_field();
 
12459
        new_cond->used_tables_cache=
 
12460
          ((Item_cond_and*) cond)->used_tables_cache &
 
12461
          tables;
 
12462
        return new_cond;
4084
12463
      }
4085
12464
    }
4086
12465
    else
4087
12466
    {                                           // Or list
4088
12467
      Item_cond_or *new_cond=new Item_cond_or;
4089
12468
      if (!new_cond)
4090
 
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
12469
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
12470
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
12471
      Item *item;
4093
12472
      while ((item=li++))
4094
12473
      {
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);
 
12474
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12475
        if (!fix)
 
12476
          return (COND*) 0;                     // Always true
 
12477
        new_cond->argument_list()->push_back(fix);
4099
12478
      }
4100
12479
      /*
4101
 
        Item_cond_and do not need fix_fields for execution, its parameters
4102
 
        are fixed or do not need fix_fields, too
 
12480
        Item_cond_and do not need fix_fields for execution, its parameters
 
12481
        are fixed or do not need fix_fields, too
4103
12482
      */
4104
12483
      new_cond->quick_fix_field();
4105
12484
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4132
12511
  {
4133
12512
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4134
12513
    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))
 
12514
    if (left_item->type() == Item::FIELD_ITEM &&
 
12515
        test_if_ref((Item_field*) left_item,right_item))
4136
12516
    {
4137
12517
      cond->marker=3;                   // Checked when read
4138
12518
      return (COND*) 0;
4139
12519
    }
4140
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12520
    if (right_item->type() == Item::FIELD_ITEM &&
 
12521
        test_if_ref((Item_field*) right_item,left_item))
4141
12522
    {
4142
12523
      cond->marker=3;                   // Checked when read
4143
12524
      return (COND*) 0;
4147
12528
  return cond;
4148
12529
}
4149
12530
 
4150
 
static Item *part_of_refkey(Table *table,Field *field)
 
12531
 
 
12532
static Item *
 
12533
part_of_refkey(Table *table,Field *field)
4151
12534
{
4152
12535
  if (!table->reginfo.join_tab)
4153
12536
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4155
12538
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4156
12539
  if (ref_parts)
4157
12540
  {
4158
 
    KeyPartInfo *key_part=
 
12541
    KEY_PART_INFO *key_part=
4159
12542
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4160
12543
    uint32_t part;
4161
12544
 
4166
12549
    }
4167
12550
 
4168
12551
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
12552
      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
 
      {
 
12553
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
12554
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
12555
  }
4180
12556
  return (Item*) 0;
4181
12557
}
4182
12558
 
 
12559
 
4183
12560
/**
4184
12561
  Test if one can use the key to resolve order_st BY.
4185
12562
 
4200
12577
  @retval
4201
12578
    -1   Reverse key can be used
4202
12579
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12580
 
 
12581
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12582
                                uint32_t *used_key_parts)
4204
12583
{
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;
 
12584
  KEY_PART_INFO *key_part,*key_part_end;
 
12585
  key_part=table->key_info[idx].key_part;
 
12586
  key_part_end=key_part+table->key_info[idx].key_parts;
4209
12587
  key_part_map const_key_parts=table->const_key_parts[idx];
4210
 
  int reverse= 0;
 
12588
  int reverse=0;
4211
12589
  bool on_primary_key= false;
4212
12590
 
4213
12591
  for (; order ; order=order->next, const_key_parts>>=1)
4217
12595
 
4218
12596
    /*
4219
12597
      Skip key parts that are constants in the WHERE clause.
4220
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12598
      These are already skipped in the order_st BY by const_expression_in_where()
4221
12599
    */
4222
12600
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4223
12601
      key_part++;
4230
12608
        the primary key as a suffix.
4231
12609
      */
4232
12610
      if (!on_primary_key &&
4233
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4234
 
          table->getShare()->hasPrimaryKey())
 
12611
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12612
          table->s->primary_key != MAX_KEY)
4235
12613
      {
4236
12614
        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()];
 
12615
        key_part= table->key_info[table->s->primary_key].key_part;
 
12616
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12617
        const_key_parts=table->const_key_parts[table->s->primary_key];
4240
12618
 
4241
12619
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4242
12620
          key_part++;
4263
12641
    key_part++;
4264
12642
  }
4265
12643
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4266
 
    (uint32_t) (key_part - table->key_info[idx].key_part);
4267
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
12644
    (uint) (key_part - table->key_info[idx].key_part);
 
12645
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4268
12646
                         HA_READ_PREV))
4269
12647
    reverse= 0;                                 // Index can't be used
4270
12648
  return(reverse);
4271
12649
}
4272
12650
 
 
12651
 
4273
12652
/**
4274
12653
  Test if a second key is the subkey of the first one.
4275
12654
 
4285
12664
  @retval
4286
12665
    0   no sub key
4287
12666
*/
4288
 
inline bool is_subkey(KeyPartInfo *key_part,
4289
 
                      KeyPartInfo *ref_key_part,
4290
 
                      KeyPartInfo *ref_key_part_end)
 
12667
 
 
12668
inline bool
 
12669
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12670
          KEY_PART_INFO *ref_key_part_end)
4291
12671
{
4292
12672
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4293
 
    if (! key_part->field->eq(ref_key_part->field))
 
12673
    if (!key_part->field->eq(ref_key_part->field))
4294
12674
      return 0;
4295
12675
  return 1;
4296
12676
}
4306
12686
    - MAX_KEY                   If we can't use other key
4307
12687
    - the number of found key   Otherwise
4308
12688
*/
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)
 
12689
 
 
12690
static uint
 
12691
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12692
               const key_map *usable_keys)
4314
12693
{
4315
12694
  uint32_t nr;
4316
12695
  uint32_t min_length= UINT32_MAX;
4317
12696
  uint32_t best= MAX_KEY;
4318
12697
  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;
 
12698
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12699
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4321
12700
 
4322
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12701
  for (nr= 0 ; nr < table->s->keys ; nr++)
4323
12702
  {
4324
 
    if (usable_keys->test(nr) &&
 
12703
    if (usable_keys->is_set(nr) &&
4325
12704
        table->key_info[nr].key_length < min_length &&
4326
12705
        table->key_info[nr].key_parts >= ref_key_parts &&
4327
12706
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4335
12714
  return best;
4336
12715
}
4337
12716
 
 
12717
 
4338
12718
/**
4339
12719
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4340
12720
  already known to be distinct.
4366
12746
  @retval
4367
12747
    0                    not found.
4368
12748
*/
4369
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12749
 
 
12750
static bool
 
12751
list_contains_unique_index(Table *table,
 
12752
                          bool (*find_func) (Field *, void *), void *data)
4370
12753
{
4371
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12754
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
4372
12755
  {
4373
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12756
    if (keynr == table->s->primary_key ||
4374
12757
         (table->key_info[keynr].flags & HA_NOSAME))
4375
12758
    {
4376
 
      KeyInfo *keyinfo= table->key_info + keynr;
4377
 
      KeyPartInfo *key_part= NULL;
4378
 
      KeyPartInfo *key_part_end= NULL;
 
12759
      KEY *keyinfo= table->key_info + keynr;
 
12760
      KEY_PART_INFO *key_part, *key_part_end;
4379
12761
 
4380
12762
      for (key_part=keyinfo->key_part,
4381
12763
           key_part_end=key_part+ keyinfo->key_parts;
4383
12765
           key_part++)
4384
12766
      {
4385
12767
        if (key_part->field->maybe_null() ||
4386
 
            ! find_func(key_part->field, data))
 
12768
            !find_func(key_part->field, data))
4387
12769
          break;
4388
12770
      }
4389
12771
      if (key_part == key_part_end)
4393
12775
  return 0;
4394
12776
}
4395
12777
 
 
12778
 
4396
12779
/**
4397
12780
  Helper function for list_contains_unique_index.
4398
12781
  Find a field reference in a list of order_st structures.
4406
12789
  @retval
4407
12790
    0                    not found.
4408
12791
*/
4409
 
bool find_field_in_order_list (Field *field, void *data)
 
12792
 
 
12793
static bool
 
12794
find_field_in_order_list (Field *field, void *data)
4410
12795
{
4411
 
  Order *group= (Order *) data;
 
12796
  order_st *group= (order_st *) data;
4412
12797
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12798
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
12799
  {
4415
12800
    Item *item= (*tmp_group->item)->real_item();
4416
12801
    if (item->type() == Item::FIELD_ITEM &&
4423
12808
  return part_found;
4424
12809
}
4425
12810
 
 
12811
 
4426
12812
/**
4427
12813
  Helper function for list_contains_unique_index.
4428
12814
  Find a field reference in a dynamic list of Items.
4436
12822
  @retval
4437
12823
    0                    not found.
4438
12824
*/
4439
 
bool find_field_in_item_list (Field *field, void *data)
 
12825
 
 
12826
static bool
 
12827
find_field_in_item_list (Field *field, void *data)
4440
12828
{
4441
12829
  List<Item> *fields= (List<Item> *) data;
4442
12830
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
12831
  List_iterator<Item> li(*fields);
4444
12832
  Item *item;
4445
12833
 
4446
12834
  while ((item= li++))
4455
12843
  return part_found;
4456
12844
}
4457
12845
 
 
12846
 
4458
12847
/**
4459
 
  Test if we can skip the ORDER BY by using an index.
 
12848
  Test if we can skip the order_st BY by using an index.
4460
12849
 
4461
12850
  SYNOPSIS
4462
12851
    test_if_skip_sort_order()
4466
12855
      no_changes
4467
12856
      map
4468
12857
 
4469
 
  If we can use an index, the JoinTable / tab->select struct
 
12858
  If we can use an index, the JOIN_TAB / tab->select struct
4470
12859
  is changed to use the index.
4471
12860
 
4472
12861
  The index must cover all fields in <order>, or it will not be considered.
4480
12869
  @retval
4481
12870
    1    We can use an index.
4482
12871
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12872
 
 
12873
static bool
 
12874
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12875
                        bool no_changes, const key_map *map)
4484
12876
{
4485
12877
  int32_t ref_key;
4486
12878
  uint32_t ref_key_parts;
4487
12879
  int order_direction;
4488
12880
  uint32_t used_key_parts;
4489
12881
  Table *table=tab->table;
4490
 
  optimizer::SqlSelect *select= tab->select;
 
12882
  SQL_SELECT *select=tab->select;
4491
12883
  key_map usable_keys;
4492
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12884
  QUICK_SELECT_I *save_quick= 0;
4493
12885
 
4494
12886
  /*
4495
12887
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4497
12889
  */
4498
12890
  usable_keys= *map;
4499
12891
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12892
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
12893
  {
4502
12894
    Item *item= (*tmp_order->item)->real_item();
4503
12895
    if (item->type() != Item::FIELD_ITEM)
4504
12896
    {
4505
 
      usable_keys.reset();
 
12897
      usable_keys.clear_all();
4506
12898
      return(0);
4507
12899
    }
4508
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4509
 
    if (usable_keys.none())
 
12900
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12901
    if (usable_keys.is_clear_all())
4510
12902
      return(0);                                        // No usable keys
4511
12903
  }
4512
12904
 
4516
12908
  {
4517
12909
    ref_key=       tab->ref.key;
4518
12910
    ref_key_parts= tab->ref.key_parts;
4519
 
    if (tab->type == AM_REF_OR_NULL)
 
12911
    if (tab->type == JT_REF_OR_NULL)
4520
12912
      return(0);
4521
12913
  }
4522
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12914
  else if (select && select->quick)             // Range found by opt_range
4523
12915
  {
4524
12916
    int quick_type= select->quick->get_type();
4525
12917
    save_quick= select->quick;
4526
12918
    /*
4527
12919
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
12920
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
12921
      by clustered PK values.
4530
12922
    */
4531
12923
 
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)
 
12924
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
12925
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
12926
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4535
12927
      return(0);
4536
12928
    ref_key=       select->quick->index;
4537
12929
    ref_key_parts= select->quick->used_key_parts;
4542
12934
    /*
4543
12935
      We come here when there is a REF key.
4544
12936
    */
4545
 
    if (! usable_keys.test(ref_key))
 
12937
    if (!usable_keys.is_set(ref_key))
4546
12938
    {
4547
12939
      /*
4548
 
        We come here when ref_key is not among usable_keys
 
12940
        We come here when ref_key is not among usable_keys
4549
12941
      */
4550
12942
      uint32_t new_ref_key;
4551
12943
      /*
4552
 
        If using index only read, only consider other possible index only
4553
 
        keys
 
12944
        If using index only read, only consider other possible index only
 
12945
        keys
4554
12946
      */
4555
 
      if (table->covering_keys.test(ref_key))
4556
 
        usable_keys&= table->covering_keys;
 
12947
      if (table->covering_keys.is_set(ref_key))
 
12948
        usable_keys.intersect(table->covering_keys);
4557
12949
      if (tab->pre_idx_push_select_cond)
4558
12950
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4559
12951
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4560
12952
                                       &usable_keys)) < MAX_KEY)
4561
12953
      {
4562
 
        /* Found key that can be used to retrieve data in sorted order */
4563
 
        if (tab->ref.key >= 0)
4564
 
        {
 
12954
        /* Found key that can be used to retrieve data in sorted order */
 
12955
        if (tab->ref.key >= 0)
 
12956
        {
4565
12957
          /*
4566
12958
            We'll use ref access method on key new_ref_key. In general case
4567
12959
            the index search tuple for new_ref_key will be different (e.g.
4570
12962
            "part1 = const1 AND part2=const2".
4571
12963
            So we build tab->ref from scratch here.
4572
12964
          */
4573
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4574
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12965
          KEYUSE *keyuse= tab->keyuse;
 
12966
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4575
12967
            keyuse++;
4576
12968
 
4577
12969
          if (create_ref_for_key(tab->join, tab, keyuse,
4578
12970
                                 tab->join->const_table_map))
4579
12971
            return(0);
4580
 
        }
4581
 
        else
4582
 
        {
 
12972
        }
 
12973
        else
 
12974
        {
4583
12975
          /*
4584
 
            The range optimizer constructed QuickRange for ref_key, and
 
12976
            The range optimizer constructed QUICK_RANGE for ref_key, and
4585
12977
            we want to use instead new_ref_key as the index. We can't
4586
12978
            just change the index of the quick select, because this may
4587
12979
            result in an incosistent QUICK_SELECT object. Below we
4589
12981
            parameres are set correctly by the range optimizer.
4590
12982
           */
4591
12983
          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.
 
12984
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
12985
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4594
12986
 
4595
12987
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
4596
12988
                                        (tab->join->select_options &
4600
12992
                                        true) <=
4601
12993
              0)
4602
12994
            return(0);
4603
 
        }
 
12995
        }
4604
12996
        ref_key= new_ref_key;
4605
12997
      }
4606
12998
    }
4607
12999
    /* Check if we get the rows in requested sorted order by using the key */
4608
 
    if (usable_keys.test(ref_key) &&
 
13000
    if (usable_keys.is_set(ref_key) &&
4609
13001
        (order_direction= test_if_order_by_key(order,table,ref_key,
4610
13002
                                               &used_key_parts)))
4611
13003
      goto check_reverse_order;
4626
13018
    int best_key= -1;
4627
13019
    bool is_best_covering= false;
4628
13020
    double fanout= 1;
4629
 
    Join *join= tab->join;
 
13021
    JOIN *join= tab->join;
4630
13022
    uint32_t tablenr= tab - join->join_tab;
4631
 
    ha_rows table_records= table->cursor->stats.records;
 
13023
    ha_rows table_records= table->file->stats.records;
4632
13024
    bool group= join->group && order == join->group_list;
4633
 
    optimizer::Position cur_pos;
4634
13025
 
4635
13026
    /*
4636
13027
      If not used with LIMIT, only use keys if the whole query can be
4643
13034
        filesort() and join cache are usually faster than reading in
4644
13035
        index order and not using join cache
4645
13036
        */
4646
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13037
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4647
13038
        return(0);
4648
 
      keys= *table->cursor->keys_to_use_for_scanning();
4649
 
      keys|= table->covering_keys;
 
13039
      keys= *table->file->keys_to_use_for_scanning();
 
13040
      keys.merge(table->covering_keys);
4650
13041
 
4651
13042
      /*
4652
 
        We are adding here also the index specified in FORCE INDEX clause,
4653
 
        if any.
 
13043
        We are adding here also the index specified in FORCE INDEX clause,
 
13044
        if any.
4654
13045
        This is to allow users to use index in order_st BY.
4655
13046
      */
4656
13047
      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;
 
13048
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13049
                           table->keys_in_use_for_order_by);
 
13050
      keys.intersect(usable_keys);
4660
13051
    }
4661
13052
    else
4662
13053
      keys= usable_keys;
4663
13054
 
4664
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4665
 
    read_time= cur_pos.getCost();
 
13055
    read_time= join->best_positions[tablenr].read_time;
4666
13056
    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
 
    }
 
13057
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4671
13058
 
4672
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13059
    for (nr=0; nr < table->s->keys ; nr++)
4673
13060
    {
4674
13061
      int direction;
4675
 
      if (keys.test(nr) &&
 
13062
      if (keys.is_set(nr) &&
4676
13063
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4677
13064
      {
4678
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
 
13065
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
4679
13066
 
4680
13067
        /*
4681
 
          Don't use an index scan with ORDER BY without limit.
 
13068
          Don't use an index scan with order_st BY without limit.
4682
13069
          For GROUP BY without limit always use index scan
4683
13070
          if there is a suitable index.
4684
13071
          Why we hold to this asymmetry hardly can be explained
4685
13072
          rationally. It's easy to demonstrate that using
4686
13073
          temporary table + filesort could be cheaper for grouping
4687
13074
          queries too.
4688
 
        */
 
13075
        */
4689
13076
        if (is_covering ||
4690
13077
            select_limit != HA_POS_ERROR ||
4691
13078
            (ref_key < 0 && (group || table->force_index)))
4692
13079
        {
4693
13080
          double rec_per_key;
4694
13081
          double index_scan_time;
4695
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13082
          KEY *keyinfo= tab->table->key_info+nr;
4696
13083
          if (select_limit == HA_POS_ERROR)
4697
13084
            select_limit= table_records;
4698
13085
          if (group)
4699
13086
          {
4700
13087
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4701
 
            set_if_bigger(rec_per_key, 1.0);
 
13088
            set_if_bigger(rec_per_key, 1);
4702
13089
            /*
4703
13090
              With a grouping query each group containing on average
4704
13091
              rec_per_key records produces only one row that will
4705
13092
              be included into the result set.
4706
 
            */
 
13093
            */
4707
13094
            if (select_limit > table_records/rec_per_key)
4708
13095
                select_limit= table_records;
4709
13096
            else
4718
13105
            So the estimate for L/fanout(tk,tn) will be too optimistic
4719
13106
            and as result we'll choose an index scan when using ref/range
4720
13107
            access + filesort will be cheaper.
4721
 
          */
 
13108
          */
4722
13109
          select_limit= (ha_rows) (select_limit < fanout ?
4723
13110
                                   1 : select_limit/fanout);
4724
13111
          /*
4738
13125
                                     (double) table_records /
4739
13126
                                      table->quick_condition_rows);
4740
13127
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4741
 
          set_if_bigger(rec_per_key, 1.0);
 
13128
          set_if_bigger(rec_per_key, 1);
4742
13129
          /*
4743
13130
            Here we take into account the fact that rows are
4744
13131
            accessed in sequences rec_per_key records in each.
4745
13132
            Rows in such a sequence are supposed to be ordered
4746
13133
            by rowid/primary key. When reading the data
4747
13134
            in a sequence we'll touch not more pages than the
4748
 
            table cursor contains.
 
13135
            table file contains.
4749
13136
            TODO. Use the formula for a disk sweep sequential access
4750
13137
            to calculate the cost of accessing data rows for one
4751
13138
            index entry.
4752
 
          */
 
13139
          */
4753
13140
          index_scan_time= select_limit/rec_per_key *
4754
 
                           min(rec_per_key, table->cursor->scan_time());
 
13141
                           cmin(rec_per_key, table->file->scan_time());
4755
13142
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4756
13143
              index_scan_time < read_time)
4757
13144
          {
4758
13145
            ha_rows quick_records= table_records;
4759
13146
            if (is_best_covering && !is_covering)
4760
13147
              continue;
4761
 
            if (table->quick_keys.test(nr))
 
13148
            if (table->quick_keys.is_set(nr))
4762
13149
              quick_records= table->quick_rows[nr];
4763
13150
            if (best_key < 0 ||
4764
 
                (select_limit <= min(quick_records,best_records) ?
 
13151
                (select_limit <= cmin(quick_records,best_records) ?
4765
13152
                 keyinfo->key_parts < best_key_parts :
4766
13153
                 quick_records < best_records))
4767
13154
            {
4772
13159
              best_key_direction= direction;
4773
13160
            }
4774
13161
          }
4775
 
        }
 
13162
        }
4776
13163
      }
4777
13164
    }
4778
13165
    if (best_key >= 0)
4779
13166
    {
4780
13167
      bool quick_created= false;
4781
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13168
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4782
13169
      {
4783
 
        key_map test_map;
4784
 
        test_map.reset();       // Force the creation of quick select
4785
 
        test_map.set(best_key); // only best_key.
 
13170
        key_map map;
 
13171
        map.clear_all();       // Force the creation of quick select
 
13172
        map.set_bit(best_key); // only best_key.
4786
13173
        quick_created=
4787
 
          select->test_quick_select(join->session, test_map, 0,
 
13174
          select->test_quick_select(join->session, map, 0,
4788
13175
                                    join->select_options & OPTION_FOUND_ROWS ?
4789
13176
                                    HA_POS_ERROR :
4790
13177
                                    join->unit->select_limit_cnt,
4793
13180
      if (!no_changes)
4794
13181
      {
4795
13182
        if (!quick_created)
4796
 
        {
 
13183
        {
4797
13184
          tab->index= best_key;
4798
13185
          tab->read_first_record= best_key_direction > 0 ?
4799
13186
                                  join_read_first:join_read_last;
4800
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13187
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4801
13188
          if (select && select->quick)
4802
13189
          {
4803
 
            safe_delete(select->quick);
 
13190
            delete select->quick;
 
13191
            select->quick= 0;
4804
13192
          }
4805
 
          if (table->covering_keys.test(best_key))
 
13193
          if (table->covering_keys.is_set(best_key))
4806
13194
          {
4807
13195
            table->key_read=1;
4808
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13196
            table->file->extra(HA_EXTRA_KEYREAD);
4809
13197
          }
4810
 
          table->cursor->ha_index_or_rnd_end();
 
13198
          table->file->ha_index_or_rnd_end();
4811
13199
          if (join->select_options & SELECT_DESCRIBE)
4812
13200
          {
4813
13201
            tab->ref.key= -1;
4816
13204
              tab->limit= select_limit;
4817
13205
          }
4818
13206
        }
4819
 
        else if (tab->type != AM_ALL)
 
13207
        else if (tab->type != JT_ALL)
4820
13208
        {
4821
13209
          /*
4822
13210
            We're about to use a quick access to the table.
4824
13212
            method is actually used.
4825
13213
          */
4826
13214
          assert(tab->select->quick);
4827
 
          tab->type= AM_ALL;
 
13215
          tab->type=JT_ALL;
4828
13216
          tab->use_quick=1;
4829
13217
          tab->ref.key= -1;
4830
13218
          tab->ref.key_parts=0;         // Don't use ref key.
4831
13219
          tab->read_first_record= join_init_read_record;
 
13220
          /*
 
13221
            TODO: update the number of records in join->best_positions[tablenr]
 
13222
          */
4832
13223
        }
4833
13224
      }
4834
13225
      used_key_parts= best_key_parts;
4839
13230
  }
4840
13231
 
4841
13232
check_reverse_order:
4842
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13233
  if (order_direction == -1)            // If order_st BY ... DESC
4843
13234
  {
4844
13235
    if (select && select->quick)
4845
13236
    {
4846
13237
      /*
4847
 
        Don't reverse the sort order, if it's already done.
 
13238
        Don't reverse the sort order, if it's already done.
4848
13239
        (In some cases test_if_order_by_key() can be called multiple times
4849
13240
      */
4850
 
      if (! select->quick->reverse_sorted())
 
13241
      if (!select->quick->reverse_sorted())
4851
13242
      {
4852
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13243
        QUICK_SELECT_DESC *tmp;
4853
13244
        bool error= false;
4854
13245
        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)
 
13246
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13247
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13248
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13249
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4859
13250
        {
4860
13251
          tab->limit= 0;
4861
13252
          select->quick= save_quick;
4862
 
          return 0; // Use filesort
 
13253
          return(0);                   // Use filesort
4863
13254
        }
4864
13255
 
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;
 
13256
        /* order_st BY range_key DESC */
 
13257
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13258
                                    used_key_parts, &error);
 
13259
        if (!tmp || error)
 
13260
        {
 
13261
          delete tmp;
4872
13262
          select->quick= save_quick;
4873
13263
          tab->limit= 0;
4874
 
          return 0; // Reverse sort not supported
4875
 
        }
4876
 
        select->quick= tmp;
 
13264
          return(0);            // Reverse sort not supported
 
13265
        }
 
13266
        select->quick=tmp;
4877
13267
      }
4878
13268
    }
4879
 
    else if (tab->type != AM_NEXT &&
 
13269
    else if (tab->type != JT_NEXT &&
4880
13270
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4881
13271
    {
4882
13272
      /*
4883
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13273
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4884
13274
 
4885
 
        Use a traversal function that starts by reading the last row
4886
 
        with key part (A) and then traverse the index backwards.
 
13275
        Use a traversal function that starts by reading the last row
 
13276
        with key part (A) and then traverse the index backwards.
4887
13277
      */
4888
13278
      tab->read_first_record= join_read_last_key;
4889
13279
      tab->read_record.read_record= join_read_prev_same;
4891
13281
  }
4892
13282
  else if (select && select->quick)
4893
13283
    select->quick->sorted= 1;
4894
 
  return 1;
 
13284
  return(1);
4895
13285
}
4896
13286
 
 
13287
 
4897
13288
/*
4898
13289
  If not selecting by given key, create an index how records should be read
4899
13290
 
4900
13291
  SYNOPSIS
4901
13292
   create_sort_index()
4902
 
     session            Thread Cursor
 
13293
     session            Thread handler
4903
13294
     tab                Table to sort (in join structure)
4904
13295
     order              How table should be sorted
4905
13296
     filesort_limit     Max number of rows that needs to be sorted
4912
13303
  IMPLEMENTATION
4913
13304
   - If there is an index that can be used, 'tab' is modified to use
4914
13305
     this index.
4915
 
   - If no index, create with filesort() an index cursor that can be used to
 
13306
   - If no index, create with filesort() an index file that can be used to
4916
13307
     retrieve rows in order (should be done with 'read_record').
4917
13308
     The sorted data is stored in tab->table and will be freed when calling
4918
 
     tab->table->free_io_cache().
 
13309
     free_io_cache(tab->table).
4919
13310
 
4920
13311
  RETURN VALUES
4921
13312
    0           ok
4922
13313
    -1          Some fatal error
4923
13314
    1           No records
4924
13315
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13316
 
 
13317
static int
 
13318
create_sort_index(Session *session, JOIN *join, order_st *order,
 
13319
                  ha_rows filesort_limit, ha_rows select_limit,
 
13320
                  bool is_order_by)
4926
13321
{
4927
13322
  uint32_t length= 0;
4928
13323
  ha_rows examined_rows;
4929
13324
  Table *table;
4930
 
  optimizer::SqlSelect *select= NULL;
4931
 
  JoinTable *tab;
 
13325
  SQL_SELECT *select;
 
13326
  JOIN_TAB *tab;
4932
13327
 
4933
13328
  if (join->tables == join->const_tables)
4934
13329
    return(0);                          // One row, no need to sort
4944
13339
  */
4945
13340
  if ((order != join->group_list ||
4946
13341
       !(join->select_options & SELECT_BIG_RESULT) ||
4947
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
 
13342
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
4948
13343
      test_if_skip_sort_order(tab,order,select_limit,0,
4949
13344
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
13345
                              &table->keys_in_use_for_group_by))
4951
13346
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13347
  for (order_st *ord= join->order; ord; ord= ord->next)
4953
13348
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
13349
  if (!(join->sortorder=
 
13350
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13351
    goto err;                           /* purecov: inspected */
4958
13352
 
4959
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13353
  table->sort.io_cache= new IO_CACHE;
 
13354
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
4960
13355
  table->status=0;                              // May be wrong if quick_select
4961
13356
 
4962
13357
  // If table has a range, move it to select
4970
13365
        We can only use 'Only index' if quick key is same as ref_key
4971
13366
        and in index_merge 'Only index' cannot be used
4972
13367
      */
4973
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
13368
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
4974
13369
      {
4975
 
        table->key_read=0;
4976
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13370
        table->key_read=0;
 
13371
        table->file->extra(HA_EXTRA_NO_KEYREAD);
4977
13372
      }
4978
13373
    }
4979
13374
    else
4980
13375
    {
4981
13376
      /*
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.
 
13377
        We have a ref on a const;  Change this to a range that filesort
 
13378
        can use.
 
13379
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13380
        field, quick will contain an empty record set.
4986
13381
      */
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
 
      }
 
13382
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
 
13383
                                                     tab->found_records))))
 
13384
        goto err;
4994
13385
    }
4995
13386
  }
4996
13387
 
4997
 
  if (table->getShare()->getType())
4998
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13388
  /* Fill schema tables with data before filesort if it's necessary */
 
13389
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13390
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13391
    goto err;
4999
13392
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
13393
  if (table->s->tmp_table)
 
13394
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13395
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
13396
                                     select, filesort_limit, 0,
 
13397
                                     &examined_rows);
5004
13398
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
13399
  if (select)
5006
13400
  {
5010
13404
  tab->select_cond=0;
5011
13405
  tab->last_inner= 0;
5012
13406
  tab->first_unmatched= 0;
5013
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13407
  tab->type=JT_ALL;                             // Read with normal read_record
5014
13408
  tab->read_first_record= join_init_read_record;
5015
13409
  tab->join->examined_rows+=examined_rows;
5016
13410
  if (table->key_read)                          // Restore if we used indexes
5017
13411
  {
5018
13412
    table->key_read=0;
5019
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13413
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5020
13414
  }
5021
 
 
5022
13415
  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;
 
13416
err:
 
13417
  return(-1);
 
13418
}
 
13419
 
 
13420
static bool copy_blobs(Field **ptr)
 
13421
{
 
13422
  for (; *ptr ; ptr++)
 
13423
  {
 
13424
    if ((*ptr)->flags & BLOB_FLAG)
 
13425
      if (((Field_blob *) (*ptr))->copy())
 
13426
        return 1;                               // Error
 
13427
  }
 
13428
  return 0;
 
13429
}
 
13430
 
 
13431
static void free_blobs(Field **ptr)
 
13432
{
 
13433
  for (; *ptr ; ptr++)
 
13434
  {
 
13435
    if ((*ptr)->flags & BLOB_FLAG)
 
13436
      ((Field_blob *) (*ptr))->free();
 
13437
  }
 
13438
}
 
13439
 
 
13440
 
 
13441
static int
 
13442
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13443
{
 
13444
  int error;
 
13445
  uint32_t reclength,offset;
 
13446
  uint32_t field_count;
 
13447
  Session *session= join->session;
 
13448
 
 
13449
  entry->reginfo.lock_type=TL_WRITE;
 
13450
 
 
13451
  /* Calculate how many saved fields there is in list */
 
13452
  field_count=0;
 
13453
  List_iterator<Item> it(fields);
 
13454
  Item *item;
 
13455
  while ((item=it++))
 
13456
  {
 
13457
    if (item->get_tmp_table_field() && ! item->const_item())
 
13458
      field_count++;
 
13459
  }
 
13460
 
 
13461
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
 
13462
  {                    // only const items with no OPTION_FOUND_ROWS
 
13463
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13464
    return(0);
 
13465
  }
 
13466
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13467
  offset= (field_count ?
 
13468
           entry->field[entry->s->fields - field_count]->
 
13469
           offset(entry->record[0]) : 0);
 
13470
  reclength= entry->s->reclength-offset;
 
13471
 
 
13472
  free_io_cache(entry);                         // Safety
 
13473
  entry->file->info(HA_STATUS_VARIABLE);
 
13474
  if (entry->s->db_type() == heap_hton ||
 
13475
      (!entry->s->blob_fields &&
 
13476
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13477
        session->variables.sortbuff_size)))
 
13478
    error= remove_dup_with_hash_index(join->session, entry,
 
13479
                                     field_count, first_field,
 
13480
                                     reclength, having);
 
13481
  else
 
13482
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
 
13483
                                  having);
 
13484
 
 
13485
  free_blobs(first_field);
 
13486
  return(error);
 
13487
}
 
13488
 
 
13489
 
 
13490
static int remove_dup_with_compare(Session *session, Table *table, Field **first_field,
 
13491
                                   uint32_t offset, Item *having)
 
13492
{
 
13493
  handler *file=table->file;
5028
13494
  char *org_record,*new_record;
5029
13495
  unsigned char *record;
5030
13496
  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);
 
13497
  uint32_t reclength= table->s->reclength-offset;
 
13498
 
 
13499
  org_record=(char*) (record=table->record[0])+offset;
 
13500
  new_record=(char*) table->record[1]+offset;
 
13501
 
 
13502
  file->ha_rnd_init(1);
 
13503
  error=file->rnd_next(record);
5040
13504
  for (;;)
5041
13505
  {
5042
 
    if (session->getKilled())
 
13506
    if (session->killed)
5043
13507
    {
5044
13508
      session->send_kill_message();
5045
13509
      error=0;
5048
13512
    if (error)
5049
13513
    {
5050
13514
      if (error == HA_ERR_RECORD_DELETED)
5051
 
        continue;
 
13515
        continue;
5052
13516
      if (error == HA_ERR_END_OF_FILE)
5053
 
        break;
 
13517
        break;
5054
13518
      goto err;
5055
13519
    }
5056
13520
    if (having && !having->val_int())
5057
13521
    {
5058
 
      if ((error=cursor->deleteRecord(record)))
5059
 
        goto err;
5060
 
      error=cursor->rnd_next(record);
 
13522
      if ((error=file->ha_delete_row(record)))
 
13523
        goto err;
 
13524
      error=file->rnd_next(record);
5061
13525
      continue;
5062
13526
    }
5063
13527
    if (copy_blobs(first_field))
5068
13532
    }
5069
13533
    memcpy(new_record,org_record,reclength);
5070
13534
 
5071
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13535
    /* Read through rest of file and mark duplicated rows deleted */
5072
13536
    bool found=0;
5073
13537
    for (;;)
5074
13538
    {
5075
 
      if ((error=cursor->rnd_next(record)))
 
13539
      if ((error=file->rnd_next(record)))
5076
13540
      {
5077
 
        if (error == HA_ERR_RECORD_DELETED)
5078
 
          continue;
5079
 
        if (error == HA_ERR_END_OF_FILE)
5080
 
          break;
5081
 
        goto err;
 
13541
        if (error == HA_ERR_RECORD_DELETED)
 
13542
          continue;
 
13543
        if (error == HA_ERR_END_OF_FILE)
 
13544
          break;
 
13545
        goto err;
5082
13546
      }
5083
13547
      if (table->compare_record(first_field) == 0)
5084
13548
      {
5085
 
        if ((error=cursor->deleteRecord(record)))
5086
 
          goto err;
 
13549
        if ((error=file->ha_delete_row(record)))
 
13550
          goto err;
5087
13551
      }
5088
13552
      else if (!found)
5089
13553
      {
5090
 
        found= 1;
5091
 
        cursor->position(record);       // Remember position
 
13554
        found=1;
 
13555
        file->position(record); // Remember position
5092
13556
      }
5093
13557
    }
5094
13558
    if (!found)
5095
 
      break;                                    // End of cursor
5096
 
    /* Move current position to the next row */
5097
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13559
      break;                                    // End of file
 
13560
    /* Restart search on next row */
 
13561
    error=file->restart_rnd_next(record,file->ref);
5098
13562
  }
5099
13563
 
5100
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13564
  file->extra(HA_EXTRA_NO_CACHE);
5101
13565
  return(0);
5102
13566
err:
5103
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13567
  file->extra(HA_EXTRA_NO_CACHE);
5104
13568
  if (error)
5105
 
    table->print_error(error,MYF(0));
 
13569
    file->print_error(error,MYF(0));
5106
13570
  return(1);
5107
13571
}
5108
13572
 
 
13573
 
5109
13574
/**
5110
13575
  Generate a hash index for each row to quickly find duplicate rows.
5111
13576
 
5112
13577
  @note
5113
13578
    Note that this will not work on tables with blobs!
5114
13579
*/
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)
 
13580
 
 
13581
static int remove_dup_with_hash_index(Session *session, Table *table,
 
13582
                                      uint32_t field_count,
 
13583
                                      Field **first_field,
 
13584
                                      uint32_t key_length,
 
13585
                                      Item *having)
5121
13586
{
5122
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13587
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
5123
13588
  int error;
5124
 
  Cursor *cursor= table->cursor;
 
13589
  handler *file= table->file;
5125
13590
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5126
 
  uint32_t *field_length;
 
13591
  uint32_t *field_lengths,*field_length;
5127
13592
  HASH hash;
5128
 
  std::vector<unsigned char> key_buffer;
5129
 
  std::vector<uint32_t> field_lengths;
5130
13593
 
5131
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5132
 
  field_lengths.resize(field_count);
 
13594
  if (!my_multi_malloc(MYF(MY_WME),
 
13595
                       &key_buffer,
 
13596
                       (uint) ((key_length + extra_length) *
 
13597
                               (long) file->stats.records),
 
13598
                       &field_lengths,
 
13599
                       (uint) (field_count*sizeof(*field_lengths)),
 
13600
                       NULL))
 
13601
    return(1);
5133
13602
 
5134
13603
  {
5135
13604
    Field **ptr;
5136
13605
    uint32_t total_length= 0;
5137
 
 
5138
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13606
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5139
13607
    {
5140
13608
      uint32_t length= (*ptr)->sort_length();
5141
13609
      (*field_length++)= length;
5146
13614
    extra_length= ALIGN_SIZE(key_length)-key_length;
5147
13615
  }
5148
13616
 
5149
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13617
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0,
5150
13618
                key_length, (hash_get_key) 0, 0, 0))
5151
13619
  {
 
13620
    free((char*) key_buffer);
5152
13621
    return(1);
5153
13622
  }
5154
13623
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
5158
 
  key_pos= &key_buffer[0];
 
13624
  file->ha_rnd_init(1);
 
13625
  key_pos=key_buffer;
5159
13626
  for (;;)
5160
13627
  {
5161
13628
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
13629
    if (session->killed)
5163
13630
    {
5164
13631
      session->send_kill_message();
5165
13632
      error=0;
5166
13633
      goto err;
5167
13634
    }
5168
 
    if ((error=cursor->rnd_next(record)))
 
13635
    if ((error=file->rnd_next(record)))
5169
13636
    {
5170
13637
      if (error == HA_ERR_RECORD_DELETED)
5171
 
        continue;
 
13638
        continue;
5172
13639
      if (error == HA_ERR_END_OF_FILE)
5173
 
        break;
 
13640
        break;
5174
13641
      goto err;
5175
13642
    }
5176
13643
    if (having && !having->val_int())
5177
13644
    {
5178
 
      if ((error=cursor->deleteRecord(record)))
5179
 
        goto err;
 
13645
      if ((error=file->ha_delete_row(record)))
 
13646
        goto err;
5180
13647
      continue;
5181
13648
    }
5182
13649
 
5183
13650
    /* copy fields to key buffer */
5184
13651
    org_key_pos= key_pos;
5185
 
    field_length= &field_lengths[0];
 
13652
    field_length=field_lengths;
5186
13653
    for (Field **ptr= first_field ; *ptr ; ptr++)
5187
13654
    {
5188
13655
      (*ptr)->sort_string(key_pos,*field_length);
5192
13659
    if (hash_search(&hash, org_key_pos, key_length))
5193
13660
    {
5194
13661
      /* Duplicated found ; Remove the row */
5195
 
      if ((error=cursor->deleteRecord(record)))
5196
 
        goto err;
 
13662
      if ((error=file->ha_delete_row(record)))
 
13663
        goto err;
5197
13664
    }
5198
13665
    else
5199
13666
      (void) my_hash_insert(&hash, org_key_pos);
5200
13667
    key_pos+=extra_length;
5201
13668
  }
 
13669
  free((char*) key_buffer);
5202
13670
  hash_free(&hash);
5203
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5204
 
  (void) cursor->endTableScan();
 
13671
  file->extra(HA_EXTRA_NO_CACHE);
 
13672
  (void) file->ha_rnd_end();
5205
13673
  return(0);
5206
13674
 
5207
13675
err:
 
13676
  free((char*) key_buffer);
5208
13677
  hash_free(&hash);
5209
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5210
 
  (void) cursor->endTableScan();
 
13678
  file->extra(HA_EXTRA_NO_CACHE);
 
13679
  (void) file->ha_rnd_end();
5211
13680
  if (error)
5212
 
    table->print_error(error,MYF(0));
 
13681
    file->print_error(error,MYF(0));
5213
13682
  return(1);
5214
13683
}
5215
13684
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13685
 
 
13686
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13687
                                  SORT_FIELD *sortorder)
5217
13688
{
5218
13689
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
13690
  SORT_FIELD *sort,*pos;
5220
13691
 
5221
13692
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13693
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5223
13694
    count++;
5224
13695
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5226
 
                                       (max(count, *length) + 1));
 
13696
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
13697
                                       (cmax(count, *length) + 1));
5227
13698
  pos= sort= sortorder;
5228
13699
 
5229
13700
  if (!pos)
5249
13720
  return(sort);
5250
13721
}
5251
13722
 
 
13723
 
 
13724
/*****************************************************************************
 
13725
  Fill join cache with packed records
 
13726
  Records are stored in tab->cache.buffer and last record in
 
13727
  last record is stored with pointers to blobs to support very big
 
13728
  records
 
13729
******************************************************************************/
 
13730
 
 
13731
static int
 
13732
join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count)
 
13733
{
 
13734
  register unsigned int i;
 
13735
  unsigned int length, blobs;
 
13736
  size_t size;
 
13737
  CACHE_FIELD *copy,**blob_ptr;
 
13738
  JOIN_CACHE  *cache;
 
13739
  JOIN_TAB *join_tab;
 
13740
 
 
13741
  cache= &tables[table_count].cache;
 
13742
  cache->fields=blobs=0;
 
13743
 
 
13744
  join_tab=tables;
 
13745
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13746
  {
 
13747
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13748
      calc_used_field_length(session, join_tab);
 
13749
    cache->fields+=join_tab->used_fields;
 
13750
    blobs+=join_tab->used_blobs;
 
13751
 
 
13752
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13753
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13754
    {
 
13755
      cache->fields++;
 
13756
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13757
    }
 
13758
  }
 
13759
  if (!(cache->field=(CACHE_FIELD*)
 
13760
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13761
 
 
13762
                  sizeof(CACHE_FIELD*))))
 
13763
  {
 
13764
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13765
    cache->buff=0;                              /* purecov: inspected */
 
13766
    return(1);                          /* purecov: inspected */
 
13767
  }
 
13768
  copy=cache->field;
 
13769
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13770
    (cache->field+cache->fields+table_count*2);
 
13771
 
 
13772
  length=0;
 
13773
  for (i=0 ; i < table_count ; i++)
 
13774
  {
 
13775
    uint32_t null_fields=0, used_fields;
 
13776
    Field **f_ptr,*field;
 
13777
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13778
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13779
         used_fields ;
 
13780
         f_ptr++)
 
13781
    {
 
13782
      field= *f_ptr;
 
13783
      if (bitmap_is_set(read_set, field->field_index))
 
13784
      {
 
13785
        used_fields--;
 
13786
        length+=field->fill_cache_field(copy);
 
13787
        if (copy->blob_field)
 
13788
          (*blob_ptr++)=copy;
 
13789
        if (field->maybe_null())
 
13790
          null_fields++;
 
13791
        copy->get_rowid= NULL;
 
13792
        copy++;
 
13793
      }
 
13794
    }
 
13795
    /* Copy null bits from table */
 
13796
    if (null_fields && tables[i].table->getNullFields())
 
13797
    {                                           /* must copy null bits */
 
13798
      copy->str= tables[i].table->null_flags;
 
13799
      copy->length= tables[i].table->s->null_bytes;
 
13800
      copy->strip=0;
 
13801
      copy->blob_field=0;
 
13802
      copy->get_rowid= NULL;
 
13803
      length+=copy->length;
 
13804
      copy++;
 
13805
      cache->fields++;
 
13806
    }
 
13807
    /* If outer join table, copy null_row flag */
 
13808
    if (tables[i].table->maybe_null)
 
13809
    {
 
13810
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13811
      copy->length=sizeof(tables[i].table->null_row);
 
13812
      copy->strip=0;
 
13813
      copy->blob_field=0;
 
13814
      copy->get_rowid= NULL;
 
13815
      length+=copy->length;
 
13816
      copy++;
 
13817
      cache->fields++;
 
13818
    }
 
13819
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13820
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13821
    {
 
13822
      copy->str= tables[i].table->file->ref;
 
13823
      copy->length= tables[i].table->file->ref_length;
 
13824
      copy->strip=0;
 
13825
      copy->blob_field=0;
 
13826
      copy->get_rowid= NULL;
 
13827
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13828
      {
 
13829
        /* We will need to call h->position(): */
 
13830
        copy->get_rowid= tables[i].table;
 
13831
        /* And those after us won't have to: */
 
13832
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13833
      }
 
13834
      copy++;
 
13835
    }
 
13836
  }
 
13837
 
 
13838
  cache->length=length+blobs*sizeof(char*);
 
13839
  cache->blobs=blobs;
 
13840
  *blob_ptr= NULL;                                      /* End sequentel */
 
13841
  size= cmax(session->variables.join_buff_size, (uint32_t)cache->length);
 
13842
  if (!(cache->buff=(unsigned char*) malloc(size)))
 
13843
    return 1;                           /* Don't use cache */ /* purecov: inspected */
 
13844
  cache->end=cache->buff+size;
 
13845
  reset_cache_write(cache);
 
13846
  return 0;
 
13847
}
 
13848
 
 
13849
 
 
13850
static uint32_t used_blob_length(CACHE_FIELD **ptr)
 
13851
{
 
13852
  uint32_t length,blob_length;
 
13853
  for (length=0 ; *ptr ; ptr++)
 
13854
  {
 
13855
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13856
    length+=blob_length;
 
13857
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13858
  }
 
13859
  return length;
 
13860
}
 
13861
 
 
13862
 
 
13863
static bool
 
13864
store_record_in_cache(JOIN_CACHE *cache)
 
13865
{
 
13866
  uint32_t length;
 
13867
  unsigned char *pos;
 
13868
  CACHE_FIELD *copy,*end_field;
 
13869
  bool last_record;
 
13870
 
 
13871
  pos=cache->pos;
 
13872
  end_field=cache->field+cache->fields;
 
13873
 
 
13874
  length=cache->length;
 
13875
  if (cache->blobs)
 
13876
    length+= used_blob_length(cache->blob_ptr);
 
13877
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13878
    cache->ptr_record=cache->records;
 
13879
  /*
 
13880
    There is room in cache. Put record there
 
13881
  */
 
13882
  cache->records++;
 
13883
  for (copy=cache->field ; copy < end_field; copy++)
 
13884
  {
 
13885
    if (copy->blob_field)
 
13886
    {
 
13887
      if (last_record)
 
13888
      {
 
13889
        copy->blob_field->get_image(pos, copy->length+sizeof(char*),
 
13890
                                    copy->blob_field->charset());
 
13891
        pos+=copy->length+sizeof(char*);
 
13892
      }
 
13893
      else
 
13894
      {
 
13895
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13896
                                    copy->blob_field->charset());
 
13897
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13898
        pos+=copy->length+copy->blob_length;
 
13899
      }
 
13900
    }
 
13901
    else
 
13902
    {
 
13903
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13904
      if (copy->get_rowid)
 
13905
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13906
 
 
13907
      if (copy->strip)
 
13908
      {
 
13909
        unsigned char *str,*end;
 
13910
        for (str=copy->str,end= str+copy->length;
 
13911
             end > str && end[-1] == ' ' ;
 
13912
             end--) ;
 
13913
        length=(uint) (end-str);
 
13914
        memcpy(pos+2, str, length);
 
13915
        int2store(pos, length);
 
13916
        pos+= length+2;
 
13917
      }
 
13918
      else
 
13919
      {
 
13920
        memcpy(pos,copy->str,copy->length);
 
13921
        pos+=copy->length;
 
13922
      }
 
13923
    }
 
13924
  }
 
13925
  cache->pos=pos;
 
13926
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13927
}
 
13928
 
 
13929
 
 
13930
static void
 
13931
reset_cache_read(JOIN_CACHE *cache)
 
13932
{
 
13933
  cache->record_nr=0;
 
13934
  cache->pos=cache->buff;
 
13935
}
 
13936
 
 
13937
 
 
13938
static void reset_cache_write(JOIN_CACHE *cache)
 
13939
{
 
13940
  reset_cache_read(cache);
 
13941
  cache->records= 0;
 
13942
  cache->ptr_record= UINT32_MAX;
 
13943
}
 
13944
 
 
13945
 
 
13946
static void
 
13947
read_cached_record(JOIN_TAB *tab)
 
13948
{
 
13949
  unsigned char *pos;
 
13950
  uint32_t length;
 
13951
  bool last_record;
 
13952
  CACHE_FIELD *copy,*end_field;
 
13953
 
 
13954
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13955
  pos=tab->cache.pos;
 
13956
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13957
       copy < end_field;
 
13958
       copy++)
 
13959
  {
 
13960
    if (copy->blob_field)
 
13961
    {
 
13962
      if (last_record)
 
13963
      {
 
13964
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13965
                                    copy->blob_field->charset());
 
13966
        pos+=copy->length+sizeof(char*);
 
13967
      }
 
13968
      else
 
13969
      {
 
13970
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
13971
        pos+=copy->length+copy->blob_field->get_length();
 
13972
      }
 
13973
    }
 
13974
    else
 
13975
    {
 
13976
      if (copy->strip)
 
13977
      {
 
13978
        length= uint2korr(pos);
 
13979
        memcpy(copy->str, pos+2, length);
 
13980
        memset(copy->str+length, ' ', copy->length-length);
 
13981
        pos+= 2 + length;
 
13982
      }
 
13983
      else
 
13984
      {
 
13985
        memcpy(copy->str,pos,copy->length);
 
13986
        pos+=copy->length;
 
13987
      }
 
13988
    }
 
13989
  }
 
13990
  tab->cache.pos=pos;
 
13991
  return;
 
13992
}
 
13993
 
 
13994
 
5252
13995
/*
5253
13996
  eq_ref: Create the lookup key and check if it is the same as saved key
5254
13997
 
5267
14010
    false  The created key is the same as the previous one (and the record
5268
14011
           is already in table->record)
5269
14012
*/
5270
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14013
 
 
14014
static bool
 
14015
cmp_buffer_with_ref(JOIN_TAB *tab)
5271
14016
{
5272
14017
  bool no_prev_key;
5273
14018
  if (!tab->ref.disable_cache)
5287
14032
    != 0;
5288
14033
}
5289
14034
 
5290
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14035
 
 
14036
bool
 
14037
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
5291
14038
{
5292
14039
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5293
14040
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
5294
14041
  bool result= 0;
5295
14042
 
5296
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14043
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5297
14044
  {
5298
14045
    if ((*copy)->copy() & 1)
5299
14046
    {
5305
14052
  return result;
5306
14053
}
5307
14054
 
 
14055
 
5308
14056
/*****************************************************************************
5309
14057
  Group and order functions
5310
14058
*****************************************************************************/
5311
14059
 
5312
14060
/**
5313
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14061
  Resolve an order_st BY or GROUP BY column reference.
5314
14062
 
5315
14063
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5316
14064
  BY clause, find the actual column it represents. If the column being
5317
14065
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5318
14066
  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.
 
14067
  the order_st BY clause, only the SELECT list is being searched.
5320
14068
 
5321
14069
  If 'order' is resolved to an Item, then order->item is set to the found
5322
14070
  Item. If there is no item for the found column (that is, it was resolved
5341
14089
  @retval
5342
14090
    true  if error occurred
5343
14091
*/
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)
 
14092
 
 
14093
static bool
 
14094
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
 
14095
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14096
                   bool is_group_field)
5351
14097
{
5352
14098
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5353
14099
  Item::Type order_item_type;
5362
14108
  */
5363
14109
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5364
14110
  {                                             /* Order by position */
5365
 
    uint32_t count= (uint32_t) order_item->val_int();
 
14111
    uint32_t count= (uint) order_item->val_int();
5366
14112
    if (!count || count > fields.elements)
5367
14113
    {
5368
14114
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
14115
               order_item->full_name(), session->where);
5370
14116
      return true;
5371
14117
    }
5372
14118
    order->item= ref_pointer_array + count - 1;
5376
14122
    return false;
5377
14123
  }
5378
14124
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5379
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14125
  select_item= find_item_in_list(order_item, fields, &counter,
5380
14126
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5381
14127
  if (!select_item)
5382
14128
    return true; /* The item is not unique, or some other error occured. */
5402
14148
        order_item_type == Item::REF_ITEM)
5403
14149
    {
5404
14150
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5405
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14151
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14152
                                       false);
5406
14153
      if (!from_field)
5407
14154
        from_field= (Field*) not_found_field;
5408
14155
    }
5443
14190
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
14191
                          ER(ER_NON_UNIQ_ERROR),
5445
14192
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
14193
                          current_session->where);
5447
14194
    }
5448
14195
  }
5449
14196
 
5472
14219
  return false;
5473
14220
}
5474
14221
 
 
14222
 
5475
14223
/**
5476
14224
  Change order to point at item in select list.
5477
14225
 
5478
14226
  If item isn't a number and doesn't exits in the select list, add it the
5479
14227
  the field list.
5480
14228
*/
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)
 
14229
 
 
14230
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
 
14231
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5487
14232
{
5488
 
  session->setWhere("order clause");
 
14233
  session->where="order clause";
5489
14234
  for (; order; order=order->next)
5490
14235
  {
5491
14236
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5495
14240
  return 0;
5496
14241
}
5497
14242
 
 
14243
 
5498
14244
/**
5499
14245
  Intitialize the GROUP BY list.
5500
14246
 
5501
 
  @param session                        Thread Cursor
 
14247
  @param session                        Thread handler
5502
14248
  @param ref_pointer_array      We store references to all fields that was
5503
14249
                               not in 'fields' here.
5504
14250
  @param fields         All fields in the select part. Any item in
5520
14266
  @retval
5521
14267
    1  error (probably out of memory)
5522
14268
*/
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)
 
14269
 
 
14270
int
 
14271
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
 
14272
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14273
            bool *hidden_group_fields)
5530
14274
{
5531
14275
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
14276
  order_st *ord;
5533
14277
 
5534
14278
  if (!order)
5535
14279
    return 0;                           /* Everything is ok */
5536
14280
 
5537
14281
  uint32_t org_fields=all_fields.elements;
5538
14282
 
5539
 
  session->setWhere("group statement");
 
14283
  session->where="group statement";
5540
14284
  for (ord= order; ord; ord= ord->next)
5541
14285
  {
5542
14286
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5569
14313
    Item *item;
5570
14314
    Item_field *field;
5571
14315
    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());
 
14316
    List_iterator<Item> li(fields);
 
14317
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
14318
 
5575
14319
    field= naf_it++;
5576
14320
    while (field && (item=li++))
5596
14340
            if ((*ord->item)->eq((Item*)field, 0))
5597
14341
              goto next_field;
5598
14342
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
14343
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
14344
            ER_NON_GROUPING_FIELD_USED
5600
14345
          */
5601
14346
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
14347
          return 1;
5618
14363
  Try to use the fields in the order given by 'order' to allow one to
5619
14364
  optimize away 'order by'.
5620
14365
*/
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)
 
14366
 
 
14367
static order_st *
 
14368
create_distinct_group(Session *session, Item **ref_pointer_array,
 
14369
                      order_st *order_list, List<Item> &fields,
 
14370
                      List<Item> &, bool *all_order_by_fields_used)
5627
14371
{
5628
 
  List<Item>::iterator li(fields.begin());
 
14372
  List_iterator<Item> li(fields);
5629
14373
  Item *item;
5630
 
  Order *order,*group,**prev;
 
14374
  order_st *order,*group,**prev;
5631
14375
 
5632
14376
  *all_order_by_fields_used= 1;
5633
14377
  while ((item=li++))
5638
14382
  {
5639
14383
    if (order->in_field_list)
5640
14384
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
14385
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5642
14386
      if (!ord)
5643
 
        return 0;
 
14387
        return 0;
5644
14388
      *prev=ord;
5645
14389
      prev= &ord->next;
5646
14390
      (*ord->item)->marker=1;
5649
14393
      *all_order_by_fields_used= 0;
5650
14394
  }
5651
14395
 
5652
 
  li= fields.begin();
 
14396
  li.rewind();
5653
14397
  while ((item=li++))
5654
14398
  {
5655
14399
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5658
14402
        Don't put duplicate columns from the SELECT list into the
5659
14403
        GROUP BY list.
5660
14404
      */
5661
 
      Order *ord_iter;
 
14405
      order_st *ord_iter;
5662
14406
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
14407
        if ((*ord_iter->item)->eq(item, 1))
5664
14408
          goto next_item;
5665
14409
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14410
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5667
14411
      if (!ord)
5668
 
        return 0;
 
14412
        return 0;
5669
14413
 
5670
14414
      /*
5671
14415
        We have here only field_list (not all_field_list), so we can use
5684
14428
  return group;
5685
14429
}
5686
14430
 
 
14431
 
5687
14432
/**
5688
14433
  Update join with count of the different type of fields.
5689
14434
*/
5690
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14435
 
 
14436
void
 
14437
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
 
14438
                  List<Item> &fields, bool reset_with_sum_func)
5691
14439
{
5692
 
  List<Item>::iterator li(fields.begin());
 
14440
  List_iterator<Item> li(fields);
5693
14441
  Item *field;
5694
14442
 
5695
14443
  param->field_count=param->sum_func_count=param->func_count=
5704
14452
    {
5705
14453
      if (! field->const_item())
5706
14454
      {
5707
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14455
        Item_sum *sum_item=(Item_sum*) field->real_item();
5708
14456
        if (!sum_item->depended_from() ||
5709
14457
            sum_item->depended_from() == select_lex)
5710
14458
        {
5727
14475
    {
5728
14476
      param->func_count++;
5729
14477
      if (reset_with_sum_func)
5730
 
        field->with_sum_func=0;
5731
 
    }
5732
 
  }
5733
 
}
 
14478
        field->with_sum_func=0;
 
14479
    }
 
14480
  }
 
14481
}
 
14482
 
 
14483
 
 
14484
/**
 
14485
  Return 1 if second is a subpart of first argument.
 
14486
 
 
14487
  If first parts has different direction, change it to second part
 
14488
  (group is sorted like order)
 
14489
*/
 
14490
 
 
14491
static bool
 
14492
test_if_subpart(order_st *a,order_st *b)
 
14493
{
 
14494
  for (; a && b; a=a->next,b=b->next)
 
14495
  {
 
14496
    if ((*a->item)->eq(*b->item,1))
 
14497
      a->asc=b->asc;
 
14498
    else
 
14499
      return 0;
 
14500
  }
 
14501
  return test(!b);
 
14502
}
 
14503
 
 
14504
/**
 
14505
  Return table number if there is only one table in sort order
 
14506
  and group and order is compatible, else return 0.
 
14507
*/
 
14508
 
 
14509
static Table *
 
14510
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14511
{
 
14512
  table_map map= (table_map) 0;
 
14513
 
 
14514
  if (!a)
 
14515
    a=b;                                        // Only one need to be given
 
14516
  else if (!b)
 
14517
    b=a;
 
14518
 
 
14519
  for (; a && b; a=a->next,b=b->next)
 
14520
  {
 
14521
    if (!(*a->item)->eq(*b->item,1))
 
14522
      return(0);
 
14523
    map|=a->item[0]->used_tables();
 
14524
  }
 
14525
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14526
    return(0);
 
14527
 
 
14528
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14529
  if (map != tables->table->map)
 
14530
    return(0);                          // More than one table
 
14531
  return(tables->table);
 
14532
}
 
14533
 
 
14534
 
 
14535
/**
 
14536
  calc how big buffer we need for comparing group entries.
 
14537
*/
 
14538
 
 
14539
static void
 
14540
calc_group_buffer(JOIN *join,order_st *group)
 
14541
{
 
14542
  uint32_t key_length=0, parts=0, null_parts=0;
 
14543
 
 
14544
  if (group)
 
14545
    join->group= 1;
 
14546
  for (; group ; group=group->next)
 
14547
  {
 
14548
    Item *group_item= *group->item;
 
14549
    Field *field= group_item->get_tmp_table_field();
 
14550
    if (field)
 
14551
    {
 
14552
      enum_field_types type;
 
14553
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14554
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14555
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14556
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14557
      else
 
14558
        key_length+= field->pack_length();
 
14559
    }
 
14560
    else
 
14561
    {
 
14562
      switch (group_item->result_type()) {
 
14563
      case REAL_RESULT:
 
14564
        key_length+= sizeof(double);
 
14565
        break;
 
14566
      case INT_RESULT:
 
14567
        key_length+= sizeof(int64_t);
 
14568
        break;
 
14569
      case DECIMAL_RESULT:
 
14570
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
14571
                                                (group_item->decimals ? 1 : 0),
 
14572
                                                group_item->decimals);
 
14573
        break;
 
14574
      case STRING_RESULT:
 
14575
      {
 
14576
        enum enum_field_types type= group_item->field_type();
 
14577
        /*
 
14578
          As items represented as DATE/TIME fields in the group buffer
 
14579
          have STRING_RESULT result type, we increase the length
 
14580
          by 8 as maximum pack length of such fields.
 
14581
        */
 
14582
        if (type == DRIZZLE_TYPE_TIME ||
 
14583
            type == DRIZZLE_TYPE_DATE ||
 
14584
            type == DRIZZLE_TYPE_DATETIME ||
 
14585
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14586
        {
 
14587
          key_length+= 8;
 
14588
        }
 
14589
        else
 
14590
        {
 
14591
          /*
 
14592
            Group strings are taken as varstrings and require an length field.
 
14593
            A field is not yet created by create_tmp_field()
 
14594
            and the sizes should match up.
 
14595
          */
 
14596
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14597
        }
 
14598
        break;
 
14599
      }
 
14600
      default:
 
14601
        /* This case should never be choosen */
 
14602
        assert(0);
 
14603
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14604
      }
 
14605
    }
 
14606
    parts++;
 
14607
    if (group_item->maybe_null)
 
14608
      null_parts++;
 
14609
  }
 
14610
  join->tmp_table_param.group_length=key_length+null_parts;
 
14611
  join->tmp_table_param.group_parts=parts;
 
14612
  join->tmp_table_param.group_null_parts=null_parts;
 
14613
}
 
14614
 
 
14615
 
 
14616
/**
 
14617
  allocate group fields or take prepared (cached).
 
14618
 
 
14619
  @param main_join   join of current select
 
14620
  @param curr_join   current join (join of current select or temporary copy
 
14621
                     of it)
 
14622
 
 
14623
  @retval
 
14624
    0   ok
 
14625
  @retval
 
14626
    1   failed
 
14627
*/
 
14628
 
 
14629
static bool
 
14630
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14631
{
 
14632
  if (main_join->group_fields_cache.elements)
 
14633
  {
 
14634
    curr_join->group_fields= main_join->group_fields_cache;
 
14635
    curr_join->sort_and_group= 1;
 
14636
  }
 
14637
  else
 
14638
  {
 
14639
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14640
      return (1);
 
14641
    main_join->group_fields_cache= curr_join->group_fields;
 
14642
  }
 
14643
  return (0);
 
14644
}
 
14645
 
 
14646
 
 
14647
/**
 
14648
  Get a list of buffers for saveing last group.
 
14649
 
 
14650
  Groups are saved in reverse order for easyer check loop.
 
14651
*/
 
14652
 
 
14653
static bool
 
14654
alloc_group_fields(JOIN *join,order_st *group)
 
14655
{
 
14656
  if (group)
 
14657
  {
 
14658
    for (; group ; group=group->next)
 
14659
    {
 
14660
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
 
14661
      if (!tmp || join->group_fields.push_front(tmp))
 
14662
        return true;
 
14663
    }
 
14664
  }
 
14665
  join->sort_and_group=1;                       /* Mark for do_select */
 
14666
  return false;
 
14667
}
 
14668
 
5734
14669
 
5735
14670
/*
5736
14671
  Test if a single-row cache of items changed, and update the cache.
5744
14679
  @return -1 if no item changed
5745
14680
  @return index of the first item that changed
5746
14681
*/
 
14682
 
5747
14683
int test_if_item_cache_changed(List<Cached_item> &list)
5748
14684
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
14685
  List_iterator<Cached_item> li(list);
5750
14686
  int idx= -1,i;
5751
14687
  Cached_item *buff;
5752
14688
 
5758
14694
  return(idx);
5759
14695
}
5760
14696
 
 
14697
 
5761
14698
/**
5762
14699
  Setup copy_fields to save fields at start of new group.
5763
14700
 
5786
14723
  @retval
5787
14724
    !=0   error
5788
14725
*/
5789
 
bool setup_copy_fields(Session *session,
5790
 
                       Tmp_Table_Param *param,
5791
 
                       Item **ref_pointer_array,
5792
 
                       List<Item> &res_selected_fields,
5793
 
                       List<Item> &res_all_fields,
5794
 
                       uint32_t elements,
5795
 
                       List<Item> &all_fields)
 
14726
 
 
14727
bool
 
14728
setup_copy_fields(Session *session, TMP_TABLE_PARAM *param,
 
14729
                  Item **ref_pointer_array,
 
14730
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14731
                  uint32_t elements, List<Item> &all_fields)
5796
14732
{
5797
14733
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
5799
 
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
14734
  List_iterator_fast<Item> li(all_fields);
 
14735
  Copy_field *copy= NULL;
 
14736
  res_selected_fields.empty();
 
14737
  res_all_fields.empty();
 
14738
  List_iterator_fast<Item> itr(res_all_fields);
5803
14739
  List<Item> extra_funcs;
5804
14740
  uint32_t i, border= all_fields.elements - elements;
5805
14741
 
5806
14742
  if (param->field_count &&
5807
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14743
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5808
14744
    goto err2;
5809
14745
 
5810
 
  param->copy_funcs.clear();
 
14746
  param->copy_funcs.empty();
5811
14747
  for (i= 0; (pos= li++); i++)
5812
14748
  {
5813
14749
    Field *field;
5817
14753
    {
5818
14754
      Item_field *item;
5819
14755
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5820
 
        goto err;
 
14756
        goto err;
5821
14757
      if (pos->type() == Item::REF_ITEM)
5822
14758
      {
5823
14759
        /* preserve the names of the ref when dereferncing */
5829
14765
      pos= item;
5830
14766
      if (item->field->flags & BLOB_FLAG)
5831
14767
      {
5832
 
        if (!(pos= new Item_copy_string(pos)))
5833
 
          goto err;
5834
 
            /*
5835
 
              Item_copy_string::copy for function can call
5836
 
              Item_copy_string::val_int for blob via Item_ref.
5837
 
              But if Item_copy_string::copy for blob isn't called before,
5838
 
              it's value will be wrong
5839
 
              so let's insert Item_copy_string for blobs in the beginning of
5840
 
              copy_funcs
5841
 
              (to see full test case look at having.test, BUG #4358)
5842
 
            */
5843
 
        if (param->copy_funcs.push_front(pos))
5844
 
          goto err;
 
14768
        if (!(pos= new Item_copy_string(pos)))
 
14769
          goto err;
 
14770
       /*
 
14771
         Item_copy_string::copy for function can call
 
14772
         Item_copy_string::val_int for blob via Item_ref.
 
14773
         But if Item_copy_string::copy for blob isn't called before,
 
14774
         it's value will be wrong
 
14775
         so let's insert Item_copy_string for blobs in the beginning of
 
14776
         copy_funcs
 
14777
         (to see full test case look at having.test, BUG #4358)
 
14778
       */
 
14779
        if (param->copy_funcs.push_front(pos))
 
14780
          goto err;
5845
14781
      }
5846
14782
      else
5847
14783
      {
 
14784
        /*
 
14785
           set up save buffer and change result_field to point at
 
14786
           saved value
 
14787
        */
 
14788
        field= item->field;
 
14789
        item->result_field=field->new_field(session->mem_root,field->table, 1);
5848
14790
        /*
5849
 
          set up save buffer and change result_field to point at
5850
 
          saved value
 
14791
          We need to allocate one extra byte for null handling and
 
14792
          another extra byte to not get warnings from purify in
 
14793
          Field_varstring::val_int
5851
14794
        */
5852
 
        field= item->field;
5853
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
5854
 
              /*
5855
 
                We need to allocate one extra byte for null handling and
5856
 
                another extra byte to not get warnings from purify in
5857
 
                Field_varstring::val_int
5858
 
              */
5859
 
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
5860
 
          goto err;
 
14795
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14796
          goto err;
5861
14797
        if (copy)
5862
14798
        {
5863
14799
          copy->set(tmp, item->result_field);
5864
14800
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
14801
#ifdef HAVE_purify
5866
14802
          copy->to_ptr[copy->from_length]= 0;
5867
14803
#endif
5868
14804
          copy++;
5876
14812
             !real_pos->with_sum_func)
5877
14813
    {                                           // Save for send fields
5878
14814
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
5881
 
        This should be changed to use copy_int or copy_real depending
5882
 
        on how the value is to be used: In some cases this may be an
5883
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
14815
      /* TODO:
 
14816
         In most cases this result will be sent to the user.
 
14817
         This should be changed to use copy_int or copy_real depending
 
14818
         on how the value is to be used: In some cases this may be an
 
14819
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5884
14820
      */
5885
14821
      if (!(pos=new Item_copy_string(pos)))
5886
 
        goto err;
 
14822
        goto err;
5887
14823
      if (i < border)                           // HAVING, order_st and GROUP BY
5888
14824
      {
5889
14825
        if (extra_funcs.push_back(pos))
5890
14826
          goto err;
5891
14827
      }
5892
14828
      else if (param->copy_funcs.push_back(pos))
5893
 
        goto err;
 
14829
        goto err;
5894
14830
    }
5895
14831
    res_all_fields.push_back(pos);
5896
14832
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5902
14838
    itr++;
5903
14839
  itr.sublist(res_selected_fields, elements);
5904
14840
  /*
5905
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14841
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5906
14842
    reference used in these will resolve to a item that is already calculated
5907
14843
  */
5908
14844
  param->copy_funcs.concat(&extra_funcs);
5909
14845
 
5910
14846
  return(0);
5911
14847
 
5912
 
err:
 
14848
 err:
5913
14849
  if (copy)
5914
14850
    delete [] param->copy_field;                        // This is never 0
5915
14851
  param->copy_field=0;
5917
14853
  return(true);
5918
14854
}
5919
14855
 
 
14856
 
5920
14857
/**
5921
14858
  Make a copy of all simple SELECT'ed items.
5922
14859
 
5923
14860
  This is done at the start of a new group so that we can retrieve
5924
14861
  these later when the group changes.
5925
14862
*/
5926
 
void copy_fields(Tmp_Table_Param *param)
 
14863
 
 
14864
void
 
14865
copy_fields(TMP_TABLE_PARAM *param)
5927
14866
{
5928
 
  CopyField *ptr= param->copy_field;
5929
 
  CopyField *end= param->copy_field_end;
 
14867
  Copy_field *ptr=param->copy_field;
 
14868
  Copy_field *end=param->copy_field_end;
5930
14869
 
5931
14870
  for (; ptr != end; ptr++)
5932
14871
    (*ptr->do_copy)(ptr);
5933
14872
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
14873
  List_iterator_fast<Item> it(param->copy_funcs);
5935
14874
  Item_copy_string *item;
5936
14875
  while ((item = (Item_copy_string*) it++))
5937
14876
    item->copy();
5938
14877
}
5939
14878
 
 
14879
 
 
14880
/**
 
14881
  Make an array of pointers to sum_functions to speed up
 
14882
  sum_func calculation.
 
14883
 
 
14884
  @retval
 
14885
    0   ok
 
14886
  @retval
 
14887
    1   Error
 
14888
*/
 
14889
 
 
14890
bool JOIN::alloc_func_list()
 
14891
{
 
14892
  uint32_t func_count, group_parts;
 
14893
 
 
14894
  func_count= tmp_table_param.sum_func_count;
 
14895
  /*
 
14896
    If we are using rollup, we need a copy of the summary functions for
 
14897
    each level
 
14898
  */
 
14899
  if (rollup.state != ROLLUP::STATE_NONE)
 
14900
    func_count*= (send_group_parts+1);
 
14901
 
 
14902
  group_parts= send_group_parts;
 
14903
  /*
 
14904
    If distinct, reserve memory for possible
 
14905
    disctinct->group_by optimization
 
14906
  */
 
14907
  if (select_distinct)
 
14908
  {
 
14909
    group_parts+= fields_list.elements;
 
14910
    /*
 
14911
      If the order_st clause is specified then it's possible that
 
14912
      it also will be optimized, so reserve space for it too
 
14913
    */
 
14914
    if (order)
 
14915
    {
 
14916
      order_st *ord;
 
14917
      for (ord= order; ord; ord= ord->next)
 
14918
        group_parts++;
 
14919
    }
 
14920
  }
 
14921
 
 
14922
  /* This must use calloc() as rollup_make_fields depends on this */
 
14923
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14924
                                      sizeof(Item_sum***) * (group_parts+1));
 
14925
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14926
  return(sum_funcs == 0);
 
14927
}
 
14928
 
 
14929
 
 
14930
/**
 
14931
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14932
 
 
14933
  @param field_list        All items
 
14934
  @param send_fields       Items in select list
 
14935
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14936
  @param recompute         Set to true if sum_funcs must be recomputed
 
14937
 
 
14938
  @retval
 
14939
    0  ok
 
14940
  @retval
 
14941
    1  error
 
14942
*/
 
14943
 
 
14944
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14945
                              bool before_group_by, bool recompute)
 
14946
{
 
14947
  List_iterator_fast<Item> it(field_list);
 
14948
  Item_sum **func;
 
14949
  Item *item;
 
14950
 
 
14951
  if (*sum_funcs && !recompute)
 
14952
    return(false); /* We have already initialized sum_funcs. */
 
14953
 
 
14954
  func= sum_funcs;
 
14955
  while ((item=it++))
 
14956
  {
 
14957
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14958
        (!((Item_sum*) item)->depended_from() ||
 
14959
         ((Item_sum *)item)->depended_from() == select_lex))
 
14960
      *func++= (Item_sum*) item;
 
14961
  }
 
14962
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14963
  {
 
14964
    rollup.state= ROLLUP::STATE_READY;
 
14965
    if (rollup_make_fields(field_list, send_fields, &func))
 
14966
      return(true);                     // Should never happen
 
14967
  }
 
14968
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14969
  {
 
14970
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
14971
      sum_funcs_end[i]= func;
 
14972
  }
 
14973
  else if (rollup.state == ROLLUP::STATE_READY)
 
14974
    return(false);                         // Don't put end marker
 
14975
  *func=0;                                      // End marker
 
14976
  return(false);
 
14977
}
 
14978
 
 
14979
 
5940
14980
/**
5941
14981
  Change all funcs and sum_funcs to fields in tmp table, and create
5942
14982
  new list of all items.
5953
14993
  @retval
5954
14994
    !=0   error
5955
14995
*/
5956
 
bool change_to_use_tmp_fields(Session *session,
5957
 
                              Item **ref_pointer_array,
5958
 
                                                List<Item> &res_selected_fields,
5959
 
                                                List<Item> &res_all_fields,
5960
 
                                                uint32_t elements,
5961
 
                              List<Item> &all_fields)
 
14996
 
 
14997
static bool
 
14998
change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
 
14999
                         List<Item> &res_selected_fields,
 
15000
                         List<Item> &res_all_fields,
 
15001
                         uint32_t elements, List<Item> &all_fields)
5962
15002
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
15003
  List_iterator_fast<Item> it(all_fields);
5964
15004
  Item *item_field,*item;
5965
15005
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
15006
  res_selected_fields.empty();
 
15007
  res_all_fields.empty();
5968
15008
 
5969
15009
  uint32_t i, border= all_fields.elements - elements;
5970
15010
  for (i= 0; (item= it++); i++)
5979
15019
    {
5980
15020
      if (item->type() == Item::FIELD_ITEM)
5981
15021
      {
5982
 
        item_field= item->get_tmp_table_item(session);
 
15022
        item_field= item->get_tmp_table_item(session);
5983
15023
      }
5984
15024
      else if ((field= item->get_tmp_table_field()))
5985
15025
      {
5986
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
5987
 
          item_field= ((Item_sum*) item)->result_item(field);
5988
 
        else
5989
 
          item_field= (Item*) new Item_field(field);
5990
 
        if (!item_field)
5991
 
          return(true);                    // Fatal error
 
15026
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15027
          item_field= ((Item_sum*) item)->result_item(field);
 
15028
        else
 
15029
          item_field= (Item*) new Item_field(field);
 
15030
        if (!item_field)
 
15031
          return(true);                    // Fatal error
5992
15032
 
5993
15033
        if (item->real_item()->type() != Item::FIELD_ITEM)
5994
15034
          field->orig_table= 0;
5995
 
        item_field->name= item->name;
 
15035
        item_field->name= item->name;
5996
15036
        if (item->type() == Item::REF_ITEM)
5997
15037
        {
5998
15038
          Item_field *ifield= (Item_field *) item_field;
6002
15042
        }
6003
15043
      }
6004
15044
      else
6005
 
        item_field= item;
 
15045
        item_field= item;
6006
15046
    }
6007
15047
    res_all_fields.push_back(item_field);
6008
15048
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6009
15049
      item_field;
6010
15050
  }
6011
15051
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15052
  List_iterator_fast<Item> itr(res_all_fields);
6013
15053
  for (i= 0; i < border; i++)
6014
15054
    itr++;
6015
15055
  itr.sublist(res_selected_fields, elements);
6016
15056
  return(false);
6017
15057
}
6018
15058
 
 
15059
 
6019
15060
/**
6020
15061
  Change all sum_func refs to fields to point at fields in tmp table.
6021
15062
  Change all funcs to be fields in tmp table.
6032
15073
  @retval
6033
15074
    1   error
6034
15075
*/
6035
 
bool change_refs_to_tmp_fields(Session *session,
6036
 
                               Item **ref_pointer_array,
6037
 
                               List<Item> &res_selected_fields,
6038
 
                               List<Item> &res_all_fields,
6039
 
                               uint32_t elements,
6040
 
                                                 List<Item> &all_fields)
 
15076
 
 
15077
static bool
 
15078
change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
 
15079
                          List<Item> &res_selected_fields,
 
15080
                          List<Item> &res_all_fields, uint32_t elements,
 
15081
                          List<Item> &all_fields)
6041
15082
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
15083
  List_iterator_fast<Item> it(all_fields);
6043
15084
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
15085
  res_selected_fields.empty();
 
15086
  res_all_fields.empty();
6046
15087
 
6047
15088
  uint32_t i, border= all_fields.elements - elements;
6048
15089
  for (i= 0; (item= it++); i++)
6052
15093
      new_item;
6053
15094
  }
6054
15095
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
15096
  List_iterator_fast<Item> itr(res_all_fields);
6056
15097
  for (i= 0; i < border; i++)
6057
15098
    itr++;
6058
15099
  itr.sublist(res_selected_fields, elements);
6060
15101
  return session->is_fatal_error;
6061
15102
}
6062
15103
 
 
15104
 
 
15105
 
6063
15106
/******************************************************************************
6064
15107
  Code for calculating functions
6065
15108
******************************************************************************/
6066
15109
 
 
15110
 
6067
15111
/**
6068
15112
  Call ::setup for all sum functions.
6069
15113
 
6070
 
  @param session           thread Cursor
 
15114
  @param session           thread handler
6071
15115
  @param func_ptr      sum function list
6072
15116
 
6073
15117
  @retval
6075
15119
  @retval
6076
15120
    true   error
6077
15121
*/
6078
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15122
 
 
15123
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
6079
15124
{
6080
15125
  Item_sum *func;
6081
15126
  while ((func= *(func_ptr++)))
6086
15131
  return(false);
6087
15132
}
6088
15133
 
6089
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15134
 
 
15135
static void
 
15136
init_tmptable_sum_functions(Item_sum **func_ptr)
6090
15137
{
6091
15138
  Item_sum *func;
6092
15139
  while ((func= *(func_ptr++)))
6093
15140
    func->reset_field();
6094
15141
}
6095
15142
 
 
15143
 
6096
15144
/** Update record 0 in tmp_table from record 1. */
6097
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15145
 
 
15146
static void
 
15147
update_tmptable_sum_func(Item_sum **func_ptr, Table *)
6098
15148
{
6099
15149
  Item_sum *func;
6100
15150
  while ((func= *(func_ptr++)))
6101
15151
    func->update_field();
6102
15152
}
6103
15153
 
 
15154
 
6104
15155
/** Copy result of sum functions to record in tmp_table. */
6105
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15156
 
 
15157
static void
 
15158
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6106
15159
{
6107
15160
  for (; func_ptr != end_ptr ; func_ptr++)
6108
15161
    (void) (*func_ptr)->save_in_result_field(1);
6109
15162
  return;
6110
15163
}
6111
15164
 
6112
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15165
 
 
15166
static bool
 
15167
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6113
15168
{
6114
15169
  for (; func_ptr != end_ptr ;func_ptr++)
6115
15170
  {
6125
15180
  return 0;
6126
15181
}
6127
15182
 
6128
 
bool update_sum_func(Item_sum **func_ptr)
 
15183
 
 
15184
static bool
 
15185
update_sum_func(Item_sum **func_ptr)
6129
15186
{
6130
15187
  Item_sum *func;
6131
15188
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6135
15192
}
6136
15193
 
6137
15194
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
15195
 
 
15196
void
 
15197
copy_funcs(Item **func_ptr)
6139
15198
{
6140
15199
  Item *func;
6141
15200
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
15201
    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
 
}
 
15202
}
 
15203
 
 
15204
 
 
15205
/**
 
15206
  Create a condition for a const reference and add this to the
 
15207
  currenct select for the table.
 
15208
*/
 
15209
 
 
15210
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
 
15211
{
 
15212
  if (!join_tab->ref.key_parts)
 
15213
    return(false);
 
15214
 
 
15215
  Item_cond_and *cond=new Item_cond_and();
 
15216
  Table *table=join_tab->table;
 
15217
  int error;
 
15218
  if (!cond)
 
15219
    return(true);
 
15220
 
 
15221
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15222
  {
 
15223
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15224
                              fieldnr-1];
 
15225
    Item *value=join_tab->ref.items[i];
 
15226
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15227
  }
 
15228
  if (session->is_fatal_error)
 
15229
    return(true);
 
15230
 
 
15231
  if (!cond->fixed)
 
15232
    cond->fix_fields(session, (Item**)&cond);
 
15233
  if (join_tab->select)
 
15234
  {
 
15235
    error=(int) cond->add(join_tab->select->cond);
 
15236
    join_tab->select_cond=join_tab->select->cond=cond;
 
15237
  }
 
15238
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15239
                                          &error)))
 
15240
    join_tab->select_cond=cond;
 
15241
 
 
15242
  return(error ? true : false);
 
15243
}
 
15244
 
6155
15245
 
6156
15246
/**
6157
15247
  Free joins of subselect of this select.
6158
15248
 
6159
15249
  @param session      Session pointer
6160
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
15250
  @param select   pointer to st_select_lex which subselects joins we will free
6161
15251
*/
6162
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
15252
 
 
15253
void free_underlaid_joins(Session *, SELECT_LEX *select)
6163
15254
{
6164
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
15255
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6165
15256
       unit;
6166
15257
       unit= unit->next_unit())
6167
15258
    unit->cleanup();
6202
15293
  @param changed        out:  returns 1 if item contains a replaced field item
6203
15294
 
6204
15295
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
15296
    - TODO: Some functions are not null-preserving. For those functions
6206
15297
    updating of the maybe_null attribute is an overkill.
6207
15298
 
6208
15299
  @retval
6210
15301
  @retval
6211
15302
    1   on error
6212
15303
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15304
 
 
15305
static bool change_group_ref(Session *session, Item_func *expr, order_st *group_list,
 
15306
                             bool *changed)
6214
15307
{
6215
15308
  if (expr->arg_count)
6216
15309
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
15310
    Name_resolution_context *context= &session->lex->current_select->context;
6218
15311
    Item **arg,**arg_end;
6219
15312
    bool arg_changed= false;
6220
15313
    for (arg= expr->arguments(),
6224
15317
      Item *item= *arg;
6225
15318
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
15319
      {
6227
 
        Order *group_tmp;
 
15320
        order_st *group_tmp;
6228
15321
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
15322
        {
6230
15323
          if (item->eq(*group_tmp->item,0))
6254
15347
}
6255
15348
 
6256
15349
 
 
15350
/** Allocate memory needed for other rollup functions. */
 
15351
 
 
15352
bool JOIN::rollup_init()
 
15353
{
 
15354
  uint32_t i,j;
 
15355
  Item **ref_array;
 
15356
 
 
15357
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15358
  rollup.state= ROLLUP::STATE_INITED;
 
15359
 
 
15360
  /*
 
15361
    Create pointers to the different sum function groups
 
15362
    These are updated by rollup_make_fields()
 
15363
  */
 
15364
  tmp_table_param.group_parts= send_group_parts;
 
15365
 
 
15366
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
15367
                                                sizeof(Item**) +
 
15368
                                                sizeof(List<Item>) +
 
15369
                                                ref_pointer_array_size)
 
15370
                                                * send_group_parts )))
 
15371
    return 1;
 
15372
 
 
15373
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15374
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15375
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15376
 
 
15377
  /*
 
15378
    Prepare space for field list for the different levels
 
15379
    These will be filled up in rollup_make_fields()
 
15380
  */
 
15381
  for (i= 0 ; i < send_group_parts ; i++)
 
15382
  {
 
15383
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
15384
    List<Item> *rollup_fields= &rollup.fields[i];
 
15385
    rollup_fields->empty();
 
15386
    rollup.ref_pointer_arrays[i]= ref_array;
 
15387
    ref_array+= all_fields.elements;
 
15388
  }
 
15389
  for (i= 0 ; i < send_group_parts; i++)
 
15390
  {
 
15391
    for (j=0 ; j < fields_list.elements ; j++)
 
15392
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15393
  }
 
15394
  List_iterator<Item> it(all_fields);
 
15395
  Item *item;
 
15396
  while ((item= it++))
 
15397
  {
 
15398
    order_st *group_tmp;
 
15399
    bool found_in_group= 0;
 
15400
 
 
15401
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15402
    {
 
15403
      if (*group_tmp->item == item)
 
15404
      {
 
15405
        item->maybe_null= 1;
 
15406
        found_in_group= 1;
 
15407
        if (item->const_item())
 
15408
        {
 
15409
          /*
 
15410
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15411
            is wrapped up into an Item_func object yielding the same value
 
15412
            as the constant item. The objects of the wrapper class are never
 
15413
            considered as constant items and besides they inherit all
 
15414
            properties of the Item_result_field class.
 
15415
            This wrapping allows us to ensure writing constant items
 
15416
            into temporary tables whenever the result of the ROLLUP
 
15417
            operation has to be written into a temporary table, e.g. when
 
15418
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15419
            Usually when creating temporary tables for a intermidiate
 
15420
            result we do not include fields for constant expressions.
 
15421
          */
 
15422
          Item* new_item= new Item_func_rollup_const(item);
 
15423
          if (!new_item)
 
15424
            return 1;
 
15425
          new_item->fix_fields(session, (Item **) 0);
 
15426
          session->change_item_tree(it.ref(), new_item);
 
15427
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15428
          {
 
15429
            if (*tmp->item == item)
 
15430
              session->change_item_tree(tmp->item, new_item);
 
15431
          }
 
15432
        }
 
15433
      }
 
15434
    }
 
15435
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15436
    {
 
15437
      bool changed= false;
 
15438
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
 
15439
        return 1;
 
15440
      /*
 
15441
        We have to prevent creation of a field in a temporary table for
 
15442
        an expression that contains GROUP BY attributes.
 
15443
        Marking the expression item as 'with_sum_func' will ensure this.
 
15444
      */
 
15445
      if (changed)
 
15446
        item->with_sum_func= 1;
 
15447
    }
 
15448
  }
 
15449
  return 0;
 
15450
}
 
15451
 
 
15452
 
 
15453
/**
 
15454
  Fill up rollup structures with pointers to fields to use.
 
15455
 
 
15456
  Creates copies of item_sum items for each sum level.
 
15457
 
 
15458
  @param fields_arg             List of all fields (hidden and real ones)
 
15459
  @param sel_fields             Pointer to selected fields
 
15460
  @param func                   Store here a pointer to all fields
 
15461
 
 
15462
  @retval
 
15463
    0   if ok;
 
15464
    In this case func is pointing to next not used element.
 
15465
  @retval
 
15466
    1    on error
 
15467
*/
 
15468
 
 
15469
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15470
                              Item_sum ***func)
 
15471
{
 
15472
  List_iterator_fast<Item> it(fields_arg);
 
15473
  Item *first_field= sel_fields.head();
 
15474
  uint32_t level;
 
15475
 
 
15476
  /*
 
15477
    Create field lists for the different levels
 
15478
 
 
15479
    The idea here is to have a separate field list for each rollup level to
 
15480
    avoid all runtime checks of which columns should be NULL.
 
15481
 
 
15482
    The list is stored in reverse order to get sum function in such an order
 
15483
    in func that it makes it easy to reset them with init_sum_functions()
 
15484
 
 
15485
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15486
 
 
15487
    rollup.fields[0] will contain list where a,b,c is NULL
 
15488
    rollup.fields[1] will contain list where b,c is NULL
 
15489
    ...
 
15490
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15491
    ...
 
15492
    sum_funcs_end[0] points to all sum functions
 
15493
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15494
    ...
 
15495
  */
 
15496
 
 
15497
  for (level=0 ; level < send_group_parts ; level++)
 
15498
  {
 
15499
    uint32_t i;
 
15500
    uint32_t pos= send_group_parts - level -1;
 
15501
    bool real_fields= 0;
 
15502
    Item *item;
 
15503
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15504
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15505
    order_st *start_group;
 
15506
 
 
15507
    /* Point to first hidden field */
 
15508
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15509
 
 
15510
    /* Remember where the sum functions ends for the previous level */
 
15511
    sum_funcs_end[pos+1]= *func;
 
15512
 
 
15513
    /* Find the start of the group for this level */
 
15514
    for (i= 0, start_group= group_list ;
 
15515
         i++ < pos ;
 
15516
         start_group= start_group->next)
 
15517
      ;
 
15518
 
 
15519
    it.rewind();
 
15520
    while ((item= it++))
 
15521
    {
 
15522
      if (item == first_field)
 
15523
      {
 
15524
        real_fields= 1;                         // End of hidden fields
 
15525
        ref_array= ref_array_start;
 
15526
      }
 
15527
 
 
15528
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15529
          (!((Item_sum*) item)->depended_from() ||
 
15530
           ((Item_sum *)item)->depended_from() == select_lex))
 
15531
 
 
15532
      {
 
15533
        /*
 
15534
          This is a top level summary function that must be replaced with
 
15535
          a sum function that is reset for this level.
 
15536
 
 
15537
          NOTE: This code creates an object which is not that nice in a
 
15538
          sub select.  Fortunately it's not common to have rollup in
 
15539
          sub selects.
 
15540
        */
 
15541
        item= item->copy_or_same(session);
 
15542
        ((Item_sum*) item)->make_unique();
 
15543
        *(*func)= (Item_sum*) item;
 
15544
        (*func)++;
 
15545
      }
 
15546
      else
 
15547
      {
 
15548
        /* Check if this is something that is part of this group by */
 
15549
        order_st *group_tmp;
 
15550
        for (group_tmp= start_group, i= pos ;
 
15551
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15552
        {
 
15553
          if (*group_tmp->item == item)
 
15554
          {
 
15555
            /*
 
15556
              This is an element that is used by the GROUP BY and should be
 
15557
              set to NULL in this level
 
15558
            */
 
15559
            Item_null_result *null_item= new (session->mem_root) Item_null_result();
 
15560
            if (!null_item)
 
15561
              return 1;
 
15562
            item->maybe_null= 1;                // Value will be null sometimes
 
15563
            null_item->result_field= item->get_tmp_table_field();
 
15564
            item= null_item;
 
15565
            break;
 
15566
          }
 
15567
        }
 
15568
      }
 
15569
      *ref_array= item;
 
15570
      if (real_fields)
 
15571
      {
 
15572
        (void) new_it++;                        // Point to next item
 
15573
        new_it.replace(item);                   // Replace previous
 
15574
        ref_array++;
 
15575
      }
 
15576
      else
 
15577
        ref_array--;
 
15578
    }
 
15579
  }
 
15580
  sum_funcs_end[0]= *func;                      // Point to last function
 
15581
  return 0;
 
15582
}
 
15583
 
 
15584
/**
 
15585
  Send all rollup levels higher than the current one to the client.
 
15586
 
 
15587
  @b SAMPLE
 
15588
    @code
 
15589
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15590
  @endcode
 
15591
 
 
15592
  @param idx            Level we are on:
 
15593
                        - 0 = Total sum level
 
15594
                        - 1 = First group changed  (a)
 
15595
                        - 2 = Second group changed (a,b)
 
15596
 
 
15597
  @retval
 
15598
    0   ok
 
15599
  @retval
 
15600
    1   If send_data_failed()
 
15601
*/
 
15602
 
 
15603
int JOIN::rollup_send_data(uint32_t idx)
 
15604
{
 
15605
  uint32_t i;
 
15606
  for (i= send_group_parts ; i-- > idx ; )
 
15607
  {
 
15608
    /* Get reference pointers to sum functions in place */
 
15609
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15610
           ref_pointer_array_size);
 
15611
    if ((!having || having->val_int()))
 
15612
    {
 
15613
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15614
          result->send_data(rollup.fields[i]))
 
15615
        return 1;
 
15616
      send_records++;
 
15617
    }
 
15618
  }
 
15619
  /* Restore ref_pointer_array */
 
15620
  set_items_ref_array(current_ref_pointer_array);
 
15621
  return 0;
 
15622
}
 
15623
 
 
15624
/**
 
15625
  Write all rollup levels higher than the current one to a temp table.
 
15626
 
 
15627
  @b SAMPLE
 
15628
    @code
 
15629
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15630
  @endcode
 
15631
 
 
15632
  @param idx                 Level we are on:
 
15633
                               - 0 = Total sum level
 
15634
                               - 1 = First group changed  (a)
 
15635
                               - 2 = Second group changed (a,b)
 
15636
  @param table               reference to temp table
 
15637
 
 
15638
  @retval
 
15639
    0   ok
 
15640
  @retval
 
15641
    1   if write_data_failed()
 
15642
*/
 
15643
 
 
15644
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15645
{
 
15646
  uint32_t i;
 
15647
  for (i= send_group_parts ; i-- > idx ; )
 
15648
  {
 
15649
    /* Get reference pointers to sum functions in place */
 
15650
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15651
           ref_pointer_array_size);
 
15652
    if ((!having || having->val_int()))
 
15653
    {
 
15654
      int write_error;
 
15655
      Item *item;
 
15656
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15657
      while ((item= it++))
 
15658
      {
 
15659
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15660
          item->save_in_result_field(1);
 
15661
      }
 
15662
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15663
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15664
      {
 
15665
        if (create_myisam_from_heap(session, table_arg,
 
15666
                                    tmp_table_param.start_recinfo,
 
15667
                                    &tmp_table_param.recinfo,
 
15668
                                    write_error, 0))
 
15669
          return 1;
 
15670
      }
 
15671
    }
 
15672
  }
 
15673
  /* Restore ref_pointer_array */
 
15674
  set_items_ref_array(current_ref_pointer_array);
 
15675
  return 0;
 
15676
}
 
15677
 
 
15678
/**
 
15679
  clear results if there are not rows found for group
 
15680
  (end_send_group/end_write_group)
 
15681
*/
 
15682
 
 
15683
void JOIN::clear()
 
15684
{
 
15685
  clear_tables(this);
 
15686
  copy_fields(&tmp_table_param);
 
15687
 
 
15688
  if (sum_funcs)
 
15689
  {
 
15690
    Item_sum *func, **func_ptr= sum_funcs;
 
15691
    while ((func= *(func_ptr++)))
 
15692
      func->clear();
 
15693
  }
 
15694
}
 
15695
 
 
15696
/**
 
15697
  EXPLAIN handling.
 
15698
 
 
15699
  Send a description about what how the select will be done to stdout.
 
15700
*/
 
15701
 
 
15702
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15703
                     bool distinct,const char *message)
 
15704
{
 
15705
  List<Item> field_list;
 
15706
  List<Item> item_list;
 
15707
  Session *session=join->session;
 
15708
  select_result *result=join->result;
 
15709
  Item *item_null= new Item_null();
 
15710
  const CHARSET_INFO * const cs= system_charset_info;
 
15711
  int quick_type;
 
15712
  /* Don't log this into the slow query log */
 
15713
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15714
  join->unit->offset_limit_cnt= 0;
 
15715
 
 
15716
  /*
 
15717
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15718
    EXPLAIN column types as they're "defined" in Session::send_explain_fields()
 
15719
  */
 
15720
  if (message)
 
15721
  {
 
15722
    item_list.push_back(new Item_int((int32_t)
 
15723
                                     join->select_lex->select_number));
 
15724
    item_list.push_back(new Item_string(join->select_lex->type,
 
15725
                                        strlen(join->select_lex->type), cs));
 
15726
    for (uint32_t i=0 ; i < 7; i++)
 
15727
      item_list.push_back(item_null);
 
15728
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15729
      item_list.push_back(item_null);
 
15730
 
 
15731
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15732
    if (result->send_data(item_list))
 
15733
      join->error= 1;
 
15734
  }
 
15735
  else if (join->select_lex == join->unit->fake_select_lex)
 
15736
  {
 
15737
    /*
 
15738
      here we assume that the query will return at least two rows, so we
 
15739
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15740
      and no filesort will be actually done, but executing all selects in
 
15741
      the UNION to provide precise EXPLAIN information will hardly be
 
15742
      appreciated :)
 
15743
    */
 
15744
    char table_name_buffer[NAME_LEN];
 
15745
    item_list.empty();
 
15746
    /* id */
 
15747
    item_list.push_back(new Item_null);
 
15748
    /* select_type */
 
15749
    item_list.push_back(new Item_string(join->select_lex->type,
 
15750
                                        strlen(join->select_lex->type),
 
15751
                                        cs));
 
15752
    /* table */
 
15753
    {
 
15754
      SELECT_LEX *sl= join->unit->first_select();
 
15755
      uint32_t len= 6, lastop= 0;
 
15756
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15757
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15758
      {
 
15759
        len+= lastop;
 
15760
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15761
                         "%u,", sl->select_number);
 
15762
      }
 
15763
      if (sl || len + lastop >= NAME_LEN)
 
15764
      {
 
15765
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15766
        len+= 4;
 
15767
      }
 
15768
      else
 
15769
      {
 
15770
        len+= lastop;
 
15771
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15772
      }
 
15773
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15774
    }
 
15775
    /* type */
 
15776
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15777
                                          strlen(join_type_str[JT_ALL]),
 
15778
                                          cs));
 
15779
    /* possible_keys */
 
15780
    item_list.push_back(item_null);
 
15781
    /* key*/
 
15782
    item_list.push_back(item_null);
 
15783
    /* key_len */
 
15784
    item_list.push_back(item_null);
 
15785
    /* ref */
 
15786
    item_list.push_back(item_null);
 
15787
    /* in_rows */
 
15788
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15789
      item_list.push_back(item_null);
 
15790
    /* rows */
 
15791
    item_list.push_back(item_null);
 
15792
    /* extra */
 
15793
    if (join->unit->global_parameters->order_list.first)
 
15794
      item_list.push_back(new Item_string("Using filesort",
 
15795
                                          14, cs));
 
15796
    else
 
15797
      item_list.push_back(new Item_string("", 0, cs));
 
15798
 
 
15799
    if (result->send_data(item_list))
 
15800
      join->error= 1;
 
15801
  }
 
15802
  else
 
15803
  {
 
15804
    table_map used_tables=0;
 
15805
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15806
    {
 
15807
      JOIN_TAB *tab=join->join_tab+i;
 
15808
      Table *table=tab->table;
 
15809
      TableList *table_list= tab->table->pos_in_table_list;
 
15810
      char buff[512];
 
15811
      char buff1[512], buff2[512], buff3[512];
 
15812
      char keylen_str_buf[64];
 
15813
      String extra(buff, sizeof(buff),cs);
 
15814
      char table_name_buffer[NAME_LEN];
 
15815
      String tmp1(buff1,sizeof(buff1),cs);
 
15816
      String tmp2(buff2,sizeof(buff2),cs);
 
15817
      String tmp3(buff3,sizeof(buff3),cs);
 
15818
      extra.length(0);
 
15819
      tmp1.length(0);
 
15820
      tmp2.length(0);
 
15821
      tmp3.length(0);
 
15822
 
 
15823
      quick_type= -1;
 
15824
      item_list.empty();
 
15825
      /* id */
 
15826
      item_list.push_back(new Item_uint((uint32_t)
 
15827
                                       join->select_lex->select_number));
 
15828
      /* select_type */
 
15829
      item_list.push_back(new Item_string(join->select_lex->type,
 
15830
                                          strlen(join->select_lex->type),
 
15831
                                          cs));
 
15832
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15833
      {
 
15834
        quick_type= tab->select->quick->get_type();
 
15835
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15836
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15837
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15838
          tab->type = JT_INDEX_MERGE;
 
15839
        else
 
15840
          tab->type = JT_RANGE;
 
15841
      }
 
15842
      /* table */
 
15843
      if (table->derived_select_number)
 
15844
      {
 
15845
        /* Derived table name generation */
 
15846
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15847
                          "<derived%u>",
 
15848
                          table->derived_select_number);
 
15849
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15850
      }
 
15851
      else
 
15852
      {
 
15853
        TableList *real_table= table->pos_in_table_list;
 
15854
        item_list.push_back(new Item_string(real_table->alias,
 
15855
                                            strlen(real_table->alias),
 
15856
                                            cs));
 
15857
      }
 
15858
      /* "type" column */
 
15859
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15860
                                          strlen(join_type_str[tab->type]),
 
15861
                                          cs));
 
15862
      /* Build "possible_keys" value and add it to item_list */
 
15863
      if (!tab->keys.is_clear_all())
 
15864
      {
 
15865
        uint32_t j;
 
15866
        for (j=0 ; j < table->s->keys ; j++)
 
15867
        {
 
15868
          if (tab->keys.is_set(j))
 
15869
          {
 
15870
            if (tmp1.length())
 
15871
              tmp1.append(',');
 
15872
            tmp1.append(table->key_info[j].name,
 
15873
                        strlen(table->key_info[j].name),
 
15874
                        system_charset_info);
 
15875
          }
 
15876
        }
 
15877
      }
 
15878
      if (tmp1.length())
 
15879
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15880
      else
 
15881
        item_list.push_back(item_null);
 
15882
 
 
15883
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15884
      if (tab->ref.key_parts)
 
15885
      {
 
15886
        KEY *key_info=table->key_info+ tab->ref.key;
 
15887
        register uint32_t length;
 
15888
        item_list.push_back(new Item_string(key_info->name,
 
15889
                                            strlen(key_info->name),
 
15890
                                            system_charset_info));
 
15891
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
 
15892
                keylen_str_buf;
 
15893
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15894
                                            system_charset_info));
 
15895
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15896
        {
 
15897
          if (tmp2.length())
 
15898
            tmp2.append(',');
 
15899
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15900
                      system_charset_info);
 
15901
        }
 
15902
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15903
      }
 
15904
      else if (tab->type == JT_NEXT)
 
15905
      {
 
15906
        KEY *key_info=table->key_info+ tab->index;
 
15907
        register uint32_t length;
 
15908
        item_list.push_back(new Item_string(key_info->name,
 
15909
                                            strlen(key_info->name),cs));
 
15910
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
 
15911
                keylen_str_buf;
 
15912
        item_list.push_back(new Item_string(keylen_str_buf,
 
15913
                                            length,
 
15914
                                            system_charset_info));
 
15915
        item_list.push_back(item_null);
 
15916
      }
 
15917
      else if (tab->select && tab->select->quick)
 
15918
      {
 
15919
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15920
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15921
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15922
        item_list.push_back(item_null);
 
15923
      }
 
15924
      else
 
15925
      {
 
15926
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15927
        {
 
15928
          const char *tmp_buff;
 
15929
          int f_idx;
 
15930
          if (table_list->has_db_lookup_value)
 
15931
          {
 
15932
            f_idx= table_list->schema_table->idx_field1;
 
15933
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15934
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15935
          }
 
15936
          if (table_list->has_table_lookup_value)
 
15937
          {
 
15938
            if (table_list->has_db_lookup_value)
 
15939
              tmp2.append(',');
 
15940
            f_idx= table_list->schema_table->idx_field2;
 
15941
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15942
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15943
          }
 
15944
          if (tmp2.length())
 
15945
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15946
          else
 
15947
            item_list.push_back(item_null);
 
15948
        }
 
15949
        else
 
15950
          item_list.push_back(item_null);
 
15951
        item_list.push_back(item_null);
 
15952
        item_list.push_back(item_null);
 
15953
      }
 
15954
 
 
15955
      /* Add "rows" field to item_list. */
 
15956
      if (table_list->schema_table)
 
15957
      {
 
15958
        /* in_rows */
 
15959
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15960
          item_list.push_back(item_null);
 
15961
        /* rows */
 
15962
        item_list.push_back(item_null);
 
15963
      }
 
15964
      else
 
15965
      {
 
15966
        double examined_rows;
 
15967
        if (tab->select && tab->select->quick)
 
15968
          examined_rows= rows2double(tab->select->quick->records);
 
15969
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15970
          examined_rows= rows2double(tab->limit ? tab->limit :
 
15971
                                     tab->table->file->records());
 
15972
        else
 
15973
          examined_rows= join->best_positions[i].records_read;
 
15974
 
 
15975
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
 
15976
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
15977
 
 
15978
        /* Add "filtered" field to item_list. */
 
15979
        if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15980
        {
 
15981
          float f= 0.0;
 
15982
          if (examined_rows)
 
15983
            f= (float) (100.0 * join->best_positions[i].records_read /
 
15984
                        examined_rows);
 
15985
          item_list.push_back(new Item_float(f, 2));
 
15986
        }
 
15987
      }
 
15988
 
 
15989
      /* Build "Extra" field and add it to item_list. */
 
15990
      bool key_read=table->key_read;
 
15991
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
15992
          table->covering_keys.is_set(tab->index))
 
15993
        key_read=1;
 
15994
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
15995
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
15996
        key_read=1;
 
15997
 
 
15998
      if (tab->info)
 
15999
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16000
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16001
      {
 
16002
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16003
          extra.append(STRING_WITH_LEN("; Using index"));
 
16004
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16005
          extra.append(STRING_WITH_LEN("; Using where"));
 
16006
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16007
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16008
        /* Skip initial "; "*/
 
16009
        const char *str= extra.ptr();
 
16010
        uint32_t len= extra.length();
 
16011
        if (len)
 
16012
        {
 
16013
          str += 2;
 
16014
          len -= 2;
 
16015
        }
 
16016
        item_list.push_back(new Item_string(str, len, cs));
 
16017
      }
 
16018
      else
 
16019
      {
 
16020
        uint32_t keyno= MAX_KEY;
 
16021
        if (tab->ref.key_parts)
 
16022
          keyno= tab->ref.key;
 
16023
        else if (tab->select && tab->select->quick)
 
16024
          keyno = tab->select->quick->index;
 
16025
 
 
16026
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16027
            table->file->pushed_idx_cond)
 
16028
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16029
 
 
16030
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
16031
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16032
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16033
        {
 
16034
          extra.append(STRING_WITH_LEN("; Using "));
 
16035
          tab->select->quick->add_info_string(&extra);
 
16036
        }
 
16037
          if (tab->select)
 
16038
        {
 
16039
          if (tab->use_quick == 2)
 
16040
          {
 
16041
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16042
            char buf[MAX_KEY / 4 + 1];
 
16043
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16044
                                         "record (index map: 0x"));
 
16045
            extra.append(tab->keys.print(buf));
 
16046
            extra.append(')');
 
16047
          }
 
16048
          else if (tab->select->cond)
 
16049
          {
 
16050
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16051
 
 
16052
            if (session->variables.engine_condition_pushdown && pushed_cond)
 
16053
            {
 
16054
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16055
                                           "condition"));
 
16056
              if (session->lex->describe & DESCRIBE_EXTENDED)
 
16057
              {
 
16058
                extra.append(STRING_WITH_LEN(": "));
 
16059
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16060
              }
 
16061
            }
 
16062
            else
 
16063
              extra.append(STRING_WITH_LEN("; Using where"));
 
16064
          }
 
16065
        }
 
16066
        if (key_read)
 
16067
        {
 
16068
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16069
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16070
          else
 
16071
            extra.append(STRING_WITH_LEN("; Using index"));
 
16072
        }
 
16073
        if (table->reginfo.not_exists_optimize)
 
16074
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16075
 
 
16076
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16077
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16078
             HA_MRR_USE_DEFAULT_IMPL))
 
16079
        {
 
16080
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16081
        }
 
16082
 
 
16083
        if (table_list->schema_table &&
 
16084
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16085
        {
 
16086
          if (!table_list->table_open_method)
 
16087
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16088
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16089
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16090
          else
 
16091
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16092
          if (table_list->has_db_lookup_value &&
 
16093
              table_list->has_table_lookup_value)
 
16094
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16095
          else if (table_list->has_db_lookup_value ||
 
16096
                   table_list->has_table_lookup_value)
 
16097
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16098
          else
 
16099
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16100
        }
 
16101
        if (need_tmp_table)
 
16102
        {
 
16103
          need_tmp_table=0;
 
16104
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16105
        }
 
16106
        if (need_order)
 
16107
        {
 
16108
          need_order=0;
 
16109
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16110
        }
 
16111
        if (distinct & test_all_bits(used_tables,session->used_tables))
 
16112
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16113
 
 
16114
        if (tab->insideout_match_tab)
 
16115
        {
 
16116
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16117
        }
 
16118
 
 
16119
        if (tab->flush_weedout_table)
 
16120
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16121
        else if (tab->check_weed_out_table)
 
16122
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16123
        else if (tab->do_firstmatch)
 
16124
        {
 
16125
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16126
          Table *prev_table=tab->do_firstmatch->table;
 
16127
          if (prev_table->derived_select_number)
 
16128
          {
 
16129
            char namebuf[NAME_LEN];
 
16130
            /* Derived table name generation */
 
16131
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16132
                              "<derived%u>",
 
16133
                              prev_table->derived_select_number);
 
16134
            extra.append(namebuf, len);
 
16135
          }
 
16136
          else
 
16137
            extra.append(prev_table->pos_in_table_list->alias);
 
16138
          extra.append(STRING_WITH_LEN(")"));
 
16139
        }
 
16140
 
 
16141
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16142
        {
 
16143
          if (tab->ref.cond_guards[part])
 
16144
          {
 
16145
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16146
            break;
 
16147
          }
 
16148
        }
 
16149
 
 
16150
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16151
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16152
 
 
16153
        /* Skip initial "; "*/
 
16154
        const char *str= extra.ptr();
 
16155
        uint32_t len= extra.length();
 
16156
        if (len)
 
16157
        {
 
16158
          str += 2;
 
16159
          len -= 2;
 
16160
        }
 
16161
        item_list.push_back(new Item_string(str, len, cs));
 
16162
      }
 
16163
      // For next iteration
 
16164
      used_tables|=table->map;
 
16165
      if (result->send_data(item_list))
 
16166
        join->error= 1;
 
16167
    }
 
16168
  }
 
16169
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
16170
       unit;
 
16171
       unit= unit->next_unit())
 
16172
  {
 
16173
    if (mysql_explain_union(session, unit, result))
 
16174
      return;
 
16175
  }
 
16176
  return;
 
16177
}
 
16178
 
 
16179
 
 
16180
bool mysql_explain_union(Session *session, SELECT_LEX_UNIT *unit, select_result *result)
 
16181
{
 
16182
  bool res= 0;
 
16183
  SELECT_LEX *first= unit->first_select();
 
16184
 
 
16185
  for (SELECT_LEX *sl= first;
 
16186
       sl;
 
16187
       sl= sl->next_select())
 
16188
  {
 
16189
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16190
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16191
    sl->type= (((&session->lex->select_lex)==sl)?
 
16192
               (sl->first_inner_unit() || sl->next_select() ?
 
16193
                "PRIMARY" : "SIMPLE"):
 
16194
               ((sl == first)?
 
16195
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16196
                 "DERIVED":
 
16197
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16198
                  "DEPENDENT SUBQUERY":
 
16199
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16200
                   "SUBQUERY"))):
 
16201
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16202
                 "DEPENDENT UNION":
 
16203
                 uncacheable?"UNCACHEABLE UNION":
 
16204
                 "UNION")));
 
16205
    sl->options|= SELECT_DESCRIBE;
 
16206
  }
 
16207
  if (unit->is_union())
 
16208
  {
 
16209
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16210
    unit->fake_select_lex->type= "UNION RESULT";
 
16211
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16212
    if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16213
      res= unit->exec();
 
16214
    res|= unit->cleanup();
 
16215
  }
 
16216
  else
 
16217
  {
 
16218
    session->lex->current_select= first;
 
16219
    unit->set_limit(unit->global_parameters);
 
16220
    res= mysql_select(session, &first->ref_pointer_array,
 
16221
                        (TableList*) first->table_list.first,
 
16222
                        first->with_wild, first->item_list,
 
16223
                        first->where,
 
16224
                        first->order_list.elements +
 
16225
                        first->group_list.elements,
 
16226
                        (order_st*) first->order_list.first,
 
16227
                        (order_st*) first->group_list.first,
 
16228
                        first->having,
 
16229
                        (order_st*) session->lex->proc_list.first,
 
16230
                        first->options | session->options | SELECT_DESCRIBE,
 
16231
                        result, unit, first);
 
16232
  }
 
16233
  return(res || session->is_error());
 
16234
}
 
16235
 
 
16236
 
6257
16237
static void print_table_array(Session *session, String *str, TableList **table,
6258
16238
                              TableList **end)
6259
16239
{
6269
16249
    }
6270
16250
    else if (curr->straight)
6271
16251
      str->append(STRING_WITH_LEN(" straight_join "));
 
16252
    else if (curr->sj_inner_tables)
 
16253
      str->append(STRING_WITH_LEN(" semi join "));
6272
16254
    else
6273
16255
      str->append(STRING_WITH_LEN(" join "));
6274
16256
    curr->print(session, str, QT_ORDINARY);
6281
16263
  }
6282
16264
}
6283
16265
 
 
16266
 
6284
16267
/**
6285
16268
  Print joins from the FROM clause.
6286
 
  @param session     thread Cursor
 
16269
  @param session     thread handler
6287
16270
  @param str     string where table should be printed
6288
16271
  @param tables  list of tables in join
6289
16272
  @query_type    type of the query is being generated
6290
16273
*/
6291
 
void print_join(Session *session, String *str,
6292
 
                List<TableList> *tables, enum_query_type)
 
16274
 
 
16275
static void print_join(Session *session, String *str,
 
16276
                       List<TableList> *tables, enum_query_type)
6293
16277
{
6294
16278
  /* 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*) *
 
16279
  List_iterator_fast<TableList> ti(*tables);
 
16280
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
16281
                                                tables->elements);
6298
16282
  if (table == 0)
6299
16283
    return;  // out of memory
6300
16284
 
6301
16285
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6302
16286
    *t= ti++;
 
16287
 
 
16288
  /*
 
16289
    If the first table is a semi-join nest, swap it with something that is
 
16290
    not a semi-join nest.
 
16291
  */
 
16292
  if ((*table)->sj_inner_tables)
 
16293
  {
 
16294
    TableList **end= table + tables->elements;
 
16295
    for (TableList **t2= table; t2!=end; t2++)
 
16296
    {
 
16297
      if (!(*t2)->sj_inner_tables)
 
16298
      {
 
16299
        TableList *tmp= *t2;
 
16300
        *t2= *table;
 
16301
        *table= tmp;
 
16302
        break;
 
16303
      }
 
16304
    }
 
16305
  }
6303
16306
  assert(tables->elements >= 1);
6304
16307
  print_table_array(session, str, table, table + tables->elements);
6305
16308
}
6306
16309
 
6307
 
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
 
16310
 
 
16311
/**
 
16312
  @brief Print an index hint
 
16313
 
 
16314
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16315
 
 
16316
  @param      session         the current thread
 
16317
  @param[out] str         appends the index hint here
 
16318
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16319
                          "FORCE INDEX"|"IGNORE INDEX")
 
16320
  @param      hint_length the length of the string in 'hint'
 
16321
  @param      indexes     a list of index names for the hint
 
16322
*/
 
16323
 
 
16324
void
 
16325
Index_hint::print(Session *session, String *str)
 
16326
{
 
16327
  switch (type)
 
16328
  {
 
16329
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16330
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16331
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16332
  }
 
16333
  str->append (STRING_WITH_LEN(" ("));
 
16334
  if (key_name.length)
 
16335
  {
 
16336
    if (session && is_primary_key_name(key_name.str))
 
16337
      str->append(is_primary_key_name(key_name.str));
 
16338
    else
 
16339
      append_identifier(session, str, key_name.str, key_name.length);
 
16340
  }
 
16341
  str->append(')');
 
16342
}
 
16343
 
 
16344
 
 
16345
/**
 
16346
  Print table as it should be in join list.
 
16347
 
 
16348
  @param str   string where table should be printed
 
16349
*/
 
16350
 
 
16351
void TableList::print(Session *session, String *str, enum_query_type query_type)
 
16352
{
 
16353
  if (nested_join)
 
16354
  {
 
16355
    str->append('(');
 
16356
    print_join(session, str, &nested_join->join_list, query_type);
 
16357
    str->append(')');
 
16358
  }
 
16359
  else
 
16360
  {
 
16361
    const char *cmp_name;                         // Name to compare with alias
 
16362
    if (derived)
 
16363
    {
 
16364
      // A derived table
 
16365
      str->append('(');
 
16366
      derived->print(str, query_type);
 
16367
      str->append(')');
 
16368
      cmp_name= "";                               // Force printing of alias
 
16369
    }
 
16370
    else
 
16371
    {
 
16372
      // A normal table
 
16373
      {
 
16374
        append_identifier(session, str, db, db_length);
 
16375
        str->append('.');
 
16376
      }
 
16377
      if (schema_table)
 
16378
      {
 
16379
        append_identifier(session, str, schema_table_name,
 
16380
                          strlen(schema_table_name));
 
16381
        cmp_name= schema_table_name;
 
16382
      }
 
16383
      else
 
16384
      {
 
16385
        append_identifier(session, str, table_name, table_name_length);
 
16386
        cmp_name= table_name;
 
16387
      }
 
16388
    }
 
16389
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16390
    {
 
16391
      char t_alias_buff[MAX_ALIAS_NAME];
 
16392
      const char *t_alias= alias;
 
16393
 
 
16394
      str->append(' ');
 
16395
      if (lower_case_table_names== 1)
 
16396
      {
 
16397
        if (alias && alias[0])
 
16398
        {
 
16399
          strcpy(t_alias_buff, alias);
 
16400
          my_casedn_str(files_charset_info, t_alias_buff);
 
16401
          t_alias= t_alias_buff;
 
16402
        }
 
16403
      }
 
16404
 
 
16405
      append_identifier(session, str, t_alias, strlen(t_alias));
 
16406
    }
 
16407
 
 
16408
    if (index_hints)
 
16409
    {
 
16410
      List_iterator<Index_hint> it(*index_hints);
 
16411
      Index_hint *hint;
 
16412
 
 
16413
      while ((hint= it++))
 
16414
      {
 
16415
        str->append (STRING_WITH_LEN(" "));
 
16416
        hint->print (session, str);
 
16417
      }
 
16418
    }
 
16419
  }
 
16420
}
 
16421
 
 
16422
 
 
16423
void st_select_lex::print(Session *session, String *str, enum_query_type query_type)
6308
16424
{
6309
16425
  /* QQ: session may not be set for sub queries, but this should be fixed */
6310
 
  if(not session)
 
16426
  if (!session)
6311
16427
    session= current_session;
6312
16428
 
6313
 
 
6314
16429
  str->append(STRING_WITH_LEN("select "));
6315
16430
 
6316
16431
  /* First add options */
6317
16432
  if (options & SELECT_STRAIGHT_JOIN)
6318
16433
    str->append(STRING_WITH_LEN("straight_join "));
 
16434
  if ((session->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16435
      (this == &session->lex->select_lex))
 
16436
    str->append(STRING_WITH_LEN("high_priority "));
6319
16437
  if (options & SELECT_DISTINCT)
6320
16438
    str->append(STRING_WITH_LEN("distinct "));
6321
16439
  if (options & SELECT_SMALL_RESULT)
6329
16447
 
6330
16448
  //Item List
6331
16449
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
16450
  List_iterator_fast<Item> it(item_list);
6333
16451
  Item *item;
6334
16452
  while ((item= it++))
6335
16453
  {
6342
16460
 
6343
16461
  /*
6344
16462
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
16463
    TODO: support USING/FORCE/IGNORE index
6346
16464
  */
6347
16465
  if (table_list.elements)
6348
16466
  {
6376
16494
  if (group_list.elements)
6377
16495
  {
6378
16496
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
16497
    print_order(str, (order_st *) group_list.first, query_type);
6380
16498
    switch (olap)
6381
16499
    {
6382
16500
      case CUBE_TYPE:
6407
16525
  if (order_list.elements)
6408
16526
  {
6409
16527
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
16528
    print_order(str, (order_st *) order_list.first, query_type);
6411
16529
  }
6412
16530
 
6413
16531
  // limit
6416
16534
  // PROCEDURE unsupported here
6417
16535
}
6418
16536
 
 
16537
 
 
16538
/**
 
16539
  change select_result object of JOIN.
 
16540
 
 
16541
  @param res            new select_result object
 
16542
 
 
16543
  @retval
 
16544
    false   OK
 
16545
  @retval
 
16546
    true    error
 
16547
*/
 
16548
 
 
16549
bool JOIN::change_result(select_result *res)
 
16550
{
 
16551
  result= res;
 
16552
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
16553
                     result->prepare2())
 
16554
  {
 
16555
    return(true);
 
16556
  }
 
16557
  return(false);
 
16558
}
 
16559
 
6419
16560
/**
6420
16561
  @} (end of group Query_Optimizer)
6421
16562
*/
6422
 
 
6423
 
} /* namespace drizzled */