~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2008-08-22 19:59:34 UTC
  • Revision ID: brian@tangent.org-20080822195934-c2krkt6759jct6mf
Removed dead bits.

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
19
19
  @brief
20
20
  mysql_select and join optimization
21
21
 
 
22
 
22
23
  @defgroup Query_Optimizer  Query Optimizer
23
24
  @{
24
25
*/
25
 
#include "config.h"
26
 
 
27
 
#include <string>
28
 
#include <iostream>
29
 
#include <algorithm>
30
 
#include <vector>
31
 
 
32
 
#include "drizzled/sql_select.h" /* include join.h */
33
 
 
34
 
#include "drizzled/error.h"
35
 
#include "drizzled/gettext.h"
36
 
#include "drizzled/util/test.h"
37
 
#include "drizzled/name_resolution_context_state.h"
38
 
#include "drizzled/nested_join.h"
39
 
#include "drizzled/probes.h"
40
 
#include "drizzled/show.h"
41
 
#include "drizzled/item/cache.h"
42
 
#include "drizzled/item/cmpfunc.h"
43
 
#include "drizzled/item/copy_string.h"
44
 
#include "drizzled/item/uint.h"
45
 
#include "drizzled/cached_item.h"
46
 
#include "drizzled/sql_base.h"
47
 
#include "drizzled/field/blob.h"
48
 
#include "drizzled/check_stack_overrun.h"
49
 
#include "drizzled/lock.h"
50
 
#include "drizzled/item/outer_ref.h"
51
 
#include "drizzled/index_hint.h"
52
 
#include "drizzled/records.h"
53
 
#include "drizzled/internal/iocache.h"
54
 
#include "drizzled/drizzled.h"
55
 
 
56
 
#include "drizzled/sql_union.h"
57
 
#include "drizzled/optimizer/key_field.h"
58
 
#include "drizzled/optimizer/position.h"
59
 
#include "drizzled/optimizer/sargable_param.h"
60
 
#include "drizzled/optimizer/key_use.h"
61
 
#include "drizzled/optimizer/range.h"
62
 
#include "drizzled/optimizer/quick_range_select.h"
63
 
#include "drizzled/optimizer/quick_ror_intersect_select.h"
64
 
 
65
 
#include "drizzled/filesort.h"
66
 
 
67
 
using namespace std;
68
 
 
69
 
namespace drizzled
70
 
{
71
 
 
72
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
73
 
static COND *build_equal_items(Session *session, COND *cond,
 
26
#include <drizzled/server_includes.h>
 
27
#include <drizzled/sql_select.h>
 
28
#include "sj_tmp_table.h"
 
29
 
 
30
#include <mysys/my_bit.h>
 
31
#include <drizzled/drizzled_error_messages.h>
 
32
#include <libdrizzle/gettext.h>
 
33
 
 
34
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
35
                              "MAYBE_REF","ALL","range","index",
 
36
                              "ref_or_null","unique_subquery","index_subquery",
 
37
                              "index_merge"
 
38
};
 
39
 
 
40
struct st_sargable_param;
 
41
 
 
42
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
43
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
44
                                 DYNAMIC_ARRAY *keyuse);
 
45
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
46
                                JOIN_TAB *join_tab,
 
47
                                uint tables, COND *conds,
 
48
                                COND_EQUAL *cond_equal,
 
49
                                table_map table_map, SELECT_LEX *select_lex,
 
50
                                st_sargable_param **sargables);
 
51
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
52
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
 
53
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
54
                               table_map used_tables);
 
55
static bool choose_plan(JOIN *join,table_map join_tables);
 
56
 
 
57
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
58
                             table_map remaining_tables, uint idx,
 
59
                             double record_count, double read_time);
 
60
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
61
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
62
                             uint depth, uint prune_level);
 
63
static bool best_extension_by_limited_search(JOIN *join,
 
64
                                             table_map remaining_tables,
 
65
                                             uint idx, double record_count,
 
66
                                             double read_time, uint depth,
 
67
                                             uint prune_level);
 
68
static uint determine_search_depth(JOIN* join);
 
69
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
70
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
71
/*
 
72
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
73
  tested and approved.
 
74
*/
 
75
static bool find_best(JOIN *join,table_map rest_tables,uint index,
 
76
                      double record_count,double read_time);
 
77
static uint cache_record_length(JOIN *join,uint index);
 
78
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
 
79
static bool get_best_combination(JOIN *join);
 
80
static store_key *get_store_key(THD *thd,
 
81
                                KEYUSE *keyuse, table_map used_tables,
 
82
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
83
                                uint maybe_null);
 
84
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
85
static void make_outerjoin_info(JOIN *join);
 
86
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
87
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
 
88
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
89
static void update_depend_map(JOIN *join);
 
90
static void update_depend_map(JOIN *join, order_st *order);
 
91
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
92
                           bool change_list, bool *simple_order);
 
93
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
94
                            List<Item> &fields, bool send_row,
 
95
                            uint64_t select_options, const char *info,
 
96
                            Item *having);
 
97
static COND *build_equal_items(THD *thd, COND *cond,
74
98
                               COND_EQUAL *inherited,
75
99
                               List<TableList> *join_list,
76
100
                               COND_EQUAL **cond_equal_ref);
77
 
 
 
101
static COND* substitute_for_best_equal_field(COND *cond,
 
102
                                             COND_EQUAL *cond_equal,
 
103
                                             void *table_join_idx);
 
104
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
105
                            COND *conds, bool top, bool in_sj);
 
106
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
107
static void restore_prev_nj_state(JOIN_TAB *last);
 
108
static void reset_nj_counters(List<TableList> *join_list);
 
109
static uint build_bitmap_for_nested_joins(List<TableList> *join_list,
 
110
                                          uint first_unused);
 
111
 
 
112
static 
 
113
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
114
static void restore_prev_sj_state(const table_map remaining_tables, 
 
115
                                  const JOIN_TAB *tab);
 
116
 
 
117
static COND *optimize_cond(JOIN *join, COND *conds,
 
118
                           List<TableList> *join_list,
 
119
                           Item::cond_result *cond_value);
 
120
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
121
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
122
 
 
123
static enum_nested_loop_state
 
124
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
125
                     int error);
 
126
static enum_nested_loop_state
 
127
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
128
static enum_nested_loop_state
 
129
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
130
static enum_nested_loop_state
 
131
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
132
static enum_nested_loop_state
 
133
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
134
static enum_nested_loop_state
 
135
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
136
static enum_nested_loop_state
 
137
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
138
 
 
139
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
140
static int join_read_system(JOIN_TAB *tab);
 
141
static int join_read_const(JOIN_TAB *tab);
 
142
static int join_read_key(JOIN_TAB *tab);
 
143
static int join_read_always_key(JOIN_TAB *tab);
 
144
static int join_read_last_key(JOIN_TAB *tab);
 
145
static int join_no_more_records(READ_RECORD *info);
 
146
static int join_read_next(READ_RECORD *info);
 
147
static int join_read_next_different(READ_RECORD *info);
 
148
static int join_init_quick_read_record(JOIN_TAB *tab);
 
149
static int test_if_quick_select(JOIN_TAB *tab);
 
150
static int join_init_read_record(JOIN_TAB *tab);
 
151
static int join_read_first(JOIN_TAB *tab);
 
152
static int join_read_next_same(READ_RECORD *info);
 
153
static int join_read_next_same_diff(READ_RECORD *info);
 
154
static int join_read_last(JOIN_TAB *tab);
 
155
static int join_read_prev_same(READ_RECORD *info);
 
156
static int join_read_prev(READ_RECORD *info);
 
157
int join_read_always_key_or_null(JOIN_TAB *tab);
 
158
int join_read_next_same_or_null(READ_RECORD *info);
 
159
static COND *make_cond_for_table(COND *cond,table_map table,
 
160
                                 table_map used_table,
 
161
                                 bool exclude_expensive_cond);
78
162
static Item* part_of_refkey(Table *form,Field *field);
79
 
static bool cmp_buffer_with_ref(JoinTable *tab);
80
 
static void change_cond_ref_to_const(Session *session,
81
 
                                     vector<COND_CMP>& save_list,
82
 
                                     Item *and_father,
83
 
                                     Item *cond,
84
 
                                     Item *field,
85
 
                                     Item *value);
86
 
static bool copy_blobs(Field **ptr);
 
163
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
164
                                    ha_rows select_limit, bool no_changes,
 
165
                                    const key_map *map);
 
166
static bool list_contains_unique_index(Table *table,
 
167
                          bool (*find_func) (Field *, void *), void *data);
 
168
static bool find_field_in_item_list (Field *field, void *data);
 
169
static bool find_field_in_order_list (Field *field, void *data);
 
170
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
 
171
                             ha_rows filesort_limit, ha_rows select_limit,
 
172
                             bool is_order_by);
 
173
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
174
                             Item *having);
 
175
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
 
176
                                   ulong offset,Item *having);
 
177
static int remove_dup_with_hash_index(THD *thd,Table *table,
 
178
                                      uint field_count, Field **first_field,
87
179
 
88
 
static bool eval_const_cond(COND *cond)
89
 
{
90
 
    return ((Item_func*) cond)->val_int() ? true : false;
91
 
}
 
180
                                      ulong key_length,Item *having);
 
181
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
 
182
static ulong used_blob_length(CACHE_FIELD **ptr);
 
183
static bool store_record_in_cache(JOIN_CACHE *cache);
 
184
static void reset_cache_read(JOIN_CACHE *cache);
 
185
static void reset_cache_write(JOIN_CACHE *cache);
 
186
static void read_cached_record(JOIN_TAB *tab);
 
187
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
188
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
189
                                    order_st *order, List<Item> &fields,
 
190
                                    List<Item> &all_fields,
 
191
                                    bool *all_order_by_fields_used);
 
192
static bool test_if_subpart(order_st *a,order_st *b);
 
193
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
194
static void calc_group_buffer(JOIN *join,order_st *group);
 
195
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
196
static bool alloc_group_fields(JOIN *join,order_st *group);
 
197
// Create list for using with tempory table
 
198
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
199
                                     List<Item> &new_list1,
 
200
                                     List<Item> &new_list2,
 
201
                                     uint elements, List<Item> &items);
 
202
// Create list for using with tempory table
 
203
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
204
                                      List<Item> &new_list1,
 
205
                                      List<Item> &new_list2,
 
206
                                      uint elements, List<Item> &items);
 
207
static void init_tmptable_sum_functions(Item_sum **func);
 
208
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
209
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
210
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
211
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
212
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
213
static bool update_sum_func(Item_sum **func);
 
214
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
215
                            bool distinct, const char *message=NullS);
 
216
static Item *remove_additional_cond(Item* conds);
 
217
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
218
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
219
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
220
                                       Item *new_cond, bool fix_fields);
92
221
 
93
222
/*
94
223
  This is used to mark equalities that were made from i-th IN-equality.
98
227
const char *subq_sj_cond_name=
99
228
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
100
229
 
101
 
static bool copy_blobs(Field **ptr)
 
230
static bool bitmap_covers(const table_map x, const table_map y)
102
231
{
103
 
  for (; *ptr ; ptr++)
104
 
  {
105
 
    if ((*ptr)->flags & BLOB_FLAG)
106
 
      if (((Field_blob *) (*ptr))->copy())
107
 
        return 1;                               // Error
108
 
  }
109
 
  return 0;
 
232
  return !test(y & ~x);
110
233
}
111
234
 
112
235
/**
113
236
  This handles SELECT with and without UNION.
114
237
*/
115
 
bool handle_select(Session *session, LEX *lex, select_result *result,
116
 
                   uint64_t setup_tables_done_option)
 
238
 
 
239
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
240
                   ulong setup_tables_done_option)
117
241
{
118
242
  bool res;
119
 
  register Select_Lex *select_lex= &lex->select_lex;
120
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
243
  register SELECT_LEX *select_lex = &lex->select_lex;
 
244
  DRIZZLE_SELECT_START();
121
245
 
122
 
  if (select_lex->master_unit()->is_union() ||
 
246
  if (select_lex->master_unit()->is_union() || 
123
247
      select_lex->master_unit()->fake_select_lex)
124
 
  {
125
 
    res= drizzle_union(session, lex, result, &lex->unit,
126
 
                       setup_tables_done_option);
127
 
  }
 
248
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
128
249
  else
129
250
  {
130
 
    Select_Lex_Unit *unit= &lex->unit;
 
251
    SELECT_LEX_UNIT *unit= &lex->unit;
131
252
    unit->set_limit(unit->global_parameters);
132
 
    session->session_marker= 0;
 
253
    thd->thd_marker= 0;
133
254
    /*
134
255
      'options' of mysql_select will be set in JOIN, as far as JOIN for
135
 
      every PS/SP execution new, we will not need reset this flag if
 
256
      every PS/SP execution new, we will not need reset this flag if 
136
257
      setup_tables_done_option changed for next rexecution
137
258
    */
138
 
    res= mysql_select(session,
139
 
                      &select_lex->ref_pointer_array,
 
259
    res= mysql_select(thd, &select_lex->ref_pointer_array,
140
260
                      (TableList*) select_lex->table_list.first,
141
 
                      select_lex->with_wild,
142
 
                      select_lex->item_list,
 
261
                      select_lex->with_wild, select_lex->item_list,
143
262
                      select_lex->where,
144
263
                      select_lex->order_list.elements +
145
264
                      select_lex->group_list.elements,
146
 
                      (Order*) select_lex->order_list.first,
147
 
                      (Order*) select_lex->group_list.first,
 
265
                      (order_st*) select_lex->order_list.first,
 
266
                      (order_st*) select_lex->group_list.first,
148
267
                      select_lex->having,
149
 
                      select_lex->options | session->options |
 
268
                      (order_st*) lex->proc_list.first,
 
269
                      select_lex->options | thd->options |
150
270
                      setup_tables_done_option,
151
271
                      result, unit, select_lex);
152
272
  }
153
 
  res|= session->is_error();
 
273
  res|= thd->is_error();
154
274
  if (unlikely(res))
155
275
    result->abort();
156
276
 
157
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
158
 
  return res;
 
277
  DRIZZLE_SELECT_END();
 
278
  return(res);
159
279
}
160
280
 
 
281
 
161
282
/*
162
283
  Fix fields referenced from inner selects.
163
284
 
164
285
  SYNOPSIS
165
286
    fix_inner_refs()
166
 
    session               Thread handle
 
287
    thd               Thread handle
167
288
    all_fields        List of all fields used in select
168
289
    select            Current select
169
290
    ref_pointer_array Array of references to Items used in current select
198
319
    true  an error occured
199
320
    false ok
200
321
*/
201
 
bool fix_inner_refs(Session *session, 
202
 
                    List<Item> &all_fields, 
203
 
                    Select_Lex *select, 
204
 
                    Item **ref_pointer_array)
 
322
 
 
323
bool
 
324
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
325
                 Item **ref_pointer_array)
205
326
{
206
327
  Item_outer_ref *ref;
207
328
  bool res= false;
261
382
    ref->outer_ref= new_ref;
262
383
    ref->ref= &ref->outer_ref;
263
384
 
264
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
385
    if (!ref->fixed && ref->fix_fields(thd, 0))
265
386
      return true;
266
 
    session->used_tables|= item->used_tables();
 
387
    thd->used_tables|= item->used_tables();
267
388
  }
268
389
  return res;
269
390
}
270
391
 
 
392
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
393
/**
 
394
  Function to setup clauses without sum functions.
 
395
*/
 
396
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
397
                               TableList *tables,
 
398
                               TableList *leaves,
 
399
                               List<Item> &fields,
 
400
                               List<Item> &all_fields,
 
401
                               COND **conds,
 
402
                               order_st *order,
 
403
                               order_st *group, bool *hidden_group_fields)
 
404
{
 
405
  int res;
 
406
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
407
 
 
408
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
409
  res= setup_conds(thd, tables, leaves, conds);
 
410
 
 
411
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
412
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
413
                          order);
 
414
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
415
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
416
                          group, hidden_group_fields);
 
417
  thd->lex->allow_sum_func= save_allow_sum_func;
 
418
  return(res);
 
419
}
 
420
 
271
421
/*****************************************************************************
272
422
  Check fields, find best join, do the select and output fields.
273
423
  mysql_select assumes that all tables are already opened
274
424
*****************************************************************************/
275
425
 
 
426
/**
 
427
  Prepare of whole select (including sub queries in future).
 
428
 
 
429
  @todo
 
430
    Add check of calculation of GROUP functions and fields:
 
431
    SELECT COUNT(*)+table.col1 from table1;
 
432
 
 
433
  @retval
 
434
    -1   on error
 
435
  @retval
 
436
    0   on success
 
437
*/
 
438
int
 
439
JOIN::prepare(Item ***rref_pointer_array,
 
440
              TableList *tables_init,
 
441
              uint wild_num, COND *conds_init, uint og_num,
 
442
              order_st *order_init, order_st *group_init,
 
443
              Item *having_init,
 
444
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
445
              SELECT_LEX_UNIT *unit_arg)
 
446
{
 
447
  // to prevent double initialization on EXPLAIN
 
448
  if (optimized)
 
449
    return(0);
 
450
 
 
451
  conds= conds_init;
 
452
  order= order_init;
 
453
  group_list= group_init;
 
454
  having= having_init;
 
455
  proc_param= proc_param_init;
 
456
  tables_list= tables_init;
 
457
  select_lex= select_lex_arg;
 
458
  select_lex->join= this;
 
459
  join_list= &select_lex->top_join_list;
 
460
  union_part= unit_arg->is_union();
 
461
 
 
462
  thd->lex->current_select->is_item_list_lookup= 1;
 
463
  /*
 
464
    If we have already executed SELECT, then it have not sense to prevent
 
465
    its table from update (see unique_table())
 
466
  */
 
467
  if (thd->derived_tables_processing)
 
468
    select_lex->exclude_from_table_unique_test= true;
 
469
 
 
470
  /* Check that all tables, fields, conds and order are ok */
 
471
 
 
472
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
473
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
474
                                    tables_list, &select_lex->leaf_tables,
 
475
                                    false))
 
476
      return(-1);
 
477
 
 
478
  TableList *table_ptr;
 
479
  for (table_ptr= select_lex->leaf_tables;
 
480
       table_ptr;
 
481
       table_ptr= table_ptr->next_leaf)
 
482
    tables++;
 
483
 
 
484
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
485
      select_lex->setup_ref_array(thd, og_num) ||
 
486
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
487
                   &all_fields, 1) ||
 
488
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
489
                          select_lex->leaf_tables, fields_list,
 
490
                          all_fields, &conds, order, group_list,
 
491
                          &hidden_group_fields))
 
492
    return(-1);                         /* purecov: inspected */
 
493
 
 
494
  ref_pointer_array= *rref_pointer_array;
 
495
  
 
496
  if (having)
 
497
  {
 
498
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
499
    thd->where="having clause";
 
500
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
501
    select_lex->having_fix_field= 1;
 
502
    bool having_fix_rc= (!having->fixed &&
 
503
                         (having->fix_fields(thd, &having) ||
 
504
                          having->check_cols(1)));
 
505
    select_lex->having_fix_field= 0;
 
506
    if (having_fix_rc || thd->is_error())
 
507
      return(-1);                               /* purecov: inspected */
 
508
    thd->lex->allow_sum_func= save_allow_sum_func;
 
509
  }
 
510
 
 
511
  {
 
512
    Item_subselect *subselect;
 
513
    Item_in_subselect *in_subs= NULL;
 
514
    /*
 
515
      Are we in a subquery predicate?
 
516
      TODO: the block below will be executed for every PS execution without need.
 
517
    */
 
518
    if ((subselect= select_lex->master_unit()->item))
 
519
    {
 
520
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
521
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
522
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
523
        in_subs= (Item_in_subselect*)subselect;
 
524
 
 
525
      /*
 
526
        Check if we're in subquery that is a candidate for flattening into a
 
527
        semi-join (which is done done in flatten_subqueries()). The
 
528
        requirements are:
 
529
          1. Subquery predicate is an IN/=ANY subq predicate
 
530
          2. Subquery is a single SELECT (not a UNION)
 
531
          3. Subquery does not have GROUP BY or order_st BY
 
532
          4. Subquery does not use aggregate functions or HAVING
 
533
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
534
          6. No execution method was already chosen (by a prepared statement).
 
535
 
 
536
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
537
               doesn't have a JOIN (TODO: We should handle this at some
 
538
               point by switching to multi-table UPDATE/DELETE)
 
539
 
 
540
          (**). We're not in a confluent table-less subquery, like
 
541
                "SELECT 1". 
 
542
      */
 
543
      if (in_subs &&                                                    // 1
 
544
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
545
          !select_lex->group_list.elements && !order &&                 // 3
 
546
          !having && !select_lex->with_sum_func &&                      // 4
 
547
          thd->thd_marker &&                                            // 5
 
548
          select_lex->outer_select()->join &&                           // (*)
 
549
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
550
          do_semijoin &&
 
551
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
552
      {
 
553
        {
 
554
          if (!in_subs->left_expr->fixed &&
 
555
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
556
          {
 
557
            return(-1);
 
558
          }
 
559
          /*
 
560
            Check that the right part of the subselect contains no more than one
 
561
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
562
          */
 
563
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
564
             (select_lex->item_list.elements != 
 
565
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
566
          {
 
567
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
568
            return(-1);
 
569
          }
 
570
        }
 
571
 
 
572
        /* Register the subquery for further processing */
 
573
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
574
        in_subs->expr_join_nest= (TableList*)thd->thd_marker;
 
575
      }
 
576
      else
 
577
      {
 
578
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
579
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
580
        /*
 
581
          Check if the subquery predicate can be executed via materialization.
 
582
          The required conditions are:
 
583
          1. Subquery predicate is an IN/=ANY subq predicate
 
584
          2. Subquery is a single SELECT (not a UNION)
 
585
          3. Subquery is not a table-less query. In this case there is no
 
586
             point in materializing.
 
587
          4. Subquery predicate is a top-level predicate
 
588
             (this implies it is not negated)
 
589
             TODO: this is a limitation that should be lifeted once we
 
590
             implement correct NULL semantics (WL#3830)
 
591
          5. Subquery is non-correlated
 
592
             TODO:
 
593
             This is an overly restrictive condition. It can be extended to:
 
594
             (Subquery is non-correlated ||
 
595
              Subquery is correlated to any query outer to IN predicate ||
 
596
              (Subquery is correlated to the immediate outer query &&
 
597
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
598
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
599
          6. No execution method was already chosen (by a prepared statement).
 
600
 
 
601
          (*) The subquery must be part of a SELECT statement. The current
 
602
               condition also excludes multi-table update statements.
 
603
 
 
604
          We have to determine whether we will perform subquery materialization
 
605
          before calling the IN=>EXISTS transformation, so that we know whether to
 
606
          perform the whole transformation or only that part of it which wraps
 
607
          Item_in_subselect in an Item_in_optimizer.
 
608
        */
 
609
        if (do_materialize && 
 
610
            in_subs  &&                                                   // 1
 
611
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
612
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
613
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
614
        {
 
615
          if (in_subs->is_top_level_item() &&                             // 4
 
616
              !in_subs->is_correlated &&                                  // 5
 
617
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
618
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
619
        }
 
620
 
 
621
        Item_subselect::trans_res trans_res;
 
622
        if ((trans_res= subselect->select_transformer(this)) !=
 
623
            Item_subselect::RES_OK)
 
624
        {
 
625
          return((trans_res == Item_subselect::RES_ERROR));
 
626
        }
 
627
      }
 
628
    }
 
629
  }
 
630
 
 
631
  if (order)
 
632
  {
 
633
    order_st *ord;
 
634
    for (ord= order; ord; ord= ord->next)
 
635
    {
 
636
      Item *item= *ord->item;
 
637
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
638
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
639
    }
 
640
  }
 
641
 
 
642
  if (having && having->with_sum_func)
 
643
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
644
                            &having, true);
 
645
  if (select_lex->inner_sum_func_list)
 
646
  {
 
647
    Item_sum *end=select_lex->inner_sum_func_list;
 
648
    Item_sum *item_sum= end;  
 
649
    do
 
650
    { 
 
651
      item_sum= item_sum->next;
 
652
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
653
                                all_fields, item_sum->ref_by, false);
 
654
    } while (item_sum != end);
 
655
  }
 
656
 
 
657
  if (select_lex->inner_refs_list.elements &&
 
658
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
659
    return(-1);
 
660
 
 
661
  /*
 
662
    Check if there are references to un-aggregated columns when computing 
 
663
    aggregate functions with implicit grouping (there is no GROUP BY).
 
664
 
 
665
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
666
  */
 
667
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
668
  {
 
669
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
670
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
671
    return(-1);
 
672
  }
 
673
  {
 
674
    /* Caclulate the number of groups */
 
675
    send_group_parts= 0;
 
676
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
677
      send_group_parts++;
 
678
  }
 
679
  
 
680
  if (error)
 
681
    goto err;                                   /* purecov: inspected */
 
682
 
 
683
  if (result && result->prepare(fields_list, unit_arg))
 
684
    goto err;                                   /* purecov: inspected */
 
685
 
 
686
  /* Init join struct */
 
687
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
688
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
689
  this->group= group_list != 0;
 
690
  unit= unit_arg;
 
691
 
 
692
#ifdef RESTRICTED_GROUP
 
693
  if (sum_func_count && !group_list && (func_count || field_count))
 
694
  {
 
695
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
696
    goto err;
 
697
  }
 
698
#endif
 
699
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
700
    goto err;
 
701
  if (alloc_func_list())
 
702
    goto err;
 
703
 
 
704
  return(0); // All OK
 
705
 
 
706
err:
 
707
  return(-1);                           /* purecov: inspected */
 
708
}
 
709
 
 
710
 
 
711
/*
 
712
  Remove the predicates pushed down into the subquery
 
713
 
 
714
  SYNOPSIS
 
715
    JOIN::remove_subq_pushed_predicates()
 
716
      where   IN  Must be NULL
 
717
              OUT The remaining WHERE condition, or NULL
 
718
 
 
719
  DESCRIPTION
 
720
    Given that this join will be executed using (unique|index)_subquery,
 
721
    without "checking NULL", remove the predicates that were pushed down
 
722
    into the subquery.
 
723
 
 
724
    If the subquery compares scalar values, we can remove the condition that
 
725
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
726
    engine)
 
727
 
 
728
    If the subquery compares row values, we need to keep the wrapped
 
729
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
730
    and non-NULL values, we'll do a full table scan and will rely on the
 
731
    equalities corresponding to non-NULL parts of left tuple to filter out
 
732
    non-matching records.
 
733
 
 
734
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
735
    fact that subquery engine will be using index lookup. This must be done only
 
736
    for cases where there are no conversion errors of significance, e.g. 257
 
737
    that is searched in a byte. But this requires homogenization of the return 
 
738
    codes of all Field*::store() methods.
 
739
*/
 
740
 
 
741
void JOIN::remove_subq_pushed_predicates(Item **where)
 
742
{
 
743
  if (conds->type() == Item::FUNC_ITEM &&
 
744
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
745
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
746
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
747
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
748
                   ((Item_func *)conds)->arguments()[0]))
 
749
  {
 
750
    *where= 0;
 
751
    return;
 
752
  }
 
753
}
 
754
 
 
755
 
276
756
/*
277
757
  Index lookup-based subquery: save some flags for EXPLAIN output
278
758
 
285
765
  DESCRIPTION
286
766
    For index lookup-based subquery (i.e. one executed with
287
767
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
288
 
    check its EXPLAIN output row should contain
289
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
768
    check its EXPLAIN output row should contain 
 
769
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
290
770
      "Using Where" (TAB_INFO_USING_WHERE)
291
771
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
292
772
    and set appropriate flags in join_tab->packed_info.
293
773
*/
294
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
774
 
 
775
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
295
776
{
296
777
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
297
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
778
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
298
779
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
299
780
  if (where)
300
781
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
301
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
782
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
302
783
  {
303
784
    if (join_tab->ref.cond_guards[i])
304
785
    {
308
789
  }
309
790
}
310
791
 
 
792
 
 
793
 
 
794
 
 
795
/*
 
796
  Check if the table's rowid is included in the temptable
 
797
 
 
798
  SYNOPSIS
 
799
    sj_table_is_included()
 
800
      join      The join
 
801
      join_tab  The table to be checked
 
802
 
 
803
  DESCRIPTION
 
804
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
805
    in the temptable. This is so if
 
806
 
 
807
    1. The table is not embedded within some semi-join nest
 
808
    2. The has been pulled out of a semi-join nest, or
 
809
 
 
810
    3. The table is functionally dependent on some previous table
 
811
 
 
812
    [4. This is also true for constant tables that can't be
 
813
        NULL-complemented but this function is not called for such tables]
 
814
 
 
815
  RETURN
 
816
    true  - Include table's rowid
 
817
    false - Don't
 
818
*/
 
819
 
 
820
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
821
{
 
822
  if (join_tab->emb_sj_nest)
 
823
    return false;
 
824
  
 
825
  /* Check if this table is functionally dependent on the tables that
 
826
     are within the same outer join nest
 
827
  */
 
828
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
829
  if (join_tab->type == JT_EQ_REF)
 
830
  {
 
831
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
832
    uint idx;
 
833
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
834
    {
 
835
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
836
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
837
        return true;
 
838
    }
 
839
    /* Ok, functionally dependent */
 
840
    return false;
 
841
  }
 
842
  /* Not functionally dependent => need to include*/
 
843
  return true;
 
844
}
 
845
 
 
846
 
 
847
/*
 
848
  Setup the strategies to eliminate semi-join duplicates.
 
849
  
 
850
  SYNOPSIS
 
851
    setup_semijoin_dups_elimination()
 
852
      join           Join to process
 
853
      options        Join options (needed to see if join buffering will be 
 
854
                     used or not)
 
855
      no_jbuf_after  Another bit of information re where join buffering will
 
856
                     be used.
 
857
 
 
858
  DESCRIPTION
 
859
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
860
    strategies:
 
861
 
 
862
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
863
                         of row combinations)
 
864
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
865
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
866
                  together and picking the 1st one)
 
867
    
 
868
    The join order has "duplicate-generating ranges", and every range is
 
869
    served by one strategy or a combination of FirstMatch with with some
 
870
    other strategy.
 
871
    
 
872
    "Duplicate-generating range" is defined as a range within the join order
 
873
    that contains all of the inner tables of a semi-join. All ranges must be
 
874
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
875
    are joined together, which is equivalent to converting
 
876
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
877
    to
 
878
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
879
    .
 
880
 
 
881
    Applicability conditions are as follows:
 
882
 
 
883
    DuplicateWeedout strategy
 
884
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
885
 
 
886
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
887
      +------+  +=========================+  +---+
 
888
        (1)                 (2)               (3)
 
889
 
 
890
       (1) - Prefix of OuterTables (those that participate in 
 
891
             IN-equality and/or are correlated with subquery) and outer 
 
892
             Noncorrelated Tables.
 
893
       (2) - The handled range. The range starts with the first sj-inner
 
894
             table, and covers all sj-inner and outer tables 
 
895
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
896
             may follow in any order.
 
897
       (3) - The suffix of outer Noncorrelated tables.
 
898
    
 
899
    FirstMatch strategy
 
900
    ~~~~~~~~~~~~~~~~~~~
 
901
 
 
902
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
903
      +------+  +==================+  +---+
 
904
        (1)             (2)          (3)
 
905
 
 
906
      (1) - Prefix of outer and non-correlated tables
 
907
      (2) - The handled range, which may contain only inner and
 
908
            non-correlated tables.
 
909
      (3) - The suffix of outer Noncorrelated tables.
 
910
 
 
911
    InsideOut strategy 
 
912
    ~~~~~~~~~~~~~~~~~~
 
913
 
 
914
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
915
     +--------+   +===========+ +=============+   +------+
 
916
        (1)           (2)          (3)              (4)
 
917
     
 
918
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
919
            all the non-trivially correlated outer tables. (non-trivially means
 
920
            that the correlation is not just through the IN-equality).
 
921
      
 
922
      (2) - Inner table for which the InsideOut scan is performed.
 
923
 
 
924
      (3) - The remainder of the duplicate-generating range. It is served by 
 
925
            application of FirstMatch strategy, with the exception that
 
926
            outer IN-correlated tables are considered to be non-correlated.
 
927
 
 
928
      (4) - THe suffix of outer and outer non-correlated tables.
 
929
 
 
930
    If several strategies are applicable, their relative priorities are:
 
931
      1. InsideOut
 
932
      2. FirstMatch 
 
933
      3. DuplicateWeedout
 
934
 
 
935
    This function walks over the join order and sets up the strategies by
 
936
    setting appropriate members in join_tab structures.
 
937
 
 
938
  RETURN
 
939
    false  OK 
 
940
    true   Out of memory error
 
941
*/
 
942
 
 
943
static
 
944
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
 
945
{
 
946
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
947
  struct {
 
948
    /* 
 
949
      0 - invalid (EOF marker), 
 
950
      1 - InsideOut, 
 
951
      2 - Temptable (maybe confluent),
 
952
      3 - Temptable with join buffering
 
953
    */
 
954
    uint strategy;
 
955
    uint start_idx; /* Left range bound */
 
956
    uint end_idx;   /* Right range bound */
 
957
    /* 
 
958
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
959
      all involved join nests.
 
960
    */
 
961
    table_map outer_tables;
 
962
  } dups_ranges [MAX_TABLES];
 
963
 
 
964
  TableList *emb_insideout_nest= NULL;
 
965
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
966
                               tables) whose ranges we're in */
 
967
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
968
  table_map range_start_map= 0; /* table_map at current range start */
 
969
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
970
  int cur_range= 0;
 
971
  uint i;
 
972
 
 
973
  /*
 
974
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
975
  */
 
976
  for (i=join->const_tables ; i < join->tables ; i++)
 
977
  {
 
978
    JOIN_TAB *tab=join->join_tab+i;
 
979
    Table *table=tab->table;
 
980
    cur_map |= table->map;
 
981
 
 
982
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
983
    {
 
984
      if (!emb_sj_map)
 
985
      {
 
986
        dups_ranges[cur_range].start_idx= i;
 
987
        range_start_map= cur_map & ~table->map;
 
988
        /*
 
989
          Remember if this is a possible start of range that is covered by
 
990
          the InsideOut strategy (the reason that it is not covered could
 
991
          be that it overlaps with anther semi-join's range. we don't
 
992
          support InsideOut for joined ranges)
 
993
        */
 
994
        if (join->best_positions[i].use_insideout_scan)
 
995
          emb_insideout_nest= tab->emb_sj_nest;
 
996
      }
 
997
 
 
998
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
999
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1000
 
 
1001
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1002
      {
 
1003
        /*
 
1004
          Two different semi-joins interleave. This cannot be handled by
 
1005
          InsideOut strategy.
 
1006
        */
 
1007
        emb_insideout_nest= NULL;
 
1008
      }
 
1009
    }
 
1010
 
 
1011
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1012
    {
 
1013
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1014
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1015
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1016
      {
 
1017
        /*
 
1018
          This table uses join buffering, which makes use of FirstMatch or 
 
1019
          InsideOut strategies impossible for the current and (we assume) 
 
1020
          preceding duplicate-producing ranges.
 
1021
          That is, for the join order:
 
1022
 
 
1023
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1024
                  |     |     |     |          | \
 
1025
                  +-----+     +-----+          |  join buffering use
 
1026
                     r1          r2         we're here
 
1027
 
 
1028
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1029
          strategy that spans all the tables, starting from the very 1st
 
1030
          one.
 
1031
        */
 
1032
        dealing_with_jbuf= true;
 
1033
        emb_insideout_nest= false;
 
1034
 
 
1035
        /* 
 
1036
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1037
          do not matter: 
 
1038
        */
 
1039
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1040
        {
 
1041
          dups_ranges[cur_range].outer_tables |= 
 
1042
            dups_ranges[prev_range].outer_tables;
 
1043
        }
 
1044
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1045
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1046
        cur_range=  0;
 
1047
      }
 
1048
 
 
1049
      /*
 
1050
        Check if we are at the end of duplicate-producing range. We are if
 
1051
 
 
1052
        1. It's an InsideOut range (which presumes all correlated tables are
 
1053
           in the prefix), and all inner tables are in the join order prefix,
 
1054
           or
 
1055
        2. It's a DuplicateElimination range (possibly covering several
 
1056
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1057
           sj-nests are in the join order prefix.
 
1058
      */
 
1059
      bool end_of_range= false;
 
1060
      if (emb_insideout_nest && 
 
1061
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1062
      {
 
1063
        /* Save that this range is handled with InsideOut: */
 
1064
        dups_ranges[cur_range].strategy= 1;
 
1065
        end_of_range= true;
 
1066
      }
 
1067
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1068
      {
 
1069
        /*
 
1070
          This is a complete range to be handled with either DuplicateWeedout 
 
1071
          or FirstMatch
 
1072
        */
 
1073
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1074
        /* 
 
1075
          This will hold tables from within the range that need to be put 
 
1076
          into the join buffer before we can use the FirstMatch on its tail.
 
1077
        */
 
1078
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1079
                                             ~range_start_map;
 
1080
        end_of_range= true;
 
1081
      }
 
1082
 
 
1083
      if (end_of_range)
 
1084
      {
 
1085
        dups_ranges[cur_range].end_idx= i+1;
 
1086
        emb_sj_map= emb_outer_tables= 0;
 
1087
        emb_insideout_nest= NULL;
 
1088
        dealing_with_jbuf= false;
 
1089
        dups_ranges[++cur_range].strategy= 0;
 
1090
      }
 
1091
    }
 
1092
  }
 
1093
 
 
1094
  THD *thd= join->thd;
 
1095
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1096
  /*
 
1097
    Second pass: setup the chosen strategies    
 
1098
  */
 
1099
  for (int j= 0; j < cur_range; j++)
 
1100
  {
 
1101
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1102
    JOIN_TAB *jump_to;
 
1103
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1104
    {
 
1105
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1106
      jump_to= tab++;
 
1107
    }
 
1108
    else // DuplicateWeedout strategy
 
1109
    {
 
1110
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1111
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1112
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1113
      uint jt_null_bits= 0;    // # null bits in tuple bytes
 
1114
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1115
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1116
      JOIN_TAB *last_outer_tab= tab - 1;
 
1117
      /*
 
1118
        Walk through the range and remember
 
1119
         - tables that need their rowids to be put into temptable
 
1120
         - the last outer table
 
1121
      */
 
1122
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1123
      {
 
1124
        if (sj_table_is_included(join, tab))
 
1125
        {
 
1126
          last_tab->join_tab= tab;
 
1127
          last_tab->rowid_offset= jt_rowid_offset;
 
1128
          jt_rowid_offset += tab->table->file->ref_length;
 
1129
          if (tab->table->maybe_null)
 
1130
          {
 
1131
            last_tab->null_byte= jt_null_bits / 8;
 
1132
            last_tab->null_bit= jt_null_bits++;
 
1133
          }
 
1134
          last_tab++;
 
1135
          tab->table->prepare_for_position();
 
1136
          tab->rowid_keep_flags= rowid_keep_flags;
 
1137
        }
 
1138
        cur_map |= tab->table->map;
 
1139
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1140
                                               dups_ranges[j].outer_tables))
 
1141
          last_outer_tab= tab;
 
1142
      }
 
1143
 
 
1144
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1145
      {
 
1146
        SJ_TMP_TABLE *sjtbl;
 
1147
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1148
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1149
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1150
          return(true);
 
1151
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1152
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1153
        sjtbl->rowid_len= jt_rowid_offset;
 
1154
        sjtbl->null_bits= jt_null_bits;
 
1155
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1156
 
 
1157
        *next_sjtbl_ptr= sjtbl;
 
1158
        next_sjtbl_ptr= &(sjtbl->next);
 
1159
        sjtbl->next= NULL;
 
1160
 
 
1161
        sjtbl->tmp_table= 
 
1162
          create_duplicate_weedout_tmp_table(thd, 
 
1163
                                             sjtbl->rowid_len + 
 
1164
                                             sjtbl->null_bytes,
 
1165
                                             sjtbl);
 
1166
 
 
1167
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1168
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1169
      }
 
1170
      tab= last_outer_tab + 1;
 
1171
      jump_to= last_outer_tab;
 
1172
    }
 
1173
 
 
1174
    /* Create the FirstMatch tail */
 
1175
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1176
    {
 
1177
      if (tab->emb_sj_nest)
 
1178
        tab->do_firstmatch= jump_to; 
 
1179
      else
 
1180
        jump_to= tab;
 
1181
    }
 
1182
  }
 
1183
  return(false);
 
1184
}
 
1185
 
 
1186
 
 
1187
static void cleanup_sj_tmp_tables(JOIN *join)
 
1188
{
 
1189
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1190
       sj_tbl= sj_tbl->next)
 
1191
  {
 
1192
    if (sj_tbl->tmp_table)
 
1193
    {
 
1194
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1195
    }
 
1196
  }
 
1197
  join->sj_tmp_tables= NULL;
 
1198
}
 
1199
 
 
1200
uint make_join_orderinfo(JOIN *join);
 
1201
 
 
1202
/**
 
1203
  global select optimisation.
 
1204
 
 
1205
  @note
 
1206
    error code saved in field 'error'
 
1207
 
 
1208
  @retval
 
1209
    0   success
 
1210
  @retval
 
1211
    1   error
 
1212
*/
 
1213
 
 
1214
int
 
1215
JOIN::optimize()
 
1216
{
 
1217
  // to prevent double initialization on EXPLAIN
 
1218
  if (optimized)
 
1219
    return(0);
 
1220
  optimized= 1;
 
1221
 
 
1222
  thd_proc_info(thd, "optimizing");
 
1223
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1224
              unit->select_limit_cnt);
 
1225
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1226
  select_limit= unit->select_limit_cnt;
 
1227
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1228
    select_limit= HA_POS_ERROR;
 
1229
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1230
  // Ignore errors of execution if option IGNORE present
 
1231
  if (thd->lex->ignore)
 
1232
    thd->lex->current_select->no_error= 1;
 
1233
 
 
1234
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1235
  /* Add HAVING to WHERE if possible */
 
1236
  if (having && !group_list && !sum_func_count)
 
1237
  {
 
1238
    if (!conds)
 
1239
    {
 
1240
      conds= having;
 
1241
      having= 0;
 
1242
    }
 
1243
    else if ((conds=new Item_cond_and(conds,having)))
 
1244
    {
 
1245
      /*
 
1246
        Item_cond_and can't be fixed after creation, so we do not check
 
1247
        conds->fixed
 
1248
      */
 
1249
      conds->fix_fields(thd, &conds);
 
1250
      conds->change_ref_to_fields(thd, tables_list);
 
1251
      conds->top_level_item();
 
1252
      having= 0;
 
1253
    }
 
1254
  }
 
1255
#endif
 
1256
  SELECT_LEX *sel= thd->lex->current_select;
 
1257
  if (sel->first_cond_optimization)
 
1258
  {
 
1259
    /*
 
1260
      The following code will allocate the new items in a permanent
 
1261
      MEMROOT for prepared statements and stored procedures.
 
1262
    */
 
1263
    sel->first_cond_optimization= 0;
 
1264
 
 
1265
    /* Convert all outer joins to inner joins if possible */
 
1266
    conds= simplify_joins(this, join_list, conds, true, false);
 
1267
    build_bitmap_for_nested_joins(join_list, 0);
 
1268
  }
 
1269
 
 
1270
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1271
  if (thd->is_error())
 
1272
  {
 
1273
    error= 1;
 
1274
    return(1);
 
1275
  }
 
1276
 
 
1277
  {
 
1278
    having= optimize_cond(this, having, join_list, &having_value);
 
1279
    if (thd->is_error())
 
1280
    {
 
1281
      error= 1;
 
1282
      return(1);
 
1283
    }
 
1284
    if (select_lex->where)
 
1285
      select_lex->cond_value= cond_value;
 
1286
    if (select_lex->having)
 
1287
      select_lex->having_value= having_value;
 
1288
 
 
1289
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1290
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1291
    {                                           /* Impossible cond */
 
1292
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1293
                           "Impossible HAVING" : "Impossible WHERE";
 
1294
      error= 0;
 
1295
      return(0);
 
1296
    }
 
1297
  }
 
1298
 
 
1299
  /* Optimize count(*), min() and max() */
 
1300
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1301
  {
 
1302
    int res;
 
1303
    /*
 
1304
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1305
      to the WHERE conditions,
 
1306
      or 1 if all items were resolved,
 
1307
      or 0, or an error number HA_ERR_...
 
1308
    */
 
1309
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1310
    {
 
1311
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1312
      {
 
1313
        zero_result_cause= "No matching min/max row";
 
1314
        error=0;
 
1315
        return(0);
 
1316
      }
 
1317
      if (res > 1)
 
1318
      {
 
1319
        error= res;
 
1320
        return(1);
 
1321
      }
 
1322
      if (res < 0)
 
1323
      {
 
1324
        zero_result_cause= "No matching min/max row";
 
1325
        error=0;
 
1326
        return(0);
 
1327
      }
 
1328
      zero_result_cause= "Select tables optimized away";
 
1329
      tables_list= 0;                           // All tables resolved
 
1330
      /*
 
1331
        Extract all table-independent conditions and replace the WHERE
 
1332
        clause with them. All other conditions were computed by opt_sum_query
 
1333
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1334
        so there is no need to compute the whole WHERE clause again.
 
1335
        Notice that make_cond_for_table() will always succeed to remove all
 
1336
        computed conditions, because opt_sum_query() is applicable only to
 
1337
        conjunctions.
 
1338
        Preserve conditions for EXPLAIN.
 
1339
      */
 
1340
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1341
      {
 
1342
        COND *table_independent_conds=
 
1343
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1344
        conds= table_independent_conds;
 
1345
      }
 
1346
    }
 
1347
  }
 
1348
  if (!tables_list)
 
1349
  {
 
1350
    error= 0;
 
1351
    return(0);
 
1352
  }
 
1353
  error= -1;                                    // Error is sent to client
 
1354
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1355
 
 
1356
  /* Calculate how to do the join */
 
1357
  thd_proc_info(thd, "statistics");
 
1358
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1359
      thd->is_fatal_error)
 
1360
  {
 
1361
    return(1);
 
1362
  }
 
1363
 
 
1364
  /* Remove distinct if only const tables */
 
1365
  select_distinct= select_distinct && (const_tables != tables);
 
1366
  thd_proc_info(thd, "preparing");
 
1367
  if (result->initialize_tables(this))
 
1368
  {
 
1369
    return(1);                          // error == -1
 
1370
  }
 
1371
  if (const_table_map != found_const_table_map &&
 
1372
      !(select_options & SELECT_DESCRIBE) &&
 
1373
      (!conds ||
 
1374
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1375
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1376
  {
 
1377
    zero_result_cause= "no matching row in const table";
 
1378
    error= 0;
 
1379
    return(0);
 
1380
  }
 
1381
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1382
      best_read > (double) thd->variables.max_join_size &&
 
1383
      !(select_options & SELECT_DESCRIBE))
 
1384
  {                                             /* purecov: inspected */
 
1385
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1386
    error= -1;
 
1387
    return(1);
 
1388
  }
 
1389
  if (const_tables && !thd->locked_tables &&
 
1390
      !(select_options & SELECT_NO_UNLOCK))
 
1391
    mysql_unlock_some_tables(thd, table, const_tables);
 
1392
  if (!conds && outer_join)
 
1393
  {
 
1394
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1395
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1396
  }
 
1397
  select= make_select(*table, const_table_map,
 
1398
                      const_table_map, conds, 1, &error);
 
1399
  if (error)
 
1400
  {                                             /* purecov: inspected */
 
1401
    error= -1;                                  /* purecov: inspected */
 
1402
    return(1);
 
1403
  }
 
1404
  
 
1405
  reset_nj_counters(join_list);
 
1406
  make_outerjoin_info(this);
 
1407
 
 
1408
  /*
 
1409
    Among the equal fields belonging to the same multiple equality
 
1410
    choose the one that is to be retrieved first and substitute
 
1411
    all references to these in where condition for a reference for
 
1412
    the selected field.
 
1413
  */
 
1414
  if (conds)
 
1415
  {
 
1416
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1417
    conds->update_used_tables();
 
1418
  }
 
1419
 
 
1420
  /*
 
1421
    Permorm the the optimization on fields evaluation mentioned above
 
1422
    for all on expressions.
 
1423
  */ 
 
1424
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1425
  {
 
1426
    if (*tab->on_expr_ref)
 
1427
    {
 
1428
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1429
                                                         tab->cond_equal,
 
1430
                                                         map2table);
 
1431
      (*tab->on_expr_ref)->update_used_tables();
 
1432
    }
 
1433
  }
 
1434
 
 
1435
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1436
      (select_options & SELECT_DESCRIBE) &&
 
1437
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1438
  {
 
1439
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1440
  }
 
1441
  if (make_join_select(this, select, conds))
 
1442
  {
 
1443
    zero_result_cause=
 
1444
      "Impossible WHERE noticed after reading const tables";
 
1445
    return(0);                          // error == 0
 
1446
  }
 
1447
 
 
1448
  error= -1;                                    /* if goto err */
 
1449
 
 
1450
  /* Optimize distinct away if possible */
 
1451
  {
 
1452
    order_st *org_order= order;
 
1453
    order=remove_const(this, order,conds,1, &simple_order);
 
1454
    if (thd->is_error())
 
1455
    {
 
1456
      error= 1;
 
1457
      return(1);
 
1458
    }
 
1459
 
 
1460
    /*
 
1461
      If we are using order_st BY NULL or order_st BY const_expression,
 
1462
      return result in any order (even if we are using a GROUP BY)
 
1463
    */
 
1464
    if (!order && org_order)
 
1465
      skip_sort_order= 1;
 
1466
  }
 
1467
  /*
 
1468
     Check if we can optimize away GROUP BY/DISTINCT.
 
1469
     We can do that if there are no aggregate functions, the
 
1470
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1471
     (if present) contain direct references to all key parts of
 
1472
     an unique index (in whatever order) and if the key parts of the
 
1473
     unique index cannot contain NULLs.
 
1474
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1475
     be the same (as long as they are unique).
 
1476
 
 
1477
     The FROM clause must contain a single non-constant table.
 
1478
  */
 
1479
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1480
      !tmp_table_param.sum_func_count &&
 
1481
      (!join_tab[const_tables].select ||
 
1482
       !join_tab[const_tables].select->quick ||
 
1483
       join_tab[const_tables].select->quick->get_type() != 
 
1484
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1485
  {
 
1486
    if (group_list &&
 
1487
       list_contains_unique_index(join_tab[const_tables].table,
 
1488
                                 find_field_in_order_list,
 
1489
                                 (void *) group_list))
 
1490
    {
 
1491
      /*
 
1492
        We have found that grouping can be removed since groups correspond to
 
1493
        only one row anyway, but we still have to guarantee correct result
 
1494
        order. The line below effectively rewrites the query from GROUP BY
 
1495
        <fields> to order_st BY <fields>. There are two exceptions:
 
1496
        - if skip_sort_order is set (see above), then we can simply skip
 
1497
          GROUP BY;
 
1498
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1499
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1500
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1501
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1502
          order_st BY fields.
 
1503
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1504
          'order' as is.
 
1505
       */
 
1506
      if (!order || test_if_subpart(group_list, order))
 
1507
          order= skip_sort_order ? 0 : group_list;
 
1508
      /*
 
1509
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1510
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1511
      */
 
1512
      join_tab->table->keys_in_use_for_order_by=
 
1513
        join_tab->table->keys_in_use_for_group_by;
 
1514
      group_list= 0;
 
1515
      group= 0;
 
1516
    }
 
1517
    if (select_distinct &&
 
1518
       list_contains_unique_index(join_tab[const_tables].table,
 
1519
                                 find_field_in_item_list,
 
1520
                                 (void *) &fields_list))
 
1521
    {
 
1522
      select_distinct= 0;
 
1523
    }
 
1524
  }
 
1525
  if (group_list || tmp_table_param.sum_func_count)
 
1526
  {
 
1527
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1528
      select_distinct=0;
 
1529
  }
 
1530
  else if (select_distinct && tables - const_tables == 1)
 
1531
  {
 
1532
    /*
 
1533
      We are only using one table. In this case we change DISTINCT to a
 
1534
      GROUP BY query if:
 
1535
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1536
        BY only uses selected fields.
 
1537
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1538
      - We are scanning the whole table without LIMIT
 
1539
        This can happen if:
 
1540
        - We are using CALC_FOUND_ROWS
 
1541
        - We are using an order_st BY that can't be optimized away.
 
1542
 
 
1543
      We don't want to use this optimization when we are using LIMIT
 
1544
      because in this case we can just create a temporary table that
 
1545
      holds LIMIT rows and stop when this table is full.
 
1546
    */
 
1547
    JOIN_TAB *tab= &join_tab[const_tables];
 
1548
    bool all_order_fields_used;
 
1549
    if (order)
 
1550
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1551
        &tab->table->keys_in_use_for_order_by);
 
1552
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1553
                                          order, fields_list, all_fields,
 
1554
                                          &all_order_fields_used)))
 
1555
    {
 
1556
      bool skip_group= (skip_sort_order &&
 
1557
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1558
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1559
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1560
      if ((skip_group && all_order_fields_used) ||
 
1561
          select_limit == HA_POS_ERROR ||
 
1562
          (order && !skip_sort_order))
 
1563
      {
 
1564
        /*  Change DISTINCT to GROUP BY */
 
1565
        select_distinct= 0;
 
1566
        no_order= !order;
 
1567
        if (all_order_fields_used)
 
1568
        {
 
1569
          if (order && skip_sort_order)
 
1570
          {
 
1571
            /*
 
1572
              Force MySQL to read the table in sorted order to get result in
 
1573
              order_st BY order.
 
1574
            */
 
1575
            tmp_table_param.quick_group=0;
 
1576
          }
 
1577
          order=0;
 
1578
        }
 
1579
        group=1;                                // For end_write_group
 
1580
      }
 
1581
      else
 
1582
        group_list= 0;
 
1583
    }
 
1584
    else if (thd->is_fatal_error)                       // End of memory
 
1585
      return(1);
 
1586
  }
 
1587
  simple_group= 0;
 
1588
  {
 
1589
    order_st *old_group_list;
 
1590
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1591
                             rollup.state == ROLLUP::STATE_NONE,
 
1592
                             &simple_group);
 
1593
    if (thd->is_error())
 
1594
    {
 
1595
      error= 1;
 
1596
      return(1);
 
1597
    }
 
1598
    if (old_group_list && !group_list)
 
1599
      select_distinct= 0;
 
1600
  }
 
1601
  if (!group_list && group)
 
1602
  {
 
1603
    order=0;                                    // The output has only one row
 
1604
    simple_order=1;
 
1605
    select_distinct= 0;                       // No need in distinct for 1 row
 
1606
    group_optimized_away= 1;
 
1607
  }
 
1608
 
 
1609
  calc_group_buffer(this, group_list);
 
1610
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1611
 
 
1612
  if (test_if_subpart(group_list, order) ||
 
1613
      (!group_list && tmp_table_param.sum_func_count))
 
1614
    order=0;
 
1615
 
 
1616
  // Can't use sort on head table if using row cache
 
1617
  if (full_join)
 
1618
  {
 
1619
    if (group_list)
 
1620
      simple_group=0;
 
1621
    if (order)
 
1622
      simple_order=0;
 
1623
  }
 
1624
 
 
1625
  /*
 
1626
    Check if we need to create a temporary table.
 
1627
    This has to be done if all tables are not already read (const tables)
 
1628
    and one of the following conditions holds:
 
1629
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1630
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1631
    - We are using different order_st BY and GROUP BY orders
 
1632
    - The user wants us to buffer the result.
 
1633
  */
 
1634
  need_tmp= (const_tables != tables &&
 
1635
             ((select_distinct || !simple_order || !simple_group) ||
 
1636
              (group_list && order) ||
 
1637
              test(select_options & OPTION_BUFFER_RESULT)));
 
1638
 
 
1639
  uint no_jbuf_after= make_join_orderinfo(this);
 
1640
  uint64_t select_opts_for_readinfo= 
 
1641
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1642
 
 
1643
  sj_tmp_tables= NULL;
 
1644
  if (!select_lex->sj_nests.is_empty())
 
1645
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1646
                                    no_jbuf_after);
 
1647
 
 
1648
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1649
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1650
    return(1);
 
1651
 
 
1652
  /* Create all structures needed for materialized subquery execution. */
 
1653
  if (setup_subquery_materialization())
 
1654
    return(1);
 
1655
 
 
1656
  /*
 
1657
    is this simple IN subquery?
 
1658
  */
 
1659
  if (!group_list && !order &&
 
1660
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1661
      tables == 1 && conds &&
 
1662
      !unit->is_union())
 
1663
  {
 
1664
    if (!having)
 
1665
    {
 
1666
      Item *where= conds;
 
1667
      if (join_tab[0].type == JT_EQ_REF &&
 
1668
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1669
      {
 
1670
        remove_subq_pushed_predicates(&where);
 
1671
        save_index_subquery_explain_info(join_tab, where);
 
1672
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1673
        error= 0;
 
1674
        return(unit->item->
 
1675
                    change_engine(new
 
1676
                                  subselect_uniquesubquery_engine(thd,
 
1677
                                                                  join_tab,
 
1678
                                                                  unit->item,
 
1679
                                                                  where)));
 
1680
      }
 
1681
      else if (join_tab[0].type == JT_REF &&
 
1682
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1683
      {
 
1684
        remove_subq_pushed_predicates(&where);
 
1685
        save_index_subquery_explain_info(join_tab, where);
 
1686
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1687
        error= 0;
 
1688
        return(unit->item->
 
1689
                    change_engine(new
 
1690
                                  subselect_indexsubquery_engine(thd,
 
1691
                                                                 join_tab,
 
1692
                                                                 unit->item,
 
1693
                                                                 where,
 
1694
                                                                 NULL,
 
1695
                                                                 0)));
 
1696
      }
 
1697
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1698
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1699
               having->name == in_having_cond)
 
1700
    {
 
1701
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1702
      error= 0;
 
1703
      conds= remove_additional_cond(conds);
 
1704
      save_index_subquery_explain_info(join_tab, conds);
 
1705
      return(unit->item->
 
1706
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1707
                                                                   join_tab,
 
1708
                                                                   unit->item,
 
1709
                                                                   conds,
 
1710
                                                                   having,
 
1711
                                                                   1)));
 
1712
    }
 
1713
 
 
1714
  }
 
1715
  /*
 
1716
    Need to tell handlers that to play it safe, it should fetch all
 
1717
    columns of the primary key of the tables: this is because MySQL may
 
1718
    build row pointers for the rows, and for all columns of the primary key
 
1719
    the read set has not necessarily been set by the server code.
 
1720
  */
 
1721
  if (need_tmp || select_distinct || group_list || order)
 
1722
  {
 
1723
    for (uint i = const_tables; i < tables; i++)
 
1724
      join_tab[i].table->prepare_for_position();
 
1725
  }
 
1726
 
 
1727
  if (const_tables != tables)
 
1728
  {
 
1729
    /*
 
1730
      Because filesort always does a full table scan or a quick range scan
 
1731
      we must add the removed reference to the select for the table.
 
1732
      We only need to do this when we have a simple_order or simple_group
 
1733
      as in other cases the join is done before the sort.
 
1734
    */
 
1735
    if ((order || group_list) &&
 
1736
        (join_tab[const_tables].type != JT_ALL) &&
 
1737
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1738
        ((order && simple_order) || (group_list && simple_group)))
 
1739
    {
 
1740
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1741
        return(1);
 
1742
      }
 
1743
    }
 
1744
    
 
1745
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1746
        ((group_list &&
 
1747
          (!simple_group ||
 
1748
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1749
                                    unit->select_limit_cnt, 0, 
 
1750
                                    &join_tab[const_tables].table->
 
1751
                                    keys_in_use_for_group_by))) ||
 
1752
         select_distinct) &&
 
1753
        tmp_table_param.quick_group)
 
1754
    {
 
1755
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1756
    }
 
1757
    if (order)
 
1758
    {
 
1759
      /*
 
1760
        Force using of tmp table if sorting by a SP or UDF function due to
 
1761
        their expensive and probably non-deterministic nature.
 
1762
      */
 
1763
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1764
      {
 
1765
        Item *item= *tmp_order->item;
 
1766
        if (item->is_expensive())
 
1767
        {
 
1768
          /* Force tmp table without sort */
 
1769
          need_tmp=1; simple_order=simple_group=0;
 
1770
          break;
 
1771
        }
 
1772
      }
 
1773
    }
 
1774
  }
 
1775
 
 
1776
  tmp_having= having;
 
1777
  if (select_options & SELECT_DESCRIBE)
 
1778
  {
 
1779
    error= 0;
 
1780
    return(0);
 
1781
  }
 
1782
  having= 0;
 
1783
 
 
1784
  /*
 
1785
    The loose index scan access method guarantees that all grouping or
 
1786
    duplicate row elimination (for distinct) is already performed
 
1787
    during data retrieval, and that all MIN/MAX functions are already
 
1788
    computed for each group. Thus all MIN/MAX functions should be
 
1789
    treated as regular functions, and there is no need to perform
 
1790
    grouping in the main execution loop.
 
1791
    Notice that currently loose index scan is applicable only for
 
1792
    single table queries, thus it is sufficient to test only the first
 
1793
    join_tab element of the plan for its access method.
 
1794
  */
 
1795
  if (join_tab->is_using_loose_index_scan())
 
1796
    tmp_table_param.precomputed_group_by= true;
 
1797
 
 
1798
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1799
  if (need_tmp)
 
1800
  {
 
1801
    thd_proc_info(thd, "Creating tmp table");
 
1802
 
 
1803
    init_items_ref_array();
 
1804
 
 
1805
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1806
                                         fields_list.elements);
 
1807
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1808
                                                             (order_st*) 0);
 
1809
    /*
 
1810
      Pushing LIMIT to the temporary table creation is not applicable
 
1811
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1812
      there are aggregate functions, because in all these cases we need
 
1813
      all result rows.
 
1814
    */
 
1815
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1816
                             !tmp_group &&
 
1817
                             !thd->lex->current_select->with_sum_func) ?
 
1818
                            select_limit : HA_POS_ERROR;
 
1819
 
 
1820
    if (!(exec_tmp_table1=
 
1821
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1822
                           tmp_group,
 
1823
                           group_list ? 0 : select_distinct,
 
1824
                           group_list && simple_group,
 
1825
                           select_options,
 
1826
                           tmp_rows_limit,
 
1827
                           (char *) "")))
 
1828
                {
 
1829
      return(1);
 
1830
    }
 
1831
 
 
1832
    /*
 
1833
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1834
      - we are sorting the table and writing complete group rows to the
 
1835
        temp table.
 
1836
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1837
        on all columns.
 
1838
      
 
1839
      If having is not handled here, it will be checked before the row
 
1840
      is sent to the client.
 
1841
    */    
 
1842
    if (tmp_having && 
 
1843
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1844
      having= tmp_having;
 
1845
 
 
1846
    /* if group or order on first table, sort first */
 
1847
    if (group_list && simple_group)
 
1848
    {
 
1849
      thd_proc_info(thd, "Sorting for group");
 
1850
      if (create_sort_index(thd, this, group_list,
 
1851
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1852
          alloc_group_fields(this, group_list) ||
 
1853
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1854
          setup_sum_funcs(thd, sum_funcs))
 
1855
      {
 
1856
        return(1);
 
1857
      }
 
1858
      group_list=0;
 
1859
    }
 
1860
    else
 
1861
    {
 
1862
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1863
          setup_sum_funcs(thd, sum_funcs))
 
1864
      {
 
1865
        return(1);
 
1866
      }
 
1867
 
 
1868
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1869
      {
 
1870
        thd_proc_info(thd, "Sorting for order");
 
1871
        if (create_sort_index(thd, this, order,
 
1872
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1873
        {
 
1874
          return(1);
 
1875
        }
 
1876
        order=0;
 
1877
      }
 
1878
    }
 
1879
    
 
1880
    /*
 
1881
      Optimize distinct when used on some of the tables
 
1882
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1883
      In this case we can stop scanning t2 when we have found one t1.a
 
1884
    */
 
1885
 
 
1886
    if (exec_tmp_table1->distinct)
 
1887
    {
 
1888
      table_map used_tables= thd->used_tables;
 
1889
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1890
      do
 
1891
      {
 
1892
        if (used_tables & last_join_tab->table->map)
 
1893
          break;
 
1894
        last_join_tab->not_used_in_distinct=1;
 
1895
      } while (last_join_tab-- != join_tab);
 
1896
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1897
      if (order && skip_sort_order)
 
1898
      {
 
1899
        /* Should always succeed */
 
1900
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1901
                                    order, unit->select_limit_cnt, 0, 
 
1902
                                    &join_tab[const_tables].table->
 
1903
                                      keys_in_use_for_order_by))
 
1904
          order=0;
 
1905
      }
 
1906
    }
 
1907
 
 
1908
    /* 
 
1909
      If this join belongs to an uncacheable subquery save 
 
1910
      the original join 
 
1911
    */
 
1912
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1913
        init_save_join_tab())
 
1914
      return(-1);                         /* purecov: inspected */
 
1915
  }
 
1916
 
 
1917
  error= 0;
 
1918
  return(0);
 
1919
}
 
1920
 
 
1921
 
 
1922
/**
 
1923
  Restore values in temporary join.
 
1924
*/
 
1925
void JOIN::restore_tmp()
 
1926
{
 
1927
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1928
}
 
1929
 
 
1930
 
 
1931
int
 
1932
JOIN::reinit()
 
1933
{
 
1934
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1935
                                    select_lex->offset_limit->val_uint() :
 
1936
                                    0ULL);
 
1937
 
 
1938
  first_record= 0;
 
1939
 
 
1940
  if (exec_tmp_table1)
 
1941
  {
 
1942
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1943
    exec_tmp_table1->file->ha_delete_all_rows();
 
1944
    free_io_cache(exec_tmp_table1);
 
1945
    filesort_free_buffers(exec_tmp_table1,0);
 
1946
  }
 
1947
  if (exec_tmp_table2)
 
1948
  {
 
1949
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1950
    exec_tmp_table2->file->ha_delete_all_rows();
 
1951
    free_io_cache(exec_tmp_table2);
 
1952
    filesort_free_buffers(exec_tmp_table2,0);
 
1953
  }
 
1954
  if (items0)
 
1955
    set_items_ref_array(items0);
 
1956
 
 
1957
  if (join_tab_save)
 
1958
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1959
 
 
1960
  if (tmp_join)
 
1961
    restore_tmp();
 
1962
 
 
1963
  /* Reset of sum functions */
 
1964
  if (sum_funcs)
 
1965
  {
 
1966
    Item_sum *func, **func_ptr= sum_funcs;
 
1967
    while ((func= *(func_ptr++)))
 
1968
      func->clear();
 
1969
  }
 
1970
 
 
1971
  return(0);
 
1972
}
 
1973
 
 
1974
/**
 
1975
   @brief Save the original join layout
 
1976
      
 
1977
   @details Saves the original join layout so it can be reused in 
 
1978
   re-execution and for EXPLAIN.
 
1979
             
 
1980
   @return Operation status
 
1981
   @retval 0      success.
 
1982
   @retval 1      error occurred.
 
1983
*/
 
1984
 
 
1985
bool
 
1986
JOIN::init_save_join_tab()
 
1987
{
 
1988
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
1989
    return 1;                                  /* purecov: inspected */
 
1990
  error= 0;                                    // Ensure that tmp_join.error= 0
 
1991
  restore_tmp();
 
1992
  return 0;
 
1993
}
 
1994
 
 
1995
 
 
1996
bool
 
1997
JOIN::save_join_tab()
 
1998
{
 
1999
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2000
  {
 
2001
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
 
2002
                                                sizeof(JOIN_TAB) * tables)))
 
2003
      return 1;
 
2004
  }
 
2005
  return 0;
 
2006
}
 
2007
 
 
2008
 
 
2009
/**
 
2010
  Exec select.
 
2011
 
 
2012
  @todo
 
2013
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2014
    finally replace sorting with index scan if there is a LIMIT clause in
 
2015
    the query.  It's never shown in EXPLAIN!
 
2016
 
 
2017
  @todo
 
2018
    When can we have here thd->net.report_error not zero?
 
2019
*/
 
2020
void
 
2021
JOIN::exec()
 
2022
{
 
2023
  List<Item> *columns_list= &fields_list;
 
2024
  int      tmp_error;
 
2025
 
 
2026
  thd_proc_info(thd, "executing");
 
2027
  error= 0;
 
2028
  (void) result->prepare2(); // Currently, this cannot fail.
 
2029
 
 
2030
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2031
  {                                           // Only test of functions
 
2032
    if (select_options & SELECT_DESCRIBE)
 
2033
      select_describe(this, false, false, false,
 
2034
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2035
    else
 
2036
    {
 
2037
      result->send_fields(*columns_list,
 
2038
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2039
      /*
 
2040
        We have to test for 'conds' here as the WHERE may not be constant
 
2041
        even if we don't have any tables for prepared statements or if
 
2042
        conds uses something like 'rand()'.
 
2043
      */
 
2044
      if (cond_value != Item::COND_FALSE &&
 
2045
          (!conds || conds->val_int()) &&
 
2046
          (!having || having->val_int()))
 
2047
      {
 
2048
        if (do_send_rows && result->send_data(fields_list))
 
2049
          error= 1;
 
2050
        else
 
2051
        {
 
2052
          error= (int) result->send_eof();
 
2053
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2054
                         thd->sent_row_count);
 
2055
        }
 
2056
      }
 
2057
      else
 
2058
      {
 
2059
        error=(int) result->send_eof();
 
2060
        send_records= 0;
 
2061
      }
 
2062
    }
 
2063
    /* Single select (without union) always returns 0 or 1 row */
 
2064
    thd->limit_found_rows= send_records;
 
2065
    thd->examined_row_count= 0;
 
2066
    return;
 
2067
  }
 
2068
  /*
 
2069
    Don't reset the found rows count if there're no tables as
 
2070
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2071
    It must be accumulated from all join iterations of all join parts.
 
2072
  */
 
2073
  if (tables)
 
2074
    thd->limit_found_rows= 0;
 
2075
 
 
2076
  if (zero_result_cause)
 
2077
  {
 
2078
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2079
                            *columns_list,
 
2080
                            send_row_on_empty_set(),
 
2081
                            select_options,
 
2082
                            zero_result_cause,
 
2083
                            having);
 
2084
    return;
 
2085
  }
 
2086
 
 
2087
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2088
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2089
    return;
 
2090
 
 
2091
  if (select_options & SELECT_DESCRIBE)
 
2092
  {
 
2093
    /*
 
2094
      Check if we managed to optimize order_st BY away and don't use temporary
 
2095
      table to resolve order_st BY: in that case, we only may need to do
 
2096
      filesort for GROUP BY.
 
2097
    */
 
2098
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2099
    {
 
2100
      /*
 
2101
        Reset 'order' to 'group_list' and reinit variables describing
 
2102
        'order'
 
2103
      */
 
2104
      order= group_list;
 
2105
      simple_order= simple_group;
 
2106
      skip_sort_order= 0;
 
2107
    }
 
2108
    if (order && 
 
2109
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2110
        (const_tables == tables ||
 
2111
         ((simple_order || skip_sort_order) &&
 
2112
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2113
                                  select_limit, 0, 
 
2114
                                  &join_tab[const_tables].table->
 
2115
                                    keys_in_use_for_query))))
 
2116
      order=0;
 
2117
    having= tmp_having;
 
2118
    select_describe(this, need_tmp,
 
2119
                    order != 0 && !skip_sort_order,
 
2120
                    select_distinct,
 
2121
                    !tables ? "No tables used" : NullS);
 
2122
    return;
 
2123
  }
 
2124
 
 
2125
  JOIN *curr_join= this;
 
2126
  List<Item> *curr_all_fields= &all_fields;
 
2127
  List<Item> *curr_fields_list= &fields_list;
 
2128
  Table *curr_tmp_table= 0;
 
2129
  /*
 
2130
    Initialize examined rows here because the values from all join parts
 
2131
    must be accumulated in examined_row_count. Hence every join
 
2132
    iteration must count from zero.
 
2133
  */
 
2134
  curr_join->examined_rows= 0;
 
2135
 
 
2136
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2137
  if (need_tmp)
 
2138
  {
 
2139
    if (tmp_join)
 
2140
    {
 
2141
      /*
 
2142
        We are in a non cacheable sub query. Get the saved join structure
 
2143
        after optimization.
 
2144
        (curr_join may have been modified during last exection and we need
 
2145
        to reset it)
 
2146
      */
 
2147
      curr_join= tmp_join;
 
2148
    }
 
2149
    curr_tmp_table= exec_tmp_table1;
 
2150
 
 
2151
    /* Copy data to the temporary table */
 
2152
    thd_proc_info(thd, "Copying to tmp table");
 
2153
    if (!curr_join->sort_and_group &&
 
2154
        curr_join->const_tables != curr_join->tables)
 
2155
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2156
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2157
    {
 
2158
      error= tmp_error;
 
2159
      return;
 
2160
    }
 
2161
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2162
    
 
2163
    if (curr_join->having)
 
2164
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2165
    
 
2166
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2167
    curr_join->all_fields= *curr_all_fields;
 
2168
    if (!items1)
 
2169
    {
 
2170
      items1= items0 + all_fields.elements;
 
2171
      if (sort_and_group || curr_tmp_table->group)
 
2172
      {
 
2173
        if (change_to_use_tmp_fields(thd, items1,
 
2174
                                     tmp_fields_list1, tmp_all_fields1,
 
2175
                                     fields_list.elements, all_fields))
 
2176
          return;
 
2177
      }
 
2178
      else
 
2179
      {
 
2180
        if (change_refs_to_tmp_fields(thd, items1,
 
2181
                                      tmp_fields_list1, tmp_all_fields1,
 
2182
                                      fields_list.elements, all_fields))
 
2183
          return;
 
2184
      }
 
2185
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2186
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2187
      curr_join->items1= items1;
 
2188
    }
 
2189
    curr_all_fields= &tmp_all_fields1;
 
2190
    curr_fields_list= &tmp_fields_list1;
 
2191
    curr_join->set_items_ref_array(items1);
 
2192
    
 
2193
    if (sort_and_group || curr_tmp_table->group)
 
2194
    {
 
2195
      curr_join->tmp_table_param.field_count+= 
 
2196
        curr_join->tmp_table_param.sum_func_count+
 
2197
        curr_join->tmp_table_param.func_count;
 
2198
      curr_join->tmp_table_param.sum_func_count= 
 
2199
        curr_join->tmp_table_param.func_count= 0;
 
2200
    }
 
2201
    else
 
2202
    {
 
2203
      curr_join->tmp_table_param.field_count+= 
 
2204
        curr_join->tmp_table_param.func_count;
 
2205
      curr_join->tmp_table_param.func_count= 0;
 
2206
    }
 
2207
    
 
2208
    if (curr_tmp_table->group)
 
2209
    {                                           // Already grouped
 
2210
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2211
        curr_join->order= curr_join->group_list;  /* order by group */
 
2212
      curr_join->group_list= 0;
 
2213
    }
 
2214
    
 
2215
    /*
 
2216
      If we have different sort & group then we must sort the data by group
 
2217
      and copy it to another tmp table
 
2218
      This code is also used if we are using distinct something
 
2219
      we haven't been able to store in the temporary table yet
 
2220
      like SEC_TO_TIME(SUM(...)).
 
2221
    */
 
2222
 
 
2223
    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))
 
2224
    {                                   /* Must copy to another table */
 
2225
      /* Free first data from old join */
 
2226
      curr_join->join_free();
 
2227
      if (make_simple_join(curr_join, curr_tmp_table))
 
2228
        return;
 
2229
      calc_group_buffer(curr_join, group_list);
 
2230
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2231
                        curr_join->tmp_all_fields1,
 
2232
                        curr_join->select_distinct && !curr_join->group_list);
 
2233
      curr_join->tmp_table_param.hidden_field_count= 
 
2234
        (curr_join->tmp_all_fields1.elements-
 
2235
         curr_join->tmp_fields_list1.elements);
 
2236
      
 
2237
      
 
2238
      if (exec_tmp_table2)
 
2239
        curr_tmp_table= exec_tmp_table2;
 
2240
      else
 
2241
      {
 
2242
        /* group data to new table */
 
2243
 
 
2244
        /*
 
2245
          If the access method is loose index scan then all MIN/MAX
 
2246
          functions are precomputed, and should be treated as regular
 
2247
          functions. See extended comment in JOIN::exec.
 
2248
        */
 
2249
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2250
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2251
 
 
2252
        if (!(curr_tmp_table=
 
2253
              exec_tmp_table2= create_tmp_table(thd,
 
2254
                                                &curr_join->tmp_table_param,
 
2255
                                                *curr_all_fields,
 
2256
                                                (order_st*) 0,
 
2257
                                                curr_join->select_distinct && 
 
2258
                                                !curr_join->group_list,
 
2259
                                                1, curr_join->select_options,
 
2260
                                                HA_POS_ERROR,
 
2261
                                                (char *) "")))
 
2262
          return;
 
2263
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2264
      }
 
2265
      if (curr_join->group_list)
 
2266
      {
 
2267
        thd_proc_info(thd, "Creating sort index");
 
2268
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2269
        {
 
2270
          return;
 
2271
        }
 
2272
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2273
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2274
            make_group_fields(this, curr_join))
 
2275
        {
 
2276
          return;
 
2277
        }
 
2278
        sortorder= curr_join->sortorder;
 
2279
      }
 
2280
      
 
2281
      thd_proc_info(thd, "Copying to group table");
 
2282
      tmp_error= -1;
 
2283
      if (curr_join != this)
 
2284
      {
 
2285
        if (sum_funcs2)
 
2286
        {
 
2287
          curr_join->sum_funcs= sum_funcs2;
 
2288
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2289
        }
 
2290
        else
 
2291
        {
 
2292
          curr_join->alloc_func_list();
 
2293
          sum_funcs2= curr_join->sum_funcs;
 
2294
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2295
        }
 
2296
      }
 
2297
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2298
                                        1, true))
 
2299
        return;
 
2300
      curr_join->group_list= 0;
 
2301
      if (!curr_join->sort_and_group &&
 
2302
          curr_join->const_tables != curr_join->tables)
 
2303
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2304
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2305
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2306
      {
 
2307
        error= tmp_error;
 
2308
        return;
 
2309
      }
 
2310
      end_read_record(&curr_join->join_tab->read_record);
 
2311
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2312
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2313
      
 
2314
      // No sum funcs anymore
 
2315
      if (!items2)
 
2316
      {
 
2317
        items2= items1 + all_fields.elements;
 
2318
        if (change_to_use_tmp_fields(thd, items2,
 
2319
                                     tmp_fields_list2, tmp_all_fields2, 
 
2320
                                     fields_list.elements, tmp_all_fields1))
 
2321
          return;
 
2322
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2323
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2324
      }
 
2325
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2326
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2327
      curr_join->set_items_ref_array(items2);
 
2328
      curr_join->tmp_table_param.field_count+= 
 
2329
        curr_join->tmp_table_param.sum_func_count;
 
2330
      curr_join->tmp_table_param.sum_func_count= 0;
 
2331
    }
 
2332
    if (curr_tmp_table->distinct)
 
2333
      curr_join->select_distinct=0;             /* Each row is unique */
 
2334
    
 
2335
    curr_join->join_free();                     /* Free quick selects */
 
2336
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2337
    {
 
2338
      thd_proc_info(thd, "Removing duplicates");
 
2339
      if (curr_join->tmp_having)
 
2340
        curr_join->tmp_having->update_used_tables();
 
2341
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2342
                            *curr_fields_list, curr_join->tmp_having))
 
2343
        return;
 
2344
      curr_join->tmp_having=0;
 
2345
      curr_join->select_distinct=0;
 
2346
    }
 
2347
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2348
    if (make_simple_join(curr_join, curr_tmp_table))
 
2349
      return;
 
2350
    calc_group_buffer(curr_join, curr_join->group_list);
 
2351
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2352
                      *curr_all_fields, 0);
 
2353
    
 
2354
  }
 
2355
  
 
2356
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2357
  {
 
2358
    if (make_group_fields(this, curr_join))
 
2359
    {
 
2360
      return;
 
2361
    }
 
2362
    if (!items3)
 
2363
    {
 
2364
      if (!items0)
 
2365
        init_items_ref_array();
 
2366
      items3= ref_pointer_array + (all_fields.elements*4);
 
2367
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2368
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2369
                        curr_fields_list->elements, *curr_all_fields);
 
2370
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2371
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2372
      tmp_table_param.save_copy_field_end=
 
2373
        curr_join->tmp_table_param.copy_field_end;
 
2374
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2375
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2376
    }
 
2377
    else
 
2378
    {
 
2379
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2380
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2381
      curr_join->tmp_table_param.copy_field_end=
 
2382
        tmp_table_param.save_copy_field_end;
 
2383
    }
 
2384
    curr_fields_list= &tmp_fields_list3;
 
2385
    curr_all_fields= &tmp_all_fields3;
 
2386
    curr_join->set_items_ref_array(items3);
 
2387
 
 
2388
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2389
                                      1, true) || 
 
2390
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2391
        thd->is_fatal_error)
 
2392
      return;
 
2393
  }
 
2394
  if (curr_join->group_list || curr_join->order)
 
2395
  {
 
2396
    thd_proc_info(thd, "Sorting result");
 
2397
    /* If we have already done the group, add HAVING to sorted table */
 
2398
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2399
        ! curr_join->sort_and_group)
 
2400
    {
 
2401
      // Some tables may have been const
 
2402
      curr_join->tmp_having->update_used_tables();
 
2403
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2404
      table_map used_tables= (curr_join->const_table_map |
 
2405
                              curr_table->table->map);
 
2406
 
 
2407
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2408
                                                 used_tables,
 
2409
                                                 used_tables, 0);
 
2410
      if (sort_table_cond)
 
2411
      {
 
2412
        if (!curr_table->select)
 
2413
          if (!(curr_table->select= new SQL_SELECT))
 
2414
            return;
 
2415
        if (!curr_table->select->cond)
 
2416
          curr_table->select->cond= sort_table_cond;
 
2417
        else                                    // This should never happen
 
2418
        {
 
2419
          if (!(curr_table->select->cond=
 
2420
                new Item_cond_and(curr_table->select->cond,
 
2421
                                  sort_table_cond)))
 
2422
            return;
 
2423
          /*
 
2424
            Item_cond_and do not need fix_fields for execution, its parameters
 
2425
            are fixed or do not need fix_fields, too
 
2426
          */
 
2427
          curr_table->select->cond->quick_fix_field();
 
2428
        }
 
2429
        curr_table->select_cond= curr_table->select->cond;
 
2430
        curr_table->select_cond->top_level_item();
 
2431
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2432
                                                   ~ (table_map) 0,
 
2433
                                                   ~used_tables, 0);
 
2434
      }
 
2435
    }
 
2436
    {
 
2437
      if (group)
 
2438
        curr_join->select_limit= HA_POS_ERROR;
 
2439
      else
 
2440
      {
 
2441
        /*
 
2442
          We can abort sorting after thd->select_limit rows if we there is no
 
2443
          WHERE clause for any tables after the sorted one.
 
2444
        */
 
2445
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2446
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2447
        for (; curr_table < end_table ; curr_table++)
 
2448
        {
 
2449
          /*
 
2450
            table->keyuse is set in the case there was an original WHERE clause
 
2451
            on the table that was optimized away.
 
2452
          */
 
2453
          if (curr_table->select_cond ||
 
2454
              (curr_table->keyuse && !curr_table->first_inner))
 
2455
          {
 
2456
            /* We have to sort all rows */
 
2457
            curr_join->select_limit= HA_POS_ERROR;
 
2458
            break;
 
2459
          }
 
2460
        }
 
2461
      }
 
2462
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2463
      {
 
2464
        return;
 
2465
      }
 
2466
      /*
 
2467
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2468
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2469
        suitable index present.
 
2470
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2471
        finally replace sorting with index scan if there is a LIMIT clause in
 
2472
        the query. XXX: it's never shown in EXPLAIN!
 
2473
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2474
      */
 
2475
      if (create_sort_index(thd, curr_join,
 
2476
                            curr_join->group_list ? 
 
2477
                            curr_join->group_list : curr_join->order,
 
2478
                            curr_join->select_limit,
 
2479
                            (select_options & OPTION_FOUND_ROWS ?
 
2480
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2481
                            curr_join->group_list ? true : false))
 
2482
        return;
 
2483
      sortorder= curr_join->sortorder;
 
2484
      if (curr_join->const_tables != curr_join->tables &&
 
2485
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2486
      {
 
2487
        /*
 
2488
          If no IO cache exists for the first table then we are using an
 
2489
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2490
          attribute on the INDEX SCAN.
 
2491
        */
 
2492
        skip_sort_order= 1;
 
2493
      }
 
2494
    }
 
2495
  }
 
2496
  /* XXX: When can we have here thd->is_error() not zero? */
 
2497
  if (thd->is_error())
 
2498
  {
 
2499
    error= thd->is_error();
 
2500
    return;
 
2501
  }
 
2502
  curr_join->having= curr_join->tmp_having;
 
2503
  curr_join->fields= curr_fields_list;
 
2504
 
 
2505
  {
 
2506
    thd_proc_info(thd, "Sending data");
 
2507
    result->send_fields(*curr_fields_list,
 
2508
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2509
    error= do_select(curr_join, curr_fields_list, NULL);
 
2510
    thd->limit_found_rows= curr_join->send_records;
 
2511
  }
 
2512
 
 
2513
  /* Accumulate the counts from all join iterations of all join parts. */
 
2514
  thd->examined_row_count+= curr_join->examined_rows;
 
2515
 
 
2516
  /* 
 
2517
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2518
    for a derived table which is always materialized.
 
2519
    Otherwise we would not be able to print the query  correctly.
 
2520
  */ 
 
2521
  if (items0 &&
 
2522
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2523
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2524
    set_items_ref_array(items0);
 
2525
 
 
2526
  return;
 
2527
}
 
2528
 
 
2529
 
 
2530
/**
 
2531
  Clean up join.
 
2532
 
 
2533
  @return
 
2534
    Return error that hold JOIN.
 
2535
*/
 
2536
 
 
2537
int
 
2538
JOIN::destroy()
 
2539
{
 
2540
  select_lex->join= 0;
 
2541
 
 
2542
  if (tmp_join)
 
2543
  {
 
2544
    if (join_tab != tmp_join->join_tab)
 
2545
    {
 
2546
      JOIN_TAB *tab, *end;
 
2547
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2548
        tab->cleanup();
 
2549
    }
 
2550
    tmp_join->tmp_join= 0;
 
2551
    tmp_table_param.copy_field=0;
 
2552
    return(tmp_join->destroy());
 
2553
  }
 
2554
  cond_equal= 0;
 
2555
 
 
2556
  cleanup(1);
 
2557
  if (exec_tmp_table1)
 
2558
    exec_tmp_table1->free_tmp_table(thd);
 
2559
  if (exec_tmp_table2)
 
2560
    exec_tmp_table2->free_tmp_table(thd);
 
2561
  delete select;
 
2562
  delete_dynamic(&keyuse);
 
2563
  return(error);
 
2564
}
 
2565
 
 
2566
 
 
2567
 
311
2568
/**
312
2569
  An entry point to single-unit select (a select without UNION).
313
2570
 
314
 
  @param session                  thread Cursor
 
2571
  @param thd                  thread handler
315
2572
  @param rref_pointer_array   a reference to ref_pointer_array of
316
2573
                              the top-level select_lex for this query
317
2574
  @param tables               list of all tables used in this query.
318
2575
                              The tables have been pre-opened.
319
 
  @param wild_num             number of wildcards used in the top level
 
2576
  @param wild_num             number of wildcards used in the top level 
320
2577
                              select of this query.
321
2578
                              For example statement
322
2579
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
327
2584
                              for a, b and c in this list.
328
2585
  @param conds                top level item of an expression representing
329
2586
                              WHERE clause of the top level select
330
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2587
  @param og_num               total number of order_st BY and GROUP BY clauses
331
2588
                              arguments
332
 
  @param order                linked list of ORDER BY agruments
 
2589
  @param order                linked list of order_st BY agruments
333
2590
  @param group                linked list of GROUP BY arguments
334
2591
  @param having               top level item of HAVING expression
 
2592
  @param proc_param           list of PROCEDUREs
335
2593
  @param select_options       select options (BIG_RESULT, etc)
336
2594
  @param result               an instance of result set handling class.
337
2595
                              This object is responsible for send result
338
2596
                              set rows to the client or inserting them
339
2597
                              into a table.
340
 
  @param select_lex           the only Select_Lex of this query
 
2598
  @param select_lex           the only SELECT_LEX of this query
341
2599
  @param unit                 top-level UNIT of this query
342
2600
                              UNIT is an artificial object created by the
343
2601
                              parser for every SELECT clause.
350
2608
  @retval
351
2609
    true   an error
352
2610
*/
353
 
bool mysql_select(Session *session,
354
 
                  Item ***rref_pointer_array,
355
 
                  TableList *tables, 
356
 
                  uint32_t wild_num, 
357
 
                  List<Item> &fields,
358
 
                  COND *conds, 
359
 
                  uint32_t og_num,  
360
 
                  Order *order,
361
 
                  Order *group,
362
 
                  Item *having, 
363
 
                  uint64_t select_options,
364
 
                  select_result *result, 
365
 
                  Select_Lex_Unit *unit,
366
 
                  Select_Lex *select_lex)
 
2611
 
 
2612
bool
 
2613
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2614
             TableList *tables, uint wild_num, List<Item> &fields,
 
2615
             COND *conds, uint og_num,  order_st *order, order_st *group,
 
2616
             Item *having, order_st *proc_param, uint64_t select_options,
 
2617
             select_result *result, SELECT_LEX_UNIT *unit,
 
2618
             SELECT_LEX *select_lex)
367
2619
{
368
2620
  bool err;
369
2621
  bool free_join= 1;
370
2622
 
371
2623
  select_lex->context.resolve_in_select_list= true;
372
 
  Join *join;
 
2624
  JOIN *join;
373
2625
  if (select_lex->join != 0)
374
2626
  {
375
2627
    join= select_lex->join;
378
2630
      creation
379
2631
    */
380
2632
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
381
 
        (select_options & SELECT_DESCRIBE))
 
2633
        (select_options & SELECT_DESCRIBE))
382
2634
    {
383
2635
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
384
2636
      {
385
 
        //here is EXPLAIN of subselect or derived table
386
 
        if (join->change_result(result))
387
 
        {
388
 
          return(true);
389
 
        }
 
2637
        //here is EXPLAIN of subselect or derived table
 
2638
        if (join->change_result(result))
 
2639
        {
 
2640
          return(true);
 
2641
        }
390
2642
      }
391
2643
      else
392
2644
      {
393
2645
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
394
 
                               conds, og_num, order, group, having, select_lex, unit)))
395
 
        {
396
 
          goto err;
397
 
        }
 
2646
                               conds, og_num, order, group, having, proc_param,
 
2647
                               select_lex, unit)))
 
2648
        {
 
2649
          goto err;
 
2650
        }
398
2651
      }
399
2652
    }
400
2653
    free_join= 0;
402
2655
  }
403
2656
  else
404
2657
  {
405
 
    if (!(join= new Join(session, fields, select_options, result)))
406
 
      return(true);
407
 
    session->set_proc_info("init");
408
 
    session->used_tables=0;                         // Updated by setup_fields
 
2658
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2659
        return(true);
 
2660
    thd_proc_info(thd, "init");
 
2661
    thd->used_tables=0;                         // Updated by setup_fields
409
2662
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
410
 
                           conds, og_num, order, group, having,
 
2663
                           conds, og_num, order, group, having, proc_param,
411
2664
                           select_lex, unit)) == true)
412
2665
    {
413
2666
      goto err;
414
2667
    }
415
2668
  }
416
2669
 
417
 
  err= join->optimize();
418
 
  if (err)
419
 
  {
420
 
    goto err; // 1
421
 
  }
422
 
 
423
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2670
  /* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
 
2671
  if (join->flatten_subqueries())
 
2672
  {
 
2673
    err= 1;
 
2674
    goto err;
 
2675
  }
 
2676
  /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
2677
 
 
2678
  if ((err= join->optimize()))
 
2679
  {
 
2680
    goto err;                                   // 1
 
2681
  }
 
2682
 
 
2683
  if (thd->lex->describe & DESCRIBE_EXTENDED)
424
2684
  {
425
2685
    join->conds_history= join->conds;
426
2686
    join->having_history= (join->having?join->having:join->tmp_having);
427
2687
  }
428
2688
 
429
 
  if (session->is_error())
 
2689
  if (thd->is_error())
430
2690
    goto err;
431
2691
 
432
2692
  join->exec();
433
2693
 
434
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2694
  if (thd->lex->describe & DESCRIBE_EXTENDED)
435
2695
  {
436
2696
    select_lex->where= join->conds_history;
437
2697
    select_lex->having= join->having_history;
440
2700
err:
441
2701
  if (free_join)
442
2702
  {
443
 
    session->set_proc_info("end");
 
2703
    thd_proc_info(thd, "end");
444
2704
    err|= select_lex->cleanup();
445
 
    return(err || session->is_error());
 
2705
    return(err || thd->is_error());
446
2706
  }
447
2707
  return(join->error);
448
2708
}
449
2709
 
450
 
inline Item *and_items(Item* cond, Item *item)
 
2710
 
 
2711
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2712
                          Item_in_subselect* const *el2)
 
2713
{
 
2714
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2715
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2716
}
 
2717
 
 
2718
 
 
2719
inline Item * and_items(Item* cond, Item *item)
451
2720
{
452
2721
  return (cond? (new Item_cond_and(cond, item)) : item);
453
2722
}
454
2723
 
 
2724
 
 
2725
static TableList *alloc_join_nest(THD *thd)
 
2726
{
 
2727
  TableList *tbl;
 
2728
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2729
                                       sizeof(nested_join_st))))
 
2730
    return NULL;
 
2731
  tbl->nested_join= (nested_join_st*) ((uchar*)tbl + 
 
2732
                                    ALIGN_SIZE(sizeof(TableList)));
 
2733
  return tbl;
 
2734
}
 
2735
 
 
2736
 
 
2737
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2738
{
 
2739
  List_iterator<TableList> it(*tlist);
 
2740
  TableList *table;
 
2741
  while ((table= it++))
 
2742
  {
 
2743
    if (table->on_expr)
 
2744
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2745
    if (table->nested_join)
 
2746
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2747
  }
 
2748
}
 
2749
 
 
2750
 
 
2751
/*
 
2752
  Convert a subquery predicate into a TableList semi-join nest
 
2753
 
 
2754
  SYNOPSIS
 
2755
    convert_subq_to_sj()
 
2756
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2757
                    clause
 
2758
       subq_pred    Subquery predicate to be converted
 
2759
  
 
2760
  DESCRIPTION
 
2761
    Convert a subquery predicate into a TableList semi-join nest. All the 
 
2762
    prerequisites are already checked, so the conversion is always successfull.
 
2763
 
 
2764
    Prepared Statements: the transformation is permanent:
 
2765
     - Changes in TableList structures are naturally permanent
 
2766
     - Item tree changes are performed on statement MEM_ROOT:
 
2767
        = we activate statement MEM_ROOT 
 
2768
        = this function is called before the first fix_prepare_information
 
2769
          call.
 
2770
 
 
2771
    This is intended because the criteria for subquery-to-sj conversion remain
 
2772
    constant for the lifetime of the Prepared Statement.
 
2773
 
 
2774
  RETURN
 
2775
    false  OK
 
2776
    true   Out of memory error
 
2777
*/
 
2778
 
 
2779
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2780
{
 
2781
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2782
  TableList *emb_tbl_nest= NULL;
 
2783
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2784
  THD *thd= parent_join->thd;
 
2785
 
 
2786
  /*
 
2787
    1. Find out where to put the predicate into.
 
2788
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2789
  */
 
2790
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2791
  {
 
2792
    if (subq_pred->expr_join_nest->nested_join)
 
2793
    {
 
2794
      /*
 
2795
        We're dealing with
 
2796
 
 
2797
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2798
 
 
2799
        The sj-nest will be inserted into the brackets nest.
 
2800
      */
 
2801
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2802
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2803
    }
 
2804
    else if (!subq_pred->expr_join_nest->outer_join)
 
2805
    {
 
2806
      /*
 
2807
        We're dealing with
 
2808
 
 
2809
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2810
 
 
2811
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2812
        parent. This is ok because tblX is joined as an inner join.
 
2813
      */
 
2814
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2815
      if (emb_tbl_nest)
 
2816
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2817
    }
 
2818
    else if (!subq_pred->expr_join_nest->nested_join)
 
2819
    {
 
2820
      TableList *outer_tbl= subq_pred->expr_join_nest;      
 
2821
      TableList *wrap_nest;
 
2822
      /*
 
2823
        We're dealing with
 
2824
 
 
2825
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2826
 
 
2827
        we'll need to convert it into:
 
2828
 
 
2829
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2830
                        |                      |
 
2831
                        |<----- wrap_nest ---->|
 
2832
        
 
2833
        Q:  other subqueries may be pointing to this element. What to do?
 
2834
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2835
            But we'll need to fix other pointers.
 
2836
        A2: Another way: have TableList::next_ptr so the following
 
2837
            subqueries know the table has been nested.
 
2838
        A3: changes in the TableList::outer_join will make everything work
 
2839
            automatically.
 
2840
      */
 
2841
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2842
      {
 
2843
        return(true);
 
2844
      }
 
2845
      wrap_nest->embedding= outer_tbl->embedding;
 
2846
      wrap_nest->join_list= outer_tbl->join_list;
 
2847
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2848
 
 
2849
      wrap_nest->nested_join->join_list.empty();
 
2850
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2851
 
 
2852
      outer_tbl->embedding= wrap_nest;
 
2853
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2854
 
 
2855
      /*
 
2856
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2857
        and on_expr
 
2858
      */
 
2859
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2860
      outer_tbl->outer_join= 0;
 
2861
 
 
2862
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2863
      outer_tbl->on_expr= NULL;
 
2864
 
 
2865
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2866
      TableList *tbl;
 
2867
      while ((tbl= li++))
 
2868
      {
 
2869
        if (tbl == outer_tbl)
 
2870
        {
 
2871
          li.replace(wrap_nest);
 
2872
          break;
 
2873
        }
 
2874
      }
 
2875
      /*
 
2876
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2877
        semi-join nest into it
 
2878
      */
 
2879
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2880
      emb_tbl_nest=  wrap_nest;
 
2881
    }
 
2882
  }
 
2883
 
 
2884
  TableList *sj_nest;
 
2885
  nested_join_st *nested_join;
 
2886
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2887
  {
 
2888
    return(true);
 
2889
  }
 
2890
  nested_join= sj_nest->nested_join;
 
2891
 
 
2892
  sj_nest->join_list= emb_join_list;
 
2893
  sj_nest->embedding= emb_tbl_nest;
 
2894
  sj_nest->alias= (char*) "(sj-nest)";
 
2895
  /* Nests do not participate in those 'chains', so: */
 
2896
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2897
  emb_join_list->push_back(sj_nest);
 
2898
 
 
2899
  /* 
 
2900
    nested_join->used_tables and nested_join->not_null_tables are
 
2901
    initialized in simplify_joins().
 
2902
  */
 
2903
  
 
2904
  /* 
 
2905
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2906
       sj-nest.
 
2907
  */
 
2908
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2909
  nested_join->join_list.empty();
 
2910
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2911
  TableList *tl, *last_leaf;
 
2912
  while ((tl= li++))
 
2913
  {
 
2914
    tl->embedding= sj_nest;
 
2915
    tl->join_list= &nested_join->join_list;
 
2916
    nested_join->join_list.push_back(tl);
 
2917
  }
 
2918
  
 
2919
  /*
 
2920
    Reconnect the next_leaf chain.
 
2921
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2922
          Inserting them at the beginning would be a bit faster.
 
2923
    NOTE: We actually insert them at the front! That's because the order is
 
2924
          reversed in this list.
 
2925
  */
 
2926
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2927
  tl->next_leaf= subq_lex->leaf_tables;
 
2928
  last_leaf= tl;
 
2929
 
 
2930
  /*
 
2931
    Same as above for next_local chain
 
2932
    (a theory: a next_local chain always starts with ::leaf_tables
 
2933
     because view's tables are inserted after the view)
 
2934
  */
 
2935
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2936
  tl->next_local= subq_lex->leaf_tables;
 
2937
 
 
2938
  /* A theory: no need to re-connect the next_global chain */
 
2939
 
 
2940
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2941
 
 
2942
  // The subqueries were replaced for Item_int(1) earlier
 
2943
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2944
  /*TODO: also reset the 'with_subselect' there. */
 
2945
 
 
2946
  /* n. Adjust the parent_join->tables counter */
 
2947
  uint table_no= parent_join->tables;
 
2948
  /* n. Walk through child's tables and adjust table->map */
 
2949
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2950
  {
 
2951
    tl->table->tablenr= table_no;
 
2952
    tl->table->map= ((table_map)1) << table_no;
 
2953
    SELECT_LEX *old_sl= tl->select_lex;
 
2954
    tl->select_lex= parent_join->select_lex; 
 
2955
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2956
      emb->select_lex= parent_join->select_lex;
 
2957
  }
 
2958
  parent_join->tables += subq_lex->join->tables;
 
2959
 
 
2960
  /* 
 
2961
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2962
    Add the subquery-induced equalities too.
 
2963
  */
 
2964
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2965
  thd->lex->current_select=subq_lex;
 
2966
  if (!subq_pred->left_expr->fixed &&
 
2967
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2968
    return(true);
 
2969
  thd->lex->current_select=save_lex;
 
2970
 
 
2971
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2972
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2973
                                        subq_pred->left_expr->used_tables();
 
2974
  sj_nest->sj_on_expr= subq_lex->where;
 
2975
 
 
2976
  /*
 
2977
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2978
    Additionally, for InsideOut strategy
 
2979
     - Record the number of IN-equalities.
 
2980
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2981
       see which of the expressions are bound and which are not (for those
 
2982
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2983
 
 
2984
       (TODO: can we just create a list of pointers and hope the expressions
 
2985
       will not substitute themselves on fix_fields()? or we need to wrap
 
2986
       them into Item_direct_view_refs and store pointers to those. The
 
2987
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
2988
       Item_direct_view_refs doesn't substitute itself with anything in 
 
2989
       Item_direct_view_ref::fix_fields.
 
2990
  */
 
2991
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
2992
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
2993
 
 
2994
  if (subq_pred->left_expr->cols() == 1)
 
2995
  {
 
2996
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
2997
 
 
2998
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
2999
                                    subq_lex->ref_pointer_array[0]);
 
3000
    item_eq->name= (char*)subq_sj_cond_name;
 
3001
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3002
  }
 
3003
  else
 
3004
  {
 
3005
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
 
3006
    {
 
3007
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3008
                                                element_index(i));
 
3009
      Item *item_eq= 
 
3010
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3011
                         subq_lex->ref_pointer_array[i]);
 
3012
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3013
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3014
    }
 
3015
  }
 
3016
  /* Fix the created equality and AND */
 
3017
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3018
 
 
3019
  /*
 
3020
    Walk through sj nest's WHERE and ON expressions and call
 
3021
    item->fix_table_changes() for all items.
 
3022
  */
 
3023
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3024
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3025
 
 
3026
 
 
3027
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3028
  subq_lex->master_unit()->exclude_level();
 
3029
 
 
3030
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3031
  if (emb_tbl_nest)
 
3032
  {
 
3033
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3034
                                     sj_nest->sj_on_expr);
 
3035
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3036
  }
 
3037
  else
 
3038
  {
 
3039
    /* Inject into the WHERE */
 
3040
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3041
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3042
    parent_join->select_lex->where= parent_join->conds;
 
3043
  }
 
3044
 
 
3045
  return(false);
 
3046
}
 
3047
 
 
3048
 
 
3049
/*
 
3050
  Convert candidate subquery predicates to semi-joins
 
3051
 
 
3052
  SYNOPSIS
 
3053
    JOIN::flatten_subqueries()
 
3054
 
 
3055
  DESCRIPTION
 
3056
    Convert candidate subquery predicates to semi-joins.
 
3057
 
 
3058
  RETURN 
 
3059
    false  OK
 
3060
    true   Error
 
3061
*/
 
3062
 
 
3063
bool JOIN::flatten_subqueries()
 
3064
{
 
3065
  Item_in_subselect **in_subq;
 
3066
  Item_in_subselect **in_subq_end;
 
3067
 
 
3068
  if (sj_subselects.elements() == 0)
 
3069
    return(false);
 
3070
 
 
3071
  /* 1. Fix children subqueries */
 
3072
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3073
       in_subq != in_subq_end; in_subq++)
 
3074
  {
 
3075
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3076
    child_join->outer_tables = child_join->tables;
 
3077
    if (child_join->flatten_subqueries())
 
3078
      return(true);
 
3079
    (*in_subq)->sj_convert_priority= 
 
3080
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3081
  }
 
3082
 
 
3083
  //dump_TableList_struct(select_lex, select_lex->leaf_tables);
 
3084
  /* 
 
3085
    2. Pick which subqueries to convert:
 
3086
      sort the subquery array
 
3087
      - prefer correlated subqueries over uncorrelated;
 
3088
      - prefer subqueries that have greater number of outer tables;
 
3089
  */
 
3090
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3091
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3092
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3093
  for (in_subq= sj_subselects.front(); 
 
3094
       in_subq != in_subq_end && 
 
3095
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3096
       in_subq++)
 
3097
  {
 
3098
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3099
      return(true);
 
3100
  }
 
3101
 
 
3102
  for (in_subq= sj_subselects.front(); 
 
3103
       in_subq != in_subq_end && 
 
3104
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3105
       in_subq++)
 
3106
  {
 
3107
    if (convert_subq_to_sj(this, *in_subq))
 
3108
      return(true);
 
3109
  }
 
3110
 
 
3111
  /* 3. Finalize those we didn't convert */
 
3112
  for (; in_subq!= in_subq_end; in_subq++)
 
3113
  {
 
3114
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3115
    Item_subselect::trans_res res;
 
3116
    (*in_subq)->changed= 0;
 
3117
    (*in_subq)->fixed= 0;
 
3118
    res= (*in_subq)->select_transformer(child_join);
 
3119
    if (res == Item_subselect::RES_ERROR)
 
3120
      return(true);
 
3121
 
 
3122
    (*in_subq)->changed= 1;
 
3123
    (*in_subq)->fixed= 1;
 
3124
 
 
3125
    Item *substitute= (*in_subq)->substitution;
 
3126
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3127
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3128
      return(true);
 
3129
 
 
3130
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3131
    //  return(true);
 
3132
  }
 
3133
  sj_subselects.clear();
 
3134
  return(false);
 
3135
}
 
3136
 
 
3137
 
 
3138
/**
 
3139
  Setup for execution all subqueries of a query, for which the optimizer
 
3140
  chose hash semi-join.
 
3141
 
 
3142
  @details Iterate over all subqueries of the query, and if they are under an
 
3143
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3144
  - try to initialize all data structures needed for the materialized execution
 
3145
    of the IN predicate,
 
3146
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3147
    previously blocked during JOIN::prepare.
 
3148
 
 
3149
  This method is part of the "code generation" query processing phase.
 
3150
 
 
3151
  This phase must be called after substitute_for_best_equal_field() because
 
3152
  that function may replace items with other items from a multiple equality,
 
3153
  and we need to reference the correct items in the index access method of the
 
3154
  IN predicate.
 
3155
 
 
3156
  @return Operation status
 
3157
  @retval false     success.
 
3158
  @retval true      error occurred.
 
3159
*/
 
3160
 
 
3161
bool JOIN::setup_subquery_materialization()
 
3162
{
 
3163
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3164
       un= un->next_unit())
 
3165
  {
 
3166
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3167
    {
 
3168
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3169
      if (subquery_predicate &&
 
3170
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3171
      {
 
3172
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3173
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3174
            in_subs->setup_engine())
 
3175
          return true;
 
3176
      }
 
3177
    }
 
3178
  }
 
3179
  return false;
 
3180
}
 
3181
 
 
3182
 
 
3183
/*
 
3184
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3185
 
 
3186
  SYNOPSIS
 
3187
    find_eq_ref_candidate()
 
3188
      table             Table to be checked
 
3189
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3190
                        count.
 
3191
 
 
3192
  DESCRIPTION
 
3193
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3194
 
 
3195
  TODO
 
3196
    Check again if it is feasible to factor common parts with constant table
 
3197
    search
 
3198
 
 
3199
  RETURN
 
3200
    true  - There exists an eq_ref(outer-tables) candidate
 
3201
    false - Otherwise
 
3202
*/
 
3203
 
 
3204
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3205
{
 
3206
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3207
  uint key;
 
3208
 
 
3209
  if (keyuse)
 
3210
  {
 
3211
    while (1) /* For each key */
 
3212
    {
 
3213
      key= keyuse->key;
 
3214
      KEY *keyinfo= table->key_info + key;
 
3215
      key_part_map bound_parts= 0;
 
3216
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3217
      {
 
3218
        do  /* For all equalities on all key parts */
 
3219
        {
 
3220
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3221
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3222
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3223
          {
 
3224
            bound_parts |= 1 << keyuse->keypart;
 
3225
          }
 
3226
          keyuse++;
 
3227
        } while (keyuse->key == key && keyuse->table == table);
 
3228
 
 
3229
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3230
          return true;
 
3231
        if (keyuse->table != table)
 
3232
          return false;
 
3233
      }
 
3234
      else
 
3235
      {
 
3236
        do
 
3237
        {
 
3238
          keyuse++;
 
3239
          if (keyuse->table != table)
 
3240
            return false;
 
3241
        }
 
3242
        while (keyuse->key == key);
 
3243
      }
 
3244
    }
 
3245
  }
 
3246
  return false;
 
3247
}
 
3248
 
 
3249
 
 
3250
/*
 
3251
  Pull tables out of semi-join nests, if possible
 
3252
 
 
3253
  SYNOPSIS
 
3254
    pull_out_semijoin_tables()
 
3255
      join  The join where to do the semi-join flattening
 
3256
 
 
3257
  DESCRIPTION
 
3258
    Try to pull tables out of semi-join nests.
 
3259
     
 
3260
    PRECONDITIONS
 
3261
    When this function is called, the join may have several semi-join nests
 
3262
    (possibly within different semi-join nests), but it is guaranteed that
 
3263
    one semi-join nest does not contain another.
 
3264
   
 
3265
    ACTION
 
3266
    A table can be pulled out of the semi-join nest if
 
3267
     - It is a constant table
 
3268
     - It is accessed 
 
3269
 
 
3270
    POSTCONDITIONS
 
3271
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3272
       tables)
 
3273
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3274
     * Semi-join nests TableList::sj_inner_tables
 
3275
 
 
3276
    This operation is (and should be) performed at each PS execution since
 
3277
    tables may become/cease to be constant across PS reexecutions.
 
3278
 
 
3279
  RETURN 
 
3280
    0 - OK
 
3281
    1 - Out of memory error
 
3282
*/
 
3283
 
 
3284
int pull_out_semijoin_tables(JOIN *join)
 
3285
{
 
3286
  TableList *sj_nest;
 
3287
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3288
   
 
3289
  /* Try pulling out of the each of the semi-joins */
 
3290
  while ((sj_nest= sj_list_it++))
 
3291
  {
 
3292
    /* Action #1: Mark the constant tables to be pulled out */
 
3293
    table_map pulled_tables= 0;
 
3294
     
 
3295
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3296
    TableList *tbl;
 
3297
    while ((tbl= child_li++))
 
3298
    {
 
3299
      if (tbl->table)
 
3300
      {
 
3301
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3302
        if (tbl->table->map & join->const_table_map)
 
3303
        {
 
3304
          pulled_tables |= tbl->table->map;
 
3305
        }
 
3306
      }
 
3307
    }
 
3308
    
 
3309
    /*
 
3310
      Action #2: Find which tables we can pull out based on
 
3311
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3312
      us to pull out some other tables too.
 
3313
    */
 
3314
    bool pulled_a_table;
 
3315
    do 
 
3316
    {
 
3317
      pulled_a_table= false;
 
3318
      child_li.rewind();
 
3319
      while ((tbl= child_li++))
 
3320
      {
 
3321
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3322
        {
 
3323
          if (find_eq_ref_candidate(tbl->table, 
 
3324
                                    sj_nest->nested_join->used_tables & 
 
3325
                                    ~pulled_tables))
 
3326
          {
 
3327
            pulled_a_table= true;
 
3328
            pulled_tables |= tbl->table->map;
 
3329
          }
 
3330
        }
 
3331
      }
 
3332
    } while (pulled_a_table);
 
3333
 
 
3334
    child_li.rewind();
 
3335
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3336
    {
 
3337
      (sj_nest)->sj_inner_tables= 0;
 
3338
      while ((tbl= child_li++))
 
3339
      {
 
3340
        if (tbl->table)
 
3341
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3342
      }
 
3343
    }
 
3344
    else
 
3345
    {
 
3346
      /* Record the bitmap of inner tables, mark the inner tables */
 
3347
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3348
                             ~pulled_tables;
 
3349
      (sj_nest)->sj_inner_tables= inner_tables;
 
3350
      while ((tbl= child_li++))
 
3351
      {
 
3352
        if (tbl->table)
 
3353
        {
 
3354
          if (inner_tables & tbl->table->map)
 
3355
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3356
          else
 
3357
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3358
        }
 
3359
      }
 
3360
    }
 
3361
  }
 
3362
  return(0);
 
3363
}
 
3364
 
455
3365
/*****************************************************************************
456
 
  Create JoinTableS, make a guess about the table types,
 
3366
  Create JOIN_TABS, make a guess about the table types,
457
3367
  Approximate how many records will be used in each table
458
3368
*****************************************************************************/
459
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3369
 
 
3370
 
 
3371
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3372
                                      Table *table,
 
3373
                                      const key_map *keys,ha_rows limit)
460
3374
{
461
3375
  int error;
462
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3376
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
463
3377
    return(0);                           // Fatal error flag is set
464
3378
  if (select)
465
3379
  {
466
3380
    select->head=table;
467
3381
    table->reginfo.impossible_range=0;
468
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3382
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
469
3383
                                          limit, 0, false)) == 1)
470
3384
      return(select->quick->records);
471
3385
    if (error == -1)
477
3391
  return(HA_POS_ERROR);                 /* This shouldn't happend */
478
3392
}
479
3393
 
 
3394
/*
 
3395
   This structure is used to collect info on potentially sargable
 
3396
   predicates in order to check whether they become sargable after
 
3397
   reading const tables.
 
3398
   We form a bitmap of indexes that can be used for sargable predicates.
 
3399
   Only such indexes are involved in range analysis.
 
3400
*/
 
3401
typedef struct st_sargable_param
 
3402
{
 
3403
  Field *field;              /* field against which to check sargability */
 
3404
  Item **arg_value;          /* values of potential keys for lookups     */
 
3405
  uint num_values;           /* number of values in the above array      */
 
3406
} SARGABLE_PARAM;  
 
3407
 
 
3408
/**
 
3409
  Calculate the best possible join and initialize the join structure.
 
3410
 
 
3411
  @retval
 
3412
    0   ok
 
3413
  @retval
 
3414
    1   Fatal error
 
3415
*/
 
3416
 
 
3417
static bool
 
3418
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3419
                     DYNAMIC_ARRAY *keyuse_array)
 
3420
{
 
3421
  int error;
 
3422
  Table *table;
 
3423
  uint i,table_count,const_count,key;
 
3424
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3425
  key_map const_ref, eq_part;
 
3426
  Table **table_vector;
 
3427
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3428
  KEYUSE *keyuse,*start_keyuse;
 
3429
  table_map outer_join=0;
 
3430
  SARGABLE_PARAM *sargables= 0;
 
3431
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3432
 
 
3433
  table_count=join->tables;
 
3434
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3435
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3436
  table_vector=(Table**) join->thd->alloc(sizeof(Table*)*(table_count*2));
 
3437
  if (!stat || !stat_ref || !table_vector)
 
3438
    return(1);                          // Eom /* purecov: inspected */
 
3439
 
 
3440
  join->best_ref=stat_vector;
 
3441
 
 
3442
  stat_end=stat+table_count;
 
3443
  found_const_table_map= all_table_map=0;
 
3444
  const_count=0;
 
3445
 
 
3446
  for (s= stat, i= 0;
 
3447
       tables;
 
3448
       s++, tables= tables->next_leaf, i++)
 
3449
  {
 
3450
    TableList *embedding= tables->embedding;
 
3451
    stat_vector[i]=s;
 
3452
    s->keys.init();
 
3453
    s->const_keys.init();
 
3454
    s->checked_keys.init();
 
3455
    s->needed_reg.init();
 
3456
    table_vector[i]=s->table=table=tables->table;
 
3457
    table->pos_in_table_list= tables;
 
3458
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3459
    if(error)
 
3460
    {
 
3461
        table->file->print_error(error, MYF(0));
 
3462
        return(1);
 
3463
    }
 
3464
    table->quick_keys.clear_all();
 
3465
    table->reginfo.join_tab=s;
 
3466
    table->reginfo.not_exists_optimize=0;
 
3467
    memset(table->const_key_parts, 0,
 
3468
           sizeof(key_part_map)*table->s->keys);
 
3469
    all_table_map|= table->map;
 
3470
    s->join=join;
 
3471
    s->info=0;                                  // For describe
 
3472
 
 
3473
    s->dependent= tables->dep_tables;
 
3474
    s->key_dependent= 0;
 
3475
    if (tables->schema_table)
 
3476
      table->file->stats.records= 2;
 
3477
    table->quick_condition_rows= table->file->stats.records;
 
3478
 
 
3479
    s->on_expr_ref= &tables->on_expr;
 
3480
    if (*s->on_expr_ref)
 
3481
    {
 
3482
      /* s is the only inner table of an outer join */
 
3483
      if (!table->file->stats.records && !embedding)
 
3484
      {                                         // Empty table
 
3485
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3486
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3487
        continue;
 
3488
      }
 
3489
      outer_join|= table->map;
 
3490
      s->embedding_map= 0;
 
3491
      for (;embedding; embedding= embedding->embedding)
 
3492
        s->embedding_map|= embedding->nested_join->nj_map;
 
3493
      continue;
 
3494
    }
 
3495
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3496
    {
 
3497
      /* s belongs to a nested join, maybe to several embedded joins */
 
3498
      s->embedding_map= 0;
 
3499
      do
 
3500
      {
 
3501
        nested_join_st *nested_join= embedding->nested_join;
 
3502
        s->embedding_map|=nested_join->nj_map;
 
3503
        s->dependent|= embedding->dep_tables;
 
3504
        embedding= embedding->embedding;
 
3505
        outer_join|= nested_join->used_tables;
 
3506
      }
 
3507
      while (embedding);
 
3508
      continue;
 
3509
    }
 
3510
    if ((table->file->stats.records <= 1) &&
 
3511
        !s->dependent &&
 
3512
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3513
    {
 
3514
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3515
    }
 
3516
  }
 
3517
  stat_vector[i]=0;
 
3518
  join->outer_join=outer_join;
 
3519
 
 
3520
  if (join->outer_join)
 
3521
  {
 
3522
    /* 
 
3523
       Build transitive closure for relation 'to be dependent on'.
 
3524
       This will speed up the plan search for many cases with outer joins,
 
3525
       as well as allow us to catch illegal cross references/
 
3526
       Warshall's algorithm is used to build the transitive closure.
 
3527
       As we use bitmaps to represent the relation the complexity
 
3528
       of the algorithm is O((number of tables)^2). 
 
3529
    */
 
3530
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3531
    {
 
3532
      for (uint j= 0 ; j < table_count ; j++)
 
3533
      {
 
3534
        table= stat[j].table;
 
3535
        if (s->dependent & table->map)
 
3536
          s->dependent |= table->reginfo.join_tab->dependent;
 
3537
      }
 
3538
      if (s->dependent)
 
3539
        s->table->maybe_null= 1;
 
3540
    }
 
3541
    /* Catch illegal cross references for outer joins */
 
3542
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3543
    {
 
3544
      if (s->dependent & s->table->map)
 
3545
      {
 
3546
        join->tables=0;                 // Don't use join->table
 
3547
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3548
        return(1);
 
3549
      }
 
3550
      s->key_dependent= s->dependent;
 
3551
    }
 
3552
  }
 
3553
 
 
3554
  if (conds || outer_join)
 
3555
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3556
                            conds, join->cond_equal,
 
3557
                            ~outer_join, join->select_lex, &sargables))
 
3558
      return(1);
 
3559
 
 
3560
  /* Read tables with 0 or 1 rows (system tables) */
 
3561
  join->const_table_map= 0;
 
3562
 
 
3563
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3564
       p_pos < p_end ;
 
3565
       p_pos++)
 
3566
  {
 
3567
    int tmp;
 
3568
    s= p_pos->table;
 
3569
    s->type=JT_SYSTEM;
 
3570
    join->const_table_map|=s->table->map;
 
3571
    if ((tmp=join_read_const_table(s, p_pos)))
 
3572
    {
 
3573
      if (tmp > 0)
 
3574
        return(1);                      // Fatal error
 
3575
    }
 
3576
    else
 
3577
      found_const_table_map|= s->table->map;
 
3578
  }
 
3579
 
 
3580
  /* loop until no more const tables are found */
 
3581
  int ref_changed;
 
3582
  do
 
3583
  {
 
3584
  more_const_tables_found:
 
3585
    ref_changed = 0;
 
3586
    found_ref=0;
 
3587
 
 
3588
    /*
 
3589
      We only have to loop from stat_vector + const_count as
 
3590
      set_position() will move all const_tables first in stat_vector
 
3591
    */
 
3592
 
 
3593
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3594
    {
 
3595
      table=s->table;
 
3596
 
 
3597
      /* 
 
3598
        If equi-join condition by a key is null rejecting and after a
 
3599
        substitution of a const table the key value happens to be null
 
3600
        then we can state that there are no matches for this equi-join.
 
3601
      */  
 
3602
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3603
      {
 
3604
        /* 
 
3605
          When performing an outer join operation if there are no matching rows
 
3606
          for the single row of the outer table all the inner tables are to be
 
3607
          null complemented and thus considered as constant tables.
 
3608
          Here we apply this consideration to the case of outer join operations 
 
3609
          with a single inner table only because the case with nested tables
 
3610
          would require a more thorough analysis.
 
3611
          TODO. Apply single row substitution to null complemented inner tables
 
3612
          for nested outer join operations. 
 
3613
        */              
 
3614
        while (keyuse->table == table)
 
3615
        {
 
3616
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3617
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3618
          {
 
3619
            s->type= JT_CONST;
 
3620
            mark_as_null_row(table);
 
3621
            found_const_table_map|= table->map;
 
3622
            join->const_table_map|= table->map;
 
3623
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3624
            goto more_const_tables_found;
 
3625
           }
 
3626
          keyuse++;
 
3627
        }
 
3628
      }
 
3629
 
 
3630
      if (s->dependent)                         // If dependent on some table
 
3631
      {
 
3632
        // All dep. must be constants
 
3633
        if (s->dependent & ~(found_const_table_map))
 
3634
          continue;
 
3635
        if (table->file->stats.records <= 1L &&
 
3636
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3637
            !table->pos_in_table_list->embedding)
 
3638
        {                                       // system table
 
3639
          int tmp= 0;
 
3640
          s->type=JT_SYSTEM;
 
3641
          join->const_table_map|=table->map;
 
3642
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3643
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3644
          {
 
3645
            if (tmp > 0)
 
3646
              return(1);                        // Fatal error
 
3647
          }
 
3648
          else
 
3649
            found_const_table_map|= table->map;
 
3650
          continue;
 
3651
        }
 
3652
      }
 
3653
      /* check if table can be read by key or table only uses const refs */
 
3654
      if ((keyuse=s->keyuse))
 
3655
      {
 
3656
        s->type= JT_REF;
 
3657
        while (keyuse->table == table)
 
3658
        {
 
3659
          start_keyuse=keyuse;
 
3660
          key=keyuse->key;
 
3661
          s->keys.set_bit(key);               // QQ: remove this ?
 
3662
 
 
3663
          refs=0;
 
3664
          const_ref.clear_all();
 
3665
          eq_part.clear_all();
 
3666
          do
 
3667
          {
 
3668
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3669
            {
 
3670
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3671
                const_ref.set_bit(keyuse->keypart);
 
3672
              else
 
3673
                refs|=keyuse->used_tables;
 
3674
              eq_part.set_bit(keyuse->keypart);
 
3675
            }
 
3676
            keyuse++;
 
3677
          } while (keyuse->table == table && keyuse->key == key);
 
3678
 
 
3679
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3680
              !table->pos_in_table_list->embedding)
 
3681
          {
 
3682
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3683
                 == HA_NOSAME)
 
3684
            {
 
3685
              if (const_ref == eq_part)
 
3686
              {                                 // Found everything for ref.
 
3687
                int tmp;
 
3688
                ref_changed = 1;
 
3689
                s->type= JT_CONST;
 
3690
                join->const_table_map|=table->map;
 
3691
                set_position(join,const_count++,s,start_keyuse);
 
3692
                if (create_ref_for_key(join, s, start_keyuse,
 
3693
                                       found_const_table_map))
 
3694
                  return(1);
 
3695
                if ((tmp=join_read_const_table(s,
 
3696
                                               join->positions+const_count-1)))
 
3697
                {
 
3698
                  if (tmp > 0)
 
3699
                    return(1);                  // Fatal error
 
3700
                }
 
3701
                else
 
3702
                  found_const_table_map|= table->map;
 
3703
                break;
 
3704
              }
 
3705
              else
 
3706
                found_ref|= refs;      // Table is const if all refs are const
 
3707
            }
 
3708
            else if (const_ref == eq_part)
 
3709
              s->const_keys.set_bit(key);
 
3710
          }
 
3711
        }
 
3712
      }
 
3713
    }
 
3714
  } while (join->const_table_map & found_ref && ref_changed);
 
3715
 
 
3716
  /* 
 
3717
    Update info on indexes that can be used for search lookups as
 
3718
    reading const tables may has added new sargable predicates. 
 
3719
  */
 
3720
  if (const_count && sargables)
 
3721
  {
 
3722
    for( ; sargables->field ; sargables++)
 
3723
    {
 
3724
      Field *field= sargables->field;
 
3725
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3726
      key_map possible_keys= field->key_start;
 
3727
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3728
      bool is_const= 1;
 
3729
      for (uint j=0; j < sargables->num_values; j++)
 
3730
        is_const&= sargables->arg_value[j]->const_item();
 
3731
      if (is_const)
 
3732
        join_tab[0].const_keys.merge(possible_keys);
 
3733
    }
 
3734
  }
 
3735
 
 
3736
  if (pull_out_semijoin_tables(join))
 
3737
    return(true);
 
3738
 
 
3739
  /* Calc how many (possible) matched records in each table */
 
3740
 
 
3741
  for (s=stat ; s < stat_end ; s++)
 
3742
  {
 
3743
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3744
    {
 
3745
      /* Only one matching row */
 
3746
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3747
      continue;
 
3748
    }
 
3749
    /* Approximate found rows and time to read them */
 
3750
    s->found_records=s->records=s->table->file->stats.records;
 
3751
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3752
 
 
3753
    /*
 
3754
      Set a max range of how many seeks we can expect when using keys
 
3755
      This is can't be to high as otherwise we are likely to use
 
3756
      table scan.
 
3757
    */
 
3758
    s->worst_seeks= min((double) s->found_records / 10,
 
3759
                        (double) s->read_time*3);
 
3760
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3761
      s->worst_seeks=2.0;
 
3762
 
 
3763
    /*
 
3764
      Add to stat->const_keys those indexes for which all group fields or
 
3765
      all select distinct fields participate in one index.
 
3766
    */
 
3767
    add_group_and_distinct_keys(join, s);
 
3768
 
 
3769
    if (!s->const_keys.is_clear_all() &&
 
3770
        !s->table->pos_in_table_list->embedding)
 
3771
    {
 
3772
      ha_rows records;
 
3773
      SQL_SELECT *select;
 
3774
      select= make_select(s->table, found_const_table_map,
 
3775
                          found_const_table_map,
 
3776
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3777
                          1, &error);
 
3778
      if (!select)
 
3779
        return(1);
 
3780
      records= get_quick_record_count(join->thd, select, s->table,
 
3781
                                      &s->const_keys, join->row_limit);
 
3782
      s->quick=select->quick;
 
3783
      s->needed_reg=select->needed_reg;
 
3784
      select->quick=0;
 
3785
      if (records == 0 && s->table->reginfo.impossible_range)
 
3786
      {
 
3787
        /*
 
3788
          Impossible WHERE or ON expression
 
3789
          In case of ON, we mark that the we match one empty NULL row.
 
3790
          In case of WHERE, don't set found_const_table_map to get the
 
3791
          caller to abort with a zero row result.
 
3792
        */
 
3793
        join->const_table_map|= s->table->map;
 
3794
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3795
        s->type= JT_CONST;
 
3796
        if (*s->on_expr_ref)
 
3797
        {
 
3798
          /* Generate empty row */
 
3799
          s->info= "Impossible ON condition";
 
3800
          found_const_table_map|= s->table->map;
 
3801
          s->type= JT_CONST;
 
3802
          mark_as_null_row(s->table);           // All fields are NULL
 
3803
        }
 
3804
      }
 
3805
      if (records != HA_POS_ERROR)
 
3806
      {
 
3807
        s->found_records=records;
 
3808
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3809
      }
 
3810
      delete select;
 
3811
    }
 
3812
  }
 
3813
 
 
3814
  join->join_tab=stat;
 
3815
  join->map2table=stat_ref;
 
3816
  join->table= join->all_tables=table_vector;
 
3817
  join->const_tables=const_count;
 
3818
  join->found_const_table_map=found_const_table_map;
 
3819
 
 
3820
  /* Find an optimal join order of the non-constant tables. */
 
3821
  if (join->const_tables != join->tables)
 
3822
  {
 
3823
    optimize_keyuse(join, keyuse_array);
 
3824
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3825
      return(true);
 
3826
  }
 
3827
  else
 
3828
  {
 
3829
    memcpy(join->best_positions, join->positions,
 
3830
           sizeof(POSITION)*join->const_tables);
 
3831
    join->best_read=1.0;
 
3832
  }
 
3833
  /* Generate an execution plan from the found optimal join order. */
 
3834
  return(join->thd->killed || get_best_combination(join));
 
3835
}
 
3836
 
 
3837
 
480
3838
/*****************************************************************************
481
3839
  Check with keys are used and with tables references with tables
482
3840
  Updates in stat:
485
3843
          keyuse     Pointer to possible keys
486
3844
*****************************************************************************/
487
3845
 
 
3846
/// Used when finding key fields
 
3847
typedef struct key_field_t {
 
3848
  Field         *field;
 
3849
  Item          *val;                   ///< May be empty if diff constant
 
3850
  uint          level;
 
3851
  uint          optimize; // KEY_OPTIMIZE_*
 
3852
  bool          eq_func;
 
3853
  /**
 
3854
    If true, the condition this struct represents will not be satisfied
 
3855
    when val IS NULL.
 
3856
  */
 
3857
  bool          null_rejecting; 
 
3858
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3859
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3860
} KEY_FIELD;
 
3861
 
 
3862
/**
 
3863
  Merge new key definitions to old ones, remove those not used in both.
 
3864
 
 
3865
  This is called for OR between different levels.
 
3866
 
 
3867
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3868
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3869
  that are internally transformed to something like:.
 
3870
 
 
3871
  @code
 
3872
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3873
  @endcode
 
3874
 
 
3875
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3876
  result has null_rejecting=true if it is set for both ORed references.
 
3877
  for example:
 
3878
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3879
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3880
 
 
3881
  @todo
 
3882
    The result of this is that we're missing some 'ref' accesses.
 
3883
    OptimizerTeam: Fix this
 
3884
*/
 
3885
 
 
3886
static KEY_FIELD *
 
3887
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3888
                 uint and_level)
 
3889
{
 
3890
  if (start == new_fields)
 
3891
    return start;                               // Impossible or
 
3892
  if (new_fields == end)
 
3893
    return start;                               // No new fields, skip all
 
3894
 
 
3895
  KEY_FIELD *first_free=new_fields;
 
3896
 
 
3897
  /* Mark all found fields in old array */
 
3898
  for (; new_fields != end ; new_fields++)
 
3899
  {
 
3900
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3901
    {
 
3902
      if (old->field == new_fields->field)
 
3903
      {
 
3904
        /*
 
3905
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3906
          it can return false where it is feasible to make it return true.
 
3907
          
 
3908
          The cause is as follows: Some of the tables are already known to be
 
3909
          const tables (the detection code is in make_join_statistics(),
 
3910
          above the update_ref_and_keys() call), but we didn't propagate 
 
3911
          information about this: Table::const_table is not set to true, and
 
3912
          Item::update_used_tables() hasn't been called for each item.
 
3913
          The result of this is that we're missing some 'ref' accesses.
 
3914
          TODO: OptimizerTeam: Fix this
 
3915
        */
 
3916
        if (!new_fields->val->const_item())
 
3917
        {
 
3918
          /*
 
3919
            If the value matches, we can use the key reference.
 
3920
            If not, we keep it until we have examined all new values
 
3921
          */
 
3922
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3923
          {
 
3924
            old->level= and_level;
 
3925
            old->optimize= ((old->optimize & new_fields->optimize &
 
3926
                             KEY_OPTIMIZE_EXISTS) |
 
3927
                            ((old->optimize | new_fields->optimize) &
 
3928
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3929
            old->null_rejecting= (old->null_rejecting &&
 
3930
                                  new_fields->null_rejecting);
 
3931
          }
 
3932
        }
 
3933
        else if (old->eq_func && new_fields->eq_func &&
 
3934
                 old->val->eq_by_collation(new_fields->val, 
 
3935
                                           old->field->binary(),
 
3936
                                           old->field->charset()))
 
3937
 
 
3938
        {
 
3939
          old->level= and_level;
 
3940
          old->optimize= ((old->optimize & new_fields->optimize &
 
3941
                           KEY_OPTIMIZE_EXISTS) |
 
3942
                          ((old->optimize | new_fields->optimize) &
 
3943
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3944
          old->null_rejecting= (old->null_rejecting &&
 
3945
                                new_fields->null_rejecting);
 
3946
        }
 
3947
        else if (old->eq_func && new_fields->eq_func &&
 
3948
                 ((old->val->const_item() && old->val->is_null()) || 
 
3949
                  new_fields->val->is_null()))
 
3950
        {
 
3951
          /* field = expression OR field IS NULL */
 
3952
          old->level= and_level;
 
3953
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3954
          /*
 
3955
            Remember the NOT NULL value unless the value does not depend
 
3956
            on other tables.
 
3957
          */
 
3958
          if (!old->val->used_tables() && old->val->is_null())
 
3959
            old->val= new_fields->val;
 
3960
          /* The referred expression can be NULL: */ 
 
3961
          old->null_rejecting= 0;
 
3962
        }
 
3963
        else
 
3964
        {
 
3965
          /*
 
3966
            We are comparing two different const.  In this case we can't
 
3967
            use a key-lookup on this so it's better to remove the value
 
3968
            and let the range optimzier handle it
 
3969
          */
 
3970
          if (old == --first_free)              // If last item
 
3971
            break;
 
3972
          *old= *first_free;                    // Remove old value
 
3973
          old--;                                // Retry this value
 
3974
        }
 
3975
      }
 
3976
    }
 
3977
  }
 
3978
  /* Remove all not used items */
 
3979
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3980
  {
 
3981
    if (old->level != and_level)
 
3982
    {                                           // Not used in all levels
 
3983
      if (old == --first_free)
 
3984
        break;
 
3985
      *old= *first_free;                        // Remove old value
 
3986
      continue;
 
3987
    }
 
3988
    old++;
 
3989
  }
 
3990
  return first_free;
 
3991
}
 
3992
 
 
3993
 
 
3994
/**
 
3995
  Add a possible key to array of possible keys if it's usable as a key
 
3996
 
 
3997
    @param key_fields      Pointer to add key, if usable
 
3998
    @param and_level       And level, to be stored in KEY_FIELD
 
3999
    @param cond            Condition predicate
 
4000
    @param field           Field used in comparision
 
4001
    @param eq_func         True if we used =, <=> or IS NULL
 
4002
    @param value           Value used for comparison with field
 
4003
    @param usable_tables   Tables which can be used for key optimization
 
4004
    @param sargables       IN/OUT Array of found sargable candidates
 
4005
 
 
4006
  @note
 
4007
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4008
    table, we store this to be able to do not exists optimization later.
 
4009
 
 
4010
  @returns
 
4011
    *key_fields is incremented if we stored a key in the array
 
4012
*/
 
4013
 
 
4014
static void
 
4015
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4016
              Field *field, bool eq_func, Item **value, uint num_values,
 
4017
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4018
{
 
4019
  uint exists_optimize= 0;
 
4020
  if (!(field->flags & PART_KEY_FLAG))
 
4021
  {
 
4022
    // Don't remove column IS NULL on a LEFT JOIN table
 
4023
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4024
        !field->table->maybe_null || field->null_ptr)
 
4025
      return;                                   // Not a key. Skip it
 
4026
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4027
    assert(num_values == 1);
 
4028
  }
 
4029
  else
 
4030
  {
 
4031
    table_map used_tables=0;
 
4032
    bool optimizable=0;
 
4033
    for (uint i=0; i<num_values; i++)
 
4034
    {
 
4035
      used_tables|=(value[i])->used_tables();
 
4036
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4037
        optimizable=1;
 
4038
    }
 
4039
    if (!optimizable)
 
4040
      return;
 
4041
    if (!(usable_tables & field->table->map))
 
4042
    {
 
4043
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4044
          !field->table->maybe_null || field->null_ptr)
 
4045
        return;                                 // Can't use left join optimize
 
4046
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4047
    }
 
4048
    else
 
4049
    {
 
4050
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4051
      key_map possible_keys=field->key_start;
 
4052
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4053
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4054
 
 
4055
      /*
 
4056
        Save the following cases:
 
4057
        Field op constant
 
4058
        Field LIKE constant where constant doesn't start with a wildcard
 
4059
        Field = field2 where field2 is in a different table
 
4060
        Field op formula
 
4061
        Field IS NULL
 
4062
        Field IS NOT NULL
 
4063
         Field BETWEEN ...
 
4064
         Field IN ...
 
4065
      */
 
4066
      stat[0].key_dependent|=used_tables;
 
4067
 
 
4068
      bool is_const=1;
 
4069
      for (uint i=0; i<num_values; i++)
 
4070
      {
 
4071
        if (!(is_const&= value[i]->const_item()))
 
4072
          break;
 
4073
      }
 
4074
      if (is_const)
 
4075
        stat[0].const_keys.merge(possible_keys);
 
4076
      else if (!eq_func)
 
4077
      {
 
4078
        /* 
 
4079
          Save info to be able check whether this predicate can be 
 
4080
          considered as sargable for range analisis after reading const tables.
 
4081
          We do not save info about equalities as update_const_equal_items
 
4082
          will take care of updating info on keys from sargable equalities. 
 
4083
        */
 
4084
        (*sargables)--;
 
4085
        (*sargables)->field= field;
 
4086
        (*sargables)->arg_value= value;
 
4087
        (*sargables)->num_values= num_values;
 
4088
      }
 
4089
      /*
 
4090
        We can't always use indexes when comparing a string index to a
 
4091
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4092
        eq_func is NEVER true when num_values > 1
 
4093
       */
 
4094
      if (!eq_func)
 
4095
      {
 
4096
        /* 
 
4097
          Additional optimization: if we're processing
 
4098
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4099
          "t.key = c1".
 
4100
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4101
          There are 2 options:
 
4102
          A) Make equality propagation code be able to handle BETWEEN
 
4103
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4104
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4105
             and use them in equality propagation process (see details in
 
4106
             OptimizerKBAndTodo)
 
4107
        */
 
4108
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4109
            ((Item_func_between*) cond)->negated ||
 
4110
            !value[0]->eq(value[1], field->binary()))
 
4111
          return;
 
4112
        eq_func= true;
 
4113
      }
 
4114
 
 
4115
      if (field->result_type() == STRING_RESULT)
 
4116
      {
 
4117
        if ((*value)->result_type() != STRING_RESULT)
 
4118
        {
 
4119
          if (field->cmp_type() != (*value)->result_type())
 
4120
            return;
 
4121
        }
 
4122
        else
 
4123
        {
 
4124
          /*
 
4125
            We can't use indexes if the effective collation
 
4126
            of the operation differ from the field collation.
 
4127
          */
 
4128
          if (field->cmp_type() == STRING_RESULT &&
 
4129
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4130
            return;
 
4131
        }
 
4132
      }
 
4133
    }
 
4134
  }
 
4135
  /*
 
4136
    For the moment eq_func is always true. This slot is reserved for future
 
4137
    extensions where we want to remembers other things than just eq comparisons
 
4138
  */
 
4139
  assert(eq_func);
 
4140
  /* Store possible eq field */
 
4141
  (*key_fields)->field=         field;
 
4142
  (*key_fields)->eq_func=       eq_func;
 
4143
  (*key_fields)->val=           *value;
 
4144
  (*key_fields)->level=         and_level;
 
4145
  (*key_fields)->optimize=      exists_optimize;
 
4146
  /*
 
4147
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4148
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4149
    has NULL value.
 
4150
    We use null_rejecting in add_not_null_conds() to add
 
4151
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4152
  */
 
4153
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4154
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4155
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4156
                                  ((Item_field*)*value)->field->maybe_null());
 
4157
  (*key_fields)->cond_guard= NULL;
 
4158
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4159
                              cond->name < subq_sj_cond_name + 64)? 
 
4160
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4161
  (*key_fields)++;
 
4162
}
 
4163
 
 
4164
/**
 
4165
  Add possible keys to array of possible keys originated from a simple
 
4166
  predicate.
 
4167
 
 
4168
    @param  key_fields     Pointer to add key, if usable
 
4169
    @param  and_level      And level, to be stored in KEY_FIELD
 
4170
    @param  cond           Condition predicate
 
4171
    @param  field          Field used in comparision
 
4172
    @param  eq_func        True if we used =, <=> or IS NULL
 
4173
    @param  value          Value used for comparison with field
 
4174
                           Is NULL for BETWEEN and IN    
 
4175
    @param  usable_tables  Tables which can be used for key optimization
 
4176
    @param  sargables      IN/OUT Array of found sargable candidates
 
4177
 
 
4178
  @note
 
4179
    If field items f1 and f2 belong to the same multiple equality and
 
4180
    a key is added for f1, the the same key is added for f2.
 
4181
 
 
4182
  @returns
 
4183
    *key_fields is incremented if we stored a key in the array
 
4184
*/
 
4185
 
 
4186
static void
 
4187
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
 
4188
                     Item_func *cond, Item_field *field_item,
 
4189
                     bool eq_func, Item **val,
 
4190
                     uint num_values, table_map usable_tables,
 
4191
                     SARGABLE_PARAM **sargables)
 
4192
{
 
4193
  Field *field= field_item->field;
 
4194
  add_key_field(key_fields, and_level, cond, field,
 
4195
                eq_func, val, num_values, usable_tables, sargables);
 
4196
  Item_equal *item_equal= field_item->item_equal;
 
4197
  if (item_equal)
 
4198
  { 
 
4199
    /*
 
4200
      Add to the set of possible key values every substitution of
 
4201
      the field for an equal field included into item_equal
 
4202
    */
 
4203
    Item_equal_iterator it(*item_equal);
 
4204
    Item_field *item;
 
4205
    while ((item= it++))
 
4206
    {
 
4207
      if (!field->eq(item->field))
 
4208
      {
 
4209
        add_key_field(key_fields, and_level, cond, item->field,
 
4210
                      eq_func, val, num_values, usable_tables,
 
4211
                      sargables);
 
4212
      }
 
4213
    }
 
4214
  }
 
4215
}
 
4216
 
 
4217
static void
 
4218
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
 
4219
               COND *cond, table_map usable_tables,
 
4220
               SARGABLE_PARAM **sargables)
 
4221
{
 
4222
  if (cond->type() == Item_func::COND_ITEM)
 
4223
  {
 
4224
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4225
    KEY_FIELD *org_key_fields= *key_fields;
 
4226
 
 
4227
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4228
    {
 
4229
      Item *item;
 
4230
      while ((item=li++))
 
4231
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4232
                       sargables);
 
4233
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4234
        org_key_fields->level= *and_level;
 
4235
    }
 
4236
    else
 
4237
    {
 
4238
      (*and_level)++;
 
4239
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4240
                     sargables);
 
4241
      Item *item;
 
4242
      while ((item=li++))
 
4243
      {
 
4244
        KEY_FIELD *start_key_fields= *key_fields;
 
4245
        (*and_level)++;
 
4246
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4247
                       sargables);
 
4248
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4249
                                     *key_fields,++(*and_level));
 
4250
      }
 
4251
    }
 
4252
    return;
 
4253
  }
 
4254
 
 
4255
  /* 
 
4256
    Subquery optimization: Conditions that are pushed down into subqueries
 
4257
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4258
    but need to set cond_guard for KEYUSE elements generated from it.
 
4259
  */
 
4260
  {
 
4261
    if (cond->type() == Item::FUNC_ITEM &&
 
4262
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4263
    {
 
4264
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4265
      if (!join->group_list && !join->order &&
 
4266
          join->unit->item && 
 
4267
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4268
          !join->unit->is_union())
 
4269
      {
 
4270
        KEY_FIELD *save= *key_fields;
 
4271
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4272
                       sargables);
 
4273
        // Indicate that this ref access candidate is for subquery lookup:
 
4274
        for (; save != *key_fields; save++)
 
4275
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4276
      }
 
4277
      return;
 
4278
    }
 
4279
  }
 
4280
 
 
4281
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4282
  if (cond->type() != Item::FUNC_ITEM)
 
4283
    return;
 
4284
  Item_func *cond_func= (Item_func*) cond;
 
4285
  switch (cond_func->select_optimize()) {
 
4286
  case Item_func::OPTIMIZE_NONE:
 
4287
    break;
 
4288
  case Item_func::OPTIMIZE_KEY:
 
4289
  {
 
4290
    Item **values;
 
4291
    // BETWEEN, IN, NE
 
4292
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4293
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4294
    {
 
4295
      values= cond_func->arguments()+1;
 
4296
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4297
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4298
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4299
        values--;
 
4300
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4301
                  cond_func->argument_count() != 2);
 
4302
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4303
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4304
                           0, values, 
 
4305
                           cond_func->argument_count()-1,
 
4306
                           usable_tables, sargables);
 
4307
    }
 
4308
    if (cond_func->functype() == Item_func::BETWEEN)
 
4309
    {
 
4310
      values= cond_func->arguments();
 
4311
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
 
4312
      {
 
4313
        Item_field *field_item;
 
4314
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4315
            &&
 
4316
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4317
        {
 
4318
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4319
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4320
                               field_item, 0, values, 1, usable_tables, 
 
4321
                               sargables);
 
4322
        }
 
4323
      }  
 
4324
    }
 
4325
    break;
 
4326
  }
 
4327
  case Item_func::OPTIMIZE_OP:
 
4328
  {
 
4329
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4330
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4331
 
 
4332
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4333
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4334
    {
 
4335
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4336
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4337
                           equal_func,
 
4338
                           cond_func->arguments()+1, 1, usable_tables,
 
4339
                           sargables);
 
4340
    }
 
4341
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4342
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4343
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4344
    {
 
4345
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4346
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4347
                           equal_func,
 
4348
                           cond_func->arguments(),1,usable_tables,
 
4349
                           sargables);
 
4350
    }
 
4351
    break;
 
4352
  }
 
4353
  case Item_func::OPTIMIZE_NULL:
 
4354
    /* column_name IS [NOT] NULL */
 
4355
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4356
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4357
    {
 
4358
      Item *tmp=new Item_null;
 
4359
      if (unlikely(!tmp))                       // Should never be true
 
4360
        return;
 
4361
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4362
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4363
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4364
                           &tmp, 1, usable_tables, sargables);
 
4365
    }
 
4366
    break;
 
4367
  case Item_func::OPTIMIZE_EQUAL:
 
4368
    Item_equal *item_equal= (Item_equal *) cond;
 
4369
    Item *const_item= item_equal->get_const();
 
4370
    Item_equal_iterator it(*item_equal);
 
4371
    Item_field *item;
 
4372
    if (const_item)
 
4373
    {
 
4374
      /*
 
4375
        For each field field1 from item_equal consider the equality 
 
4376
        field1=const_item as a condition allowing an index access of the table
 
4377
        with field1 by the keys value of field1.
 
4378
      */   
 
4379
      while ((item= it++))
 
4380
      {
 
4381
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4382
                      true, &const_item, 1, usable_tables, sargables);
 
4383
      }
 
4384
    }
 
4385
    else 
 
4386
    {
 
4387
      /*
 
4388
        Consider all pairs of different fields included into item_equal.
 
4389
        For each of them (field1, field1) consider the equality 
 
4390
        field1=field2 as a condition allowing an index access of the table
 
4391
        with field1 by the keys value of field2.
 
4392
      */   
 
4393
      Item_equal_iterator fi(*item_equal);
 
4394
      while ((item= fi++))
 
4395
      {
 
4396
        Field *field= item->field;
 
4397
        while ((item= it++))
 
4398
        {
 
4399
          if (!field->eq(item->field))
 
4400
          {
 
4401
            add_key_field(key_fields, *and_level, cond_func, field,
 
4402
                          true, (Item **) &item, 1, usable_tables,
 
4403
                          sargables);
 
4404
          }
 
4405
        }
 
4406
        it.rewind();
 
4407
      }
 
4408
    }
 
4409
    break;
 
4410
  }
 
4411
}
488
4412
 
489
4413
/**
490
4414
  Add all keys with uses 'field' for some keypart.
491
4415
 
492
4416
  If field->and_level != and_level then only mark key_part as const_part.
493
4417
*/
494
 
uint32_t max_part_bit(key_part_map bits)
 
4418
 
 
4419
static uint
 
4420
max_part_bit(key_part_map bits)
495
4421
{
496
 
  uint32_t found;
 
4422
  uint found;
497
4423
  for (found=0; bits & 1 ; found++,bits>>=1) ;
498
4424
  return found;
499
4425
}
500
4426
 
501
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4427
static void
 
4428
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4429
{
 
4430
  Field *field=key_field->field;
 
4431
  Table *form= field->table;
 
4432
  KEYUSE keyuse;
 
4433
 
 
4434
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4435
  {
 
4436
    for (uint key= 0 ; key < form->sizeKeys() ; key++)
 
4437
    {
 
4438
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4439
        continue;
 
4440
 
 
4441
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4442
      for (uint part=0 ; part <  key_parts ; part++)
 
4443
      {
 
4444
        if (field->eq(form->key_info[key].key_part[part].field))
 
4445
        {
 
4446
          keyuse.table= field->table;
 
4447
          keyuse.val =  key_field->val;
 
4448
          keyuse.key =  key;
 
4449
          keyuse.keypart=part;
 
4450
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4451
          keyuse.used_tables=key_field->val->used_tables();
 
4452
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4453
          keyuse.null_rejecting= key_field->null_rejecting;
 
4454
          keyuse.cond_guard= key_field->cond_guard;
 
4455
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4456
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
 
4457
        }
 
4458
      }
 
4459
    }
 
4460
  }
 
4461
}
 
4462
 
 
4463
static int
 
4464
sort_keyuse(KEYUSE *a,KEYUSE *b)
502
4465
{
503
4466
  int res;
504
 
  if (a->getTable()->tablenr != b->getTable()->tablenr)
505
 
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
506
 
  if (a->getKey() != b->getKey())
507
 
    return static_cast<int>((a->getKey() - b->getKey()));
508
 
  if (a->getKeypart() != b->getKeypart())
509
 
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
4467
  if (a->table->tablenr != b->table->tablenr)
 
4468
    return (int) (a->table->tablenr - b->table->tablenr);
 
4469
  if (a->key != b->key)
 
4470
    return (int) (a->key - b->key);
 
4471
  if (a->keypart != b->keypart)
 
4472
    return (int) (a->keypart - b->keypart);
510
4473
  // Place const values before other ones
511
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
512
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4474
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4475
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
513
4476
    return res;
514
4477
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
515
 
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
516
 
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
 
4478
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4479
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4480
}
 
4481
 
 
4482
 
 
4483
/*
 
4484
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4485
 
 
4486
    This function populates KEY_FIELD array with entries generated from the 
 
4487
    ON condition of the given nested join, and does the same for nested joins 
 
4488
    contained within this nested join.
 
4489
 
 
4490
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4491
  @param[in,out]  end                 End of the key field array
 
4492
  @param[in,out]  and_level           And-level
 
4493
  @param[in,out]  sargables           Array of found sargable candidates
 
4494
 
 
4495
 
 
4496
  @note
 
4497
    We can add accesses to the tables that are direct children of this nested 
 
4498
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4499
    
 
4500
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4501
    invoked for):
 
4502
    @code
 
4503
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4504
    @endcode
 
4505
    Example for #2:
 
4506
    @code
 
4507
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4508
    @endcode
 
4509
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4510
    t1 only.
 
4511
    Example #3:
 
4512
    @code
 
4513
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4514
    @endcode
 
4515
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4516
*/
 
4517
 
 
4518
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4519
                                  KEY_FIELD **end, uint *and_level,
 
4520
                                  SARGABLE_PARAM **sargables)
 
4521
{
 
4522
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4523
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4524
  bool have_another = false;
 
4525
  table_map tables= 0;
 
4526
  TableList *table;
 
4527
  assert(nested_join_table->nested_join);
 
4528
 
 
4529
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4530
                                            (table= li++))))
 
4531
  {
 
4532
    if (table->nested_join)
 
4533
    {
 
4534
      if (!table->on_expr)
 
4535
      {
 
4536
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4537
        have_another= true;
 
4538
        li2= li;
 
4539
        li= List_iterator<TableList>(table->nested_join->join_list); 
 
4540
      }
 
4541
      else
 
4542
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4543
    }
 
4544
    else
 
4545
      if (!table->on_expr)
 
4546
        tables |= table->table->map;
 
4547
  }
 
4548
  if (nested_join_table->on_expr)
 
4549
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4550
                   sargables);
517
4551
}
518
4552
 
519
4553
 
520
4554
/**
521
4555
  Update keyuse array with all possible keys we can use to fetch rows.
522
 
 
523
 
  @param       session
524
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4556
  
 
4557
  @param       thd 
 
4558
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
525
4559
  @param       join_tab       Array in tablenr_order
526
4560
  @param       tables         Number of tables in join
527
4561
  @param       cond           WHERE condition (note that the function analyzes
530
4564
                              for which we can make ref access based the WHERE
531
4565
                              clause)
532
4566
  @param       select_lex     current SELECT
533
 
  @param[out]  sargables      std::vector of found sargable candidates
534
 
 
 
4567
  @param[out]  sargables      Array of found sargable candidates
 
4568
      
535
4569
   @retval
536
4570
     0  OK
537
4571
   @retval
538
4572
     1  Out of memory.
539
4573
*/
540
 
bool update_ref_and_keys(Session *session,
541
 
                         DYNAMIC_ARRAY *keyuse,
542
 
                         JoinTable *join_tab,
543
 
                         uint32_t tables,
544
 
                         COND *cond, 
545
 
                         COND_EQUAL *,
546
 
                         table_map normal_tables,
547
 
                         Select_Lex *select_lex,
548
 
                         vector<optimizer::SargableParam> &sargables)
 
4574
 
 
4575
static bool
 
4576
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4577
                    uint tables, COND *cond,
 
4578
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4579
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4580
                    SARGABLE_PARAM **sargables)
549
4581
{
550
 
  uint  and_level,found_eq_constant;
551
 
  optimizer::KeyField *key_fields, *end, *field;
552
 
  uint32_t sz;
553
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
554
 
 
555
 
  /*
556
 
    All predicates that are used to fill arrays of KeyField
557
 
    and SargableParam classes have at most 2 arguments
558
 
    except BETWEEN predicates that have 3 arguments and
 
4582
  uint  and_level,i,found_eq_constant;
 
4583
  KEY_FIELD *key_fields, *end, *field;
 
4584
  uint sz;
 
4585
  uint m= max(select_lex->max_equal_elems,(uint32_t)1);
 
4586
  
 
4587
  /* 
 
4588
    We use the same piece of memory to store both  KEY_FIELD 
 
4589
    and SARGABLE_PARAM structure.
 
4590
    KEY_FIELD values are placed at the beginning this memory
 
4591
    while  SARGABLE_PARAM values are put at the end.
 
4592
    All predicates that are used to fill arrays of KEY_FIELD
 
4593
    and SARGABLE_PARAM structures have at most 2 arguments
 
4594
    except BETWEEN predicates that have 3 arguments and 
559
4595
    IN predicates.
560
 
    This any predicate if it's not BETWEEN/IN can be used
561
 
    directly to fill at most 2 array elements, either of KeyField 
562
 
    or SargableParam type. For a BETWEEN predicate 3 elements
 
4596
    This any predicate if it's not BETWEEN/IN can be used 
 
4597
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4598
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
563
4599
    can be filled as this predicate is considered as
564
4600
    saragable with respect to each of its argument.
565
4601
    An IN predicate can require at most 1 element as currently
566
4602
    it is considered as sargable only for its first argument.
567
4603
    Multiple equality can add  elements that are filled after
568
4604
    substitution of field arguments by equal fields. There
569
 
    can be not more than select_lex->max_equal_elems such
 
4605
    can be not more than select_lex->max_equal_elems such 
570
4606
    substitutions.
571
 
  */
572
 
  sz= sizeof(optimizer::KeyField) *
573
 
      (((session->lex->current_select->cond_count+1)*2 +
574
 
        session->lex->current_select->between_count)*m+1);
575
 
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
576
 
    return true;
 
4607
  */ 
 
4608
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4609
      (((thd->lex->current_select->cond_count+1)*2 +
 
4610
        thd->lex->current_select->between_count)*m+1);
 
4611
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4612
    return true; /* purecov: inspected */
577
4613
  and_level= 0;
578
4614
  field= end= key_fields;
 
4615
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4616
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4617
  /* set a barrier for the array of SARGABLE_PARAM */
 
4618
  (*sargables)[0].field= 0; 
579
4619
 
580
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4620
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
581
4621
    return true;
582
4622
  if (cond)
583
4623
  {
584
4624
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
585
4625
                   sargables);
586
 
    for (; field != end; field++)
 
4626
    for (; field != end ; field++)
587
4627
    {
588
 
      add_key_part(keyuse, field);
 
4628
      add_key_part(keyuse,field);
589
4629
      /* Mark that we can optimize LEFT JOIN */
590
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
591
 
          ! field->getField()->real_maybe_null())
592
 
      {
593
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
594
 
      }
 
4630
      if (field->val->type() == Item::NULL_ITEM &&
 
4631
          !field->field->real_maybe_null())
 
4632
        field->field->table->reginfo.not_exists_optimize=1;
595
4633
    }
596
4634
  }
597
 
  for (uint32_t i= 0; i < tables; i++)
 
4635
  for (i=0 ; i < tables ; i++)
598
4636
  {
599
4637
    /*
600
4638
      Block the creation of keys for inner tables of outer joins.
604
4642
      In the future when we introduce conditional accesses
605
4643
      for inner tables in outer joins these keys will be taken
606
4644
      into account as well.
607
 
    */
 
4645
    */ 
608
4646
    if (*join_tab[i].on_expr_ref)
609
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4647
      add_key_fields(join_tab->join, &end, &and_level, 
610
4648
                     *join_tab[i].on_expr_ref,
611
4649
                     join_tab[i].table->map, sargables);
612
4650
  }
617
4655
    TableList *table;
618
4656
    while ((table= li++))
619
4657
    {
620
 
      if (table->getNestedJoin())
621
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4658
      if (table->nested_join)
 
4659
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
622
4660
                              sargables);
623
4661
    }
624
4662
  }
638
4676
  */
639
4677
  if (keyuse->elements)
640
4678
  {
641
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4679
    KEYUSE key_end,*prev,*save_pos,*use;
642
4680
 
643
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
644
 
                       (qsort_cmp) sort_keyuse);
 
4681
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4682
          (qsort_cmp) sort_keyuse);
645
4683
 
646
4684
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
647
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
4685
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
648
4686
 
649
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4687
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
650
4688
    prev= &key_end;
651
 
    found_eq_constant= 0;
 
4689
    found_eq_constant=0;
 
4690
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
652
4691
    {
653
 
      uint32_t i;
654
 
 
655
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4692
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4693
        use->table->const_key_parts[use->key]|= use->keypart_map;
656
4694
      {
657
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
658
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
659
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
660
 
        {
661
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
662
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
663
 
            continue;                           /* remove */
664
 
        }
665
 
        else if (use->getKeypart() != 0)                // First found must be 0
666
 
          continue;
 
4695
        if (use->key == prev->key && use->table == prev->table)
 
4696
        {
 
4697
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4698
            continue;                           /* remove */
 
4699
        }
 
4700
        else if (use->keypart != 0)             // First found must be 0
 
4701
          continue;
 
4702
      }
667
4703
 
668
 
#ifdef HAVE_VALGRIND
669
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
670
 
        if (save_pos != use)
 
4704
#ifdef HAVE_purify
 
4705
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4706
      if (save_pos != use)
671
4707
#endif
672
 
          *save_pos= *use;
673
 
        prev=use;
674
 
        found_eq_constant= ! use->getUsedTables();
675
 
        /* Save ptr to first use */
676
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
677
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
678
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
679
 
        save_pos++;
680
 
      }
681
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
682
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
683
 
      keyuse->elements= i;
 
4708
        *save_pos= *use;
 
4709
      prev=use;
 
4710
      found_eq_constant= !use->used_tables;
 
4711
      /* Save ptr to first use */
 
4712
      if (!use->table->reginfo.join_tab->keyuse)
 
4713
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4714
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4715
      save_pos++;
684
4716
    }
 
4717
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4718
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
 
4719
    keyuse->elements=i;
685
4720
  }
686
4721
  return false;
687
4722
}
689
4724
/**
690
4725
  Update some values in keyuse for faster choose_plan() loop.
691
4726
*/
692
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4727
 
 
4728
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
693
4729
{
694
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
695
 
                                                  0, 
696
 
                                                  optimizer::KeyUse*);
 
4730
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
697
4731
 
698
4732
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
699
4733
  {
706
4740
      Constant tables are ignored.
707
4741
      To avoid bad matches, we don't make ref_table_rows less than 100.
708
4742
    */
709
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
710
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4743
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4744
    if (keyuse->used_tables &
 
4745
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4746
               ~OUTER_REF_TABLE_BIT)))
711
4747
    {
712
 
      uint32_t tablenr;
 
4748
      uint tablenr;
713
4749
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
714
4750
      if (map == 1)                     // Only one table
715
4751
      {
716
 
        Table *tmp_table=join->all_tables[tablenr];
717
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4752
        Table *tmp_table=join->all_tables[tablenr];
 
4753
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, (ha_rows)100);
718
4754
      }
719
4755
    }
720
4756
    /*
721
4757
      Outer reference (external field) is constant for single executing
722
4758
      of subquery
723
4759
    */
724
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
725
 
      keyuse->setTableRows(1);
 
4760
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4761
      keyuse->ref_table_rows= 1;
726
4762
  }
727
4763
}
728
4764
 
744
4780
  @return
745
4781
    None
746
4782
*/
747
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4783
 
 
4784
static void
 
4785
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
748
4786
{
749
4787
  List<Item_field> indexed_fields;
750
4788
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
751
 
  Order      *cur_group;
 
4789
  order_st      *cur_group;
752
4790
  Item_field *cur_item;
753
4791
  key_map possible_keys(0);
754
4792
 
756
4794
  { /* Collect all query fields referenced in the GROUP clause. */
757
4795
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
758
4796
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
759
 
                               (unsigned char*) &indexed_fields);
 
4797
                               (uchar*) &indexed_fields);
760
4798
  }
761
4799
  else if (join->select_distinct)
762
4800
  { /* Collect all query fields referenced in the SELECT clause. */
765
4803
    Item *item;
766
4804
    while ((item= select_items_it++))
767
4805
      item->walk(&Item::collect_item_field_processor, 0,
768
 
                 (unsigned char*) &indexed_fields);
 
4806
                 (uchar*) &indexed_fields);
769
4807
  }
770
4808
  else
771
4809
    return;
775
4813
 
776
4814
  /* Intersect the keys of all group fields. */
777
4815
  cur_item= indexed_fields_it++;
778
 
  possible_keys|= cur_item->field->part_of_key;
 
4816
  possible_keys.merge(cur_item->field->part_of_key);
779
4817
  while ((cur_item= indexed_fields_it++))
780
4818
  {
781
 
    possible_keys&= cur_item->field->part_of_key;
782
 
  }
783
 
 
784
 
  if (possible_keys.any())
785
 
    join_tab->const_keys|= possible_keys;
786
 
}
787
 
 
788
 
/**
789
 
  Compare two JoinTable objects based on the number of accessed records.
790
 
 
791
 
  @param ptr1 pointer to first JoinTable object
792
 
  @param ptr2 pointer to second JoinTable object
 
4819
    possible_keys.intersect(cur_item->field->part_of_key);
 
4820
  }
 
4821
 
 
4822
  if (!possible_keys.is_clear_all())
 
4823
    join_tab->const_keys.merge(possible_keys);
 
4824
}
 
4825
 
 
4826
 
 
4827
/*****************************************************************************
 
4828
  Go through all combinations of not marked tables and find the one
 
4829
  which uses least records
 
4830
*****************************************************************************/
 
4831
 
 
4832
/** Save const tables first as used tables. */
 
4833
 
 
4834
static void
 
4835
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
 
4836
{
 
4837
  join->positions[idx].table= table;
 
4838
  join->positions[idx].key=key;
 
4839
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4840
  join->positions[idx].ref_depend_map= 0;
 
4841
 
 
4842
  /* Move the const table as down as possible in best_ref */
 
4843
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4844
  JOIN_TAB *next=join->best_ref[idx];
 
4845
  for (;next != table ; pos++)
 
4846
  {
 
4847
    JOIN_TAB *tmp=pos[0];
 
4848
    pos[0]=next;
 
4849
    next=tmp;
 
4850
  }
 
4851
  join->best_ref[idx]=table;
 
4852
}
 
4853
 
 
4854
 
 
4855
/*
 
4856
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4857
 
 
4858
  SYNOPSIS
 
4859
    get_bound_sj_equalities()
 
4860
      sj_nest           Semi-join nest
 
4861
      remaining_tables  Tables that are not yet bound
 
4862
 
 
4863
  DESCRIPTION
 
4864
    Given a semi-join nest, find out which of the IN-equalities have their
 
4865
    left part expression bound (i.e. the said expression doesn't refer to
 
4866
    any of remaining_tables and can be evaluated).
 
4867
 
 
4868
  RETURN
 
4869
    Bitmap of bound IN-equalities.
 
4870
*/
 
4871
 
 
4872
uint64_t get_bound_sj_equalities(TableList *sj_nest, 
 
4873
                                  table_map remaining_tables)
 
4874
{
 
4875
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4876
  Item *item;
 
4877
  uint i= 0;
 
4878
  uint64_t res= 0;
 
4879
  while ((item= li++))
 
4880
  {
 
4881
    /*
 
4882
      Q: should this take into account equality propagation and how?
 
4883
      A: If e->outer_side is an Item_field, walk over the equality
 
4884
         class and see if there is an element that is bound?
 
4885
      (this is an optional feature)
 
4886
    */
 
4887
    if (!(item->used_tables() & remaining_tables))
 
4888
    {
 
4889
      res |= 1ULL < i;
 
4890
    }
 
4891
  }
 
4892
  return res;
 
4893
}
 
4894
 
 
4895
 
 
4896
/**
 
4897
  Find the best access path for an extension of a partial execution
 
4898
  plan and add this path to the plan.
 
4899
 
 
4900
  The function finds the best access path to table 's' from the passed
 
4901
  partial plan where an access path is the general term for any means to
 
4902
  access the data in 's'. An access path may use either an index or a scan,
 
4903
  whichever is cheaper. The input partial plan is passed via the array
 
4904
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4905
  cost are stored in 'join->positions[idx]'.
 
4906
 
 
4907
  @param join             pointer to the structure providing all context info
 
4908
                          for the query
 
4909
  @param s                the table to be joined by the function
 
4910
  @param thd              thread for the connection that submitted the query
 
4911
  @param remaining_tables set of tables not included into the partial plan yet
 
4912
  @param idx              the length of the partial plan
 
4913
  @param record_count     estimate for the number of records returned by the
 
4914
                          partial plan
 
4915
  @param read_time        the cost of the partial plan
 
4916
 
 
4917
  @return
 
4918
    None
 
4919
*/
 
4920
 
 
4921
static void
 
4922
best_access_path(JOIN      *join,
 
4923
                 JOIN_TAB  *s,
 
4924
                 THD       *thd,
 
4925
                 table_map remaining_tables,
 
4926
                 uint      idx,
 
4927
                 double    record_count,
 
4928
                 double    read_time __attribute__((unused)))
 
4929
{
 
4930
  KEYUSE *best_key=         0;
 
4931
  uint best_max_key_part=   0;
 
4932
  bool found_constraint= 0;
 
4933
  double best=              DBL_MAX;
 
4934
  double best_time=         DBL_MAX;
 
4935
  double records=           DBL_MAX;
 
4936
  table_map best_ref_depends_map= 0;
 
4937
  double tmp;
 
4938
  ha_rows rec;
 
4939
  uint best_is_sj_inside_out=    0;
 
4940
 
 
4941
  if (s->keyuse)
 
4942
  {                                            /* Use key if possible */
 
4943
    Table *table= s->table;
 
4944
    KEYUSE *keyuse,*start_key=0;
 
4945
    double best_records= DBL_MAX;
 
4946
    uint32_t max_key_part=0;
 
4947
    uint64_t bound_sj_equalities= 0;
 
4948
    bool try_sj_inside_out= false;
 
4949
    /*
 
4950
      Discover the bound equalites. We need to do this, if
 
4951
        1. The next table is an SJ-inner table, and
 
4952
        2. It is the first table from that semijoin, and
 
4953
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4954
           all or none of their tables in join_table_map), except
 
4955
           s->emb_sj_nest (which we've just entered).
 
4956
        3. All correlation references from this sj-nest are bound
 
4957
    */
 
4958
    if (s->emb_sj_nest &&                                                 // (1)
 
4959
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4960
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4961
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4962
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4963
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4964
    {
 
4965
      /* This table is an InsideOut scan candidate */
 
4966
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4967
                                                   remaining_tables);
 
4968
      try_sj_inside_out= true;
 
4969
    }
 
4970
 
 
4971
    /* Test how we can use keys */
 
4972
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4973
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4974
    {
 
4975
      key_part_map found_part= 0;
 
4976
      table_map found_ref= 0;
 
4977
      uint key= keyuse->key;
 
4978
      KEY *keyinfo= table->key_info+key;
 
4979
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4980
      key_part_map const_part= 0;
 
4981
      /* The or-null keypart in ref-or-null access: */
 
4982
      key_part_map ref_or_null_part= 0;
 
4983
 
 
4984
      /* Calculate how many key segments of the current key we can use */
 
4985
      start_key= keyuse;
 
4986
      uint64_t handled_sj_equalities=0;
 
4987
      key_part_map sj_insideout_map= 0;
 
4988
 
 
4989
      do /* For each keypart */
 
4990
      {
 
4991
        uint keypart= keyuse->keypart;
 
4992
        table_map best_part_found_ref= 0;
 
4993
        double best_prev_record_reads= DBL_MAX;
 
4994
        
 
4995
        do /* For each way to access the keypart */
 
4996
        {
 
4997
 
 
4998
          /*
 
4999
            if 1. expression doesn't refer to forward tables
 
5000
               2. we won't get two ref-or-null's
 
5001
          */
 
5002
          if (!(remaining_tables & keyuse->used_tables) &&
 
5003
              !(ref_or_null_part && (keyuse->optimize &
 
5004
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5005
          {
 
5006
            found_part|= keyuse->keypart_map;
 
5007
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5008
              const_part|= keyuse->keypart_map;
 
5009
 
 
5010
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5011
                                                      keyuse->used_tables));
 
5012
            if (tmp2 < best_prev_record_reads)
 
5013
            {
 
5014
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5015
              best_prev_record_reads= tmp2;
 
5016
            }
 
5017
            if (rec > keyuse->ref_table_rows)
 
5018
              rec= keyuse->ref_table_rows;
 
5019
            /*
 
5020
              If there is one 'key_column IS NULL' expression, we can
 
5021
              use this ref_or_null optimisation of this field
 
5022
            */
 
5023
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5024
              ref_or_null_part |= keyuse->keypart_map;
 
5025
          }
 
5026
 
 
5027
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5028
          {
 
5029
            if (!(remaining_tables & keyuse->used_tables))
 
5030
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5031
            else
 
5032
            {
 
5033
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5034
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5035
            }
 
5036
          }
 
5037
 
 
5038
          keyuse++;
 
5039
        } while (keyuse->table == table && keyuse->key == key &&
 
5040
                 keyuse->keypart == keypart);
 
5041
        found_ref|= best_part_found_ref;
 
5042
      } while (keyuse->table == table && keyuse->key == key);
 
5043
 
 
5044
      /*
 
5045
        Assume that that each key matches a proportional part of table.
 
5046
      */
 
5047
      if (!found_part && !handled_sj_equalities)
 
5048
        continue;                               // Nothing usable found
 
5049
 
 
5050
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5051
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5052
 
 
5053
      bool sj_inside_out_scan= false;
 
5054
      {
 
5055
        found_constraint= 1;
 
5056
        /*
 
5057
          Check if InsideOut scan is applicable:
 
5058
          1. All IN-equalities are either "bound" or "handled"
 
5059
          2. Index keyparts are 
 
5060
             ...
 
5061
        */
 
5062
        if (try_sj_inside_out && 
 
5063
            table->covering_keys.is_set(key) &&
 
5064
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5065
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5066
        {
 
5067
          uint n_fixed_parts= max_part_bit(found_part);
 
5068
          if (n_fixed_parts != keyinfo->key_parts &&
 
5069
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5070
               PREV_BITS(uint, keyinfo->key_parts))
 
5071
          {
 
5072
            /*
 
5073
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5074
              check if all of them are covered.
 
5075
            */
 
5076
            sj_inside_out_scan= true;
 
5077
            if (!n_fixed_parts)
 
5078
            {
 
5079
              /*
 
5080
                It's a confluent ref scan.
 
5081
 
 
5082
                That is, all found KEYUSE elements refer to IN-equalities,
 
5083
                and there is really no ref access because there is no
 
5084
                  t.keypart0 = {bound expression}
 
5085
 
 
5086
                Calculate the cost of complete loose index scan.
 
5087
              */
 
5088
              records= (double)s->table->file->stats.records;
 
5089
 
 
5090
              /* The cost is entire index scan cost (divided by 2) */
 
5091
              best_time= s->table->file->index_only_read_time(key, records);
 
5092
 
 
5093
              /* Now figure how many different keys we will get */
 
5094
              ulong rpc;
 
5095
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5096
                records= records / rpc;
 
5097
              start_key= NULL;
 
5098
            }
 
5099
          }
 
5100
        }
 
5101
 
 
5102
        /*
 
5103
          Check if we found full key
 
5104
        */
 
5105
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5106
            !ref_or_null_part)
 
5107
        {                                         /* use eq key */
 
5108
          max_key_part= UINT32_MAX;
 
5109
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5110
          {
 
5111
            tmp = prev_record_reads(join, idx, found_ref);
 
5112
            records=1.0;
 
5113
          }
 
5114
          else
 
5115
          {
 
5116
            if (!found_ref)
 
5117
            {                                     /* We found a const key */
 
5118
              /*
 
5119
                ReuseRangeEstimateForRef-1:
 
5120
                We get here if we've found a ref(const) (c_i are constants):
 
5121
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5122
                
 
5123
                If range optimizer was able to construct a "range" 
 
5124
                access on this index, then its condition "quick_cond" was
 
5125
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5126
                from the range optimizer.
 
5127
                
 
5128
                Proof of (*): By properties of range and ref optimizers 
 
5129
                quick_cond will be equal or tighther than ref_const_cond. 
 
5130
                ref_const_cond already covers "smallest" possible interval - 
 
5131
                a singlepoint interval over all keyparts. Therefore, 
 
5132
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5133
                empty interval we wouldn't have got here).
 
5134
              */
 
5135
              if (table->quick_keys.is_set(key))
 
5136
                records= (double) table->quick_rows[key];
 
5137
              else
 
5138
              {
 
5139
                /* quick_range couldn't use key! */
 
5140
                records= (double) s->records/rec;
 
5141
              }
 
5142
            }
 
5143
            else
 
5144
            {
 
5145
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5146
              {                                   /* Prefer longer keys */
 
5147
                records=
 
5148
                  ((double) s->records / (double) rec *
 
5149
                   (1.0 +
 
5150
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5151
                     (double) table->s->max_key_length)));
 
5152
                if (records < 2.0)
 
5153
                  records=2.0;               /* Can't be as good as a unique */
 
5154
              }
 
5155
              /*
 
5156
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5157
                E(#rows) from range optimizer. Make another try:
 
5158
                
 
5159
                If range optimizer produced E(#rows) for a prefix of the ref
 
5160
                access we're considering, and that E(#rows) is lower then our
 
5161
                current estimate, make an adjustment. The criteria of when we
 
5162
                can make an adjustment is a special case of the criteria used
 
5163
                in ReuseRangeEstimateForRef-3.
 
5164
              */
 
5165
              if (table->quick_keys.is_set(key) &&
 
5166
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5167
                  table->quick_n_ranges[key] == 1 &&
 
5168
                  records > (double) table->quick_rows[key])
 
5169
              {
 
5170
                records= (double) table->quick_rows[key];
 
5171
              }
 
5172
            }
 
5173
            /* Limit the number of matched rows */
 
5174
            tmp= records;
 
5175
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5176
            if (table->covering_keys.is_set(key))
 
5177
            {
 
5178
              /* we can use only index tree */
 
5179
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5180
            }
 
5181
            else
 
5182
              tmp= record_count*min(tmp,s->worst_seeks);
 
5183
          }
 
5184
        }
 
5185
        else
 
5186
        {
 
5187
          /*
 
5188
            Use as much key-parts as possible and a uniq key is better
 
5189
            than a not unique key
 
5190
            Set tmp to (previous record count) * (records / combination)
 
5191
          */
 
5192
          if ((found_part & 1) &&
 
5193
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5194
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5195
          {
 
5196
            max_key_part= max_part_bit(found_part);
 
5197
            /*
 
5198
              ReuseRangeEstimateForRef-3:
 
5199
              We're now considering a ref[or_null] access via
 
5200
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5201
              (same-as-above but with one cond replaced 
 
5202
               with "t.keypart_i IS NULL")]  (**)
 
5203
              
 
5204
              Try re-using E(#rows) from "range" optimizer:
 
5205
              We can do so if "range" optimizer used the same intervals as
 
5206
              in (**). The intervals used by range optimizer may be not 
 
5207
              available at this point (as "range" access might have choosen to
 
5208
              create quick select over another index), so we can't compare
 
5209
              them to (**). We'll make indirect judgements instead.
 
5210
              The sufficient conditions for re-use are:
 
5211
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5212
                   this is not satisfied we have no way to know which ranges
 
5213
                   will be actually scanned by 'ref' until we execute the 
 
5214
                   join)
 
5215
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5216
                   is apparently a necessary requirement)
 
5217
 
 
5218
              We also have a property that "range optimizer produces equal or 
 
5219
              tighter set of scan intervals than ref(const) optimizer". Each
 
5220
              of the intervals in (**) are "tightest possible" intervals when 
 
5221
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5222
              From here it follows that range access used either one, or
 
5223
              both of the (I1) and (I2) intervals:
 
5224
              
 
5225
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5226
               (same-as-above but with one cond replaced  
 
5227
                with "t.keypart_i IS NULL")               (I2)
 
5228
 
 
5229
              The remaining part is to exclude the situation where range
 
5230
              optimizer used one interval while we're considering
 
5231
              ref-or-null and looking for estimate for two intervals. This
 
5232
              is done by last limitation:
 
5233
 
 
5234
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5235
            */
 
5236
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5237
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5238
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
 
5239
            {
 
5240
              tmp= records= (double) table->quick_rows[key];
 
5241
            }
 
5242
            else
 
5243
            {
 
5244
              /* Check if we have statistic about the distribution */
 
5245
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5246
              {
 
5247
                /* 
 
5248
                  Fix for the case where the index statistics is too
 
5249
                  optimistic: If 
 
5250
                  (1) We're considering ref(const) and there is quick select
 
5251
                      on the same index, 
 
5252
                  (2) and that quick select uses more keyparts (i.e. it will
 
5253
                      scan equal/smaller interval then this ref(const))
 
5254
                  (3) and E(#rows) for quick select is higher then our
 
5255
                      estimate,
 
5256
                  Then 
 
5257
                    We'll use E(#rows) from quick select.
 
5258
 
 
5259
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5260
                  cheaper in some cases ?
 
5261
                  TODO: figure this out and adjust the plan choice if needed.
 
5262
                */
 
5263
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5264
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5265
                    records < (double)table->quick_rows[key])         // (3)
 
5266
                  records= (double)table->quick_rows[key];
 
5267
 
 
5268
                tmp= records;
 
5269
              }
 
5270
              else
 
5271
              {
 
5272
                /*
 
5273
                  Assume that the first key part matches 1% of the file
 
5274
                  and that the whole key matches 10 (duplicates) or 1
 
5275
                  (unique) records.
 
5276
                  Assume also that more key matches proportionally more
 
5277
                  records
 
5278
                  This gives the formula:
 
5279
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5280
 
 
5281
                  b = records matched by whole key
 
5282
                  a = records matched by first key part (1% of all records?)
 
5283
                  c = number of key parts in key
 
5284
                  x = used key parts (1 <= x <= c)
 
5285
                */
 
5286
                double rec_per_key;
 
5287
                if (!(rec_per_key=(double)
 
5288
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5289
                  rec_per_key=(double) s->records/rec+1;
 
5290
 
 
5291
                if (!s->records)
 
5292
                  tmp = 0;
 
5293
                else if (rec_per_key/(double) s->records >= 0.01)
 
5294
                  tmp = rec_per_key;
 
5295
                else
 
5296
                {
 
5297
                  double a=s->records*0.01;
 
5298
                  if (keyinfo->key_parts > 1)
 
5299
                    tmp= (max_key_part * (rec_per_key - a) +
 
5300
                          a*keyinfo->key_parts - rec_per_key)/
 
5301
                         (keyinfo->key_parts-1);
 
5302
                  else
 
5303
                    tmp= a;
 
5304
                  set_if_bigger(tmp,1.0);
 
5305
                }
 
5306
                records = (ulong) tmp;
 
5307
              }
 
5308
 
 
5309
              if (ref_or_null_part)
 
5310
              {
 
5311
                /* We need to do two key searches to find key */
 
5312
                tmp *= 2.0;
 
5313
                records *= 2.0;
 
5314
              }
 
5315
 
 
5316
              /*
 
5317
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5318
                E(#rows) from range optimizer. Make another try:
 
5319
                
 
5320
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5321
                access we're considering, and that E(#rows) is lower then our
 
5322
                current estimate, make the adjustment.
 
5323
 
 
5324
                The decision whether we can re-use the estimate from the range
 
5325
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5326
                applied to first table->quick_key_parts[key] key parts.
 
5327
              */
 
5328
              if (table->quick_keys.is_set(key) &&
 
5329
                  table->quick_key_parts[key] <= max_key_part &&
 
5330
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5331
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5332
                                                         const_part) &&
 
5333
                  records > (double) table->quick_rows[key])
 
5334
              {
 
5335
                tmp= records= (double) table->quick_rows[key];
 
5336
              }
 
5337
            }
 
5338
 
 
5339
            /* Limit the number of matched rows */
 
5340
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5341
            if (table->covering_keys.is_set(key))
 
5342
            {
 
5343
              /* we can use only index tree */
 
5344
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5345
            }
 
5346
            else
 
5347
              tmp= record_count * min(tmp,s->worst_seeks);
 
5348
          }
 
5349
          else
 
5350
            tmp= best_time;                    // Do nothing
 
5351
        }
 
5352
 
 
5353
        if (sj_inside_out_scan && !start_key)
 
5354
        {
 
5355
          tmp= tmp/2;
 
5356
          if (records)
 
5357
            records= records/2;
 
5358
        }
 
5359
 
 
5360
      }
 
5361
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5362
      {
 
5363
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5364
        best= tmp;
 
5365
        best_records= records;
 
5366
        best_key= start_key;
 
5367
        best_max_key_part= max_key_part;
 
5368
        best_ref_depends_map= found_ref;
 
5369
        best_is_sj_inside_out= sj_inside_out_scan;
 
5370
      }
 
5371
    }
 
5372
    records= best_records;
 
5373
  }
 
5374
 
 
5375
  /*
 
5376
    Don't test table scan if it can't be better.
 
5377
    Prefer key lookup if we would use the same key for scanning.
 
5378
 
 
5379
    Don't do a table scan on InnoDB tables, if we can read the used
 
5380
    parts of the row from any of the used index.
 
5381
    This is because table scans uses index and we would not win
 
5382
    anything by using a table scan.
 
5383
 
 
5384
    A word for word translation of the below if-statement in sergefp's
 
5385
    understanding: we check if we should use table scan if:
 
5386
    (1) The found 'ref' access produces more records than a table scan
 
5387
        (or index scan, or quick select), or 'ref' is more expensive than
 
5388
        any of them.
 
5389
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5390
        'range' access using index IDX, and the best way to perform 'ref' 
 
5391
        access is to use the same index IDX, with the same or more key parts.
 
5392
        (note: it is not clear how this rule is/should be extended to 
 
5393
        index_merge quick selects)
 
5394
    (3) See above note about InnoDB.
 
5395
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5396
             path, but there is no quick select)
 
5397
        If the condition in the above brackets holds, then the only possible
 
5398
        "table scan" access method is ALL/index (there is no quick select).
 
5399
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5400
        choose it over ALL/index, there is no need to consider a full table
 
5401
        scan.
 
5402
  */
 
5403
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5404
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5405
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5406
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5407
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5408
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5409
  {                                             // Check full join
 
5410
    ha_rows rnd_records= s->found_records;
 
5411
    /*
 
5412
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5413
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5414
      preceding this table in the join order we're now considering), then 
 
5415
      assume that 25% of the rows will be filtered out by this condition.
 
5416
 
 
5417
      This heuristic is supposed to force tables used in exprZ to be before
 
5418
      this table in join order.
 
5419
    */
 
5420
    if (found_constraint)
 
5421
      rnd_records-= rnd_records/4;
 
5422
 
 
5423
    /*
 
5424
      If applicable, get a more accurate estimate. Don't use the two
 
5425
      heuristics at once.
 
5426
    */
 
5427
    if (s->table->quick_condition_rows != s->found_records)
 
5428
      rnd_records= s->table->quick_condition_rows;
 
5429
 
 
5430
    /*
 
5431
      Range optimizer never proposes a RANGE if it isn't better
 
5432
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5433
      Here we estimate its cost.
 
5434
    */
 
5435
    if (s->quick)
 
5436
    {
 
5437
      /*
 
5438
        For each record we:
 
5439
        - read record range through 'quick'
 
5440
        - skip rows which does not satisfy WHERE constraints
 
5441
        TODO: 
 
5442
        We take into account possible use of join cache for ALL/index
 
5443
        access (see first else-branch below), but we don't take it into 
 
5444
        account here for range/index_merge access. Find out why this is so.
 
5445
      */
 
5446
      tmp= record_count *
 
5447
        (s->quick->read_time +
 
5448
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5449
    }
 
5450
    else
 
5451
    {
 
5452
      /* Estimate cost of reading table. */
 
5453
      tmp= s->table->file->scan_time();
 
5454
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5455
      {
 
5456
        /*
 
5457
          For each record we have to:
 
5458
          - read the whole table record 
 
5459
          - skip rows which does not satisfy join condition
 
5460
        */
 
5461
        tmp= record_count *
 
5462
          (tmp +
 
5463
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5464
      }
 
5465
      else
 
5466
      {
 
5467
        /* We read the table as many times as join buffer becomes full. */
 
5468
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5469
                           record_count /
 
5470
                           (double) thd->variables.join_buff_size));
 
5471
        /* 
 
5472
            We don't make full cartesian product between rows in the scanned
 
5473
           table and existing records because we skip all rows from the
 
5474
           scanned table, which does not satisfy join condition when 
 
5475
           we read the table (see flush_cached_records for details). Here we
 
5476
           take into account cost to read and skip these records.
 
5477
        */
 
5478
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5479
      }
 
5480
    }
 
5481
 
 
5482
    /*
 
5483
      We estimate the cost of evaluating WHERE clause for found records
 
5484
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5485
      tmp give us total cost of using Table SCAN
 
5486
    */
 
5487
    if (best == DBL_MAX ||
 
5488
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5489
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5490
    {
 
5491
      /*
 
5492
        If the table has a range (s->quick is set) make_join_select()
 
5493
        will ensure that this will be used
 
5494
      */
 
5495
      best= tmp;
 
5496
      records= rows2double(rnd_records);
 
5497
      best_key= 0;
 
5498
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5499
      best_ref_depends_map= 0;
 
5500
      best_is_sj_inside_out= false;
 
5501
    }
 
5502
  }
 
5503
 
 
5504
  /* Update the cost information for the current partial plan */
 
5505
  join->positions[idx].records_read= records;
 
5506
  join->positions[idx].read_time=    best;
 
5507
  join->positions[idx].key=          best_key;
 
5508
  join->positions[idx].table=        s;
 
5509
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5510
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5511
 
 
5512
  if (!best_key &&
 
5513
      idx == join->const_tables &&
 
5514
      s->table == join->sort_by_table &&
 
5515
      join->unit->select_limit_cnt >= records)
 
5516
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5517
 
 
5518
  return;
 
5519
}
 
5520
 
 
5521
 
 
5522
/**
 
5523
  Selects and invokes a search strategy for an optimal query plan.
 
5524
 
 
5525
  The function checks user-configurable parameters that control the search
 
5526
  strategy for an optimal plan, selects the search method and then invokes
 
5527
  it. Each specific optimization procedure stores the final optimal plan in
 
5528
  the array 'join->best_positions', and the cost of the plan in
 
5529
  'join->best_read'.
 
5530
 
 
5531
  @param join         pointer to the structure providing all context info for
 
5532
                      the query
 
5533
  @param join_tables  set of the tables in the query
 
5534
 
 
5535
  @todo
 
5536
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5537
    the greedy version. Will be removed when greedy_search is approved.
 
5538
 
 
5539
  @retval
 
5540
    false       ok
 
5541
  @retval
 
5542
    true        Fatal error
 
5543
*/
 
5544
 
 
5545
static bool
 
5546
choose_plan(JOIN *join, table_map join_tables)
 
5547
{
 
5548
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5549
  uint prune_level=  join->thd->variables.optimizer_prune_level;
 
5550
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5551
 
 
5552
  join->cur_embedding_map= 0;
 
5553
  reset_nj_counters(join->join_list);
 
5554
  /*
 
5555
    if (SELECT_STRAIGHT_JOIN option is set)
 
5556
      reorder tables so dependent tables come after tables they depend 
 
5557
      on, otherwise keep tables in the order they were specified in the query 
 
5558
    else
 
5559
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5560
      records accessed.
 
5561
  */
 
5562
  my_qsort(join->best_ref + join->const_tables,
 
5563
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5564
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5565
  join->cur_emb_sj_nests= 0;
 
5566
  if (straight_join)
 
5567
  {
 
5568
    optimize_straight_join(join, join_tables);
 
5569
  }
 
5570
  else
 
5571
  {
 
5572
    if (search_depth == MAX_TABLES+2)
 
5573
    { /*
 
5574
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5575
        the greedy version. Will be removed when greedy_search is approved.
 
5576
      */
 
5577
      join->best_read= DBL_MAX;
 
5578
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5579
        return(true);
 
5580
    } 
 
5581
    else
 
5582
    {
 
5583
      if (search_depth == 0)
 
5584
        /* Automatically determine a reasonable value for 'search_depth' */
 
5585
        search_depth= determine_search_depth(join);
 
5586
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5587
        return(true);
 
5588
    }
 
5589
  }
 
5590
 
 
5591
  /* 
 
5592
    Store the cost of this query into a user variable
 
5593
    Don't update last_query_cost for statements that are not "flat joins" :
 
5594
    i.e. they have subqueries, unions or call stored procedures.
 
5595
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5596
  */
 
5597
  if (join->thd->lex->is_single_level_stmt())
 
5598
    join->thd->status_var.last_query_cost= join->best_read;
 
5599
  return(false);
 
5600
}
 
5601
 
 
5602
 
 
5603
/**
 
5604
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5605
 
 
5606
  @param ptr1 pointer to first JOIN_TAB object
 
5607
  @param ptr2 pointer to second JOIN_TAB object
793
5608
 
794
5609
  NOTES
795
5610
    The order relation implemented by join_tab_cmp() is not transitive,
801
5616
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
802
5617
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
803
5618
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
804
 
 
 
5619
     
805
5620
  @retval
806
5621
    1  if first is bigger
807
5622
  @retval
809
5624
  @retval
810
5625
    0  if equal
811
5626
*/
812
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5627
 
 
5628
static int
 
5629
join_tab_cmp(const void* ptr1, const void* ptr2)
813
5630
{
814
 
  JoinTable *jt1= *(JoinTable**) ptr1;
815
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5631
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5632
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
816
5633
 
817
5634
  if (jt1->dependent & jt2->table->map)
818
5635
    return 1;
819
5636
  if (jt2->dependent & jt1->table->map)
820
 
    return -1;
 
5637
    return -1;  
821
5638
  if (jt1->found_records > jt2->found_records)
822
5639
    return 1;
823
5640
  if (jt1->found_records < jt2->found_records)
824
 
    return -1;
 
5641
    return -1; 
825
5642
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
826
5643
}
827
5644
 
 
5645
 
828
5646
/**
829
5647
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
830
5648
*/
831
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5649
 
 
5650
static int
 
5651
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
832
5652
{
833
 
  JoinTable *jt1= *(JoinTable**) ptr1;
834
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5653
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5654
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
835
5655
 
836
5656
  if (jt1->dependent & jt2->table->map)
837
5657
    return 1;
841
5661
}
842
5662
 
843
5663
/**
 
5664
  Heuristic procedure to automatically guess a reasonable degree of
 
5665
  exhaustiveness for the greedy search procedure.
 
5666
 
 
5667
  The procedure estimates the optimization time and selects a search depth
 
5668
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5669
  find. If the number of tables in the query exceeds some constant, then
 
5670
  search_depth is set to this constant.
 
5671
 
 
5672
  @param join   pointer to the structure providing all context info for
 
5673
                the query
 
5674
 
 
5675
  @note
 
5676
    This is an extremely simplistic implementation that serves as a stub for a
 
5677
    more advanced analysis of the join. Ideally the search depth should be
 
5678
    determined by learning from previous query optimizations, because it will
 
5679
    depend on the CPU power (and other factors).
 
5680
 
 
5681
  @todo
 
5682
    this value should be determined dynamically, based on statistics:
 
5683
    uint max_tables_for_exhaustive_opt= 7;
 
5684
 
 
5685
  @todo
 
5686
    this value could be determined by some mapping of the form:
 
5687
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5688
 
 
5689
  @return
 
5690
    A positive integer that specifies the search depth (and thus the
 
5691
    exhaustiveness) of the depth-first search algorithm used by
 
5692
    'greedy_search'.
 
5693
*/
 
5694
 
 
5695
static uint
 
5696
determine_search_depth(JOIN *join)
 
5697
{
 
5698
  uint table_count=  join->tables - join->const_tables;
 
5699
  uint search_depth;
 
5700
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5701
  uint max_tables_for_exhaustive_opt= 7;
 
5702
 
 
5703
  if (table_count <= max_tables_for_exhaustive_opt)
 
5704
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5705
  else
 
5706
    /*
 
5707
      TODO: this value could be determined by some mapping of the form:
 
5708
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5709
    */
 
5710
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5711
 
 
5712
  return search_depth;
 
5713
}
 
5714
 
 
5715
 
 
5716
/**
 
5717
  Select the best ways to access the tables in a query without reordering them.
 
5718
 
 
5719
    Find the best access paths for each query table and compute their costs
 
5720
    according to their order in the array 'join->best_ref' (thus without
 
5721
    reordering the join tables). The function calls sequentially
 
5722
    'best_access_path' for each table in the query to select the best table
 
5723
    access method. The final optimal plan is stored in the array
 
5724
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5725
 
 
5726
  @param join          pointer to the structure providing all context info for
 
5727
                       the query
 
5728
  @param join_tables   set of the tables in the query
 
5729
 
 
5730
  @note
 
5731
    This function can be applied to:
 
5732
    - queries with STRAIGHT_JOIN
 
5733
    - internally to compute the cost of an arbitrary QEP
 
5734
  @par
 
5735
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5736
    optimization process to finalize a QEP as it is.
 
5737
*/
 
5738
 
 
5739
static void
 
5740
optimize_straight_join(JOIN *join, table_map join_tables)
 
5741
{
 
5742
  JOIN_TAB *s;
 
5743
  uint idx= join->const_tables;
 
5744
  double    record_count= 1.0;
 
5745
  double    read_time=    0.0;
 
5746
 
 
5747
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5748
  {
 
5749
    /* Find the best access method from 's' to the current partial plan */
 
5750
    advance_sj_state(join_tables, s);
 
5751
    best_access_path(join, s, join->thd, join_tables, idx,
 
5752
                     record_count, read_time);
 
5753
    /* compute the cost of the new plan extended with 's' */
 
5754
    record_count*= join->positions[idx].records_read;
 
5755
    read_time+=    join->positions[idx].read_time;
 
5756
    join_tables&= ~(s->table->map);
 
5757
    ++idx;
 
5758
  }
 
5759
 
 
5760
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5761
  if (join->sort_by_table &&
 
5762
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5763
    read_time+= record_count;  // We have to make a temp table
 
5764
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5765
  join->best_read= read_time;
 
5766
}
 
5767
 
 
5768
 
 
5769
/**
 
5770
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5771
 
 
5772
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5773
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5774
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5775
    selects the most promising table, and extends the current partial QEP with
 
5776
    that table.  Currenly the most 'promising' table is the one with least
 
5777
    expensive extension.\
 
5778
 
 
5779
    There are two extreme cases:
 
5780
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5781
    best complete continuation of the partial QEP. This continuation can be
 
5782
    used directly as a result of the search.
 
5783
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5784
    consideres the extension of the current QEP with each of the remaining
 
5785
    unoptimized tables.
 
5786
 
 
5787
    All other cases are in-between these two extremes. Thus the parameter
 
5788
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5789
    value, the longer the optimizaton time and possibly the better the
 
5790
    resulting plan. The lower the value, the fewer alternative plans are
 
5791
    estimated, but the more likely to get a bad QEP.
 
5792
 
 
5793
    All intermediate and final results of the procedure are stored in 'join':
 
5794
    - join->positions     : modified for every partial QEP that is explored
 
5795
    - join->best_positions: modified for the current best complete QEP
 
5796
    - join->best_read     : modified for the current best complete QEP
 
5797
    - join->best_ref      : might be partially reordered
 
5798
 
 
5799
    The final optimal plan is stored in 'join->best_positions', and its
 
5800
    corresponding cost in 'join->best_read'.
 
5801
 
 
5802
  @note
 
5803
    The following pseudocode describes the algorithm of 'greedy_search':
 
5804
 
 
5805
    @code
 
5806
    procedure greedy_search
 
5807
    input: remaining_tables
 
5808
    output: pplan;
 
5809
    {
 
5810
      pplan = <>;
 
5811
      do {
 
5812
        (t, a) = best_extension(pplan, remaining_tables);
 
5813
        pplan = concat(pplan, (t, a));
 
5814
        remaining_tables = remaining_tables - t;
 
5815
      } while (remaining_tables != {})
 
5816
      return pplan;
 
5817
    }
 
5818
 
 
5819
  @endcode
 
5820
    where 'best_extension' is a placeholder for a procedure that selects the
 
5821
    most "promising" of all tables in 'remaining_tables'.
 
5822
    Currently this estimate is performed by calling
 
5823
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5824
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5825
    mainly depends on that of 'best_extension_by_limited_search'.
 
5826
 
 
5827
  @par
 
5828
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5829
    worst-case complexity of this algorithm is <=
 
5830
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5831
    complexity of greedy_search is O(N!).
 
5832
 
 
5833
  @par
 
5834
    In the future, 'greedy_search' might be extended to support other
 
5835
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5836
 
 
5837
  @param join             pointer to the structure providing all context info
 
5838
                          for the query
 
5839
  @param remaining_tables set of tables not included into the partial plan yet
 
5840
  @param search_depth     controlls the exhaustiveness of the search
 
5841
  @param prune_level      the pruning heuristics that should be applied during
 
5842
                          search
 
5843
 
 
5844
  @retval
 
5845
    false       ok
 
5846
  @retval
 
5847
    true        Fatal error
 
5848
*/
 
5849
 
 
5850
static bool
 
5851
greedy_search(JOIN      *join,
 
5852
              table_map remaining_tables,
 
5853
              uint      search_depth,
 
5854
              uint      prune_level)
 
5855
{
 
5856
  double    record_count= 1.0;
 
5857
  double    read_time=    0.0;
 
5858
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5859
  uint      best_idx;
 
5860
  uint      size_remain;    // cardinality of remaining_tables
 
5861
  POSITION  best_pos;
 
5862
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5863
 
 
5864
  /* number of tables that remain to be optimized */
 
5865
  size_remain= my_count_bits(remaining_tables);
 
5866
 
 
5867
  do {
 
5868
    /* Find the extension of the current QEP with the lowest cost */
 
5869
    join->best_read= DBL_MAX;
 
5870
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5871
                                         read_time, search_depth, prune_level))
 
5872
      return(true);
 
5873
 
 
5874
    if (size_remain <= search_depth)
 
5875
    {
 
5876
      /*
 
5877
        'join->best_positions' contains a complete optimal extension of the
 
5878
        current partial QEP.
 
5879
      */
 
5880
      return(false);
 
5881
    }
 
5882
 
 
5883
    /* select the first table in the optimal extension as most promising */
 
5884
    best_pos= join->best_positions[idx];
 
5885
    best_table= best_pos.table;
 
5886
    /*
 
5887
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5888
      'join->positions' for cost estimates, therefore we have to update its
 
5889
      value.
 
5890
    */
 
5891
    join->positions[idx]= best_pos;
 
5892
 
 
5893
    /* find the position of 'best_table' in 'join->best_ref' */
 
5894
    best_idx= idx;
 
5895
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5896
    while (pos && best_table != pos)
 
5897
      pos= join->best_ref[++best_idx];
 
5898
    assert((pos != NULL)); // should always find 'best_table'
 
5899
    /* move 'best_table' at the first free position in the array of joins */
 
5900
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
 
5901
 
 
5902
    /* compute the cost of the new plan extended with 'best_table' */
 
5903
    record_count*= join->positions[idx].records_read;
 
5904
    read_time+=    join->positions[idx].read_time;
 
5905
 
 
5906
    remaining_tables&= ~(best_table->table->map);
 
5907
    --size_remain;
 
5908
    ++idx;
 
5909
  } while (true);
 
5910
}
 
5911
 
 
5912
 
 
5913
/**
 
5914
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5915
  exhaustive search.
 
5916
 
 
5917
    The procedure searches for the optimal ordering of the query tables in set
 
5918
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5919
    each table. The choice of a table order and an access path for each table
 
5920
    constitutes a query execution plan (QEP) that fully specifies how to
 
5921
    execute the query.
 
5922
   
 
5923
    The maximal size of the found plan is controlled by the parameter
 
5924
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5925
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5926
    of only some of the query tables. Such "partial" optimal plans are useful
 
5927
    only as input to query optimization procedures, and cannot be used directly
 
5928
    to execute a query.
 
5929
 
 
5930
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5931
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5932
    evaluates the cost of the partial plan extended by all access plans for
 
5933
    each of the relations in 'remaining_tables', expands the current partial
 
5934
    plan with the access plan that results in lowest cost of the expanded
 
5935
    partial plan, and removes the corresponding relation from
 
5936
    'remaining_tables'. The algorithm continues until it either constructs a
 
5937
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5938
    search_depth.
 
5939
 
 
5940
    The final optimal plan is stored in 'join->best_positions'. The
 
5941
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5942
 
 
5943
  @note
 
5944
    The procedure uses a recursive depth-first search where the depth of the
 
5945
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5946
    parameter 'search_depth'.
 
5947
 
 
5948
  @note
 
5949
    The pseudocode below describes the algorithm of
 
5950
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5951
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5952
    the complexity of greedy_search is O(N!).
 
5953
 
 
5954
    @code
 
5955
    procedure best_extension_by_limited_search(
 
5956
      pplan in,             // in, partial plan of tables-joined-so-far
 
5957
      pplan_cost,           // in, cost of pplan
 
5958
      remaining_tables,     // in, set of tables not referenced in pplan
 
5959
      best_plan_so_far,     // in/out, best plan found so far
 
5960
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5961
      search_depth)         // in, maximum size of the plans being considered
 
5962
    {
 
5963
      for each table T from remaining_tables
 
5964
      {
 
5965
        // Calculate the cost of using table T as above
 
5966
        cost = complex-series-of-calculations;
 
5967
 
 
5968
        // Add the cost to the cost so far.
 
5969
        pplan_cost+= cost;
 
5970
 
 
5971
        if (pplan_cost >= best_plan_so_far_cost)
 
5972
          // pplan_cost already too great, stop search
 
5973
          continue;
 
5974
 
 
5975
        pplan= expand pplan by best_access_method;
 
5976
        remaining_tables= remaining_tables - table T;
 
5977
        if (remaining_tables is not an empty set
 
5978
            and
 
5979
            search_depth > 1)
 
5980
        {
 
5981
          best_extension_by_limited_search(pplan, pplan_cost,
 
5982
                                           remaining_tables,
 
5983
                                           best_plan_so_far,
 
5984
                                           best_plan_so_far_cost,
 
5985
                                           search_depth - 1);
 
5986
        }
 
5987
        else
 
5988
        {
 
5989
          best_plan_so_far_cost= pplan_cost;
 
5990
          best_plan_so_far= pplan;
 
5991
        }
 
5992
      }
 
5993
    }
 
5994
    @endcode
 
5995
 
 
5996
  @note
 
5997
    When 'best_extension_by_limited_search' is called for the first time,
 
5998
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
5999
    The actual implementation provides a way to optionally use pruning
 
6000
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6001
    space by skipping some partial plans.
 
6002
 
 
6003
  @note
 
6004
    The parameter 'search_depth' provides control over the recursion
 
6005
    depth, and thus the size of the resulting optimal plan.
 
6006
 
 
6007
  @param join             pointer to the structure providing all context info
 
6008
                          for the query
 
6009
  @param remaining_tables set of tables not included into the partial plan yet
 
6010
  @param idx              length of the partial QEP in 'join->positions';
 
6011
                          since a depth-first search is used, also corresponds
 
6012
                          to the current depth of the search tree;
 
6013
                          also an index in the array 'join->best_ref';
 
6014
  @param record_count     estimate for the number of records returned by the
 
6015
                          best partial plan
 
6016
  @param read_time        the cost of the best partial plan
 
6017
  @param search_depth     maximum depth of the recursion and thus size of the
 
6018
                          found optimal plan
 
6019
                          (0 < search_depth <= join->tables+1).
 
6020
  @param prune_level      pruning heuristics that should be applied during
 
6021
                          optimization
 
6022
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6023
 
 
6024
  @retval
 
6025
    false       ok
 
6026
  @retval
 
6027
    true        Fatal error
 
6028
*/
 
6029
 
 
6030
static bool
 
6031
best_extension_by_limited_search(JOIN      *join,
 
6032
                                 table_map remaining_tables,
 
6033
                                 uint      idx,
 
6034
                                 double    record_count,
 
6035
                                 double    read_time,
 
6036
                                 uint      search_depth,
 
6037
                                 uint      prune_level)
 
6038
{
 
6039
  THD *thd= join->thd;
 
6040
  if (thd->killed)  // Abort
 
6041
    return(true);
 
6042
 
 
6043
  /* 
 
6044
     'join' is a partial plan with lower cost than the best plan so far,
 
6045
     so continue expanding it further with the tables in 'remaining_tables'.
 
6046
  */
 
6047
  JOIN_TAB *s;
 
6048
  double best_record_count= DBL_MAX;
 
6049
  double best_read_time=    DBL_MAX;
 
6050
 
 
6051
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6052
  {
 
6053
    table_map real_table_bit= s->table->map;
 
6054
    if ((remaining_tables & real_table_bit) && 
 
6055
        !(remaining_tables & s->dependent) && 
 
6056
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6057
    {
 
6058
      double current_record_count, current_read_time;
 
6059
      advance_sj_state(remaining_tables, s);
 
6060
 
 
6061
      /*
 
6062
        psergey-insideout-todo: 
 
6063
          when best_access_path() detects it could do an InsideOut scan or 
 
6064
          some other scan, have it return an insideout scan and a flag that 
 
6065
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6066
          when the depth is insufficient??)
 
6067
      */
 
6068
      /* Find the best access method from 's' to the current partial plan */
 
6069
      best_access_path(join, s, thd, remaining_tables, idx,
 
6070
                       record_count, read_time);
 
6071
      /* Compute the cost of extending the plan with 's' */
 
6072
      current_record_count= record_count * join->positions[idx].records_read;
 
6073
      current_read_time=    read_time + join->positions[idx].read_time;
 
6074
 
 
6075
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6076
      if ((current_read_time +
 
6077
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6078
      {
 
6079
        restore_prev_nj_state(s);
 
6080
        restore_prev_sj_state(remaining_tables, s);
 
6081
        continue;
 
6082
      }
 
6083
 
 
6084
      /*
 
6085
        Prune some less promising partial plans. This heuristic may miss
 
6086
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6087
      */
 
6088
      if (prune_level == 1)
 
6089
      {
 
6090
        if (best_record_count > current_record_count ||
 
6091
            best_read_time > current_read_time ||
 
6092
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6093
        {
 
6094
          if (best_record_count >= current_record_count &&
 
6095
              best_read_time >= current_read_time &&
 
6096
              /* TODO: What is the reasoning behind this condition? */
 
6097
              (!(s->key_dependent & remaining_tables) ||
 
6098
               join->positions[idx].records_read < 2.0))
 
6099
          {
 
6100
            best_record_count= current_record_count;
 
6101
            best_read_time=    current_read_time;
 
6102
          }
 
6103
        }
 
6104
        else
 
6105
        {
 
6106
          restore_prev_nj_state(s);
 
6107
          restore_prev_sj_state(remaining_tables, s);
 
6108
          continue;
 
6109
        }
 
6110
      }
 
6111
 
 
6112
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6113
      { /* Recursively expand the current partial plan */
 
6114
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6115
        if (best_extension_by_limited_search(join,
 
6116
                                             remaining_tables & ~real_table_bit,
 
6117
                                             idx + 1,
 
6118
                                             current_record_count,
 
6119
                                             current_read_time,
 
6120
                                             search_depth - 1,
 
6121
                                             prune_level))
 
6122
          return(true);
 
6123
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6124
      }
 
6125
      else
 
6126
      { /*
 
6127
          'join' is either the best partial QEP with 'search_depth' relations,
 
6128
          or the best complete QEP so far, whichever is smaller.
 
6129
        */
 
6130
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6131
        if (join->sort_by_table &&
 
6132
            join->sort_by_table !=
 
6133
            join->positions[join->const_tables].table->table)
 
6134
          /* We have to make a temp table */
 
6135
          current_read_time+= current_record_count;
 
6136
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6137
        {
 
6138
          memcpy(join->best_positions, join->positions,
 
6139
                 sizeof(POSITION) * (idx + 1));
 
6140
          join->best_read= current_read_time - 0.001;
 
6141
        }
 
6142
      }
 
6143
      restore_prev_nj_state(s);
 
6144
      restore_prev_sj_state(remaining_tables, s);
 
6145
    }
 
6146
  }
 
6147
  return(false);
 
6148
}
 
6149
 
 
6150
 
 
6151
/**
 
6152
  @todo
 
6153
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6154
  tested and accepted.
 
6155
 
 
6156
  RETURN VALUES
 
6157
    false       ok
 
6158
    true        Fatal error
 
6159
*/
 
6160
static bool
 
6161
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
 
6162
          double read_time)
 
6163
{
 
6164
  THD *thd= join->thd;
 
6165
  if (thd->killed)
 
6166
    return(true);
 
6167
  if (!rest_tables)
 
6168
  {
 
6169
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6170
    if (join->sort_by_table &&
 
6171
        join->sort_by_table !=
 
6172
        join->positions[join->const_tables].table->table)
 
6173
      read_time+=record_count;                  // We have to make a temp table
 
6174
    if (read_time < join->best_read)
 
6175
    {
 
6176
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6177
      join->best_read= read_time - 0.001;
 
6178
    }
 
6179
    return(false);
 
6180
  }
 
6181
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6182
    return(false);                                      /* Found better before */
 
6183
 
 
6184
  JOIN_TAB *s;
 
6185
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6186
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6187
  {
 
6188
    table_map real_table_bit=s->table->map;
 
6189
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6190
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6191
    {
 
6192
      double records, best;
 
6193
      advance_sj_state(rest_tables, s);
 
6194
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6195
                       read_time);
 
6196
      records= join->positions[idx].records_read;
 
6197
      best= join->positions[idx].read_time;
 
6198
      /*
 
6199
        Go to the next level only if there hasn't been a better key on
 
6200
        this level! This will cut down the search for a lot simple cases!
 
6201
      */
 
6202
      double current_record_count=record_count*records;
 
6203
      double current_read_time=read_time+best;
 
6204
      if (best_record_count > current_record_count ||
 
6205
          best_read_time > current_read_time ||
 
6206
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6207
      {
 
6208
        if (best_record_count >= current_record_count &&
 
6209
            best_read_time >= current_read_time &&
 
6210
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6211
        {
 
6212
          best_record_count=current_record_count;
 
6213
          best_read_time=current_read_time;
 
6214
        }
 
6215
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6216
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6217
                      current_record_count,current_read_time))
 
6218
          return(true);
 
6219
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6220
      }
 
6221
      restore_prev_nj_state(s);
 
6222
      restore_prev_sj_state(rest_tables, s);
 
6223
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6224
        break;                          // Don't test all combinations
 
6225
    }
 
6226
  }
 
6227
  return(false);
 
6228
}
 
6229
 
 
6230
 
 
6231
/**
844
6232
  Find how much space the prevous read not const tables takes in cache.
845
6233
*/
846
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6234
 
 
6235
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6236
                                   JOIN_TAB *join_tab)
847
6237
{
848
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6238
  uint null_fields,blobs,fields,rec_length;
849
6239
  Field **f_ptr,*field;
 
6240
  MY_BITMAP *read_set= join_tab->table->read_set;;
850
6241
 
851
6242
  null_fields= blobs= fields= rec_length=0;
852
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6243
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
853
6244
  {
854
 
    if (field->isReadSet())
 
6245
    if (bitmap_is_set(read_set, field->field_index))
855
6246
    {
856
 
      uint32_t flags=field->flags;
 
6247
      uint flags=field->flags;
857
6248
      fields++;
858
6249
      rec_length+=field->pack_length();
859
6250
      if (flags & BLOB_FLAG)
860
 
        blobs++;
 
6251
        blobs++;
861
6252
      if (!(flags & NOT_NULL_FLAG))
862
 
        null_fields++;
 
6253
        null_fields++;
863
6254
    }
864
6255
  }
865
6256
  if (null_fields)
868
6259
    rec_length+=sizeof(bool);
869
6260
  if (blobs)
870
6261
  {
871
 
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
872
 
                                     (join_tab->table->getRecordLength()- rec_length));
873
 
    rec_length+= max((uint32_t)4,blob_length);
874
 
  }
875
 
  join_tab->used_fields= fields;
876
 
  join_tab->used_fieldlength= rec_length;
877
 
  join_tab->used_blobs= blobs;
878
 
}
879
 
 
880
 
StoredKey *get_store_key(Session *session,
881
 
                         optimizer::KeyUse *keyuse,
882
 
                         table_map used_tables,
883
 
                         KeyPartInfo *key_part,
884
 
                         unsigned char *key_buff,
885
 
                         uint32_t maybe_null)
886
 
{
887
 
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
888
 
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
889
 
  {
890
 
    return new store_key_const_item(session,
891
 
                                    key_part->field,
892
 
                                    key_buff + maybe_null,
893
 
                                    maybe_null ? key_buff : 0,
894
 
                                    key_part->length,
895
 
                                    key_use_val);
896
 
  }
897
 
  else if (key_use_val->type() == Item::FIELD_ITEM ||
898
 
           (key_use_val->type() == Item::REF_ITEM &&
899
 
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
900
 
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
901
 
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
902
 
  {
903
 
    return new store_key_field(session,
904
 
                               key_part->field,
905
 
                               key_buff + maybe_null,
906
 
                               maybe_null ? key_buff : 0,
907
 
                               key_part->length,
908
 
                               ((Item_field*) key_use_val->real_item())->field,
909
 
                               key_use_val->full_name());
910
 
  }
911
 
  return new store_key_item(session,
912
 
                            key_part->field,
913
 
                            key_buff + maybe_null,
914
 
                            maybe_null ? key_buff : 0,
915
 
                            key_part->length,
916
 
                            key_use_val);
917
 
}
 
6262
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6263
                             (join_tab->table->getRecordLength()- rec_length));
 
6264
    rec_length+=(uint) max((uint)4,blob_length);
 
6265
  }
 
6266
  join_tab->used_fields=fields;
 
6267
  join_tab->used_fieldlength=rec_length;
 
6268
  join_tab->used_blobs=blobs;
 
6269
}
 
6270
 
 
6271
 
 
6272
static uint
 
6273
cache_record_length(JOIN *join,uint idx)
 
6274
{
 
6275
  uint length=0;
 
6276
  JOIN_TAB **pos,**end;
 
6277
  THD *thd=join->thd;
 
6278
 
 
6279
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6280
       pos != end ;
 
6281
       pos++)
 
6282
  {
 
6283
    JOIN_TAB *join_tab= *pos;
 
6284
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6285
      calc_used_field_length(thd, join_tab);
 
6286
    length+=join_tab->used_fieldlength;
 
6287
  }
 
6288
  return length;
 
6289
}
 
6290
 
 
6291
 
 
6292
/*
 
6293
  Get the number of different row combinations for subset of partial join
 
6294
 
 
6295
  SYNOPSIS
 
6296
    prev_record_reads()
 
6297
      join       The join structure
 
6298
      idx        Number of tables in the partial join order (i.e. the
 
6299
                 partial join order is in join->positions[0..idx-1])
 
6300
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6301
                 row combinations.
 
6302
 
 
6303
  DESCRIPTION
 
6304
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6305
    tables within that join order (specified in found_ref), find out how many
 
6306
    distinct row combinations of subset tables will be in the result of the
 
6307
    partial join order.
 
6308
     
 
6309
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6310
    method. The ref access depends on current rows of tables in found_ref.
 
6311
    We want to count # of different ref accesses. We assume two ref accesses
 
6312
    will be different if at least one of access parameters is different.
 
6313
    Example: consider a query
 
6314
 
 
6315
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6316
 
 
6317
    and a join order:
 
6318
      t1,  ref access on t1.key=c1
 
6319
      t2,  ref access on t2.key=c2       
 
6320
      t3,  ref access on t3.key=t1.field 
 
6321
    
 
6322
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6323
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6324
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6325
            n_distinct_ref_scans = #records_read(t1)
 
6326
    
 
6327
    The reason for having this function (at least the latest version of it)
 
6328
    is that we need to account for buffering in join execution. 
 
6329
    
 
6330
    An edge-case example: if we have a non-first table in join accessed via
 
6331
    ref(const) or ref(param) where there is a small number of different
 
6332
    values of param, then the access will likely hit the disk cache and will
 
6333
    not require any disk seeks.
 
6334
    
 
6335
    The proper solution would be to assume an LRU disk cache of some size,
 
6336
    calculate probability of cache hits, etc. For now we just count
 
6337
    identical ref accesses as one.
 
6338
 
 
6339
  RETURN 
 
6340
    Expected number of row combinations
 
6341
*/
 
6342
 
 
6343
static double
 
6344
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
 
6345
{
 
6346
  double found=1.0;
 
6347
  POSITION *pos_end= join->positions - 1;
 
6348
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6349
  {
 
6350
    if (pos->table->table->map & found_ref)
 
6351
    {
 
6352
      found_ref|= pos->ref_depend_map;
 
6353
      /* 
 
6354
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6355
        with no matching row we will get position[t2].records_read==0. 
 
6356
        Actually the size of output is one null-complemented row, therefore 
 
6357
        we will use value of 1 whenever we get records_read==0.
 
6358
 
 
6359
        Note
 
6360
        - the above case can't occur if inner part of outer join has more 
 
6361
          than one table: table with no matches will not be marked as const.
 
6362
 
 
6363
        - Ideally we should add 1 to records_read for every possible null-
 
6364
          complemented row. We're not doing it because: 1. it will require
 
6365
          non-trivial code and add overhead. 2. The value of records_read
 
6366
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6367
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6368
      */
 
6369
      if (pos->records_read > DBL_EPSILON)
 
6370
        found*= pos->records_read;
 
6371
    }
 
6372
  }
 
6373
  return found;
 
6374
}
 
6375
 
918
6376
 
919
6377
/**
920
 
  This function is only called for const items on fields which are keys.
921
 
 
922
 
  @return
923
 
    returns 1 if there was some conversion made when the field was stored.
 
6378
  Set up join struct according to best position.
924
6379
*/
925
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
926
 
{
927
 
  bool error;
928
 
  Table *table= field->getTable();
929
 
  Session *session= table->in_use;
930
 
  ha_rows cuted_fields=session->cuted_fields;
931
 
 
932
 
  /*
933
 
    we should restore old value of count_cuted_fields because
934
 
    store_val_in_field can be called from mysql_insert
935
 
    with select_insert, which make count_cuted_fields= 1
936
 
   */
937
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
938
 
  session->count_cuted_fields= check_flag;
939
 
  error= item->save_in_field(field, 1);
940
 
  session->count_cuted_fields= old_count_cuted_fields;
941
 
  return error || cuted_fields != session->cuted_fields;
942
 
}
943
 
 
944
 
inline void add_cond_and_fix(Item **e1, Item *e2)
945
 
{
946
 
  if (*e1)
 
6380
 
 
6381
static bool
 
6382
get_best_combination(JOIN *join)
 
6383
{
 
6384
  uint i,tablenr;
 
6385
  table_map used_tables;
 
6386
  JOIN_TAB *join_tab,*j;
 
6387
  KEYUSE *keyuse;
 
6388
  uint table_count;
 
6389
  THD *thd=join->thd;
 
6390
 
 
6391
  table_count=join->tables;
 
6392
  if (!(join->join_tab=join_tab=
 
6393
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6394
    return(true);
 
6395
 
 
6396
  join->full_join=0;
 
6397
 
 
6398
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6399
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
947
6400
  {
948
 
    Item *res;
949
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6401
    Table *form;
 
6402
    *j= *join->best_positions[tablenr].table;
 
6403
    form=join->table[tablenr]=j->table;
 
6404
    used_tables|= form->map;
 
6405
    form->reginfo.join_tab=j;
 
6406
    if (!*j->on_expr_ref)
 
6407
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6408
    if (j->type == JT_CONST)
 
6409
      continue;                                 // Handled in make_join_stat..
 
6410
 
 
6411
    j->ref.key = -1;
 
6412
    j->ref.key_parts=0;
 
6413
 
 
6414
    if (j->type == JT_SYSTEM)
 
6415
      continue;
 
6416
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
950
6417
    {
951
 
      *e1= res;
952
 
      res->quick_fix_field();
 
6418
      j->type=JT_ALL;
 
6419
      if (tablenr != join->const_tables)
 
6420
        join->full_join=1;
953
6421
    }
 
6422
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6423
      return(true);                        // Something went wrong
954
6424
  }
955
 
  else
956
 
    *e1= e2;
 
6425
 
 
6426
  for (i=0 ; i < table_count ; i++)
 
6427
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6428
  update_depend_map(join);
 
6429
  return(0);
957
6430
}
958
6431
 
959
 
bool create_ref_for_key(Join *join, 
960
 
                        JoinTable *j, 
961
 
                        optimizer::KeyUse *org_keyuse,
962
 
                        table_map used_tables)
 
6432
 
 
6433
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6434
                               table_map used_tables)
963
6435
{
964
 
  optimizer::KeyUse *keyuse= org_keyuse;
965
 
  Session  *session= join->session;
966
 
  uint32_t keyparts;
967
 
  uint32_t length;
968
 
  uint32_t key;
969
 
  Table *table= NULL;
970
 
  KeyInfo *keyinfo= NULL;
 
6436
  KEYUSE *keyuse=org_keyuse;
 
6437
  THD  *thd= join->thd;
 
6438
  uint keyparts,length,key;
 
6439
  Table *table;
 
6440
  KEY *keyinfo;
971
6441
 
972
6442
  /*  Use best key from find_best */
973
 
  table= j->table;
974
 
  key= keyuse->getKey();
975
 
  keyinfo= table->key_info + key;
 
6443
  table=j->table;
 
6444
  key=keyuse->key;
 
6445
  keyinfo=table->key_info+key;
976
6446
 
977
6447
  {
978
 
    keyparts= length= 0;
979
 
    uint32_t found_part_ref_or_null= 0;
 
6448
    keyparts=length=0;
 
6449
    uint found_part_ref_or_null= 0;
980
6450
    /*
981
6451
      Calculate length for the used key
982
6452
      Stop if there is a missing key part or when we find second key_part
984
6454
    */
985
6455
    do
986
6456
    {
987
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6457
      if (!(~used_tables & keyuse->used_tables))
988
6458
      {
989
 
        if (keyparts == keyuse->getKeypart() &&
990
 
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
991
 
        {
992
 
          keyparts++;
993
 
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
994
 
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
995
 
        }
 
6459
        if (keyparts == keyuse->keypart &&
 
6460
            !(found_part_ref_or_null & keyuse->optimize))
 
6461
        {
 
6462
          keyparts++;
 
6463
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6464
          found_part_ref_or_null|= keyuse->optimize;
 
6465
        }
996
6466
      }
997
6467
      keyuse++;
998
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6468
    } while (keyuse->table == table && keyuse->key == key);
999
6469
  }
1000
6470
 
1001
6471
  /* set up fieldref */
1003
6473
  j->ref.key_parts=keyparts;
1004
6474
  j->ref.key_length=length;
1005
6475
  j->ref.key=(int) key;
1006
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1007
 
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1008
 
               (keyparts+1)))) ||
1009
 
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
1010
 
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6476
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6477
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6478
                                                   (keyparts+1)))) ||
 
6479
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6480
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1011
6481
  {
1012
6482
    return(true);
1013
6483
  }
1017
6487
  j->ref.disable_cache= false;
1018
6488
  keyuse=org_keyuse;
1019
6489
 
1020
 
  StoredKey **ref_key= j->ref.key_copy;
1021
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6490
  store_key **ref_key= j->ref.key_copy;
 
6491
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
1022
6492
  bool keyuse_uses_no_tables= true;
1023
6493
  {
1024
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6494
    uint i;
 
6495
    for (i=0 ; i < keyparts ; keyuse++,i++)
1025
6496
    {
1026
 
      while (keyuse->getKeypart() != i ||
1027
 
             ((~used_tables) & keyuse->getUsedTables()))
1028
 
        keyuse++;       /* Skip other parts */
 
6497
      while (keyuse->keypart != i ||
 
6498
             ((~used_tables) & keyuse->used_tables))
 
6499
        keyuse++;                               /* Skip other parts */
1029
6500
 
1030
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1031
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1032
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1033
 
      if (keyuse->isNullRejected())
 
6501
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
 
6502
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6503
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6504
      if (keyuse->null_rejecting) 
1034
6505
        j->ref.null_rejecting |= 1 << i;
1035
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1036
 
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1037
 
      {         // Compare against constant
1038
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6506
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6507
      if (!keyuse->used_tables &&
 
6508
          !(join->select_options & SELECT_DESCRIBE))
 
6509
      {                                 // Compare against constant
 
6510
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1039
6511
                           key_buff + maybe_null,
1040
6512
                           maybe_null ?  key_buff : 0,
1041
 
                           keyinfo->key_part[i].length, keyuse->getVal());
1042
 
        if (session->is_fatal_error)
1043
 
          return(true);
1044
 
        tmp.copy();
 
6513
                           keyinfo->key_part[i].length, keyuse->val);
 
6514
        if (thd->is_fatal_error)
 
6515
          return(true);
 
6516
        tmp.copy();
1045
6517
      }
1046
6518
      else
1047
 
        *ref_key++= get_store_key(session,
1048
 
          keyuse,join->const_table_map,
1049
 
          &keyinfo->key_part[i],
1050
 
          key_buff, maybe_null);
 
6519
        *ref_key++= get_store_key(thd,
 
6520
                                  keyuse,join->const_table_map,
 
6521
                                  &keyinfo->key_part[i],
 
6522
                                  key_buff, maybe_null);
1051
6523
      /*
1052
 
        Remember if we are going to use REF_OR_NULL
1053
 
        But only if field _really_ can be null i.e. we force AM_REF
1054
 
        instead of AM_REF_OR_NULL in case if field can't be null
 
6524
        Remember if we are going to use REF_OR_NULL
 
6525
        But only if field _really_ can be null i.e. we force JT_REF
 
6526
        instead of JT_REF_OR_NULL in case if field can't be null
1055
6527
      */
1056
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1057
 
        null_ref_key= key_buff;
 
6528
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6529
        null_ref_key= key_buff;
1058
6530
      key_buff+=keyinfo->key_part[i].store_length;
1059
6531
    }
1060
6532
  }
1061
 
  *ref_key= 0;       // end_marker
1062
 
  if (j->type == AM_CONST)
 
6533
  *ref_key=0;                           // end_marker
 
6534
  if (j->type == JT_CONST)
1063
6535
    j->table->const_table= 1;
1064
6536
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1065
6537
           keyparts != keyinfo->key_parts || null_ref_key)
1066
6538
  {
1067
6539
    /* Must read with repeat */
1068
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6540
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1069
6541
    j->ref.null_ref_key= null_ref_key;
1070
6542
  }
1071
6543
  else if (keyuse_uses_no_tables)
1077
6549
      Here we should not mark the table as a 'const' as a field may
1078
6550
      have a 'normal' value or a NULL value.
1079
6551
    */
1080
 
    j->type= AM_CONST;
1081
 
  }
1082
 
  else
1083
 
    j->type= AM_EQ_REF;
1084
 
  return 0;
1085
 
}
 
6552
    j->type=JT_CONST;
 
6553
  }
 
6554
  else
 
6555
    j->type=JT_EQ_REF;
 
6556
  return(0);
 
6557
}
 
6558
 
 
6559
 
 
6560
 
 
6561
static store_key *
 
6562
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6563
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
 
6564
{
 
6565
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6566
  {
 
6567
    return new store_key_const_item(thd,
 
6568
                                    key_part->field,
 
6569
                                    key_buff + maybe_null,
 
6570
                                    maybe_null ? key_buff : 0,
 
6571
                                    key_part->length,
 
6572
                                    keyuse->val);
 
6573
  }
 
6574
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6575
           (keyuse->val->type() == Item::REF_ITEM &&
 
6576
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6577
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6578
             Item_ref::DIRECT_REF && 
 
6579
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6580
    return new store_key_field(thd,
 
6581
                               key_part->field,
 
6582
                               key_buff + maybe_null,
 
6583
                               maybe_null ? key_buff : 0,
 
6584
                               key_part->length,
 
6585
                               ((Item_field*) keyuse->val->real_item())->field,
 
6586
                               keyuse->val->full_name());
 
6587
  return new store_key_item(thd,
 
6588
                            key_part->field,
 
6589
                            key_buff + maybe_null,
 
6590
                            maybe_null ? key_buff : 0,
 
6591
                            key_part->length,
 
6592
                            keyuse->val);
 
6593
}
 
6594
 
 
6595
/**
 
6596
  This function is only called for const items on fields which are keys.
 
6597
 
 
6598
  @return
 
6599
    returns 1 if there was some conversion made when the field was stored.
 
6600
*/
 
6601
 
 
6602
bool
 
6603
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6604
{
 
6605
  bool error;
 
6606
  Table *table= field->table;
 
6607
  THD *thd= table->in_use;
 
6608
  ha_rows cuted_fields=thd->cuted_fields;
 
6609
 
 
6610
  /*
 
6611
    we should restore old value of count_cuted_fields because
 
6612
    store_val_in_field can be called from mysql_insert 
 
6613
    with select_insert, which make count_cuted_fields= 1
 
6614
   */
 
6615
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6616
  thd->count_cuted_fields= check_flag;
 
6617
  error= item->save_in_field(field, 1);
 
6618
  thd->count_cuted_fields= old_count_cuted_fields;
 
6619
  return error || cuted_fields != thd->cuted_fields;
 
6620
}
 
6621
 
 
6622
 
 
6623
static bool
 
6624
make_simple_join(JOIN *join,Table *tmp_table)
 
6625
{
 
6626
  Table **tableptr;
 
6627
  JOIN_TAB *join_tab;
 
6628
 
 
6629
  /*
 
6630
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6631
    to this function through JOIN::exec (may happen for sub-queries).
 
6632
  */
 
6633
  if (!join->table_reexec)
 
6634
  {
 
6635
    if (!(join->table_reexec= (Table**) join->thd->alloc(sizeof(Table*))))
 
6636
      return(true);                        /* purecov: inspected */
 
6637
    if (join->tmp_join)
 
6638
      join->tmp_join->table_reexec= join->table_reexec;
 
6639
  }
 
6640
  if (!join->join_tab_reexec)
 
6641
  {
 
6642
    if (!(join->join_tab_reexec=
 
6643
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6644
      return(true);                        /* purecov: inspected */
 
6645
    if (join->tmp_join)
 
6646
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6647
  }
 
6648
  tableptr= join->table_reexec;
 
6649
  join_tab= join->join_tab_reexec;
 
6650
 
 
6651
  join->join_tab=join_tab;
 
6652
  join->table=tableptr; tableptr[0]=tmp_table;
 
6653
  join->tables=1;
 
6654
  join->const_tables=0;
 
6655
  join->const_table_map=0;
 
6656
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6657
    join->tmp_table_param.func_count=0;
 
6658
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6659
  join->first_record=join->sort_and_group=0;
 
6660
  join->send_records=(ha_rows) 0;
 
6661
  join->group=0;
 
6662
  join->row_limit=join->unit->select_limit_cnt;
 
6663
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6664
 
 
6665
  join_tab->cache.buff=0;                       /* No caching */
 
6666
  join_tab->table=tmp_table;
 
6667
  join_tab->select=0;
 
6668
  join_tab->select_cond=0;
 
6669
  join_tab->quick=0;
 
6670
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6671
  join_tab->keys.init();
 
6672
  join_tab->keys.set_all();                     /* test everything in quick */
 
6673
  join_tab->info=0;
 
6674
  join_tab->on_expr_ref=0;
 
6675
  join_tab->last_inner= 0;
 
6676
  join_tab->first_unmatched= 0;
 
6677
  join_tab->ref.key = -1;
 
6678
  join_tab->not_used_in_distinct=0;
 
6679
  join_tab->read_first_record= join_init_read_record;
 
6680
  join_tab->join=join;
 
6681
  join_tab->ref.key_parts= 0;
 
6682
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6683
  join_tab->do_firstmatch= NULL;
 
6684
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6685
  tmp_table->status=0;
 
6686
  tmp_table->null_row=0;
 
6687
  return(false);
 
6688
}
 
6689
 
 
6690
 
 
6691
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6692
{
 
6693
  if (*e1)
 
6694
  {
 
6695
    Item *res;
 
6696
    if ((res= new Item_cond_and(*e1, e2)))
 
6697
    {
 
6698
      *e1= res;
 
6699
      res->quick_fix_field();
 
6700
    }
 
6701
  }
 
6702
  else
 
6703
    *e1= e2;
 
6704
}
 
6705
 
1086
6706
 
1087
6707
/**
1088
6708
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1097
6717
    add "t1.field IS NOT NULL" to t1's table condition. @n
1098
6718
 
1099
6719
    Description of the optimization:
1100
 
 
 
6720
    
1101
6721
      We look through equalities choosen to perform ref/eq_ref access,
1102
6722
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1103
6723
      (where othertbl is a non-const table and othertbl.field may be NULL)
1125
6745
      This optimization doesn't affect the choices that ref, range, or join
1126
6746
      optimizer make. This was intentional because this was added after 4.1
1127
6747
      was GA.
1128
 
 
 
6748
      
1129
6749
    Implementation overview
1130
6750
      1. update_ref_and_keys() accumulates info about null-rejecting
1131
 
         predicates in in KeyField::null_rejecting
1132
 
      1.1 add_key_part saves these to KeyUse.
1133
 
      2. create_ref_for_key copies them to table_reference_st.
 
6751
         predicates in in KEY_FIELD::null_rejecting
 
6752
      1.1 add_key_part saves these to KEYUSE.
 
6753
      2. create_ref_for_key copies them to TABLE_REF.
1134
6754
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1135
 
         appropiate JoinTable members.
 
6755
         appropiate JOIN_TAB members.
1136
6756
*/
1137
 
void add_not_null_conds(Join *join)
 
6757
 
 
6758
static void add_not_null_conds(JOIN *join)
1138
6759
{
1139
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6760
  for (uint i=join->const_tables ; i < join->tables ; i++)
1140
6761
  {
1141
 
    JoinTable *tab=join->join_tab+i;
1142
 
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1143
 
         tab->type == AM_REF_OR_NULL) &&
 
6762
    JOIN_TAB *tab=join->join_tab+i;
 
6763
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6764
         tab->type == JT_REF_OR_NULL) &&
1144
6765
        !tab->table->maybe_null)
1145
6766
    {
1146
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6767
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
1147
6768
      {
1148
6769
        if (tab->ref.null_rejecting & (1 << keypart))
1149
6770
        {
1151
6772
          Item *notnull;
1152
6773
          assert(item->type() == Item::FIELD_ITEM);
1153
6774
          Item_field *not_null_item= (Item_field*)item;
1154
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6775
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1155
6776
          /*
1156
6777
            For UPDATE queries such as:
1157
6778
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1163
6784
            return;
1164
6785
          /*
1165
6786
            We need to do full fix_fields() call here in order to have correct
1166
 
            notnull->const_item(). This is needed e.g. by test_quick_select
1167
 
            when it is called from make_join_select after this function is
 
6787
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6788
            when it is called from make_join_select after this function is 
1168
6789
            called.
1169
6790
          */
1170
 
          if (notnull->fix_fields(join->session, &notnull))
 
6791
          if (notnull->fix_fields(join->thd, &notnull))
1171
6792
            return;
1172
6793
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1173
6794
        }
1192
6813
    -  pointer to the guarded predicate, if success
1193
6814
    -  0, otherwise
1194
6815
*/
1195
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6816
 
 
6817
static COND*
 
6818
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1196
6819
{
1197
6820
  COND *tmp;
1198
6821
  assert(cond != 0);
1208
6831
  return tmp;
1209
6832
}
1210
6833
 
 
6834
 
 
6835
/**
 
6836
  Fill in outer join related info for the execution plan structure.
 
6837
 
 
6838
    For each outer join operation left after simplification of the
 
6839
    original query the function set up the following pointers in the linear
 
6840
    structure join->join_tab representing the selected execution plan.
 
6841
    The first inner table t0 for the operation is set to refer to the last
 
6842
    inner table tk through the field t0->last_inner.
 
6843
    Any inner table ti for the operation are set to refer to the first
 
6844
    inner table ti->first_inner.
 
6845
    The first inner table t0 for the operation is set to refer to the
 
6846
    first inner table of the embedding outer join operation, if there is any,
 
6847
    through the field t0->first_upper.
 
6848
    The on expression for the outer join operation is attached to the
 
6849
    corresponding first inner table through the field t0->on_expr_ref.
 
6850
    Here ti are structures of the JOIN_TAB type.
 
6851
 
 
6852
  EXAMPLE. For the query: 
 
6853
  @code
 
6854
        SELECT * FROM t1
 
6855
                      LEFT JOIN
 
6856
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6857
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6858
          WHERE t1.c > 5,
 
6859
  @endcode
 
6860
 
 
6861
    given the execution plan with the table order t1,t2,t3,t4
 
6862
    is selected, the following references will be set;
 
6863
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6864
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6865
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6866
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6867
 
 
6868
  @param join   reference to the info fully describing the query
 
6869
 
 
6870
  @note
 
6871
    The function assumes that the simplification procedure has been
 
6872
    already applied to the join query (see simplify_joins).
 
6873
    This function can be called only after the execution plan
 
6874
    has been chosen.
 
6875
*/
 
6876
 
 
6877
static void
 
6878
make_outerjoin_info(JOIN *join)
 
6879
{
 
6880
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
6881
  {
 
6882
    JOIN_TAB *tab=join->join_tab+i;
 
6883
    Table *table=tab->table;
 
6884
    TableList *tbl= table->pos_in_table_list;
 
6885
    TableList *embedding= tbl->embedding;
 
6886
 
 
6887
    if (tbl->outer_join)
 
6888
    {
 
6889
      /* 
 
6890
        Table tab is the only one inner table for outer join.
 
6891
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6892
        is in the query above.)
 
6893
      */
 
6894
      tab->last_inner= tab->first_inner= tab;
 
6895
      tab->on_expr_ref= &tbl->on_expr;
 
6896
      tab->cond_equal= tbl->cond_equal;
 
6897
      if (embedding)
 
6898
        tab->first_upper= embedding->nested_join->first_nested;
 
6899
    }    
 
6900
    for ( ; embedding ; embedding= embedding->embedding)
 
6901
    {
 
6902
      /* Ignore sj-nests: */
 
6903
      if (!embedding->on_expr)
 
6904
        continue;
 
6905
      nested_join_st *nested_join= embedding->nested_join;
 
6906
      if (!nested_join->counter_)
 
6907
      {
 
6908
        /* 
 
6909
          Table tab is the first inner table for nested_join.
 
6910
          Save reference to it in the nested join structure.
 
6911
        */ 
 
6912
        nested_join->first_nested= tab;
 
6913
        tab->on_expr_ref= &embedding->on_expr;
 
6914
        tab->cond_equal= tbl->cond_equal;
 
6915
        if (embedding->embedding)
 
6916
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6917
      }
 
6918
      if (!tab->first_inner)  
 
6919
        tab->first_inner= nested_join->first_nested;
 
6920
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6921
        break;
 
6922
      /* Table tab is the last inner table for nested join. */
 
6923
      nested_join->first_nested->last_inner= tab;
 
6924
    }
 
6925
  }
 
6926
  return;
 
6927
}
 
6928
 
 
6929
 
 
6930
static bool
 
6931
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6932
{
 
6933
  THD *thd= join->thd;
 
6934
  if (select)
 
6935
  {
 
6936
    add_not_null_conds(join);
 
6937
    table_map used_tables;
 
6938
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6939
    {                        /* there may be a select without a cond. */    
 
6940
      if (join->tables > 1)
 
6941
        cond->update_used_tables();             // Tablenr may have changed
 
6942
      if (join->const_tables == join->tables &&
 
6943
          thd->lex->current_select->master_unit() ==
 
6944
          &thd->lex->unit)              // not upper level SELECT
 
6945
        join->const_table_map|=RAND_TABLE_BIT;
 
6946
      {                                         // Check const tables
 
6947
        COND *const_cond=
 
6948
          make_cond_for_table(cond,
 
6949
                              join->const_table_map,
 
6950
                              (table_map) 0, 1);
 
6951
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6952
             tab < join->join_tab+join->tables ; tab++)
 
6953
        {
 
6954
          if (*tab->on_expr_ref)
 
6955
          {
 
6956
            JOIN_TAB *cond_tab= tab->first_inner;
 
6957
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6958
                                           join->const_table_map,
 
6959
                                           (  table_map) 0, 0);
 
6960
            if (!tmp)
 
6961
              continue;
 
6962
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6963
            if (!tmp)
 
6964
              return(1);
 
6965
            tmp->quick_fix_field();
 
6966
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6967
                                    new Item_cond_and(cond_tab->select_cond,
 
6968
                                                      tmp);
 
6969
            if (!cond_tab->select_cond)
 
6970
              return(1);
 
6971
            cond_tab->select_cond->quick_fix_field();
 
6972
          }       
 
6973
        }
 
6974
        if (const_cond && !const_cond->val_int())
 
6975
        {
 
6976
          return(1);     // Impossible const condition
 
6977
        }
 
6978
      }
 
6979
    }
 
6980
    used_tables=((select->const_tables=join->const_table_map) |
 
6981
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
6982
    for (uint i=join->const_tables ; i < join->tables ; i++)
 
6983
    {
 
6984
      JOIN_TAB *tab=join->join_tab+i;
 
6985
      /*
 
6986
        first_inner is the X in queries like:
 
6987
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
6988
      */
 
6989
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
6990
      table_map current_map= tab->table->map;
 
6991
      bool use_quick_range=0;
 
6992
      COND *tmp;
 
6993
 
 
6994
      /*
 
6995
        Following force including random expression in last table condition.
 
6996
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
6997
      */
 
6998
      if (i == join->tables-1)
 
6999
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7000
      used_tables|=current_map;
 
7001
 
 
7002
      if (tab->type == JT_REF && tab->quick &&
 
7003
          (uint) tab->ref.key == tab->quick->index &&
 
7004
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7005
      {
 
7006
        /* Range uses longer key;  Use this instead of ref on key */
 
7007
        tab->type=JT_ALL;
 
7008
        use_quick_range=1;
 
7009
        tab->use_quick=1;
 
7010
        tab->ref.key= -1;
 
7011
        tab->ref.key_parts=0;           // Don't use ref key.
 
7012
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7013
        /* 
 
7014
          We will use join cache here : prevent sorting of the first
 
7015
          table only and sort at the end.
 
7016
        */
 
7017
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7018
          join->full_join= 1;
 
7019
      }
 
7020
 
 
7021
      tmp= NULL;
 
7022
      if (cond)
 
7023
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7024
      if (cond && !tmp && tab->quick)
 
7025
      {                                         // Outer join
 
7026
        if (tab->type != JT_ALL)
 
7027
        {
 
7028
          /*
 
7029
            Don't use the quick method
 
7030
            We come here in the case where we have 'key=constant' and
 
7031
            the test is removed by make_cond_for_table()
 
7032
          */
 
7033
          delete tab->quick;
 
7034
          tab->quick= 0;
 
7035
        }
 
7036
        else
 
7037
        {
 
7038
          /*
 
7039
            Hack to handle the case where we only refer to a table
 
7040
            in the ON part of an OUTER JOIN. In this case we want the code
 
7041
            below to check if we should use 'quick' instead.
 
7042
          */
 
7043
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7044
        }
 
7045
 
 
7046
      }
 
7047
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7048
          tab->type == JT_EQ_REF)
 
7049
      {
 
7050
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7051
                                       thd->memdup((uchar*) select,
 
7052
                                                   sizeof(*select)));
 
7053
        if (!sel)
 
7054
          return(1);                    // End of memory
 
7055
        /*
 
7056
          If tab is an inner table of an outer join operation,
 
7057
          add a match guard to the pushed down predicate.
 
7058
          The guard will turn the predicate on only after
 
7059
          the first match for outer tables is encountered.
 
7060
        */        
 
7061
        if (cond && tmp)
 
7062
        {
 
7063
          /*
 
7064
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7065
            a cond, so neutralize the hack above.
 
7066
          */
 
7067
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7068
            return(1);
 
7069
          tab->select_cond=sel->cond=tmp;
 
7070
          /* Push condition to storage engine if this is enabled
 
7071
             and the condition is not guarded */
 
7072
          tab->table->file->pushed_cond= NULL;
 
7073
          if (thd->variables.engine_condition_pushdown)
 
7074
          {
 
7075
            COND *push_cond= 
 
7076
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7077
            if (push_cond)
 
7078
            {
 
7079
              /* Push condition to handler */
 
7080
              if (!tab->table->file->cond_push(push_cond))
 
7081
                tab->table->file->pushed_cond= push_cond;
 
7082
            }
 
7083
          }
 
7084
        }
 
7085
        else
 
7086
          tab->select_cond= sel->cond= NULL;
 
7087
 
 
7088
        sel->head=tab->table;
 
7089
        if (tab->quick)
 
7090
        {
 
7091
          /* Use quick key read if it's a constant and it's not used
 
7092
             with key reading */
 
7093
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7094
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7095
          {
 
7096
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7097
            sel->quick_keys.clear_all();
 
7098
            sel->needed_reg.clear_all();
 
7099
          }
 
7100
          else
 
7101
          {
 
7102
            delete tab->quick;
 
7103
          }
 
7104
          tab->quick=0;
 
7105
        }
 
7106
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7107
        if (i == join->const_tables && ref_key)
 
7108
        {
 
7109
          if (!tab->const_keys.is_clear_all() &&
 
7110
              tab->table->reginfo.impossible_range)
 
7111
            return(1);
 
7112
        }
 
7113
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7114
        {
 
7115
          if (!tab->const_keys.is_clear_all() &&
 
7116
              tab->table->reginfo.impossible_range)
 
7117
            return(1);                          // Impossible range
 
7118
          /*
 
7119
            We plan to scan all rows.
 
7120
            Check again if we should use an index.
 
7121
            We could have used an column from a previous table in
 
7122
            the index if we are using limit and this is the first table
 
7123
          */
 
7124
 
 
7125
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7126
              (!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)))
 
7127
          {
 
7128
            /* Join with outer join condition */
 
7129
            COND *orig_cond=sel->cond;
 
7130
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7131
 
 
7132
            /*
 
7133
              We can't call sel->cond->fix_fields,
 
7134
              as it will break tab->on_expr if it's AND condition
 
7135
              (fix_fields currently removes extra AND/OR levels).
 
7136
              Yet attributes of the just built condition are not needed.
 
7137
              Thus we call sel->cond->quick_fix_field for safety.
 
7138
            */
 
7139
            if (sel->cond && !sel->cond->fixed)
 
7140
              sel->cond->quick_fix_field();
 
7141
 
 
7142
            if (sel->test_quick_select(thd, tab->keys,
 
7143
                                       used_tables & ~ current_map,
 
7144
                                       (join->select_options &
 
7145
                                        OPTION_FOUND_ROWS ?
 
7146
                                        HA_POS_ERROR :
 
7147
                                        join->unit->select_limit_cnt), 0,
 
7148
                                        false) < 0)
 
7149
            {
 
7150
              /*
 
7151
                Before reporting "Impossible WHERE" for the whole query
 
7152
                we have to check isn't it only "impossible ON" instead
 
7153
              */
 
7154
              sel->cond=orig_cond;
 
7155
              if (!*tab->on_expr_ref ||
 
7156
                  sel->test_quick_select(thd, tab->keys,
 
7157
                                         used_tables & ~ current_map,
 
7158
                                         (join->select_options &
 
7159
                                          OPTION_FOUND_ROWS ?
 
7160
                                          HA_POS_ERROR :
 
7161
                                          join->unit->select_limit_cnt),0,
 
7162
                                          false) < 0)
 
7163
                return(1);                      // Impossible WHERE
 
7164
            }
 
7165
            else
 
7166
              sel->cond=orig_cond;
 
7167
 
 
7168
            /* Fix for EXPLAIN */
 
7169
            if (sel->quick)
 
7170
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7171
          }
 
7172
          else
 
7173
          {
 
7174
            sel->needed_reg=tab->needed_reg;
 
7175
            sel->quick_keys.clear_all();
 
7176
          }
 
7177
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7178
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7179
          {
 
7180
            tab->keys=sel->quick_keys;
 
7181
            tab->keys.merge(sel->needed_reg);
 
7182
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7183
                             (select->quick_keys.is_clear_all() ||
 
7184
                              (select->quick &&
 
7185
                               (select->quick->records >= 100L)))) ?
 
7186
              2 : 1;
 
7187
            sel->read_tables= used_tables & ~current_map;
 
7188
          }
 
7189
          if (i != join->const_tables && tab->use_quick != 2)
 
7190
          {                                     /* Read with cache */
 
7191
            if (cond &&
 
7192
                (tmp=make_cond_for_table(cond,
 
7193
                                         join->const_table_map |
 
7194
                                         current_map,
 
7195
                                         current_map, 0)))
 
7196
            {
 
7197
              tab->cache.select=(SQL_SELECT*)
 
7198
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
 
7199
              tab->cache.select->cond=tmp;
 
7200
              tab->cache.select->read_tables=join->const_table_map;
 
7201
            }
 
7202
          }
 
7203
        }
 
7204
      }
 
7205
      
 
7206
      /* 
 
7207
        Push down conditions from all on expressions.
 
7208
        Each of these conditions are guarded by a variable
 
7209
        that turns if off just before null complemented row for
 
7210
        outer joins is formed. Thus, the condition from an
 
7211
        'on expression' are guaranteed not to be checked for
 
7212
        the null complemented row.
 
7213
      */ 
 
7214
 
 
7215
      /* First push down constant conditions from on expressions */
 
7216
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7217
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7218
      {
 
7219
        if (*join_tab->on_expr_ref)
 
7220
        {
 
7221
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7222
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7223
                                         join->const_table_map,
 
7224
                                         (table_map) 0, 0);
 
7225
          if (!tmp)
 
7226
            continue;
 
7227
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7228
          if (!tmp)
 
7229
            return(1);
 
7230
          tmp->quick_fix_field();
 
7231
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7232
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7233
          if (!cond_tab->select_cond)
 
7234
            return(1);
 
7235
          cond_tab->select_cond->quick_fix_field();
 
7236
        }       
 
7237
      }
 
7238
 
 
7239
      /* Push down non-constant conditions from on expressions */
 
7240
      JOIN_TAB *last_tab= tab;
 
7241
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7242
      {  
 
7243
        /* 
 
7244
          Table tab is the last inner table of an outer join.
 
7245
          An on expression is always attached to it.
 
7246
        */     
 
7247
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7248
 
 
7249
        table_map used_tables2= (join->const_table_map |
 
7250
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7251
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7252
        {
 
7253
          current_map= tab->table->map;
 
7254
          used_tables2|= current_map;
 
7255
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7256
                                              current_map, 0);
 
7257
          if (tmp_cond)
 
7258
          {
 
7259
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7260
            /*
 
7261
              First add the guards for match variables of
 
7262
              all embedding outer join operations.
 
7263
            */
 
7264
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7265
                                                     tmp_cond,
 
7266
                                                     first_inner_tab)))
 
7267
              return(1);
 
7268
            /* 
 
7269
              Now add the guard turning the predicate off for 
 
7270
              the null complemented row.
 
7271
            */ 
 
7272
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7273
                                              &first_inner_tab->
 
7274
                                              not_null_compl);
 
7275
            if (tmp_cond)
 
7276
              tmp_cond->quick_fix_field();
 
7277
            /* Add the predicate to other pushed down predicates */
 
7278
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7279
                                  new Item_cond_and(cond_tab->select_cond,
 
7280
                                                    tmp_cond);
 
7281
            if (!cond_tab->select_cond)
 
7282
              return(1);
 
7283
            cond_tab->select_cond->quick_fix_field();
 
7284
          }              
 
7285
        }
 
7286
        first_inner_tab= first_inner_tab->first_upper;       
 
7287
      }
 
7288
    }
 
7289
  }
 
7290
  return(0);
 
7291
}
 
7292
 
 
7293
 
 
7294
/* 
 
7295
  Check if given expression uses only table fields covered by the given index
 
7296
 
 
7297
  SYNOPSIS
 
7298
    uses_index_fields_only()
 
7299
      item           Expression to check
 
7300
      tbl            The table having the index
 
7301
      keyno          The index number
 
7302
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7303
 
 
7304
  DESCRIPTION
 
7305
    Check if given expression only uses fields covered by index #keyno in the
 
7306
    table tbl. The expression can use any fields in any other tables.
 
7307
    
 
7308
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7309
    handled outside of this function.
 
7310
 
 
7311
  RETURN
 
7312
    true   Yes
 
7313
    false  No
 
7314
*/
 
7315
 
 
7316
bool uses_index_fields_only(Item *item, Table *tbl, uint keyno, 
 
7317
                            bool other_tbls_ok)
 
7318
{
 
7319
  if (item->const_item())
 
7320
    return true;
 
7321
 
 
7322
  /* 
 
7323
    Don't push down the triggered conditions. Nested outer joins execution 
 
7324
    code may need to evaluate a condition several times (both triggered and
 
7325
    untriggered), and there is no way to put thi
 
7326
    TODO: Consider cloning the triggered condition and using the copies for:
 
7327
      1. push the first copy down, to have most restrictive index condition
 
7328
         possible
 
7329
      2. Put the second copy into tab->select_cond. 
 
7330
  */
 
7331
  if (item->type() == Item::FUNC_ITEM && 
 
7332
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7333
    return false;
 
7334
 
 
7335
  if (!(item->used_tables() & tbl->map))
 
7336
    return other_tbls_ok;
 
7337
 
 
7338
  Item::Type item_type= item->type();
 
7339
  switch (item_type) {
 
7340
  case Item::FUNC_ITEM:
 
7341
    {
 
7342
      /* This is a function, apply condition recursively to arguments */
 
7343
      Item_func *item_func= (Item_func*)item;
 
7344
      Item **child;
 
7345
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7346
      for (child= item_func->arguments(); child != item_end; child++)
 
7347
      {
 
7348
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7349
          return false;
 
7350
      }
 
7351
      return true;
 
7352
    }
 
7353
  case Item::COND_ITEM:
 
7354
    {
 
7355
      /* This is a function, apply condition recursively to arguments */
 
7356
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7357
      Item *item;
 
7358
      while ((item=li++))
 
7359
      {
 
7360
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7361
          return false;
 
7362
      }
 
7363
      return true;
 
7364
    }
 
7365
  case Item::FIELD_ITEM:
 
7366
    {
 
7367
      Item_field *item_field= (Item_field*)item;
 
7368
      if (item_field->field->table != tbl) 
 
7369
        return true;
 
7370
      return item_field->field->part_of_key.is_set(keyno);
 
7371
    }
 
7372
  case Item::REF_ITEM:
 
7373
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7374
                                  other_tbls_ok);
 
7375
  default:
 
7376
    return false; /* Play it safe, don't push unknown non-const items */
 
7377
  }
 
7378
}
 
7379
 
 
7380
 
1211
7381
#define ICP_COND_USES_INDEX_ONLY 10
1212
7382
 
1213
 
 
1214
 
/**
1215
 
  cleanup JoinTable.
1216
 
*/
1217
 
void JoinTable::cleanup()
 
7383
/*
 
7384
  Get a part of the condition that can be checked using only index fields
 
7385
 
 
7386
  SYNOPSIS
 
7387
    make_cond_for_index()
 
7388
      cond           The source condition
 
7389
      table          The table that is partially available
 
7390
      keyno          The index in the above table. Only fields covered by the index
 
7391
                     are available
 
7392
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7393
 
 
7394
  DESCRIPTION
 
7395
    Get a part of the condition that can be checked when for the given table 
 
7396
    we have values only of fields covered by some index. The condition may
 
7397
    refer to other tables, it is assumed that we have values of all of their 
 
7398
    fields.
 
7399
 
 
7400
    Example:
 
7401
      make_cond_for_index(
 
7402
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7403
          t2, keyno(t2.key1)) 
 
7404
      will return
 
7405
        "cond(t1.field) AND cond(t2.key2)"
 
7406
 
 
7407
  RETURN
 
7408
    Index condition, or NULL if no condition could be inferred.
 
7409
*/
 
7410
 
 
7411
Item *make_cond_for_index(Item *cond, Table *table, uint keyno,
 
7412
                          bool other_tbls_ok)
 
7413
{
 
7414
  if (!cond)
 
7415
    return NULL;
 
7416
  if (cond->type() == Item::COND_ITEM)
 
7417
  {
 
7418
    uint n_marked= 0;
 
7419
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7420
    {
 
7421
      Item_cond_and *new_cond=new Item_cond_and;
 
7422
      if (!new_cond)
 
7423
        return (COND*) 0;
 
7424
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7425
      Item *item;
 
7426
      while ((item=li++))
 
7427
      {
 
7428
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7429
        if (fix)
 
7430
          new_cond->argument_list()->push_back(fix);
 
7431
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7432
      }
 
7433
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7434
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7435
      switch (new_cond->argument_list()->elements) {
 
7436
      case 0:
 
7437
        return (COND*) 0;
 
7438
      case 1:
 
7439
        return new_cond->argument_list()->head();
 
7440
      default:
 
7441
        new_cond->quick_fix_field();
 
7442
        return new_cond;
 
7443
      }
 
7444
    }
 
7445
    else /* It's OR */
 
7446
    {
 
7447
      Item_cond_or *new_cond=new Item_cond_or;
 
7448
      if (!new_cond)
 
7449
        return (COND*) 0;
 
7450
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7451
      Item *item;
 
7452
      while ((item=li++))
 
7453
      {
 
7454
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7455
        if (!fix)
 
7456
          return (COND*) 0;
 
7457
        new_cond->argument_list()->push_back(fix);
 
7458
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7459
      }
 
7460
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7461
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7462
      new_cond->quick_fix_field();
 
7463
      new_cond->top_level_item();
 
7464
      return new_cond;
 
7465
    }
 
7466
  }
 
7467
 
 
7468
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7469
    return (COND*) 0;
 
7470
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7471
  return cond;
 
7472
}
 
7473
 
 
7474
 
 
7475
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7476
{
 
7477
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7478
    return 0; /* Already checked */
 
7479
 
 
7480
  if (cond->type() == Item::COND_ITEM)
 
7481
  {
 
7482
    table_map tbl_map= 0;
 
7483
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7484
    {
 
7485
      /* Create new top level AND item */
 
7486
      Item_cond_and *new_cond=new Item_cond_and;
 
7487
      if (!new_cond)
 
7488
        return (COND*) 0;
 
7489
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7490
      Item *item;
 
7491
      while ((item=li++))
 
7492
      {
 
7493
        Item *fix= make_cond_remainder(item, exclude_index);
 
7494
        if (fix)
 
7495
        {
 
7496
          new_cond->argument_list()->push_back(fix);
 
7497
          tbl_map |= fix->used_tables();
 
7498
        }
 
7499
      }
 
7500
      switch (new_cond->argument_list()->elements) {
 
7501
      case 0:
 
7502
        return (COND*) 0;
 
7503
      case 1:
 
7504
        return new_cond->argument_list()->head();
 
7505
      default:
 
7506
        new_cond->quick_fix_field();
 
7507
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7508
        return new_cond;
 
7509
      }
 
7510
    }
 
7511
    else /* It's OR */
 
7512
    {
 
7513
      Item_cond_or *new_cond=new Item_cond_or;
 
7514
      if (!new_cond)
 
7515
        return (COND*) 0;
 
7516
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7517
      Item *item;
 
7518
      while ((item=li++))
 
7519
      {
 
7520
        Item *fix= make_cond_remainder(item, false);
 
7521
        if (!fix)
 
7522
          return (COND*) 0;
 
7523
        new_cond->argument_list()->push_back(fix);
 
7524
        tbl_map |= fix->used_tables();
 
7525
      }
 
7526
      new_cond->quick_fix_field();
 
7527
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7528
      new_cond->top_level_item();
 
7529
      return new_cond;
 
7530
    }
 
7531
  }
 
7532
  return cond;
 
7533
}
 
7534
 
 
7535
 
 
7536
/*
 
7537
  Try to extract and push the index condition
 
7538
 
 
7539
  SYNOPSIS
 
7540
    push_index_cond()
 
7541
      tab            A join tab that has tab->table->file and its condition
 
7542
                     in tab->select_cond
 
7543
      keyno          Index for which extract and push the condition
 
7544
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7545
 
 
7546
  DESCRIPTION
 
7547
    Try to extract and push the index condition down to table handler
 
7548
*/
 
7549
 
 
7550
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
 
7551
{
 
7552
  Item *idx_cond;
 
7553
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7554
      tab->join->thd->variables.engine_condition_pushdown)
 
7555
  {
 
7556
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7557
                                  other_tbls_ok);
 
7558
 
 
7559
    if (idx_cond)
 
7560
    {
 
7561
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7562
      Item *idx_remainder_cond= 
 
7563
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7564
 
 
7565
      /*
 
7566
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7567
        condition. 
 
7568
        TODO: This check happens to work on current ICP implementations, but
 
7569
        there may exist a compliant implementation that will not work 
 
7570
        correctly with it. Sort this out when we stabilize the condition
 
7571
        pushdown APIs.
 
7572
      */
 
7573
      if (idx_remainder_cond != idx_cond)
 
7574
        tab->ref.disable_cache= true;
 
7575
 
 
7576
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7577
 
 
7578
      if (row_cond)
 
7579
      {
 
7580
        if (!idx_remainder_cond)
 
7581
          tab->select_cond= row_cond;
 
7582
        else
 
7583
        {
 
7584
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7585
          tab->select_cond->quick_fix_field();
 
7586
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7587
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7588
        }
 
7589
      }
 
7590
      else
 
7591
        tab->select_cond= idx_remainder_cond;
 
7592
      if (tab->select)
 
7593
      {
 
7594
        tab->select->cond= tab->select_cond;
 
7595
      }
 
7596
    }
 
7597
  }
 
7598
  return;
 
7599
}
 
7600
 
 
7601
 
 
7602
 
 
7603
    /*
 
7604
      Determine if the set is already ordered for order_st BY, so it can 
 
7605
      disable join cache because it will change the ordering of the results.
 
7606
      Code handles sort table that is at any location (not only first after 
 
7607
      the const tables) despite the fact that it's currently prohibited.
 
7608
      We must disable join cache if the first non-const table alone is
 
7609
      ordered. If there is a temp table the ordering is done as a last
 
7610
      operation and doesn't prevent join cache usage.
 
7611
    */
 
7612
uint make_join_orderinfo(JOIN *join)
 
7613
{
 
7614
  uint i;
 
7615
  if (join->need_tmp)
 
7616
    return join->tables;
 
7617
 
 
7618
  for (i=join->const_tables ; i < join->tables ; i++)
 
7619
  {
 
7620
    JOIN_TAB *tab=join->join_tab+i;
 
7621
    Table *table=tab->table;
 
7622
    if ((table == join->sort_by_table && 
 
7623
         (!join->order || join->skip_sort_order)) ||
 
7624
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7625
    {
 
7626
      break;
 
7627
    }
 
7628
  }
 
7629
  return i;
 
7630
}
 
7631
 
 
7632
 
 
7633
/*
 
7634
  Plan refinement stage: do various set ups for the executioner
 
7635
 
 
7636
  SYNOPSIS
 
7637
    make_join_readinfo()
 
7638
      join           Join being processed
 
7639
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7640
                     SELECT_NO_JOIN_CACHE)
 
7641
      no_jbuf_after  Don't use join buffering after table with this number.
 
7642
 
 
7643
  DESCRIPTION
 
7644
    Plan refinement stage: do various set ups for the executioner
 
7645
      - set up use of join buffering
 
7646
      - push index conditions
 
7647
      - increment counters
 
7648
      - etc
 
7649
 
 
7650
  RETURN 
 
7651
    false - OK
 
7652
    true  - Out of memory
 
7653
*/
 
7654
 
 
7655
static bool
 
7656
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
 
7657
{
 
7658
  uint i;
 
7659
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7660
  bool sorted= 1;
 
7661
 
 
7662
  for (i=join->const_tables ; i < join->tables ; i++)
 
7663
  {
 
7664
    JOIN_TAB *tab=join->join_tab+i;
 
7665
    Table *table=tab->table;
 
7666
    bool using_join_cache;
 
7667
    tab->read_record.table= table;
 
7668
    tab->read_record.file=table->file;
 
7669
    tab->next_select=sub_select;                /* normal select */
 
7670
    /* 
 
7671
      TODO: don't always instruct first table's ref/range access method to 
 
7672
      produce sorted output.
 
7673
    */
 
7674
    tab->sorted= sorted;
 
7675
    sorted= 0;                                  // only first must be sorted
 
7676
    if (tab->insideout_match_tab)
 
7677
    {
 
7678
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
 
7679
                                                         [tab->index].
 
7680
                                                         key_length)))
 
7681
        return true;
 
7682
    }
 
7683
    switch (tab->type) {
 
7684
    case JT_SYSTEM:                             // Only happens with left join
 
7685
      table->status=STATUS_NO_RECORD;
 
7686
      tab->read_first_record= join_read_system;
 
7687
      tab->read_record.read_record= join_no_more_records;
 
7688
      break;
 
7689
    case JT_CONST:                              // Only happens with left join
 
7690
      table->status=STATUS_NO_RECORD;
 
7691
      tab->read_first_record= join_read_const;
 
7692
      tab->read_record.read_record= join_no_more_records;
 
7693
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7694
          !table->no_keyread)
 
7695
      {
 
7696
        table->key_read=1;
 
7697
        table->file->extra(HA_EXTRA_KEYREAD);
 
7698
      }
 
7699
      break;
 
7700
    case JT_EQ_REF:
 
7701
      table->status=STATUS_NO_RECORD;
 
7702
      if (tab->select)
 
7703
      {
 
7704
        delete tab->select->quick;
 
7705
        tab->select->quick=0;
 
7706
      }
 
7707
      delete tab->quick;
 
7708
      tab->quick=0;
 
7709
      tab->read_first_record= join_read_key;
 
7710
      tab->read_record.read_record= join_no_more_records;
 
7711
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7712
          !table->no_keyread)
 
7713
      {
 
7714
        table->key_read=1;
 
7715
        table->file->extra(HA_EXTRA_KEYREAD);
 
7716
      }
 
7717
      else
 
7718
        push_index_cond(tab, tab->ref.key, true);
 
7719
      break;
 
7720
    case JT_REF_OR_NULL:
 
7721
    case JT_REF:
 
7722
      table->status=STATUS_NO_RECORD;
 
7723
      if (tab->select)
 
7724
      {
 
7725
        delete tab->select->quick;
 
7726
        tab->select->quick=0;
 
7727
      }
 
7728
      delete tab->quick;
 
7729
      tab->quick=0;
 
7730
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7731
          !table->no_keyread)
 
7732
      {
 
7733
        table->key_read=1;
 
7734
        table->file->extra(HA_EXTRA_KEYREAD);
 
7735
      }
 
7736
      else
 
7737
        push_index_cond(tab, tab->ref.key, true);
 
7738
      if (tab->type == JT_REF)
 
7739
      {
 
7740
        tab->read_first_record= join_read_always_key;
 
7741
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7742
           join_read_next_same_diff : join_read_next_same;
 
7743
      }
 
7744
      else
 
7745
      {
 
7746
        tab->read_first_record= join_read_always_key_or_null;
 
7747
        tab->read_record.read_record= join_read_next_same_or_null;
 
7748
      }
 
7749
      break;
 
7750
    case JT_ALL:
 
7751
      /*
 
7752
        If previous table use cache
 
7753
        If the incoming data set is already sorted don't use cache.
 
7754
      */
 
7755
      table->status=STATUS_NO_RECORD;
 
7756
      using_join_cache= false;
 
7757
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7758
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7759
          !tab->insideout_match_tab)
 
7760
      {
 
7761
        if ((options & SELECT_DESCRIBE) ||
 
7762
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7763
                             i-join->const_tables))
 
7764
        {
 
7765
          using_join_cache= true;
 
7766
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7767
        }
 
7768
      }
 
7769
      /* These init changes read_record */
 
7770
      if (tab->use_quick == 2)
 
7771
      {
 
7772
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7773
        tab->read_first_record= join_init_quick_read_record;
 
7774
        if (statistics)
 
7775
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7776
      }
 
7777
      else
 
7778
      {
 
7779
        tab->read_first_record= join_init_read_record;
 
7780
        if (i == join->const_tables)
 
7781
        {
 
7782
          if (tab->select && tab->select->quick)
 
7783
          {
 
7784
            if (statistics)
 
7785
              status_var_increment(join->thd->status_var.select_range_count);
 
7786
          }
 
7787
          else
 
7788
          {
 
7789
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7790
            if (statistics)
 
7791
              status_var_increment(join->thd->status_var.select_scan_count);
 
7792
          }
 
7793
        }
 
7794
        else
 
7795
        {
 
7796
          if (tab->select && tab->select->quick)
 
7797
          {
 
7798
            if (statistics)
 
7799
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7800
          }
 
7801
          else
 
7802
          {
 
7803
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7804
            if (statistics)
 
7805
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7806
          }
 
7807
        }
 
7808
        if (!table->no_keyread)
 
7809
        {
 
7810
          if (tab->select && tab->select->quick &&
 
7811
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7812
              table->covering_keys.is_set(tab->select->quick->index))
 
7813
          {
 
7814
            table->key_read=1;
 
7815
            table->file->extra(HA_EXTRA_KEYREAD);
 
7816
          }
 
7817
          else if (!table->covering_keys.is_clear_all() &&
 
7818
                   !(tab->select && tab->select->quick))
 
7819
          {                                     // Only read index tree
 
7820
            if (!tab->insideout_match_tab)
 
7821
            {
 
7822
              /*
 
7823
                See bug #26447: "Using the clustered index for a table scan
 
7824
                is always faster than using a secondary index".
 
7825
              */
 
7826
              if (table->s->primary_key != MAX_KEY &&
 
7827
                  table->file->primary_key_is_clustered())
 
7828
                tab->index= table->s->primary_key;
 
7829
              else
 
7830
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7831
            }
 
7832
            tab->read_first_record= join_read_first;
 
7833
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7834
          }
 
7835
        }
 
7836
        if (tab->select && tab->select->quick &&
 
7837
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7838
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7839
      }
 
7840
      break;
 
7841
    default:
 
7842
      break;                                    /* purecov: deadcode */
 
7843
    case JT_UNKNOWN:
 
7844
    case JT_MAYBE_REF:
 
7845
      abort();                                  /* purecov: deadcode */
 
7846
    }
 
7847
  }
 
7848
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7849
  return(false);
 
7850
}
 
7851
 
 
7852
 
 
7853
/**
 
7854
  Give error if we some tables are done with a full join.
 
7855
 
 
7856
  This is used by multi_table_update and multi_table_delete when running
 
7857
  in safe mode.
 
7858
 
 
7859
  @param join           Join condition
 
7860
 
 
7861
  @retval
 
7862
    0   ok
 
7863
  @retval
 
7864
    1   Error (full join used)
 
7865
*/
 
7866
 
 
7867
bool error_if_full_join(JOIN *join)
 
7868
{
 
7869
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7870
       tab < end;
 
7871
       tab++)
 
7872
  {
 
7873
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7874
    {
 
7875
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7876
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7877
      return(1);
 
7878
    }
 
7879
  }
 
7880
  return(0);
 
7881
}
 
7882
 
 
7883
 
 
7884
/**
 
7885
  cleanup JOIN_TAB.
 
7886
*/
 
7887
 
 
7888
void JOIN_TAB::cleanup()
1218
7889
{
1219
7890
  delete select;
1220
7891
  select= 0;
1221
7892
  delete quick;
1222
7893
  quick= 0;
1223
 
  if (cache.buff)
1224
 
  {
1225
 
    size_t size= cache.end - cache.buff;
1226
 
    global_join_buffer.sub(size);
1227
 
    free(cache.buff);
1228
 
  }
 
7894
  x_free(cache.buff);
1229
7895
  cache.buff= 0;
1230
7896
  limit= 0;
1231
7897
  if (table)
1233
7899
    if (table->key_read)
1234
7900
    {
1235
7901
      table->key_read= 0;
1236
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7902
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1237
7903
    }
1238
 
    table->cursor->ha_index_or_rnd_end();
 
7904
    table->file->ha_index_or_rnd_end();
1239
7905
    /*
1240
7906
      We need to reset this for next select
1241
7907
      (Tested in part_of_refkey)
1242
7908
    */
1243
7909
    table->reginfo.join_tab= 0;
1244
7910
  }
1245
 
  read_record.end_read_record();
1246
 
}
1247
 
 
1248
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1249
 
{
1250
 
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1251
 
  {
1252
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1253
 
      return 0;
1254
 
  }
1255
 
  return 1;
1256
 
}
1257
 
 
1258
 
/**
1259
 
  Remove the following expressions from ORDER BY and GROUP BY:
 
7911
  end_read_record(&read_record);
 
7912
}
 
7913
 
 
7914
 
 
7915
/**
 
7916
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7917
  (table cursors), free quick selects.
 
7918
 
 
7919
    This function is called in the end of execution of a JOIN, before the used
 
7920
    tables are unlocked and closed.
 
7921
 
 
7922
    For a join that is resolved using a temporary table, the first sweep is
 
7923
    performed against actual tables and an intermediate result is inserted
 
7924
    into the temprorary table.
 
7925
    The last sweep is performed against the temporary table. Therefore,
 
7926
    the base tables and associated buffers used to fill the temporary table
 
7927
    are no longer needed, and this function is called to free them.
 
7928
 
 
7929
    For a join that is performed without a temporary table, this function
 
7930
    is called after all rows are sent, but before EOF packet is sent.
 
7931
 
 
7932
    For a simple SELECT with no subqueries this function performs a full
 
7933
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7934
    tables.
 
7935
 
 
7936
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7937
    do the full cleanup and need to do a partial cleanup only.
 
7938
    - If a JOIN is not the top level join, we must not unlock the tables
 
7939
    because the outer select may not have been evaluated yet, and we
 
7940
    can't unlock only selected tables of a query.
 
7941
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7942
    should not free quick selects and join buffers because they will be
 
7943
    needed for the next execution of the correlated subquery.
 
7944
    - However, if this is a JOIN for a [sub]select, which is not
 
7945
    a correlated subquery itself, but has subqueries, we can free it
 
7946
    fully and also free JOINs of all its subqueries. The exception
 
7947
    is a subquery in SELECT list, e.g: @n
 
7948
    SELECT a, (select max(b) from t1) group by c @n
 
7949
    This subquery will not be evaluated at first sweep and its value will
 
7950
    not be inserted into the temporary table. Instead, it's evaluated
 
7951
    when selecting from the temporary table. Therefore, it can't be freed
 
7952
    here even though it's not correlated.
 
7953
 
 
7954
  @todo
 
7955
    Unlock tables even if the join isn't top level select in the tree
 
7956
*/
 
7957
 
 
7958
void JOIN::join_free()
 
7959
{
 
7960
  SELECT_LEX_UNIT *tmp_unit;
 
7961
  SELECT_LEX *sl;
 
7962
  /*
 
7963
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7964
    free all tables.
 
7965
  */
 
7966
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7967
  bool can_unlock= full;
 
7968
 
 
7969
  cleanup(full);
 
7970
 
 
7971
  for (tmp_unit= select_lex->first_inner_unit();
 
7972
       tmp_unit;
 
7973
       tmp_unit= tmp_unit->next_unit())
 
7974
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7975
    {
 
7976
      Item_subselect *subselect= sl->master_unit()->item;
 
7977
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7978
      /*
 
7979
        If this join is evaluated, we can fully clean it up and clean up all
 
7980
        its underlying joins even if they are correlated -- they will not be
 
7981
        used any more anyway.
 
7982
        If this join is not yet evaluated, we still must clean it up to
 
7983
        close its table cursors -- it may never get evaluated, as in case of
 
7984
        ... HAVING false OR a IN (SELECT ...))
 
7985
        but all table cursors must be closed before the unlock.
 
7986
      */
 
7987
      sl->cleanup_all_joins(full_local);
 
7988
      /* Can't unlock if at least one JOIN is still needed */
 
7989
      can_unlock= can_unlock && full_local;
 
7990
    }
 
7991
 
 
7992
  /*
 
7993
    We are not using tables anymore
 
7994
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
7995
  */
 
7996
  if (can_unlock && lock && thd->lock &&
 
7997
      !(select_options & SELECT_NO_UNLOCK) &&
 
7998
      !select_lex->subquery_in_having &&
 
7999
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
8000
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
8001
  {
 
8002
    /*
 
8003
      TODO: unlock tables even if the join isn't top level select in the
 
8004
      tree.
 
8005
    */
 
8006
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8007
    lock= 0;
 
8008
  }
 
8009
 
 
8010
  return;
 
8011
}
 
8012
 
 
8013
 
 
8014
/**
 
8015
  Free resources of given join.
 
8016
 
 
8017
  @param fill   true if we should free all resources, call with full==1
 
8018
                should be last, before it this function can be called with
 
8019
                full==0
 
8020
 
 
8021
  @note
 
8022
    With subquery this function definitely will be called several times,
 
8023
    but even for simple query it can be called several times.
 
8024
*/
 
8025
 
 
8026
void JOIN::cleanup(bool full)
 
8027
{
 
8028
  if (table)
 
8029
  {
 
8030
    JOIN_TAB *tab,*end;
 
8031
    /*
 
8032
      Only a sorted table may be cached.  This sorted table is always the
 
8033
      first non const table in join->table
 
8034
    */
 
8035
    if (tables > const_tables) // Test for not-const tables
 
8036
    {
 
8037
      free_io_cache(table[const_tables]);
 
8038
      filesort_free_buffers(table[const_tables],full);
 
8039
    }
 
8040
 
 
8041
    if (full)
 
8042
    {
 
8043
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8044
        tab->cleanup();
 
8045
      table= 0;
 
8046
    }
 
8047
    else
 
8048
    {
 
8049
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8050
      {
 
8051
        if (tab->table)
 
8052
          tab->table->file->ha_index_or_rnd_end();
 
8053
      }
 
8054
    }
 
8055
    cleanup_sj_tmp_tables(this);//
 
8056
  }
 
8057
  /*
 
8058
    We are not using tables anymore
 
8059
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8060
  */
 
8061
  if (full)
 
8062
  {
 
8063
    if (tmp_join)
 
8064
      tmp_table_param.copy_field= 0;
 
8065
    group_fields.delete_elements();
 
8066
    /*
 
8067
      We can't call delete_elements() on copy_funcs as this will cause
 
8068
      problems in free_elements() as some of the elements are then deleted.
 
8069
    */
 
8070
    tmp_table_param.copy_funcs.empty();
 
8071
    /*
 
8072
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8073
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8074
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8075
      be removed in tmp_table_param.cleanup().
 
8076
    */
 
8077
    if (tmp_join &&
 
8078
        tmp_join != this &&
 
8079
        tmp_join->tmp_table_param.copy_field ==
 
8080
        tmp_table_param.copy_field)
 
8081
    {
 
8082
      tmp_join->tmp_table_param.copy_field=
 
8083
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8084
    }
 
8085
    tmp_table_param.cleanup();
 
8086
  }
 
8087
  return;
 
8088
}
 
8089
 
 
8090
 
 
8091
/**
 
8092
  Remove the following expressions from order_st BY and GROUP BY:
1260
8093
  Constant expressions @n
1261
8094
  Expression that only uses tables that are of type EQ_REF and the reference
1262
8095
  is in the order_st list or if all refereed tables are of the above type.
1263
8096
 
1264
8097
  In the following, the X field can be removed:
1265
8098
  @code
1266
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
1267
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
 
8099
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8100
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1268
8101
  @endcode
1269
8102
 
1270
8103
  These can't be optimized:
1271
8104
  @code
1272
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
1273
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
1274
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
 
8105
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8106
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8107
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1275
8108
  @endcode
1276
8109
*/
1277
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8110
 
 
8111
static bool
 
8112
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1278
8113
{
1279
8114
  if (tab->cached_eq_ref_table)                 // If cached
1280
8115
    return tab->eq_ref_table;
1281
8116
  tab->cached_eq_ref_table=1;
1282
8117
  /* We can skip const tables only if not an outer table */
1283
 
  if (tab->type == AM_CONST && !tab->first_inner)
1284
 
    return (tab->eq_ref_table=1);
1285
 
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
8118
  if (tab->type == JT_CONST && !tab->first_inner)
 
8119
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8120
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1286
8121
    return (tab->eq_ref_table=0);               // We must use this
1287
8122
  Item **ref_item=tab->ref.items;
1288
8123
  Item **end=ref_item+tab->ref.key_parts;
1289
 
  uint32_t found=0;
 
8124
  uint found=0;
1290
8125
  table_map map=tab->table->map;
1291
8126
 
1292
8127
  for (; ref_item != end ; ref_item++)
1293
8128
  {
1294
8129
    if (! (*ref_item)->const_item())
1295
8130
    {                                           // Not a const ref
1296
 
      Order *order;
 
8131
      order_st *order;
1297
8132
      for (order=start_order ; order ; order=order->next)
1298
8133
      {
1299
 
        if ((*ref_item)->eq(order->item[0],0))
1300
 
          break;
 
8134
        if ((*ref_item)->eq(order->item[0],0))
 
8135
          break;
1301
8136
      }
1302
8137
      if (order)
1303
8138
      {
1304
 
        found++;
1305
 
        assert(!(order->used & map));
1306
 
        order->used|=map;
1307
 
        continue;                               // Used in order_st BY
 
8139
        found++;
 
8140
        assert(!(order->used & map));
 
8141
        order->used|=map;
 
8142
        continue;                               // Used in order_st BY
1308
8143
      }
1309
8144
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1310
 
        return (tab->eq_ref_table= 0);
 
8145
        return (tab->eq_ref_table=0);
1311
8146
    }
1312
8147
  }
1313
8148
  /* Check that there was no reference to table before sort order */
1319
8154
      continue;
1320
8155
    }
1321
8156
    if (start_order->depend_map & map)
1322
 
      return (tab->eq_ref_table= 0);
1323
 
  }
1324
 
  return tab->eq_ref_table= 1;
1325
 
}
 
8157
      return (tab->eq_ref_table=0);
 
8158
  }
 
8159
  return tab->eq_ref_table=1;
 
8160
}
 
8161
 
 
8162
 
 
8163
static bool
 
8164
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8165
{
 
8166
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8167
  {
 
8168
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8169
      return 0;
 
8170
  }
 
8171
  return 1;
 
8172
}
 
8173
 
 
8174
 
 
8175
/** Update the dependency map for the tables. */
 
8176
 
 
8177
static void update_depend_map(JOIN *join)
 
8178
{
 
8179
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8180
 
 
8181
  for (; join_tab != end ; join_tab++)
 
8182
  {
 
8183
    TABLE_REF *ref= &join_tab->ref;
 
8184
    table_map depend_map=0;
 
8185
    Item **item=ref->items;
 
8186
    uint i;
 
8187
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8188
      depend_map|=(*item)->used_tables();
 
8189
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8190
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8191
    for (JOIN_TAB **tab=join->map2table;
 
8192
         depend_map ;
 
8193
         tab++,depend_map>>=1 )
 
8194
    {
 
8195
      if (depend_map & 1)
 
8196
        ref->depend_map|=(*tab)->ref.depend_map;
 
8197
    }
 
8198
  }
 
8199
}
 
8200
 
 
8201
 
 
8202
/** Update the dependency map for the sort order. */
 
8203
 
 
8204
static void update_depend_map(JOIN *join, order_st *order)
 
8205
{
 
8206
  for (; order ; order=order->next)
 
8207
  {
 
8208
    table_map depend_map;
 
8209
    order->item[0]->update_used_tables();
 
8210
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8211
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8212
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8213
        && !order->item[0]->with_sum_func)
 
8214
    {
 
8215
      for (JOIN_TAB **tab=join->map2table;
 
8216
           depend_map ;
 
8217
           tab++, depend_map>>=1)
 
8218
      {
 
8219
        if (depend_map & 1)
 
8220
          order->depend_map|=(*tab)->ref.depend_map;
 
8221
      }
 
8222
    }
 
8223
  }
 
8224
}
 
8225
 
 
8226
 
 
8227
/**
 
8228
  Remove all constants and check if order_st only contains simple
 
8229
  expressions.
 
8230
 
 
8231
  simple_order is set to 1 if sort_order only uses fields from head table
 
8232
  and the head table is not a LEFT JOIN table.
 
8233
 
 
8234
  @param join                   Join handler
 
8235
  @param first_order            List of SORT or GROUP order
 
8236
  @param cond                   WHERE statement
 
8237
  @param change_list            Set to 1 if we should remove things from list.
 
8238
                               If this is not set, then only simple_order is
 
8239
                               calculated.
 
8240
  @param simple_order           Set to 1 if we are only using simple expressions
 
8241
 
 
8242
  @return
 
8243
    Returns new sort order
 
8244
*/
 
8245
 
 
8246
static order_st *
 
8247
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8248
             bool change_list, bool *simple_order)
 
8249
{
 
8250
  if (join->tables == join->const_tables)
 
8251
    return change_list ? 0 : first_order;               // No need to sort
 
8252
 
 
8253
  order_st *order,**prev_ptr;
 
8254
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8255
  table_map not_const_tables= ~join->const_table_map;
 
8256
  table_map ref;
 
8257
 
 
8258
  prev_ptr= &first_order;
 
8259
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8260
 
 
8261
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8262
 
 
8263
  update_depend_map(join, first_order);
 
8264
  for (order=first_order; order ; order=order->next)
 
8265
  {
 
8266
    table_map order_tables=order->item[0]->used_tables();
 
8267
    if (order->item[0]->with_sum_func)
 
8268
      *simple_order=0;                          // Must do a temp table to sort
 
8269
    else if (!(order_tables & not_const_tables))
 
8270
    {
 
8271
      if (order->item[0]->with_subselect)
 
8272
        order->item[0]->val_str(&order->item[0]->str_value);
 
8273
      continue;                                 // skip const item
 
8274
    }
 
8275
    else
 
8276
    {
 
8277
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8278
        *simple_order=0;
 
8279
      else
 
8280
      {
 
8281
        Item *comp_item=0;
 
8282
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8283
        {
 
8284
          continue;
 
8285
        }
 
8286
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8287
        {
 
8288
          if (!(order_tables & first_table) &&
 
8289
              only_eq_ref_tables(join,first_order, ref))
 
8290
          {
 
8291
            continue;
 
8292
          }
 
8293
          *simple_order=0;                      // Must do a temp table to sort
 
8294
        }
 
8295
      }
 
8296
    }
 
8297
    if (change_list)
 
8298
      *prev_ptr= order;                         // use this entry
 
8299
    prev_ptr= &order->next;
 
8300
  }
 
8301
  if (change_list)
 
8302
    *prev_ptr=0;
 
8303
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8304
    *simple_order=1;
 
8305
  return(first_order);
 
8306
}
 
8307
 
 
8308
 
 
8309
static int
 
8310
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8311
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8312
                 const char *info, Item *having)
 
8313
{
 
8314
  if (select_options & SELECT_DESCRIBE)
 
8315
  {
 
8316
    select_describe(join, false, false, false, info);
 
8317
    return(0);
 
8318
  }
 
8319
 
 
8320
  join->join_free();
 
8321
 
 
8322
  if (send_row)
 
8323
  {
 
8324
    for (TableList *table= tables; table; table= table->next_leaf)
 
8325
      mark_as_null_row(table->table);           // All fields are NULL
 
8326
    if (having && having->val_int() == 0)
 
8327
      send_row=0;
 
8328
  }
 
8329
  if (!(result->send_fields(fields,
 
8330
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8331
  {
 
8332
    if (send_row)
 
8333
    {
 
8334
      List_iterator_fast<Item> it(fields);
 
8335
      Item *item;
 
8336
      while ((item= it++))
 
8337
        item->no_rows_in_result();
 
8338
      result->send_data(fields);
 
8339
    }
 
8340
    result->send_eof();                         // Should be safe
 
8341
  }
 
8342
  /* Update results for FOUND_ROWS */
 
8343
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8344
  return(0);
 
8345
}
 
8346
 
 
8347
/*
 
8348
  used only in JOIN::clear
 
8349
*/
 
8350
static void clear_tables(JOIN *join)
 
8351
{
 
8352
  /* 
 
8353
    must clear only the non-const tables, as const tables
 
8354
    are not re-calculated.
 
8355
  */
 
8356
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
8357
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8358
}
 
8359
 
 
8360
/*****************************************************************************
 
8361
  Make som simple condition optimization:
 
8362
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8363
  Remove all dummy tests 'item = item', 'const op const'.
 
8364
  Remove all 'item is NULL', when item can never be null!
 
8365
  item->marker should be 0 for all items on entry
 
8366
  Return in cond_value false if condition is impossible (1 = 2)
 
8367
*****************************************************************************/
 
8368
 
 
8369
class COND_CMP :public ilink {
 
8370
public:
 
8371
  static void *operator new(size_t size)
 
8372
  {
 
8373
    return (void*) sql_alloc((uint) size);
 
8374
  }
 
8375
  static void operator delete(void *ptr __attribute__((unused)),
 
8376
                              size_t size __attribute__((unused)))
 
8377
  { TRASH(ptr, size); }
 
8378
 
 
8379
  Item *and_level;
 
8380
  Item_func *cmp_func;
 
8381
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8382
};
 
8383
 
 
8384
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8385
template class I_List<COND_CMP>;
 
8386
template class I_List_iterator<COND_CMP>;
 
8387
#endif
 
8388
 
1326
8389
 
1327
8390
/**
1328
8391
  Find the multiple equality predicate containing a field.
1342
8405
    - Item_equal for the found multiple equality predicate if a success;
1343
8406
    - NULL otherwise.
1344
8407
*/
1345
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8408
 
 
8409
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8410
                            bool *inherited_fl)
1346
8411
{
1347
8412
  Item_equal *item= 0;
1348
8413
  bool in_upper_level= false;
1363
8428
  return item;
1364
8429
}
1365
8430
 
 
8431
  
1366
8432
/**
1367
8433
  Check whether an equality can be used to build multiple equalities.
1368
8434
 
1389
8455
    the check_equality will be called for the following equality
1390
8456
    predicates a=b, b=c, b=2 and f=e.
1391
8457
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1392
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8458
      *cond_equal into (0,[Item_equal(a,b)]). 
1393
8459
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1394
8460
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1395
8461
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1402
8468
    the Field::eq_def method) are placed to the same multiple equalities.
1403
8469
    Because of this some equality predicates are not eliminated and
1404
8470
    can be used in the constant propagation procedure.
1405
 
    We could weeken the equlity test as soon as at least one of the
1406
 
    equal fields is to be equal to a constant. It would require a
 
8471
    We could weeken the equlity test as soon as at least one of the 
 
8472
    equal fields is to be equal to a constant. It would require a 
1407
8473
    more complicated implementation: we would have to store, in
1408
8474
    general case, its own constant for each fields from the multiple
1409
8475
    equality. But at the same time it would allow us to get rid
1421
8487
    containing just field1 and field2 is added to the existing
1422
8488
    multiple equalities.
1423
8489
    If the function processes the predicate of the form field1=const,
1424
 
    it looks for a multiple equality containing field1. If found, the
 
8490
    it looks for a multiple equality containing field1. If found, the 
1425
8491
    function checks the constant of the multiple equality. If the value
1426
8492
    is unknown, it is setup to const. Otherwise the value is compared with
1427
8493
    const and the evaluation of the equality predicate is performed.
1444
8510
  @retval
1445
8511
    false   otherwise
1446
8512
*/
1447
 
static bool check_simple_equality(Item *left_item,
1448
 
                                  Item *right_item,
1449
 
                                  Item *item,
1450
 
                                  COND_EQUAL *cond_equal)
 
8513
 
 
8514
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8515
                                  Item *item, COND_EQUAL *cond_equal)
1451
8516
{
 
8517
  if (left_item->type() == Item::REF_ITEM &&
 
8518
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8519
  {
 
8520
    if (((Item_ref*)left_item)->depended_from)
 
8521
      return false;
 
8522
    left_item= left_item->real_item();
 
8523
  }
 
8524
  if (right_item->type() == Item::REF_ITEM &&
 
8525
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8526
  {
 
8527
    if (((Item_ref*)right_item)->depended_from)
 
8528
      return false;
 
8529
    right_item= right_item->real_item();
 
8530
  }
1452
8531
  if (left_item->type() == Item::FIELD_ITEM &&
1453
8532
      right_item->type() == Item::FIELD_ITEM &&
1454
8533
      !((Item_field*)left_item)->depended_from &&
1466
8545
    bool left_copyfl, right_copyfl;
1467
8546
    Item_equal *left_item_equal=
1468
8547
               find_item_equal(cond_equal, left_field, &left_copyfl);
1469
 
    Item_equal *right_item_equal=
 
8548
    Item_equal *right_item_equal= 
1470
8549
               find_item_equal(cond_equal, right_field, &right_copyfl);
1471
8550
 
1472
8551
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1473
8552
    if (left_field->eq(right_field)) /* f = f */
1474
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8553
      return (!(left_field->maybe_null() && !left_item_equal)); 
1475
8554
 
1476
8555
    if (left_item_equal && left_item_equal == right_item_equal)
1477
8556
    {
1478
 
      /*
 
8557
      /* 
1479
8558
        The equality predicate is inference of one of the existing
1480
8559
        multiple equalities, i.e the condition is already covered
1481
8560
        by upper level equalities
1482
8561
      */
1483
8562
       return true;
1484
8563
    }
1485
 
 
1486
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8564
    
 
8565
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1487
8566
                              item->name < subq_sj_cond_name + 64);
1488
8567
    /* Copy the found multiple equalities at the current level if needed */
1489
8568
    if (left_copyfl)
1504
8583
    }
1505
8584
 
1506
8585
    if (left_item_equal)
1507
 
    {
 
8586
    { 
1508
8587
      /* left item was found in the current or one of the upper levels */
1509
8588
      if (! right_item_equal)
1510
8589
        left_item_equal->add((Item_field *) right_item);
1519
8598
      }
1520
8599
    }
1521
8600
    else
1522
 
    {
 
8601
    { 
1523
8602
      /* left item was not found neither the current nor in upper levels  */
1524
8603
      if (right_item_equal)
1525
8604
      {
1527
8606
        if (copy_item_name)
1528
8607
          right_item_equal->name = item->name;
1529
8608
      }
1530
 
      else
 
8609
      else 
1531
8610
      {
1532
8611
        /* None of the fields was found in multiple equalities */
1533
8612
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1575
8654
          eq_item->set_cmp_func();
1576
8655
          eq_item->quick_fix_field();
1577
8656
          item= eq_item;
1578
 
        }
 
8657
        }  
1579
8658
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1580
8659
            !cs->coll->propagate(cs, 0, 0))
1581
8660
          return false;
1590
8669
      }
1591
8670
      if (item_equal)
1592
8671
      {
1593
 
        /*
 
8672
        /* 
1594
8673
          The flag cond_false will be set to 1 after this, if item_equal
1595
8674
          already contains a constant and its value is  not equal to
1596
8675
          the value of const_item.
1608
8687
  return false;
1609
8688
}
1610
8689
 
 
8690
 
1611
8691
/**
1612
8692
  Convert row equalities into a conjunction of regular equalities.
1613
8693
 
1620
8700
    simple equality nor a row equality the item for this predicate is added
1621
8701
    to eq_list.
1622
8702
 
1623
 
  @param session        thread handle
 
8703
  @param thd        thread handle
1624
8704
  @param left_row   left term of the row equality to be processed
1625
8705
  @param right_row  right term of the row equality to be processed
1626
8706
  @param cond_equal multiple equalities that must hold together with the
1633
8713
  @retval
1634
8714
    false   otherwise
1635
8715
*/
1636
 
static bool check_row_equality(Session *session,
1637
 
                               Item *left_row, 
1638
 
                               Item_row *right_row,
1639
 
                               COND_EQUAL *cond_equal,
1640
 
                               List<Item>* eq_list)
1641
 
{
1642
 
  uint32_t n= left_row->cols();
1643
 
  for (uint32_t i= 0 ; i < n; i++)
 
8716
 
 
8717
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8718
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8719
 
8720
  uint n= left_row->cols();
 
8721
  for (uint i= 0 ; i < n; i++)
1644
8722
  {
1645
8723
    bool is_converted;
1646
8724
    Item *left_item= left_row->element_index(i);
1648
8726
    if (left_item->type() == Item::ROW_ITEM &&
1649
8727
        right_item->type() == Item::ROW_ITEM)
1650
8728
    {
1651
 
      is_converted= check_row_equality(session,
 
8729
      is_converted= check_row_equality(thd, 
1652
8730
                                       (Item_row *) left_item,
1653
8731
                                       (Item_row *) right_item,
1654
8732
                                       cond_equal, eq_list);
1655
8733
      if (!is_converted)
1656
 
        session->lex->current_select->cond_count++;
 
8734
        thd->lex->current_select->cond_count++;      
1657
8735
    }
1658
8736
    else
1659
 
    {
 
8737
    { 
1660
8738
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1661
 
      session->lex->current_select->cond_count++;
1662
 
    }
1663
 
 
 
8739
      thd->lex->current_select->cond_count++;
 
8740
    }  
 
8741
 
1664
8742
    if (!is_converted)
1665
8743
    {
1666
8744
      Item_func_eq *eq_item;
1674
8752
  return true;
1675
8753
}
1676
8754
 
 
8755
 
1677
8756
/**
1678
8757
  Eliminate row equalities and form multiple equalities predicates.
1679
8758
 
1688
8767
    equalities which are treated in the same way as original equality
1689
8768
    predicates.
1690
8769
 
1691
 
  @param session        thread handle
 
8770
  @param thd        thread handle
1692
8771
  @param item       predicate to process
1693
8772
  @param cond_equal multiple equalities that must hold together with the
1694
8773
                    predicate
1703
8782
           or, if the equality is neither a simple one nor a row equality,
1704
8783
           or, if the procedure fails by a fatal error.
1705
8784
*/
1706
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8785
 
 
8786
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8787
                           List<Item> *eq_list)
1707
8788
{
1708
8789
  if (item->type() == Item::FUNC_ITEM &&
1709
8790
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1714
8795
    if (left_item->type() == Item::ROW_ITEM &&
1715
8796
        right_item->type() == Item::ROW_ITEM)
1716
8797
    {
1717
 
      session->lex->current_select->cond_count--;
1718
 
      return check_row_equality(session,
 
8798
      thd->lex->current_select->cond_count--;
 
8799
      return check_row_equality(thd,
1719
8800
                                (Item_row *) left_item,
1720
8801
                                (Item_row *) right_item,
1721
8802
                                cond_equal, eq_list);
1722
8803
    }
1723
 
    else
 
8804
    else 
1724
8805
      return check_simple_equality(left_item, right_item, item, cond_equal);
1725
 
  }
 
8806
  } 
1726
8807
  return false;
1727
8808
}
1728
8809
 
 
8810
                          
1729
8811
/**
1730
8812
  Replace all equality predicates in a condition by multiple equality items.
1731
8813
 
1732
8814
    At each 'and' level the function detects items for equality predicates
1733
8815
    and replaced them by a set of multiple equality items of class Item_equal,
1734
 
    taking into account inherited equalities from upper levels.
 
8816
    taking into account inherited equalities from upper levels. 
1735
8817
    If an equality predicate is used not in a conjunction it's just
1736
8818
    replaced by a multiple equality predicate.
1737
8819
    For each 'and' level the function set a pointer to the inherited
1738
8820
    multiple equalities in the cond_equal field of the associated
1739
 
    object of the type Item_cond_and.
 
8821
    object of the type Item_cond_and.   
1740
8822
    The function also traverses the cond tree and and for each field reference
1741
8823
    sets a pointer to the multiple equality item containing the field, if there
1742
8824
    is any. If this multiple equality equates fields to a constant the
1743
 
    function replaces the field reference by the constant in the cases
 
8825
    function replaces the field reference by the constant in the cases 
1744
8826
    when the field is not of a string type or when the field reference is
1745
8827
    just an argument of a comparison predicate.
1746
 
    The function also determines the maximum number of members in
 
8828
    The function also determines the maximum number of members in 
1747
8829
    equality lists of each Item_cond_and object assigning it to
1748
 
    session->lex->current_select->max_equal_elems.
 
8830
    thd->lex->current_select->max_equal_elems.
1749
8831
 
1750
8832
  @note
1751
8833
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1757
8839
    in a conjuction for a minimal set of multiple equality predicates.
1758
8840
    This set can be considered as a canonical representation of the
1759
8841
    sub-conjunction of the equality predicates.
1760
 
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
 
8842
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1761
8843
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1762
8844
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1763
8845
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1768
8850
    The function performs the substitution in a recursive descent by
1769
8851
    the condtion tree, passing to the next AND level a chain of multiple
1770
8852
    equality predicates which have been built at the upper levels.
1771
 
    The Item_equal items built at the level are attached to other
 
8853
    The Item_equal items built at the level are attached to other 
1772
8854
    non-equality conjucts as a sublist. The pointer to the inherited
1773
8855
    multiple equalities is saved in the and condition object (Item_cond_and).
1774
 
    This chain allows us for any field reference occurence easyly to find a
 
8856
    This chain allows us for any field reference occurence easyly to find a 
1775
8857
    multiple equality that must be held for this occurence.
1776
8858
    For each AND level we do the following:
1777
8859
    - scan it for all equality predicate (=) items
1778
8860
    - join them into disjoint Item_equal() groups
1779
 
    - process the included OR conditions recursively to do the same for
1780
 
      lower AND levels.
 
8861
    - process the included OR conditions recursively to do the same for 
 
8862
      lower AND levels. 
1781
8863
 
1782
8864
    We need to do things in this order as lower AND levels need to know about
1783
8865
    all possible Item_equal objects in upper levels.
1784
8866
 
1785
 
  @param session        thread handle
 
8867
  @param thd        thread handle
1786
8868
  @param cond       condition(expression) where to make replacement
1787
8869
  @param inherited  path to all inherited multiple equality items
1788
8870
 
1789
8871
  @return
1790
8872
    pointer to the transformed condition
1791
8873
*/
1792
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8874
 
 
8875
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8876
                                        COND_EQUAL *inherited)
1793
8877
{
1794
8878
  Item_equal *item_equal;
1795
8879
  COND_EQUAL cond_equal;
1801
8885
    bool and_level= ((Item_cond*) cond)->functype() ==
1802
8886
      Item_func::COND_AND_FUNC;
1803
8887
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1804
 
 
 
8888
    
1805
8889
    List_iterator<Item> li(*args);
1806
8890
    Item *item;
1807
8891
 
1810
8894
      /*
1811
8895
         Retrieve all conjucts of this level detecting the equality
1812
8896
         that are subject to substitution by multiple equality items and
1813
 
         removing each such predicate from the conjunction after having
 
8897
         removing each such predicate from the conjunction after having 
1814
8898
         found/created a multiple equality whose inference the predicate is.
1815
 
     */
 
8899
     */      
1816
8900
      while ((item= li++))
1817
8901
      {
1818
8902
        /*
1820
8904
          structure here because it's restored before each
1821
8905
          re-execution of any prepared statement/stored procedure.
1822
8906
        */
1823
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8907
        if (check_equality(thd, item, &cond_equal, &eq_list))
1824
8908
          li.remove();
1825
8909
      }
1826
8910
 
1829
8913
      {
1830
8914
        item_equal->fix_length_and_dec();
1831
8915
        item_equal->update_used_tables();
1832
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1833
 
                      item_equal->members());
 
8916
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8917
                      item_equal->members());  
1834
8918
      }
1835
8919
 
1836
8920
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1842
8926
    */
1843
8927
    li.rewind();
1844
8928
    while ((item= li++))
1845
 
    {
 
8929
    { 
1846
8930
      Item *new_item;
1847
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8931
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1848
8932
      {
1849
8933
        /* This replacement happens only for standalone equalities */
1850
8934
        /*
1872
8956
      (b=5) and (a=c) are standalone equalities.
1873
8957
      In general we can't leave alone standalone eqalities:
1874
8958
      for WHERE a=b AND c=d AND (b=c OR d=5)
1875
 
      b=c is replaced by =(a,b,c,d).
 
8959
      b=c is replaced by =(a,b,c,d).  
1876
8960
     */
1877
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8961
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1878
8962
    {
1879
8963
      int n= cond_equal.current_level.elements + eq_list.elements;
1880
8964
      if (n == 0)
1885
8969
        {
1886
8970
          item_equal->fix_length_and_dec();
1887
8971
          item_equal->update_used_tables();
1888
 
        }
 
8972
        }
1889
8973
        else
1890
8974
          item_equal= (Item_equal *) eq_list.pop();
1891
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1892
 
                      item_equal->members());
 
8975
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8976
                      item_equal->members());  
1893
8977
        return item_equal;
1894
8978
      }
1895
8979
      else
1896
8980
      {
1897
 
        /*
 
8981
        /* 
1898
8982
          Here a new AND level must be created. It can happen only
1899
8983
          when a row equality is processed as a standalone predicate.
1900
 
        */
 
8984
        */
1901
8985
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1902
8986
        and_cond->quick_fix_field();
1903
8987
        List<Item> *args= and_cond->argument_list();
1906
8990
        {
1907
8991
          item_equal->fix_length_and_dec();
1908
8992
          item_equal->update_used_tables();
1909
 
          set_if_bigger(session->lex->current_select->max_equal_elems,
1910
 
                        item_equal->members());
 
8993
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8994
                        item_equal->members());  
1911
8995
        }
1912
8996
        and_cond->cond_equal= cond_equal;
1913
8997
        args->concat((List<Item> *)&cond_equal.current_level);
1914
 
 
 
8998
        
1915
8999
        return and_cond;
1916
9000
      }
1917
9001
    }
1918
 
    /*
 
9002
    /* 
1919
9003
      For each field reference in cond, not from equal item predicates,
1920
9004
      set a pointer to the multiple equality it belongs to (if there is any)
1921
9005
      as soon the field is not of a string type or the field reference is
1922
 
      an argument of a comparison predicate.
1923
 
    */
1924
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9006
      an argument of a comparison predicate. 
 
9007
    */ 
 
9008
    uchar *is_subst_valid= (uchar *) 1;
1925
9009
    cond= cond->compile(&Item::subst_argument_checker,
1926
 
                        &is_subst_valid,
 
9010
                        &is_subst_valid, 
1927
9011
                        &Item::equal_fields_propagator,
1928
 
                        (unsigned char *) inherited);
 
9012
                        (uchar *) inherited);
1929
9013
    cond->update_used_tables();
1930
9014
  }
1931
9015
  return cond;
1932
9016
}
1933
9017
 
 
9018
 
1934
9019
/**
1935
9020
  Build multiple equalities for a condition and all on expressions that
1936
9021
  inherit these multiple equalities.
1976
9061
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1977
9062
        WHERE t1.a=t2.a
1978
9063
    @endcode
1979
 
    that is equivalent to:
 
9064
    that is equivalent to:   
1980
9065
    @code
1981
9066
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1982
9067
        WHERE t1.a=t2.a
1983
9068
    @endcode
1984
9069
    Thus, applying equalities from the where condition we basically
1985
9070
    can get more freedom in performing join operations.
1986
 
    Althogh we don't use this property now, it probably makes sense to use
1987
 
    it in the future.
1988
 
  @param session                      Thread Cursor
 
9071
    Althogh we don't use this property now, it probably makes sense to use 
 
9072
    it in the future.    
 
9073
  @param thd                  Thread handler
1989
9074
  @param cond                condition to build the multiple equalities for
1990
9075
  @param inherited           path to all inherited multiple equality items
1991
9076
  @param join_list           list of join tables to which the condition
1996
9081
  @return
1997
9082
    pointer to the transformed condition containing multiple equalities
1998
9083
*/
1999
 
static COND *build_equal_items(Session *session, COND *cond,
 
9084
   
 
9085
static COND *build_equal_items(THD *thd, COND *cond,
2000
9086
                               COND_EQUAL *inherited,
2001
9087
                               List<TableList> *join_list,
2002
9088
                               COND_EQUAL **cond_equal_ref)
2003
9089
{
2004
9090
  COND_EQUAL *cond_equal= 0;
2005
9091
 
2006
 
  if (cond)
 
9092
  if (cond) 
2007
9093
  {
2008
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9094
    cond= build_equal_items_for_cond(thd, cond, inherited);
2009
9095
    cond->update_used_tables();
2010
9096
    if (cond->type() == Item::COND_ITEM &&
2011
9097
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2033
9119
    {
2034
9120
      if (table->on_expr)
2035
9121
      {
2036
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2037
 
          &table->getNestedJoin()->join_list : NULL;
 
9122
        List<TableList> *nested_join_list= table->nested_join ?
 
9123
          &table->nested_join->join_list : NULL;
2038
9124
        /*
2039
9125
          We can modify table->on_expr because its old value will
2040
9126
          be restored before re-execution of PS/SP.
2041
9127
        */
2042
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9128
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2043
9129
                                          nested_join_list,
2044
9130
                                          &table->cond_equal);
2045
9131
      }
2047
9133
  }
2048
9134
 
2049
9135
  return cond;
2050
 
}
 
9136
}    
 
9137
 
2051
9138
 
2052
9139
/**
2053
9140
  Compare field items by table order in the execution plan.
2054
9141
 
2055
9142
    field1 considered as better than field2 if the table containing
2056
 
    field1 is accessed earlier than the table containing field2.
 
9143
    field1 is accessed earlier than the table containing field2.   
2057
9144
    The function finds out what of two fields is better according
2058
9145
    this criteria.
2059
9146
 
2068
9155
  @retval
2069
9156
    0  otherwise
2070
9157
*/
 
9158
 
2071
9159
static int compare_fields_by_table_order(Item_field *field1,
2072
 
                                         Item_field *field2,
2073
 
                                         void *table_join_idx)
 
9160
                                  Item_field *field2,
 
9161
                                  void *table_join_idx)
2074
9162
{
2075
9163
  int cmp= 0;
2076
9164
  bool outer_ref= 0;
2077
9165
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2078
 
  {
 
9166
  {  
2079
9167
    outer_ref= 1;
2080
9168
    cmp= -1;
2081
9169
  }
2086
9174
  }
2087
9175
  if (outer_ref)
2088
9176
    return cmp;
2089
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2090
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9177
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9178
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2091
9179
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2092
9180
}
2093
9181
 
 
9182
 
2094
9183
/**
2095
9184
  Generate minimal set of simple equalities equivalent to a multiple equality.
2096
9185
 
2130
9219
    a pointer to the simple generated equality, if success.
2131
9220
    - 0, otherwise.
2132
9221
*/
2133
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9222
 
 
9223
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9224
                                  Item_equal *item_equal)
2134
9225
{
2135
9226
  List<Item> eq_list;
2136
9227
  Item_func_eq *eq_item= 0;
2137
9228
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2138
 
    return new Item_int((int64_t) 0,1);
 
9229
    return new Item_int((int64_t) 0,1); 
2139
9230
  Item *item_const= item_equal->get_const();
2140
9231
  Item_equal_iterator it(*item_equal);
2141
9232
  Item *head;
2152
9243
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2153
9244
    Item_field *item= item_field;
2154
9245
    if (upper)
2155
 
    {
 
9246
    { 
2156
9247
      if (item_const && upper->get_const())
2157
9248
        item= 0;
2158
9249
      else
2196
9287
 
2197
9288
  cond->quick_fix_field();
2198
9289
  cond->update_used_tables();
2199
 
 
 
9290
   
2200
9291
  return cond;
2201
9292
}
2202
9293
 
 
9294
 
2203
9295
/**
2204
9296
  Substitute every field reference in a condition by the best equal field
2205
9297
  and eliminate all multiple equality predicates.
2208
9300
    multiple equality predicate it sorts the field references in it
2209
9301
    according to the order of tables specified by the table_join_idx
2210
9302
    parameter. Then it eliminates the multiple equality predicate it
2211
 
    replacing it by the conjunction of simple equality predicates
 
9303
    replacing it by the conjunction of simple equality predicates 
2212
9304
    equating every field from the multiple equality to the first
2213
9305
    field in it, or to the constant, if there is any.
2214
9306
    After this the function retrieves all other conjuncted
2227
9319
  @return
2228
9320
    The transformed condition
2229
9321
*/
2230
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9322
 
 
9323
static COND* substitute_for_best_equal_field(COND *cond,
 
9324
                                             COND_EQUAL *cond_equal,
 
9325
                                             void *table_join_idx)
2231
9326
{
2232
9327
  Item_equal *item_equal;
2233
9328
 
2242
9337
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2243
9338
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2244
9339
 
2245
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
9340
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2246
9341
      while ((item_equal= it++))
2247
9342
      {
2248
9343
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2249
9344
      }
2250
9345
    }
2251
 
 
 
9346
    
2252
9347
    List_iterator<Item> li(*cond_list);
2253
9348
    Item *item;
2254
9349
    while ((item= li++))
2281
9376
      cond= new Item_int((int32_t)cond->val_bool());
2282
9377
 
2283
9378
  }
2284
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9379
  else if (cond->type() == Item::FUNC_ITEM && 
2285
9380
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2286
9381
  {
2287
9382
    item_equal= (Item_equal *) cond;
2295
9390
  return cond;
2296
9391
}
2297
9392
 
 
9393
 
2298
9394
/**
2299
9395
  Check appearance of new constant items in multiple equalities
2300
9396
  of a condition after reading a constant table.
2307
9403
  @param cond       condition whose multiple equalities are to be checked
2308
9404
  @param table      constant table that has been read
2309
9405
*/
2310
 
static void update_const_equal_items(COND *cond, JoinTable *tab)
 
9406
 
 
9407
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2311
9408
{
2312
9409
  if (!(cond->used_tables() & tab->table->map))
2313
9410
    return;
2314
9411
 
2315
9412
  if (cond->type() == Item::COND_ITEM)
2316
9413
  {
2317
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
 
9414
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
2318
9415
    List_iterator_fast<Item> li(*cond_list);
2319
9416
    Item *item;
2320
9417
    while ((item= li++))
2321
9418
      update_const_equal_items(item, tab);
2322
9419
  }
2323
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9420
  else if (cond->type() == Item::FUNC_ITEM && 
2324
9421
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2325
9422
  {
2326
9423
    Item_equal *item_equal= (Item_equal *) cond;
2334
9431
      while ((item_field= it++))
2335
9432
      {
2336
9433
        Field *field= item_field->field;
2337
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9434
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2338
9435
        key_map possible_keys= field->key_start;
2339
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2340
 
        stat[0].const_keys|= possible_keys;
 
9436
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9437
        stat[0].const_keys.merge(possible_keys);
2341
9438
 
2342
9439
        /*
2343
 
          For each field in the multiple equality (for which we know that it
2344
 
          is a constant) we have to find its corresponding key part, and set
 
9440
          For each field in the multiple equality (for which we know that it 
 
9441
          is a constant) we have to find its corresponding key part, and set 
2345
9442
          that key part in const_key_parts.
2346
 
        */
2347
 
        if (possible_keys.any())
 
9443
        */  
 
9444
        if (!possible_keys.is_clear_all())
2348
9445
        {
2349
 
          Table *field_tab= field->getTable();
2350
 
          optimizer::KeyUse *use;
2351
 
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2352
 
            if (possible_keys.test(use->getKey()) &&
2353
 
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
 
9446
          Table *tab= field->table;
 
9447
          KEYUSE *use;
 
9448
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9449
            if (possible_keys.is_set(use->key) && 
 
9450
                tab->key_info[use->key].key_part[use->keypart].field ==
2354
9451
                field)
2355
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9452
              tab->const_key_parts[use->key]|= use->keypart_map;
2356
9453
        }
2357
9454
      }
2358
9455
    }
2359
9456
  }
2360
9457
}
2361
9458
 
 
9459
 
2362
9460
/*
2363
9461
  change field = field to field = const for each found field = const in the
2364
9462
  and_level
2365
9463
*/
2366
 
static void change_cond_ref_to_const(Session *session,
2367
 
                                     vector<COND_CMP>& save_list,
2368
 
                                     Item *and_father,
2369
 
                                     Item *cond,
2370
 
                                     Item *field,
2371
 
                                     Item *value)
 
9464
 
 
9465
static void
 
9466
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9467
                         Item *and_father, Item *cond,
 
9468
                         Item *field, Item *value)
2372
9469
{
2373
9470
  if (cond->type() == Item::COND_ITEM)
2374
9471
  {
2375
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9472
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9473
      Item_func::COND_AND_FUNC;
2376
9474
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2377
9475
    Item *item;
2378
9476
    while ((item=li++))
2379
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2380
 
 
 
9477
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9478
                               field, value);
2381
9479
    return;
2382
9480
  }
2383
9481
  if (cond->eq_cmp_result() == Item::COND_OK)
2396
9494
       left_item->collation.collation == value->collation.collation))
2397
9495
  {
2398
9496
    Item *tmp=value->clone_item();
 
9497
    tmp->collation.set(right_item->collation);
 
9498
    
2399
9499
    if (tmp)
2400
9500
    {
2401
 
      tmp->collation.set(right_item->collation);
2402
 
      session->change_item_tree(args + 1, tmp);
 
9501
      thd->change_item_tree(args + 1, tmp);
2403
9502
      func->update_used_tables();
2404
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2405
 
                and_father != cond && 
2406
 
          ! left_item->const_item())
 
9503
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9504
          && and_father != cond && !left_item->const_item())
2407
9505
      {
2408
 
        cond->marker=1;
2409
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9506
        cond->marker=1;
 
9507
        COND_CMP *tmp2;
 
9508
        if ((tmp2=new COND_CMP(and_father,func)))
 
9509
          save_list->push_back(tmp2);
2410
9510
      }
2411
9511
      func->set_cmp_func();
2412
9512
    }
2418
9518
            right_item->collation.collation == value->collation.collation))
2419
9519
  {
2420
9520
    Item *tmp= value->clone_item();
 
9521
    tmp->collation.set(left_item->collation);
 
9522
    
2421
9523
    if (tmp)
2422
9524
    {
2423
 
      tmp->collation.set(left_item->collation);
2424
 
      session->change_item_tree(args, tmp);
 
9525
      thd->change_item_tree(args, tmp);
2425
9526
      value= tmp;
2426
9527
      func->update_used_tables();
2427
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2428
 
          and_father != cond && 
2429
 
          ! right_item->const_item())
 
9528
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9529
          && and_father != cond && !right_item->const_item())
2430
9530
      {
2431
9531
        args[0]= args[1];                       // For easy check
2432
 
        session->change_item_tree(args + 1, value);
2433
 
        cond->marker=1;
2434
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9532
        thd->change_item_tree(args + 1, value);
 
9533
        cond->marker=1;
 
9534
        COND_CMP *tmp2;
 
9535
        if ((tmp2=new COND_CMP(and_father,func)))
 
9536
          save_list->push_back(tmp2);
2435
9537
      }
2436
9538
      func->set_cmp_func();
2437
9539
    }
2446
9548
  @return
2447
9549
    new conditions
2448
9550
*/
2449
 
Item *remove_additional_cond(Item* conds)
 
9551
 
 
9552
static Item *remove_additional_cond(Item* conds)
2450
9553
{
2451
9554
  if (conds->name == in_additional_cond)
2452
9555
    return 0;
2469
9572
  return conds;
2470
9573
}
2471
9574
 
2472
 
static void propagate_cond_constants(Session *session, 
2473
 
                                     vector<COND_CMP>& save_list, 
2474
 
                                     COND *and_father, 
2475
 
                                     COND *cond)
 
9575
static void
 
9576
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9577
                         COND *and_father, COND *cond)
2476
9578
{
2477
9579
  if (cond->type() == Item::COND_ITEM)
2478
9580
  {
2479
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9581
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9582
      Item_func::COND_AND_FUNC;
2480
9583
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2481
9584
    Item *item;
2482
 
    vector<COND_CMP> save;
 
9585
    I_List<COND_CMP> save;
2483
9586
    while ((item=li++))
2484
9587
    {
2485
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9588
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2486
9589
    }
2487
9590
    if (and_level)
2488
 
    {
2489
 
      // Handle other found items
2490
 
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9591
    {                                           // Handle other found items
 
9592
      I_List_iterator<COND_CMP> cond_itr(save);
 
9593
      COND_CMP *cond_cmp;
 
9594
      while ((cond_cmp=cond_itr++))
2491
9595
      {
2492
 
        Item **args= iter->cmp_func->arguments();
2493
 
        if (not args[0]->const_item())
2494
 
        {
2495
 
          change_cond_ref_to_const(session, save_list, iter->and_level,
2496
 
                                   iter->and_level, args[0], args[1] );
2497
 
        }
 
9596
        Item **args= cond_cmp->cmp_func->arguments();
 
9597
        if (!args[0]->const_item())
 
9598
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9599
                                   cond_cmp->and_level, args[0], args[1]);
2498
9600
      }
2499
9601
    }
2500
9602
  }
2501
9603
  else if (and_father != cond && !cond->marker)         // In a AND group
2502
9604
  {
2503
9605
    if (cond->type() == Item::FUNC_ITEM &&
2504
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2505
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9606
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9607
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2506
9608
    {
2507
9609
      Item_func_eq *func=(Item_func_eq*) cond;
2508
9610
      Item **args= func->arguments();
2511
9613
      if (!(left_const && right_const) &&
2512
9614
          args[0]->result_type() == args[1]->result_type())
2513
9615
      {
2514
 
        if (right_const)
2515
 
        {
2516
 
                resolve_const_item(session, &args[1], args[0]);
2517
 
          func->update_used_tables();
2518
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2519
 
                                        args[0], args[1]);
2520
 
        }
2521
 
        else if (left_const)
2522
 
        {
2523
 
                resolve_const_item(session, &args[0], args[1]);
2524
 
          func->update_used_tables();
2525
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2526
 
                                        args[1], args[0]);
2527
 
        }
2528
 
      }
2529
 
    }
2530
 
  }
2531
 
}
 
9616
        if (right_const)
 
9617
        {
 
9618
          resolve_const_item(thd, &args[1], args[0]);
 
9619
          func->update_used_tables();
 
9620
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9621
                                   args[0], args[1]);
 
9622
        }
 
9623
        else if (left_const)
 
9624
        {
 
9625
          resolve_const_item(thd, &args[0], args[1]);
 
9626
          func->update_used_tables();
 
9627
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9628
                                   args[1], args[0]);
 
9629
        }
 
9630
      }
 
9631
    }
 
9632
  }
 
9633
}
 
9634
 
 
9635
 
 
9636
/**
 
9637
  Simplify joins replacing outer joins by inner joins whenever it's
 
9638
  possible.
 
9639
 
 
9640
    The function, during a retrieval of join_list,  eliminates those
 
9641
    outer joins that can be converted into inner join, possibly nested.
 
9642
    It also moves the on expressions for the converted outer joins
 
9643
    and from inner joins to conds.
 
9644
    The function also calculates some attributes for nested joins:
 
9645
    - used_tables    
 
9646
    - not_null_tables
 
9647
    - dep_tables.
 
9648
    - on_expr_dep_tables
 
9649
    The first two attributes are used to test whether an outer join can
 
9650
    be substituted for an inner join. The third attribute represents the
 
9651
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9652
    on table t1, then in any evaluated execution plan table access to
 
9653
    table t2 must precede access to table t2. This relation is used also
 
9654
    to check whether the query contains  invalid cross-references.
 
9655
    The forth attribute is an auxiliary one and is used to calculate
 
9656
    dep_tables.
 
9657
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9658
    execution plan, the dependencies required by the straight join
 
9659
    modifiers are reflected in this attribute as well.
 
9660
    The function also removes all braces that can be removed from the join
 
9661
    expression without changing its meaning.
 
9662
 
 
9663
  @note
 
9664
    An outer join can be replaced by an inner join if the where condition
 
9665
    or the on expression for an embedding nested join contains a conjunctive
 
9666
    predicate rejecting null values for some attribute of the inner tables.
 
9667
 
 
9668
    E.g. in the query:    
 
9669
    @code
 
9670
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9671
    @endcode
 
9672
    the predicate t2.b < 5 rejects nulls.
 
9673
    The query is converted first to:
 
9674
    @code
 
9675
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9676
    @endcode
 
9677
    then to the equivalent form:
 
9678
    @code
 
9679
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9680
    @endcode
 
9681
 
 
9682
 
 
9683
    Similarly the following query:
 
9684
    @code
 
9685
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9686
        WHERE t2.c < 5  
 
9687
    @endcode
 
9688
    is converted to:
 
9689
    @code
 
9690
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9691
 
 
9692
    @endcode
 
9693
 
 
9694
    One conversion might trigger another:
 
9695
    @code
 
9696
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9697
                       LEFT JOIN t3 ON t3.b=t2.b
 
9698
        WHERE t3 IS NOT NULL =>
 
9699
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9700
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9701
      SELECT * FROM t1, t2, t3
 
9702
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9703
  @endcode
 
9704
 
 
9705
    The function removes all unnecessary braces from the expression
 
9706
    produced by the conversions.
 
9707
    E.g.
 
9708
    @code
 
9709
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9710
    @endcode
 
9711
    finally is converted to: 
 
9712
    @code
 
9713
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9714
 
 
9715
    @endcode
 
9716
 
 
9717
 
 
9718
    It also will remove braces from the following queries:
 
9719
    @code
 
9720
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9721
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9722
    @endcode
 
9723
 
 
9724
    The benefit of this simplification procedure is that it might return 
 
9725
    a query for which the optimizer can evaluate execution plan with more
 
9726
    join orders. With a left join operation the optimizer does not
 
9727
    consider any plan where one of the inner tables is before some of outer
 
9728
    tables.
 
9729
 
 
9730
  IMPLEMENTATION
 
9731
    The function is implemented by a recursive procedure.  On the recursive
 
9732
    ascent all attributes are calculated, all outer joins that can be
 
9733
    converted are replaced and then all unnecessary braces are removed.
 
9734
    As join list contains join tables in the reverse order sequential
 
9735
    elimination of outer joins does not require extra recursive calls.
 
9736
 
 
9737
  SEMI-JOIN NOTES
 
9738
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9739
    an "ancestor" semi-join nest)
 
9740
 
 
9741
  EXAMPLES
 
9742
    Here is an example of a join query with invalid cross references:
 
9743
    @code
 
9744
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9745
    @endcode
 
9746
 
 
9747
  @param join        reference to the query info
 
9748
  @param join_list   list representation of the join to be converted
 
9749
  @param conds       conditions to add on expressions for converted joins
 
9750
  @param top         true <=> conds is the where condition
 
9751
 
 
9752
  @return
 
9753
    - The new condition, if success
 
9754
    - 0, otherwise
 
9755
*/
 
9756
 
 
9757
static COND *
 
9758
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9759
               bool in_sj)
 
9760
{
 
9761
  TableList *table;
 
9762
  nested_join_st *nested_join;
 
9763
  TableList *prev_table= 0;
 
9764
  List_iterator<TableList> li(*join_list);
 
9765
 
 
9766
  /* 
 
9767
    Try to simplify join operations from join_list.
 
9768
    The most outer join operation is checked for conversion first. 
 
9769
  */
 
9770
  while ((table= li++))
 
9771
  {
 
9772
    table_map used_tables;
 
9773
    table_map not_null_tables= (table_map) 0;
 
9774
 
 
9775
    if ((nested_join= table->nested_join))
 
9776
    {
 
9777
      /* 
 
9778
         If the element of join_list is a nested join apply
 
9779
         the procedure to its nested join list first.
 
9780
      */
 
9781
      if (table->on_expr)
 
9782
      {
 
9783
        Item *expr= table->on_expr;
 
9784
        /* 
 
9785
           If an on expression E is attached to the table, 
 
9786
           check all null rejected predicates in this expression.
 
9787
           If such a predicate over an attribute belonging to
 
9788
           an inner table of an embedded outer join is found,
 
9789
           the outer join is converted to an inner join and
 
9790
           the corresponding on expression is added to E. 
 
9791
        */ 
 
9792
        expr= simplify_joins(join, &nested_join->join_list,
 
9793
                             expr, false, in_sj || table->sj_on_expr);
 
9794
 
 
9795
        if (!table->prep_on_expr || expr != table->on_expr)
 
9796
        {
 
9797
          assert(expr);
 
9798
 
 
9799
          table->on_expr= expr;
 
9800
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9801
        }
 
9802
      }
 
9803
      nested_join->used_tables= (table_map) 0;
 
9804
      nested_join->not_null_tables=(table_map) 0;
 
9805
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9806
                            in_sj || table->sj_on_expr);
 
9807
      used_tables= nested_join->used_tables;
 
9808
      not_null_tables= nested_join->not_null_tables;  
 
9809
    }
 
9810
    else
 
9811
    {
 
9812
      if (!table->prep_on_expr)
 
9813
        table->prep_on_expr= table->on_expr;
 
9814
      used_tables= table->table->map;
 
9815
      if (conds)
 
9816
        not_null_tables= conds->not_null_tables();
 
9817
    }
 
9818
      
 
9819
    if (table->embedding)
 
9820
    {
 
9821
      table->embedding->nested_join->used_tables|= used_tables;
 
9822
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9823
    }
 
9824
 
 
9825
    if (!table->outer_join || (used_tables & not_null_tables))
 
9826
    {
 
9827
      /* 
 
9828
        For some of the inner tables there are conjunctive predicates
 
9829
        that reject nulls => the outer join can be replaced by an inner join.
 
9830
      */
 
9831
      table->outer_join= 0;
 
9832
      if (table->on_expr)
 
9833
      {
 
9834
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9835
        if (conds)
 
9836
        {
 
9837
          conds= and_conds(conds, table->on_expr);
 
9838
          conds->top_level_item();
 
9839
          /* conds is always a new item as both cond and on_expr existed */
 
9840
          assert(!conds->fixed);
 
9841
          conds->fix_fields(join->thd, &conds);
 
9842
        }
 
9843
        else
 
9844
          conds= table->on_expr; 
 
9845
        table->prep_on_expr= table->on_expr= 0;
 
9846
      }
 
9847
    }
 
9848
    
 
9849
    if (!top)
 
9850
      continue;
 
9851
 
 
9852
    /* 
 
9853
      Only inner tables of non-convertible outer joins
 
9854
      remain with on_expr.
 
9855
    */ 
 
9856
    if (table->on_expr)
 
9857
    {
 
9858
      table->dep_tables|= table->on_expr->used_tables(); 
 
9859
      if (table->embedding)
 
9860
      {
 
9861
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9862
        /*
 
9863
           Embedding table depends on tables used
 
9864
           in embedded on expressions. 
 
9865
        */
 
9866
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9867
      }
 
9868
      else
 
9869
        table->dep_tables&= ~table->table->map;
 
9870
    }
 
9871
 
 
9872
    if (prev_table)
 
9873
    {
 
9874
      /* The order of tables is reverse: prev_table follows table */
 
9875
      if (prev_table->straight)
 
9876
        prev_table->dep_tables|= used_tables;
 
9877
      if (prev_table->on_expr)
 
9878
      {
 
9879
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9880
        table_map prev_used_tables= prev_table->nested_join ?
 
9881
                                    prev_table->nested_join->used_tables :
 
9882
                                    prev_table->table->map;
 
9883
        /* 
 
9884
          If on expression contains only references to inner tables
 
9885
          we still make the inner tables dependent on the outer tables.
 
9886
          It would be enough to set dependency only on one outer table
 
9887
          for them. Yet this is really a rare case.
 
9888
        */  
 
9889
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9890
          prev_table->dep_tables|= used_tables;
 
9891
      }
 
9892
    }
 
9893
    prev_table= table;
 
9894
  }
 
9895
    
 
9896
  /* 
 
9897
    Flatten nested joins that can be flattened.
 
9898
    no ON expression and not a semi-join => can be flattened.
 
9899
  */
 
9900
  li.rewind();
 
9901
  while ((table= li++))
 
9902
  {
 
9903
    nested_join= table->nested_join;
 
9904
    if (table->sj_on_expr && !in_sj)
 
9905
    {
 
9906
       /*
 
9907
         If this is a semi-join that is not contained within another semi-join, 
 
9908
         leave it intact (otherwise it is flattened)
 
9909
       */
 
9910
      join->select_lex->sj_nests.push_back(table);
 
9911
    }
 
9912
    else if (nested_join && !table->on_expr)
 
9913
    {
 
9914
      TableList *tbl;
 
9915
      List_iterator<TableList> it(nested_join->join_list);
 
9916
      while ((tbl= it++))
 
9917
      {
 
9918
        tbl->embedding= table->embedding;
 
9919
        tbl->join_list= table->join_list;
 
9920
      }      
 
9921
      li.replace(nested_join->join_list);
 
9922
    }
 
9923
  }
 
9924
  return(conds); 
 
9925
}
 
9926
 
 
9927
 
 
9928
/**
 
9929
  Assign each nested join structure a bit in nested_join_map.
 
9930
 
 
9931
    Assign each nested join structure (except "confluent" ones - those that
 
9932
    embed only one element) a bit in nested_join_map.
 
9933
 
 
9934
  @param join          Join being processed
 
9935
  @param join_list     List of tables
 
9936
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9937
                       call
 
9938
 
 
9939
  @note
 
9940
    This function is called after simplify_joins(), when there are no
 
9941
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9942
    we will not run out of bits in nested_join_map.
 
9943
 
 
9944
  @return
 
9945
    First unused bit in nested_join_map after the call.
 
9946
*/
 
9947
 
 
9948
static uint build_bitmap_for_nested_joins(List<TableList> *join_list, 
 
9949
                                          uint first_unused)
 
9950
{
 
9951
  List_iterator<TableList> li(*join_list);
 
9952
  TableList *table;
 
9953
  while ((table= li++))
 
9954
  {
 
9955
    nested_join_st *nested_join;
 
9956
    if ((nested_join= table->nested_join))
 
9957
    {
 
9958
      /*
 
9959
        It is guaranteed by simplify_joins() function that a nested join
 
9960
        that has only one child is either
 
9961
         - a single-table view (the child is the underlying table), or 
 
9962
         - a single-table semi-join nest
 
9963
 
 
9964
        We don't assign bits to such sj-nests because 
 
9965
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9966
            with anything)
 
9967
        2. we could run out bits in nested_join_map otherwise.
 
9968
      */
 
9969
      if (nested_join->join_list.elements != 1)
 
9970
      {
 
9971
        /* Don't assign bits to sj-nests */
 
9972
        if (table->on_expr)
 
9973
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9974
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9975
                                                    first_unused);
 
9976
      }
 
9977
    }
 
9978
  }
 
9979
  return(first_unused);
 
9980
}
 
9981
 
 
9982
 
 
9983
/**
 
9984
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
9985
 
 
9986
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
9987
    the passed join_list.
 
9988
 
 
9989
  @param join_list  List of nested joins to process. It may also contain base
 
9990
                    tables which will be ignored.
 
9991
*/
 
9992
 
 
9993
static void reset_nj_counters(List<TableList> *join_list)
 
9994
{
 
9995
  List_iterator<TableList> li(*join_list);
 
9996
  TableList *table;
 
9997
  while ((table= li++))
 
9998
  {
 
9999
    nested_join_st *nested_join;
 
10000
    if ((nested_join= table->nested_join))
 
10001
    {
 
10002
      nested_join->counter_= 0;
 
10003
      reset_nj_counters(&nested_join->join_list);
 
10004
    }
 
10005
  }
 
10006
  return;
 
10007
}
 
10008
 
2532
10009
 
2533
10010
/**
2534
10011
  Check interleaving with an inner tables of an outer join for
2535
10012
  extension table.
2536
10013
 
2537
 
    Check if table next_tab can be added to current partial join order, and
 
10014
    Check if table next_tab can be added to current partial join order, and 
2538
10015
    if yes, record that it has been added.
2539
10016
 
2540
10017
    The function assumes that both current partial join order and its
2541
10018
    extension with next_tab are valid wrt table dependencies.
2542
10019
 
2543
10020
  @verbatim
2544
 
     IMPLEMENTATION
 
10021
     IMPLEMENTATION 
2545
10022
       LIMITATIONS ON JOIN order_st
2546
10023
         The nested [outer] joins executioner algorithm imposes these limitations
2547
10024
         on join order:
2548
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10025
         1. "Outer tables first" -  any "outer" table must be before any 
2549
10026
             corresponding "inner" table.
2550
10027
         2. "No interleaving" - tables inside a nested join must form a continuous
2551
 
            sequence in join order (i.e. the sequence must not be interrupted by
 
10028
            sequence in join order (i.e. the sequence must not be interrupted by 
2552
10029
            tables that are outside of this nested join).
2553
10030
 
2554
10031
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2555
10032
         been already checked.
2556
10033
 
2557
10034
       WHY NEED NON-INTERLEAVING
2558
 
         Consider an example:
 
10035
         Consider an example: 
2559
10036
 
2560
10037
           select * from t0 join t1 left join (t2 join t3) on cond1
2561
10038
 
2579
10056
         The limitations on join order can be rephrased as follows: for valid
2580
10057
         join order one must be able to:
2581
10058
           1. write down the used tables in the join order on one line.
2582
 
           2. for each nested join, put one '(' and one ')' on the said line
 
10059
           2. for each nested join, put one '(' and one ')' on the said line        
2583
10060
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2584
10061
           4. get a query equivalent to the query we're trying to execute.
2585
10062
 
2586
10063
         Calls to check_interleaving_with_nj() are equivalent to writing the
2587
 
         above described line from left to right.
2588
 
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
 
10064
         above described line from left to right. 
 
10065
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2589
10066
         table B and appropriate brackets on condition that table A and
2590
10067
         appropriate brackets is the last what was written. Graphically the
2591
10068
         transition is as follows:
2598
10075
                                                      position.
2599
10076
 
2600
10077
         Notes about the position:
2601
 
           The caller guarantees that there is no more then one X-bracket by
2602
 
           checking "!(remaining_tables & s->dependent)" before calling this
 
10078
           The caller guarantees that there is no more then one X-bracket by 
 
10079
           checking "!(remaining_tables & s->dependent)" before calling this 
2603
10080
           function. X-bracket may have a pair in Y-bracket.
2604
10081
 
2605
10082
         When "writing" we store/update this auxilary info about the current
2612
10089
  @endverbatim
2613
10090
 
2614
10091
  @param join       Join being processed
 
10092
  @param last_tab   Last table in current partial join order (this function is
 
10093
                    not called for empty partial join orders)
2615
10094
  @param next_tab   Table we're going to extend the current partial join with
2616
10095
 
2617
10096
  @retval
2620
10099
  @retval
2621
10100
    true   Requested join order extension not allowed.
2622
10101
*/
2623
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10102
 
 
10103
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2624
10104
{
2625
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2626
 
  Join *join= next_tab->join;
 
10105
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10106
  JOIN *join= last_tab->join;
2627
10107
 
2628
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10108
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2629
10109
  {
2630
 
    /*
 
10110
    /* 
2631
10111
      next_tab is outside of the "pair of brackets" we're currently in.
2632
10112
      Cannot add it.
2633
10113
    */
2634
10114
    return true;
2635
10115
  }
2636
 
 
 
10116
   
2637
10117
  /*
2638
10118
    Do update counters for "pairs of brackets" that we've left (marked as
2639
10119
    X,Y,Z in the above picture)
2640
10120
  */
2641
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10121
  for (;next_emb; next_emb= next_emb->embedding)
2642
10122
  {
2643
 
    next_emb->getNestedJoin()->counter_++;
2644
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10123
    next_emb->nested_join->counter_++;
 
10124
    if (next_emb->nested_join->counter_ == 1)
2645
10125
    {
2646
 
      /*
 
10126
      /* 
2647
10127
        next_emb is the first table inside a nested join we've "entered". In
2648
10128
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2649
10129
        X bracket might have Y pair bracket.
2650
10130
      */
2651
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10131
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2652
10132
    }
2653
 
 
2654
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2655
 
        next_emb->getNestedJoin()->counter_)
 
10133
    
 
10134
    if (next_emb->nested_join->join_list.elements !=
 
10135
        next_emb->nested_join->counter_)
2656
10136
      break;
2657
10137
 
2658
10138
    /*
2659
10139
      We're currently at Y or Z-bracket as depicted in the above picture.
2660
10140
      Mark that we've left it and continue walking up the brackets hierarchy.
2661
10141
    */
2662
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10142
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2663
10143
  }
2664
10144
  return false;
2665
10145
}
2666
10146
 
2667
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2668
 
{
2669
 
  Session *session= join->session;
 
10147
 
 
10148
/**
 
10149
  Nested joins perspective: Remove the last table from the join order.
 
10150
 
 
10151
    Remove the last table from the partial join order and update the nested
 
10152
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10153
    function for the first table in join order (for which 
 
10154
    check_interleaving_with_nj has not been called)
 
10155
 
 
10156
  @param last  join table to remove, it is assumed to be the last in current
 
10157
               partial join order.
 
10158
*/
 
10159
 
 
10160
static void restore_prev_nj_state(JOIN_TAB *last)
 
10161
{
 
10162
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10163
  JOIN *join= last->join;
 
10164
  while (last_emb)
 
10165
  {
 
10166
    if (last_emb->on_expr)
 
10167
    {
 
10168
      if (!(--last_emb->nested_join->counter_))
 
10169
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10170
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10171
               last_emb->nested_join->counter_) 
 
10172
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10173
      else
 
10174
        break;
 
10175
    }
 
10176
    last_emb= last_emb->embedding;
 
10177
  }
 
10178
}
 
10179
 
 
10180
 
 
10181
 
 
10182
static 
 
10183
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10184
{
 
10185
  TableList *emb_sj_nest;
 
10186
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10187
  {
 
10188
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10189
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10190
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10191
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10192
  }
 
10193
}
 
10194
 
 
10195
 
 
10196
/*
 
10197
  we assume remaining_tables doesnt contain @tab.
 
10198
*/
 
10199
 
 
10200
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10201
                                  const JOIN_TAB *tab)
 
10202
{
 
10203
  TableList *emb_sj_nest;
 
10204
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10205
  {
 
10206
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10207
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10208
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10209
    {
 
10210
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10211
    }
 
10212
  }
 
10213
}
 
10214
 
 
10215
 
 
10216
static COND *
 
10217
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10218
              Item::cond_result *cond_value)
 
10219
{
 
10220
  THD *thd= join->thd;
2670
10221
 
2671
10222
  if (!conds)
2672
10223
    *cond_value= Item::COND_TRUE;
2673
10224
  else
2674
10225
  {
2675
 
    /*
 
10226
    /* 
2676
10227
      Build all multiple equality predicates and eliminate equality
2677
10228
      predicates that can be inferred from these multiple equalities.
2678
10229
      For each reference of a field included into a multiple equality
2679
10230
      that occurs in a function set a pointer to the multiple equality
2680
10231
      predicate. Substitute a constant instead of this field if the
2681
10232
      multiple equality contains a constant.
2682
 
    */
2683
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10233
    */ 
 
10234
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2684
10235
                             &join->cond_equal);
2685
10236
 
2686
10237
    /* change field = field to field = const for each found field = const */
2687
 
    vector<COND_CMP> temp;
2688
 
    propagate_cond_constants(session, temp, conds, conds);
 
10238
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2689
10239
    /*
2690
10240
      Remove all instances of item == item
2691
10241
      Remove all and-levels where CONST item != CONST item
2692
10242
    */
2693
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10243
    conds= remove_eq_conds(thd, conds, cond_value) ;
2694
10244
  }
2695
10245
  return(conds);
2696
10246
}
2697
10247
 
 
10248
 
2698
10249
/**
2699
10250
  Remove const and eq items.
2700
10251
 
2705
10256
    - COND_TRUE   : always true ( 1 = 1 )
2706
10257
    - COND_FALSE  : always false        ( 1 = 2 )
2707
10258
*/
2708
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10259
 
 
10260
COND *
 
10261
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2709
10262
{
2710
10263
  if (cond->type() == Item::COND_ITEM)
2711
10264
  {
2712
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2713
 
 
 
10265
    bool and_level= ((Item_cond*) cond)->functype()
 
10266
      == Item_func::COND_AND_FUNC;
2714
10267
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2715
10268
    Item::cond_result tmp_cond_value;
2716
 
    bool should_fix_fields= false;
 
10269
    bool should_fix_fields=0;
2717
10270
 
2718
 
    *cond_value= Item::COND_UNDEF;
 
10271
    *cond_value=Item::COND_UNDEF;
2719
10272
    Item *item;
2720
 
    while ((item= li++))
 
10273
    while ((item=li++))
2721
10274
    {
2722
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2723
 
      if (! new_item)
2724
 
              li.remove();
 
10275
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10276
      if (!new_item)
 
10277
        li.remove();
2725
10278
      else if (item != new_item)
2726
10279
      {
2727
 
        li.replace(new_item);
2728
 
        should_fix_fields= true;
 
10280
        VOID(li.replace(new_item));
 
10281
        should_fix_fields=1;
2729
10282
      }
2730
10283
      if (*cond_value == Item::COND_UNDEF)
2731
 
              *cond_value= tmp_cond_value;
2732
 
 
2733
 
      switch (tmp_cond_value) 
2734
 
      {
2735
 
        case Item::COND_OK:                     /* Not true or false */
2736
 
          if (and_level || (*cond_value == Item::COND_FALSE))
2737
 
            *cond_value= tmp_cond_value;
2738
 
          break;
2739
 
        case Item::COND_FALSE:
2740
 
          if (and_level)
2741
 
          {
2742
 
            *cond_value= tmp_cond_value;
2743
 
            return (COND *) NULL;                       /* Always false */
2744
 
          }
2745
 
          break;
2746
 
        case Item::COND_TRUE:
2747
 
          if (! and_level)
2748
 
          {
2749
 
            *cond_value= tmp_cond_value;
2750
 
            return (COND *) NULL;                       /* Always true */
2751
 
          }
2752
 
          break;
2753
 
        case Item::COND_UNDEF:                  /* Impossible */
2754
 
          break;
 
10284
        *cond_value=tmp_cond_value;
 
10285
      switch (tmp_cond_value) {
 
10286
      case Item::COND_OK:                       // Not true or false
 
10287
        if (and_level || *cond_value == Item::COND_FALSE)
 
10288
          *cond_value=tmp_cond_value;
 
10289
        break;
 
10290
      case Item::COND_FALSE:
 
10291
        if (and_level)
 
10292
        {
 
10293
          *cond_value=tmp_cond_value;
 
10294
          return (COND*) 0;                     // Always false
 
10295
        }
 
10296
        break;
 
10297
      case Item::COND_TRUE:
 
10298
        if (!and_level)
 
10299
        {
 
10300
          *cond_value= tmp_cond_value;
 
10301
          return (COND*) 0;                     // Always true
 
10302
        }
 
10303
        break;
 
10304
      case Item::COND_UNDEF:                    // Impossible
 
10305
        break; /* purecov: deadcode */
2755
10306
      }
2756
10307
    }
2757
 
 
2758
10308
    if (should_fix_fields)
2759
10309
      cond->update_used_tables();
2760
10310
 
2761
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2762
 
      return (COND*) NULL;
2763
 
 
 
10311
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10312
        *cond_value != Item::COND_OK)
 
10313
      return (COND*) 0;
2764
10314
    if (((Item_cond*) cond)->argument_list()->elements == 1)
2765
 
    {                                           
2766
 
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
10315
    {                                           // Remove list
2767
10316
      item= ((Item_cond*) cond)->argument_list()->head();
2768
10317
      ((Item_cond*) cond)->argument_list()->empty();
2769
10318
      return item;
2770
10319
    }
2771
10320
  }
2772
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10321
  else if (cond->type() == Item::FUNC_ITEM &&
 
10322
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2773
10323
  {
2774
10324
    /*
2775
10325
      Handles this special case for some ODBC applications:
2781
10331
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2782
10332
    */
2783
10333
 
2784
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10334
    Item_func_isnull *func=(Item_func_isnull*) cond;
2785
10335
    Item **args= func->arguments();
2786
10336
    if (args[0]->type() == Item::FIELD_ITEM)
2787
10337
    {
2788
 
      Field *field= ((Item_field*) args[0])->field;
2789
 
      if (field->flags & AUTO_INCREMENT_FLAG 
2790
 
          && ! field->getTable()->maybe_null 
2791
 
          && session->options & OPTION_AUTO_IS_NULL
2792
 
          && (
2793
 
            session->first_successful_insert_id_in_prev_stmt > 0 
2794
 
            && session->substitute_null_with_insert_id
2795
 
            )
2796
 
          )
 
10338
      Field *field=((Item_field*) args[0])->field;
 
10339
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10340
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10341
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10342
           thd->substitute_null_with_insert_id))
2797
10343
      {
2798
 
        COND *new_cond;
2799
 
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
2800
 
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
2801
 
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
2802
 
        {
2803
 
          cond= new_cond;
 
10344
        COND *new_cond;
 
10345
        if ((new_cond= new Item_func_eq(args[0],
 
10346
                                        new Item_int("last_insert_id()",
 
10347
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10348
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10349
        {
 
10350
          cond=new_cond;
2804
10351
          /*
2805
10352
            Item_func_eq can't be fixed after creation so we do not check
2806
10353
            cond->fixed, also it do not need tables so we use 0 as second
2807
10354
            argument.
2808
10355
          */
2809
 
          cond->fix_fields(session, &cond);
2810
 
        }
 
10356
          cond->fix_fields(thd, &cond);
 
10357
        }
2811
10358
        /*
2812
10359
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2813
10360
          clear for next row
2814
10361
        */
2815
 
        session->substitute_null_with_insert_id= false;
 
10362
        thd->substitute_null_with_insert_id= false;
2816
10363
      }
2817
 
#ifdef NOTDEFINED
2818
10364
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
2819
 
      else if (
2820
 
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
2821
 
          && (field->flags & NOT_NULL_FLAG) 
2822
 
          && ! field->table->maybe_null)
 
10365
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
 
10366
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10367
                (field->flags & NOT_NULL_FLAG) &&
 
10368
               !field->table->maybe_null)
2823
10369
      {
2824
 
        COND *new_cond;
2825
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
2826
 
        {
2827
 
          cond= new_cond;
 
10370
        COND *new_cond;
 
10371
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10372
        {
 
10373
          cond=new_cond;
2828
10374
          /*
2829
10375
            Item_func_eq can't be fixed after creation so we do not check
2830
10376
            cond->fixed, also it do not need tables so we use 0 as second
2831
10377
            argument.
2832
10378
          */
2833
 
          cond->fix_fields(session, &cond);
2834
 
        }
 
10379
          cond->fix_fields(thd, &cond);
 
10380
        }
2835
10381
      }
2836
 
#endif /* NOTDEFINED */
2837
10382
    }
2838
10383
    if (cond->const_item())
2839
10384
    {
2840
10385
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2841
 
      return (COND *) NULL;
 
10386
      return (COND*) 0;
2842
10387
    }
2843
10388
  }
2844
10389
  else if (cond->const_item() && !cond->is_expensive())
2845
10390
  /*
2846
10391
    TODO:
2847
10392
    Excluding all expensive functions is too restritive we should exclude only
2848
 
    materialized IN subquery predicates because they can't yet be evaluated
2849
 
    here (they need additional initialization that is done later on).
2850
 
 
2851
 
    The proper way to exclude the subqueries would be to walk the cond tree and
2852
 
    check for materialized subqueries there.
2853
 
 
 
10393
    materialized IN because it is created later than this phase, and cannot be
 
10394
    evaluated at this point.
 
10395
    The condition should be something as (need to fix member access):
 
10396
      !(cond->type() == Item::FUNC_ITEM &&
 
10397
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10398
        ((Item_in_optimizer*)cond)->is_expensive()))
2854
10399
  */
2855
10400
  {
2856
10401
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2857
 
    return (COND *) NULL;
 
10402
    return (COND*) 0;
2858
10403
  }
2859
10404
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2860
 
  {                                             
2861
 
    /* boolan compare function */
 
10405
  {                                             // boolan compare function
2862
10406
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2863
10407
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2864
10408
    if (left_item->eq(right_item,1))
2865
10409
    {
2866
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2867
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10410
      if (!left_item->maybe_null ||
 
10411
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10412
        return (COND*) 0;                       // Compare of identical items
2868
10413
    }
2869
10414
  }
2870
 
  *cond_value= Item::COND_OK;
2871
 
  return cond;                                  /* Point at next and return into recursion */
 
10415
  *cond_value=Item::COND_OK;
 
10416
  return cond;                                  // Point at next and level
2872
10417
}
2873
10418
 
2874
 
/*
 
10419
/* 
2875
10420
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2876
 
 
 
10421
  
2877
10422
  SYNOPSIS
2878
10423
    test_if_equality_guarantees_uniqueness()
2879
10424
      l          the left comparison argument (a field if any)
2880
10425
      r          the right comparison argument (a const of any)
2881
 
 
2882
 
  DESCRIPTION
2883
 
    Checks if an equality predicate can be used to take away
2884
 
    DISTINCT/GROUP BY because it is known to be true for exactly one
 
10426
  
 
10427
  DESCRIPTION    
 
10428
    Checks if an equality predicate can be used to take away 
 
10429
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2885
10430
    distinct value (e.g. <expr> == <const>).
2886
 
    Arguments must be of the same type because e.g.
2887
 
    <string_field> = <int_const> may match more than 1 distinct value from
2888
 
    the column.
2889
 
    We must take into consideration and the optimization done for various
 
10431
    Arguments must be of the same type because e.g. 
 
10432
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10433
    the column. 
 
10434
    We must take into consideration and the optimization done for various 
2890
10435
    string constants when compared to dates etc (see Item_int_with_ref) as
2891
10436
    well as the collation of the arguments.
2892
 
 
2893
 
  RETURN VALUE
 
10437
  
 
10438
  RETURN VALUE  
2894
10439
    true    can be used
2895
10440
    false   cannot be used
2896
10441
*/
2897
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10442
static bool
 
10443
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2898
10444
{
2899
10445
  return r->const_item() &&
2900
10446
    /* elements must be compared as dates */
2909
10455
/**
2910
10456
  Return true if the item is a const value in all the WHERE clause.
2911
10457
*/
2912
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10458
 
 
10459
static bool
 
10460
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2913
10461
{
2914
10462
  if (cond->type() == Item::COND_ITEM)
2915
10463
  {
2922
10470
      bool res=const_expression_in_where(item, comp_item, const_item);
2923
10471
      if (res)                                  // Is a const value
2924
10472
      {
2925
 
        if (and_level)
2926
 
          return 1;
 
10473
        if (and_level)
 
10474
          return 1;
2927
10475
      }
2928
10476
      else if (!and_level)
2929
 
        return 0;
 
10477
        return 0;
2930
10478
    }
2931
10479
    return and_level ? 0 : 1;
2932
10480
  }
2934
10482
  {                                             // boolan compare function
2935
10483
    Item_func* func= (Item_func*) cond;
2936
10484
    if (func->functype() != Item_func::EQUAL_FUNC &&
2937
 
              func->functype() != Item_func::EQ_FUNC)
 
10485
        func->functype() != Item_func::EQ_FUNC)
2938
10486
      return 0;
2939
10487
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2940
10488
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2942
10490
    {
2943
10491
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2944
10492
      {
2945
 
        if (*const_item)
2946
 
          return right_item->eq(*const_item, 1);
2947
 
        *const_item=right_item;
2948
 
        return 1;
 
10493
        if (*const_item)
 
10494
          return right_item->eq(*const_item, 1);
 
10495
        *const_item=right_item;
 
10496
        return 1;
2949
10497
      }
2950
10498
    }
2951
10499
    else if (right_item->eq(comp_item,1))
2952
10500
    {
2953
10501
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2954
10502
      {
2955
 
        if (*const_item)
2956
 
          return left_item->eq(*const_item, 1);
2957
 
        *const_item=left_item;
2958
 
        return 1;
 
10503
        if (*const_item)
 
10504
          return left_item->eq(*const_item, 1);
 
10505
        *const_item=left_item;
 
10506
        return 1;
2959
10507
      }
2960
10508
    }
2961
10509
  }
2962
10510
  return 0;
2963
10511
}
2964
10512
 
 
10513
 
2965
10514
/**
2966
10515
  @details
2967
10516
  Rows produced by a join sweep may end up in a temporary table or be sent
2973
10522
  @return
2974
10523
    end_select function to use. This function can't fail.
2975
10524
*/
2976
 
Next_select_func setup_end_select_func(Join *join)
 
10525
 
 
10526
Next_select_func setup_end_select_func(JOIN *join)
2977
10527
{
2978
10528
  Table *table= join->tmp_table;
2979
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
10529
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2980
10530
  Next_select_func end_select;
2981
10531
 
2982
10532
  /* Set up select_end */
2983
10533
  if (table)
2984
10534
  {
2985
 
    if (table->group && tmp_tbl->sum_func_count &&
 
10535
    if (table->group && tmp_tbl->sum_func_count && 
2986
10536
        !tmp_tbl->precomputed_group_by)
2987
10537
    {
2988
 
      if (table->getShare()->sizeKeys())
 
10538
      if (table->s->keys)
2989
10539
      {
2990
 
        end_select= end_update;
 
10540
        end_select=end_update;
2991
10541
      }
2992
10542
      else
2993
10543
      {
2994
 
        end_select= end_unique_update;
 
10544
        end_select=end_unique_update;
2995
10545
      }
2996
10546
    }
2997
10547
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
2998
10548
    {
2999
 
      end_select= end_write_group;
 
10549
      end_select=end_write_group;
3000
10550
    }
3001
10551
    else
3002
10552
    {
3003
 
      end_select= end_write;
 
10553
      end_select=end_write;
3004
10554
      if (tmp_tbl->precomputed_group_by)
3005
10555
      {
3006
10556
        /*
3007
10557
          A preceding call to create_tmp_table in the case when loose
3008
10558
          index scan is used guarantees that
3009
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
10559
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3010
10560
          by functions. It is OK here to use memcpy since we copy
3011
10561
          Item_sum pointers into an array of Item pointers.
3012
10562
        */
3028
10578
  return end_select;
3029
10579
}
3030
10580
 
 
10581
 
3031
10582
/**
3032
10583
  Make a join of all tables and write it on socket or to table.
3033
10584
 
3038
10589
  @retval
3039
10590
    -1  if error should be sent
3040
10591
*/
3041
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10592
 
 
10593
static int
 
10594
do_select(JOIN *join,List<Item> *fields,Table *table)
3042
10595
{
3043
10596
  int rc= 0;
3044
10597
  enum_nested_loop_state error= NESTED_LOOP_OK;
3045
 
  JoinTable *join_tab= NULL;
3046
 
 
 
10598
  JOIN_TAB *join_tab= NULL;
 
10599
  
3047
10600
  join->tmp_table= table;                       /* Save for easy recursion */
3048
10601
  join->fields= fields;
3049
10602
 
3050
10603
  if (table)
3051
10604
  {
3052
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3053
 
    table->emptyRecord();
 
10605
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
 
10606
    empty_record(table);
3054
10607
    if (table->group && join->tmp_table_param.sum_func_count &&
3055
 
        table->getShare()->sizeKeys() && !table->cursor->inited)
3056
 
      table->cursor->startIndexScan(0, 0);
 
10608
        table->s->keys && !table->file->inited)
 
10609
      table->file->ha_index_init(0, 0);
3057
10610
  }
3058
10611
  /* Set up select_end */
3059
10612
  Next_select_func end_select= setup_end_select_func(join);
3074
10627
    {
3075
10628
      error= (*end_select)(join, 0, 0);
3076
10629
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3077
 
              error= (*end_select)(join, 0, 1);
 
10630
        error= (*end_select)(join, 0, 1);
3078
10631
 
3079
10632
      /*
3080
10633
        If we don't go through evaluate_join_record(), do the counting
3082
10635
        so we don't touch it here.
3083
10636
      */
3084
10637
      join->examined_rows++;
3085
 
      join->session->row_count++;
 
10638
      join->thd->row_count++;
3086
10639
      assert(join->examined_rows <= 1);
3087
10640
    }
3088
10641
    else if (join->send_row_on_empty_set())
3112
10665
    if (!table)                                 // If sending data to client
3113
10666
    {
3114
10667
      /*
3115
 
        The following will unlock all cursors if the command wasn't an
3116
 
        update command
 
10668
        The following will unlock all cursors if the command wasn't an
 
10669
        update command
3117
10670
      */
3118
10671
      join->join_free();                        // Unlock all cursors
3119
10672
      if (join->result->send_eof())
3120
 
        rc= 1;                                  // Don't send error
 
10673
        rc= 1;                                  // Don't send error
3121
10674
    }
3122
10675
  }
3123
10676
  else
3125
10678
  if (table)
3126
10679
  {
3127
10680
    int tmp, new_errno= 0;
3128
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10681
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3129
10682
    {
3130
10683
      new_errno= tmp;
3131
10684
    }
3132
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10685
    if ((tmp=table->file->ha_index_or_rnd_end()))
3133
10686
    {
3134
10687
      new_errno= tmp;
3135
10688
    }
3136
10689
    if (new_errno)
3137
 
      table->print_error(new_errno,MYF(0));
 
10690
      table->file->print_error(new_errno,MYF(0));
3138
10691
  }
3139
 
  return(join->session->is_error() ? -1 : rc);
 
10692
  return(join->thd->is_error() ? -1 : rc);
3140
10693
}
3141
10694
 
3142
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10695
 
 
10696
enum_nested_loop_state
 
10697
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3143
10698
{
3144
10699
  enum_nested_loop_state rc;
3145
10700
 
3150
10705
      rc= sub_select(join,join_tab,end_of_records);
3151
10706
    return rc;
3152
10707
  }
3153
 
  if (join->session->getKilled())               // If aborted by user
 
10708
  if (join->thd->killed)                // If aborted by user
3154
10709
  {
3155
 
    join->session->send_kill_message();
3156
 
    return NESTED_LOOP_KILLED;
 
10710
    join->thd->send_kill_message();
 
10711
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3157
10712
  }
3158
10713
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3159
10714
  {
3160
 
    if (! join_tab->cache.store_record_in_cache())
 
10715
    if (!store_record_in_cache(&join_tab->cache))
3161
10716
      return NESTED_LOOP_OK;                     // There is more room in cache
3162
10717
    return flush_cached_records(join,join_tab,false);
3163
10718
  }
3170
10725
/**
3171
10726
  Retrieve records ends with a given beginning from the result of a join.
3172
10727
 
3173
 
    For a given partial join record consisting of records from the tables
 
10728
    For a given partial join record consisting of records from the tables 
3174
10729
    preceding the table join_tab in the execution plan, the function
3175
10730
    retrieves all matching full records from the result set and
3176
 
    send them to the result set stream.
 
10731
    send them to the result set stream. 
3177
10732
 
3178
10733
  @note
3179
10734
    The function effectively implements the  final (n-k) nested loops
3213
10768
    first row with t3.a=t1.a has been encountered.
3214
10769
    Thus, the second predicate P2 is supplied with a guarded value that are
3215
10770
    stored in the field 'found' of the first inner table for the outer join
3216
 
    (table t2). When the first row with t3.a=t1.a for the  current row
 
10771
    (table t2). When the first row with t3.a=t1.a for the  current row 
3217
10772
    of table t1  appears, the value becomes true. For now on the predicate
3218
10773
    is evaluated immediately after the row of table t2 has been read.
3219
10774
    When the first row with t3.a=t1.a has been encountered all
3221
10776
    Only when all of them are true the row is sent to the output stream.
3222
10777
    If not, the function returns to the lowest nest level that has a false
3223
10778
    attached condition.
3224
 
    The predicates from on expressions are also pushed down. If in the
 
10779
    The predicates from on expressions are also pushed down. If in the 
3225
10780
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3226
10781
    then t1.a=t2.a would be pushed down to table t2, and without any
3227
10782
    guard.
3231
10786
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3232
10787
    are checked for the composed row almost in the same way as it had
3233
10788
    been done for the first row with a match. The only difference is
3234
 
    the predicates from on expressions are not checked.
 
10789
    the predicates from on expressions are not checked. 
3235
10790
 
3236
10791
  @par
3237
10792
  @b IMPLEMENTATION
3247
10802
    and a pointer to a guarding boolean variable.
3248
10803
    When the value of the guard variable is true the value of the object
3249
10804
    is the same as the value of the predicate, otherwise it's just returns
3250
 
    true.
3251
 
    To carry out a return to a nested loop level of join table t the pointer
 
10805
    true. 
 
10806
    To carry out a return to a nested loop level of join table t the pointer 
3252
10807
    to t is remembered in the field 'return_tab' of the join structure.
3253
10808
    Consider the following query:
3254
10809
    @code
3265
10820
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3266
10821
    becomes 'activated', as well the predicate t4.a=t2.a. But
3267
10822
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3268
 
    t4.a=t2.a becomes true.
 
10823
    t4.a=t2.a becomes true. 
3269
10824
    In order not to re-evaluate the predicates that were already evaluated
3270
10825
    as attached pushed down predicates, a pointer to the the first
3271
10826
    most inner unmatched table is maintained in join_tab->first_unmatched.
3272
10827
    Thus, when the first row from t5 with t5.a=t3.a is found
3273
 
    this pointer for t5 is changed from t4 to t2.
 
10828
    this pointer for t5 is changed from t4 to t2.             
3274
10829
 
3275
10830
    @par
3276
10831
    @b STRUCTURE @b NOTES
3281
10836
  @param join      pointer to the structure providing all context info for
3282
10837
                   the query
3283
10838
  @param join_tab  the first next table of the execution plan to be retrieved
3284
 
  @param end_records  true when we need to perform final steps of retrival
 
10839
  @param end_records  true when we need to perform final steps of retrival   
3285
10840
 
3286
10841
  @return
3287
10842
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3288
10843
*/
3289
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10844
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10845
 
 
10846
enum_nested_loop_state
 
10847
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3290
10848
{
3291
10849
  join_tab->table->null_row=0;
3292
10850
  if (end_of_records)
3294
10852
 
3295
10853
  int error;
3296
10854
  enum_nested_loop_state rc;
3297
 
  ReadRecord *info= &join_tab->read_record;
 
10855
  READ_RECORD *info= &join_tab->read_record;
 
10856
 
 
10857
  if (join_tab->flush_weedout_table)
 
10858
  {
 
10859
    do_sj_reset(join_tab->flush_weedout_table);
 
10860
  }
3298
10861
 
3299
10862
  if (join->resume_nested_loop)
3300
10863
  {
3322
10885
      /* Set first_unmatched for the last inner table of this group */
3323
10886
      join_tab->last_inner->first_unmatched= join_tab;
3324
10887
    }
3325
 
    join->session->row_count= 0;
 
10888
    join->thd->row_count= 0;
3326
10889
 
3327
10890
    error= (*join_tab->read_first_record)(join_tab);
3328
10891
    rc= evaluate_join_record(join, join_tab, error);
3329
10892
  }
3330
 
 
3331
 
  /*
3332
 
    Note: psergey has added the 2nd part of the following condition; the
 
10893
  
 
10894
  /* 
 
10895
    Note: psergey has added the 2nd part of the following condition; the 
3333
10896
    change should probably be made in 5.1, too.
3334
10897
  */
3335
10898
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3347
10910
  return rc;
3348
10911
}
3349
10912
 
3350
 
int safe_index_read(JoinTable *tab)
 
10913
 
 
10914
 
 
10915
 
 
10916
/*
 
10917
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10918
 
 
10919
  SYNPOSIS
 
10920
    do_sj_dups_weedout()
 
10921
      
 
10922
  RETURN
 
10923
    -1  Error
 
10924
    1   The row combination is a duplicate (discard it)
 
10925
    0   The row combination is not a duplicate (continue)
 
10926
*/
 
10927
 
 
10928
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
10929
{
 
10930
  int error;
 
10931
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10932
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10933
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
10934
  uchar *nulls_ptr= ptr;
 
10935
  
 
10936
  /* Put the the rowids tuple into table->record[0]: */
 
10937
 
 
10938
  // 1. Store the length 
 
10939
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10940
  {
 
10941
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10942
    ptr++;
 
10943
  }
 
10944
  else
 
10945
  {
 
10946
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10947
    ptr += 2;
 
10948
  }
 
10949
 
 
10950
  // 2. Zero the null bytes 
 
10951
  if (sjtbl->null_bytes)
 
10952
  {
 
10953
    memset(ptr, 0, sjtbl->null_bytes);
 
10954
    ptr += sjtbl->null_bytes; 
 
10955
  }
 
10956
 
 
10957
  // 3. Put the rowids
 
10958
  for (uint i=0; tab != tab_end; tab++, i++)
 
10959
  {
 
10960
    handler *h= tab->join_tab->table->file;
 
10961
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10962
    {
 
10963
      /* It's a NULL-complemented row */
 
10964
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10965
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10966
    }
 
10967
    else
 
10968
    {
 
10969
      /* Copy the rowid value */
 
10970
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10971
        h->position(tab->join_tab->table->record[0]);
 
10972
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10973
    }
 
10974
  }
 
10975
 
 
10976
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10977
  if (error)
 
10978
  {
 
10979
    /* create_myisam_from_heap will generate error if needed */
 
10980
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
10981
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
10982
                                &sjtbl->recinfo, error, 1))
 
10983
      return -1;
 
10984
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
10985
    return 1;
 
10986
  }
 
10987
  return 0;
 
10988
}
 
10989
 
 
10990
 
 
10991
/*
 
10992
  SemiJoinDuplicateElimination: Reset the temporary table
 
10993
*/
 
10994
 
 
10995
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
10996
{
 
10997
  if (sj_tbl->tmp_table)
 
10998
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
10999
  return 0;
 
11000
}
 
11001
 
 
11002
/*
 
11003
  Process one record of the nested loop join.
 
11004
 
 
11005
    This function will evaluate parts of WHERE/ON clauses that are
 
11006
    applicable to the partial record on hand and in case of success
 
11007
    submit this record to the next level of the nested loop.
 
11008
*/
 
11009
 
 
11010
static enum_nested_loop_state
 
11011
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11012
                     int error)
 
11013
{
 
11014
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11015
  ha_rows found_records=join->found_records;
 
11016
  COND *select_cond= join_tab->select_cond;
 
11017
 
 
11018
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
11019
    return NESTED_LOOP_ERROR;
 
11020
  if (error < 0)
 
11021
    return NESTED_LOOP_NO_MORE_ROWS;
 
11022
  if (join->thd->killed)                        // Aborted by user
 
11023
  {
 
11024
    join->thd->send_kill_message();
 
11025
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11026
  }
 
11027
  if (!select_cond || select_cond->val_int())
 
11028
  {
 
11029
    /*
 
11030
      There is no select condition or the attached pushed down
 
11031
      condition is true => a match is found.
 
11032
    */
 
11033
    bool found= 1;
 
11034
    while (join_tab->first_unmatched && found)
 
11035
    {
 
11036
      /*
 
11037
        The while condition is always false if join_tab is not
 
11038
        the last inner join table of an outer join operation.
 
11039
      */
 
11040
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11041
      /*
 
11042
        Mark that a match for current outer table is found.
 
11043
        This activates push down conditional predicates attached
 
11044
        to the all inner tables of the outer join.
 
11045
      */
 
11046
      first_unmatched->found= 1;
 
11047
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11048
      {
 
11049
        if (tab->table->reginfo.not_exists_optimize)
 
11050
          return NESTED_LOOP_NO_MORE_ROWS;
 
11051
        /* Check all predicates that has just been activated. */
 
11052
        /*
 
11053
          Actually all predicates non-guarded by first_unmatched->found
 
11054
          will be re-evaluated again. It could be fixed, but, probably,
 
11055
          it's not worth doing now.
 
11056
        */
 
11057
        if (tab->select_cond && !tab->select_cond->val_int())
 
11058
        {
 
11059
          /* The condition attached to table tab is false */
 
11060
          if (tab == join_tab)
 
11061
            found= 0;
 
11062
          else
 
11063
          {
 
11064
            /*
 
11065
              Set a return point if rejected predicate is attached
 
11066
              not to the last table of the current nest level.
 
11067
            */
 
11068
            join->return_tab= tab;
 
11069
            return NESTED_LOOP_OK;
 
11070
          }
 
11071
        }
 
11072
      }
 
11073
      /*
 
11074
        Check whether join_tab is not the last inner table
 
11075
        for another embedding outer join.
 
11076
      */
 
11077
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11078
          first_unmatched->last_inner != join_tab)
 
11079
        first_unmatched= 0;
 
11080
      join_tab->first_unmatched= first_unmatched;
 
11081
    }
 
11082
 
 
11083
    JOIN_TAB *return_tab= join->return_tab;
 
11084
    join_tab->found_match= true;
 
11085
    if (join_tab->check_weed_out_table)
 
11086
    {
 
11087
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
11088
      if (res == -1)
 
11089
        return NESTED_LOOP_ERROR;
 
11090
      if (res == 1)
 
11091
        return NESTED_LOOP_OK;
 
11092
    }
 
11093
    else if (join_tab->do_firstmatch)
 
11094
    {
 
11095
      /* 
 
11096
        We should return to the join_tab->do_firstmatch after we have 
 
11097
        enumerated all the suffixes for current prefix row combination
 
11098
      */
 
11099
      return_tab= join_tab->do_firstmatch;
 
11100
    }
 
11101
 
 
11102
    /*
 
11103
      It was not just a return to lower loop level when one
 
11104
      of the newly activated predicates is evaluated as false
 
11105
      (See above join->return_tab= tab).
 
11106
    */
 
11107
    join->examined_rows++;
 
11108
    join->thd->row_count++;
 
11109
 
 
11110
    if (found)
 
11111
    {
 
11112
      enum enum_nested_loop_state rc;
 
11113
      /* A match from join_tab is found for the current partial join. */
 
11114
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11115
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11116
        return rc;
 
11117
      if (return_tab < join->return_tab)
 
11118
        join->return_tab= return_tab;
 
11119
 
 
11120
      if (join->return_tab < join_tab)
 
11121
        return NESTED_LOOP_OK;
 
11122
      /*
 
11123
        Test if this was a SELECT DISTINCT query on a table that
 
11124
        was not in the field list;  In this case we can abort if
 
11125
        we found a row, as no new rows can be added to the result.
 
11126
      */
 
11127
      if (not_used_in_distinct && found_records != join->found_records)
 
11128
        return NESTED_LOOP_NO_MORE_ROWS;
 
11129
    }
 
11130
    else
 
11131
      join_tab->read_record.file->unlock_row();
 
11132
  }
 
11133
  else
 
11134
  {
 
11135
    /*
 
11136
      The condition pushed down to the table join_tab rejects all rows
 
11137
      with the beginning coinciding with the current partial join.
 
11138
    */
 
11139
    join->examined_rows++;
 
11140
    join->thd->row_count++;
 
11141
    join_tab->read_record.file->unlock_row();
 
11142
  }
 
11143
  return NESTED_LOOP_OK;
 
11144
}
 
11145
 
 
11146
 
 
11147
/**
 
11148
 
 
11149
  @details
 
11150
    Construct a NULL complimented partial join record and feed it to the next
 
11151
    level of the nested loop. This function is used in case we have
 
11152
    an OUTER join and no matching record was found.
 
11153
*/
 
11154
 
 
11155
static enum_nested_loop_state
 
11156
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11157
{
 
11158
  /*
 
11159
    The table join_tab is the first inner table of a outer join operation
 
11160
    and no matches has been found for the current outer row.
 
11161
  */
 
11162
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11163
  /* Cache variables for faster loop */
 
11164
  COND *select_cond;
 
11165
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11166
  {
 
11167
    /* Change the the values of guard predicate variables. */
 
11168
    join_tab->found= 1;
 
11169
    join_tab->not_null_compl= 0;
 
11170
    /* The outer row is complemented by nulls for each inner tables */
 
11171
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11172
    mark_as_null_row(join_tab->table);       // For group by without error
 
11173
    select_cond= join_tab->select_cond;
 
11174
    /* Check all attached conditions for inner table rows. */
 
11175
    if (select_cond && !select_cond->val_int())
 
11176
      return NESTED_LOOP_OK;
 
11177
  }
 
11178
  join_tab--;
 
11179
  /*
 
11180
    The row complemented by nulls might be the first row
 
11181
    of embedding outer joins.
 
11182
    If so, perform the same actions as in the code
 
11183
    for the first regular outer join row above.
 
11184
  */
 
11185
  for ( ; ; )
 
11186
  {
 
11187
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11188
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11189
        first_unmatched->last_inner != join_tab)
 
11190
      first_unmatched= 0;
 
11191
    join_tab->first_unmatched= first_unmatched;
 
11192
    if (!first_unmatched)
 
11193
      break;
 
11194
    first_unmatched->found= 1;
 
11195
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11196
    {
 
11197
      if (tab->select_cond && !tab->select_cond->val_int())
 
11198
      {
 
11199
        join->return_tab= tab;
 
11200
        return NESTED_LOOP_OK;
 
11201
      }
 
11202
    }
 
11203
  }
 
11204
  /*
 
11205
    The row complemented by nulls satisfies all conditions
 
11206
    attached to inner tables.
 
11207
    Send the row complemented by nulls to be joined with the
 
11208
    remaining tables.
 
11209
  */
 
11210
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11211
}
 
11212
 
 
11213
 
 
11214
static enum_nested_loop_state
 
11215
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11216
{
 
11217
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11218
  int error;
 
11219
  READ_RECORD *info;
 
11220
 
 
11221
  join_tab->table->null_row= 0;
 
11222
  if (!join_tab->cache.records)
 
11223
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11224
  if (skip_last)
 
11225
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11226
  if (join_tab->use_quick == 2)
 
11227
  {
 
11228
    if (join_tab->select->quick)
 
11229
    {                                   /* Used quick select last. reset it */
 
11230
      delete join_tab->select->quick;
 
11231
      join_tab->select->quick=0;
 
11232
    }
 
11233
  }
 
11234
 /* read through all records */
 
11235
  if ((error=join_init_read_record(join_tab)))
 
11236
  {
 
11237
    reset_cache_write(&join_tab->cache);
 
11238
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11239
  }
 
11240
 
 
11241
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11242
  {
 
11243
    tmp->status=tmp->table->status;
 
11244
    tmp->table->status=0;
 
11245
  }
 
11246
 
 
11247
  info= &join_tab->read_record;
 
11248
  do
 
11249
  {
 
11250
    if (join->thd->killed)
 
11251
    {
 
11252
      join->thd->send_kill_message();
 
11253
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11254
    }
 
11255
    SQL_SELECT *select=join_tab->select;
 
11256
    if (rc == NESTED_LOOP_OK &&
 
11257
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11258
    {
 
11259
      uint i;
 
11260
      reset_cache_read(&join_tab->cache);
 
11261
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11262
      {
 
11263
        read_cached_record(join_tab);
 
11264
        if (!select || !select->skip_record())
 
11265
        {
 
11266
          int res= 0;
 
11267
          if (!join_tab->check_weed_out_table || 
 
11268
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
11269
          {
 
11270
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11271
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11272
            {
 
11273
              reset_cache_write(&join_tab->cache);
 
11274
              return rc;
 
11275
            }
 
11276
          }
 
11277
          if (res == -1)
 
11278
            return NESTED_LOOP_ERROR;
 
11279
        }
 
11280
      }
 
11281
    }
 
11282
  } while (!(error=info->read_record(info)));
 
11283
 
 
11284
  if (skip_last)
 
11285
    read_cached_record(join_tab);               // Restore current record
 
11286
  reset_cache_write(&join_tab->cache);
 
11287
  if (error > 0)                                // Fatal error
 
11288
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11289
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11290
    tmp2->table->status=tmp2->status;
 
11291
  return NESTED_LOOP_OK;
 
11292
}
 
11293
 
 
11294
int safe_index_read(JOIN_TAB *tab)
3351
11295
{
3352
11296
  int error;
3353
11297
  Table *table= tab->table;
3354
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11298
  if ((error=table->file->index_read_map(table->record[0],
3355
11299
                                         tab->ref.key_buff,
3356
11300
                                         make_prev_keypart_map(tab->ref.key_parts),
3357
11301
                                         HA_READ_KEY_EXACT)))
3359
11303
  return 0;
3360
11304
}
3361
11305
 
3362
 
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
11306
 
 
11307
static int
 
11308
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3363
11309
{
3364
11310
  int error;
3365
11311
  Table *table=tab->table;
3366
11312
  table->const_table=1;
3367
11313
  table->null_row=0;
3368
11314
  table->status=STATUS_NO_RECORD;
3369
 
 
3370
 
  if (tab->type == AM_SYSTEM)
 
11315
  
 
11316
  if (tab->type == JT_SYSTEM)
3371
11317
  {
3372
11318
    if ((error=join_read_system(tab)))
3373
11319
    {                                           // Info for DESCRIBE
3374
11320
      tab->info="const row not found";
3375
11321
      /* Mark for EXPLAIN that the row was not found */
3376
 
      pos->setFanout(0.0);
3377
 
      pos->clearRefDependMap();
3378
 
      if (! table->maybe_null || error > 0)
3379
 
        return(error);
 
11322
      pos->records_read=0.0;
 
11323
      pos->ref_depend_map= 0;
 
11324
      if (!table->maybe_null || error > 0)
 
11325
        return(error);
3380
11326
    }
3381
11327
  }
3382
11328
  else
3383
11329
  {
3384
 
    if (! table->key_read && 
3385
 
        table->covering_keys.test(tab->ref.key) && 
3386
 
        ! table->no_keyread &&
3387
 
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
11330
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11331
        !table->no_keyread &&
 
11332
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3388
11333
    {
3389
11334
      table->key_read=1;
3390
 
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
11335
      table->file->extra(HA_EXTRA_KEYREAD);
3391
11336
      tab->index= tab->ref.key;
3392
11337
    }
3393
11338
    error=join_read_const(tab);
3394
11339
    if (table->key_read)
3395
11340
    {
3396
11341
      table->key_read=0;
3397
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
11342
      table->file->extra(HA_EXTRA_NO_KEYREAD);
3398
11343
    }
3399
11344
    if (error)
3400
11345
    {
3401
11346
      tab->info="unique row not found";
3402
11347
      /* Mark for EXPLAIN that the row was not found */
3403
 
      pos->setFanout(0.0);
3404
 
      pos->clearRefDependMap();
 
11348
      pos->records_read=0.0;
 
11349
      pos->ref_depend_map= 0;
3405
11350
      if (!table->maybe_null || error > 0)
3406
 
        return(error);
 
11351
        return(error);
3407
11352
    }
3408
11353
  }
3409
11354
  if (*tab->on_expr_ref && !table->null_row)
3410
11355
  {
3411
11356
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3412
 
      table->mark_as_null_row();
 
11357
      mark_as_null_row(table);  
3413
11358
  }
3414
11359
  if (!table->null_row)
3415
11360
    table->maybe_null=0;
3416
11361
 
3417
11362
  /* Check appearance of new constant items in Item_equal objects */
3418
 
  Join *join= tab->join;
 
11363
  JOIN *join= tab->join;
3419
11364
  if (join->conds)
3420
11365
    update_const_equal_items(join->conds, tab);
3421
11366
  TableList *tbl;
3428
11373
      embedded= embedding;
3429
11374
      if (embedded->on_expr)
3430
11375
         update_const_equal_items(embedded->on_expr, tab);
3431
 
      embedding= embedded->getEmbedding();
 
11376
      embedding= embedded->embedding;
3432
11377
    }
3433
11378
    while (embedding &&
3434
 
           embedding->getNestedJoin()->join_list.head() == embedded);
 
11379
           embedding->nested_join->join_list.head() == embedded);
3435
11380
  }
3436
11381
 
3437
11382
  return(0);
3438
11383
}
3439
11384
 
3440
 
int join_read_system(JoinTable *tab)
 
11385
 
 
11386
static int
 
11387
join_read_system(JOIN_TAB *tab)
3441
11388
{
3442
11389
  Table *table= tab->table;
3443
11390
  int error;
3444
11391
  if (table->status & STATUS_GARBAGE)           // If first read
3445
11392
  {
3446
 
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3447
 
                                           table->getShare()->getPrimaryKey())))
 
11393
    if ((error=table->file->read_first_row(table->record[0],
 
11394
                                           table->s->primary_key)))
3448
11395
    {
3449
11396
      if (error != HA_ERR_END_OF_FILE)
3450
 
        return table->report_error(error);
3451
 
      tab->table->mark_as_null_row();
3452
 
      table->emptyRecord();                     // Make empty record
 
11397
        return table->report_error(error);
 
11398
      mark_as_null_row(tab->table);
 
11399
      empty_record(table);                      // Make empty record
3453
11400
      return -1;
3454
11401
    }
3455
 
    table->storeRecord();
 
11402
    store_record(table,record[1]);
3456
11403
  }
3457
11404
  else if (!table->status)                      // Only happens with left join
3458
 
    table->restoreRecord();                     // restore old record
 
11405
    restore_record(table,record[1]);                    // restore old record
3459
11406
  table->null_row=0;
3460
11407
  return table->status ? -1 : 0;
3461
11408
}
3462
11409
 
 
11410
 
3463
11411
/**
3464
11412
  Read a (constant) table when there is at most one matching row.
3465
11413
 
3472
11420
  @retval
3473
11421
    1   Got an error (other than row not found) during read
3474
11422
*/
3475
 
int join_read_const(JoinTable *tab)
 
11423
 
 
11424
static int
 
11425
join_read_const(JOIN_TAB *tab)
3476
11426
{
3477
11427
  int error;
3478
11428
  Table *table= tab->table;
3479
11429
  if (table->status & STATUS_GARBAGE)           // If first read
3480
11430
  {
3481
11431
    table->status= 0;
3482
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3483
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11432
    if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
11433
      error=HA_ERR_KEY_NOT_FOUND;
3484
11434
    else
3485
11435
    {
3486
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3487
 
                                            (unsigned char*) tab->ref.key_buff,
 
11436
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
11437
                                            (uchar*) tab->ref.key_buff,
3488
11438
                                            make_prev_keypart_map(tab->ref.key_parts),
3489
11439
                                            HA_READ_KEY_EXACT);
3490
11440
    }
3491
11441
    if (error)
3492
11442
    {
3493
11443
      table->status= STATUS_NOT_FOUND;
3494
 
      tab->table->mark_as_null_row();
3495
 
      table->emptyRecord();
 
11444
      mark_as_null_row(tab->table);
 
11445
      empty_record(table);
3496
11446
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3497
 
        return table->report_error(error);
 
11447
        return table->report_error(error);
3498
11448
      return -1;
3499
11449
    }
3500
 
    table->storeRecord();
 
11450
    store_record(table,record[1]);
3501
11451
  }
3502
11452
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3503
11453
  {
3504
11454
    table->status=0;
3505
 
    table->restoreRecord();                     // restore old record
 
11455
    restore_record(table,record[1]);                    // restore old record
3506
11456
  }
3507
11457
  table->null_row=0;
3508
11458
  return table->status ? -1 : 0;
3509
11459
}
3510
11460
 
 
11461
 
3511
11462
/*
3512
11463
  eq_ref access method implementation: "read_first" function
3513
11464
 
3514
11465
  SYNOPSIS
3515
11466
    join_read_key()
3516
 
      tab  JoinTable of the accessed table
 
11467
      tab  JOIN_TAB of the accessed table
3517
11468
 
3518
11469
  DESCRIPTION
3519
11470
    This is "read_fist" function for the "ref" access method. The difference
3521
11472
 
3522
11473
  RETURN
3523
11474
    0  - Ok
3524
 
   -1  - Row not found
 
11475
   -1  - Row not found 
3525
11476
    1  - Error
3526
11477
*/
3527
 
int join_read_key(JoinTable *tab)
 
11478
 
 
11479
static int
 
11480
join_read_key(JOIN_TAB *tab)
3528
11481
{
3529
11482
  int error;
3530
11483
  Table *table= tab->table;
3531
11484
 
3532
 
  if (!table->cursor->inited)
 
11485
  if (!table->file->inited)
3533
11486
  {
3534
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
11487
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3535
11488
  }
3536
11489
 
3537
11490
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3543
11496
      table->status=STATUS_NOT_FOUND;
3544
11497
      return -1;
3545
11498
    }
3546
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11499
    error=table->file->index_read_map(table->record[0],
3547
11500
                                      tab->ref.key_buff,
3548
11501
                                      make_prev_keypart_map(tab->ref.key_parts),
3549
11502
                                      HA_READ_KEY_EXACT);
3554
11507
  return table->status ? -1 : 0;
3555
11508
}
3556
11509
 
 
11510
 
3557
11511
/*
3558
11512
  ref access method implementation: "read_first" function
3559
11513
 
3560
11514
  SYNOPSIS
3561
11515
    join_read_always_key()
3562
 
      tab  JoinTable of the accessed table
 
11516
      tab  JOIN_TAB of the accessed table
3563
11517
 
3564
11518
  DESCRIPTION
3565
 
    This is "read_first" function for the "ref" access method.
3566
 
 
 
11519
    This is "read_fist" function for the "ref" access method.
 
11520
   
3567
11521
    The functon must leave the index initialized when it returns.
3568
11522
    ref_or_null access implementation depends on that.
3569
11523
 
3570
11524
  RETURN
3571
11525
    0  - Ok
3572
 
   -1  - Row not found
 
11526
   -1  - Row not found 
3573
11527
    1  - Error
3574
11528
*/
3575
 
int join_read_always_key(JoinTable *tab)
 
11529
 
 
11530
static int
 
11531
join_read_always_key(JOIN_TAB *tab)
3576
11532
{
3577
11533
  int error;
3578
11534
  Table *table= tab->table;
3579
11535
 
3580
11536
  /* Initialize the index first */
3581
 
  if (!table->cursor->inited)
3582
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3583
 
 
 
11537
  if (!table->file->inited)
 
11538
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11539
 
3584
11540
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3585
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
11541
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
3586
11542
  {
3587
11543
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3588
11544
        return -1;
3589
11545
  }
3590
11546
 
3591
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11547
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3592
11548
    return -1;
3593
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11549
  if ((error=table->file->index_read_map(table->record[0],
3594
11550
                                         tab->ref.key_buff,
3595
11551
                                         make_prev_keypart_map(tab->ref.key_parts),
3596
11552
                                         HA_READ_KEY_EXACT)))
3597
11553
  {
3598
11554
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3599
11555
      return table->report_error(error);
3600
 
    return -1;
 
11556
    return -1; /* purecov: inspected */
3601
11557
  }
3602
 
 
3603
11558
  return 0;
3604
11559
}
3605
11560
 
 
11561
 
3606
11562
/**
3607
 
  This function is used when optimizing away ORDER BY in
3608
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11563
  This function is used when optimizing away order_st BY in 
 
11564
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3609
11565
*/
3610
 
int join_read_last_key(JoinTable *tab)
 
11566
  
 
11567
static int
 
11568
join_read_last_key(JOIN_TAB *tab)
3611
11569
{
3612
11570
  int error;
3613
11571
  Table *table= tab->table;
3614
11572
 
3615
 
  if (!table->cursor->inited)
3616
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3617
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11573
  if (!table->file->inited)
 
11574
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11575
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3618
11576
    return -1;
3619
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11577
  if ((error=table->file->index_read_last_map(table->record[0],
3620
11578
                                              tab->ref.key_buff,
3621
11579
                                              make_prev_keypart_map(tab->ref.key_parts))))
3622
11580
  {
3623
11581
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3624
11582
      return table->report_error(error);
3625
 
    return -1;
 
11583
    return -1; /* purecov: inspected */
3626
11584
  }
3627
11585
  return 0;
3628
11586
}
3629
11587
 
3630
 
int join_no_more_records(ReadRecord *)
 
11588
 
 
11589
        /* ARGSUSED */
 
11590
static int
 
11591
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3631
11592
{
3632
11593
  return -1;
3633
11594
}
3634
11595
 
3635
 
int join_read_next_same_diff(ReadRecord *info)
 
11596
static int
 
11597
join_read_next_same_diff(READ_RECORD *info)
3636
11598
{
3637
11599
  Table *table= info->table;
3638
 
  JoinTable *tab=table->reginfo.join_tab;
 
11600
  JOIN_TAB *tab=table->reginfo.join_tab;
3639
11601
  if (tab->insideout_match_tab->found_match)
3640
11602
  {
3641
 
    KeyInfo *key= tab->table->key_info + tab->index;
3642
 
    do
 
11603
    KEY *key= tab->table->key_info + tab->index;
 
11604
    do 
3643
11605
    {
3644
11606
      int error;
3645
11607
      /* Save index tuple from record to the buffer */
3646
11608
      key_copy(tab->insideout_buf, info->record, key, 0);
3647
11609
 
3648
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11610
      if ((error=table->file->index_next_same(table->record[0],
3649
11611
                                              tab->ref.key_buff,
3650
11612
                                              tab->ref.key_length)))
3651
11613
      {
3654
11616
        table->status= STATUS_GARBAGE;
3655
11617
        return -1;
3656
11618
      }
3657
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11619
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3658
11620
                      tab->insideout_buf, key->key_length));
3659
11621
    tab->insideout_match_tab->found_match= 0;
3660
11622
    return 0;
3663
11625
    return join_read_next_same(info);
3664
11626
}
3665
11627
 
3666
 
int join_read_next_same(ReadRecord *info)
 
11628
static int
 
11629
join_read_next_same(READ_RECORD *info)
3667
11630
{
3668
11631
  int error;
3669
11632
  Table *table= info->table;
3670
 
  JoinTable *tab=table->reginfo.join_tab;
 
11633
  JOIN_TAB *tab=table->reginfo.join_tab;
3671
11634
 
3672
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11635
  if ((error=table->file->index_next_same(table->record[0],
3673
11636
                                          tab->ref.key_buff,
3674
11637
                                          tab->ref.key_length)))
3675
11638
  {
3678
11641
    table->status= STATUS_GARBAGE;
3679
11642
    return -1;
3680
11643
  }
3681
 
 
3682
11644
  return 0;
3683
11645
}
3684
11646
 
3685
 
int join_read_prev_same(ReadRecord *info)
 
11647
 
 
11648
static int
 
11649
join_read_prev_same(READ_RECORD *info)
3686
11650
{
3687
11651
  int error;
3688
11652
  Table *table= info->table;
3689
 
  JoinTable *tab=table->reginfo.join_tab;
 
11653
  JOIN_TAB *tab=table->reginfo.join_tab;
3690
11654
 
3691
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11655
  if ((error=table->file->index_prev(table->record[0])))
3692
11656
    return table->report_error(error);
3693
11657
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3694
11658
                      tab->ref.key_length))
3699
11663
  return error;
3700
11664
}
3701
11665
 
3702
 
int join_init_quick_read_record(JoinTable *tab)
 
11666
 
 
11667
static int
 
11668
join_init_quick_read_record(JOIN_TAB *tab)
3703
11669
{
3704
11670
  if (test_if_quick_select(tab) == -1)
3705
11671
    return -1;                                  /* No possible records */
3706
11672
  return join_init_read_record(tab);
3707
11673
}
3708
11674
 
3709
 
int init_read_record_seq(JoinTable *tab)
 
11675
 
 
11676
int rr_sequential(READ_RECORD *info);
 
11677
int init_read_record_seq(JOIN_TAB *tab)
3710
11678
{
3711
 
  tab->read_record.init_reard_record_sequential();
3712
 
 
3713
 
  if (tab->read_record.cursor->startTableScan(1))
 
11679
  tab->read_record.read_record= rr_sequential;
 
11680
  if (tab->read_record.file->ha_rnd_init(1))
3714
11681
    return 1;
3715
11682
  return (*tab->read_record.read_record)(&tab->read_record);
3716
11683
}
3717
11684
 
3718
 
int test_if_quick_select(JoinTable *tab)
 
11685
static int
 
11686
test_if_quick_select(JOIN_TAB *tab)
3719
11687
{
3720
11688
  delete tab->select->quick;
3721
 
  tab->select->quick= 0;
3722
 
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3723
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11689
  tab->select->quick=0;
 
11690
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
11691
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11692
                                        false);
3724
11693
}
3725
11694
 
3726
 
int join_init_read_record(JoinTable *tab)
 
11695
 
 
11696
static int
 
11697
join_init_read_record(JOIN_TAB *tab)
3727
11698
{
3728
11699
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3729
11700
    return 1;
3730
 
 
3731
 
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
3732
 
 
 
11701
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
11702
                   tab->select,1,1);
3733
11703
  return (*tab->read_record.read_record)(&tab->read_record);
3734
11704
}
3735
11705
 
3736
 
int join_read_first(JoinTable *tab)
 
11706
 
 
11707
static int
 
11708
join_read_first(JOIN_TAB *tab)
3737
11709
{
3738
11710
  int error;
3739
11711
  Table *table=tab->table;
3740
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11712
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3741
11713
      !table->no_keyread)
3742
11714
  {
3743
 
    table->key_read= 1;
3744
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11715
    table->key_read=1;
 
11716
    table->file->extra(HA_EXTRA_KEYREAD);
3745
11717
  }
3746
 
  tab->table->status= 0;
 
11718
  tab->table->status=0;
3747
11719
  tab->read_record.table=table;
3748
 
  tab->read_record.cursor=table->cursor;
 
11720
  tab->read_record.file=table->file;
3749
11721
  tab->read_record.index=tab->index;
3750
 
  tab->read_record.record=table->getInsertRecord();
 
11722
  tab->read_record.record=table->record[0];
3751
11723
  if (tab->insideout_match_tab)
3752
11724
  {
3753
11725
    tab->read_record.do_insideout_scan= tab;
3760
11732
    tab->read_record.do_insideout_scan= 0;
3761
11733
  }
3762
11734
 
3763
 
  if (!table->cursor->inited)
3764
 
    table->cursor->startIndexScan(tab->index, tab->sorted);
3765
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
11735
  if (!table->file->inited)
 
11736
    table->file->ha_index_init(tab->index, tab->sorted);
 
11737
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3766
11738
  {
3767
11739
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3768
11740
      table->report_error(error);
3769
11741
    return -1;
3770
11742
  }
3771
 
 
3772
11743
  return 0;
3773
11744
}
3774
11745
 
3775
 
int join_read_next_different(ReadRecord *info)
 
11746
 
 
11747
static int
 
11748
join_read_next_different(READ_RECORD *info)
3776
11749
{
3777
 
  JoinTable *tab= info->do_insideout_scan;
 
11750
  JOIN_TAB *tab= info->do_insideout_scan;
3778
11751
  if (tab->insideout_match_tab->found_match)
3779
11752
  {
3780
 
    KeyInfo *key= tab->table->key_info + tab->index;
3781
 
    do
 
11753
    KEY *key= tab->table->key_info + tab->index;
 
11754
    do 
3782
11755
    {
3783
11756
      int error;
3784
11757
      /* Save index tuple from record to the buffer */
3785
11758
      key_copy(tab->insideout_buf, info->record, key, 0);
3786
11759
 
3787
 
      if ((error=info->cursor->index_next(info->record)))
 
11760
      if ((error=info->file->index_next(info->record)))
3788
11761
        return info->table->report_error(error);
3789
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11762
      
 
11763
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3790
11764
                      tab->insideout_buf, key->key_length));
3791
11765
    tab->insideout_match_tab->found_match= 0;
3792
11766
    return 0;
3795
11769
    return join_read_next(info);
3796
11770
}
3797
11771
 
3798
 
int join_read_next(ReadRecord *info)
 
11772
 
 
11773
static int
 
11774
join_read_next(READ_RECORD *info)
3799
11775
{
3800
11776
  int error;
3801
 
  if ((error=info->cursor->index_next(info->record)))
 
11777
  if ((error=info->file->index_next(info->record)))
3802
11778
    return info->table->report_error(error);
3803
11779
  return 0;
3804
11780
}
3805
11781
 
3806
 
int join_read_last(JoinTable *tab)
 
11782
 
 
11783
static int
 
11784
join_read_last(JOIN_TAB *tab)
3807
11785
{
3808
11786
  Table *table=tab->table;
3809
11787
  int error;
3810
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11788
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3811
11789
      !table->no_keyread)
3812
11790
  {
3813
11791
    table->key_read=1;
3814
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11792
    table->file->extra(HA_EXTRA_KEYREAD);
3815
11793
  }
3816
11794
  tab->table->status=0;
3817
11795
  tab->read_record.read_record=join_read_prev;
3818
11796
  tab->read_record.table=table;
3819
 
  tab->read_record.cursor=table->cursor;
 
11797
  tab->read_record.file=table->file;
3820
11798
  tab->read_record.index=tab->index;
3821
 
  tab->read_record.record=table->getInsertRecord();
3822
 
  if (!table->cursor->inited)
3823
 
    table->cursor->startIndexScan(tab->index, 1);
3824
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
 
11799
  tab->read_record.record=table->record[0];
 
11800
  if (!table->file->inited)
 
11801
    table->file->ha_index_init(tab->index, 1);
 
11802
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3825
11803
    return table->report_error(error);
3826
 
 
3827
11804
  return 0;
3828
11805
}
3829
11806
 
3830
 
int join_read_prev(ReadRecord *info)
 
11807
 
 
11808
static int
 
11809
join_read_prev(READ_RECORD *info)
3831
11810
{
3832
11811
  int error;
3833
 
  if ((error= info->cursor->index_prev(info->record)))
 
11812
  if ((error= info->file->index_prev(info->record)))
3834
11813
    return info->table->report_error(error);
3835
 
 
3836
11814
  return 0;
3837
11815
}
3838
11816
 
3839
11817
/**
3840
11818
  Reading of key with key reference and one part that may be NULL.
3841
11819
*/
3842
 
int join_read_always_key_or_null(JoinTable *tab)
 
11820
 
 
11821
int
 
11822
join_read_always_key_or_null(JOIN_TAB *tab)
3843
11823
{
3844
11824
  int res;
3845
11825
 
3853
11833
  return safe_index_read(tab);
3854
11834
}
3855
11835
 
3856
 
int join_read_next_same_or_null(ReadRecord *info)
 
11836
 
 
11837
int
 
11838
join_read_next_same_or_null(READ_RECORD *info)
3857
11839
{
3858
11840
  int error;
3859
11841
  if ((error= join_read_next_same(info)) >= 0)
3860
11842
    return error;
3861
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11843
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3862
11844
 
3863
11845
  /* Test if we have already done a read after null key */
3864
11846
  if (*tab->ref.null_ref_key)
3867
11849
  return safe_index_read(tab);                  // then read null keys
3868
11850
}
3869
11851
 
3870
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11852
 
 
11853
/*****************************************************************************
 
11854
  DESCRIPTION
 
11855
    Functions that end one nested loop iteration. Different functions
 
11856
    are used to support GROUP BY clause and to redirect records
 
11857
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11858
    network client.
 
11859
 
 
11860
  RETURN VALUES
 
11861
    NESTED_LOOP_OK           - the record has been successfully handled
 
11862
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11863
                               was detected
 
11864
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11865
                               the record
 
11866
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11867
                               additionally, the nested loop produced the
 
11868
                               number of rows specified in the LIMIT clause
 
11869
                               for the query
 
11870
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11871
                               additionally, there is a cursor and the nested
 
11872
                               loop algorithm produced the number of rows
 
11873
                               that is specified for current cursor fetch
 
11874
                               operation.
 
11875
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11876
*****************************************************************************/
 
11877
 
 
11878
/* ARGSUSED */
 
11879
static enum_nested_loop_state
 
11880
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11881
         bool end_of_records)
 
11882
{
 
11883
  if (!end_of_records)
 
11884
  {
 
11885
    int error;
 
11886
    if (join->having && join->having->val_int() == 0)
 
11887
      return(NESTED_LOOP_OK);               // Didn't match having
 
11888
    error=0;
 
11889
    if (join->do_send_rows)
 
11890
      error=join->result->send_data(*join->fields);
 
11891
    if (error)
 
11892
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11893
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11894
        join->do_send_rows)
 
11895
    {
 
11896
      if (join->select_options & OPTION_FOUND_ROWS)
 
11897
      {
 
11898
        JOIN_TAB *jt=join->join_tab;
 
11899
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11900
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11901
            !(jt->select && jt->select->quick) &&
 
11902
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11903
            (jt->ref.key < 0))
 
11904
        {
 
11905
          /* Join over all rows in table;  Return number of found rows */
 
11906
          Table *table=jt->table;
 
11907
 
 
11908
          join->select_options ^= OPTION_FOUND_ROWS;
 
11909
          if (table->sort.record_pointers ||
 
11910
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11911
          {
 
11912
            /* Using filesort */
 
11913
            join->send_records= table->sort.found_records;
 
11914
          }
 
11915
          else
 
11916
          {
 
11917
            table->file->info(HA_STATUS_VARIABLE);
 
11918
            join->send_records= table->file->stats.records;
 
11919
          }
 
11920
        }
 
11921
        else 
 
11922
        {
 
11923
          join->do_send_rows= 0;
 
11924
          if (join->unit->fake_select_lex)
 
11925
            join->unit->fake_select_lex->select_limit= 0;
 
11926
          return(NESTED_LOOP_OK);
 
11927
        }
 
11928
      }
 
11929
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11930
    }
 
11931
    else if (join->send_records >= join->fetch_limit)
 
11932
    {
 
11933
      /*
 
11934
        There is a server side cursor and all rows for
 
11935
        this fetch request are sent.
 
11936
      */
 
11937
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11938
    }
 
11939
  }
 
11940
 
 
11941
  return(NESTED_LOOP_OK);
 
11942
}
 
11943
 
 
11944
 
 
11945
        /* ARGSUSED */
 
11946
enum_nested_loop_state
 
11947
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11948
               bool end_of_records)
3871
11949
{
3872
11950
  int idx= -1;
3873
11951
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3875
11953
  if (!join->first_record || end_of_records ||
3876
11954
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3877
11955
  {
3878
 
    if (join->first_record ||
 
11956
    if (join->first_record || 
3879
11957
        (end_of_records && !join->group && !join->group_optimized_away))
3880
11958
    {
3881
11959
      if (idx < (int) join->send_group_parts)
3882
11960
      {
3883
 
        int error=0;
3884
 
        {
3885
 
          if (!join->first_record)
3886
 
          {
3887
 
                  List_iterator_fast<Item> it(*join->fields);
3888
 
                  Item *item;
3889
 
            /* No matching rows for group function */
3890
 
            join->clear();
 
11961
        int error=0;
 
11962
        {
 
11963
          if (!join->first_record)
 
11964
          {
 
11965
            List_iterator_fast<Item> it(*join->fields);
 
11966
            Item *item;
 
11967
            /* No matching rows for group function */
 
11968
            join->clear();
3891
11969
 
3892
11970
            while ((item= it++))
3893
11971
              item->no_rows_in_result();
3894
 
          }
3895
 
          if (join->having && join->having->val_int() == 0)
3896
 
            error= -1;                          // Didn't satisfy having
3897
 
          else
3898
 
          {
3899
 
            if (join->do_send_rows)
3900
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
3901
 
            join->send_records++;
3902
 
          }
3903
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
3904
 
          {
3905
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
3906
 
              error= 1;
3907
 
          }
3908
 
        }
3909
 
        if (error > 0)
3910
 
          return(NESTED_LOOP_ERROR);
3911
 
        if (end_of_records)
3912
 
          return(NESTED_LOOP_OK);
3913
 
        if (join->send_records >= join->unit->select_limit_cnt &&
3914
 
            join->do_send_rows)
3915
 
        {
3916
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
3917
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
3918
 
          join->do_send_rows=0;
3919
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
11972
          }
 
11973
          if (join->having && join->having->val_int() == 0)
 
11974
            error= -1;                          // Didn't satisfy having
 
11975
          else
 
11976
          {
 
11977
            if (join->do_send_rows)
 
11978
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
11979
            join->send_records++;
 
11980
          }
 
11981
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
11982
          {
 
11983
            if (join->rollup_send_data((uint) (idx+1)))
 
11984
              error= 1;
 
11985
          }
 
11986
        }
 
11987
        if (error > 0)
 
11988
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
11989
        if (end_of_records)
 
11990
          return(NESTED_LOOP_OK);
 
11991
        if (join->send_records >= join->unit->select_limit_cnt &&
 
11992
            join->do_send_rows)
 
11993
        {
 
11994
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
11995
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
11996
          join->do_send_rows=0;
 
11997
          join->unit->select_limit_cnt = HA_POS_ERROR;
3920
11998
        }
3921
11999
        else if (join->send_records >= join->fetch_limit)
3922
12000
        {
3935
12013
    else
3936
12014
    {
3937
12015
      if (end_of_records)
3938
 
        return(NESTED_LOOP_OK);
 
12016
        return(NESTED_LOOP_OK);
3939
12017
      join->first_record=1;
3940
 
      test_if_item_cache_changed(join->group_fields);
 
12018
      VOID(test_if_item_cache_changed(join->group_fields));
3941
12019
    }
3942
12020
    if (idx < (int) join->send_group_parts)
3943
12021
    {
3947
12025
      */
3948
12026
      copy_fields(&join->tmp_table_param);
3949
12027
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return(NESTED_LOOP_ERROR);
 
12028
        return(NESTED_LOOP_ERROR);
3951
12029
      return(ok_code);
3952
12030
    }
3953
12031
  }
3956
12034
  return(NESTED_LOOP_OK);
3957
12035
}
3958
12036
 
3959
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12037
 
 
12038
        /* ARGSUSED */
 
12039
enum_nested_loop_state
 
12040
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12041
          bool end_of_records)
 
12042
{
 
12043
  Table *table=join->tmp_table;
 
12044
 
 
12045
  if (join->thd->killed)                        // Aborted by user
 
12046
  {
 
12047
    join->thd->send_kill_message();
 
12048
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12049
  }
 
12050
  if (!end_of_records)
 
12051
  {
 
12052
    copy_fields(&join->tmp_table_param);
 
12053
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12054
#ifdef TO_BE_DELETED
 
12055
    if (!table->uniques)                        // If not unique handling
 
12056
    {
 
12057
      /* Copy null values from group to row */
 
12058
      order_st   *group;
 
12059
      for (group=table->group ; group ; group=group->next)
 
12060
      {
 
12061
        Item *item= *group->item;
 
12062
        if (item->maybe_null)
 
12063
        {
 
12064
          Field *field=item->get_tmp_table_field();
 
12065
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
 
12066
        }
 
12067
      }
 
12068
    }
 
12069
#endif
 
12070
    if (!join->having || join->having->val_int())
 
12071
    {
 
12072
      int error;
 
12073
      join->found_records++;
 
12074
      if ((error=table->file->ha_write_row(table->record[0])))
 
12075
      {
 
12076
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12077
          goto end;
 
12078
        if (create_myisam_from_heap(join->thd, table,
 
12079
                                    join->tmp_table_param.start_recinfo,
 
12080
                                    &join->tmp_table_param.recinfo,
 
12081
                                    error, 1))
 
12082
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12083
        table->s->uniques=0;                    // To ensure rows are the same
 
12084
      }
 
12085
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12086
          join->do_send_rows)
 
12087
      {
 
12088
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12089
          return(NESTED_LOOP_QUERY_LIMIT);
 
12090
        join->do_send_rows=0;
 
12091
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12092
        return(NESTED_LOOP_OK);
 
12093
      }
 
12094
    }
 
12095
  }
 
12096
end:
 
12097
  return(NESTED_LOOP_OK);
 
12098
}
 
12099
 
 
12100
/* ARGSUSED */
 
12101
/** Group by searching after group record and updating it if possible. */
 
12102
 
 
12103
static enum_nested_loop_state
 
12104
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12105
           bool end_of_records)
 
12106
{
 
12107
  Table *table=join->tmp_table;
 
12108
  order_st   *group;
 
12109
  int     error;
 
12110
 
 
12111
  if (end_of_records)
 
12112
    return(NESTED_LOOP_OK);
 
12113
  if (join->thd->killed)                        // Aborted by user
 
12114
  {
 
12115
    join->thd->send_kill_message();
 
12116
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12117
  }
 
12118
 
 
12119
  join->found_records++;
 
12120
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12121
  /* Make a key of group index */
 
12122
  for (group=table->group ; group ; group=group->next)
 
12123
  {
 
12124
    Item *item= *group->item;
 
12125
    item->save_org_in_field(group->field);
 
12126
    /* Store in the used key if the field was 0 */
 
12127
    if (item->maybe_null)
 
12128
      group->buff[-1]= (char) group->field->is_null();
 
12129
  }
 
12130
  if (!table->file->index_read_map(table->record[1],
 
12131
                                   join->tmp_table_param.group_buff,
 
12132
                                   HA_WHOLE_KEY,
 
12133
                                   HA_READ_KEY_EXACT))
 
12134
  {                                             /* Update old record */
 
12135
    restore_record(table,record[1]);
 
12136
    update_tmptable_sum_func(join->sum_funcs,table);
 
12137
    if ((error=table->file->ha_update_row(table->record[1],
 
12138
                                          table->record[0])))
 
12139
    {
 
12140
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12141
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12142
    }
 
12143
    return(NESTED_LOOP_OK);
 
12144
  }
 
12145
 
 
12146
  /*
 
12147
    Copy null bits from group key to table
 
12148
    We can't copy all data as the key may have different format
 
12149
    as the row data (for example as with VARCHAR keys)
 
12150
  */
 
12151
  KEY_PART_INFO *key_part;
 
12152
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12153
       group ;
 
12154
       group=group->next,key_part++)
 
12155
  {
 
12156
    if (key_part->null_bit)
 
12157
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12158
  }
 
12159
  init_tmptable_sum_functions(join->sum_funcs);
 
12160
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12161
  if ((error=table->file->ha_write_row(table->record[0])))
 
12162
  {
 
12163
    if (create_myisam_from_heap(join->thd, table,
 
12164
                                join->tmp_table_param.start_recinfo,
 
12165
                                &join->tmp_table_param.recinfo,
 
12166
                                error, 0))
 
12167
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12168
    /* Change method to update rows */
 
12169
    table->file->ha_index_init(0, 0);
 
12170
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12171
  }
 
12172
  join->send_records++;
 
12173
  return(NESTED_LOOP_OK);
 
12174
}
 
12175
 
 
12176
 
 
12177
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12178
 
 
12179
static enum_nested_loop_state
 
12180
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12181
                  bool end_of_records)
 
12182
{
 
12183
  Table *table=join->tmp_table;
 
12184
  int     error;
 
12185
 
 
12186
  if (end_of_records)
 
12187
    return(NESTED_LOOP_OK);
 
12188
  if (join->thd->killed)                        // Aborted by user
 
12189
  {
 
12190
    join->thd->send_kill_message();
 
12191
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12192
  }
 
12193
 
 
12194
  init_tmptable_sum_functions(join->sum_funcs);
 
12195
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12196
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12197
 
 
12198
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12199
    join->send_records++;                       // New group
 
12200
  else
 
12201
  {
 
12202
    if ((int) table->file->get_dup_key(error) < 0)
 
12203
    {
 
12204
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12205
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12206
    }
 
12207
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12208
    {
 
12209
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12210
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12211
    }
 
12212
    restore_record(table,record[1]);
 
12213
    update_tmptable_sum_func(join->sum_funcs,table);
 
12214
    if ((error=table->file->ha_update_row(table->record[1],
 
12215
                                          table->record[0])))
 
12216
    {
 
12217
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12218
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12219
    }
 
12220
  }
 
12221
  return(NESTED_LOOP_OK);
 
12222
}
 
12223
 
 
12224
 
 
12225
        /* ARGSUSED */
 
12226
enum_nested_loop_state
 
12227
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12228
                bool end_of_records)
3960
12229
{
3961
12230
  Table *table=join->tmp_table;
3962
12231
  int     idx= -1;
3963
12232
 
3964
 
  if (join->session->getKilled())
 
12233
  if (join->thd->killed)
3965
12234
  {                                             // Aborted by user
3966
 
    join->session->send_kill_message();
3967
 
    return NESTED_LOOP_KILLED;
 
12235
    join->thd->send_kill_message();
 
12236
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3968
12237
  }
3969
12238
  if (!join->first_record || end_of_records ||
3970
12239
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3974
12243
      int send_group_parts= join->send_group_parts;
3975
12244
      if (idx < send_group_parts)
3976
12245
      {
3977
 
        if (!join->first_record)
3978
 
        {
3979
 
          /* No matching rows for group function */
3980
 
          join->clear();
3981
 
        }
3982
 
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
3983
 
        if (!join->having || join->having->val_int())
3984
 
        {
3985
 
          int error= table->cursor->insertRecord(table->getInsertRecord());
3986
 
 
3987
 
          if (error)
3988
 
          {
3989
 
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3990
 
            return NESTED_LOOP_ERROR;
3991
 
          }
 
12246
        if (!join->first_record)
 
12247
        {
 
12248
          /* No matching rows for group function */
 
12249
          join->clear();
 
12250
        }
 
12251
        copy_sum_funcs(join->sum_funcs,
 
12252
                       join->sum_funcs_end[send_group_parts]);
 
12253
        if (!join->having || join->having->val_int())
 
12254
        {
 
12255
          int error= table->file->ha_write_row(table->record[0]);
 
12256
          if (error && create_myisam_from_heap(join->thd, table,
 
12257
                                               join->tmp_table_param.start_recinfo,
 
12258
                                                &join->tmp_table_param.recinfo,
 
12259
                                               error, 0))
 
12260
            return(NESTED_LOOP_ERROR);
3992
12261
        }
3993
12262
        if (join->rollup.state != ROLLUP::STATE_NONE)
3994
 
        {
3995
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3996
 
            return NESTED_LOOP_ERROR;
3997
 
        }
3998
 
        if (end_of_records)
3999
 
          return NESTED_LOOP_OK;
 
12263
        {
 
12264
          if (join->rollup_write_data((uint) (idx+1), table))
 
12265
            return(NESTED_LOOP_ERROR);
 
12266
        }
 
12267
        if (end_of_records)
 
12268
          return(NESTED_LOOP_OK);
4000
12269
      }
4001
12270
    }
4002
12271
    else
4003
12272
    {
4004
12273
      if (end_of_records)
4005
 
        return NESTED_LOOP_OK;
 
12274
        return(NESTED_LOOP_OK);
4006
12275
      join->first_record=1;
4007
 
      test_if_item_cache_changed(join->group_fields);
 
12276
      VOID(test_if_item_cache_changed(join->group_fields));
4008
12277
    }
4009
12278
    if (idx < (int) join->send_group_parts)
4010
12279
    {
4011
12280
      copy_fields(&join->tmp_table_param);
4012
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4013
 
        return NESTED_LOOP_ERROR;
 
12281
      copy_funcs(join->tmp_table_param.items_to_copy);
4014
12282
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4015
 
        return NESTED_LOOP_ERROR;
4016
 
      return NESTED_LOOP_OK;
 
12283
        return(NESTED_LOOP_ERROR);
 
12284
      return(NESTED_LOOP_OK);
4017
12285
    }
4018
12286
  }
4019
12287
  if (update_sum_func(join->sum_funcs))
4020
 
    return NESTED_LOOP_ERROR;
4021
 
  return NESTED_LOOP_OK;
 
12288
    return(NESTED_LOOP_ERROR);
 
12289
  return(NESTED_LOOP_OK);
4022
12290
}
4023
12291
 
 
12292
 
4024
12293
/*****************************************************************************
4025
12294
  Remove calculation with tables that aren't yet read. Remove also tests
4026
12295
  against fields that are read through key where the table is not a
4027
12296
  outer join table.
4028
12297
  We can't remove tests that are made against columns which are stored
4029
12298
  in sorted order.
 
12299
*****************************************************************************/
 
12300
 
 
12301
/**
4030
12302
  @return
4031
 
    1 if right_item used is a removable reference key on left_item
4032
 
    0 otherwise.
4033
 
****************************************************************************/
4034
 
bool test_if_ref(Item_field *left_item,Item *right_item)
 
12303
    1 if right_item is used removable reference key on left_item
 
12304
*/
 
12305
 
 
12306
static bool test_if_ref(Item_field *left_item,Item *right_item)
4035
12307
{
4036
12308
  Field *field=left_item->field;
4037
12309
  // No need to change const test. We also have to keep tests on LEFT JOIN
4038
 
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
 
12310
  if (!field->table->const_table && !field->table->maybe_null)
4039
12311
  {
4040
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12312
    Item *ref_item=part_of_refkey(field->table,field);
4041
12313
    if (ref_item && ref_item->eq(right_item,1))
4042
12314
    {
4043
12315
      right_item= right_item->real_item();
4044
12316
      if (right_item->type() == Item::FIELD_ITEM)
4045
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12317
        return (field->eq_def(((Item_field *) right_item)->field));
4046
12318
      /* remove equalities injected by IN->EXISTS transformation */
4047
12319
      else if (right_item->type() == Item::CACHE_ITEM)
4048
12320
        return ((Item_cache *)right_item)->eq_def (field);
4049
12321
      if (right_item->const_item() && !(right_item->is_null()))
4050
12322
      {
4051
 
        /*
4052
 
          We can remove binary fields and numerical fields except float,
4053
 
          as float comparison isn't 100 % secure
4054
 
          We have to keep normal strings to be able to check for end spaces
 
12323
        /*
 
12324
          We can remove binary fields and numerical fields except float,
 
12325
          as float comparison isn't 100 % secure
 
12326
          We have to keep normal strings to be able to check for end spaces
4055
12327
 
4056
 
                sergefp: the above seems to be too restrictive. Counterexample:
4057
 
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
4058
 
                  insert into t100 values ('a'),('a ');
4059
 
                  explain select * from t100 where v='a';
4060
 
                The EXPLAIN shows 'using Where'. Running the query returns both
4061
 
                rows, so it seems there are no problems with endspace in the most
4062
 
                frequent case?
4063
 
        */
4064
 
        if (field->binary() &&
4065
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4066
 
            field->decimals() == 0)
4067
 
        {
4068
 
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4069
 
        }
 
12328
          sergefp: the above seems to be too restrictive. Counterexample:
 
12329
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12330
            insert into t100 values ('a'),('a ');
 
12331
            explain select * from t100 where v='a';
 
12332
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12333
          rows, so it seems there are no problems with endspace in the most
 
12334
          frequent case?
 
12335
        */
 
12336
        if (field->binary() &&
 
12337
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12338
            field->decimals() == 0)
 
12339
        {
 
12340
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12341
        }
4070
12342
      }
4071
12343
    }
4072
12344
  }
4073
 
  return 0;
 
12345
  return 0;                                     // keep test
 
12346
}
 
12347
 
 
12348
/**
 
12349
   @brief Replaces an expression destructively inside the expression tree of
 
12350
   the WHERE clase.
 
12351
 
 
12352
   @note Because of current requirements for semijoin flattening, we do not
 
12353
   need to recurse here, hence this function will only examine the top-level
 
12354
   AND conditions. (see JOIN::prepare, comment above the line 
 
12355
   'if (do_materialize)'
 
12356
   
 
12357
   @param join The top-level query.
 
12358
   @param old_cond The expression to be replaced.
 
12359
   @param new_cond The expression to be substituted.
 
12360
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
12361
   the new expression.
 
12362
   @return <code>true</code> if there was an error, <code>false</code> if
 
12363
   successful.
 
12364
*/
 
12365
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
12366
                                       Item *new_cond, bool do_fix_fields)
 
12367
{
 
12368
  if (join->conds == old_cond) {
 
12369
    join->conds= new_cond;
 
12370
    if (do_fix_fields)
 
12371
      new_cond->fix_fields(join->thd, &join->conds);
 
12372
    return false;
 
12373
  }
 
12374
  
 
12375
  if (join->conds->type() == Item::COND_ITEM) {
 
12376
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12377
    Item *item;
 
12378
    while ((item= li++))
 
12379
      if (item == old_cond) 
 
12380
      {
 
12381
        li.replace(new_cond);
 
12382
        if (do_fix_fields)
 
12383
          new_cond->fix_fields(join->thd, li.ref());
 
12384
        return false;
 
12385
      }
 
12386
  }
 
12387
 
 
12388
  return true;
4074
12389
}
4075
12390
 
4076
12391
/*
4077
12392
  Extract a condition that can be checked after reading given table
4078
 
 
 
12393
  
4079
12394
  SYNOPSIS
4080
12395
    make_cond_for_table()
4081
12396
      cond         Condition to analyze
4082
12397
      tables       Tables for which "current field values" are available
4083
 
      used_table   Table that we're extracting the condition for (may
 
12398
      used_table   Table that we're extracting the condition for (may 
4084
12399
                   also include PSEUDO_TABLE_BITS
4085
12400
 
4086
12401
  DESCRIPTION
4090
12405
 
4091
12406
    The function assumes that
4092
12407
      - Constant parts of the condition has already been checked.
4093
 
      - Condition that could be checked for tables in 'tables' has already
 
12408
      - Condition that could be checked for tables in 'tables' has already 
4094
12409
        been checked.
4095
 
 
 
12410
        
4096
12411
    The function takes into account that some parts of the condition are
4097
12412
    guaranteed to be true by employed 'ref' access methods (the code that
4098
12413
    does this is located at the end, search down for "EQ_FUNC").
4099
12414
 
4100
12415
 
4101
 
  SEE ALSO
 
12416
  SEE ALSO 
4102
12417
    make_cond_for_info_schema uses similar algorithm
4103
12418
 
4104
12419
  RETURN
4105
12420
    Extracted condition
4106
12421
*/
4107
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12422
 
 
12423
static COND *
 
12424
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12425
                    bool exclude_expensive_cond)
4108
12426
{
4109
12427
  if (used_table && !(cond->used_tables() & used_table) &&
4110
 
    /*
4111
 
      Exclude constant conditions not checked at optimization time if
4112
 
      the table we are pushing conditions to is the first one.
4113
 
      As a result, such conditions are not considered as already checked
4114
 
      and will be checked at execution time, attached to the first table.
4115
 
    */
4116
 
    !((used_table & 1) && cond->is_expensive()))
 
12428
      /*
 
12429
        Exclude constant conditions not checked at optimization time if
 
12430
        the table we are pushing conditions to is the first one.
 
12431
        As a result, such conditions are not considered as already checked
 
12432
        and will be checked at execution time, attached to the first table.
 
12433
      */
 
12434
      !((used_table & 1) && cond->is_expensive()))
4117
12435
    return (COND*) 0;                           // Already checked
4118
12436
  if (cond->type() == Item::COND_ITEM)
4119
12437
  {
4122
12440
      /* Create new top level AND item */
4123
12441
      Item_cond_and *new_cond=new Item_cond_and;
4124
12442
      if (!new_cond)
4125
 
        return (COND*) 0;
 
12443
        return (COND*) 0;                       // OOM /* purecov: inspected */
4126
12444
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4127
12445
      Item *item;
4128
12446
      while ((item=li++))
4129
12447
      {
4130
 
        Item *fix= make_cond_for_table(item,tables,used_table,
4131
 
                                            exclude_expensive_cond);
4132
 
        if (fix)
4133
 
          new_cond->argument_list()->push_back(fix);
 
12448
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12449
                                      exclude_expensive_cond);
 
12450
        if (fix)
 
12451
          new_cond->argument_list()->push_back(fix);
4134
12452
      }
4135
 
      switch (new_cond->argument_list()->elements) 
4136
 
      {
4137
 
        case 0:
4138
 
          return (COND*) 0;                     // Always true
4139
 
        case 1:
4140
 
          return new_cond->argument_list()->head();
4141
 
        default:
4142
 
          /*
4143
 
            Item_cond_and do not need fix_fields for execution, its parameters
4144
 
            are fixed or do not need fix_fields, too
4145
 
          */
4146
 
          new_cond->quick_fix_field();
4147
 
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4148
 
          return new_cond;
 
12453
      switch (new_cond->argument_list()->elements) {
 
12454
      case 0:
 
12455
        return (COND*) 0;                       // Always true
 
12456
      case 1:
 
12457
        return new_cond->argument_list()->head();
 
12458
      default:
 
12459
        /*
 
12460
          Item_cond_and do not need fix_fields for execution, its parameters
 
12461
          are fixed or do not need fix_fields, too
 
12462
        */
 
12463
        new_cond->quick_fix_field();
 
12464
        new_cond->used_tables_cache=
 
12465
          ((Item_cond_and*) cond)->used_tables_cache &
 
12466
          tables;
 
12467
        return new_cond;
4149
12468
      }
4150
12469
    }
4151
12470
    else
4152
12471
    {                                           // Or list
4153
12472
      Item_cond_or *new_cond=new Item_cond_or;
4154
12473
      if (!new_cond)
4155
 
        return (COND*) 0;
 
12474
        return (COND*) 0;                       // OOM /* purecov: inspected */
4156
12475
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4157
12476
      Item *item;
4158
12477
      while ((item=li++))
4159
12478
      {
4160
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4161
 
        if (!fix)
4162
 
          return (COND*) 0;                     // Always true
4163
 
        new_cond->argument_list()->push_back(fix);
 
12479
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12480
        if (!fix)
 
12481
          return (COND*) 0;                     // Always true
 
12482
        new_cond->argument_list()->push_back(fix);
4164
12483
      }
4165
12484
      /*
4166
 
        Item_cond_and do not need fix_fields for execution, its parameters
4167
 
        are fixed or do not need fix_fields, too
 
12485
        Item_cond_and do not need fix_fields for execution, its parameters
 
12486
        are fixed or do not need fix_fields, too
4168
12487
      */
4169
12488
      new_cond->quick_fix_field();
4170
12489
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4189
12508
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4190
12509
    return cond;                                // Not boolean op
4191
12510
 
4192
 
  /*
 
12511
  /* 
4193
12512
    Remove equalities that are guaranteed to be true by use of 'ref' access
4194
12513
    method
4195
12514
  */
4197
12516
  {
4198
12517
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4199
12518
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4200
 
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
 
12519
    if (left_item->type() == Item::FIELD_ITEM &&
 
12520
        test_if_ref((Item_field*) left_item,right_item))
4201
12521
    {
4202
12522
      cond->marker=3;                   // Checked when read
4203
12523
      return (COND*) 0;
4204
12524
    }
4205
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12525
    if (right_item->type() == Item::FIELD_ITEM &&
 
12526
        test_if_ref((Item_field*) right_item,left_item))
4206
12527
    {
4207
12528
      cond->marker=3;                   // Checked when read
4208
12529
      return (COND*) 0;
4212
12533
  return cond;
4213
12534
}
4214
12535
 
4215
 
static Item *part_of_refkey(Table *table,Field *field)
 
12536
 
 
12537
static Item *
 
12538
part_of_refkey(Table *table,Field *field)
4216
12539
{
4217
12540
  if (!table->reginfo.join_tab)
4218
12541
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4219
12542
 
4220
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
12543
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
4221
12544
  if (ref_parts)
4222
12545
  {
4223
 
    KeyPartInfo *key_part=
 
12546
    KEY_PART_INFO *key_part=
4224
12547
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4225
 
    uint32_t part;
 
12548
    uint part;
4226
12549
 
4227
12550
    for (part=0 ; part < ref_parts ; part++)
4228
12551
    {
4231
12554
    }
4232
12555
 
4233
12556
    for (part=0 ; part < ref_parts ; part++,key_part++)
4234
 
    {
4235
12557
      if (field->eq(key_part->field) &&
4236
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4237
 
          //If field can be NULL, we should not remove this predicate, as
4238
 
          //it may lead to non-rejection of NULL values. 
4239
 
          !(field->real_maybe_null()))
4240
 
      {
 
12558
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4241
12559
        return table->reginfo.join_tab->ref.items[part];
4242
 
      }
4243
 
    }
4244
12560
  }
4245
12561
  return (Item*) 0;
4246
12562
}
4247
12563
 
 
12564
 
4248
12565
/**
4249
12566
  Test if one can use the key to resolve order_st BY.
4250
12567
 
4265
12582
  @retval
4266
12583
    -1   Reverse key can be used
4267
12584
*/
4268
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12585
 
 
12586
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12587
                                uint32_t *used_key_parts)
4269
12588
{
4270
 
  KeyPartInfo *key_part= NULL;
4271
 
  KeyPartInfo *key_part_end= NULL;
4272
 
  key_part= table->key_info[idx].key_part;
4273
 
  key_part_end= key_part + table->key_info[idx].key_parts;
 
12589
  KEY_PART_INFO *key_part,*key_part_end;
 
12590
  key_part=table->key_info[idx].key_part;
 
12591
  key_part_end=key_part+table->key_info[idx].key_parts;
4274
12592
  key_part_map const_key_parts=table->const_key_parts[idx];
4275
 
  int reverse= 0;
 
12593
  int reverse=0;
4276
12594
  bool on_primary_key= false;
4277
12595
 
4278
12596
  for (; order ; order=order->next, const_key_parts>>=1)
4282
12600
 
4283
12601
    /*
4284
12602
      Skip key parts that are constants in the WHERE clause.
4285
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12603
      These are already skipped in the order_st BY by const_expression_in_where()
4286
12604
    */
4287
12605
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4288
 
      key_part++;
 
12606
      key_part++; 
4289
12607
 
4290
12608
    if (key_part == key_part_end)
4291
12609
    {
4292
 
      /*
 
12610
      /* 
4293
12611
        We are at the end of the key. Check if the engine has the primary
4294
12612
        key as a suffix to the secondary keys. If it has continue to check
4295
12613
        the primary key as a suffix.
4296
12614
      */
4297
12615
      if (!on_primary_key &&
4298
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4299
 
          table->getShare()->hasPrimaryKey())
 
12616
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12617
          table->s->primary_key != MAX_KEY)
4300
12618
      {
4301
12619
        on_primary_key= true;
4302
 
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4303
 
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4304
 
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
 
12620
        key_part= table->key_info[table->s->primary_key].key_part;
 
12621
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12622
        const_key_parts=table->const_key_parts[table->s->primary_key];
4305
12623
 
4306
12624
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4307
 
          key_part++;
 
12625
          key_part++; 
4308
12626
        /*
4309
12627
         The primary and secondary key parts were all const (i.e. there's
4310
12628
         one row).  The sorting doesn't matter.
4328
12646
    key_part++;
4329
12647
  }
4330
12648
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4331
 
    (uint32_t) (key_part - table->key_info[idx].key_part);
4332
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
12649
    (uint) (key_part - table->key_info[idx].key_part);
 
12650
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4333
12651
                         HA_READ_PREV))
4334
12652
    reverse= 0;                                 // Index can't be used
4335
12653
  return(reverse);
4336
12654
}
4337
12655
 
 
12656
 
4338
12657
/**
4339
12658
  Test if a second key is the subkey of the first one.
4340
12659
 
4350
12669
  @retval
4351
12670
    0   no sub key
4352
12671
*/
4353
 
inline bool is_subkey(KeyPartInfo *key_part,
4354
 
                      KeyPartInfo *ref_key_part,
4355
 
                      KeyPartInfo *ref_key_part_end)
 
12672
 
 
12673
inline bool 
 
12674
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12675
          KEY_PART_INFO *ref_key_part_end)
4356
12676
{
4357
12677
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4358
 
    if (! key_part->field->eq(ref_key_part->field))
 
12678
    if (!key_part->field->eq(ref_key_part->field))
4359
12679
      return 0;
4360
12680
  return 1;
4361
12681
}
4371
12691
    - MAX_KEY                   If we can't use other key
4372
12692
    - the number of found key   Otherwise
4373
12693
*/
4374
 
static uint32_t test_if_subkey(Order *order,
4375
 
                               Table *table,
4376
 
                               uint32_t ref,
4377
 
                               uint32_t ref_key_parts,
4378
 
                               const key_map *usable_keys)
 
12694
 
 
12695
static uint
 
12696
test_if_subkey(order_st *order, Table *table, uint ref, uint ref_key_parts,
 
12697
               const key_map *usable_keys)
4379
12698
{
4380
12699
  uint32_t nr;
4381
12700
  uint32_t min_length= UINT32_MAX;
4382
12701
  uint32_t best= MAX_KEY;
4383
12702
  uint32_t not_used;
4384
 
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4385
 
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
 
12703
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12704
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4386
12705
 
4387
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12706
  for (nr= 0 ; nr < table->s->keys ; nr++)
4388
12707
  {
4389
 
    if (usable_keys->test(nr) &&
 
12708
    if (usable_keys->is_set(nr) &&
4390
12709
        table->key_info[nr].key_length < min_length &&
4391
12710
        table->key_info[nr].key_parts >= ref_key_parts &&
4392
12711
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4400
12719
  return best;
4401
12720
}
4402
12721
 
 
12722
 
4403
12723
/**
4404
12724
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4405
12725
  already known to be distinct.
4418
12738
    of the table are referenced by a list : either the select list
4419
12739
    through find_field_in_item_list or GROUP BY list through
4420
12740
    find_field_in_order_list.
4421
 
    If the above holds and the key parts cannot contain NULLs then we
 
12741
    If the above holds and the key parts cannot contain NULLs then we 
4422
12742
    can safely remove the GROUP BY/DISTINCT,
4423
12743
    as no result set can be more distinct than an unique key.
4424
12744
 
4431
12751
  @retval
4432
12752
    0                    not found.
4433
12753
*/
4434
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12754
 
 
12755
static bool
 
12756
list_contains_unique_index(Table *table,
 
12757
                          bool (*find_func) (Field *, void *), void *data)
4435
12758
{
4436
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12759
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
4437
12760
  {
4438
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12761
    if (keynr == table->s->primary_key ||
4439
12762
         (table->key_info[keynr].flags & HA_NOSAME))
4440
12763
    {
4441
 
      KeyInfo *keyinfo= table->key_info + keynr;
4442
 
      KeyPartInfo *key_part= NULL;
4443
 
      KeyPartInfo *key_part_end= NULL;
 
12764
      KEY *keyinfo= table->key_info + keynr;
 
12765
      KEY_PART_INFO *key_part, *key_part_end;
4444
12766
 
4445
12767
      for (key_part=keyinfo->key_part,
4446
12768
           key_part_end=key_part+ keyinfo->key_parts;
4447
12769
           key_part < key_part_end;
4448
12770
           key_part++)
4449
12771
      {
4450
 
        if (key_part->field->maybe_null() ||
4451
 
            ! find_func(key_part->field, data))
 
12772
        if (key_part->field->maybe_null() || 
 
12773
            !find_func(key_part->field, data))
4452
12774
          break;
4453
12775
      }
4454
12776
      if (key_part == key_part_end)
4458
12780
  return 0;
4459
12781
}
4460
12782
 
 
12783
 
4461
12784
/**
4462
12785
  Helper function for list_contains_unique_index.
4463
12786
  Find a field reference in a list of order_st structures.
4471
12794
  @retval
4472
12795
    0                    not found.
4473
12796
*/
4474
 
bool find_field_in_order_list (Field *field, void *data)
 
12797
 
 
12798
static bool
 
12799
find_field_in_order_list (Field *field, void *data)
4475
12800
{
4476
 
  Order *group= (Order *) data;
 
12801
  order_st *group= (order_st *) data;
4477
12802
  bool part_found= 0;
4478
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12803
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4479
12804
  {
4480
12805
    Item *item= (*tmp_group->item)->real_item();
4481
12806
    if (item->type() == Item::FIELD_ITEM &&
4488
12813
  return part_found;
4489
12814
}
4490
12815
 
 
12816
 
4491
12817
/**
4492
12818
  Helper function for list_contains_unique_index.
4493
12819
  Find a field reference in a dynamic list of Items.
4501
12827
  @retval
4502
12828
    0                    not found.
4503
12829
*/
4504
 
bool find_field_in_item_list (Field *field, void *data)
 
12830
 
 
12831
static bool
 
12832
find_field_in_item_list (Field *field, void *data)
4505
12833
{
4506
12834
  List<Item> *fields= (List<Item> *) data;
4507
12835
  bool part_found= 0;
4520
12848
  return part_found;
4521
12849
}
4522
12850
 
 
12851
 
4523
12852
/**
4524
 
  Test if we can skip the ORDER BY by using an index.
 
12853
  Test if we can skip the order_st BY by using an index.
4525
12854
 
4526
12855
  SYNOPSIS
4527
12856
    test_if_skip_sort_order()
4531
12860
      no_changes
4532
12861
      map
4533
12862
 
4534
 
  If we can use an index, the JoinTable / tab->select struct
 
12863
  If we can use an index, the JOIN_TAB / tab->select struct
4535
12864
  is changed to use the index.
4536
12865
 
4537
12866
  The index must cover all fields in <order>, or it will not be considered.
4538
12867
 
4539
12868
  @todo
4540
 
    - sergeyp: Results of all index merge selects actually are ordered
 
12869
    - sergeyp: Results of all index merge selects actually are ordered 
4541
12870
    by clustered PK values.
4542
12871
 
4543
12872
  @retval
4545
12874
  @retval
4546
12875
    1    We can use an index.
4547
12876
*/
4548
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12877
 
 
12878
static bool
 
12879
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12880
                        bool no_changes, const key_map *map)
4549
12881
{
4550
12882
  int32_t ref_key;
4551
 
  uint32_t ref_key_parts;
 
12883
  uint ref_key_parts;
4552
12884
  int order_direction;
4553
12885
  uint32_t used_key_parts;
4554
12886
  Table *table=tab->table;
4555
 
  optimizer::SqlSelect *select= tab->select;
 
12887
  SQL_SELECT *select=tab->select;
4556
12888
  key_map usable_keys;
4557
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12889
  QUICK_SELECT_I *save_quick= 0;
4558
12890
 
4559
12891
  /*
4560
12892
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4562
12894
  */
4563
12895
  usable_keys= *map;
4564
12896
 
4565
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12897
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4566
12898
  {
4567
12899
    Item *item= (*tmp_order->item)->real_item();
4568
12900
    if (item->type() != Item::FIELD_ITEM)
4569
12901
    {
4570
 
      usable_keys.reset();
 
12902
      usable_keys.clear_all();
4571
12903
      return(0);
4572
12904
    }
4573
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4574
 
    if (usable_keys.none())
 
12905
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12906
    if (usable_keys.is_clear_all())
4575
12907
      return(0);                                        // No usable keys
4576
12908
  }
4577
12909
 
4581
12913
  {
4582
12914
    ref_key=       tab->ref.key;
4583
12915
    ref_key_parts= tab->ref.key_parts;
4584
 
    if (tab->type == AM_REF_OR_NULL)
 
12916
    if (tab->type == JT_REF_OR_NULL)
4585
12917
      return(0);
4586
12918
  }
4587
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12919
  else if (select && select->quick)             // Range found by opt_range
4588
12920
  {
4589
12921
    int quick_type= select->quick->get_type();
4590
12922
    save_quick= select->quick;
4591
 
    /*
4592
 
      assume results are not ordered when index merge is used
4593
 
      TODO: sergeyp: Results of all index merge selects actually are ordered
 
12923
    /* 
 
12924
      assume results are not ordered when index merge is used 
 
12925
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4594
12926
      by clustered PK values.
4595
12927
    */
4596
 
 
4597
 
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4598
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4599
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
 
12928
  
 
12929
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
12930
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
12931
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4600
12932
      return(0);
4601
12933
    ref_key=       select->quick->index;
4602
12934
    ref_key_parts= select->quick->used_key_parts;
4607
12939
    /*
4608
12940
      We come here when there is a REF key.
4609
12941
    */
4610
 
    if (! usable_keys.test(ref_key))
 
12942
    if (!usable_keys.is_set(ref_key))
4611
12943
    {
4612
12944
      /*
4613
 
        We come here when ref_key is not among usable_keys
 
12945
        We come here when ref_key is not among usable_keys
4614
12946
      */
4615
 
      uint32_t new_ref_key;
 
12947
      uint new_ref_key;
4616
12948
      /*
4617
 
        If using index only read, only consider other possible index only
4618
 
        keys
 
12949
        If using index only read, only consider other possible index only
 
12950
        keys
4619
12951
      */
4620
 
      if (table->covering_keys.test(ref_key))
4621
 
        usable_keys&= table->covering_keys;
 
12952
      if (table->covering_keys.is_set(ref_key))
 
12953
        usable_keys.intersect(table->covering_keys);
4622
12954
      if (tab->pre_idx_push_select_cond)
4623
12955
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4624
12956
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4625
12957
                                       &usable_keys)) < MAX_KEY)
4626
12958
      {
4627
 
        /* Found key that can be used to retrieve data in sorted order */
4628
 
        if (tab->ref.key >= 0)
4629
 
        {
 
12959
        /* Found key that can be used to retrieve data in sorted order */
 
12960
        if (tab->ref.key >= 0)
 
12961
        {
4630
12962
          /*
4631
 
            We'll use ref access method on key new_ref_key. In general case
 
12963
            We'll use ref access method on key new_ref_key. In general case 
4632
12964
            the index search tuple for new_ref_key will be different (e.g.
4633
12965
            when one index is defined as (part1, part2, ...) and another as
4634
 
            (part1, part2(N), ...) and the WHERE clause contains
4635
 
            "part1 = const1 AND part2=const2".
 
12966
            (part1, part2(N), ...) and the WHERE clause contains 
 
12967
            "part1 = const1 AND part2=const2". 
4636
12968
            So we build tab->ref from scratch here.
4637
12969
          */
4638
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4639
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12970
          KEYUSE *keyuse= tab->keyuse;
 
12971
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4640
12972
            keyuse++;
4641
12973
 
4642
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
12974
          if (create_ref_for_key(tab->join, tab, keyuse, 
4643
12975
                                 tab->join->const_table_map))
4644
12976
            return(0);
4645
 
        }
4646
 
        else
4647
 
        {
 
12977
        }
 
12978
        else
 
12979
        {
4648
12980
          /*
4649
 
            The range optimizer constructed QuickRange for ref_key, and
 
12981
            The range optimizer constructed QUICK_RANGE for ref_key, and
4650
12982
            we want to use instead new_ref_key as the index. We can't
4651
12983
            just change the index of the quick select, because this may
4652
12984
            result in an incosistent QUICK_SELECT object. Below we
4654
12986
            parameres are set correctly by the range optimizer.
4655
12987
           */
4656
12988
          key_map new_ref_key_map;
4657
 
          new_ref_key_map.reset();  // Force the creation of quick select
4658
 
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
 
12989
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
12990
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4659
12991
 
4660
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
12992
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4661
12993
                                        (tab->join->select_options &
4662
12994
                                         OPTION_FOUND_ROWS) ?
4663
12995
                                        HA_POS_ERROR :
4665
12997
                                        true) <=
4666
12998
              0)
4667
12999
            return(0);
4668
 
        }
 
13000
        }
4669
13001
        ref_key= new_ref_key;
4670
13002
      }
4671
13003
    }
4672
13004
    /* Check if we get the rows in requested sorted order by using the key */
4673
 
    if (usable_keys.test(ref_key) &&
 
13005
    if (usable_keys.is_set(ref_key) &&
4674
13006
        (order_direction= test_if_order_by_key(order,table,ref_key,
4675
13007
                                               &used_key_parts)))
4676
13008
      goto check_reverse_order;
4682
13014
      or a table scan.
4683
13015
      It may be the case if order_st/GROUP BY is used with LIMIT.
4684
13016
    */
4685
 
    uint32_t nr;
 
13017
    uint nr;
4686
13018
    key_map keys;
4687
 
    uint32_t best_key_parts= 0;
 
13019
    uint best_key_parts= 0;
4688
13020
    int best_key_direction= 0;
4689
13021
    ha_rows best_records= 0;
4690
13022
    double read_time;
4691
13023
    int best_key= -1;
4692
13024
    bool is_best_covering= false;
4693
13025
    double fanout= 1;
4694
 
    Join *join= tab->join;
4695
 
    uint32_t tablenr= tab - join->join_tab;
4696
 
    ha_rows table_records= table->cursor->stats.records;
 
13026
    JOIN *join= tab->join;
 
13027
    uint tablenr= tab - join->join_tab;
 
13028
    ha_rows table_records= table->file->stats.records;
4697
13029
    bool group= join->group && order == join->group_list;
4698
 
    optimizer::Position cur_pos;
4699
13030
 
4700
13031
    /*
4701
13032
      If not used with LIMIT, only use keys if the whole query can be
4704
13035
    */
4705
13036
    if (select_limit >= table_records)
4706
13037
    {
4707
 
      /*
4708
 
        filesort() and join cache are usually faster than reading in
 
13038
      /* 
 
13039
        filesort() and join cache are usually faster than reading in 
4709
13040
        index order and not using join cache
4710
13041
        */
4711
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13042
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4712
13043
        return(0);
4713
 
      keys= *table->cursor->keys_to_use_for_scanning();
4714
 
      keys|= table->covering_keys;
 
13044
      keys= *table->file->keys_to_use_for_scanning();
 
13045
      keys.merge(table->covering_keys);
4715
13046
 
4716
13047
      /*
4717
 
        We are adding here also the index specified in FORCE INDEX clause,
4718
 
        if any.
 
13048
        We are adding here also the index specified in FORCE INDEX clause, 
 
13049
        if any.
4719
13050
        This is to allow users to use index in order_st BY.
4720
13051
      */
4721
 
      if (table->force_index)
4722
 
        keys|= (group ? table->keys_in_use_for_group_by :
4723
 
                                table->keys_in_use_for_order_by);
4724
 
      keys&= usable_keys;
 
13052
      if (table->force_index) 
 
13053
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13054
                           table->keys_in_use_for_order_by);
 
13055
      keys.intersect(usable_keys);
4725
13056
    }
4726
13057
    else
4727
13058
      keys= usable_keys;
4728
13059
 
4729
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4730
 
    read_time= cur_pos.getCost();
4731
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4732
 
    {
4733
 
      cur_pos= join->getPosFromOptimalPlan(i);
4734
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4735
 
    }
 
13060
    read_time= join->best_positions[tablenr].read_time;
 
13061
    for (uint i= tablenr+1; i < join->tables; i++)
 
13062
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4736
13063
 
4737
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13064
    for (nr=0; nr < table->s->keys ; nr++)
4738
13065
    {
4739
13066
      int direction;
4740
 
      if (keys.test(nr) &&
 
13067
      if (keys.is_set(nr) &&
4741
13068
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4742
13069
      {
4743
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4744
 
 
4745
 
        /*
4746
 
          Don't use an index scan with ORDER BY without limit.
 
13070
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
13071
        
 
13072
        /* 
 
13073
          Don't use an index scan with order_st BY without limit.
4747
13074
          For GROUP BY without limit always use index scan
4748
 
          if there is a suitable index.
 
13075
          if there is a suitable index. 
4749
13076
          Why we hold to this asymmetry hardly can be explained
4750
13077
          rationally. It's easy to demonstrate that using
4751
13078
          temporary table + filesort could be cheaper for grouping
4752
13079
          queries too.
4753
 
        */
 
13080
        */ 
4754
13081
        if (is_covering ||
4755
 
            select_limit != HA_POS_ERROR ||
 
13082
            select_limit != HA_POS_ERROR || 
4756
13083
            (ref_key < 0 && (group || table->force_index)))
4757
 
        {
 
13084
        { 
4758
13085
          double rec_per_key;
4759
13086
          double index_scan_time;
4760
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13087
          KEY *keyinfo= tab->table->key_info+nr;
4761
13088
          if (select_limit == HA_POS_ERROR)
4762
13089
            select_limit= table_records;
4763
13090
          if (group)
4764
13091
          {
4765
13092
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4766
 
            set_if_bigger(rec_per_key, 1.0);
 
13093
            set_if_bigger(rec_per_key, 1);
4767
13094
            /*
4768
13095
              With a grouping query each group containing on average
4769
13096
              rec_per_key records produces only one row that will
4770
13097
              be included into the result set.
4771
 
            */
 
13098
            */  
4772
13099
            if (select_limit > table_records/rec_per_key)
4773
13100
                select_limit= table_records;
4774
13101
            else
4775
13102
              select_limit= (ha_rows) (select_limit*rec_per_key);
4776
13103
          }
4777
 
          /*
 
13104
          /* 
4778
13105
            If tab=tk is not the last joined table tn then to get first
4779
13106
            L records from the result set we can expect to retrieve
4780
13107
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4783
13110
            So the estimate for L/fanout(tk,tn) will be too optimistic
4784
13111
            and as result we'll choose an index scan when using ref/range
4785
13112
            access + filesort will be cheaper.
4786
 
          */
 
13113
          */
4787
13114
          select_limit= (ha_rows) (select_limit < fanout ?
4788
13115
                                   1 : select_limit/fanout);
4789
13116
          /*
4790
13117
            We assume that each of the tested indexes is not correlated
4791
13118
            with ref_key. Thus, to select first N records we have to scan
4792
 
            N/selectivity(ref_key) index entries.
 
13119
            N/selectivity(ref_key) index entries. 
4793
13120
            selectivity(ref_key) = #scanned_records/#table_records =
4794
13121
            table->quick_condition_rows/table_records.
4795
13122
            In any case we can't select more than #table_records.
4796
 
            N/(table->quick_condition_rows/table_records) > table_records
 
13123
            N/(table->quick_condition_rows/table_records) > table_records 
4797
13124
            <=> N > table->quick_condition_rows.
4798
 
          */
 
13125
          */ 
4799
13126
          if (select_limit > table->quick_condition_rows)
4800
13127
            select_limit= table_records;
4801
13128
          else
4803
13130
                                     (double) table_records /
4804
13131
                                      table->quick_condition_rows);
4805
13132
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4806
 
          set_if_bigger(rec_per_key, 1.0);
 
13133
          set_if_bigger(rec_per_key, 1);
4807
13134
          /*
4808
13135
            Here we take into account the fact that rows are
4809
13136
            accessed in sequences rec_per_key records in each.
4810
13137
            Rows in such a sequence are supposed to be ordered
4811
13138
            by rowid/primary key. When reading the data
4812
13139
            in a sequence we'll touch not more pages than the
4813
 
            table cursor contains.
 
13140
            table file contains.
4814
13141
            TODO. Use the formula for a disk sweep sequential access
4815
 
            to calculate the cost of accessing data rows for one
 
13142
            to calculate the cost of accessing data rows for one 
4816
13143
            index entry.
4817
 
          */
 
13144
          */
4818
13145
          index_scan_time= select_limit/rec_per_key *
4819
 
                           min(rec_per_key, table->cursor->scan_time());
 
13146
                           min(rec_per_key, table->file->scan_time());
4820
13147
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4821
13148
              index_scan_time < read_time)
4822
13149
          {
4823
13150
            ha_rows quick_records= table_records;
4824
13151
            if (is_best_covering && !is_covering)
4825
13152
              continue;
4826
 
            if (table->quick_keys.test(nr))
 
13153
            if (table->quick_keys.is_set(nr))
4827
13154
              quick_records= table->quick_rows[nr];
4828
13155
            if (best_key < 0 ||
4829
13156
                (select_limit <= min(quick_records,best_records) ?
4834
13161
              best_key_parts= keyinfo->key_parts;
4835
13162
              best_records= quick_records;
4836
13163
              is_best_covering= is_covering;
4837
 
              best_key_direction= direction;
 
13164
              best_key_direction= direction; 
4838
13165
            }
4839
 
          }
4840
 
        }
 
13166
          }   
 
13167
        }      
4841
13168
      }
4842
13169
    }
4843
13170
    if (best_key >= 0)
4844
13171
    {
4845
13172
      bool quick_created= false;
4846
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13173
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4847
13174
      {
4848
 
        key_map test_map;
4849
 
        test_map.reset();       // Force the creation of quick select
4850
 
        test_map.set(best_key); // only best_key.
4851
 
        quick_created=
4852
 
          select->test_quick_select(join->session, test_map, 0,
 
13175
        key_map map;
 
13176
        map.clear_all();       // Force the creation of quick select
 
13177
        map.set_bit(best_key); // only best_key.
 
13178
        quick_created=         
 
13179
          select->test_quick_select(join->thd, map, 0,
4853
13180
                                    join->select_options & OPTION_FOUND_ROWS ?
4854
13181
                                    HA_POS_ERROR :
4855
13182
                                    join->unit->select_limit_cnt,
4858
13185
      if (!no_changes)
4859
13186
      {
4860
13187
        if (!quick_created)
4861
 
        {
 
13188
        {
4862
13189
          tab->index= best_key;
4863
13190
          tab->read_first_record= best_key_direction > 0 ?
4864
13191
                                  join_read_first:join_read_last;
4865
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13192
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4866
13193
          if (select && select->quick)
4867
13194
          {
4868
13195
            delete select->quick;
4869
13196
            select->quick= 0;
4870
13197
          }
4871
 
          if (table->covering_keys.test(best_key))
 
13198
          if (table->covering_keys.is_set(best_key))
4872
13199
          {
4873
13200
            table->key_read=1;
4874
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13201
            table->file->extra(HA_EXTRA_KEYREAD);
4875
13202
          }
4876
 
          table->cursor->ha_index_or_rnd_end();
 
13203
          table->file->ha_index_or_rnd_end();
4877
13204
          if (join->select_options & SELECT_DESCRIBE)
4878
13205
          {
4879
13206
            tab->ref.key= -1;
4880
13207
            tab->ref.key_parts= 0;
4881
 
            if (select_limit < table_records)
 
13208
            if (select_limit < table_records) 
4882
13209
              tab->limit= select_limit;
4883
13210
          }
4884
13211
        }
4885
 
        else if (tab->type != AM_ALL)
 
13212
        else if (tab->type != JT_ALL)
4886
13213
        {
4887
13214
          /*
4888
13215
            We're about to use a quick access to the table.
4890
13217
            method is actually used.
4891
13218
          */
4892
13219
          assert(tab->select->quick);
4893
 
          tab->type= AM_ALL;
 
13220
          tab->type=JT_ALL;
4894
13221
          tab->use_quick=1;
4895
13222
          tab->ref.key= -1;
4896
13223
          tab->ref.key_parts=0;         // Don't use ref key.
4897
13224
          tab->read_first_record= join_init_read_record;
 
13225
          /*
 
13226
            TODO: update the number of records in join->best_positions[tablenr]
 
13227
          */
4898
13228
        }
4899
13229
      }
4900
13230
      used_key_parts= best_key_parts;
4901
13231
      order_direction= best_key_direction;
4902
13232
    }
4903
13233
    else
4904
 
      return(0);
4905
 
  }
 
13234
      return(0); 
 
13235
  } 
4906
13236
 
4907
 
check_reverse_order:
4908
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13237
check_reverse_order:                  
 
13238
  if (order_direction == -1)            // If order_st BY ... DESC
4909
13239
  {
4910
13240
    if (select && select->quick)
4911
13241
    {
4912
13242
      /*
4913
 
        Don't reverse the sort order, if it's already done.
 
13243
        Don't reverse the sort order, if it's already done.
4914
13244
        (In some cases test_if_order_by_key() can be called multiple times
4915
13245
      */
4916
 
      if (! select->quick->reverse_sorted())
 
13246
      if (!select->quick->reverse_sorted())
4917
13247
      {
4918
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13248
        QUICK_SELECT_DESC *tmp;
4919
13249
        bool error= false;
4920
13250
        int quick_type= select->quick->get_type();
4921
 
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4922
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
4923
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4924
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
4925
 
        {
4926
 
          tab->limit= 0;
4927
 
          select->quick= save_quick;
4928
 
          return 0; // Use filesort
4929
 
        }
4930
 
 
4931
 
        /* ORDER BY range_key DESC */
4932
 
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
4933
 
                                                  used_key_parts, 
4934
 
                                                  &error);
4935
 
        if (! tmp || error)
4936
 
        {
4937
 
          delete tmp;
4938
 
          select->quick= save_quick;
4939
 
          tab->limit= 0;
4940
 
          return 0; // Reverse sort not supported
4941
 
        }
4942
 
        select->quick=tmp;
 
13251
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13252
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13253
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13254
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
13255
        {
 
13256
          tab->limit= 0;
 
13257
          select->quick= save_quick;
 
13258
          return(0);                   // Use filesort
 
13259
        }
 
13260
            
 
13261
        /* order_st BY range_key DESC */
 
13262
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13263
                                    used_key_parts, &error);
 
13264
        if (!tmp || error)
 
13265
        {
 
13266
          delete tmp;
 
13267
          select->quick= save_quick;
 
13268
          tab->limit= 0;
 
13269
          return(0);            // Reverse sort not supported
 
13270
        }
 
13271
        select->quick=tmp;
4943
13272
      }
4944
13273
    }
4945
 
    else if (tab->type != AM_NEXT &&
 
13274
    else if (tab->type != JT_NEXT && 
4946
13275
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4947
13276
    {
4948
13277
      /*
4949
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13278
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4950
13279
 
4951
 
        Use a traversal function that starts by reading the last row
4952
 
        with key part (A) and then traverse the index backwards.
 
13280
        Use a traversal function that starts by reading the last row
 
13281
        with key part (A) and then traverse the index backwards.
4953
13282
      */
4954
13283
      tab->read_first_record= join_read_last_key;
4955
13284
      tab->read_record.read_record= join_read_prev_same;
4957
13286
  }
4958
13287
  else if (select && select->quick)
4959
13288
    select->quick->sorted= 1;
4960
 
  return 1;
 
13289
  return(1);
4961
13290
}
4962
13291
 
 
13292
 
4963
13293
/*
4964
13294
  If not selecting by given key, create an index how records should be read
4965
13295
 
4966
13296
  SYNOPSIS
4967
13297
   create_sort_index()
4968
 
     session            Thread Cursor
 
13298
     thd                Thread handler
4969
13299
     tab                Table to sort (in join structure)
4970
13300
     order              How table should be sorted
4971
13301
     filesort_limit     Max number of rows that needs to be sorted
4972
13302
     select_limit       Max number of rows in final output
4973
13303
                        Used to decide if we should use index or not
4974
13304
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
4975
 
                        Used to decide if we should use index or not
 
13305
                        Used to decide if we should use index or not     
4976
13306
 
4977
13307
 
4978
13308
  IMPLEMENTATION
4979
13309
   - If there is an index that can be used, 'tab' is modified to use
4980
13310
     this index.
4981
 
   - If no index, create with filesort() an index cursor that can be used to
 
13311
   - If no index, create with filesort() an index file that can be used to
4982
13312
     retrieve rows in order (should be done with 'read_record').
4983
13313
     The sorted data is stored in tab->table and will be freed when calling
4984
 
     tab->table->free_io_cache().
 
13314
     free_io_cache(tab->table).
4985
13315
 
4986
13316
  RETURN VALUES
4987
13317
    0           ok
4988
13318
    -1          Some fatal error
4989
13319
    1           No records
4990
13320
*/
4991
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13321
 
 
13322
static int
 
13323
create_sort_index(THD *thd, JOIN *join, order_st *order,
 
13324
                  ha_rows filesort_limit, ha_rows select_limit,
 
13325
                  bool is_order_by)
4992
13326
{
4993
 
  uint32_t length= 0;
 
13327
  uint length= 0;
4994
13328
  ha_rows examined_rows;
4995
13329
  Table *table;
4996
 
  optimizer::SqlSelect *select= NULL;
4997
 
  JoinTable *tab;
 
13330
  SQL_SELECT *select;
 
13331
  JOIN_TAB *tab;
4998
13332
 
4999
13333
  if (join->tables == join->const_tables)
5000
13334
    return(0);                          // One row, no need to sort
5008
13342
    is going to be used as it is applied now only for one table queries
5009
13343
    with covering indexes.
5010
13344
  */
5011
 
  if ((order != join->group_list ||
 
13345
  if ((order != join->group_list || 
5012
13346
       !(join->select_options & SELECT_BIG_RESULT) ||
5013
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
5014
 
      test_if_skip_sort_order(tab,order,select_limit,0,
 
13347
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
13348
      test_if_skip_sort_order(tab,order,select_limit,0, 
5015
13349
                              is_order_by ?  &table->keys_in_use_for_order_by :
5016
13350
                              &table->keys_in_use_for_group_by))
5017
13351
    return(0);
5018
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13352
  for (order_st *ord= join->order; ord; ord= ord->next)
5019
13353
    length++;
5020
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
5021
 
  {
5022
 
    return(-1);
5023
 
  }
 
13354
  if (!(join->sortorder= 
 
13355
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13356
    goto err;                           /* purecov: inspected */
5024
13357
 
5025
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13358
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
13359
                                             MYF(MY_WME | MY_ZEROFILL));
5026
13360
  table->status=0;                              // May be wrong if quick_select
5027
13361
 
5028
13362
  // If table has a range, move it to select
5032
13366
    {
5033
13367
      select->quick=tab->quick;
5034
13368
      tab->quick=0;
5035
 
      /*
 
13369
      /* 
5036
13370
        We can only use 'Only index' if quick key is same as ref_key
5037
13371
        and in index_merge 'Only index' cannot be used
5038
13372
      */
5039
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
13373
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
5040
13374
      {
5041
 
        table->key_read=0;
5042
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13375
        table->key_read=0;
 
13376
        table->file->extra(HA_EXTRA_NO_KEYREAD);
5043
13377
      }
5044
13378
    }
5045
13379
    else
5046
13380
    {
5047
13381
      /*
5048
 
        We have a ref on a const;  Change this to a range that filesort
5049
 
        can use.
5050
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
5051
 
        field, quick will contain an empty record set.
 
13382
        We have a ref on a const;  Change this to a range that filesort
 
13383
        can use.
 
13384
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13385
        field, quick will contain an empty record set.
5052
13386
      */
5053
 
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session, 
5054
 
                                                                 table, 
5055
 
                                                                 &tab->ref,
5056
 
                                                                 tab->found_records))))
5057
 
      {
5058
 
        return(-1);
5059
 
      }
 
13387
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
13388
                                                     tab->found_records))))
 
13389
        goto err;
5060
13390
    }
5061
13391
  }
5062
13392
 
5063
 
  if (table->getShare()->getType())
5064
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13393
  /* Fill schema tables with data before filesort if it's necessary */
 
13394
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13395
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13396
    goto err;
5065
13397
 
5066
 
  FileSort filesort(*session);
5067
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5068
 
                                         select, filesort_limit, 0,
5069
 
                                         examined_rows);
 
13398
  if (table->s->tmp_table)
 
13399
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13400
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
13401
                                     select, filesort_limit, 0,
 
13402
                                     &examined_rows);
5070
13403
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5071
13404
  if (select)
5072
13405
  {
5076
13409
  tab->select_cond=0;
5077
13410
  tab->last_inner= 0;
5078
13411
  tab->first_unmatched= 0;
5079
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13412
  tab->type=JT_ALL;                             // Read with normal read_record
5080
13413
  tab->read_first_record= join_init_read_record;
5081
13414
  tab->join->examined_rows+=examined_rows;
5082
13415
  if (table->key_read)                          // Restore if we used indexes
5083
13416
  {
5084
13417
    table->key_read=0;
5085
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13418
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5086
13419
  }
5087
 
 
5088
13420
  return(table->sort.found_records == HA_POS_ERROR);
5089
 
}
5090
 
 
5091
 
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5092
 
{
5093
 
  Cursor *cursor=table->cursor;
 
13421
err:
 
13422
  return(-1);
 
13423
}
 
13424
 
 
13425
static bool copy_blobs(Field **ptr)
 
13426
{
 
13427
  for (; *ptr ; ptr++)
 
13428
  {
 
13429
    if ((*ptr)->flags & BLOB_FLAG)
 
13430
      if (((Field_blob *) (*ptr))->copy())
 
13431
        return 1;                               // Error
 
13432
  }
 
13433
  return 0;
 
13434
}
 
13435
 
 
13436
static void free_blobs(Field **ptr)
 
13437
{
 
13438
  for (; *ptr ; ptr++)
 
13439
  {
 
13440
    if ((*ptr)->flags & BLOB_FLAG)
 
13441
      ((Field_blob *) (*ptr))->free();
 
13442
  }
 
13443
}
 
13444
 
 
13445
 
 
13446
static int
 
13447
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13448
{
 
13449
  int error;
 
13450
  ulong reclength,offset;
 
13451
  uint field_count;
 
13452
  THD *thd= join->thd;
 
13453
 
 
13454
  entry->reginfo.lock_type=TL_WRITE;
 
13455
 
 
13456
  /* Calculate how many saved fields there is in list */
 
13457
  field_count=0;
 
13458
  List_iterator<Item> it(fields);
 
13459
  Item *item;
 
13460
  while ((item=it++))
 
13461
  {
 
13462
    if (item->get_tmp_table_field() && ! item->const_item())
 
13463
      field_count++;
 
13464
  }
 
13465
 
 
13466
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
13467
  {                    // only const items with no OPTION_FOUND_ROWS
 
13468
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13469
    return(0);
 
13470
  }
 
13471
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13472
  offset= (field_count ? 
 
13473
           entry->field[entry->s->fields - field_count]->
 
13474
           offset(entry->record[0]) : 0);
 
13475
  reclength=entry->s->reclength-offset;
 
13476
 
 
13477
  free_io_cache(entry);                         // Safety
 
13478
  entry->file->info(HA_STATUS_VARIABLE);
 
13479
  if (entry->s->db_type() == heap_hton ||
 
13480
      (!entry->s->blob_fields &&
 
13481
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13482
        thd->variables.sortbuff_size)))
 
13483
    error=remove_dup_with_hash_index(join->thd, entry,
 
13484
                                     field_count, first_field,
 
13485
                                     reclength, having);
 
13486
  else
 
13487
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
13488
                                  having);
 
13489
 
 
13490
  free_blobs(first_field);
 
13491
  return(error);
 
13492
}
 
13493
 
 
13494
 
 
13495
static int remove_dup_with_compare(THD *thd, Table *table, Field **first_field,
 
13496
                                   ulong offset, Item *having)
 
13497
{
 
13498
  handler *file=table->file;
5094
13499
  char *org_record,*new_record;
5095
 
  unsigned char *record;
 
13500
  uchar *record;
5096
13501
  int error;
5097
 
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5098
 
 
5099
 
  org_record=(char*) (record=table->getInsertRecord())+offset;
5100
 
  new_record=(char*) table->getUpdateRecord()+offset;
5101
 
 
5102
 
  cursor->startTableScan(1);
5103
 
  error=cursor->rnd_next(record);
 
13502
  ulong reclength= table->s->reclength-offset;
 
13503
 
 
13504
  org_record=(char*) (record=table->record[0])+offset;
 
13505
  new_record=(char*) table->record[1]+offset;
 
13506
 
 
13507
  file->ha_rnd_init(1);
 
13508
  error=file->rnd_next(record);
5104
13509
  for (;;)
5105
13510
  {
5106
 
    if (session->getKilled())
 
13511
    if (thd->killed)
5107
13512
    {
5108
 
      session->send_kill_message();
 
13513
      thd->send_kill_message();
5109
13514
      error=0;
5110
13515
      goto err;
5111
13516
    }
5112
13517
    if (error)
5113
13518
    {
5114
13519
      if (error == HA_ERR_RECORD_DELETED)
5115
 
        continue;
 
13520
        continue;
5116
13521
      if (error == HA_ERR_END_OF_FILE)
5117
 
        break;
 
13522
        break;
5118
13523
      goto err;
5119
13524
    }
5120
13525
    if (having && !having->val_int())
5121
13526
    {
5122
 
      if ((error=cursor->deleteRecord(record)))
5123
 
        goto err;
5124
 
      error=cursor->rnd_next(record);
 
13527
      if ((error=file->ha_delete_row(record)))
 
13528
        goto err;
 
13529
      error=file->rnd_next(record);
5125
13530
      continue;
5126
13531
    }
5127
13532
    if (copy_blobs(first_field))
5132
13537
    }
5133
13538
    memcpy(new_record,org_record,reclength);
5134
13539
 
5135
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13540
    /* Read through rest of file and mark duplicated rows deleted */
5136
13541
    bool found=0;
5137
13542
    for (;;)
5138
13543
    {
5139
 
      if ((error=cursor->rnd_next(record)))
 
13544
      if ((error=file->rnd_next(record)))
5140
13545
      {
5141
 
        if (error == HA_ERR_RECORD_DELETED)
5142
 
          continue;
5143
 
        if (error == HA_ERR_END_OF_FILE)
5144
 
          break;
5145
 
        goto err;
 
13546
        if (error == HA_ERR_RECORD_DELETED)
 
13547
          continue;
 
13548
        if (error == HA_ERR_END_OF_FILE)
 
13549
          break;
 
13550
        goto err;
5146
13551
      }
5147
13552
      if (table->compare_record(first_field) == 0)
5148
13553
      {
5149
 
        if ((error=cursor->deleteRecord(record)))
5150
 
          goto err;
 
13554
        if ((error=file->ha_delete_row(record)))
 
13555
          goto err;
5151
13556
      }
5152
13557
      else if (!found)
5153
13558
      {
5154
 
        found= 1;
5155
 
        cursor->position(record);       // Remember position
 
13559
        found=1;
 
13560
        file->position(record); // Remember position
5156
13561
      }
5157
13562
    }
5158
13563
    if (!found)
5159
 
      break;                                    // End of cursor
5160
 
    /* Move current position to the next row */
5161
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13564
      break;                                    // End of file
 
13565
    /* Restart search on next row */
 
13566
    error=file->restart_rnd_next(record,file->ref);
5162
13567
  }
5163
13568
 
5164
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13569
  file->extra(HA_EXTRA_NO_CACHE);
5165
13570
  return(0);
5166
13571
err:
5167
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13572
  file->extra(HA_EXTRA_NO_CACHE);
5168
13573
  if (error)
5169
 
    table->print_error(error,MYF(0));
 
13574
    file->print_error(error,MYF(0));
5170
13575
  return(1);
5171
13576
}
5172
13577
 
 
13578
 
5173
13579
/**
5174
13580
  Generate a hash index for each row to quickly find duplicate rows.
5175
13581
 
5176
13582
  @note
5177
13583
    Note that this will not work on tables with blobs!
5178
13584
*/
5179
 
int remove_dup_with_hash_index(Session *session, 
5180
 
                               Table *table,
5181
 
                               uint32_t field_count,
5182
 
                               Field **first_field,
5183
 
                               uint32_t key_length,
5184
 
                               Item *having)
 
13585
 
 
13586
static int remove_dup_with_hash_index(THD *thd, Table *table,
 
13587
                                      uint field_count,
 
13588
                                      Field **first_field,
 
13589
                                      ulong key_length,
 
13590
                                      Item *having)
5185
13591
{
5186
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13592
  uchar *key_buffer, *key_pos, *record=table->record[0];
5187
13593
  int error;
5188
 
  Cursor *cursor= table->cursor;
5189
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5190
 
  uint32_t *field_length;
 
13594
  handler *file= table->file;
 
13595
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
13596
  uint *field_lengths,*field_length;
5191
13597
  HASH hash;
5192
 
  std::vector<unsigned char> key_buffer;
5193
 
  std::vector<uint32_t> field_lengths;
5194
13598
 
5195
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5196
 
  field_lengths.resize(field_count);
 
13599
  if (!my_multi_malloc(MYF(MY_WME),
 
13600
                       &key_buffer,
 
13601
                       (uint) ((key_length + extra_length) *
 
13602
                               (long) file->stats.records),
 
13603
                       &field_lengths,
 
13604
                       (uint) (field_count*sizeof(*field_lengths)),
 
13605
                       NullS))
 
13606
    return(1);
5197
13607
 
5198
13608
  {
5199
13609
    Field **ptr;
5200
 
    uint32_t total_length= 0;
5201
 
 
5202
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13610
    ulong total_length= 0;
 
13611
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5203
13612
    {
5204
 
      uint32_t length= (*ptr)->sort_length();
 
13613
      uint length= (*ptr)->sort_length();
5205
13614
      (*field_length++)= length;
5206
13615
      total_length+= length;
5207
13616
    }
5210
13619
    extra_length= ALIGN_SIZE(key_length)-key_length;
5211
13620
  }
5212
13621
 
5213
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13622
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5214
13623
                key_length, (hash_get_key) 0, 0, 0))
5215
13624
  {
 
13625
    my_free((char*) key_buffer,MYF(0));
5216
13626
    return(1);
5217
13627
  }
5218
13628
 
5219
 
  cursor->startTableScan(1);
5220
 
  key_pos= &key_buffer[0];
 
13629
  file->ha_rnd_init(1);
 
13630
  key_pos=key_buffer;
5221
13631
  for (;;)
5222
13632
  {
5223
 
    unsigned char *org_key_pos;
5224
 
    if (session->getKilled())
 
13633
    uchar *org_key_pos;
 
13634
    if (thd->killed)
5225
13635
    {
5226
 
      session->send_kill_message();
 
13636
      thd->send_kill_message();
5227
13637
      error=0;
5228
13638
      goto err;
5229
13639
    }
5230
 
    if ((error=cursor->rnd_next(record)))
 
13640
    if ((error=file->rnd_next(record)))
5231
13641
    {
5232
13642
      if (error == HA_ERR_RECORD_DELETED)
5233
 
        continue;
 
13643
        continue;
5234
13644
      if (error == HA_ERR_END_OF_FILE)
5235
 
        break;
 
13645
        break;
5236
13646
      goto err;
5237
13647
    }
5238
13648
    if (having && !having->val_int())
5239
13649
    {
5240
 
      if ((error=cursor->deleteRecord(record)))
5241
 
        goto err;
 
13650
      if ((error=file->ha_delete_row(record)))
 
13651
        goto err;
5242
13652
      continue;
5243
13653
    }
5244
13654
 
5245
13655
    /* copy fields to key buffer */
5246
13656
    org_key_pos= key_pos;
5247
 
    field_length= &field_lengths[0];
 
13657
    field_length=field_lengths;
5248
13658
    for (Field **ptr= first_field ; *ptr ; ptr++)
5249
13659
    {
5250
13660
      (*ptr)->sort_string(key_pos,*field_length);
5254
13664
    if (hash_search(&hash, org_key_pos, key_length))
5255
13665
    {
5256
13666
      /* Duplicated found ; Remove the row */
5257
 
      if ((error=cursor->deleteRecord(record)))
5258
 
        goto err;
 
13667
      if ((error=file->ha_delete_row(record)))
 
13668
        goto err;
5259
13669
    }
5260
13670
    else
5261
13671
      (void) my_hash_insert(&hash, org_key_pos);
5262
13672
    key_pos+=extra_length;
5263
13673
  }
 
13674
  my_free((char*) key_buffer,MYF(0));
5264
13675
  hash_free(&hash);
5265
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5266
 
  (void) cursor->endTableScan();
 
13676
  file->extra(HA_EXTRA_NO_CACHE);
 
13677
  (void) file->ha_rnd_end();
5267
13678
  return(0);
5268
13679
 
5269
13680
err:
 
13681
  my_free((char*) key_buffer,MYF(0));
5270
13682
  hash_free(&hash);
5271
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5272
 
  (void) cursor->endTableScan();
 
13683
  file->extra(HA_EXTRA_NO_CACHE);
 
13684
  (void) file->ha_rnd_end();
5273
13685
  if (error)
5274
 
    table->print_error(error,MYF(0));
 
13686
    file->print_error(error,MYF(0));
5275
13687
  return(1);
5276
13688
}
5277
13689
 
5278
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13690
 
 
13691
SORT_FIELD *make_unireg_sortorder(order_st *order, uint *length,
 
13692
                                  SORT_FIELD *sortorder)
5279
13693
{
5280
 
  uint32_t count;
5281
 
  SortField *sort,*pos;
 
13694
  uint count;
 
13695
  SORT_FIELD *sort,*pos;
5282
13696
 
5283
13697
  count=0;
5284
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13698
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5285
13699
    count++;
5286
13700
  if (!sortorder)
5287
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
13701
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
5288
13702
                                       (max(count, *length) + 1));
5289
13703
  pos= sort= sortorder;
5290
13704
 
5311
13725
  return(sort);
5312
13726
}
5313
13727
 
 
13728
 
 
13729
/*****************************************************************************
 
13730
  Fill join cache with packed records
 
13731
  Records are stored in tab->cache.buffer and last record in
 
13732
  last record is stored with pointers to blobs to support very big
 
13733
  records
 
13734
******************************************************************************/
 
13735
 
 
13736
static int
 
13737
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
 
13738
{
 
13739
  register unsigned int i;
 
13740
  unsigned int length, blobs;
 
13741
  size_t size;
 
13742
  CACHE_FIELD *copy,**blob_ptr;
 
13743
  JOIN_CACHE  *cache;
 
13744
  JOIN_TAB *join_tab;
 
13745
 
 
13746
  cache= &tables[table_count].cache;
 
13747
  cache->fields=blobs=0;
 
13748
 
 
13749
  join_tab=tables;
 
13750
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13751
  {
 
13752
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13753
      calc_used_field_length(thd, join_tab);
 
13754
    cache->fields+=join_tab->used_fields;
 
13755
    blobs+=join_tab->used_blobs;
 
13756
 
 
13757
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13758
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13759
    {
 
13760
      cache->fields++;
 
13761
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13762
    }
 
13763
  }
 
13764
  if (!(cache->field=(CACHE_FIELD*)
 
13765
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13766
 
 
13767
                  sizeof(CACHE_FIELD*))))
 
13768
  {
 
13769
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
 
13770
    cache->buff=0;                              /* purecov: inspected */
 
13771
    return(1);                          /* purecov: inspected */
 
13772
  }
 
13773
  copy=cache->field;
 
13774
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13775
    (cache->field+cache->fields+table_count*2);
 
13776
 
 
13777
  length=0;
 
13778
  for (i=0 ; i < table_count ; i++)
 
13779
  {
 
13780
    uint null_fields=0, used_fields;
 
13781
    Field **f_ptr,*field;
 
13782
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13783
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13784
         used_fields ;
 
13785
         f_ptr++)
 
13786
    {
 
13787
      field= *f_ptr;
 
13788
      if (bitmap_is_set(read_set, field->field_index))
 
13789
      {
 
13790
        used_fields--;
 
13791
        length+=field->fill_cache_field(copy);
 
13792
        if (copy->blob_field)
 
13793
          (*blob_ptr++)=copy;
 
13794
        if (field->maybe_null())
 
13795
          null_fields++;
 
13796
        copy->get_rowid= NULL;
 
13797
        copy++;
 
13798
      }
 
13799
    }
 
13800
    /* Copy null bits from table */
 
13801
    if (null_fields && tables[i].table->getNullFields())
 
13802
    {                                           /* must copy null bits */
 
13803
      copy->str= tables[i].table->null_flags;
 
13804
      copy->length= tables[i].table->s->null_bytes;
 
13805
      copy->strip=0;
 
13806
      copy->blob_field=0;
 
13807
      copy->get_rowid= NULL;
 
13808
      length+=copy->length;
 
13809
      copy++;
 
13810
      cache->fields++;
 
13811
    }
 
13812
    /* If outer join table, copy null_row flag */
 
13813
    if (tables[i].table->maybe_null)
 
13814
    {
 
13815
      copy->str= (uchar*) &tables[i].table->null_row;
 
13816
      copy->length=sizeof(tables[i].table->null_row);
 
13817
      copy->strip=0;
 
13818
      copy->blob_field=0;
 
13819
      copy->get_rowid= NULL;
 
13820
      length+=copy->length;
 
13821
      copy++;
 
13822
      cache->fields++;
 
13823
    }
 
13824
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13825
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13826
    {
 
13827
      copy->str= tables[i].table->file->ref;
 
13828
      copy->length= tables[i].table->file->ref_length;
 
13829
      copy->strip=0;
 
13830
      copy->blob_field=0;
 
13831
      copy->get_rowid= NULL;
 
13832
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13833
      {
 
13834
        /* We will need to call h->position(): */
 
13835
        copy->get_rowid= tables[i].table;
 
13836
        /* And those after us won't have to: */
 
13837
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13838
      }
 
13839
      copy++;
 
13840
    }
 
13841
  }
 
13842
 
 
13843
  cache->length=length+blobs*sizeof(char*);
 
13844
  cache->blobs=blobs;
 
13845
  *blob_ptr=0;                                  /* End sequentel */
 
13846
  size=max(thd->variables.join_buff_size, (ulong)cache->length);
 
13847
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
13848
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
13849
  cache->end=cache->buff+size;
 
13850
  reset_cache_write(cache);
 
13851
  return(0);
 
13852
}
 
13853
 
 
13854
 
 
13855
static ulong
 
13856
used_blob_length(CACHE_FIELD **ptr)
 
13857
{
 
13858
  uint length,blob_length;
 
13859
  for (length=0 ; *ptr ; ptr++)
 
13860
  {
 
13861
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13862
    length+=blob_length;
 
13863
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13864
  }
 
13865
  return length;
 
13866
}
 
13867
 
 
13868
 
 
13869
static bool
 
13870
store_record_in_cache(JOIN_CACHE *cache)
 
13871
{
 
13872
  uint length;
 
13873
  uchar *pos;
 
13874
  CACHE_FIELD *copy,*end_field;
 
13875
  bool last_record;
 
13876
 
 
13877
  pos=cache->pos;
 
13878
  end_field=cache->field+cache->fields;
 
13879
 
 
13880
  length=cache->length;
 
13881
  if (cache->blobs)
 
13882
    length+=used_blob_length(cache->blob_ptr);
 
13883
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13884
    cache->ptr_record=cache->records;
 
13885
  /*
 
13886
    There is room in cache. Put record there
 
13887
  */
 
13888
  cache->records++;
 
13889
  for (copy=cache->field ; copy < end_field; copy++)
 
13890
  {
 
13891
    if (copy->blob_field)
 
13892
    {
 
13893
      if (last_record)
 
13894
      {
 
13895
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
13896
                                    copy->blob_field->charset());
 
13897
        pos+=copy->length+sizeof(char*);
 
13898
      }
 
13899
      else
 
13900
      {
 
13901
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13902
                                    copy->blob_field->charset());
 
13903
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13904
        pos+=copy->length+copy->blob_length;
 
13905
      }
 
13906
    }
 
13907
    else
 
13908
    {
 
13909
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13910
      if (copy->get_rowid)
 
13911
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13912
 
 
13913
      if (copy->strip)
 
13914
      {
 
13915
        uchar *str,*end;
 
13916
        for (str=copy->str,end= str+copy->length;
 
13917
             end > str && end[-1] == ' ' ;
 
13918
             end--) ;
 
13919
        length=(uint) (end-str);
 
13920
        memcpy(pos+2, str, length);
 
13921
        int2store(pos, length);
 
13922
        pos+= length+2;
 
13923
      }
 
13924
      else
 
13925
      {
 
13926
        memcpy(pos,copy->str,copy->length);
 
13927
        pos+=copy->length;
 
13928
      }
 
13929
    }
 
13930
  }
 
13931
  cache->pos=pos;
 
13932
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13933
}
 
13934
 
 
13935
 
 
13936
static void
 
13937
reset_cache_read(JOIN_CACHE *cache)
 
13938
{
 
13939
  cache->record_nr=0;
 
13940
  cache->pos=cache->buff;
 
13941
}
 
13942
 
 
13943
 
 
13944
static void reset_cache_write(JOIN_CACHE *cache)
 
13945
{
 
13946
  reset_cache_read(cache);
 
13947
  cache->records= 0;
 
13948
  cache->ptr_record= UINT32_MAX;
 
13949
}
 
13950
 
 
13951
 
 
13952
static void
 
13953
read_cached_record(JOIN_TAB *tab)
 
13954
{
 
13955
  uchar *pos;
 
13956
  uint length;
 
13957
  bool last_record;
 
13958
  CACHE_FIELD *copy,*end_field;
 
13959
 
 
13960
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13961
  pos=tab->cache.pos;
 
13962
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13963
       copy < end_field;
 
13964
       copy++)
 
13965
  {
 
13966
    if (copy->blob_field)
 
13967
    {
 
13968
      if (last_record)
 
13969
      {
 
13970
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13971
                                    copy->blob_field->charset());
 
13972
        pos+=copy->length+sizeof(char*);
 
13973
      }
 
13974
      else
 
13975
      {
 
13976
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
13977
        pos+=copy->length+copy->blob_field->get_length();
 
13978
      }
 
13979
    }
 
13980
    else
 
13981
    {
 
13982
      if (copy->strip)
 
13983
      {
 
13984
        length= uint2korr(pos);
 
13985
        memcpy(copy->str, pos+2, length);
 
13986
        memset(copy->str+length, ' ', copy->length-length);
 
13987
        pos+= 2 + length;
 
13988
      }
 
13989
      else
 
13990
      {
 
13991
        memcpy(copy->str,pos,copy->length);
 
13992
        pos+=copy->length;
 
13993
      }
 
13994
    }
 
13995
  }
 
13996
  tab->cache.pos=pos;
 
13997
  return;
 
13998
}
 
13999
 
 
14000
 
5314
14001
/*
5315
14002
  eq_ref: Create the lookup key and check if it is the same as saved key
5316
14003
 
5317
14004
  SYNOPSIS
5318
14005
    cmp_buffer_with_ref()
5319
14006
      tab  Join tab of the accessed table
5320
 
 
5321
 
  DESCRIPTION
5322
 
    Used by eq_ref access method: create the index lookup key and check if
 
14007
 
 
14008
  DESCRIPTION 
 
14009
    Used by eq_ref access method: create the index lookup key and check if 
5323
14010
    we've used this key at previous lookup (If yes, we don't need to repeat
5324
14011
    the lookup - the record has been already fetched)
5325
14012
 
5326
 
  RETURN
 
14013
  RETURN 
5327
14014
    true   No cached record for the key, or failed to create the key (due to
5328
14015
           out-of-domain error)
5329
 
    false  The created key is the same as the previous one (and the record
 
14016
    false  The created key is the same as the previous one (and the record 
5330
14017
           is already in table->record)
5331
14018
*/
5332
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14019
 
 
14020
static bool
 
14021
cmp_buffer_with_ref(JOIN_TAB *tab)
5333
14022
{
5334
14023
  bool no_prev_key;
5335
14024
  if (!tab->ref.disable_cache)
5340
14029
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5341
14030
    }
5342
14031
  }
5343
 
  else
 
14032
  else 
5344
14033
    no_prev_key= true;
5345
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
14034
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, &tab->ref)) ||
5346
14035
      no_prev_key)
5347
14036
    return 1;
5348
14037
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5349
14038
    != 0;
5350
14039
}
5351
14040
 
5352
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14041
 
 
14042
bool
 
14043
cp_buffer_from_ref(THD *thd, TABLE_REF *ref)
5353
14044
{
5354
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5355
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
14045
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
14046
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
5356
14047
  bool result= 0;
5357
14048
 
5358
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14049
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5359
14050
  {
5360
14051
    if ((*copy)->copy() & 1)
5361
14052
    {
5363
14054
      break;
5364
14055
    }
5365
14056
  }
5366
 
  session->count_cuted_fields= save_count_cuted_fields;
 
14057
  thd->count_cuted_fields= save_count_cuted_fields;
5367
14058
  return result;
5368
14059
}
5369
14060
 
 
14061
 
5370
14062
/*****************************************************************************
5371
14063
  Group and order functions
5372
14064
*****************************************************************************/
5373
14065
 
5374
14066
/**
5375
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14067
  Resolve an order_st BY or GROUP BY column reference.
5376
14068
 
5377
14069
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5378
14070
  BY clause, find the actual column it represents. If the column being
5379
14071
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5380
14072
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5381
 
  the ORDER BY clause, only the SELECT list is being searched.
 
14073
  the order_st BY clause, only the SELECT list is being searched.
5382
14074
 
5383
14075
  If 'order' is resolved to an Item, then order->item is set to the found
5384
14076
  Item. If there is no item for the found column (that is, it was resolved
5387
14079
 
5388
14080
  ref_pointer_array and all_fields are updated.
5389
14081
 
5390
 
  @param[in] session                 Pointer to current thread structure
 
14082
  @param[in] thd                     Pointer to current thread structure
5391
14083
  @param[in,out] ref_pointer_array  All select, group and order by fields
5392
14084
  @param[in] tables                 List of tables to search in (usually
5393
14085
    FROM clause)
5403
14095
  @retval
5404
14096
    true  if error occurred
5405
14097
*/
5406
 
static bool find_order_in_list(Session *session, 
5407
 
                               Item **ref_pointer_array, 
5408
 
                               TableList *tables,
5409
 
                               Order *order,
5410
 
                               List<Item> &fields,
5411
 
                               List<Item> &all_fields,
5412
 
                               bool is_group_field)
 
14098
 
 
14099
static bool
 
14100
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14101
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14102
                   bool is_group_field)
5413
14103
{
5414
14104
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5415
14105
  Item::Type order_item_type;
5416
14106
  Item **select_item; /* The corresponding item from the SELECT clause. */
5417
14107
  Field *from_field;  /* The corresponding field from the FROM clause. */
5418
 
  uint32_t counter;
 
14108
  uint counter;
5419
14109
  enum_resolution_type resolution;
5420
14110
 
5421
14111
  /*
5424
14114
  */
5425
14115
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5426
14116
  {                                             /* Order by position */
5427
 
    uint32_t count= (uint32_t) order_item->val_int();
 
14117
    uint count= (uint) order_item->val_int();
5428
14118
    if (!count || count > fields.elements)
5429
14119
    {
5430
14120
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5431
 
               order_item->full_name(), session->where);
 
14121
               order_item->full_name(), thd->where);
5432
14122
      return true;
5433
14123
    }
5434
14124
    order->item= ref_pointer_array + count - 1;
5438
14128
    return false;
5439
14129
  }
5440
14130
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5441
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14131
  select_item= find_item_in_list(order_item, fields, &counter,
5442
14132
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5443
14133
  if (!select_item)
5444
14134
    return true; /* The item is not unique, or some other error occured. */
5454
14144
      for this name (in case if we would perform lookup in all tables).
5455
14145
    */
5456
14146
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5457
 
        order_item->fix_fields(session, order->item))
 
14147
        order_item->fix_fields(thd, order->item))
5458
14148
      return true;
5459
14149
 
5460
14150
    /* Lookup the current GROUP field in the FROM clause. */
5463
14153
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5464
14154
        order_item_type == Item::REF_ITEM)
5465
14155
    {
5466
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5467
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14156
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
14157
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14158
                                       false);
5468
14159
      if (!from_field)
5469
14160
        from_field= (Field*) not_found_field;
5470
14161
    }
5502
14193
        warning so the user knows that the field from the FROM clause
5503
14194
        overshadows the column reference from the SELECT list.
5504
14195
      */
5505
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
14196
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5506
14197
                          ER(ER_NON_UNIQ_ERROR),
5507
14198
                          ((Item_ident*) order_item)->field_name,
5508
 
                          session->where);
 
14199
                          current_thd->where);
5509
14200
    }
5510
14201
  }
5511
14202
 
5522
14213
    arguments for which fix_fields already was called.
5523
14214
  */
5524
14215
  if (!order_item->fixed &&
5525
 
      (order_item->fix_fields(session, order->item) ||
 
14216
      (order_item->fix_fields(thd, order->item) ||
5526
14217
       (order_item= *order->item)->check_cols(1) ||
5527
 
       session->is_fatal_error))
 
14218
       thd->is_fatal_error))
5528
14219
    return true; /* Wrong field. */
5529
14220
 
5530
 
  uint32_t el= all_fields.elements;
 
14221
  uint el= all_fields.elements;
5531
14222
  all_fields.push_front(order_item); /* Add new field to field list. */
5532
14223
  ref_pointer_array[el]= order_item;
5533
14224
  order->item= ref_pointer_array + el;
5534
14225
  return false;
5535
14226
}
5536
14227
 
 
14228
 
5537
14229
/**
5538
14230
  Change order to point at item in select list.
5539
14231
 
5540
14232
  If item isn't a number and doesn't exits in the select list, add it the
5541
14233
  the field list.
5542
14234
*/
5543
 
int setup_order(Session *session,
5544
 
                Item **ref_pointer_array,
5545
 
                TableList *tables,
5546
 
                            List<Item> &fields,
5547
 
                List<Item> &all_fields,
5548
 
                Order *order)
 
14235
 
 
14236
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14237
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5549
14238
{
5550
 
  session->where="order clause";
 
14239
  thd->where="order clause";
5551
14240
  for (; order; order=order->next)
5552
14241
  {
5553
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
14242
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5554
14243
                           all_fields, false))
5555
14244
      return 1;
5556
14245
  }
5557
14246
  return 0;
5558
14247
}
5559
14248
 
 
14249
 
5560
14250
/**
5561
14251
  Intitialize the GROUP BY list.
5562
14252
 
5563
 
  @param session                        Thread Cursor
 
14253
  @param thd                    Thread handler
5564
14254
  @param ref_pointer_array      We store references to all fields that was
5565
14255
                               not in 'fields' here.
5566
14256
  @param fields         All fields in the select part. Any item in
5582
14272
  @retval
5583
14273
    1  error (probably out of memory)
5584
14274
*/
5585
 
int setup_group(Session *session,
5586
 
                Item **ref_pointer_array,
5587
 
                TableList *tables,
5588
 
                      List<Item> &fields,
5589
 
                List<Item> &all_fields,
5590
 
                Order *order,
5591
 
                      bool *hidden_group_fields)
 
14275
 
 
14276
int
 
14277
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14278
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14279
            bool *hidden_group_fields)
5592
14280
{
5593
14281
  *hidden_group_fields=0;
5594
 
  Order *ord;
 
14282
  order_st *ord;
5595
14283
 
5596
14284
  if (!order)
5597
14285
    return 0;                           /* Everything is ok */
5598
14286
 
5599
 
  uint32_t org_fields=all_fields.elements;
 
14287
  uint org_fields=all_fields.elements;
5600
14288
 
5601
 
  session->where="group statement";
 
14289
  thd->where="group statement";
5602
14290
  for (ord= order; ord; ord= ord->next)
5603
14291
  {
5604
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
14292
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5605
14293
                           all_fields, true))
5606
14294
      return 1;
5607
14295
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5632
14320
    Item_field *field;
5633
14321
    int cur_pos_in_select_list= 0;
5634
14322
    List_iterator<Item> li(fields);
5635
 
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
 
14323
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5636
14324
 
5637
14325
    field= naf_it++;
5638
14326
    while (field && (item=li++))
5681
14369
  Try to use the fields in the order given by 'order' to allow one to
5682
14370
  optimize away 'order by'.
5683
14371
*/
5684
 
Order *create_distinct_group(Session *session,
5685
 
                                Item **ref_pointer_array,
5686
 
                                Order *order_list,
5687
 
                                List<Item> &fields,
5688
 
                                List<Item> &,
5689
 
                                bool *all_order_by_fields_used)
 
14372
 
 
14373
static order_st *
 
14374
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
14375
                      order_st *order_list, List<Item> &fields,
 
14376
                      List<Item> &all_fields __attribute__((unused)),
 
14377
                      bool *all_order_by_fields_used)
5690
14378
{
5691
14379
  List_iterator<Item> li(fields);
5692
14380
  Item *item;
5693
 
  Order *order,*group,**prev;
 
14381
  order_st *order,*group,**prev;
5694
14382
 
5695
14383
  *all_order_by_fields_used= 1;
5696
14384
  while ((item=li++))
5701
14389
  {
5702
14390
    if (order->in_field_list)
5703
14391
    {
5704
 
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
 
14392
      order_st *ord=(order_st*) thd->memdup((char*) order,sizeof(order_st));
5705
14393
      if (!ord)
5706
 
        return 0;
 
14394
        return 0;
5707
14395
      *prev=ord;
5708
14396
      prev= &ord->next;
5709
14397
      (*ord->item)->marker=1;
5717
14405
  {
5718
14406
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5719
14407
    {
5720
 
      /*
5721
 
        Don't put duplicate columns from the SELECT list into the
 
14408
      /* 
 
14409
        Don't put duplicate columns from the SELECT list into the 
5722
14410
        GROUP BY list.
5723
14411
      */
5724
 
      Order *ord_iter;
 
14412
      order_st *ord_iter;
5725
14413
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5726
14414
        if ((*ord_iter->item)->eq(item, 1))
5727
14415
          goto next_item;
5728
 
 
5729
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14416
      
 
14417
      order_st *ord=(order_st*) thd->calloc(sizeof(order_st));
5730
14418
      if (!ord)
5731
 
        return 0;
 
14419
        return 0;
5732
14420
 
5733
14421
      /*
5734
14422
        We have here only field_list (not all_field_list), so we can use
5747
14435
  return group;
5748
14436
}
5749
14437
 
 
14438
 
5750
14439
/**
5751
14440
  Update join with count of the different type of fields.
5752
14441
*/
5753
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14442
 
 
14443
void
 
14444
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
14445
                  List<Item> &fields, bool reset_with_sum_func)
5754
14446
{
5755
14447
  List_iterator<Item> li(fields);
5756
14448
  Item *field;
5767
14459
    {
5768
14460
      if (! field->const_item())
5769
14461
      {
5770
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14462
        Item_sum *sum_item=(Item_sum*) field->real_item();
5771
14463
        if (!sum_item->depended_from() ||
5772
14464
            sum_item->depended_from() == select_lex)
5773
14465
        {
5775
14467
            param->quick_group=0;                       // UDF SUM function
5776
14468
          param->sum_func_count++;
5777
14469
 
5778
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
14470
          for (uint i=0 ; i < sum_item->arg_count ; i++)
5779
14471
          {
5780
14472
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
5781
14473
              param->field_count++;
5790
14482
    {
5791
14483
      param->func_count++;
5792
14484
      if (reset_with_sum_func)
5793
 
        field->with_sum_func=0;
5794
 
    }
5795
 
  }
5796
 
}
 
14485
        field->with_sum_func=0;
 
14486
    }
 
14487
  }
 
14488
}
 
14489
 
 
14490
 
 
14491
/**
 
14492
  Return 1 if second is a subpart of first argument.
 
14493
 
 
14494
  If first parts has different direction, change it to second part
 
14495
  (group is sorted like order)
 
14496
*/
 
14497
 
 
14498
static bool
 
14499
test_if_subpart(order_st *a,order_st *b)
 
14500
{
 
14501
  for (; a && b; a=a->next,b=b->next)
 
14502
  {
 
14503
    if ((*a->item)->eq(*b->item,1))
 
14504
      a->asc=b->asc;
 
14505
    else
 
14506
      return 0;
 
14507
  }
 
14508
  return test(!b);
 
14509
}
 
14510
 
 
14511
/**
 
14512
  Return table number if there is only one table in sort order
 
14513
  and group and order is compatible, else return 0.
 
14514
*/
 
14515
 
 
14516
static Table *
 
14517
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14518
{
 
14519
  table_map map= (table_map) 0;
 
14520
 
 
14521
  if (!a)
 
14522
    a=b;                                        // Only one need to be given
 
14523
  else if (!b)
 
14524
    b=a;
 
14525
 
 
14526
  for (; a && b; a=a->next,b=b->next)
 
14527
  {
 
14528
    if (!(*a->item)->eq(*b->item,1))
 
14529
      return(0);
 
14530
    map|=a->item[0]->used_tables();
 
14531
  }
 
14532
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14533
    return(0);
 
14534
 
 
14535
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14536
  if (map != tables->table->map)
 
14537
    return(0);                          // More than one table
 
14538
  return(tables->table);
 
14539
}
 
14540
 
 
14541
 
 
14542
/**
 
14543
  calc how big buffer we need for comparing group entries.
 
14544
*/
 
14545
 
 
14546
static void
 
14547
calc_group_buffer(JOIN *join,order_st *group)
 
14548
{
 
14549
  uint key_length=0, parts=0, null_parts=0;
 
14550
 
 
14551
  if (group)
 
14552
    join->group= 1;
 
14553
  for (; group ; group=group->next)
 
14554
  {
 
14555
    Item *group_item= *group->item;
 
14556
    Field *field= group_item->get_tmp_table_field();
 
14557
    if (field)
 
14558
    {
 
14559
      enum_field_types type;
 
14560
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14561
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14562
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14563
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14564
      else
 
14565
        key_length+= field->pack_length();
 
14566
    }
 
14567
    else
 
14568
    { 
 
14569
      switch (group_item->result_type()) {
 
14570
      case REAL_RESULT:
 
14571
        key_length+= sizeof(double);
 
14572
        break;
 
14573
      case INT_RESULT:
 
14574
        key_length+= sizeof(int64_t);
 
14575
        break;
 
14576
      case DECIMAL_RESULT:
 
14577
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
14578
                                                (group_item->decimals ? 1 : 0),
 
14579
                                                group_item->decimals);
 
14580
        break;
 
14581
      case STRING_RESULT:
 
14582
      {
 
14583
        enum enum_field_types type= group_item->field_type();
 
14584
        /*
 
14585
          As items represented as DATE/TIME fields in the group buffer
 
14586
          have STRING_RESULT result type, we increase the length 
 
14587
          by 8 as maximum pack length of such fields.
 
14588
        */
 
14589
        if (type == DRIZZLE_TYPE_TIME ||
 
14590
            type == DRIZZLE_TYPE_NEWDATE ||
 
14591
            type == DRIZZLE_TYPE_DATETIME ||
 
14592
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14593
        {
 
14594
          key_length+= 8;
 
14595
        }
 
14596
        else
 
14597
        {
 
14598
          /*
 
14599
            Group strings are taken as varstrings and require an length field.
 
14600
            A field is not yet created by create_tmp_field()
 
14601
            and the sizes should match up.
 
14602
          */
 
14603
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14604
        }
 
14605
        break;
 
14606
      }
 
14607
      default:
 
14608
        /* This case should never be choosen */
 
14609
        assert(0);
 
14610
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14611
      }
 
14612
    }
 
14613
    parts++;
 
14614
    if (group_item->maybe_null)
 
14615
      null_parts++;
 
14616
  }
 
14617
  join->tmp_table_param.group_length=key_length+null_parts;
 
14618
  join->tmp_table_param.group_parts=parts;
 
14619
  join->tmp_table_param.group_null_parts=null_parts;
 
14620
}
 
14621
 
 
14622
 
 
14623
/**
 
14624
  allocate group fields or take prepared (cached).
 
14625
 
 
14626
  @param main_join   join of current select
 
14627
  @param curr_join   current join (join of current select or temporary copy
 
14628
                     of it)
 
14629
 
 
14630
  @retval
 
14631
    0   ok
 
14632
  @retval
 
14633
    1   failed
 
14634
*/
 
14635
 
 
14636
static bool
 
14637
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14638
{
 
14639
  if (main_join->group_fields_cache.elements)
 
14640
  {
 
14641
    curr_join->group_fields= main_join->group_fields_cache;
 
14642
    curr_join->sort_and_group= 1;
 
14643
  }
 
14644
  else
 
14645
  {
 
14646
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14647
      return (1);
 
14648
    main_join->group_fields_cache= curr_join->group_fields;
 
14649
  }
 
14650
  return (0);
 
14651
}
 
14652
 
 
14653
 
 
14654
/**
 
14655
  Get a list of buffers for saveing last group.
 
14656
 
 
14657
  Groups are saved in reverse order for easyer check loop.
 
14658
*/
 
14659
 
 
14660
static bool
 
14661
alloc_group_fields(JOIN *join,order_st *group)
 
14662
{
 
14663
  if (group)
 
14664
  {
 
14665
    for (; group ; group=group->next)
 
14666
    {
 
14667
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
14668
      if (!tmp || join->group_fields.push_front(tmp))
 
14669
        return true;
 
14670
    }
 
14671
  }
 
14672
  join->sort_and_group=1;                       /* Mark for do_select */
 
14673
  return false;
 
14674
}
 
14675
 
5797
14676
 
5798
14677
/*
5799
14678
  Test if a single-row cache of items changed, and update the cache.
5801
14680
  @details Test if a list of items that typically represents a result
5802
14681
  row has changed. If the value of some item changed, update the cached
5803
14682
  value for this item.
5804
 
 
 
14683
  
5805
14684
  @param list list of <item, cached_value> pairs stored as Cached_item.
5806
14685
 
5807
14686
  @return -1 if no item changed
5808
14687
  @return index of the first item that changed
5809
14688
*/
 
14689
 
5810
14690
int test_if_item_cache_changed(List<Cached_item> &list)
5811
14691
{
5812
14692
  List_iterator<Cached_item> li(list);
5821
14701
  return(idx);
5822
14702
}
5823
14703
 
 
14704
 
5824
14705
/**
5825
14706
  Setup copy_fields to save fields at start of new group.
5826
14707
 
5830
14711
  Change old item_field to use a new field with points at saved fieldvalue
5831
14712
  This function is only called before use of send_fields.
5832
14713
 
5833
 
  @param session                   Session pointer
 
14714
  @param thd                   THD pointer
5834
14715
  @param param                 temporary table parameters
5835
14716
  @param ref_pointer_array     array of pointers to top elements of filed list
5836
14717
  @param res_selected_fields   new list of items of select item list
5849
14730
  @retval
5850
14731
    !=0   error
5851
14732
*/
5852
 
bool setup_copy_fields(Session *session,
5853
 
                       Tmp_Table_Param *param,
5854
 
                       Item **ref_pointer_array,
5855
 
                       List<Item> &res_selected_fields,
5856
 
                       List<Item> &res_all_fields,
5857
 
                       uint32_t elements,
5858
 
                       List<Item> &all_fields)
 
14733
 
 
14734
bool
 
14735
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
14736
                  Item **ref_pointer_array,
 
14737
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14738
                  uint elements, List<Item> &all_fields)
5859
14739
{
5860
14740
  Item *pos;
5861
14741
  List_iterator_fast<Item> li(all_fields);
5862
 
  CopyField *copy= NULL;
 
14742
  Copy_field *copy= NULL;
5863
14743
  res_selected_fields.empty();
5864
14744
  res_all_fields.empty();
5865
14745
  List_iterator_fast<Item> itr(res_all_fields);
5866
14746
  List<Item> extra_funcs;
5867
 
  uint32_t i, border= all_fields.elements - elements;
 
14747
  uint i, border= all_fields.elements - elements;
5868
14748
 
5869
 
  if (param->field_count &&
5870
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14749
  if (param->field_count && 
 
14750
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5871
14751
    goto err2;
5872
14752
 
5873
14753
  param->copy_funcs.empty();
5874
14754
  for (i= 0; (pos= li++); i++)
5875
14755
  {
5876
14756
    Field *field;
5877
 
    unsigned char *tmp;
 
14757
    uchar *tmp;
5878
14758
    Item *real_pos= pos->real_item();
5879
14759
    if (real_pos->type() == Item::FIELD_ITEM)
5880
14760
    {
5881
14761
      Item_field *item;
5882
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5883
 
        goto err;
 
14762
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
14763
        goto err;
5884
14764
      if (pos->type() == Item::REF_ITEM)
5885
14765
      {
5886
14766
        /* preserve the names of the ref when dereferncing */
5892
14772
      pos= item;
5893
14773
      if (item->field->flags & BLOB_FLAG)
5894
14774
      {
5895
 
        if (!(pos= new Item_copy_string(pos)))
5896
 
          goto err;
5897
 
            /*
5898
 
              Item_copy_string::copy for function can call
5899
 
              Item_copy_string::val_int for blob via Item_ref.
5900
 
              But if Item_copy_string::copy for blob isn't called before,
5901
 
              it's value will be wrong
5902
 
              so let's insert Item_copy_string for blobs in the beginning of
5903
 
              copy_funcs
5904
 
              (to see full test case look at having.test, BUG #4358)
5905
 
            */
5906
 
        if (param->copy_funcs.push_front(pos))
5907
 
          goto err;
 
14775
        if (!(pos= new Item_copy_string(pos)))
 
14776
          goto err;
 
14777
       /*
 
14778
         Item_copy_string::copy for function can call 
 
14779
         Item_copy_string::val_int for blob via Item_ref.
 
14780
         But if Item_copy_string::copy for blob isn't called before,
 
14781
         it's value will be wrong
 
14782
         so let's insert Item_copy_string for blobs in the beginning of 
 
14783
         copy_funcs
 
14784
         (to see full test case look at having.test, BUG #4358) 
 
14785
       */
 
14786
        if (param->copy_funcs.push_front(pos))
 
14787
          goto err;
5908
14788
      }
5909
14789
      else
5910
14790
      {
 
14791
        /* 
 
14792
           set up save buffer and change result_field to point at 
 
14793
           saved value
 
14794
        */
 
14795
        field= item->field;
 
14796
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5911
14797
        /*
5912
 
          set up save buffer and change result_field to point at
5913
 
          saved value
 
14798
          We need to allocate one extra byte for null handling and
 
14799
          another extra byte to not get warnings from purify in
 
14800
          Field_varstring::val_int
5914
14801
        */
5915
 
        field= item->field;
5916
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
5917
 
              /*
5918
 
                We need to allocate one extra byte for null handling and
5919
 
                another extra byte to not get warnings from purify in
5920
 
                Field_varstring::val_int
5921
 
              */
5922
 
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
5923
 
          goto err;
 
14802
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
 
14803
          goto err;
5924
14804
        if (copy)
5925
14805
        {
5926
14806
          copy->set(tmp, item->result_field);
5927
14807
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5928
 
#ifdef HAVE_VALGRIND
 
14808
#ifdef HAVE_purify
5929
14809
          copy->to_ptr[copy->from_length]= 0;
5930
14810
#endif
5931
14811
          copy++;
5940
14820
    {                                           // Save for send fields
5941
14821
      pos= real_pos;
5942
14822
      /* TODO:
5943
 
        In most cases this result will be sent to the user.
5944
 
        This should be changed to use copy_int or copy_real depending
5945
 
        on how the value is to be used: In some cases this may be an
5946
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
14823
         In most cases this result will be sent to the user.
 
14824
         This should be changed to use copy_int or copy_real depending
 
14825
         on how the value is to be used: In some cases this may be an
 
14826
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5947
14827
      */
5948
14828
      if (!(pos=new Item_copy_string(pos)))
5949
 
        goto err;
 
14829
        goto err;
5950
14830
      if (i < border)                           // HAVING, order_st and GROUP BY
5951
14831
      {
5952
14832
        if (extra_funcs.push_back(pos))
5953
14833
          goto err;
5954
14834
      }
5955
14835
      else if (param->copy_funcs.push_back(pos))
5956
 
        goto err;
 
14836
        goto err;
5957
14837
    }
5958
14838
    res_all_fields.push_back(pos);
5959
14839
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5965
14845
    itr++;
5966
14846
  itr.sublist(res_selected_fields, elements);
5967
14847
  /*
5968
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14848
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5969
14849
    reference used in these will resolve to a item that is already calculated
5970
14850
  */
5971
14851
  param->copy_funcs.concat(&extra_funcs);
5972
14852
 
5973
14853
  return(0);
5974
14854
 
5975
 
err:
 
14855
 err:
5976
14856
  if (copy)
5977
14857
    delete [] param->copy_field;                        // This is never 0
5978
14858
  param->copy_field=0;
5980
14860
  return(true);
5981
14861
}
5982
14862
 
 
14863
 
5983
14864
/**
5984
14865
  Make a copy of all simple SELECT'ed items.
5985
14866
 
5986
14867
  This is done at the start of a new group so that we can retrieve
5987
14868
  these later when the group changes.
5988
14869
*/
5989
 
void copy_fields(Tmp_Table_Param *param)
 
14870
 
 
14871
void
 
14872
copy_fields(TMP_TABLE_PARAM *param)
5990
14873
{
5991
 
  CopyField *ptr= param->copy_field;
5992
 
  CopyField *end= param->copy_field_end;
 
14874
  Copy_field *ptr=param->copy_field;
 
14875
  Copy_field *end=param->copy_field_end;
5993
14876
 
5994
14877
  for (; ptr != end; ptr++)
5995
14878
    (*ptr->do_copy)(ptr);
6000
14883
    item->copy();
6001
14884
}
6002
14885
 
 
14886
 
 
14887
/**
 
14888
  Make an array of pointers to sum_functions to speed up
 
14889
  sum_func calculation.
 
14890
 
 
14891
  @retval
 
14892
    0   ok
 
14893
  @retval
 
14894
    1   Error
 
14895
*/
 
14896
 
 
14897
bool JOIN::alloc_func_list()
 
14898
{
 
14899
  uint func_count, group_parts;
 
14900
 
 
14901
  func_count= tmp_table_param.sum_func_count;
 
14902
  /*
 
14903
    If we are using rollup, we need a copy of the summary functions for
 
14904
    each level
 
14905
  */
 
14906
  if (rollup.state != ROLLUP::STATE_NONE)
 
14907
    func_count*= (send_group_parts+1);
 
14908
 
 
14909
  group_parts= send_group_parts;
 
14910
  /*
 
14911
    If distinct, reserve memory for possible
 
14912
    disctinct->group_by optimization
 
14913
  */
 
14914
  if (select_distinct)
 
14915
  {
 
14916
    group_parts+= fields_list.elements;
 
14917
    /*
 
14918
      If the order_st clause is specified then it's possible that
 
14919
      it also will be optimized, so reserve space for it too
 
14920
    */
 
14921
    if (order)
 
14922
    {
 
14923
      order_st *ord;
 
14924
      for (ord= order; ord; ord= ord->next)
 
14925
        group_parts++;
 
14926
    }
 
14927
  }
 
14928
 
 
14929
  /* This must use calloc() as rollup_make_fields depends on this */
 
14930
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14931
                                      sizeof(Item_sum***) * (group_parts+1));
 
14932
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14933
  return(sum_funcs == 0);
 
14934
}
 
14935
 
 
14936
 
 
14937
/**
 
14938
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14939
 
 
14940
  @param field_list        All items
 
14941
  @param send_fields       Items in select list
 
14942
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14943
  @param recompute         Set to true if sum_funcs must be recomputed
 
14944
 
 
14945
  @retval
 
14946
    0  ok
 
14947
  @retval
 
14948
    1  error
 
14949
*/
 
14950
 
 
14951
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14952
                              bool before_group_by, bool recompute)
 
14953
{
 
14954
  List_iterator_fast<Item> it(field_list);
 
14955
  Item_sum **func;
 
14956
  Item *item;
 
14957
 
 
14958
  if (*sum_funcs && !recompute)
 
14959
    return(false); /* We have already initialized sum_funcs. */
 
14960
 
 
14961
  func= sum_funcs;
 
14962
  while ((item=it++))
 
14963
  {
 
14964
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14965
        (!((Item_sum*) item)->depended_from() ||
 
14966
         ((Item_sum *)item)->depended_from() == select_lex))
 
14967
      *func++= (Item_sum*) item;
 
14968
  }
 
14969
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14970
  {
 
14971
    rollup.state= ROLLUP::STATE_READY;
 
14972
    if (rollup_make_fields(field_list, send_fields, &func))
 
14973
      return(true);                     // Should never happen
 
14974
  }
 
14975
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14976
  {
 
14977
    for (uint i=0 ; i <= send_group_parts ;i++)
 
14978
      sum_funcs_end[i]= func;
 
14979
  }
 
14980
  else if (rollup.state == ROLLUP::STATE_READY)
 
14981
    return(false);                         // Don't put end marker
 
14982
  *func=0;                                      // End marker
 
14983
  return(false);
 
14984
}
 
14985
 
 
14986
 
6003
14987
/**
6004
14988
  Change all funcs and sum_funcs to fields in tmp table, and create
6005
14989
  new list of all items.
6006
14990
 
6007
 
  @param session                   Session pointer
 
14991
  @param thd                   THD pointer
6008
14992
  @param ref_pointer_array     array of pointers to top elements of filed list
6009
14993
  @param res_selected_fields   new list of items of select item list
6010
14994
  @param res_all_fields        new list of all items
6016
15000
  @retval
6017
15001
    !=0   error
6018
15002
*/
6019
 
bool change_to_use_tmp_fields(Session *session,
6020
 
                              Item **ref_pointer_array,
6021
 
                                                List<Item> &res_selected_fields,
6022
 
                                                List<Item> &res_all_fields,
6023
 
                                                uint32_t elements,
6024
 
                              List<Item> &all_fields)
 
15003
 
 
15004
static bool
 
15005
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15006
                         List<Item> &res_selected_fields,
 
15007
                         List<Item> &res_all_fields,
 
15008
                         uint elements, List<Item> &all_fields)
6025
15009
{
6026
15010
  List_iterator_fast<Item> it(all_fields);
6027
15011
  Item *item_field,*item;
6029
15013
  res_selected_fields.empty();
6030
15014
  res_all_fields.empty();
6031
15015
 
6032
 
  uint32_t i, border= all_fields.elements - elements;
 
15016
  uint i, border= all_fields.elements - elements;
6033
15017
  for (i= 0; (item= it++); i++)
6034
15018
  {
6035
15019
    Field *field;
6042
15026
    {
6043
15027
      if (item->type() == Item::FIELD_ITEM)
6044
15028
      {
6045
 
        item_field= item->get_tmp_table_item(session);
 
15029
        item_field= item->get_tmp_table_item(thd);
6046
15030
      }
6047
15031
      else if ((field= item->get_tmp_table_field()))
6048
15032
      {
6049
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6050
 
          item_field= ((Item_sum*) item)->result_item(field);
6051
 
        else
6052
 
          item_field= (Item*) new Item_field(field);
6053
 
        if (!item_field)
6054
 
          return(true);                    // Fatal error
 
15033
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15034
          item_field= ((Item_sum*) item)->result_item(field);
 
15035
        else
 
15036
          item_field= (Item*) new Item_field(field);
 
15037
        if (!item_field)
 
15038
          return(true);                    // Fatal error
6055
15039
 
6056
15040
        if (item->real_item()->type() != Item::FIELD_ITEM)
6057
15041
          field->orig_table= 0;
6058
 
        item_field->name= item->name;
 
15042
        item_field->name= item->name;
6059
15043
        if (item->type() == Item::REF_ITEM)
6060
15044
        {
6061
15045
          Item_field *ifield= (Item_field *) item_field;
6065
15049
        }
6066
15050
      }
6067
15051
      else
6068
 
        item_field= item;
 
15052
        item_field= item;
6069
15053
    }
6070
15054
    res_all_fields.push_back(item_field);
6071
15055
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6079
15063
  return(false);
6080
15064
}
6081
15065
 
 
15066
 
6082
15067
/**
6083
15068
  Change all sum_func refs to fields to point at fields in tmp table.
6084
15069
  Change all funcs to be fields in tmp table.
6085
15070
 
6086
 
  @param session                   Session pointer
 
15071
  @param thd                   THD pointer
6087
15072
  @param ref_pointer_array     array of pointers to top elements of filed list
6088
15073
  @param res_selected_fields   new list of items of select item list
6089
15074
  @param res_all_fields        new list of all items
6095
15080
  @retval
6096
15081
    1   error
6097
15082
*/
6098
 
bool change_refs_to_tmp_fields(Session *session,
6099
 
                               Item **ref_pointer_array,
6100
 
                               List<Item> &res_selected_fields,
6101
 
                               List<Item> &res_all_fields,
6102
 
                               uint32_t elements,
6103
 
                                                 List<Item> &all_fields)
 
15083
 
 
15084
static bool
 
15085
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15086
                          List<Item> &res_selected_fields,
 
15087
                          List<Item> &res_all_fields, uint elements,
 
15088
                          List<Item> &all_fields)
6104
15089
{
6105
15090
  List_iterator_fast<Item> it(all_fields);
6106
15091
  Item *item, *new_item;
6107
15092
  res_selected_fields.empty();
6108
15093
  res_all_fields.empty();
6109
15094
 
6110
 
  uint32_t i, border= all_fields.elements - elements;
 
15095
  uint i, border= all_fields.elements - elements;
6111
15096
  for (i= 0; (item= it++); i++)
6112
15097
  {
6113
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
15098
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6114
15099
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6115
15100
      new_item;
6116
15101
  }
6120
15105
    itr++;
6121
15106
  itr.sublist(res_selected_fields, elements);
6122
15107
 
6123
 
  return session->is_fatal_error;
 
15108
  return thd->is_fatal_error;
6124
15109
}
6125
15110
 
 
15111
 
 
15112
 
6126
15113
/******************************************************************************
6127
15114
  Code for calculating functions
6128
15115
******************************************************************************/
6129
15116
 
 
15117
 
6130
15118
/**
6131
15119
  Call ::setup for all sum functions.
6132
15120
 
6133
 
  @param session           thread Cursor
 
15121
  @param thd           thread handler
6134
15122
  @param func_ptr      sum function list
6135
15123
 
6136
15124
  @retval
6138
15126
  @retval
6139
15127
    true   error
6140
15128
*/
6141
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15129
 
 
15130
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6142
15131
{
6143
15132
  Item_sum *func;
6144
15133
  while ((func= *(func_ptr++)))
6145
15134
  {
6146
 
    if (func->setup(session))
 
15135
    if (func->setup(thd))
6147
15136
      return(true);
6148
15137
  }
6149
15138
  return(false);
6150
15139
}
6151
15140
 
6152
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15141
 
 
15142
static void
 
15143
init_tmptable_sum_functions(Item_sum **func_ptr)
6153
15144
{
6154
15145
  Item_sum *func;
6155
15146
  while ((func= *(func_ptr++)))
6156
15147
    func->reset_field();
6157
15148
}
6158
15149
 
 
15150
 
6159
15151
/** Update record 0 in tmp_table from record 1. */
6160
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15152
 
 
15153
static void
 
15154
update_tmptable_sum_func(Item_sum **func_ptr,
 
15155
                         Table *tmp_table __attribute__((unused)))
6161
15156
{
6162
15157
  Item_sum *func;
6163
15158
  while ((func= *(func_ptr++)))
6164
15159
    func->update_field();
6165
15160
}
6166
15161
 
 
15162
 
6167
15163
/** Copy result of sum functions to record in tmp_table. */
6168
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15164
 
 
15165
static void
 
15166
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6169
15167
{
6170
15168
  for (; func_ptr != end_ptr ; func_ptr++)
6171
15169
    (void) (*func_ptr)->save_in_result_field(1);
6172
15170
  return;
6173
15171
}
6174
15172
 
6175
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15173
 
 
15174
static bool
 
15175
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6176
15176
{
6177
15177
  for (; func_ptr != end_ptr ;func_ptr++)
6178
15178
  {
6188
15188
  return 0;
6189
15189
}
6190
15190
 
6191
 
bool update_sum_func(Item_sum **func_ptr)
 
15191
 
 
15192
static bool
 
15193
update_sum_func(Item_sum **func_ptr)
6192
15194
{
6193
15195
  Item_sum *func;
6194
15196
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6198
15200
}
6199
15201
 
6200
15202
/** Copy result of functions to record in tmp_table. */
6201
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
15203
 
 
15204
void
 
15205
copy_funcs(Item **func_ptr)
6202
15206
{
6203
15207
  Item *func;
6204
15208
  for (; (func = *func_ptr) ; func_ptr++)
6205
 
  {
6206
15209
    func->save_in_result_field(1);
6207
 
    /*
6208
 
      Need to check the THD error state because Item::val_xxx() don't
6209
 
      return error code, but can generate errors
6210
 
      TODO: change it for a real status check when Item::val_xxx()
6211
 
      are extended to return status code.
6212
 
    */
6213
 
    if (session->is_error())
6214
 
      return true;
6215
 
  }
6216
 
  return false;
6217
 
}
 
15210
}
 
15211
 
 
15212
 
 
15213
/**
 
15214
  Create a condition for a const reference and add this to the
 
15215
  currenct select for the table.
 
15216
*/
 
15217
 
 
15218
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
15219
{
 
15220
  if (!join_tab->ref.key_parts)
 
15221
    return(false);
 
15222
 
 
15223
  Item_cond_and *cond=new Item_cond_and();
 
15224
  Table *table=join_tab->table;
 
15225
  int error;
 
15226
  if (!cond)
 
15227
    return(true);
 
15228
 
 
15229
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
 
15230
  {
 
15231
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15232
                              fieldnr-1];
 
15233
    Item *value=join_tab->ref.items[i];
 
15234
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15235
  }
 
15236
  if (thd->is_fatal_error)
 
15237
    return(true);
 
15238
 
 
15239
  if (!cond->fixed)
 
15240
    cond->fix_fields(thd, (Item**)&cond);
 
15241
  if (join_tab->select)
 
15242
  {
 
15243
    error=(int) cond->add(join_tab->select->cond);
 
15244
    join_tab->select_cond=join_tab->select->cond=cond;
 
15245
  }
 
15246
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15247
                                          &error)))
 
15248
    join_tab->select_cond=cond;
 
15249
 
 
15250
  return(error ? true : false);
 
15251
}
 
15252
 
6218
15253
 
6219
15254
/**
6220
15255
  Free joins of subselect of this select.
6221
15256
 
6222
 
  @param session      Session pointer
6223
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
15257
  @param thd      THD pointer
 
15258
  @param select   pointer to st_select_lex which subselects joins we will free
6224
15259
*/
6225
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
15260
 
 
15261
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
15262
                          SELECT_LEX *select)
6226
15263
{
6227
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
15264
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6228
15265
       unit;
6229
15266
       unit= unit->next_unit())
6230
15267
    unit->cleanup();
6246
15283
  @b EXAMPLES
6247
15284
    @code
6248
15285
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6249
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
15286
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6250
15287
  @endcode
6251
15288
 
6252
15289
  @b IMPLEMENTATION
6259
15296
    This substitution is needed GROUP BY queries with ROLLUP if
6260
15297
    SELECT list contains expressions over group by attributes.
6261
15298
 
6262
 
  @param session                  reference to the context
 
15299
  @param thd                  reference to the context
6263
15300
  @param expr                 expression to make replacement
6264
15301
  @param group_list           list of references to group by items
6265
15302
  @param changed        out:  returns 1 if item contains a replaced field item
6266
15303
 
6267
15304
  @todo
6268
15305
    - TODO: Some functions are not null-preserving. For those functions
6269
 
    updating of the maybe_null attribute is an overkill.
 
15306
    updating of the maybe_null attribute is an overkill. 
6270
15307
 
6271
15308
  @retval
6272
15309
    0   if ok
6273
15310
  @retval
6274
15311
    1   on error
6275
15312
*/
6276
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15313
 
 
15314
static bool change_group_ref(THD *thd, Item_func *expr, order_st *group_list,
 
15315
                             bool *changed)
6277
15316
{
6278
15317
  if (expr->arg_count)
6279
15318
  {
6280
 
    Name_resolution_context *context= &session->lex->current_select->context;
 
15319
    Name_resolution_context *context= &thd->lex->current_select->context;
6281
15320
    Item **arg,**arg_end;
6282
15321
    bool arg_changed= false;
6283
15322
    for (arg= expr->arguments(),
6287
15326
      Item *item= *arg;
6288
15327
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6289
15328
      {
6290
 
        Order *group_tmp;
 
15329
        order_st *group_tmp;
6291
15330
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6292
15331
        {
6293
15332
          if (item->eq(*group_tmp->item,0))
6296
15335
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6297
15336
                                        item->name)))
6298
15337
              return 1;                                 // fatal_error is set
6299
 
            session->change_item_tree(arg, new_item);
 
15338
            thd->change_item_tree(arg, new_item);
6300
15339
            arg_changed= true;
6301
15340
          }
6302
15341
        }
6303
15342
      }
6304
15343
      else if (item->type() == Item::FUNC_ITEM)
6305
15344
      {
6306
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
15345
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6307
15346
          return 1;
6308
15347
      }
6309
15348
    }
6317
15356
}
6318
15357
 
6319
15358
 
6320
 
static void print_table_array(Session *session, String *str, TableList **table,
 
15359
/** Allocate memory needed for other rollup functions. */
 
15360
 
 
15361
bool JOIN::rollup_init()
 
15362
{
 
15363
  uint i,j;
 
15364
  Item **ref_array;
 
15365
 
 
15366
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15367
  rollup.state= ROLLUP::STATE_INITED;
 
15368
 
 
15369
  /*
 
15370
    Create pointers to the different sum function groups
 
15371
    These are updated by rollup_make_fields()
 
15372
  */
 
15373
  tmp_table_param.group_parts= send_group_parts;
 
15374
 
 
15375
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
15376
                                                sizeof(Item**) +
 
15377
                                                sizeof(List<Item>) +
 
15378
                                                ref_pointer_array_size)
 
15379
                                                * send_group_parts )))
 
15380
    return 1;
 
15381
  
 
15382
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15383
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15384
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15385
 
 
15386
  /*
 
15387
    Prepare space for field list for the different levels
 
15388
    These will be filled up in rollup_make_fields()
 
15389
  */
 
15390
  for (i= 0 ; i < send_group_parts ; i++)
 
15391
  {
 
15392
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
15393
    List<Item> *rollup_fields= &rollup.fields[i];
 
15394
    rollup_fields->empty();
 
15395
    rollup.ref_pointer_arrays[i]= ref_array;
 
15396
    ref_array+= all_fields.elements;
 
15397
  }
 
15398
  for (i= 0 ; i < send_group_parts; i++)
 
15399
  {
 
15400
    for (j=0 ; j < fields_list.elements ; j++)
 
15401
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15402
  }
 
15403
  List_iterator<Item> it(all_fields);
 
15404
  Item *item;
 
15405
  while ((item= it++))
 
15406
  {
 
15407
    order_st *group_tmp;
 
15408
    bool found_in_group= 0;
 
15409
 
 
15410
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15411
    {
 
15412
      if (*group_tmp->item == item)
 
15413
      {
 
15414
        item->maybe_null= 1;
 
15415
        found_in_group= 1;
 
15416
        if (item->const_item())
 
15417
        {
 
15418
          /*
 
15419
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15420
            is wrapped up into an Item_func object yielding the same value
 
15421
            as the constant item. The objects of the wrapper class are never
 
15422
            considered as constant items and besides they inherit all
 
15423
            properties of the Item_result_field class.
 
15424
            This wrapping allows us to ensure writing constant items
 
15425
            into temporary tables whenever the result of the ROLLUP
 
15426
            operation has to be written into a temporary table, e.g. when
 
15427
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15428
            Usually when creating temporary tables for a intermidiate
 
15429
            result we do not include fields for constant expressions.
 
15430
          */           
 
15431
          Item* new_item= new Item_func_rollup_const(item);
 
15432
          if (!new_item)
 
15433
            return 1;
 
15434
          new_item->fix_fields(thd, (Item **) 0);
 
15435
          thd->change_item_tree(it.ref(), new_item);
 
15436
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15437
          { 
 
15438
            if (*tmp->item == item)
 
15439
              thd->change_item_tree(tmp->item, new_item);
 
15440
          }
 
15441
        }
 
15442
      }
 
15443
    }
 
15444
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15445
    {
 
15446
      bool changed= false;
 
15447
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
15448
        return 1;
 
15449
      /*
 
15450
        We have to prevent creation of a field in a temporary table for
 
15451
        an expression that contains GROUP BY attributes.
 
15452
        Marking the expression item as 'with_sum_func' will ensure this.
 
15453
      */ 
 
15454
      if (changed)
 
15455
        item->with_sum_func= 1;
 
15456
    }
 
15457
  }
 
15458
  return 0;
 
15459
}
 
15460
  
 
15461
 
 
15462
/**
 
15463
  Fill up rollup structures with pointers to fields to use.
 
15464
 
 
15465
  Creates copies of item_sum items for each sum level.
 
15466
 
 
15467
  @param fields_arg             List of all fields (hidden and real ones)
 
15468
  @param sel_fields             Pointer to selected fields
 
15469
  @param func                   Store here a pointer to all fields
 
15470
 
 
15471
  @retval
 
15472
    0   if ok;
 
15473
    In this case func is pointing to next not used element.
 
15474
  @retval
 
15475
    1    on error
 
15476
*/
 
15477
 
 
15478
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15479
                              Item_sum ***func)
 
15480
{
 
15481
  List_iterator_fast<Item> it(fields_arg);
 
15482
  Item *first_field= sel_fields.head();
 
15483
  uint level;
 
15484
 
 
15485
  /*
 
15486
    Create field lists for the different levels
 
15487
 
 
15488
    The idea here is to have a separate field list for each rollup level to
 
15489
    avoid all runtime checks of which columns should be NULL.
 
15490
 
 
15491
    The list is stored in reverse order to get sum function in such an order
 
15492
    in func that it makes it easy to reset them with init_sum_functions()
 
15493
 
 
15494
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15495
 
 
15496
    rollup.fields[0] will contain list where a,b,c is NULL
 
15497
    rollup.fields[1] will contain list where b,c is NULL
 
15498
    ...
 
15499
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15500
    ...
 
15501
    sum_funcs_end[0] points to all sum functions
 
15502
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15503
    ...
 
15504
  */
 
15505
 
 
15506
  for (level=0 ; level < send_group_parts ; level++)
 
15507
  {
 
15508
    uint i;
 
15509
    uint pos= send_group_parts - level -1;
 
15510
    bool real_fields= 0;
 
15511
    Item *item;
 
15512
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15513
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15514
    order_st *start_group;
 
15515
 
 
15516
    /* Point to first hidden field */
 
15517
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15518
 
 
15519
    /* Remember where the sum functions ends for the previous level */
 
15520
    sum_funcs_end[pos+1]= *func;
 
15521
 
 
15522
    /* Find the start of the group for this level */
 
15523
    for (i= 0, start_group= group_list ;
 
15524
         i++ < pos ;
 
15525
         start_group= start_group->next)
 
15526
      ;
 
15527
 
 
15528
    it.rewind();
 
15529
    while ((item= it++))
 
15530
    {
 
15531
      if (item == first_field)
 
15532
      {
 
15533
        real_fields= 1;                         // End of hidden fields
 
15534
        ref_array= ref_array_start;
 
15535
      }
 
15536
 
 
15537
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15538
          (!((Item_sum*) item)->depended_from() ||
 
15539
           ((Item_sum *)item)->depended_from() == select_lex))
 
15540
          
 
15541
      {
 
15542
        /*
 
15543
          This is a top level summary function that must be replaced with
 
15544
          a sum function that is reset for this level.
 
15545
 
 
15546
          NOTE: This code creates an object which is not that nice in a
 
15547
          sub select.  Fortunately it's not common to have rollup in
 
15548
          sub selects.
 
15549
        */
 
15550
        item= item->copy_or_same(thd);
 
15551
        ((Item_sum*) item)->make_unique();
 
15552
        *(*func)= (Item_sum*) item;
 
15553
        (*func)++;
 
15554
      }
 
15555
      else 
 
15556
      {
 
15557
        /* Check if this is something that is part of this group by */
 
15558
        order_st *group_tmp;
 
15559
        for (group_tmp= start_group, i= pos ;
 
15560
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15561
        {
 
15562
          if (*group_tmp->item == item)
 
15563
          {
 
15564
            /*
 
15565
              This is an element that is used by the GROUP BY and should be
 
15566
              set to NULL in this level
 
15567
            */
 
15568
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
15569
            if (!null_item)
 
15570
              return 1;
 
15571
            item->maybe_null= 1;                // Value will be null sometimes
 
15572
            null_item->result_field= item->get_tmp_table_field();
 
15573
            item= null_item;
 
15574
            break;
 
15575
          }
 
15576
        }
 
15577
      }
 
15578
      *ref_array= item;
 
15579
      if (real_fields)
 
15580
      {
 
15581
        (void) new_it++;                        // Point to next item
 
15582
        new_it.replace(item);                   // Replace previous
 
15583
        ref_array++;
 
15584
      }
 
15585
      else
 
15586
        ref_array--;
 
15587
    }
 
15588
  }
 
15589
  sum_funcs_end[0]= *func;                      // Point to last function
 
15590
  return 0;
 
15591
}
 
15592
 
 
15593
/**
 
15594
  Send all rollup levels higher than the current one to the client.
 
15595
 
 
15596
  @b SAMPLE
 
15597
    @code
 
15598
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15599
  @endcode
 
15600
 
 
15601
  @param idx            Level we are on:
 
15602
                        - 0 = Total sum level
 
15603
                        - 1 = First group changed  (a)
 
15604
                        - 2 = Second group changed (a,b)
 
15605
 
 
15606
  @retval
 
15607
    0   ok
 
15608
  @retval
 
15609
    1   If send_data_failed()
 
15610
*/
 
15611
 
 
15612
int JOIN::rollup_send_data(uint idx)
 
15613
{
 
15614
  uint i;
 
15615
  for (i= send_group_parts ; i-- > idx ; )
 
15616
  {
 
15617
    /* Get reference pointers to sum functions in place */
 
15618
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15619
           ref_pointer_array_size);
 
15620
    if ((!having || having->val_int()))
 
15621
    {
 
15622
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15623
          result->send_data(rollup.fields[i]))
 
15624
        return 1;
 
15625
      send_records++;
 
15626
    }
 
15627
  }
 
15628
  /* Restore ref_pointer_array */
 
15629
  set_items_ref_array(current_ref_pointer_array);
 
15630
  return 0;
 
15631
}
 
15632
 
 
15633
/**
 
15634
  Write all rollup levels higher than the current one to a temp table.
 
15635
 
 
15636
  @b SAMPLE
 
15637
    @code
 
15638
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15639
  @endcode
 
15640
 
 
15641
  @param idx                 Level we are on:
 
15642
                               - 0 = Total sum level
 
15643
                               - 1 = First group changed  (a)
 
15644
                               - 2 = Second group changed (a,b)
 
15645
  @param table               reference to temp table
 
15646
 
 
15647
  @retval
 
15648
    0   ok
 
15649
  @retval
 
15650
    1   if write_data_failed()
 
15651
*/
 
15652
 
 
15653
int JOIN::rollup_write_data(uint idx, Table *table_arg)
 
15654
{
 
15655
  uint i;
 
15656
  for (i= send_group_parts ; i-- > idx ; )
 
15657
  {
 
15658
    /* Get reference pointers to sum functions in place */
 
15659
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15660
           ref_pointer_array_size);
 
15661
    if ((!having || having->val_int()))
 
15662
    {
 
15663
      int write_error;
 
15664
      Item *item;
 
15665
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15666
      while ((item= it++))
 
15667
      {
 
15668
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15669
          item->save_in_result_field(1);
 
15670
      }
 
15671
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15672
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15673
      {
 
15674
        if (create_myisam_from_heap(thd, table_arg, 
 
15675
                                    tmp_table_param.start_recinfo,
 
15676
                                    &tmp_table_param.recinfo,
 
15677
                                    write_error, 0))
 
15678
          return 1;                  
 
15679
      }
 
15680
    }
 
15681
  }
 
15682
  /* Restore ref_pointer_array */
 
15683
  set_items_ref_array(current_ref_pointer_array);
 
15684
  return 0;
 
15685
}
 
15686
 
 
15687
/**
 
15688
  clear results if there are not rows found for group
 
15689
  (end_send_group/end_write_group)
 
15690
*/
 
15691
 
 
15692
void JOIN::clear()
 
15693
{
 
15694
  clear_tables(this);
 
15695
  copy_fields(&tmp_table_param);
 
15696
 
 
15697
  if (sum_funcs)
 
15698
  {
 
15699
    Item_sum *func, **func_ptr= sum_funcs;
 
15700
    while ((func= *(func_ptr++)))
 
15701
      func->clear();
 
15702
  }
 
15703
}
 
15704
 
 
15705
/**
 
15706
  EXPLAIN handling.
 
15707
 
 
15708
  Send a description about what how the select will be done to stdout.
 
15709
*/
 
15710
 
 
15711
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15712
                     bool distinct,const char *message)
 
15713
{
 
15714
  List<Item> field_list;
 
15715
  List<Item> item_list;
 
15716
  THD *thd=join->thd;
 
15717
  select_result *result=join->result;
 
15718
  Item *item_null= new Item_null();
 
15719
  const CHARSET_INFO * const cs= system_charset_info;
 
15720
  int quick_type;
 
15721
  /* Don't log this into the slow query log */
 
15722
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15723
  join->unit->offset_limit_cnt= 0;
 
15724
 
 
15725
  /* 
 
15726
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15727
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
15728
  */
 
15729
  if (message)
 
15730
  {
 
15731
    item_list.push_back(new Item_int((int32_t)
 
15732
                                     join->select_lex->select_number));
 
15733
    item_list.push_back(new Item_string(join->select_lex->type,
 
15734
                                        strlen(join->select_lex->type), cs));
 
15735
    for (uint i=0 ; i < 7; i++)
 
15736
      item_list.push_back(item_null);
 
15737
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15738
      item_list.push_back(item_null);
 
15739
  
 
15740
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15741
    if (result->send_data(item_list))
 
15742
      join->error= 1;
 
15743
  }
 
15744
  else if (join->select_lex == join->unit->fake_select_lex)
 
15745
  {
 
15746
    /* 
 
15747
      here we assume that the query will return at least two rows, so we
 
15748
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15749
      and no filesort will be actually done, but executing all selects in
 
15750
      the UNION to provide precise EXPLAIN information will hardly be
 
15751
      appreciated :)
 
15752
    */
 
15753
    char table_name_buffer[NAME_LEN];
 
15754
    item_list.empty();
 
15755
    /* id */
 
15756
    item_list.push_back(new Item_null);
 
15757
    /* select_type */
 
15758
    item_list.push_back(new Item_string(join->select_lex->type,
 
15759
                                        strlen(join->select_lex->type),
 
15760
                                        cs));
 
15761
    /* table */
 
15762
    {
 
15763
      SELECT_LEX *sl= join->unit->first_select();
 
15764
      uint len= 6, lastop= 0;
 
15765
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15766
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15767
      {
 
15768
        len+= lastop;
 
15769
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15770
                         "%u,", sl->select_number);
 
15771
      }
 
15772
      if (sl || len + lastop >= NAME_LEN)
 
15773
      {
 
15774
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15775
        len+= 4;
 
15776
      }
 
15777
      else
 
15778
      {
 
15779
        len+= lastop;
 
15780
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15781
      }
 
15782
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15783
    }
 
15784
    /* type */
 
15785
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15786
                                          strlen(join_type_str[JT_ALL]),
 
15787
                                          cs));
 
15788
    /* possible_keys */
 
15789
    item_list.push_back(item_null);
 
15790
    /* key*/
 
15791
    item_list.push_back(item_null);
 
15792
    /* key_len */
 
15793
    item_list.push_back(item_null);
 
15794
    /* ref */
 
15795
    item_list.push_back(item_null);
 
15796
    /* in_rows */
 
15797
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15798
      item_list.push_back(item_null);
 
15799
    /* rows */
 
15800
    item_list.push_back(item_null);
 
15801
    /* extra */
 
15802
    if (join->unit->global_parameters->order_list.first)
 
15803
      item_list.push_back(new Item_string("Using filesort",
 
15804
                                          14, cs));
 
15805
    else
 
15806
      item_list.push_back(new Item_string("", 0, cs));
 
15807
 
 
15808
    if (result->send_data(item_list))
 
15809
      join->error= 1;
 
15810
  }
 
15811
  else
 
15812
  {
 
15813
    table_map used_tables=0;
 
15814
    for (uint i=0 ; i < join->tables ; i++)
 
15815
    {
 
15816
      JOIN_TAB *tab=join->join_tab+i;
 
15817
      Table *table=tab->table;
 
15818
      TableList *table_list= tab->table->pos_in_table_list;
 
15819
      char buff[512]; 
 
15820
      char buff1[512], buff2[512], buff3[512];
 
15821
      char keylen_str_buf[64];
 
15822
      String extra(buff, sizeof(buff),cs);
 
15823
      char table_name_buffer[NAME_LEN];
 
15824
      String tmp1(buff1,sizeof(buff1),cs);
 
15825
      String tmp2(buff2,sizeof(buff2),cs);
 
15826
      String tmp3(buff3,sizeof(buff3),cs);
 
15827
      extra.length(0);
 
15828
      tmp1.length(0);
 
15829
      tmp2.length(0);
 
15830
      tmp3.length(0);
 
15831
 
 
15832
      quick_type= -1;
 
15833
      item_list.empty();
 
15834
      /* id */
 
15835
      item_list.push_back(new Item_uint((uint32_t)
 
15836
                                       join->select_lex->select_number));
 
15837
      /* select_type */
 
15838
      item_list.push_back(new Item_string(join->select_lex->type,
 
15839
                                          strlen(join->select_lex->type),
 
15840
                                          cs));
 
15841
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15842
      {
 
15843
        quick_type= tab->select->quick->get_type();
 
15844
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15845
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15846
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15847
          tab->type = JT_INDEX_MERGE;
 
15848
        else
 
15849
          tab->type = JT_RANGE;
 
15850
      }
 
15851
      /* table */
 
15852
      if (table->derived_select_number)
 
15853
      {
 
15854
        /* Derived table name generation */
 
15855
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15856
                          "<derived%u>",
 
15857
                          table->derived_select_number);
 
15858
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15859
      }
 
15860
      else
 
15861
      {
 
15862
        TableList *real_table= table->pos_in_table_list; 
 
15863
        item_list.push_back(new Item_string(real_table->alias,
 
15864
                                            strlen(real_table->alias),
 
15865
                                            cs));
 
15866
      }
 
15867
      /* "type" column */
 
15868
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15869
                                          strlen(join_type_str[tab->type]),
 
15870
                                          cs));
 
15871
      /* Build "possible_keys" value and add it to item_list */
 
15872
      if (!tab->keys.is_clear_all())
 
15873
      {
 
15874
        uint j;
 
15875
        for (j=0 ; j < table->s->keys ; j++)
 
15876
        {
 
15877
          if (tab->keys.is_set(j))
 
15878
          {
 
15879
            if (tmp1.length())
 
15880
              tmp1.append(',');
 
15881
            tmp1.append(table->key_info[j].name, 
 
15882
                        strlen(table->key_info[j].name),
 
15883
                        system_charset_info);
 
15884
          }
 
15885
        }
 
15886
      }
 
15887
      if (tmp1.length())
 
15888
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15889
      else
 
15890
        item_list.push_back(item_null);
 
15891
 
 
15892
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15893
      if (tab->ref.key_parts)
 
15894
      {
 
15895
        KEY *key_info=table->key_info+ tab->ref.key;
 
15896
        register uint length;
 
15897
        item_list.push_back(new Item_string(key_info->name,
 
15898
                                            strlen(key_info->name),
 
15899
                                            system_charset_info));
 
15900
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
15901
                keylen_str_buf;
 
15902
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15903
                                            system_charset_info));
 
15904
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15905
        {
 
15906
          if (tmp2.length())
 
15907
            tmp2.append(',');
 
15908
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15909
                      system_charset_info);
 
15910
        }
 
15911
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15912
      }
 
15913
      else if (tab->type == JT_NEXT)
 
15914
      {
 
15915
        KEY *key_info=table->key_info+ tab->index;
 
15916
        register uint length;
 
15917
        item_list.push_back(new Item_string(key_info->name,
 
15918
                                            strlen(key_info->name),cs));
 
15919
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
15920
                keylen_str_buf;
 
15921
        item_list.push_back(new Item_string(keylen_str_buf, 
 
15922
                                            length,
 
15923
                                            system_charset_info));
 
15924
        item_list.push_back(item_null);
 
15925
      }
 
15926
      else if (tab->select && tab->select->quick)
 
15927
      {
 
15928
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15929
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15930
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15931
        item_list.push_back(item_null);
 
15932
      }
 
15933
      else
 
15934
      {
 
15935
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15936
        {
 
15937
          const char *tmp_buff;
 
15938
          int f_idx;
 
15939
          if (table_list->has_db_lookup_value)
 
15940
          {
 
15941
            f_idx= table_list->schema_table->idx_field1;
 
15942
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15943
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15944
          }          
 
15945
          if (table_list->has_table_lookup_value)
 
15946
          {
 
15947
            if (table_list->has_db_lookup_value)
 
15948
              tmp2.append(',');
 
15949
            f_idx= table_list->schema_table->idx_field2;
 
15950
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15951
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15952
          }
 
15953
          if (tmp2.length())
 
15954
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15955
          else
 
15956
            item_list.push_back(item_null);
 
15957
        }
 
15958
        else
 
15959
          item_list.push_back(item_null);
 
15960
        item_list.push_back(item_null);
 
15961
        item_list.push_back(item_null);
 
15962
      }
 
15963
      
 
15964
      /* Add "rows" field to item_list. */
 
15965
      if (table_list->schema_table)
 
15966
      {
 
15967
        /* in_rows */
 
15968
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15969
          item_list.push_back(item_null);
 
15970
        /* rows */
 
15971
        item_list.push_back(item_null);
 
15972
      }
 
15973
      else
 
15974
      {
 
15975
        double examined_rows;
 
15976
        if (tab->select && tab->select->quick)
 
15977
          examined_rows= rows2double(tab->select->quick->records);
 
15978
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15979
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
15980
                                     tab->table->file->records());
 
15981
        else
 
15982
          examined_rows= join->best_positions[i].records_read; 
 
15983
 
 
15984
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
15985
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
15986
 
 
15987
        /* Add "filtered" field to item_list. */
 
15988
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15989
        {
 
15990
          float f= 0.0; 
 
15991
          if (examined_rows)
 
15992
            f= (float) (100.0 * join->best_positions[i].records_read /
 
15993
                        examined_rows);
 
15994
          item_list.push_back(new Item_float(f, 2));
 
15995
        }
 
15996
      }
 
15997
 
 
15998
      /* Build "Extra" field and add it to item_list. */
 
15999
      bool key_read=table->key_read;
 
16000
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
16001
          table->covering_keys.is_set(tab->index))
 
16002
        key_read=1;
 
16003
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
16004
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
16005
        key_read=1;
 
16006
        
 
16007
      if (tab->info)
 
16008
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16009
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16010
      {
 
16011
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16012
          extra.append(STRING_WITH_LEN("; Using index"));
 
16013
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16014
          extra.append(STRING_WITH_LEN("; Using where"));
 
16015
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16016
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16017
        /* Skip initial "; "*/
 
16018
        const char *str= extra.ptr();
 
16019
        uint32_t len= extra.length();
 
16020
        if (len)
 
16021
        {
 
16022
          str += 2;
 
16023
          len -= 2;
 
16024
        }
 
16025
        item_list.push_back(new Item_string(str, len, cs));
 
16026
      }
 
16027
      else
 
16028
      {
 
16029
        uint keyno= MAX_KEY;
 
16030
        if (tab->ref.key_parts)
 
16031
          keyno= tab->ref.key;
 
16032
        else if (tab->select && tab->select->quick)
 
16033
          keyno = tab->select->quick->index;
 
16034
 
 
16035
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16036
            table->file->pushed_idx_cond)
 
16037
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16038
 
 
16039
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
16040
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16041
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16042
        {
 
16043
          extra.append(STRING_WITH_LEN("; Using "));
 
16044
          tab->select->quick->add_info_string(&extra);
 
16045
        }
 
16046
          if (tab->select)
 
16047
        {
 
16048
          if (tab->use_quick == 2)
 
16049
          {
 
16050
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16051
            char buf[MAX_KEY / 4 + 1];
 
16052
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16053
                                         "record (index map: 0x"));
 
16054
            extra.append(tab->keys.print(buf));
 
16055
            extra.append(')');
 
16056
          }
 
16057
          else if (tab->select->cond)
 
16058
          {
 
16059
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16060
 
 
16061
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
16062
            {
 
16063
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16064
                                           "condition"));
 
16065
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
16066
              {
 
16067
                extra.append(STRING_WITH_LEN(": "));
 
16068
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16069
              }
 
16070
            }
 
16071
            else
 
16072
              extra.append(STRING_WITH_LEN("; Using where"));
 
16073
          }
 
16074
        }
 
16075
        if (key_read)
 
16076
        {
 
16077
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16078
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16079
          else
 
16080
            extra.append(STRING_WITH_LEN("; Using index"));
 
16081
        }
 
16082
        if (table->reginfo.not_exists_optimize)
 
16083
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16084
          
 
16085
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16086
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16087
             HA_MRR_USE_DEFAULT_IMPL))
 
16088
        {
 
16089
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16090
        }
 
16091
 
 
16092
        if (table_list->schema_table &&
 
16093
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16094
        {
 
16095
          if (!table_list->table_open_method)
 
16096
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16097
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16098
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16099
          else
 
16100
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16101
          if (table_list->has_db_lookup_value &&
 
16102
              table_list->has_table_lookup_value)
 
16103
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16104
          else if (table_list->has_db_lookup_value ||
 
16105
                   table_list->has_table_lookup_value)
 
16106
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16107
          else
 
16108
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16109
        }
 
16110
        if (need_tmp_table)
 
16111
        {
 
16112
          need_tmp_table=0;
 
16113
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16114
        }
 
16115
        if (need_order)
 
16116
        {
 
16117
          need_order=0;
 
16118
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16119
        }
 
16120
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
16121
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16122
 
 
16123
        if (tab->insideout_match_tab)
 
16124
        {
 
16125
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16126
        }
 
16127
 
 
16128
        if (tab->flush_weedout_table)
 
16129
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16130
        else if (tab->check_weed_out_table)
 
16131
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16132
        else if (tab->do_firstmatch)
 
16133
        {
 
16134
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16135
          Table *prev_table=tab->do_firstmatch->table;
 
16136
          if (prev_table->derived_select_number)
 
16137
          {
 
16138
            char namebuf[NAME_LEN];
 
16139
            /* Derived table name generation */
 
16140
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16141
                              "<derived%u>",
 
16142
                              prev_table->derived_select_number);
 
16143
            extra.append(namebuf, len);
 
16144
          }
 
16145
          else
 
16146
            extra.append(prev_table->pos_in_table_list->alias);
 
16147
          extra.append(STRING_WITH_LEN(")"));
 
16148
        }
 
16149
 
 
16150
        for (uint part= 0; part < tab->ref.key_parts; part++)
 
16151
        {
 
16152
          if (tab->ref.cond_guards[part])
 
16153
          {
 
16154
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16155
            break;
 
16156
          }
 
16157
        }
 
16158
 
 
16159
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16160
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16161
 
 
16162
        /* Skip initial "; "*/
 
16163
        const char *str= extra.ptr();
 
16164
        uint32_t len= extra.length();
 
16165
        if (len)
 
16166
        {
 
16167
          str += 2;
 
16168
          len -= 2;
 
16169
        }
 
16170
        item_list.push_back(new Item_string(str, len, cs));
 
16171
      }
 
16172
      // For next iteration
 
16173
      used_tables|=table->map;
 
16174
      if (result->send_data(item_list))
 
16175
        join->error= 1;
 
16176
    }
 
16177
  }
 
16178
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
16179
       unit;
 
16180
       unit= unit->next_unit())
 
16181
  {
 
16182
    if (mysql_explain_union(thd, unit, result))
 
16183
      return;
 
16184
  }
 
16185
  return;
 
16186
}
 
16187
 
 
16188
 
 
16189
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
16190
{
 
16191
  bool res= 0;
 
16192
  SELECT_LEX *first= unit->first_select();
 
16193
 
 
16194
  for (SELECT_LEX *sl= first;
 
16195
       sl;
 
16196
       sl= sl->next_select())
 
16197
  {
 
16198
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16199
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16200
    sl->type= (((&thd->lex->select_lex)==sl)?
 
16201
               (sl->first_inner_unit() || sl->next_select() ? 
 
16202
                "PRIMARY" : "SIMPLE"):
 
16203
               ((sl == first)?
 
16204
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16205
                 "DERIVED":
 
16206
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16207
                  "DEPENDENT SUBQUERY":
 
16208
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16209
                   "SUBQUERY"))):
 
16210
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16211
                 "DEPENDENT UNION":
 
16212
                 uncacheable?"UNCACHEABLE UNION":
 
16213
                 "UNION")));
 
16214
    sl->options|= SELECT_DESCRIBE;
 
16215
  }
 
16216
  if (unit->is_union())
 
16217
  {
 
16218
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16219
    unit->fake_select_lex->type= "UNION RESULT";
 
16220
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16221
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16222
      res= unit->exec();
 
16223
    res|= unit->cleanup();
 
16224
  }
 
16225
  else
 
16226
  {
 
16227
    thd->lex->current_select= first;
 
16228
    unit->set_limit(unit->global_parameters);
 
16229
    res= mysql_select(thd, &first->ref_pointer_array,
 
16230
                        (TableList*) first->table_list.first,
 
16231
                        first->with_wild, first->item_list,
 
16232
                        first->where,
 
16233
                        first->order_list.elements +
 
16234
                        first->group_list.elements,
 
16235
                        (order_st*) first->order_list.first,
 
16236
                        (order_st*) first->group_list.first,
 
16237
                        first->having,
 
16238
                        (order_st*) thd->lex->proc_list.first,
 
16239
                        first->options | thd->options | SELECT_DESCRIBE,
 
16240
                        result, unit, first);
 
16241
  }
 
16242
  return(res || thd->is_error());
 
16243
}
 
16244
 
 
16245
 
 
16246
static void print_table_array(THD *thd, String *str, TableList **table, 
6321
16247
                              TableList **end)
6322
16248
{
6323
 
  (*table)->print(session, str, QT_ORDINARY);
 
16249
  (*table)->print(thd, str, QT_ORDINARY);
6324
16250
 
6325
16251
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6326
16252
  {
6332
16258
    }
6333
16259
    else if (curr->straight)
6334
16260
      str->append(STRING_WITH_LEN(" straight_join "));
 
16261
    else if (curr->sj_inner_tables)
 
16262
      str->append(STRING_WITH_LEN(" semi join "));
6335
16263
    else
6336
16264
      str->append(STRING_WITH_LEN(" join "));
6337
 
    curr->print(session, str, QT_ORDINARY);
 
16265
    curr->print(thd, str, QT_ORDINARY);
6338
16266
    if (curr->on_expr)
6339
16267
    {
6340
16268
      str->append(STRING_WITH_LEN(" on("));
6344
16272
  }
6345
16273
}
6346
16274
 
 
16275
 
6347
16276
/**
6348
16277
  Print joins from the FROM clause.
6349
 
  @param session     thread Cursor
 
16278
  @param thd     thread handler
6350
16279
  @param str     string where table should be printed
6351
16280
  @param tables  list of tables in join
6352
16281
  @query_type    type of the query is being generated
6353
16282
*/
6354
 
void print_join(Session *session, String *str,
6355
 
                List<TableList> *tables, enum_query_type)
 
16283
 
 
16284
static void print_join(THD *thd,
 
16285
                       String *str,
 
16286
                       List<TableList> *tables,
 
16287
                       enum_query_type query_type __attribute__((unused)))
6356
16288
{
6357
16289
  /* List is reversed => we should reverse it before using */
6358
16290
  List_iterator_fast<TableList> ti(*tables);
6359
 
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
 
16291
  TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
6360
16292
                                                tables->elements);
6361
16293
  if (table == 0)
6362
16294
    return;  // out of memory
6363
16295
 
6364
16296
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6365
16297
    *t= ti++;
 
16298
  
 
16299
  /* 
 
16300
    If the first table is a semi-join nest, swap it with something that is
 
16301
    not a semi-join nest.
 
16302
  */
 
16303
  if ((*table)->sj_inner_tables)
 
16304
  {
 
16305
    TableList **end= table + tables->elements;
 
16306
    for (TableList **t2= table; t2!=end; t2++)
 
16307
    {
 
16308
      if (!(*t2)->sj_inner_tables)
 
16309
      {
 
16310
        TableList *tmp= *t2;
 
16311
        *t2= *table;
 
16312
        *table= tmp;
 
16313
        break;
 
16314
      }
 
16315
    }
 
16316
  }
6366
16317
  assert(tables->elements >= 1);
6367
 
  print_table_array(session, str, table, table + tables->elements);
6368
 
}
6369
 
 
6370
 
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6371
 
{
6372
 
  /* QQ: session may not be set for sub queries, but this should be fixed */
6373
 
  if(not session)
6374
 
    session= current_session;
6375
 
 
 
16318
  print_table_array(thd, str, table, table + tables->elements);
 
16319
}
 
16320
 
 
16321
 
 
16322
/**
 
16323
  @brief Print an index hint
 
16324
 
 
16325
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16326
 
 
16327
  @param      thd         the current thread
 
16328
  @param[out] str         appends the index hint here
 
16329
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16330
                          "FORCE INDEX"|"IGNORE INDEX")
 
16331
  @param      hint_length the length of the string in 'hint'
 
16332
  @param      indexes     a list of index names for the hint
 
16333
*/
 
16334
 
 
16335
void 
 
16336
Index_hint::print(THD *thd, String *str)
 
16337
{
 
16338
  switch (type)
 
16339
  {
 
16340
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16341
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16342
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16343
  }
 
16344
  str->append (STRING_WITH_LEN(" ("));
 
16345
  if (key_name.length)
 
16346
  {
 
16347
    if (thd && !my_strnncoll(system_charset_info,
 
16348
                             (const uchar *)key_name.str, key_name.length, 
 
16349
                             (const uchar *)primary_key_name, 
 
16350
                             strlen(primary_key_name)))
 
16351
      str->append(primary_key_name);
 
16352
    else
 
16353
      append_identifier(thd, str, key_name.str, key_name.length);
 
16354
  }
 
16355
  str->append(')');
 
16356
}
 
16357
 
 
16358
 
 
16359
/**
 
16360
  Print table as it should be in join list.
 
16361
 
 
16362
  @param str   string where table should be printed
 
16363
*/
 
16364
 
 
16365
void TableList::print(THD *thd, String *str, enum_query_type query_type)
 
16366
{
 
16367
  if (nested_join)
 
16368
  {
 
16369
    str->append('(');
 
16370
    print_join(thd, str, &nested_join->join_list, query_type);
 
16371
    str->append(')');
 
16372
  }
 
16373
  else
 
16374
  {
 
16375
    const char *cmp_name;                         // Name to compare with alias
 
16376
    if (derived)
 
16377
    {
 
16378
      // A derived table
 
16379
      str->append('(');
 
16380
      derived->print(str, query_type);
 
16381
      str->append(')');
 
16382
      cmp_name= "";                               // Force printing of alias
 
16383
    }
 
16384
    else
 
16385
    {
 
16386
      // A normal table
 
16387
      {
 
16388
        append_identifier(thd, str, db, db_length);
 
16389
        str->append('.');
 
16390
      }
 
16391
      if (schema_table)
 
16392
      {
 
16393
        append_identifier(thd, str, schema_table_name,
 
16394
                          strlen(schema_table_name));
 
16395
        cmp_name= schema_table_name;
 
16396
      }
 
16397
      else
 
16398
      {
 
16399
        append_identifier(thd, str, table_name, table_name_length);
 
16400
        cmp_name= table_name;
 
16401
      }
 
16402
    }
 
16403
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16404
    {
 
16405
      char t_alias_buff[MAX_ALIAS_NAME];
 
16406
      const char *t_alias= alias;
 
16407
 
 
16408
      str->append(' ');
 
16409
      if (lower_case_table_names== 1)
 
16410
      {
 
16411
        if (alias && alias[0])
 
16412
        {
 
16413
          stpcpy(t_alias_buff, alias);
 
16414
          my_casedn_str(files_charset_info, t_alias_buff);
 
16415
          t_alias= t_alias_buff;
 
16416
        }
 
16417
      }
 
16418
 
 
16419
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
16420
    }
 
16421
 
 
16422
    if (index_hints)
 
16423
    {
 
16424
      List_iterator<Index_hint> it(*index_hints);
 
16425
      Index_hint *hint;
 
16426
 
 
16427
      while ((hint= it++))
 
16428
      {
 
16429
        str->append (STRING_WITH_LEN(" "));
 
16430
        hint->print (thd, str);
 
16431
      }
 
16432
    }
 
16433
  }
 
16434
}
 
16435
 
 
16436
 
 
16437
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
16438
{
 
16439
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
16440
  if (!thd)
 
16441
    thd= current_thd;
6376
16442
 
6377
16443
  str->append(STRING_WITH_LEN("select "));
6378
16444
 
6379
16445
  /* First add options */
6380
16446
  if (options & SELECT_STRAIGHT_JOIN)
6381
16447
    str->append(STRING_WITH_LEN("straight_join "));
 
16448
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16449
      (this == &thd->lex->select_lex))
 
16450
    str->append(STRING_WITH_LEN("high_priority "));
6382
16451
  if (options & SELECT_DISTINCT)
6383
16452
    str->append(STRING_WITH_LEN("distinct "));
6384
16453
  if (options & SELECT_SMALL_RESULT)
6411
16480
  {
6412
16481
    str->append(STRING_WITH_LEN(" from "));
6413
16482
    /* go through join tree */
6414
 
    print_join(session, str, &top_join_list, query_type);
 
16483
    print_join(thd, str, &top_join_list, query_type);
6415
16484
  }
6416
16485
  else if (where)
6417
16486
  {
6418
16487
    /*
6419
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
16488
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6420
16489
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6421
16490
    */
6422
16491
    str->append(STRING_WITH_LEN(" from DUAL "));
6439
16508
  if (group_list.elements)
6440
16509
  {
6441
16510
    str->append(STRING_WITH_LEN(" group by "));
6442
 
    print_order(str, (Order *) group_list.first, query_type);
 
16511
    print_order(str, (order_st *) group_list.first, query_type);
6443
16512
    switch (olap)
6444
16513
    {
6445
16514
      case CUBE_TYPE:
6470
16539
  if (order_list.elements)
6471
16540
  {
6472
16541
    str->append(STRING_WITH_LEN(" order by "));
6473
 
    print_order(str, (Order *) order_list.first, query_type);
 
16542
    print_order(str, (order_st *) order_list.first, query_type);
6474
16543
  }
6475
16544
 
6476
16545
  // limit
6477
 
  print_limit(session, str, query_type);
 
16546
  print_limit(thd, str, query_type);
6478
16547
 
6479
16548
  // PROCEDURE unsupported here
6480
16549
}
6481
16550
 
 
16551
 
 
16552
/**
 
16553
  change select_result object of JOIN.
 
16554
 
 
16555
  @param res            new select_result object
 
16556
 
 
16557
  @retval
 
16558
    false   OK
 
16559
  @retval
 
16560
    true    error
 
16561
*/
 
16562
 
 
16563
bool JOIN::change_result(select_result *res)
 
16564
{
 
16565
  result= res;
 
16566
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
16567
                     result->prepare2())
 
16568
  {
 
16569
    return(true);
 
16570
  }
 
16571
  return(false);
 
16572
}
 
16573
 
6482
16574
/**
6483
16575
  @} (end of group Query_Optimizer)
6484
16576
*/
6485
 
 
6486
 
} /* namespace drizzled */