~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_select.cc

  • Committer: Mark Atwood
  • Date: 2008-07-12 11:37:59 UTC
  • mto: This revision was merged to the branch mainline in revision 139.
  • Revision ID: me@mark.atwood.name-20080712113759-nrjn1bq1e0shuory
Add hello_world() UDF

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
 
 
27
#ifdef USE_PRAGMA_IMPLEMENTATION
 
28
#pragma implementation                          // gcc: Class implementation
 
29
#endif
 
30
 
 
31
#include "mysql_priv.h"
 
32
#include "sql_select.h"
 
33
 
 
34
#include <m_ctype.h>
 
35
#include <my_bit.h>
 
36
#include <hash.h>
 
37
 
 
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
39
                              "MAYBE_REF","ALL","range","index",
 
40
                              "ref_or_null","unique_subquery","index_subquery",
 
41
                              "index_merge"
 
42
};
 
43
 
 
44
struct st_sargable_param;
 
45
 
 
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
 
48
                                 DYNAMIC_ARRAY *keyuse);
 
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
50
                                JOIN_TAB *join_tab,
 
51
                                uint tables, COND *conds,
 
52
                                COND_EQUAL *cond_equal,
 
53
                                table_map table_map, SELECT_LEX *select_lex,
 
54
                                st_sargable_param **sargables);
 
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
 
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
58
                               table_map used_tables);
 
59
static bool choose_plan(JOIN *join,table_map join_tables);
 
60
 
 
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
62
                             table_map remaining_tables, uint idx,
 
63
                             double record_count, double read_time);
 
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
66
                             uint depth, uint prune_level);
 
67
static bool best_extension_by_limited_search(JOIN *join,
 
68
                                             table_map remaining_tables,
 
69
                                             uint idx, double record_count,
 
70
                                             double read_time, uint depth,
 
71
                                             uint prune_level);
 
72
static uint determine_search_depth(JOIN* join);
 
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
75
/*
 
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
77
  tested and approved.
 
78
*/
 
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
 
80
                      double record_count,double read_time);
 
81
static uint cache_record_length(JOIN *join,uint index);
 
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
 
83
static bool get_best_combination(JOIN *join);
 
84
static store_key *get_store_key(THD *thd,
 
85
                                KEYUSE *keyuse, table_map used_tables,
 
86
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
87
                                uint maybe_null);
 
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
 
89
static void make_outerjoin_info(JOIN *join);
 
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
91
static bool make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after);
 
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
 
93
static void update_depend_map(JOIN *join);
 
94
static void update_depend_map(JOIN *join, ORDER *order);
 
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
 
96
                           bool change_list, bool *simple_order);
 
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
 
98
                            List<Item> &fields, bool send_row,
 
99
                            ulonglong select_options, const char *info,
 
100
                            Item *having);
 
101
static COND *build_equal_items(THD *thd, COND *cond,
74
102
                               COND_EQUAL *inherited,
75
 
                               List<TableList> *join_list,
 
103
                               List<TABLE_LIST> *join_list,
76
104
                               COND_EQUAL **cond_equal_ref);
77
 
 
78
 
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);
87
 
 
88
 
static bool eval_const_cond(COND *cond)
89
 
{
90
 
    return ((Item_func*) cond)->val_int() ? true : false;
91
 
}
 
105
static COND* substitute_for_best_equal_field(COND *cond,
 
106
                                             COND_EQUAL *cond_equal,
 
107
                                             void *table_join_idx);
 
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
 
109
                            COND *conds, bool top, bool in_sj);
 
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
111
static void restore_prev_nj_state(JOIN_TAB *last);
 
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
 
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
 
114
                                          uint first_unused);
 
115
 
 
116
static 
 
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
118
static void restore_prev_sj_state(const table_map remaining_tables, 
 
119
                                  const JOIN_TAB *tab);
 
120
 
 
121
static COND *optimize_cond(JOIN *join, COND *conds,
 
122
                           List<TABLE_LIST> *join_list,
 
123
                           Item::cond_result *cond_value);
 
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
125
static bool open_tmp_table(TABLE *table);
 
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
127
                                    MI_COLUMNDEF *start_recinfo,
 
128
                                    MI_COLUMNDEF **recinfo,
 
129
                                    ulonglong options);
 
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
 
131
 
 
132
static enum_nested_loop_state
 
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
134
                     int error);
 
135
static enum_nested_loop_state
 
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
137
static enum_nested_loop_state
 
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
139
static enum_nested_loop_state
 
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
141
static enum_nested_loop_state
 
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
143
static enum_nested_loop_state
 
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
145
static enum_nested_loop_state
 
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
147
 
 
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
149
static int join_read_system(JOIN_TAB *tab);
 
150
static int join_read_const(JOIN_TAB *tab);
 
151
static int join_read_key(JOIN_TAB *tab);
 
152
static int join_read_always_key(JOIN_TAB *tab);
 
153
static int join_read_last_key(JOIN_TAB *tab);
 
154
static int join_no_more_records(READ_RECORD *info);
 
155
static int join_read_next(READ_RECORD *info);
 
156
static int join_read_next_different(READ_RECORD *info);
 
157
static int join_init_quick_read_record(JOIN_TAB *tab);
 
158
static int test_if_quick_select(JOIN_TAB *tab);
 
159
static int join_init_read_record(JOIN_TAB *tab);
 
160
static int join_read_first(JOIN_TAB *tab);
 
161
static int join_read_next_same(READ_RECORD *info);
 
162
static int join_read_next_same_diff(READ_RECORD *info);
 
163
static int join_read_last(JOIN_TAB *tab);
 
164
static int join_read_prev_same(READ_RECORD *info);
 
165
static int join_read_prev(READ_RECORD *info);
 
166
int join_read_always_key_or_null(JOIN_TAB *tab);
 
167
int join_read_next_same_or_null(READ_RECORD *info);
 
168
static COND *make_cond_for_table(COND *cond,table_map table,
 
169
                                 table_map used_table,
 
170
                                 bool exclude_expensive_cond);
 
171
static Item* part_of_refkey(TABLE *form,Field *field);
 
172
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
 
173
                                    ha_rows select_limit, bool no_changes,
 
174
                                    const key_map *map);
 
175
static bool list_contains_unique_index(TABLE *table,
 
176
                          bool (*find_func) (Field *, void *), void *data);
 
177
static bool find_field_in_item_list (Field *field, void *data);
 
178
static bool find_field_in_order_list (Field *field, void *data);
 
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
180
                             ha_rows filesort_limit, ha_rows select_limit,
 
181
                             bool is_order_by);
 
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
 
183
                             Item *having);
 
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
 
185
                                   ulong offset,Item *having);
 
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
187
                                      uint field_count, Field **first_field,
 
188
 
 
189
                                      ulong key_length,Item *having);
 
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
 
191
static ulong used_blob_length(CACHE_FIELD **ptr);
 
192
static bool store_record_in_cache(JOIN_CACHE *cache);
 
193
static void reset_cache_read(JOIN_CACHE *cache);
 
194
static void reset_cache_write(JOIN_CACHE *cache);
 
195
static void read_cached_record(JOIN_TAB *tab);
 
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
198
                                    ORDER *order, List<Item> &fields,
 
199
                                    List<Item> &all_fields,
 
200
                                    bool *all_order_by_fields_used);
 
201
static bool test_if_subpart(ORDER *a,ORDER *b);
 
202
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
 
203
static void calc_group_buffer(JOIN *join,ORDER *group);
 
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
 
206
// Create list for using with tempory table
 
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
208
                                     List<Item> &new_list1,
 
209
                                     List<Item> &new_list2,
 
210
                                     uint elements, List<Item> &items);
 
211
// Create list for using with tempory table
 
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
213
                                      List<Item> &new_list1,
 
214
                                      List<Item> &new_list2,
 
215
                                      uint elements, List<Item> &items);
 
216
static void init_tmptable_sum_functions(Item_sum **func);
 
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
 
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
222
static bool update_sum_func(Item_sum **func);
 
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
224
                            bool distinct, const char *message=NullS);
 
225
static Item *remove_additional_cond(Item* conds);
 
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
228
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
229
                                       Item *new_cond, bool fix_fields);
92
230
 
93
231
/*
94
232
  This is used to mark equalities that were made from i-th IN-equality.
98
236
const char *subq_sj_cond_name=
99
237
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
100
238
 
101
 
static bool copy_blobs(Field **ptr)
 
239
static bool bitmap_covers(const table_map x, const table_map y)
102
240
{
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;
 
241
  return !test(y & ~x);
110
242
}
111
243
 
112
244
/**
113
245
  This handles SELECT with and without UNION.
114
246
*/
115
 
bool handle_select(Session *session, LEX *lex, select_result *result,
116
 
                   uint64_t setup_tables_done_option)
 
247
 
 
248
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
249
                   ulong setup_tables_done_option)
117
250
{
118
251
  bool res;
119
 
  register Select_Lex *select_lex= &lex->select_lex;
120
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
252
  register SELECT_LEX *select_lex = &lex->select_lex;
 
253
  MYSQL_SELECT_START();
121
254
 
122
 
  if (select_lex->master_unit()->is_union() ||
 
255
  if (select_lex->master_unit()->is_union() || 
123
256
      select_lex->master_unit()->fake_select_lex)
124
 
  {
125
 
    res= drizzle_union(session, lex, result, &lex->unit,
126
 
                       setup_tables_done_option);
127
 
  }
 
257
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
128
258
  else
129
259
  {
130
 
    Select_Lex_Unit *unit= &lex->unit;
 
260
    SELECT_LEX_UNIT *unit= &lex->unit;
131
261
    unit->set_limit(unit->global_parameters);
132
 
    session->session_marker= 0;
 
262
    thd->thd_marker= 0;
133
263
    /*
134
264
      '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
 
265
      every PS/SP execution new, we will not need reset this flag if 
136
266
      setup_tables_done_option changed for next rexecution
137
267
    */
138
 
    res= mysql_select(session,
139
 
                      &select_lex->ref_pointer_array,
140
 
                      (TableList*) select_lex->table_list.first,
141
 
                      select_lex->with_wild,
142
 
                      select_lex->item_list,
 
268
    res= mysql_select(thd, &select_lex->ref_pointer_array,
 
269
                      (TABLE_LIST*) select_lex->table_list.first,
 
270
                      select_lex->with_wild, select_lex->item_list,
143
271
                      select_lex->where,
144
272
                      select_lex->order_list.elements +
145
273
                      select_lex->group_list.elements,
146
 
                      (Order*) select_lex->order_list.first,
147
 
                      (Order*) select_lex->group_list.first,
 
274
                      (ORDER*) select_lex->order_list.first,
 
275
                      (ORDER*) select_lex->group_list.first,
148
276
                      select_lex->having,
149
 
                      select_lex->options | session->options |
 
277
                      (ORDER*) lex->proc_list.first,
 
278
                      select_lex->options | thd->options |
150
279
                      setup_tables_done_option,
151
280
                      result, unit, select_lex);
152
281
  }
153
 
  res|= session->is_error();
 
282
  res|= thd->is_error();
154
283
  if (unlikely(res))
155
284
    result->abort();
156
285
 
157
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
158
 
  return res;
 
286
  MYSQL_SELECT_END();
 
287
  return(res);
159
288
}
160
289
 
 
290
 
161
291
/*
162
292
  Fix fields referenced from inner selects.
163
293
 
164
294
  SYNOPSIS
165
295
    fix_inner_refs()
166
 
    session               Thread handle
 
296
    thd               Thread handle
167
297
    all_fields        List of all fields used in select
168
298
    select            Current select
169
299
    ref_pointer_array Array of references to Items used in current select
198
328
    true  an error occured
199
329
    false ok
200
330
*/
201
 
bool fix_inner_refs(Session *session, 
202
 
                    List<Item> &all_fields, 
203
 
                    Select_Lex *select, 
204
 
                    Item **ref_pointer_array)
 
331
 
 
332
bool
 
333
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
334
                 Item **ref_pointer_array)
205
335
{
206
336
  Item_outer_ref *ref;
207
337
  bool res= false;
261
391
    ref->outer_ref= new_ref;
262
392
    ref->ref= &ref->outer_ref;
263
393
 
264
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
394
    if (!ref->fixed && ref->fix_fields(thd, 0))
265
395
      return true;
266
 
    session->used_tables|= item->used_tables();
 
396
    thd->used_tables|= item->used_tables();
267
397
  }
268
398
  return res;
269
399
}
270
400
 
 
401
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
402
/**
 
403
  Function to setup clauses without sum functions.
 
404
*/
 
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
406
                               TABLE_LIST *tables,
 
407
                               TABLE_LIST *leaves,
 
408
                               List<Item> &fields,
 
409
                               List<Item> &all_fields,
 
410
                               COND **conds,
 
411
                               ORDER *order,
 
412
                               ORDER *group, bool *hidden_group_fields)
 
413
{
 
414
  int res;
 
415
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
416
 
 
417
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
418
  res= setup_conds(thd, tables, leaves, conds);
 
419
 
 
420
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
421
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
422
                          order);
 
423
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
424
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
425
                          group, hidden_group_fields);
 
426
  thd->lex->allow_sum_func= save_allow_sum_func;
 
427
  return(res);
 
428
}
 
429
 
271
430
/*****************************************************************************
272
431
  Check fields, find best join, do the select and output fields.
273
432
  mysql_select assumes that all tables are already opened
274
433
*****************************************************************************/
275
434
 
 
435
/**
 
436
  Prepare of whole select (including sub queries in future).
 
437
 
 
438
  @todo
 
439
    Add check of calculation of GROUP functions and fields:
 
440
    SELECT COUNT(*)+table.col1 from table1;
 
441
 
 
442
  @retval
 
443
    -1   on error
 
444
  @retval
 
445
    0   on success
 
446
*/
 
447
int
 
448
JOIN::prepare(Item ***rref_pointer_array,
 
449
              TABLE_LIST *tables_init,
 
450
              uint wild_num, COND *conds_init, uint og_num,
 
451
              ORDER *order_init, ORDER *group_init,
 
452
              Item *having_init,
 
453
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
 
454
              SELECT_LEX_UNIT *unit_arg)
 
455
{
 
456
  // to prevent double initialization on EXPLAIN
 
457
  if (optimized)
 
458
    return(0);
 
459
 
 
460
  conds= conds_init;
 
461
  order= order_init;
 
462
  group_list= group_init;
 
463
  having= having_init;
 
464
  proc_param= proc_param_init;
 
465
  tables_list= tables_init;
 
466
  select_lex= select_lex_arg;
 
467
  select_lex->join= this;
 
468
  join_list= &select_lex->top_join_list;
 
469
  union_part= unit_arg->is_union();
 
470
 
 
471
  thd->lex->current_select->is_item_list_lookup= 1;
 
472
  /*
 
473
    If we have already executed SELECT, then it have not sense to prevent
 
474
    its table from update (see unique_table())
 
475
  */
 
476
  if (thd->derived_tables_processing)
 
477
    select_lex->exclude_from_table_unique_test= true;
 
478
 
 
479
  /* Check that all tables, fields, conds and order are ok */
 
480
 
 
481
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
482
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
483
                                    tables_list, &select_lex->leaf_tables,
 
484
                                    false))
 
485
      return(-1);
 
486
 
 
487
  TABLE_LIST *table_ptr;
 
488
  for (table_ptr= select_lex->leaf_tables;
 
489
       table_ptr;
 
490
       table_ptr= table_ptr->next_leaf)
 
491
    tables++;
 
492
 
 
493
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
494
      select_lex->setup_ref_array(thd, og_num) ||
 
495
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
496
                   &all_fields, 1) ||
 
497
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
498
                          select_lex->leaf_tables, fields_list,
 
499
                          all_fields, &conds, order, group_list,
 
500
                          &hidden_group_fields))
 
501
    return(-1);                         /* purecov: inspected */
 
502
 
 
503
  ref_pointer_array= *rref_pointer_array;
 
504
  
 
505
  if (having)
 
506
  {
 
507
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
508
    thd->where="having clause";
 
509
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
510
    select_lex->having_fix_field= 1;
 
511
    bool having_fix_rc= (!having->fixed &&
 
512
                         (having->fix_fields(thd, &having) ||
 
513
                          having->check_cols(1)));
 
514
    select_lex->having_fix_field= 0;
 
515
    if (having_fix_rc || thd->is_error())
 
516
      return(-1);                               /* purecov: inspected */
 
517
    thd->lex->allow_sum_func= save_allow_sum_func;
 
518
  }
 
519
 
 
520
  if (!thd->lex->view_prepare_mode)
 
521
  {
 
522
    Item_subselect *subselect;
 
523
    Item_in_subselect *in_subs= NULL;
 
524
    /*
 
525
      Are we in a subquery predicate?
 
526
      TODO: the block below will be executed for every PS execution without need.
 
527
    */
 
528
    if ((subselect= select_lex->master_unit()->item))
 
529
    {
 
530
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
531
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
532
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
533
        in_subs= (Item_in_subselect*)subselect;
 
534
 
 
535
      /*
 
536
        Check if we're in subquery that is a candidate for flattening into a
 
537
        semi-join (which is done done in flatten_subqueries()). The
 
538
        requirements are:
 
539
          1. Subquery predicate is an IN/=ANY subq predicate
 
540
          2. Subquery is a single SELECT (not a UNION)
 
541
          3. Subquery does not have GROUP BY or ORDER BY
 
542
          4. Subquery does not use aggregate functions or HAVING
 
543
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
544
          6. No execution method was already chosen (by a prepared statement).
 
545
 
 
546
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
547
               doesn't have a JOIN (TODO: We should handle this at some
 
548
               point by switching to multi-table UPDATE/DELETE)
 
549
 
 
550
          (**). We're not in a confluent table-less subquery, like
 
551
                "SELECT 1". 
 
552
      */
 
553
      if (in_subs &&                                                    // 1
 
554
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
555
          !select_lex->group_list.elements && !order &&                 // 3
 
556
          !having && !select_lex->with_sum_func &&                      // 4
 
557
          thd->thd_marker &&                                            // 5
 
558
          select_lex->outer_select()->join &&                           // (*)
 
559
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
560
          do_semijoin &&
 
561
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
562
      {
 
563
        {
 
564
          if (!in_subs->left_expr->fixed &&
 
565
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
566
          {
 
567
            return(-1);
 
568
          }
 
569
          /*
 
570
            Check that the right part of the subselect contains no more than one
 
571
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
572
          */
 
573
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
574
             (select_lex->item_list.elements != 
 
575
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
576
          {
 
577
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
578
            return(-1);
 
579
          }
 
580
        }
 
581
 
 
582
        /* Register the subquery for further processing */
 
583
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
584
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
 
585
      }
 
586
      else
 
587
      {
 
588
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
589
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
590
        /*
 
591
          Check if the subquery predicate can be executed via materialization.
 
592
          The required conditions are:
 
593
          1. Subquery predicate is an IN/=ANY subq predicate
 
594
          2. Subquery is a single SELECT (not a UNION)
 
595
          3. Subquery is not a table-less query. In this case there is no
 
596
             point in materializing.
 
597
          4. Subquery predicate is a top-level predicate
 
598
             (this implies it is not negated)
 
599
             TODO: this is a limitation that should be lifeted once we
 
600
             implement correct NULL semantics (WL#3830)
 
601
          5. Subquery is non-correlated
 
602
             TODO:
 
603
             This is an overly restrictive condition. It can be extended to:
 
604
             (Subquery is non-correlated ||
 
605
              Subquery is correlated to any query outer to IN predicate ||
 
606
              (Subquery is correlated to the immediate outer query &&
 
607
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
 
608
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
609
          6. No execution method was already chosen (by a prepared statement).
 
610
 
 
611
          (*) The subquery must be part of a SELECT statement. The current
 
612
               condition also excludes multi-table update statements.
 
613
 
 
614
          We have to determine whether we will perform subquery materialization
 
615
          before calling the IN=>EXISTS transformation, so that we know whether to
 
616
          perform the whole transformation or only that part of it which wraps
 
617
          Item_in_subselect in an Item_in_optimizer.
 
618
        */
 
619
        if (do_materialize && 
 
620
            in_subs  &&                                                   // 1
 
621
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
622
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
623
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
624
        {
 
625
          if (in_subs->is_top_level_item() &&                             // 4
 
626
              !in_subs->is_correlated &&                                  // 5
 
627
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
628
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
629
        }
 
630
 
 
631
        Item_subselect::trans_res trans_res;
 
632
        if ((trans_res= subselect->select_transformer(this)) !=
 
633
            Item_subselect::RES_OK)
 
634
        {
 
635
          select_lex->fix_prepare_information(thd, &conds, &having);
 
636
          return((trans_res == Item_subselect::RES_ERROR));
 
637
        }
 
638
      }
 
639
    }
 
640
  }
 
641
 
 
642
  select_lex->fix_prepare_information(thd, &conds, &having);
 
643
 
 
644
  if (order)
 
645
  {
 
646
    ORDER *ord;
 
647
    for (ord= order; ord; ord= ord->next)
 
648
    {
 
649
      Item *item= *ord->item;
 
650
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
651
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
652
    }
 
653
  }
 
654
 
 
655
  if (having && having->with_sum_func)
 
656
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
657
                            &having, true);
 
658
  if (select_lex->inner_sum_func_list)
 
659
  {
 
660
    Item_sum *end=select_lex->inner_sum_func_list;
 
661
    Item_sum *item_sum= end;  
 
662
    do
 
663
    { 
 
664
      item_sum= item_sum->next;
 
665
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
666
                                all_fields, item_sum->ref_by, false);
 
667
    } while (item_sum != end);
 
668
  }
 
669
 
 
670
  if (select_lex->inner_refs_list.elements &&
 
671
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
672
    return(-1);
 
673
 
 
674
  /*
 
675
    Check if there are references to un-aggregated columns when computing 
 
676
    aggregate functions with implicit grouping (there is no GROUP BY).
 
677
 
 
678
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
679
  */
 
680
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
681
  {
 
682
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
683
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
684
    return(-1);
 
685
  }
 
686
  {
 
687
    /* Caclulate the number of groups */
 
688
    send_group_parts= 0;
 
689
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
690
      send_group_parts++;
 
691
  }
 
692
  
 
693
  if (error)
 
694
    goto err;                                   /* purecov: inspected */
 
695
 
 
696
  if (result && result->prepare(fields_list, unit_arg))
 
697
    goto err;                                   /* purecov: inspected */
 
698
 
 
699
  /* Init join struct */
 
700
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
701
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
702
  this->group= group_list != 0;
 
703
  unit= unit_arg;
 
704
 
 
705
#ifdef RESTRICTED_GROUP
 
706
  if (sum_func_count && !group_list && (func_count || field_count))
 
707
  {
 
708
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
709
    goto err;
 
710
  }
 
711
#endif
 
712
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
713
    goto err;
 
714
  if (alloc_func_list())
 
715
    goto err;
 
716
 
 
717
  return(0); // All OK
 
718
 
 
719
err:
 
720
  return(-1);                           /* purecov: inspected */
 
721
}
 
722
 
 
723
 
 
724
/*
 
725
  Remove the predicates pushed down into the subquery
 
726
 
 
727
  SYNOPSIS
 
728
    JOIN::remove_subq_pushed_predicates()
 
729
      where   IN  Must be NULL
 
730
              OUT The remaining WHERE condition, or NULL
 
731
 
 
732
  DESCRIPTION
 
733
    Given that this join will be executed using (unique|index)_subquery,
 
734
    without "checking NULL", remove the predicates that were pushed down
 
735
    into the subquery.
 
736
 
 
737
    If the subquery compares scalar values, we can remove the condition that
 
738
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
739
    engine)
 
740
 
 
741
    If the subquery compares row values, we need to keep the wrapped
 
742
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
743
    and non-NULL values, we'll do a full table scan and will rely on the
 
744
    equalities corresponding to non-NULL parts of left tuple to filter out
 
745
    non-matching records.
 
746
 
 
747
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
748
    fact that subquery engine will be using index lookup. This must be done only
 
749
    for cases where there are no conversion errors of significance, e.g. 257
 
750
    that is searched in a byte. But this requires homogenization of the return 
 
751
    codes of all Field*::store() methods.
 
752
*/
 
753
 
 
754
void JOIN::remove_subq_pushed_predicates(Item **where)
 
755
{
 
756
  if (conds->type() == Item::FUNC_ITEM &&
 
757
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
758
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
759
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
760
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
761
                   ((Item_func *)conds)->arguments()[0]))
 
762
  {
 
763
    *where= 0;
 
764
    return;
 
765
  }
 
766
}
 
767
 
 
768
 
276
769
/*
277
770
  Index lookup-based subquery: save some flags for EXPLAIN output
278
771
 
285
778
  DESCRIPTION
286
779
    For index lookup-based subquery (i.e. one executed with
287
780
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
288
 
    check its EXPLAIN output row should contain
289
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
781
    check its EXPLAIN output row should contain 
 
782
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
290
783
      "Using Where" (TAB_INFO_USING_WHERE)
291
784
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
292
785
    and set appropriate flags in join_tab->packed_info.
293
786
*/
294
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
787
 
 
788
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
295
789
{
296
790
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
297
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
791
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
298
792
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
299
793
  if (where)
300
794
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
301
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
795
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
302
796
  {
303
797
    if (join_tab->ref.cond_guards[i])
304
798
    {
308
802
  }
309
803
}
310
804
 
 
805
 
 
806
 
 
807
 
 
808
/*
 
809
  Check if the table's rowid is included in the temptable
 
810
 
 
811
  SYNOPSIS
 
812
    sj_table_is_included()
 
813
      join      The join
 
814
      join_tab  The table to be checked
 
815
 
 
816
  DESCRIPTION
 
817
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
818
    in the temptable. This is so if
 
819
 
 
820
    1. The table is not embedded within some semi-join nest
 
821
    2. The has been pulled out of a semi-join nest, or
 
822
 
 
823
    3. The table is functionally dependent on some previous table
 
824
 
 
825
    [4. This is also true for constant tables that can't be
 
826
        NULL-complemented but this function is not called for such tables]
 
827
 
 
828
  RETURN
 
829
    true  - Include table's rowid
 
830
    false - Don't
 
831
*/
 
832
 
 
833
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
834
{
 
835
  if (join_tab->emb_sj_nest)
 
836
    return false;
 
837
  
 
838
  /* Check if this table is functionally dependent on the tables that
 
839
     are within the same outer join nest
 
840
  */
 
841
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
 
842
  if (join_tab->type == JT_EQ_REF)
 
843
  {
 
844
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
845
    uint idx;
 
846
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
847
    {
 
848
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
849
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
850
        return true;
 
851
    }
 
852
    /* Ok, functionally dependent */
 
853
    return false;
 
854
  }
 
855
  /* Not functionally dependent => need to include*/
 
856
  return true;
 
857
}
 
858
 
 
859
 
 
860
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
 
861
                                          SJ_TMP_TABLE *sjtbl);
 
862
 
 
863
 
 
864
/*
 
865
  Setup the strategies to eliminate semi-join duplicates.
 
866
  
 
867
  SYNOPSIS
 
868
    setup_semijoin_dups_elimination()
 
869
      join           Join to process
 
870
      options        Join options (needed to see if join buffering will be 
 
871
                     used or not)
 
872
      no_jbuf_after  Another bit of information re where join buffering will
 
873
                     be used.
 
874
 
 
875
  DESCRIPTION
 
876
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
877
    strategies:
 
878
 
 
879
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
880
                         of row combinations)
 
881
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
882
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
883
                  together and picking the 1st one)
 
884
    
 
885
    The join order has "duplicate-generating ranges", and every range is
 
886
    served by one strategy or a combination of FirstMatch with with some
 
887
    other strategy.
 
888
    
 
889
    "Duplicate-generating range" is defined as a range within the join order
 
890
    that contains all of the inner tables of a semi-join. All ranges must be
 
891
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
892
    are joined together, which is equivalent to converting
 
893
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
894
    to
 
895
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
896
    .
 
897
 
 
898
    Applicability conditions are as follows:
 
899
 
 
900
    DuplicateWeedout strategy
 
901
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
902
 
 
903
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
904
      +------+  +=========================+  +---+
 
905
        (1)                 (2)               (3)
 
906
 
 
907
       (1) - Prefix of OuterTables (those that participate in 
 
908
             IN-equality and/or are correlated with subquery) and outer 
 
909
             Noncorrelated Tables.
 
910
       (2) - The handled range. The range starts with the first sj-inner
 
911
             table, and covers all sj-inner and outer tables 
 
912
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
913
             may follow in any order.
 
914
       (3) - The suffix of outer Noncorrelated tables.
 
915
    
 
916
    FirstMatch strategy
 
917
    ~~~~~~~~~~~~~~~~~~~
 
918
 
 
919
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
920
      +------+  +==================+  +---+
 
921
        (1)             (2)          (3)
 
922
 
 
923
      (1) - Prefix of outer and non-correlated tables
 
924
      (2) - The handled range, which may contain only inner and
 
925
            non-correlated tables.
 
926
      (3) - The suffix of outer Noncorrelated tables.
 
927
 
 
928
    InsideOut strategy 
 
929
    ~~~~~~~~~~~~~~~~~~
 
930
 
 
931
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
932
     +--------+   +===========+ +=============+   +------+
 
933
        (1)           (2)          (3)              (4)
 
934
     
 
935
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
936
            all the non-trivially correlated outer tables. (non-trivially means
 
937
            that the correlation is not just through the IN-equality).
 
938
      
 
939
      (2) - Inner table for which the InsideOut scan is performed.
 
940
 
 
941
      (3) - The remainder of the duplicate-generating range. It is served by 
 
942
            application of FirstMatch strategy, with the exception that
 
943
            outer IN-correlated tables are considered to be non-correlated.
 
944
 
 
945
      (4) - THe suffix of outer and outer non-correlated tables.
 
946
 
 
947
    If several strategies are applicable, their relative priorities are:
 
948
      1. InsideOut
 
949
      2. FirstMatch 
 
950
      3. DuplicateWeedout
 
951
 
 
952
    This function walks over the join order and sets up the strategies by
 
953
    setting appropriate members in join_tab structures.
 
954
 
 
955
  RETURN
 
956
    false  OK 
 
957
    true   Out of memory error
 
958
*/
 
959
 
 
960
static
 
961
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
 
962
{
 
963
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
964
  struct {
 
965
    /* 
 
966
      0 - invalid (EOF marker), 
 
967
      1 - InsideOut, 
 
968
      2 - Temptable (maybe confluent),
 
969
      3 - Temptable with join buffering
 
970
    */
 
971
    uint strategy;
 
972
    uint start_idx; /* Left range bound */
 
973
    uint end_idx;   /* Right range bound */
 
974
    /* 
 
975
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
976
      all involved join nests.
 
977
    */
 
978
    table_map outer_tables;
 
979
  } dups_ranges [MAX_TABLES];
 
980
 
 
981
  TABLE_LIST *emb_insideout_nest= NULL;
 
982
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
983
                               tables) whose ranges we're in */
 
984
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
985
  table_map range_start_map= 0; /* table_map at current range start */
 
986
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
987
  int cur_range= 0;
 
988
  uint i;
 
989
 
 
990
  /*
 
991
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
992
  */
 
993
  for (i=join->const_tables ; i < join->tables ; i++)
 
994
  {
 
995
    JOIN_TAB *tab=join->join_tab+i;
 
996
    TABLE *table=tab->table;
 
997
    cur_map |= table->map;
 
998
 
 
999
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
1000
    {
 
1001
      if (!emb_sj_map)
 
1002
      {
 
1003
        dups_ranges[cur_range].start_idx= i;
 
1004
        range_start_map= cur_map & ~table->map;
 
1005
        /*
 
1006
          Remember if this is a possible start of range that is covered by
 
1007
          the InsideOut strategy (the reason that it is not covered could
 
1008
          be that it overlaps with anther semi-join's range. we don't
 
1009
          support InsideOut for joined ranges)
 
1010
        */
 
1011
        if (join->best_positions[i].use_insideout_scan)
 
1012
          emb_insideout_nest= tab->emb_sj_nest;
 
1013
      }
 
1014
 
 
1015
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1016
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1017
 
 
1018
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1019
      {
 
1020
        /*
 
1021
          Two different semi-joins interleave. This cannot be handled by
 
1022
          InsideOut strategy.
 
1023
        */
 
1024
        emb_insideout_nest= NULL;
 
1025
      }
 
1026
    }
 
1027
 
 
1028
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1029
    {
 
1030
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1031
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1032
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1033
      {
 
1034
        /*
 
1035
          This table uses join buffering, which makes use of FirstMatch or 
 
1036
          InsideOut strategies impossible for the current and (we assume) 
 
1037
          preceding duplicate-producing ranges.
 
1038
          That is, for the join order:
 
1039
 
 
1040
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1041
                  |     |     |     |          | \
 
1042
                  +-----+     +-----+          |  join buffering use
 
1043
                     r1          r2         we're here
 
1044
 
 
1045
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1046
          strategy that spans all the tables, starting from the very 1st
 
1047
          one.
 
1048
        */
 
1049
        dealing_with_jbuf= true;
 
1050
        emb_insideout_nest= false;
 
1051
 
 
1052
        /* 
 
1053
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1054
          do not matter: 
 
1055
        */
 
1056
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1057
        {
 
1058
          dups_ranges[cur_range].outer_tables |= 
 
1059
            dups_ranges[prev_range].outer_tables;
 
1060
        }
 
1061
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1062
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1063
        cur_range=  0;
 
1064
      }
 
1065
 
 
1066
      /*
 
1067
        Check if we are at the end of duplicate-producing range. We are if
 
1068
 
 
1069
        1. It's an InsideOut range (which presumes all correlated tables are
 
1070
           in the prefix), and all inner tables are in the join order prefix,
 
1071
           or
 
1072
        2. It's a DuplicateElimination range (possibly covering several
 
1073
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1074
           sj-nests are in the join order prefix.
 
1075
      */
 
1076
      bool end_of_range= false;
 
1077
      if (emb_insideout_nest && 
 
1078
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1079
      {
 
1080
        /* Save that this range is handled with InsideOut: */
 
1081
        dups_ranges[cur_range].strategy= 1;
 
1082
        end_of_range= true;
 
1083
      }
 
1084
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1085
      {
 
1086
        /*
 
1087
          This is a complete range to be handled with either DuplicateWeedout 
 
1088
          or FirstMatch
 
1089
        */
 
1090
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1091
        /* 
 
1092
          This will hold tables from within the range that need to be put 
 
1093
          into the join buffer before we can use the FirstMatch on its tail.
 
1094
        */
 
1095
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1096
                                             ~range_start_map;
 
1097
        end_of_range= true;
 
1098
      }
 
1099
 
 
1100
      if (end_of_range)
 
1101
      {
 
1102
        dups_ranges[cur_range].end_idx= i+1;
 
1103
        emb_sj_map= emb_outer_tables= 0;
 
1104
        emb_insideout_nest= NULL;
 
1105
        dealing_with_jbuf= false;
 
1106
        dups_ranges[++cur_range].strategy= 0;
 
1107
      }
 
1108
    }
 
1109
  }
 
1110
 
 
1111
  THD *thd= join->thd;
 
1112
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1113
  /*
 
1114
    Second pass: setup the chosen strategies    
 
1115
  */
 
1116
  for (int j= 0; j < cur_range; j++)
 
1117
  {
 
1118
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1119
    JOIN_TAB *jump_to;
 
1120
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1121
    {
 
1122
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1123
      jump_to= tab++;
 
1124
    }
 
1125
    else // DuplicateWeedout strategy
 
1126
    {
 
1127
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1128
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1129
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1130
      uint jt_null_bits= 0;    // # null bits in tuple bytes
 
1131
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1132
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1133
      JOIN_TAB *last_outer_tab= tab - 1;
 
1134
      /*
 
1135
        Walk through the range and remember
 
1136
         - tables that need their rowids to be put into temptable
 
1137
         - the last outer table
 
1138
      */
 
1139
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1140
      {
 
1141
        if (sj_table_is_included(join, tab))
 
1142
        {
 
1143
          last_tab->join_tab= tab;
 
1144
          last_tab->rowid_offset= jt_rowid_offset;
 
1145
          jt_rowid_offset += tab->table->file->ref_length;
 
1146
          if (tab->table->maybe_null)
 
1147
          {
 
1148
            last_tab->null_byte= jt_null_bits / 8;
 
1149
            last_tab->null_bit= jt_null_bits++;
 
1150
          }
 
1151
          last_tab++;
 
1152
          tab->table->prepare_for_position();
 
1153
          tab->rowid_keep_flags= rowid_keep_flags;
 
1154
        }
 
1155
        cur_map |= tab->table->map;
 
1156
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1157
                                               dups_ranges[j].outer_tables))
 
1158
          last_outer_tab= tab;
 
1159
      }
 
1160
 
 
1161
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1162
      {
 
1163
        SJ_TMP_TABLE *sjtbl;
 
1164
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1165
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1166
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1167
          return(true);
 
1168
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1169
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1170
        sjtbl->rowid_len= jt_rowid_offset;
 
1171
        sjtbl->null_bits= jt_null_bits;
 
1172
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1173
 
 
1174
        *next_sjtbl_ptr= sjtbl;
 
1175
        next_sjtbl_ptr= &(sjtbl->next);
 
1176
        sjtbl->next= NULL;
 
1177
 
 
1178
        sjtbl->tmp_table= 
 
1179
          create_duplicate_weedout_tmp_table(thd, 
 
1180
                                             sjtbl->rowid_len + 
 
1181
                                             sjtbl->null_bytes,
 
1182
                                             sjtbl);
 
1183
 
 
1184
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1185
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1186
      }
 
1187
      tab= last_outer_tab + 1;
 
1188
      jump_to= last_outer_tab;
 
1189
    }
 
1190
 
 
1191
    /* Create the FirstMatch tail */
 
1192
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1193
    {
 
1194
      if (tab->emb_sj_nest)
 
1195
        tab->do_firstmatch= jump_to; 
 
1196
      else
 
1197
        jump_to= tab;
 
1198
    }
 
1199
  }
 
1200
  return(false);
 
1201
}
 
1202
 
 
1203
 
 
1204
static void cleanup_sj_tmp_tables(JOIN *join)
 
1205
{
 
1206
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1207
       sj_tbl= sj_tbl->next)
 
1208
  {
 
1209
    if (sj_tbl->tmp_table)
 
1210
    {
 
1211
      free_tmp_table(join->thd, sj_tbl->tmp_table);
 
1212
    }
 
1213
  }
 
1214
  join->sj_tmp_tables= NULL;
 
1215
}
 
1216
 
 
1217
uint make_join_orderinfo(JOIN *join);
 
1218
 
 
1219
/**
 
1220
  global select optimisation.
 
1221
 
 
1222
  @note
 
1223
    error code saved in field 'error'
 
1224
 
 
1225
  @retval
 
1226
    0   success
 
1227
  @retval
 
1228
    1   error
 
1229
*/
 
1230
 
 
1231
int
 
1232
JOIN::optimize()
 
1233
{
 
1234
  // to prevent double initialization on EXPLAIN
 
1235
  if (optimized)
 
1236
    return(0);
 
1237
  optimized= 1;
 
1238
 
 
1239
  thd_proc_info(thd, "optimizing");
 
1240
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1241
              unit->select_limit_cnt);
 
1242
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1243
  select_limit= unit->select_limit_cnt;
 
1244
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1245
    select_limit= HA_POS_ERROR;
 
1246
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1247
  // Ignore errors of execution if option IGNORE present
 
1248
  if (thd->lex->ignore)
 
1249
    thd->lex->current_select->no_error= 1;
 
1250
 
 
1251
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1252
  /* Add HAVING to WHERE if possible */
 
1253
  if (having && !group_list && !sum_func_count)
 
1254
  {
 
1255
    if (!conds)
 
1256
    {
 
1257
      conds= having;
 
1258
      having= 0;
 
1259
    }
 
1260
    else if ((conds=new Item_cond_and(conds,having)))
 
1261
    {
 
1262
      /*
 
1263
        Item_cond_and can't be fixed after creation, so we do not check
 
1264
        conds->fixed
 
1265
      */
 
1266
      conds->fix_fields(thd, &conds);
 
1267
      conds->change_ref_to_fields(thd, tables_list);
 
1268
      conds->top_level_item();
 
1269
      having= 0;
 
1270
    }
 
1271
  }
 
1272
#endif
 
1273
  SELECT_LEX *sel= thd->lex->current_select;
 
1274
  if (sel->first_cond_optimization)
 
1275
  {
 
1276
    /*
 
1277
      The following code will allocate the new items in a permanent
 
1278
      MEMROOT for prepared statements and stored procedures.
 
1279
    */
 
1280
    sel->first_cond_optimization= 0;
 
1281
 
 
1282
    /* Convert all outer joins to inner joins if possible */
 
1283
    conds= simplify_joins(this, join_list, conds, true, false);
 
1284
    build_bitmap_for_nested_joins(join_list, 0);
 
1285
 
 
1286
    sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
 
1287
  }
 
1288
 
 
1289
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1290
  if (thd->is_error())
 
1291
  {
 
1292
    error= 1;
 
1293
    return(1);
 
1294
  }
 
1295
 
 
1296
  {
 
1297
    having= optimize_cond(this, having, join_list, &having_value);
 
1298
    if (thd->is_error())
 
1299
    {
 
1300
      error= 1;
 
1301
      return(1);
 
1302
    }
 
1303
    if (select_lex->where)
 
1304
      select_lex->cond_value= cond_value;
 
1305
    if (select_lex->having)
 
1306
      select_lex->having_value= having_value;
 
1307
 
 
1308
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1309
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1310
    {                                           /* Impossible cond */
 
1311
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1312
                           "Impossible HAVING" : "Impossible WHERE";
 
1313
      error= 0;
 
1314
      return(0);
 
1315
    }
 
1316
  }
 
1317
 
 
1318
  /* Optimize count(*), min() and max() */
 
1319
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1320
  {
 
1321
    int res;
 
1322
    /*
 
1323
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1324
      to the WHERE conditions,
 
1325
      or 1 if all items were resolved,
 
1326
      or 0, or an error number HA_ERR_...
 
1327
    */
 
1328
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1329
    {
 
1330
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1331
      {
 
1332
        zero_result_cause= "No matching min/max row";
 
1333
        error=0;
 
1334
        return(0);
 
1335
      }
 
1336
      if (res > 1)
 
1337
      {
 
1338
        error= res;
 
1339
        return(1);
 
1340
      }
 
1341
      if (res < 0)
 
1342
      {
 
1343
        zero_result_cause= "No matching min/max row";
 
1344
        error=0;
 
1345
        return(0);
 
1346
      }
 
1347
      zero_result_cause= "Select tables optimized away";
 
1348
      tables_list= 0;                           // All tables resolved
 
1349
      /*
 
1350
        Extract all table-independent conditions and replace the WHERE
 
1351
        clause with them. All other conditions were computed by opt_sum_query
 
1352
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1353
        so there is no need to compute the whole WHERE clause again.
 
1354
        Notice that make_cond_for_table() will always succeed to remove all
 
1355
        computed conditions, because opt_sum_query() is applicable only to
 
1356
        conjunctions.
 
1357
        Preserve conditions for EXPLAIN.
 
1358
      */
 
1359
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1360
      {
 
1361
        COND *table_independent_conds=
 
1362
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1363
        conds= table_independent_conds;
 
1364
      }
 
1365
    }
 
1366
  }
 
1367
  if (!tables_list)
 
1368
  {
 
1369
    error= 0;
 
1370
    return(0);
 
1371
  }
 
1372
  error= -1;                                    // Error is sent to client
 
1373
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1374
 
 
1375
  /* Calculate how to do the join */
 
1376
  thd_proc_info(thd, "statistics");
 
1377
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1378
      thd->is_fatal_error)
 
1379
  {
 
1380
    return(1);
 
1381
  }
 
1382
 
 
1383
  /* Remove distinct if only const tables */
 
1384
  select_distinct= select_distinct && (const_tables != tables);
 
1385
  thd_proc_info(thd, "preparing");
 
1386
  if (result->initialize_tables(this))
 
1387
  {
 
1388
    return(1);                          // error == -1
 
1389
  }
 
1390
  if (const_table_map != found_const_table_map &&
 
1391
      !(select_options & SELECT_DESCRIBE) &&
 
1392
      (!conds ||
 
1393
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1394
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1395
  {
 
1396
    zero_result_cause= "no matching row in const table";
 
1397
    error= 0;
 
1398
    return(0);
 
1399
  }
 
1400
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1401
      best_read > (double) thd->variables.max_join_size &&
 
1402
      !(select_options & SELECT_DESCRIBE))
 
1403
  {                                             /* purecov: inspected */
 
1404
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1405
    error= -1;
 
1406
    return(1);
 
1407
  }
 
1408
  if (const_tables && !thd->locked_tables &&
 
1409
      !(select_options & SELECT_NO_UNLOCK))
 
1410
    mysql_unlock_some_tables(thd, table, const_tables);
 
1411
  if (!conds && outer_join)
 
1412
  {
 
1413
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1414
    conds=new Item_int((longlong) 1,1); // Always true
 
1415
  }
 
1416
  select= make_select(*table, const_table_map,
 
1417
                      const_table_map, conds, 1, &error);
 
1418
  if (error)
 
1419
  {                                             /* purecov: inspected */
 
1420
    error= -1;                                  /* purecov: inspected */
 
1421
    return(1);
 
1422
  }
 
1423
  
 
1424
  reset_nj_counters(join_list);
 
1425
  make_outerjoin_info(this);
 
1426
 
 
1427
  /*
 
1428
    Among the equal fields belonging to the same multiple equality
 
1429
    choose the one that is to be retrieved first and substitute
 
1430
    all references to these in where condition for a reference for
 
1431
    the selected field.
 
1432
  */
 
1433
  if (conds)
 
1434
  {
 
1435
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1436
    conds->update_used_tables();
 
1437
  }
 
1438
 
 
1439
  /*
 
1440
    Permorm the the optimization on fields evaluation mentioned above
 
1441
    for all on expressions.
 
1442
  */ 
 
1443
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1444
  {
 
1445
    if (*tab->on_expr_ref)
 
1446
    {
 
1447
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1448
                                                         tab->cond_equal,
 
1449
                                                         map2table);
 
1450
      (*tab->on_expr_ref)->update_used_tables();
 
1451
    }
 
1452
  }
 
1453
 
 
1454
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1455
      (select_options & SELECT_DESCRIBE) &&
 
1456
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1457
  {
 
1458
    conds=new Item_int((longlong) 0,1); // Always false
 
1459
  }
 
1460
  if (make_join_select(this, select, conds))
 
1461
  {
 
1462
    zero_result_cause=
 
1463
      "Impossible WHERE noticed after reading const tables";
 
1464
    return(0);                          // error == 0
 
1465
  }
 
1466
 
 
1467
  error= -1;                                    /* if goto err */
 
1468
 
 
1469
  /* Optimize distinct away if possible */
 
1470
  {
 
1471
    ORDER *org_order= order;
 
1472
    order=remove_const(this, order,conds,1, &simple_order);
 
1473
    if (thd->is_error())
 
1474
    {
 
1475
      error= 1;
 
1476
      return(1);
 
1477
    }
 
1478
 
 
1479
    /*
 
1480
      If we are using ORDER BY NULL or ORDER BY const_expression,
 
1481
      return result in any order (even if we are using a GROUP BY)
 
1482
    */
 
1483
    if (!order && org_order)
 
1484
      skip_sort_order= 1;
 
1485
  }
 
1486
  /*
 
1487
     Check if we can optimize away GROUP BY/DISTINCT.
 
1488
     We can do that if there are no aggregate functions, the
 
1489
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1490
     (if present) contain direct references to all key parts of
 
1491
     an unique index (in whatever order) and if the key parts of the
 
1492
     unique index cannot contain NULLs.
 
1493
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1494
     be the same (as long as they are unique).
 
1495
 
 
1496
     The FROM clause must contain a single non-constant table.
 
1497
  */
 
1498
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1499
      !tmp_table_param.sum_func_count &&
 
1500
      (!join_tab[const_tables].select ||
 
1501
       !join_tab[const_tables].select->quick ||
 
1502
       join_tab[const_tables].select->quick->get_type() != 
 
1503
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1504
  {
 
1505
    if (group_list &&
 
1506
       list_contains_unique_index(join_tab[const_tables].table,
 
1507
                                 find_field_in_order_list,
 
1508
                                 (void *) group_list))
 
1509
    {
 
1510
      /*
 
1511
        We have found that grouping can be removed since groups correspond to
 
1512
        only one row anyway, but we still have to guarantee correct result
 
1513
        order. The line below effectively rewrites the query from GROUP BY
 
1514
        <fields> to ORDER BY <fields>. There are two exceptions:
 
1515
        - if skip_sort_order is set (see above), then we can simply skip
 
1516
          GROUP BY;
 
1517
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
 
1518
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1519
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
 
1520
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1521
          ORDER BY fields.
 
1522
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
 
1523
          'order' as is.
 
1524
       */
 
1525
      if (!order || test_if_subpart(group_list, order))
 
1526
          order= skip_sort_order ? 0 : group_list;
 
1527
      /*
 
1528
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1529
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
 
1530
      */
 
1531
      join_tab->table->keys_in_use_for_order_by=
 
1532
        join_tab->table->keys_in_use_for_group_by;
 
1533
      group_list= 0;
 
1534
      group= 0;
 
1535
    }
 
1536
    if (select_distinct &&
 
1537
       list_contains_unique_index(join_tab[const_tables].table,
 
1538
                                 find_field_in_item_list,
 
1539
                                 (void *) &fields_list))
 
1540
    {
 
1541
      select_distinct= 0;
 
1542
    }
 
1543
  }
 
1544
  if (group_list || tmp_table_param.sum_func_count)
 
1545
  {
 
1546
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1547
      select_distinct=0;
 
1548
  }
 
1549
  else if (select_distinct && tables - const_tables == 1)
 
1550
  {
 
1551
    /*
 
1552
      We are only using one table. In this case we change DISTINCT to a
 
1553
      GROUP BY query if:
 
1554
      - The GROUP BY can be done through indexes (no sort) and the ORDER
 
1555
        BY only uses selected fields.
 
1556
        (In this case we can later optimize away GROUP BY and ORDER BY)
 
1557
      - We are scanning the whole table without LIMIT
 
1558
        This can happen if:
 
1559
        - We are using CALC_FOUND_ROWS
 
1560
        - We are using an ORDER BY that can't be optimized away.
 
1561
 
 
1562
      We don't want to use this optimization when we are using LIMIT
 
1563
      because in this case we can just create a temporary table that
 
1564
      holds LIMIT rows and stop when this table is full.
 
1565
    */
 
1566
    JOIN_TAB *tab= &join_tab[const_tables];
 
1567
    bool all_order_fields_used;
 
1568
    if (order)
 
1569
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1570
        &tab->table->keys_in_use_for_order_by);
 
1571
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1572
                                          order, fields_list, all_fields,
 
1573
                                          &all_order_fields_used)))
 
1574
    {
 
1575
      bool skip_group= (skip_sort_order &&
 
1576
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1577
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1578
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1579
      if ((skip_group && all_order_fields_used) ||
 
1580
          select_limit == HA_POS_ERROR ||
 
1581
          (order && !skip_sort_order))
 
1582
      {
 
1583
        /*  Change DISTINCT to GROUP BY */
 
1584
        select_distinct= 0;
 
1585
        no_order= !order;
 
1586
        if (all_order_fields_used)
 
1587
        {
 
1588
          if (order && skip_sort_order)
 
1589
          {
 
1590
            /*
 
1591
              Force MySQL to read the table in sorted order to get result in
 
1592
              ORDER BY order.
 
1593
            */
 
1594
            tmp_table_param.quick_group=0;
 
1595
          }
 
1596
          order=0;
 
1597
        }
 
1598
        group=1;                                // For end_write_group
 
1599
      }
 
1600
      else
 
1601
        group_list= 0;
 
1602
    }
 
1603
    else if (thd->is_fatal_error)                       // End of memory
 
1604
      return(1);
 
1605
  }
 
1606
  simple_group= 0;
 
1607
  {
 
1608
    ORDER *old_group_list;
 
1609
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1610
                             rollup.state == ROLLUP::STATE_NONE,
 
1611
                             &simple_group);
 
1612
    if (thd->is_error())
 
1613
    {
 
1614
      error= 1;
 
1615
      return(1);
 
1616
    }
 
1617
    if (old_group_list && !group_list)
 
1618
      select_distinct= 0;
 
1619
  }
 
1620
  if (!group_list && group)
 
1621
  {
 
1622
    order=0;                                    // The output has only one row
 
1623
    simple_order=1;
 
1624
    select_distinct= 0;                       // No need in distinct for 1 row
 
1625
    group_optimized_away= 1;
 
1626
  }
 
1627
 
 
1628
  calc_group_buffer(this, group_list);
 
1629
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1630
 
 
1631
  if (test_if_subpart(group_list, order) ||
 
1632
      (!group_list && tmp_table_param.sum_func_count))
 
1633
    order=0;
 
1634
 
 
1635
  // Can't use sort on head table if using row cache
 
1636
  if (full_join)
 
1637
  {
 
1638
    if (group_list)
 
1639
      simple_group=0;
 
1640
    if (order)
 
1641
      simple_order=0;
 
1642
  }
 
1643
 
 
1644
  /*
 
1645
    Check if we need to create a temporary table.
 
1646
    This has to be done if all tables are not already read (const tables)
 
1647
    and one of the following conditions holds:
 
1648
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1649
    - We are using an ORDER BY or GROUP BY on fields not in the first table
 
1650
    - We are using different ORDER BY and GROUP BY orders
 
1651
    - The user wants us to buffer the result.
 
1652
  */
 
1653
  need_tmp= (const_tables != tables &&
 
1654
             ((select_distinct || !simple_order || !simple_group) ||
 
1655
              (group_list && order) ||
 
1656
              test(select_options & OPTION_BUFFER_RESULT)));
 
1657
 
 
1658
  uint no_jbuf_after= make_join_orderinfo(this);
 
1659
  ulonglong select_opts_for_readinfo= 
 
1660
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1661
 
 
1662
  sj_tmp_tables= NULL;
 
1663
  if (!select_lex->sj_nests.is_empty())
 
1664
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1665
                                    no_jbuf_after);
 
1666
 
 
1667
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1668
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1669
    return(1);
 
1670
 
 
1671
  /* Create all structures needed for materialized subquery execution. */
 
1672
  if (setup_subquery_materialization())
 
1673
    return(1);
 
1674
 
 
1675
  /*
 
1676
    is this simple IN subquery?
 
1677
  */
 
1678
  if (!group_list && !order &&
 
1679
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1680
      tables == 1 && conds &&
 
1681
      !unit->is_union())
 
1682
  {
 
1683
    if (!having)
 
1684
    {
 
1685
      Item *where= conds;
 
1686
      if (join_tab[0].type == JT_EQ_REF &&
 
1687
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1688
      {
 
1689
        remove_subq_pushed_predicates(&where);
 
1690
        save_index_subquery_explain_info(join_tab, where);
 
1691
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1692
        error= 0;
 
1693
        return(unit->item->
 
1694
                    change_engine(new
 
1695
                                  subselect_uniquesubquery_engine(thd,
 
1696
                                                                  join_tab,
 
1697
                                                                  unit->item,
 
1698
                                                                  where)));
 
1699
      }
 
1700
      else if (join_tab[0].type == JT_REF &&
 
1701
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1702
      {
 
1703
        remove_subq_pushed_predicates(&where);
 
1704
        save_index_subquery_explain_info(join_tab, where);
 
1705
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1706
        error= 0;
 
1707
        return(unit->item->
 
1708
                    change_engine(new
 
1709
                                  subselect_indexsubquery_engine(thd,
 
1710
                                                                 join_tab,
 
1711
                                                                 unit->item,
 
1712
                                                                 where,
 
1713
                                                                 NULL,
 
1714
                                                                 0)));
 
1715
      }
 
1716
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1717
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1718
               having->name == in_having_cond)
 
1719
    {
 
1720
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1721
      error= 0;
 
1722
      conds= remove_additional_cond(conds);
 
1723
      save_index_subquery_explain_info(join_tab, conds);
 
1724
      return(unit->item->
 
1725
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1726
                                                                   join_tab,
 
1727
                                                                   unit->item,
 
1728
                                                                   conds,
 
1729
                                                                   having,
 
1730
                                                                   1)));
 
1731
    }
 
1732
 
 
1733
  }
 
1734
  /*
 
1735
    Need to tell handlers that to play it safe, it should fetch all
 
1736
    columns of the primary key of the tables: this is because MySQL may
 
1737
    build row pointers for the rows, and for all columns of the primary key
 
1738
    the read set has not necessarily been set by the server code.
 
1739
  */
 
1740
  if (need_tmp || select_distinct || group_list || order)
 
1741
  {
 
1742
    for (uint i = const_tables; i < tables; i++)
 
1743
      join_tab[i].table->prepare_for_position();
 
1744
  }
 
1745
 
 
1746
  if (const_tables != tables)
 
1747
  {
 
1748
    /*
 
1749
      Because filesort always does a full table scan or a quick range scan
 
1750
      we must add the removed reference to the select for the table.
 
1751
      We only need to do this when we have a simple_order or simple_group
 
1752
      as in other cases the join is done before the sort.
 
1753
    */
 
1754
    if ((order || group_list) &&
 
1755
        (join_tab[const_tables].type != JT_ALL) &&
 
1756
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1757
        ((order && simple_order) || (group_list && simple_group)))
 
1758
    {
 
1759
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1760
        return(1);
 
1761
      }
 
1762
    }
 
1763
    
 
1764
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1765
        ((group_list &&
 
1766
          (!simple_group ||
 
1767
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1768
                                    unit->select_limit_cnt, 0, 
 
1769
                                    &join_tab[const_tables].table->
 
1770
                                    keys_in_use_for_group_by))) ||
 
1771
         select_distinct) &&
 
1772
        tmp_table_param.quick_group)
 
1773
    {
 
1774
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1775
    }
 
1776
    if (order)
 
1777
    {
 
1778
      /*
 
1779
        Force using of tmp table if sorting by a SP or UDF function due to
 
1780
        their expensive and probably non-deterministic nature.
 
1781
      */
 
1782
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1783
      {
 
1784
        Item *item= *tmp_order->item;
 
1785
        if (item->is_expensive())
 
1786
        {
 
1787
          /* Force tmp table without sort */
 
1788
          need_tmp=1; simple_order=simple_group=0;
 
1789
          break;
 
1790
        }
 
1791
      }
 
1792
    }
 
1793
  }
 
1794
 
 
1795
  tmp_having= having;
 
1796
  if (select_options & SELECT_DESCRIBE)
 
1797
  {
 
1798
    error= 0;
 
1799
    return(0);
 
1800
  }
 
1801
  having= 0;
 
1802
 
 
1803
  /*
 
1804
    The loose index scan access method guarantees that all grouping or
 
1805
    duplicate row elimination (for distinct) is already performed
 
1806
    during data retrieval, and that all MIN/MAX functions are already
 
1807
    computed for each group. Thus all MIN/MAX functions should be
 
1808
    treated as regular functions, and there is no need to perform
 
1809
    grouping in the main execution loop.
 
1810
    Notice that currently loose index scan is applicable only for
 
1811
    single table queries, thus it is sufficient to test only the first
 
1812
    join_tab element of the plan for its access method.
 
1813
  */
 
1814
  if (join_tab->is_using_loose_index_scan())
 
1815
    tmp_table_param.precomputed_group_by= true;
 
1816
 
 
1817
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1818
  if (need_tmp)
 
1819
  {
 
1820
    thd_proc_info(thd, "Creating tmp table");
 
1821
 
 
1822
    init_items_ref_array();
 
1823
 
 
1824
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1825
                                         fields_list.elements);
 
1826
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1827
                                                             (ORDER*) 0);
 
1828
    /*
 
1829
      Pushing LIMIT to the temporary table creation is not applicable
 
1830
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
 
1831
      there are aggregate functions, because in all these cases we need
 
1832
      all result rows.
 
1833
    */
 
1834
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1835
                             !tmp_group &&
 
1836
                             !thd->lex->current_select->with_sum_func) ?
 
1837
                            select_limit : HA_POS_ERROR;
 
1838
 
 
1839
    if (!(exec_tmp_table1=
 
1840
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1841
                           tmp_group,
 
1842
                           group_list ? 0 : select_distinct,
 
1843
                           group_list && simple_group,
 
1844
                           select_options,
 
1845
                           tmp_rows_limit,
 
1846
                           (char *) "")))
 
1847
                {
 
1848
      return(1);
 
1849
    }
 
1850
 
 
1851
    /*
 
1852
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1853
      - we are sorting the table and writing complete group rows to the
 
1854
        temp table.
 
1855
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1856
        on all columns.
 
1857
      
 
1858
      If having is not handled here, it will be checked before the row
 
1859
      is sent to the client.
 
1860
    */    
 
1861
    if (tmp_having && 
 
1862
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1863
      having= tmp_having;
 
1864
 
 
1865
    /* if group or order on first table, sort first */
 
1866
    if (group_list && simple_group)
 
1867
    {
 
1868
      thd_proc_info(thd, "Sorting for group");
 
1869
      if (create_sort_index(thd, this, group_list,
 
1870
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1871
          alloc_group_fields(this, group_list) ||
 
1872
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1873
          setup_sum_funcs(thd, sum_funcs))
 
1874
      {
 
1875
        return(1);
 
1876
      }
 
1877
      group_list=0;
 
1878
    }
 
1879
    else
 
1880
    {
 
1881
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1882
          setup_sum_funcs(thd, sum_funcs))
 
1883
      {
 
1884
        return(1);
 
1885
      }
 
1886
 
 
1887
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1888
      {
 
1889
        thd_proc_info(thd, "Sorting for order");
 
1890
        if (create_sort_index(thd, this, order,
 
1891
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1892
        {
 
1893
          return(1);
 
1894
        }
 
1895
        order=0;
 
1896
      }
 
1897
    }
 
1898
    
 
1899
    /*
 
1900
      Optimize distinct when used on some of the tables
 
1901
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1902
      In this case we can stop scanning t2 when we have found one t1.a
 
1903
    */
 
1904
 
 
1905
    if (exec_tmp_table1->distinct)
 
1906
    {
 
1907
      table_map used_tables= thd->used_tables;
 
1908
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1909
      do
 
1910
      {
 
1911
        if (used_tables & last_join_tab->table->map)
 
1912
          break;
 
1913
        last_join_tab->not_used_in_distinct=1;
 
1914
      } while (last_join_tab-- != join_tab);
 
1915
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1916
      if (order && skip_sort_order)
 
1917
      {
 
1918
        /* Should always succeed */
 
1919
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1920
                                    order, unit->select_limit_cnt, 0, 
 
1921
                                    &join_tab[const_tables].table->
 
1922
                                      keys_in_use_for_order_by))
 
1923
          order=0;
 
1924
      }
 
1925
    }
 
1926
 
 
1927
    /* 
 
1928
      If this join belongs to an uncacheable subquery save 
 
1929
      the original join 
 
1930
    */
 
1931
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1932
        init_save_join_tab())
 
1933
      return(-1);                         /* purecov: inspected */
 
1934
  }
 
1935
 
 
1936
  error= 0;
 
1937
  return(0);
 
1938
}
 
1939
 
 
1940
 
 
1941
/**
 
1942
  Restore values in temporary join.
 
1943
*/
 
1944
void JOIN::restore_tmp()
 
1945
{
 
1946
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1947
}
 
1948
 
 
1949
 
 
1950
int
 
1951
JOIN::reinit()
 
1952
{
 
1953
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1954
                                    select_lex->offset_limit->val_uint() :
 
1955
                                    0ULL);
 
1956
 
 
1957
  first_record= 0;
 
1958
 
 
1959
  if (exec_tmp_table1)
 
1960
  {
 
1961
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1962
    exec_tmp_table1->file->ha_delete_all_rows();
 
1963
    free_io_cache(exec_tmp_table1);
 
1964
    filesort_free_buffers(exec_tmp_table1,0);
 
1965
  }
 
1966
  if (exec_tmp_table2)
 
1967
  {
 
1968
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1969
    exec_tmp_table2->file->ha_delete_all_rows();
 
1970
    free_io_cache(exec_tmp_table2);
 
1971
    filesort_free_buffers(exec_tmp_table2,0);
 
1972
  }
 
1973
  if (items0)
 
1974
    set_items_ref_array(items0);
 
1975
 
 
1976
  if (join_tab_save)
 
1977
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1978
 
 
1979
  if (tmp_join)
 
1980
    restore_tmp();
 
1981
 
 
1982
  /* Reset of sum functions */
 
1983
  if (sum_funcs)
 
1984
  {
 
1985
    Item_sum *func, **func_ptr= sum_funcs;
 
1986
    while ((func= *(func_ptr++)))
 
1987
      func->clear();
 
1988
  }
 
1989
 
 
1990
  return(0);
 
1991
}
 
1992
 
 
1993
/**
 
1994
   @brief Save the original join layout
 
1995
      
 
1996
   @details Saves the original join layout so it can be reused in 
 
1997
   re-execution and for EXPLAIN.
 
1998
             
 
1999
   @return Operation status
 
2000
   @retval 0      success.
 
2001
   @retval 1      error occurred.
 
2002
*/
 
2003
 
 
2004
bool
 
2005
JOIN::init_save_join_tab()
 
2006
{
 
2007
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
2008
    return 1;                                  /* purecov: inspected */
 
2009
  error= 0;                                    // Ensure that tmp_join.error= 0
 
2010
  restore_tmp();
 
2011
  return 0;
 
2012
}
 
2013
 
 
2014
 
 
2015
bool
 
2016
JOIN::save_join_tab()
 
2017
{
 
2018
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
2019
  {
 
2020
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
 
2021
                                                sizeof(JOIN_TAB) * tables)))
 
2022
      return 1;
 
2023
  }
 
2024
  return 0;
 
2025
}
 
2026
 
 
2027
 
 
2028
/**
 
2029
  Exec select.
 
2030
 
 
2031
  @todo
 
2032
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2033
    finally replace sorting with index scan if there is a LIMIT clause in
 
2034
    the query.  It's never shown in EXPLAIN!
 
2035
 
 
2036
  @todo
 
2037
    When can we have here thd->net.report_error not zero?
 
2038
*/
 
2039
void
 
2040
JOIN::exec()
 
2041
{
 
2042
  List<Item> *columns_list= &fields_list;
 
2043
  int      tmp_error;
 
2044
 
 
2045
  thd_proc_info(thd, "executing");
 
2046
  error= 0;
 
2047
  (void) result->prepare2(); // Currently, this cannot fail.
 
2048
 
 
2049
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2050
  {                                           // Only test of functions
 
2051
    if (select_options & SELECT_DESCRIBE)
 
2052
      select_describe(this, false, false, false,
 
2053
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2054
    else
 
2055
    {
 
2056
      result->send_fields(*columns_list,
 
2057
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2058
      /*
 
2059
        We have to test for 'conds' here as the WHERE may not be constant
 
2060
        even if we don't have any tables for prepared statements or if
 
2061
        conds uses something like 'rand()'.
 
2062
      */
 
2063
      if (cond_value != Item::COND_FALSE &&
 
2064
          (!conds || conds->val_int()) &&
 
2065
          (!having || having->val_int()))
 
2066
      {
 
2067
        if (do_send_rows && result->send_data(fields_list))
 
2068
          error= 1;
 
2069
        else
 
2070
        {
 
2071
          error= (int) result->send_eof();
 
2072
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2073
                         thd->sent_row_count);
 
2074
        }
 
2075
      }
 
2076
      else
 
2077
      {
 
2078
        error=(int) result->send_eof();
 
2079
        send_records= 0;
 
2080
      }
 
2081
    }
 
2082
    /* Single select (without union) always returns 0 or 1 row */
 
2083
    thd->limit_found_rows= send_records;
 
2084
    thd->examined_row_count= 0;
 
2085
    return;
 
2086
  }
 
2087
  /*
 
2088
    Don't reset the found rows count if there're no tables as
 
2089
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2090
    It must be accumulated from all join iterations of all join parts.
 
2091
  */
 
2092
  if (tables)
 
2093
    thd->limit_found_rows= 0;
 
2094
 
 
2095
  if (zero_result_cause)
 
2096
  {
 
2097
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2098
                            *columns_list,
 
2099
                            send_row_on_empty_set(),
 
2100
                            select_options,
 
2101
                            zero_result_cause,
 
2102
                            having);
 
2103
    return;
 
2104
  }
 
2105
 
 
2106
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2107
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2108
    return;
 
2109
 
 
2110
  if (select_options & SELECT_DESCRIBE)
 
2111
  {
 
2112
    /*
 
2113
      Check if we managed to optimize ORDER BY away and don't use temporary
 
2114
      table to resolve ORDER BY: in that case, we only may need to do
 
2115
      filesort for GROUP BY.
 
2116
    */
 
2117
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2118
    {
 
2119
      /*
 
2120
        Reset 'order' to 'group_list' and reinit variables describing
 
2121
        'order'
 
2122
      */
 
2123
      order= group_list;
 
2124
      simple_order= simple_group;
 
2125
      skip_sort_order= 0;
 
2126
    }
 
2127
    if (order && 
 
2128
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2129
        (const_tables == tables ||
 
2130
         ((simple_order || skip_sort_order) &&
 
2131
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2132
                                  select_limit, 0, 
 
2133
                                  &join_tab[const_tables].table->
 
2134
                                    keys_in_use_for_query))))
 
2135
      order=0;
 
2136
    having= tmp_having;
 
2137
    select_describe(this, need_tmp,
 
2138
                    order != 0 && !skip_sort_order,
 
2139
                    select_distinct,
 
2140
                    !tables ? "No tables used" : NullS);
 
2141
    return;
 
2142
  }
 
2143
 
 
2144
  JOIN *curr_join= this;
 
2145
  List<Item> *curr_all_fields= &all_fields;
 
2146
  List<Item> *curr_fields_list= &fields_list;
 
2147
  TABLE *curr_tmp_table= 0;
 
2148
  /*
 
2149
    Initialize examined rows here because the values from all join parts
 
2150
    must be accumulated in examined_row_count. Hence every join
 
2151
    iteration must count from zero.
 
2152
  */
 
2153
  curr_join->examined_rows= 0;
 
2154
 
 
2155
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2156
  if (need_tmp)
 
2157
  {
 
2158
    if (tmp_join)
 
2159
    {
 
2160
      /*
 
2161
        We are in a non cacheable sub query. Get the saved join structure
 
2162
        after optimization.
 
2163
        (curr_join may have been modified during last exection and we need
 
2164
        to reset it)
 
2165
      */
 
2166
      curr_join= tmp_join;
 
2167
    }
 
2168
    curr_tmp_table= exec_tmp_table1;
 
2169
 
 
2170
    /* Copy data to the temporary table */
 
2171
    thd_proc_info(thd, "Copying to tmp table");
 
2172
    if (!curr_join->sort_and_group &&
 
2173
        curr_join->const_tables != curr_join->tables)
 
2174
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2175
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2176
    {
 
2177
      error= tmp_error;
 
2178
      return;
 
2179
    }
 
2180
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2181
    
 
2182
    if (curr_join->having)
 
2183
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2184
    
 
2185
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2186
    curr_join->all_fields= *curr_all_fields;
 
2187
    if (!items1)
 
2188
    {
 
2189
      items1= items0 + all_fields.elements;
 
2190
      if (sort_and_group || curr_tmp_table->group)
 
2191
      {
 
2192
        if (change_to_use_tmp_fields(thd, items1,
 
2193
                                     tmp_fields_list1, tmp_all_fields1,
 
2194
                                     fields_list.elements, all_fields))
 
2195
          return;
 
2196
      }
 
2197
      else
 
2198
      {
 
2199
        if (change_refs_to_tmp_fields(thd, items1,
 
2200
                                      tmp_fields_list1, tmp_all_fields1,
 
2201
                                      fields_list.elements, all_fields))
 
2202
          return;
 
2203
      }
 
2204
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2205
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2206
      curr_join->items1= items1;
 
2207
    }
 
2208
    curr_all_fields= &tmp_all_fields1;
 
2209
    curr_fields_list= &tmp_fields_list1;
 
2210
    curr_join->set_items_ref_array(items1);
 
2211
    
 
2212
    if (sort_and_group || curr_tmp_table->group)
 
2213
    {
 
2214
      curr_join->tmp_table_param.field_count+= 
 
2215
        curr_join->tmp_table_param.sum_func_count+
 
2216
        curr_join->tmp_table_param.func_count;
 
2217
      curr_join->tmp_table_param.sum_func_count= 
 
2218
        curr_join->tmp_table_param.func_count= 0;
 
2219
    }
 
2220
    else
 
2221
    {
 
2222
      curr_join->tmp_table_param.field_count+= 
 
2223
        curr_join->tmp_table_param.func_count;
 
2224
      curr_join->tmp_table_param.func_count= 0;
 
2225
    }
 
2226
    
 
2227
    if (curr_tmp_table->group)
 
2228
    {                                           // Already grouped
 
2229
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2230
        curr_join->order= curr_join->group_list;  /* order by group */
 
2231
      curr_join->group_list= 0;
 
2232
    }
 
2233
    
 
2234
    /*
 
2235
      If we have different sort & group then we must sort the data by group
 
2236
      and copy it to another tmp table
 
2237
      This code is also used if we are using distinct something
 
2238
      we haven't been able to store in the temporary table yet
 
2239
      like SEC_TO_TIME(SUM(...)).
 
2240
    */
 
2241
 
 
2242
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
2243
    {                                   /* Must copy to another table */
 
2244
      /* Free first data from old join */
 
2245
      curr_join->join_free();
 
2246
      if (make_simple_join(curr_join, curr_tmp_table))
 
2247
        return;
 
2248
      calc_group_buffer(curr_join, group_list);
 
2249
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2250
                        curr_join->tmp_all_fields1,
 
2251
                        curr_join->select_distinct && !curr_join->group_list);
 
2252
      curr_join->tmp_table_param.hidden_field_count= 
 
2253
        (curr_join->tmp_all_fields1.elements-
 
2254
         curr_join->tmp_fields_list1.elements);
 
2255
      
 
2256
      
 
2257
      if (exec_tmp_table2)
 
2258
        curr_tmp_table= exec_tmp_table2;
 
2259
      else
 
2260
      {
 
2261
        /* group data to new table */
 
2262
 
 
2263
        /*
 
2264
          If the access method is loose index scan then all MIN/MAX
 
2265
          functions are precomputed, and should be treated as regular
 
2266
          functions. See extended comment in JOIN::exec.
 
2267
        */
 
2268
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2269
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2270
 
 
2271
        if (!(curr_tmp_table=
 
2272
              exec_tmp_table2= create_tmp_table(thd,
 
2273
                                                &curr_join->tmp_table_param,
 
2274
                                                *curr_all_fields,
 
2275
                                                (ORDER*) 0,
 
2276
                                                curr_join->select_distinct && 
 
2277
                                                !curr_join->group_list,
 
2278
                                                1, curr_join->select_options,
 
2279
                                                HA_POS_ERROR,
 
2280
                                                (char *) "")))
 
2281
          return;
 
2282
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2283
      }
 
2284
      if (curr_join->group_list)
 
2285
      {
 
2286
        thd_proc_info(thd, "Creating sort index");
 
2287
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2288
        {
 
2289
          return;
 
2290
        }
 
2291
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2292
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2293
            make_group_fields(this, curr_join))
 
2294
        {
 
2295
          return;
 
2296
        }
 
2297
        sortorder= curr_join->sortorder;
 
2298
      }
 
2299
      
 
2300
      thd_proc_info(thd, "Copying to group table");
 
2301
      tmp_error= -1;
 
2302
      if (curr_join != this)
 
2303
      {
 
2304
        if (sum_funcs2)
 
2305
        {
 
2306
          curr_join->sum_funcs= sum_funcs2;
 
2307
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2308
        }
 
2309
        else
 
2310
        {
 
2311
          curr_join->alloc_func_list();
 
2312
          sum_funcs2= curr_join->sum_funcs;
 
2313
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2314
        }
 
2315
      }
 
2316
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2317
                                        1, true))
 
2318
        return;
 
2319
      curr_join->group_list= 0;
 
2320
      if (!curr_join->sort_and_group &&
 
2321
          curr_join->const_tables != curr_join->tables)
 
2322
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2323
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2324
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2325
      {
 
2326
        error= tmp_error;
 
2327
        return;
 
2328
      }
 
2329
      end_read_record(&curr_join->join_tab->read_record);
 
2330
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2331
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2332
      
 
2333
      // No sum funcs anymore
 
2334
      if (!items2)
 
2335
      {
 
2336
        items2= items1 + all_fields.elements;
 
2337
        if (change_to_use_tmp_fields(thd, items2,
 
2338
                                     tmp_fields_list2, tmp_all_fields2, 
 
2339
                                     fields_list.elements, tmp_all_fields1))
 
2340
          return;
 
2341
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2342
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2343
      }
 
2344
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2345
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2346
      curr_join->set_items_ref_array(items2);
 
2347
      curr_join->tmp_table_param.field_count+= 
 
2348
        curr_join->tmp_table_param.sum_func_count;
 
2349
      curr_join->tmp_table_param.sum_func_count= 0;
 
2350
    }
 
2351
    if (curr_tmp_table->distinct)
 
2352
      curr_join->select_distinct=0;             /* Each row is unique */
 
2353
    
 
2354
    curr_join->join_free();                     /* Free quick selects */
 
2355
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2356
    {
 
2357
      thd_proc_info(thd, "Removing duplicates");
 
2358
      if (curr_join->tmp_having)
 
2359
        curr_join->tmp_having->update_used_tables();
 
2360
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2361
                            *curr_fields_list, curr_join->tmp_having))
 
2362
        return;
 
2363
      curr_join->tmp_having=0;
 
2364
      curr_join->select_distinct=0;
 
2365
    }
 
2366
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2367
    if (make_simple_join(curr_join, curr_tmp_table))
 
2368
      return;
 
2369
    calc_group_buffer(curr_join, curr_join->group_list);
 
2370
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2371
                      *curr_all_fields, 0);
 
2372
    
 
2373
  }
 
2374
  
 
2375
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2376
  {
 
2377
    if (make_group_fields(this, curr_join))
 
2378
    {
 
2379
      return;
 
2380
    }
 
2381
    if (!items3)
 
2382
    {
 
2383
      if (!items0)
 
2384
        init_items_ref_array();
 
2385
      items3= ref_pointer_array + (all_fields.elements*4);
 
2386
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2387
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2388
                        curr_fields_list->elements, *curr_all_fields);
 
2389
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2390
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2391
      tmp_table_param.save_copy_field_end=
 
2392
        curr_join->tmp_table_param.copy_field_end;
 
2393
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2394
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2395
    }
 
2396
    else
 
2397
    {
 
2398
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2399
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2400
      curr_join->tmp_table_param.copy_field_end=
 
2401
        tmp_table_param.save_copy_field_end;
 
2402
    }
 
2403
    curr_fields_list= &tmp_fields_list3;
 
2404
    curr_all_fields= &tmp_all_fields3;
 
2405
    curr_join->set_items_ref_array(items3);
 
2406
 
 
2407
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2408
                                      1, true) || 
 
2409
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2410
        thd->is_fatal_error)
 
2411
      return;
 
2412
  }
 
2413
  if (curr_join->group_list || curr_join->order)
 
2414
  {
 
2415
    thd_proc_info(thd, "Sorting result");
 
2416
    /* If we have already done the group, add HAVING to sorted table */
 
2417
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2418
        ! curr_join->sort_and_group)
 
2419
    {
 
2420
      // Some tables may have been const
 
2421
      curr_join->tmp_having->update_used_tables();
 
2422
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2423
      table_map used_tables= (curr_join->const_table_map |
 
2424
                              curr_table->table->map);
 
2425
 
 
2426
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2427
                                                 used_tables,
 
2428
                                                 used_tables, 0);
 
2429
      if (sort_table_cond)
 
2430
      {
 
2431
        if (!curr_table->select)
 
2432
          if (!(curr_table->select= new SQL_SELECT))
 
2433
            return;
 
2434
        if (!curr_table->select->cond)
 
2435
          curr_table->select->cond= sort_table_cond;
 
2436
        else                                    // This should never happen
 
2437
        {
 
2438
          if (!(curr_table->select->cond=
 
2439
                new Item_cond_and(curr_table->select->cond,
 
2440
                                  sort_table_cond)))
 
2441
            return;
 
2442
          /*
 
2443
            Item_cond_and do not need fix_fields for execution, its parameters
 
2444
            are fixed or do not need fix_fields, too
 
2445
          */
 
2446
          curr_table->select->cond->quick_fix_field();
 
2447
        }
 
2448
        curr_table->select_cond= curr_table->select->cond;
 
2449
        curr_table->select_cond->top_level_item();
 
2450
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2451
                                                   ~ (table_map) 0,
 
2452
                                                   ~used_tables, 0);
 
2453
      }
 
2454
    }
 
2455
    {
 
2456
      if (group)
 
2457
        curr_join->select_limit= HA_POS_ERROR;
 
2458
      else
 
2459
      {
 
2460
        /*
 
2461
          We can abort sorting after thd->select_limit rows if we there is no
 
2462
          WHERE clause for any tables after the sorted one.
 
2463
        */
 
2464
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2465
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2466
        for (; curr_table < end_table ; curr_table++)
 
2467
        {
 
2468
          /*
 
2469
            table->keyuse is set in the case there was an original WHERE clause
 
2470
            on the table that was optimized away.
 
2471
          */
 
2472
          if (curr_table->select_cond ||
 
2473
              (curr_table->keyuse && !curr_table->first_inner))
 
2474
          {
 
2475
            /* We have to sort all rows */
 
2476
            curr_join->select_limit= HA_POS_ERROR;
 
2477
            break;
 
2478
          }
 
2479
        }
 
2480
      }
 
2481
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2482
      {
 
2483
        return;
 
2484
      }
 
2485
      /*
 
2486
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
 
2487
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2488
        suitable index present.
 
2489
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2490
        finally replace sorting with index scan if there is a LIMIT clause in
 
2491
        the query. XXX: it's never shown in EXPLAIN!
 
2492
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2493
      */
 
2494
      if (create_sort_index(thd, curr_join,
 
2495
                            curr_join->group_list ? 
 
2496
                            curr_join->group_list : curr_join->order,
 
2497
                            curr_join->select_limit,
 
2498
                            (select_options & OPTION_FOUND_ROWS ?
 
2499
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2500
                            curr_join->group_list ? true : false))
 
2501
        return;
 
2502
      sortorder= curr_join->sortorder;
 
2503
      if (curr_join->const_tables != curr_join->tables &&
 
2504
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2505
      {
 
2506
        /*
 
2507
          If no IO cache exists for the first table then we are using an
 
2508
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2509
          attribute on the INDEX SCAN.
 
2510
        */
 
2511
        skip_sort_order= 1;
 
2512
      }
 
2513
    }
 
2514
  }
 
2515
  /* XXX: When can we have here thd->is_error() not zero? */
 
2516
  if (thd->is_error())
 
2517
  {
 
2518
    error= thd->is_error();
 
2519
    return;
 
2520
  }
 
2521
  curr_join->having= curr_join->tmp_having;
 
2522
  curr_join->fields= curr_fields_list;
 
2523
 
 
2524
  {
 
2525
    thd_proc_info(thd, "Sending data");
 
2526
    result->send_fields(*curr_fields_list,
 
2527
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2528
    error= do_select(curr_join, curr_fields_list, NULL);
 
2529
    thd->limit_found_rows= curr_join->send_records;
 
2530
  }
 
2531
 
 
2532
  /* Accumulate the counts from all join iterations of all join parts. */
 
2533
  thd->examined_row_count+= curr_join->examined_rows;
 
2534
 
 
2535
  /* 
 
2536
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2537
    for a derived table which is always materialized.
 
2538
    Otherwise we would not be able to print the query  correctly.
 
2539
  */ 
 
2540
  if (items0 &&
 
2541
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2542
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2543
    set_items_ref_array(items0);
 
2544
 
 
2545
  return;
 
2546
}
 
2547
 
 
2548
 
 
2549
/**
 
2550
  Clean up join.
 
2551
 
 
2552
  @return
 
2553
    Return error that hold JOIN.
 
2554
*/
 
2555
 
 
2556
int
 
2557
JOIN::destroy()
 
2558
{
 
2559
  select_lex->join= 0;
 
2560
 
 
2561
  if (tmp_join)
 
2562
  {
 
2563
    if (join_tab != tmp_join->join_tab)
 
2564
    {
 
2565
      JOIN_TAB *tab, *end;
 
2566
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2567
        tab->cleanup();
 
2568
    }
 
2569
    tmp_join->tmp_join= 0;
 
2570
    tmp_table_param.copy_field=0;
 
2571
    return(tmp_join->destroy());
 
2572
  }
 
2573
  cond_equal= 0;
 
2574
 
 
2575
  cleanup(1);
 
2576
  if (exec_tmp_table1)
 
2577
    free_tmp_table(thd, exec_tmp_table1);
 
2578
  if (exec_tmp_table2)
 
2579
    free_tmp_table(thd, exec_tmp_table2);
 
2580
  delete select;
 
2581
  delete_dynamic(&keyuse);
 
2582
  return(error);
 
2583
}
 
2584
 
 
2585
 
 
2586
 
311
2587
/**
312
2588
  An entry point to single-unit select (a select without UNION).
313
2589
 
314
 
  @param session                  thread Cursor
 
2590
  @param thd                  thread handler
315
2591
  @param rref_pointer_array   a reference to ref_pointer_array of
316
2592
                              the top-level select_lex for this query
317
2593
  @param tables               list of all tables used in this query.
318
2594
                              The tables have been pre-opened.
319
 
  @param wild_num             number of wildcards used in the top level
 
2595
  @param wild_num             number of wildcards used in the top level 
320
2596
                              select of this query.
321
2597
                              For example statement
322
2598
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
332
2608
  @param order                linked list of ORDER BY agruments
333
2609
  @param group                linked list of GROUP BY arguments
334
2610
  @param having               top level item of HAVING expression
 
2611
  @param proc_param           list of PROCEDUREs
335
2612
  @param select_options       select options (BIG_RESULT, etc)
336
2613
  @param result               an instance of result set handling class.
337
2614
                              This object is responsible for send result
338
2615
                              set rows to the client or inserting them
339
2616
                              into a table.
340
 
  @param select_lex           the only Select_Lex of this query
 
2617
  @param select_lex           the only SELECT_LEX of this query
341
2618
  @param unit                 top-level UNIT of this query
342
2619
                              UNIT is an artificial object created by the
343
2620
                              parser for every SELECT clause.
350
2627
  @retval
351
2628
    true   an error
352
2629
*/
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)
 
2630
 
 
2631
bool
 
2632
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2633
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
 
2634
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
 
2635
             Item *having, ORDER *proc_param, ulonglong select_options,
 
2636
             select_result *result, SELECT_LEX_UNIT *unit,
 
2637
             SELECT_LEX *select_lex)
367
2638
{
368
2639
  bool err;
369
2640
  bool free_join= 1;
370
2641
 
371
2642
  select_lex->context.resolve_in_select_list= true;
372
 
  Join *join;
 
2643
  JOIN *join;
373
2644
  if (select_lex->join != 0)
374
2645
  {
375
2646
    join= select_lex->join;
378
2649
      creation
379
2650
    */
380
2651
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
381
 
        (select_options & SELECT_DESCRIBE))
 
2652
        (select_options & SELECT_DESCRIBE))
382
2653
    {
383
2654
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
384
2655
      {
385
 
        //here is EXPLAIN of subselect or derived table
386
 
        if (join->change_result(result))
387
 
        {
388
 
          return(true);
389
 
        }
 
2656
        //here is EXPLAIN of subselect or derived table
 
2657
        if (join->change_result(result))
 
2658
        {
 
2659
          return(true);
 
2660
        }
390
2661
      }
391
2662
      else
392
2663
      {
393
2664
        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
 
        }
 
2665
                               conds, og_num, order, group, having, proc_param,
 
2666
                               select_lex, unit)))
 
2667
        {
 
2668
          goto err;
 
2669
        }
398
2670
      }
399
2671
    }
400
2672
    free_join= 0;
402
2674
  }
403
2675
  else
404
2676
  {
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
 
2677
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2678
        return(true);
 
2679
    thd_proc_info(thd, "init");
 
2680
    thd->used_tables=0;                         // Updated by setup_fields
409
2681
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
410
 
                           conds, og_num, order, group, having,
 
2682
                           conds, og_num, order, group, having, proc_param,
411
2683
                           select_lex, unit)) == true)
412
2684
    {
413
2685
      goto err;
414
2686
    }
415
2687
  }
416
2688
 
417
 
  err= join->optimize();
418
 
  if (err)
419
 
  {
420
 
    goto err; // 1
421
 
  }
422
 
 
423
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2689
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
 
2690
  if (join->flatten_subqueries())
 
2691
  {
 
2692
    err= 1;
 
2693
    goto err;
 
2694
  }
 
2695
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
 
2696
 
 
2697
  if ((err= join->optimize()))
 
2698
  {
 
2699
    goto err;                                   // 1
 
2700
  }
 
2701
 
 
2702
  if (thd->lex->describe & DESCRIBE_EXTENDED)
424
2703
  {
425
2704
    join->conds_history= join->conds;
426
2705
    join->having_history= (join->having?join->having:join->tmp_having);
427
2706
  }
428
2707
 
429
 
  if (session->is_error())
 
2708
  if (thd->is_error())
430
2709
    goto err;
431
2710
 
432
2711
  join->exec();
433
2712
 
434
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2713
  if (thd->lex->describe & DESCRIBE_EXTENDED)
435
2714
  {
436
2715
    select_lex->where= join->conds_history;
437
2716
    select_lex->having= join->having_history;
440
2719
err:
441
2720
  if (free_join)
442
2721
  {
443
 
    session->set_proc_info("end");
 
2722
    thd_proc_info(thd, "end");
444
2723
    err|= select_lex->cleanup();
445
 
    return(err || session->is_error());
 
2724
    return(err || thd->is_error());
446
2725
  }
447
2726
  return(join->error);
448
2727
}
449
2728
 
450
 
inline Item *and_items(Item* cond, Item *item)
 
2729
 
 
2730
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2731
                          Item_in_subselect* const *el2)
 
2732
{
 
2733
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2734
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2735
}
 
2736
 
 
2737
 
 
2738
inline Item * and_items(Item* cond, Item *item)
451
2739
{
452
2740
  return (cond? (new Item_cond_and(cond, item)) : item);
453
2741
}
454
2742
 
 
2743
 
 
2744
static TABLE_LIST *alloc_join_nest(THD *thd)
 
2745
{
 
2746
  TABLE_LIST *tbl;
 
2747
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2748
                                       sizeof(NESTED_JOIN))))
 
2749
    return NULL;
 
2750
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2751
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
 
2752
  return tbl;
 
2753
}
 
2754
 
 
2755
 
 
2756
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
 
2757
{
 
2758
  List_iterator<TABLE_LIST> it(*tlist);
 
2759
  TABLE_LIST *table;
 
2760
  while ((table= it++))
 
2761
  {
 
2762
    if (table->on_expr)
 
2763
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2764
    if (table->nested_join)
 
2765
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2766
  }
 
2767
}
 
2768
 
 
2769
 
 
2770
/*
 
2771
  Convert a subquery predicate into a TABLE_LIST semi-join nest
 
2772
 
 
2773
  SYNOPSIS
 
2774
    convert_subq_to_sj()
 
2775
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2776
                    clause
 
2777
       subq_pred    Subquery predicate to be converted
 
2778
  
 
2779
  DESCRIPTION
 
2780
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
 
2781
    prerequisites are already checked, so the conversion is always successfull.
 
2782
 
 
2783
    Prepared Statements: the transformation is permanent:
 
2784
     - Changes in TABLE_LIST structures are naturally permanent
 
2785
     - Item tree changes are performed on statement MEM_ROOT:
 
2786
        = we activate statement MEM_ROOT 
 
2787
        = this function is called before the first fix_prepare_information
 
2788
          call.
 
2789
 
 
2790
    This is intended because the criteria for subquery-to-sj conversion remain
 
2791
    constant for the lifetime of the Prepared Statement.
 
2792
 
 
2793
  RETURN
 
2794
    false  OK
 
2795
    true   Out of memory error
 
2796
*/
 
2797
 
 
2798
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2799
{
 
2800
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2801
  TABLE_LIST *emb_tbl_nest= NULL;
 
2802
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
 
2803
  THD *thd= parent_join->thd;
 
2804
 
 
2805
  /*
 
2806
    1. Find out where to put the predicate into.
 
2807
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2808
  */
 
2809
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2810
  {
 
2811
    if (subq_pred->expr_join_nest->nested_join)
 
2812
    {
 
2813
      /*
 
2814
        We're dealing with
 
2815
 
 
2816
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2817
 
 
2818
        The sj-nest will be inserted into the brackets nest.
 
2819
      */
 
2820
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2821
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2822
    }
 
2823
    else if (!subq_pred->expr_join_nest->outer_join)
 
2824
    {
 
2825
      /*
 
2826
        We're dealing with
 
2827
 
 
2828
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2829
 
 
2830
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2831
        parent. This is ok because tblX is joined as an inner join.
 
2832
      */
 
2833
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2834
      if (emb_tbl_nest)
 
2835
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2836
    }
 
2837
    else if (!subq_pred->expr_join_nest->nested_join)
 
2838
    {
 
2839
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2840
      TABLE_LIST *wrap_nest;
 
2841
      /*
 
2842
        We're dealing with
 
2843
 
 
2844
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2845
 
 
2846
        we'll need to convert it into:
 
2847
 
 
2848
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2849
                        |                      |
 
2850
                        |<----- wrap_nest ---->|
 
2851
        
 
2852
        Q:  other subqueries may be pointing to this element. What to do?
 
2853
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2854
            But we'll need to fix other pointers.
 
2855
        A2: Another way: have TABLE_LIST::next_ptr so the following
 
2856
            subqueries know the table has been nested.
 
2857
        A3: changes in the TABLE_LIST::outer_join will make everything work
 
2858
            automatically.
 
2859
      */
 
2860
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2861
      {
 
2862
        return(true);
 
2863
      }
 
2864
      wrap_nest->embedding= outer_tbl->embedding;
 
2865
      wrap_nest->join_list= outer_tbl->join_list;
 
2866
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2867
 
 
2868
      wrap_nest->nested_join->join_list.empty();
 
2869
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2870
 
 
2871
      outer_tbl->embedding= wrap_nest;
 
2872
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2873
 
 
2874
      /*
 
2875
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2876
        and on_expr
 
2877
      */
 
2878
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2879
      outer_tbl->outer_join= 0;
 
2880
 
 
2881
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2882
      outer_tbl->on_expr= NULL;
 
2883
 
 
2884
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2885
      TABLE_LIST *tbl;
 
2886
      while ((tbl= li++))
 
2887
      {
 
2888
        if (tbl == outer_tbl)
 
2889
        {
 
2890
          li.replace(wrap_nest);
 
2891
          break;
 
2892
        }
 
2893
      }
 
2894
      /*
 
2895
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2896
        semi-join nest into it
 
2897
      */
 
2898
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2899
      emb_tbl_nest=  wrap_nest;
 
2900
    }
 
2901
  }
 
2902
 
 
2903
  TABLE_LIST *sj_nest;
 
2904
  NESTED_JOIN *nested_join;
 
2905
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2906
  {
 
2907
    return(true);
 
2908
  }
 
2909
  nested_join= sj_nest->nested_join;
 
2910
 
 
2911
  sj_nest->join_list= emb_join_list;
 
2912
  sj_nest->embedding= emb_tbl_nest;
 
2913
  sj_nest->alias= (char*) "(sj-nest)";
 
2914
  /* Nests do not participate in those 'chains', so: */
 
2915
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2916
  emb_join_list->push_back(sj_nest);
 
2917
 
 
2918
  /* 
 
2919
    nested_join->used_tables and nested_join->not_null_tables are
 
2920
    initialized in simplify_joins().
 
2921
  */
 
2922
  
 
2923
  /* 
 
2924
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2925
       sj-nest.
 
2926
  */
 
2927
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2928
  nested_join->join_list.empty();
 
2929
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2930
  TABLE_LIST *tl, *last_leaf;
 
2931
  while ((tl= li++))
 
2932
  {
 
2933
    tl->embedding= sj_nest;
 
2934
    tl->join_list= &nested_join->join_list;
 
2935
    nested_join->join_list.push_back(tl);
 
2936
  }
 
2937
  
 
2938
  /*
 
2939
    Reconnect the next_leaf chain.
 
2940
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2941
          Inserting them at the beginning would be a bit faster.
 
2942
    NOTE: We actually insert them at the front! That's because the order is
 
2943
          reversed in this list.
 
2944
  */
 
2945
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2946
  tl->next_leaf= subq_lex->leaf_tables;
 
2947
  last_leaf= tl;
 
2948
 
 
2949
  /*
 
2950
    Same as above for next_local chain
 
2951
    (a theory: a next_local chain always starts with ::leaf_tables
 
2952
     because view's tables are inserted after the view)
 
2953
  */
 
2954
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2955
  tl->next_local= subq_lex->leaf_tables;
 
2956
 
 
2957
  /* A theory: no need to re-connect the next_global chain */
 
2958
 
 
2959
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2960
 
 
2961
  // The subqueries were replaced for Item_int(1) earlier
 
2962
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2963
  /*TODO: also reset the 'with_subselect' there. */
 
2964
 
 
2965
  /* n. Adjust the parent_join->tables counter */
 
2966
  uint table_no= parent_join->tables;
 
2967
  /* n. Walk through child's tables and adjust table->map */
 
2968
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2969
  {
 
2970
    tl->table->tablenr= table_no;
 
2971
    tl->table->map= ((table_map)1) << table_no;
 
2972
    SELECT_LEX *old_sl= tl->select_lex;
 
2973
    tl->select_lex= parent_join->select_lex; 
 
2974
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2975
      emb->select_lex= parent_join->select_lex;
 
2976
  }
 
2977
  parent_join->tables += subq_lex->join->tables;
 
2978
 
 
2979
  /* 
 
2980
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2981
    Add the subquery-induced equalities too.
 
2982
  */
 
2983
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2984
  thd->lex->current_select=subq_lex;
 
2985
  if (!subq_pred->left_expr->fixed &&
 
2986
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2987
    return(true);
 
2988
  thd->lex->current_select=save_lex;
 
2989
 
 
2990
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2991
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2992
                                        subq_pred->left_expr->used_tables();
 
2993
  sj_nest->sj_on_expr= subq_lex->where;
 
2994
 
 
2995
  /*
 
2996
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2997
    Additionally, for InsideOut strategy
 
2998
     - Record the number of IN-equalities.
 
2999
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
3000
       see which of the expressions are bound and which are not (for those
 
3001
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
3002
 
 
3003
       (TODO: can we just create a list of pointers and hope the expressions
 
3004
       will not substitute themselves on fix_fields()? or we need to wrap
 
3005
       them into Item_direct_view_refs and store pointers to those. The
 
3006
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
3007
       Item_direct_view_refs doesn't substitute itself with anything in 
 
3008
       Item_direct_view_ref::fix_fields.
 
3009
  */
 
3010
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
3011
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
3012
 
 
3013
  if (subq_pred->left_expr->cols() == 1)
 
3014
  {
 
3015
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
3016
 
 
3017
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
3018
                                    subq_lex->ref_pointer_array[0]);
 
3019
    item_eq->name= (char*)subq_sj_cond_name;
 
3020
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3021
  }
 
3022
  else
 
3023
  {
 
3024
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
 
3025
    {
 
3026
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3027
                                                element_index(i));
 
3028
      Item *item_eq= 
 
3029
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3030
                         subq_lex->ref_pointer_array[i]);
 
3031
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3032
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3033
    }
 
3034
  }
 
3035
  /* Fix the created equality and AND */
 
3036
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3037
 
 
3038
  /*
 
3039
    Walk through sj nest's WHERE and ON expressions and call
 
3040
    item->fix_table_changes() for all items.
 
3041
  */
 
3042
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3043
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3044
 
 
3045
 
 
3046
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3047
  subq_lex->master_unit()->exclude_level();
 
3048
 
 
3049
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3050
  if (emb_tbl_nest)
 
3051
  {
 
3052
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3053
                                     sj_nest->sj_on_expr);
 
3054
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3055
  }
 
3056
  else
 
3057
  {
 
3058
    /* Inject into the WHERE */
 
3059
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3060
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3061
    parent_join->select_lex->where= parent_join->conds;
 
3062
  }
 
3063
 
 
3064
  return(false);
 
3065
}
 
3066
 
 
3067
 
 
3068
/*
 
3069
  Convert candidate subquery predicates to semi-joins
 
3070
 
 
3071
  SYNOPSIS
 
3072
    JOIN::flatten_subqueries()
 
3073
 
 
3074
  DESCRIPTION
 
3075
    Convert candidate subquery predicates to semi-joins.
 
3076
 
 
3077
  RETURN 
 
3078
    false  OK
 
3079
    true   Error
 
3080
*/
 
3081
 
 
3082
bool JOIN::flatten_subqueries()
 
3083
{
 
3084
  Item_in_subselect **in_subq;
 
3085
  Item_in_subselect **in_subq_end;
 
3086
 
 
3087
  if (sj_subselects.elements() == 0)
 
3088
    return(false);
 
3089
 
 
3090
  /* 1. Fix children subqueries */
 
3091
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3092
       in_subq != in_subq_end; in_subq++)
 
3093
  {
 
3094
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3095
    child_join->outer_tables = child_join->tables;
 
3096
    if (child_join->flatten_subqueries())
 
3097
      return(true);
 
3098
    (*in_subq)->sj_convert_priority= 
 
3099
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3100
  }
 
3101
 
 
3102
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
 
3103
  /* 
 
3104
    2. Pick which subqueries to convert:
 
3105
      sort the subquery array
 
3106
      - prefer correlated subqueries over uncorrelated;
 
3107
      - prefer subqueries that have greater number of outer tables;
 
3108
  */
 
3109
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3110
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3111
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3112
  for (in_subq= sj_subselects.front(); 
 
3113
       in_subq != in_subq_end && 
 
3114
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3115
       in_subq++)
 
3116
  {
 
3117
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3118
      return(true);
 
3119
  }
 
3120
 
 
3121
  for (in_subq= sj_subselects.front(); 
 
3122
       in_subq != in_subq_end && 
 
3123
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3124
       in_subq++)
 
3125
  {
 
3126
    if (convert_subq_to_sj(this, *in_subq))
 
3127
      return(true);
 
3128
  }
 
3129
 
 
3130
  /* 3. Finalize those we didn't convert */
 
3131
  for (; in_subq!= in_subq_end; in_subq++)
 
3132
  {
 
3133
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3134
    Item_subselect::trans_res res;
 
3135
    (*in_subq)->changed= 0;
 
3136
    (*in_subq)->fixed= 0;
 
3137
    res= (*in_subq)->select_transformer(child_join);
 
3138
    if (res == Item_subselect::RES_ERROR)
 
3139
      return(true);
 
3140
 
 
3141
    (*in_subq)->changed= 1;
 
3142
    (*in_subq)->fixed= 1;
 
3143
 
 
3144
    Item *substitute= (*in_subq)->substitution;
 
3145
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3146
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3147
      return(true);
 
3148
 
 
3149
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3150
    //  return(true);
 
3151
  }
 
3152
  sj_subselects.clear();
 
3153
  return(false);
 
3154
}
 
3155
 
 
3156
 
 
3157
/**
 
3158
  Setup for execution all subqueries of a query, for which the optimizer
 
3159
  chose hash semi-join.
 
3160
 
 
3161
  @details Iterate over all subqueries of the query, and if they are under an
 
3162
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3163
  - try to initialize all data structures needed for the materialized execution
 
3164
    of the IN predicate,
 
3165
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3166
    previously blocked during JOIN::prepare.
 
3167
 
 
3168
  This method is part of the "code generation" query processing phase.
 
3169
 
 
3170
  This phase must be called after substitute_for_best_equal_field() because
 
3171
  that function may replace items with other items from a multiple equality,
 
3172
  and we need to reference the correct items in the index access method of the
 
3173
  IN predicate.
 
3174
 
 
3175
  @return Operation status
 
3176
  @retval false     success.
 
3177
  @retval true      error occurred.
 
3178
*/
 
3179
 
 
3180
bool JOIN::setup_subquery_materialization()
 
3181
{
 
3182
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3183
       un= un->next_unit())
 
3184
  {
 
3185
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3186
    {
 
3187
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3188
      if (subquery_predicate &&
 
3189
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3190
      {
 
3191
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3192
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3193
            in_subs->setup_engine())
 
3194
          return true;
 
3195
      }
 
3196
    }
 
3197
  }
 
3198
  return false;
 
3199
}
 
3200
 
 
3201
 
 
3202
/*
 
3203
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3204
 
 
3205
  SYNOPSIS
 
3206
    find_eq_ref_candidate()
 
3207
      table             Table to be checked
 
3208
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3209
                        count.
 
3210
 
 
3211
  DESCRIPTION
 
3212
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3213
 
 
3214
  TODO
 
3215
    Check again if it is feasible to factor common parts with constant table
 
3216
    search
 
3217
 
 
3218
  RETURN
 
3219
    true  - There exists an eq_ref(outer-tables) candidate
 
3220
    false - Otherwise
 
3221
*/
 
3222
 
 
3223
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
 
3224
{
 
3225
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3226
  uint key;
 
3227
 
 
3228
  if (keyuse)
 
3229
  {
 
3230
    while (1) /* For each key */
 
3231
    {
 
3232
      key= keyuse->key;
 
3233
      KEY *keyinfo= table->key_info + key;
 
3234
      key_part_map bound_parts= 0;
 
3235
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3236
      {
 
3237
        do  /* For all equalities on all key parts */
 
3238
        {
 
3239
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3240
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3241
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3242
          {
 
3243
            bound_parts |= 1 << keyuse->keypart;
 
3244
          }
 
3245
          keyuse++;
 
3246
        } while (keyuse->key == key && keyuse->table == table);
 
3247
 
 
3248
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3249
          return true;
 
3250
        if (keyuse->table != table)
 
3251
          return false;
 
3252
      }
 
3253
      else
 
3254
      {
 
3255
        do
 
3256
        {
 
3257
          keyuse++;
 
3258
          if (keyuse->table != table)
 
3259
            return false;
 
3260
        }
 
3261
        while (keyuse->key == key);
 
3262
      }
 
3263
    }
 
3264
  }
 
3265
  return false;
 
3266
}
 
3267
 
 
3268
 
 
3269
/*
 
3270
  Pull tables out of semi-join nests, if possible
 
3271
 
 
3272
  SYNOPSIS
 
3273
    pull_out_semijoin_tables()
 
3274
      join  The join where to do the semi-join flattening
 
3275
 
 
3276
  DESCRIPTION
 
3277
    Try to pull tables out of semi-join nests.
 
3278
     
 
3279
    PRECONDITIONS
 
3280
    When this function is called, the join may have several semi-join nests
 
3281
    (possibly within different semi-join nests), but it is guaranteed that
 
3282
    one semi-join nest does not contain another.
 
3283
   
 
3284
    ACTION
 
3285
    A table can be pulled out of the semi-join nest if
 
3286
     - It is a constant table
 
3287
     - It is accessed 
 
3288
 
 
3289
    POSTCONDITIONS
 
3290
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3291
       tables)
 
3292
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3293
     * Semi-join nests TABLE_LIST::sj_inner_tables
 
3294
 
 
3295
    This operation is (and should be) performed at each PS execution since
 
3296
    tables may become/cease to be constant across PS reexecutions.
 
3297
 
 
3298
  RETURN 
 
3299
    0 - OK
 
3300
    1 - Out of memory error
 
3301
*/
 
3302
 
 
3303
int pull_out_semijoin_tables(JOIN *join)
 
3304
{
 
3305
  TABLE_LIST *sj_nest;
 
3306
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
 
3307
   
 
3308
  /* Try pulling out of the each of the semi-joins */
 
3309
  while ((sj_nest= sj_list_it++))
 
3310
  {
 
3311
    /* Action #1: Mark the constant tables to be pulled out */
 
3312
    table_map pulled_tables= 0;
 
3313
     
 
3314
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3315
    TABLE_LIST *tbl;
 
3316
    while ((tbl= child_li++))
 
3317
    {
 
3318
      if (tbl->table)
 
3319
      {
 
3320
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3321
        if (tbl->table->map & join->const_table_map)
 
3322
        {
 
3323
          pulled_tables |= tbl->table->map;
 
3324
        }
 
3325
      }
 
3326
    }
 
3327
    
 
3328
    /*
 
3329
      Action #2: Find which tables we can pull out based on
 
3330
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3331
      us to pull out some other tables too.
 
3332
    */
 
3333
    bool pulled_a_table;
 
3334
    do 
 
3335
    {
 
3336
      pulled_a_table= false;
 
3337
      child_li.rewind();
 
3338
      while ((tbl= child_li++))
 
3339
      {
 
3340
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3341
        {
 
3342
          if (find_eq_ref_candidate(tbl->table, 
 
3343
                                    sj_nest->nested_join->used_tables & 
 
3344
                                    ~pulled_tables))
 
3345
          {
 
3346
            pulled_a_table= true;
 
3347
            pulled_tables |= tbl->table->map;
 
3348
          }
 
3349
        }
 
3350
      }
 
3351
    } while (pulled_a_table);
 
3352
 
 
3353
    child_li.rewind();
 
3354
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3355
    {
 
3356
      (sj_nest)->sj_inner_tables= 0;
 
3357
      while ((tbl= child_li++))
 
3358
      {
 
3359
        if (tbl->table)
 
3360
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3361
      }
 
3362
    }
 
3363
    else
 
3364
    {
 
3365
      /* Record the bitmap of inner tables, mark the inner tables */
 
3366
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3367
                             ~pulled_tables;
 
3368
      (sj_nest)->sj_inner_tables= inner_tables;
 
3369
      while ((tbl= child_li++))
 
3370
      {
 
3371
        if (tbl->table)
 
3372
        {
 
3373
          if (inner_tables & tbl->table->map)
 
3374
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3375
          else
 
3376
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3377
        }
 
3378
      }
 
3379
    }
 
3380
  }
 
3381
  return(0);
 
3382
}
 
3383
 
455
3384
/*****************************************************************************
456
 
  Create JoinTableS, make a guess about the table types,
 
3385
  Create JOIN_TABS, make a guess about the table types,
457
3386
  Approximate how many records will be used in each table
458
3387
*****************************************************************************/
459
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3388
 
 
3389
 
 
3390
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3391
                                      TABLE *table,
 
3392
                                      const key_map *keys,ha_rows limit)
460
3393
{
461
3394
  int error;
462
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3395
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
463
3396
    return(0);                           // Fatal error flag is set
464
3397
  if (select)
465
3398
  {
466
3399
    select->head=table;
467
3400
    table->reginfo.impossible_range=0;
468
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3401
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
469
3402
                                          limit, 0, false)) == 1)
470
3403
      return(select->quick->records);
471
3404
    if (error == -1)
477
3410
  return(HA_POS_ERROR);                 /* This shouldn't happend */
478
3411
}
479
3412
 
 
3413
/*
 
3414
   This structure is used to collect info on potentially sargable
 
3415
   predicates in order to check whether they become sargable after
 
3416
   reading const tables.
 
3417
   We form a bitmap of indexes that can be used for sargable predicates.
 
3418
   Only such indexes are involved in range analysis.
 
3419
*/
 
3420
typedef struct st_sargable_param
 
3421
{
 
3422
  Field *field;              /* field against which to check sargability */
 
3423
  Item **arg_value;          /* values of potential keys for lookups     */
 
3424
  uint num_values;           /* number of values in the above array      */
 
3425
} SARGABLE_PARAM;  
 
3426
 
 
3427
/**
 
3428
  Calculate the best possible join and initialize the join structure.
 
3429
 
 
3430
  @retval
 
3431
    0   ok
 
3432
  @retval
 
3433
    1   Fatal error
 
3434
*/
 
3435
 
 
3436
static bool
 
3437
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
 
3438
                     DYNAMIC_ARRAY *keyuse_array)
 
3439
{
 
3440
  int error;
 
3441
  TABLE *table;
 
3442
  uint i,table_count,const_count,key;
 
3443
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3444
  key_map const_ref, eq_part;
 
3445
  TABLE **table_vector;
 
3446
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3447
  KEYUSE *keyuse,*start_keyuse;
 
3448
  table_map outer_join=0;
 
3449
  SARGABLE_PARAM *sargables= 0;
 
3450
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3451
 
 
3452
  table_count=join->tables;
 
3453
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3454
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3455
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
 
3456
  if (!stat || !stat_ref || !table_vector)
 
3457
    return(1);                          // Eom /* purecov: inspected */
 
3458
 
 
3459
  join->best_ref=stat_vector;
 
3460
 
 
3461
  stat_end=stat+table_count;
 
3462
  found_const_table_map= all_table_map=0;
 
3463
  const_count=0;
 
3464
 
 
3465
  for (s= stat, i= 0;
 
3466
       tables;
 
3467
       s++, tables= tables->next_leaf, i++)
 
3468
  {
 
3469
    TABLE_LIST *embedding= tables->embedding;
 
3470
    stat_vector[i]=s;
 
3471
    s->keys.init();
 
3472
    s->const_keys.init();
 
3473
    s->checked_keys.init();
 
3474
    s->needed_reg.init();
 
3475
    table_vector[i]=s->table=table=tables->table;
 
3476
    table->pos_in_table_list= tables;
 
3477
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3478
    if(error)
 
3479
    {
 
3480
        table->file->print_error(error, MYF(0));
 
3481
        return(1);
 
3482
    }
 
3483
    table->quick_keys.clear_all();
 
3484
    table->reginfo.join_tab=s;
 
3485
    table->reginfo.not_exists_optimize=0;
 
3486
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
 
3487
    all_table_map|= table->map;
 
3488
    s->join=join;
 
3489
    s->info=0;                                  // For describe
 
3490
 
 
3491
    s->dependent= tables->dep_tables;
 
3492
    s->key_dependent= 0;
 
3493
    if (tables->schema_table)
 
3494
      table->file->stats.records= 2;
 
3495
    table->quick_condition_rows= table->file->stats.records;
 
3496
 
 
3497
    s->on_expr_ref= &tables->on_expr;
 
3498
    if (*s->on_expr_ref)
 
3499
    {
 
3500
      /* s is the only inner table of an outer join */
 
3501
      if (!table->file->stats.records && !embedding)
 
3502
      {                                         // Empty table
 
3503
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3504
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3505
        continue;
 
3506
      }
 
3507
      outer_join|= table->map;
 
3508
      s->embedding_map= 0;
 
3509
      for (;embedding; embedding= embedding->embedding)
 
3510
        s->embedding_map|= embedding->nested_join->nj_map;
 
3511
      continue;
 
3512
    }
 
3513
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3514
    {
 
3515
      /* s belongs to a nested join, maybe to several embedded joins */
 
3516
      s->embedding_map= 0;
 
3517
      do
 
3518
      {
 
3519
        NESTED_JOIN *nested_join= embedding->nested_join;
 
3520
        s->embedding_map|=nested_join->nj_map;
 
3521
        s->dependent|= embedding->dep_tables;
 
3522
        embedding= embedding->embedding;
 
3523
        outer_join|= nested_join->used_tables;
 
3524
      }
 
3525
      while (embedding);
 
3526
      continue;
 
3527
    }
 
3528
    if ((table->s->system || table->file->stats.records <= 1) &&
 
3529
        !s->dependent &&
 
3530
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3531
        !table->fulltext_searched && !join->no_const_tables)
 
3532
    {
 
3533
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3534
    }
 
3535
  }
 
3536
  stat_vector[i]=0;
 
3537
  join->outer_join=outer_join;
 
3538
 
 
3539
  if (join->outer_join)
 
3540
  {
 
3541
    /* 
 
3542
       Build transitive closure for relation 'to be dependent on'.
 
3543
       This will speed up the plan search for many cases with outer joins,
 
3544
       as well as allow us to catch illegal cross references/
 
3545
       Warshall's algorithm is used to build the transitive closure.
 
3546
       As we use bitmaps to represent the relation the complexity
 
3547
       of the algorithm is O((number of tables)^2). 
 
3548
    */
 
3549
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3550
    {
 
3551
      for (uint j= 0 ; j < table_count ; j++)
 
3552
      {
 
3553
        table= stat[j].table;
 
3554
        if (s->dependent & table->map)
 
3555
          s->dependent |= table->reginfo.join_tab->dependent;
 
3556
      }
 
3557
      if (s->dependent)
 
3558
        s->table->maybe_null= 1;
 
3559
    }
 
3560
    /* Catch illegal cross references for outer joins */
 
3561
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3562
    {
 
3563
      if (s->dependent & s->table->map)
 
3564
      {
 
3565
        join->tables=0;                 // Don't use join->table
 
3566
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3567
        return(1);
 
3568
      }
 
3569
      s->key_dependent= s->dependent;
 
3570
    }
 
3571
  }
 
3572
 
 
3573
  if (conds || outer_join)
 
3574
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3575
                            conds, join->cond_equal,
 
3576
                            ~outer_join, join->select_lex, &sargables))
 
3577
      return(1);
 
3578
 
 
3579
  /* Read tables with 0 or 1 rows (system tables) */
 
3580
  join->const_table_map= 0;
 
3581
 
 
3582
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3583
       p_pos < p_end ;
 
3584
       p_pos++)
 
3585
  {
 
3586
    int tmp;
 
3587
    s= p_pos->table;
 
3588
    s->type=JT_SYSTEM;
 
3589
    join->const_table_map|=s->table->map;
 
3590
    if ((tmp=join_read_const_table(s, p_pos)))
 
3591
    {
 
3592
      if (tmp > 0)
 
3593
        return(1);                      // Fatal error
 
3594
    }
 
3595
    else
 
3596
      found_const_table_map|= s->table->map;
 
3597
  }
 
3598
 
 
3599
  /* loop until no more const tables are found */
 
3600
  int ref_changed;
 
3601
  do
 
3602
  {
 
3603
  more_const_tables_found:
 
3604
    ref_changed = 0;
 
3605
    found_ref=0;
 
3606
 
 
3607
    /*
 
3608
      We only have to loop from stat_vector + const_count as
 
3609
      set_position() will move all const_tables first in stat_vector
 
3610
    */
 
3611
 
 
3612
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3613
    {
 
3614
      table=s->table;
 
3615
 
 
3616
      /* 
 
3617
        If equi-join condition by a key is null rejecting and after a
 
3618
        substitution of a const table the key value happens to be null
 
3619
        then we can state that there are no matches for this equi-join.
 
3620
      */  
 
3621
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3622
      {
 
3623
        /* 
 
3624
          When performing an outer join operation if there are no matching rows
 
3625
          for the single row of the outer table all the inner tables are to be
 
3626
          null complemented and thus considered as constant tables.
 
3627
          Here we apply this consideration to the case of outer join operations 
 
3628
          with a single inner table only because the case with nested tables
 
3629
          would require a more thorough analysis.
 
3630
          TODO. Apply single row substitution to null complemented inner tables
 
3631
          for nested outer join operations. 
 
3632
        */              
 
3633
        while (keyuse->table == table)
 
3634
        {
 
3635
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3636
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3637
          {
 
3638
            s->type= JT_CONST;
 
3639
            mark_as_null_row(table);
 
3640
            found_const_table_map|= table->map;
 
3641
            join->const_table_map|= table->map;
 
3642
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3643
            goto more_const_tables_found;
 
3644
           }
 
3645
          keyuse++;
 
3646
        }
 
3647
      }
 
3648
 
 
3649
      if (s->dependent)                         // If dependent on some table
 
3650
      {
 
3651
        // All dep. must be constants
 
3652
        if (s->dependent & ~(found_const_table_map))
 
3653
          continue;
 
3654
        if (table->file->stats.records <= 1L &&
 
3655
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3656
            !table->pos_in_table_list->embedding)
 
3657
        {                                       // system table
 
3658
          int tmp= 0;
 
3659
          s->type=JT_SYSTEM;
 
3660
          join->const_table_map|=table->map;
 
3661
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3662
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3663
          {
 
3664
            if (tmp > 0)
 
3665
              return(1);                        // Fatal error
 
3666
          }
 
3667
          else
 
3668
            found_const_table_map|= table->map;
 
3669
          continue;
 
3670
        }
 
3671
      }
 
3672
      /* check if table can be read by key or table only uses const refs */
 
3673
      if ((keyuse=s->keyuse))
 
3674
      {
 
3675
        s->type= JT_REF;
 
3676
        while (keyuse->table == table)
 
3677
        {
 
3678
          start_keyuse=keyuse;
 
3679
          key=keyuse->key;
 
3680
          s->keys.set_bit(key);               // QQ: remove this ?
 
3681
 
 
3682
          refs=0;
 
3683
          const_ref.clear_all();
 
3684
          eq_part.clear_all();
 
3685
          do
 
3686
          {
 
3687
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3688
            {
 
3689
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3690
                const_ref.set_bit(keyuse->keypart);
 
3691
              else
 
3692
                refs|=keyuse->used_tables;
 
3693
              eq_part.set_bit(keyuse->keypart);
 
3694
            }
 
3695
            keyuse++;
 
3696
          } while (keyuse->table == table && keyuse->key == key);
 
3697
 
 
3698
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3699
              !table->fulltext_searched && 
 
3700
              !table->pos_in_table_list->embedding)
 
3701
          {
 
3702
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3703
                 == HA_NOSAME)
 
3704
            {
 
3705
              if (const_ref == eq_part)
 
3706
              {                                 // Found everything for ref.
 
3707
                int tmp;
 
3708
                ref_changed = 1;
 
3709
                s->type= JT_CONST;
 
3710
                join->const_table_map|=table->map;
 
3711
                set_position(join,const_count++,s,start_keyuse);
 
3712
                if (create_ref_for_key(join, s, start_keyuse,
 
3713
                                       found_const_table_map))
 
3714
                  return(1);
 
3715
                if ((tmp=join_read_const_table(s,
 
3716
                                               join->positions+const_count-1)))
 
3717
                {
 
3718
                  if (tmp > 0)
 
3719
                    return(1);                  // Fatal error
 
3720
                }
 
3721
                else
 
3722
                  found_const_table_map|= table->map;
 
3723
                break;
 
3724
              }
 
3725
              else
 
3726
                found_ref|= refs;      // Table is const if all refs are const
 
3727
            }
 
3728
            else if (const_ref == eq_part)
 
3729
              s->const_keys.set_bit(key);
 
3730
          }
 
3731
        }
 
3732
      }
 
3733
    }
 
3734
  } while (join->const_table_map & found_ref && ref_changed);
 
3735
 
 
3736
  /* 
 
3737
    Update info on indexes that can be used for search lookups as
 
3738
    reading const tables may has added new sargable predicates. 
 
3739
  */
 
3740
  if (const_count && sargables)
 
3741
  {
 
3742
    for( ; sargables->field ; sargables++)
 
3743
    {
 
3744
      Field *field= sargables->field;
 
3745
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3746
      key_map possible_keys= field->key_start;
 
3747
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3748
      bool is_const= 1;
 
3749
      for (uint j=0; j < sargables->num_values; j++)
 
3750
        is_const&= sargables->arg_value[j]->const_item();
 
3751
      if (is_const)
 
3752
        join_tab[0].const_keys.merge(possible_keys);
 
3753
    }
 
3754
  }
 
3755
 
 
3756
  if (pull_out_semijoin_tables(join))
 
3757
    return(true);
 
3758
 
 
3759
  /* Calc how many (possible) matched records in each table */
 
3760
 
 
3761
  for (s=stat ; s < stat_end ; s++)
 
3762
  {
 
3763
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3764
    {
 
3765
      /* Only one matching row */
 
3766
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3767
      continue;
 
3768
    }
 
3769
    /* Approximate found rows and time to read them */
 
3770
    s->found_records=s->records=s->table->file->stats.records;
 
3771
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3772
 
 
3773
    /*
 
3774
      Set a max range of how many seeks we can expect when using keys
 
3775
      This is can't be to high as otherwise we are likely to use
 
3776
      table scan.
 
3777
    */
 
3778
    s->worst_seeks= min((double) s->found_records / 10,
 
3779
                        (double) s->read_time*3);
 
3780
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3781
      s->worst_seeks=2.0;
 
3782
 
 
3783
    /*
 
3784
      Add to stat->const_keys those indexes for which all group fields or
 
3785
      all select distinct fields participate in one index.
 
3786
    */
 
3787
    add_group_and_distinct_keys(join, s);
 
3788
 
 
3789
    if (!s->const_keys.is_clear_all() &&
 
3790
        !s->table->pos_in_table_list->embedding)
 
3791
    {
 
3792
      ha_rows records;
 
3793
      SQL_SELECT *select;
 
3794
      select= make_select(s->table, found_const_table_map,
 
3795
                          found_const_table_map,
 
3796
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3797
                          1, &error);
 
3798
      if (!select)
 
3799
        return(1);
 
3800
      records= get_quick_record_count(join->thd, select, s->table,
 
3801
                                      &s->const_keys, join->row_limit);
 
3802
      s->quick=select->quick;
 
3803
      s->needed_reg=select->needed_reg;
 
3804
      select->quick=0;
 
3805
      if (records == 0 && s->table->reginfo.impossible_range)
 
3806
      {
 
3807
        /*
 
3808
          Impossible WHERE or ON expression
 
3809
          In case of ON, we mark that the we match one empty NULL row.
 
3810
          In case of WHERE, don't set found_const_table_map to get the
 
3811
          caller to abort with a zero row result.
 
3812
        */
 
3813
        join->const_table_map|= s->table->map;
 
3814
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3815
        s->type= JT_CONST;
 
3816
        if (*s->on_expr_ref)
 
3817
        {
 
3818
          /* Generate empty row */
 
3819
          s->info= "Impossible ON condition";
 
3820
          found_const_table_map|= s->table->map;
 
3821
          s->type= JT_CONST;
 
3822
          mark_as_null_row(s->table);           // All fields are NULL
 
3823
        }
 
3824
      }
 
3825
      if (records != HA_POS_ERROR)
 
3826
      {
 
3827
        s->found_records=records;
 
3828
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3829
      }
 
3830
      delete select;
 
3831
    }
 
3832
  }
 
3833
 
 
3834
  join->join_tab=stat;
 
3835
  join->map2table=stat_ref;
 
3836
  join->table= join->all_tables=table_vector;
 
3837
  join->const_tables=const_count;
 
3838
  join->found_const_table_map=found_const_table_map;
 
3839
 
 
3840
  /* Find an optimal join order of the non-constant tables. */
 
3841
  if (join->const_tables != join->tables)
 
3842
  {
 
3843
    optimize_keyuse(join, keyuse_array);
 
3844
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3845
      return(true);
 
3846
  }
 
3847
  else
 
3848
  {
 
3849
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
3850
           sizeof(POSITION)*join->const_tables);
 
3851
    join->best_read=1.0;
 
3852
  }
 
3853
  /* Generate an execution plan from the found optimal join order. */
 
3854
  return(join->thd->killed || get_best_combination(join));
 
3855
}
 
3856
 
 
3857
 
480
3858
/*****************************************************************************
481
3859
  Check with keys are used and with tables references with tables
482
3860
  Updates in stat:
485
3863
          keyuse     Pointer to possible keys
486
3864
*****************************************************************************/
487
3865
 
 
3866
/// Used when finding key fields
 
3867
typedef struct key_field_t {
 
3868
  Field         *field;
 
3869
  Item          *val;                   ///< May be empty if diff constant
 
3870
  uint          level;
 
3871
  uint          optimize; // KEY_OPTIMIZE_*
 
3872
  bool          eq_func;
 
3873
  /**
 
3874
    If true, the condition this struct represents will not be satisfied
 
3875
    when val IS NULL.
 
3876
  */
 
3877
  bool          null_rejecting; 
 
3878
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3879
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3880
} KEY_FIELD;
 
3881
 
 
3882
/**
 
3883
  Merge new key definitions to old ones, remove those not used in both.
 
3884
 
 
3885
  This is called for OR between different levels.
 
3886
 
 
3887
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3888
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3889
  that are internally transformed to something like:.
 
3890
 
 
3891
  @code
 
3892
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3893
  @endcode
 
3894
 
 
3895
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3896
  result has null_rejecting=true if it is set for both ORed references.
 
3897
  for example:
 
3898
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3899
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3900
 
 
3901
  @todo
 
3902
    The result of this is that we're missing some 'ref' accesses.
 
3903
    OptimizerTeam: Fix this
 
3904
*/
 
3905
 
 
3906
static KEY_FIELD *
 
3907
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3908
                 uint and_level)
 
3909
{
 
3910
  if (start == new_fields)
 
3911
    return start;                               // Impossible or
 
3912
  if (new_fields == end)
 
3913
    return start;                               // No new fields, skip all
 
3914
 
 
3915
  KEY_FIELD *first_free=new_fields;
 
3916
 
 
3917
  /* Mark all found fields in old array */
 
3918
  for (; new_fields != end ; new_fields++)
 
3919
  {
 
3920
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3921
    {
 
3922
      if (old->field == new_fields->field)
 
3923
      {
 
3924
        /*
 
3925
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3926
          it can return false where it is feasible to make it return true.
 
3927
          
 
3928
          The cause is as follows: Some of the tables are already known to be
 
3929
          const tables (the detection code is in make_join_statistics(),
 
3930
          above the update_ref_and_keys() call), but we didn't propagate 
 
3931
          information about this: TABLE::const_table is not set to true, and
 
3932
          Item::update_used_tables() hasn't been called for each item.
 
3933
          The result of this is that we're missing some 'ref' accesses.
 
3934
          TODO: OptimizerTeam: Fix this
 
3935
        */
 
3936
        if (!new_fields->val->const_item())
 
3937
        {
 
3938
          /*
 
3939
            If the value matches, we can use the key reference.
 
3940
            If not, we keep it until we have examined all new values
 
3941
          */
 
3942
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3943
          {
 
3944
            old->level= and_level;
 
3945
            old->optimize= ((old->optimize & new_fields->optimize &
 
3946
                             KEY_OPTIMIZE_EXISTS) |
 
3947
                            ((old->optimize | new_fields->optimize) &
 
3948
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3949
            old->null_rejecting= (old->null_rejecting &&
 
3950
                                  new_fields->null_rejecting);
 
3951
          }
 
3952
        }
 
3953
        else if (old->eq_func && new_fields->eq_func &&
 
3954
                 old->val->eq_by_collation(new_fields->val, 
 
3955
                                           old->field->binary(),
 
3956
                                           old->field->charset()))
 
3957
 
 
3958
        {
 
3959
          old->level= and_level;
 
3960
          old->optimize= ((old->optimize & new_fields->optimize &
 
3961
                           KEY_OPTIMIZE_EXISTS) |
 
3962
                          ((old->optimize | new_fields->optimize) &
 
3963
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3964
          old->null_rejecting= (old->null_rejecting &&
 
3965
                                new_fields->null_rejecting);
 
3966
        }
 
3967
        else if (old->eq_func && new_fields->eq_func &&
 
3968
                 ((old->val->const_item() && old->val->is_null()) || 
 
3969
                  new_fields->val->is_null()))
 
3970
        {
 
3971
          /* field = expression OR field IS NULL */
 
3972
          old->level= and_level;
 
3973
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3974
          /*
 
3975
            Remember the NOT NULL value unless the value does not depend
 
3976
            on other tables.
 
3977
          */
 
3978
          if (!old->val->used_tables() && old->val->is_null())
 
3979
            old->val= new_fields->val;
 
3980
          /* The referred expression can be NULL: */ 
 
3981
          old->null_rejecting= 0;
 
3982
        }
 
3983
        else
 
3984
        {
 
3985
          /*
 
3986
            We are comparing two different const.  In this case we can't
 
3987
            use a key-lookup on this so it's better to remove the value
 
3988
            and let the range optimzier handle it
 
3989
          */
 
3990
          if (old == --first_free)              // If last item
 
3991
            break;
 
3992
          *old= *first_free;                    // Remove old value
 
3993
          old--;                                // Retry this value
 
3994
        }
 
3995
      }
 
3996
    }
 
3997
  }
 
3998
  /* Remove all not used items */
 
3999
  for (KEY_FIELD *old=start ; old != first_free ;)
 
4000
  {
 
4001
    if (old->level != and_level)
 
4002
    {                                           // Not used in all levels
 
4003
      if (old == --first_free)
 
4004
        break;
 
4005
      *old= *first_free;                        // Remove old value
 
4006
      continue;
 
4007
    }
 
4008
    old++;
 
4009
  }
 
4010
  return first_free;
 
4011
}
 
4012
 
 
4013
 
 
4014
/**
 
4015
  Add a possible key to array of possible keys if it's usable as a key
 
4016
 
 
4017
    @param key_fields      Pointer to add key, if usable
 
4018
    @param and_level       And level, to be stored in KEY_FIELD
 
4019
    @param cond            Condition predicate
 
4020
    @param field           Field used in comparision
 
4021
    @param eq_func         True if we used =, <=> or IS NULL
 
4022
    @param value           Value used for comparison with field
 
4023
    @param usable_tables   Tables which can be used for key optimization
 
4024
    @param sargables       IN/OUT Array of found sargable candidates
 
4025
 
 
4026
  @note
 
4027
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4028
    table, we store this to be able to do not exists optimization later.
 
4029
 
 
4030
  @returns
 
4031
    *key_fields is incremented if we stored a key in the array
 
4032
*/
 
4033
 
 
4034
static void
 
4035
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4036
              Field *field, bool eq_func, Item **value, uint num_values,
 
4037
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4038
{
 
4039
  uint exists_optimize= 0;
 
4040
  if (!(field->flags & PART_KEY_FLAG))
 
4041
  {
 
4042
    // Don't remove column IS NULL on a LEFT JOIN table
 
4043
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4044
        !field->table->maybe_null || field->null_ptr)
 
4045
      return;                                   // Not a key. Skip it
 
4046
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4047
    assert(num_values == 1);
 
4048
  }
 
4049
  else
 
4050
  {
 
4051
    table_map used_tables=0;
 
4052
    bool optimizable=0;
 
4053
    for (uint i=0; i<num_values; i++)
 
4054
    {
 
4055
      used_tables|=(value[i])->used_tables();
 
4056
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4057
        optimizable=1;
 
4058
    }
 
4059
    if (!optimizable)
 
4060
      return;
 
4061
    if (!(usable_tables & field->table->map))
 
4062
    {
 
4063
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4064
          !field->table->maybe_null || field->null_ptr)
 
4065
        return;                                 // Can't use left join optimize
 
4066
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4067
    }
 
4068
    else
 
4069
    {
 
4070
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4071
      key_map possible_keys=field->key_start;
 
4072
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4073
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4074
 
 
4075
      /*
 
4076
        Save the following cases:
 
4077
        Field op constant
 
4078
        Field LIKE constant where constant doesn't start with a wildcard
 
4079
        Field = field2 where field2 is in a different table
 
4080
        Field op formula
 
4081
        Field IS NULL
 
4082
        Field IS NOT NULL
 
4083
         Field BETWEEN ...
 
4084
         Field IN ...
 
4085
      */
 
4086
      stat[0].key_dependent|=used_tables;
 
4087
 
 
4088
      bool is_const=1;
 
4089
      for (uint i=0; i<num_values; i++)
 
4090
      {
 
4091
        if (!(is_const&= value[i]->const_item()))
 
4092
          break;
 
4093
      }
 
4094
      if (is_const)
 
4095
        stat[0].const_keys.merge(possible_keys);
 
4096
      else if (!eq_func)
 
4097
      {
 
4098
        /* 
 
4099
          Save info to be able check whether this predicate can be 
 
4100
          considered as sargable for range analisis after reading const tables.
 
4101
          We do not save info about equalities as update_const_equal_items
 
4102
          will take care of updating info on keys from sargable equalities. 
 
4103
        */
 
4104
        (*sargables)--;
 
4105
        (*sargables)->field= field;
 
4106
        (*sargables)->arg_value= value;
 
4107
        (*sargables)->num_values= num_values;
 
4108
      }
 
4109
      /*
 
4110
        We can't always use indexes when comparing a string index to a
 
4111
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4112
        eq_func is NEVER true when num_values > 1
 
4113
       */
 
4114
      if (!eq_func)
 
4115
      {
 
4116
        /* 
 
4117
          Additional optimization: if we're processing
 
4118
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4119
          "t.key = c1".
 
4120
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4121
          There are 2 options:
 
4122
          A) Make equality propagation code be able to handle BETWEEN
 
4123
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4124
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4125
             and use them in equality propagation process (see details in
 
4126
             OptimizerKBAndTodo)
 
4127
        */
 
4128
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4129
            ((Item_func_between*) cond)->negated ||
 
4130
            !value[0]->eq(value[1], field->binary()))
 
4131
          return;
 
4132
        eq_func= true;
 
4133
      }
 
4134
 
 
4135
      if (field->result_type() == STRING_RESULT)
 
4136
      {
 
4137
        if ((*value)->result_type() != STRING_RESULT)
 
4138
        {
 
4139
          if (field->cmp_type() != (*value)->result_type())
 
4140
            return;
 
4141
        }
 
4142
        else
 
4143
        {
 
4144
          /*
 
4145
            We can't use indexes if the effective collation
 
4146
            of the operation differ from the field collation.
 
4147
          */
 
4148
          if (field->cmp_type() == STRING_RESULT &&
 
4149
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4150
            return;
 
4151
        }
 
4152
      }
 
4153
    }
 
4154
  }
 
4155
  /*
 
4156
    For the moment eq_func is always true. This slot is reserved for future
 
4157
    extensions where we want to remembers other things than just eq comparisons
 
4158
  */
 
4159
  assert(eq_func);
 
4160
  /* Store possible eq field */
 
4161
  (*key_fields)->field=         field;
 
4162
  (*key_fields)->eq_func=       eq_func;
 
4163
  (*key_fields)->val=           *value;
 
4164
  (*key_fields)->level=         and_level;
 
4165
  (*key_fields)->optimize=      exists_optimize;
 
4166
  /*
 
4167
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4168
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4169
    has NULL value.
 
4170
    We use null_rejecting in add_not_null_conds() to add
 
4171
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4172
  */
 
4173
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4174
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4175
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4176
                                  ((Item_field*)*value)->field->maybe_null());
 
4177
  (*key_fields)->cond_guard= NULL;
 
4178
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4179
                              cond->name < subq_sj_cond_name + 64)? 
 
4180
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4181
  (*key_fields)++;
 
4182
}
 
4183
 
 
4184
/**
 
4185
  Add possible keys to array of possible keys originated from a simple
 
4186
  predicate.
 
4187
 
 
4188
    @param  key_fields     Pointer to add key, if usable
 
4189
    @param  and_level      And level, to be stored in KEY_FIELD
 
4190
    @param  cond           Condition predicate
 
4191
    @param  field          Field used in comparision
 
4192
    @param  eq_func        True if we used =, <=> or IS NULL
 
4193
    @param  value          Value used for comparison with field
 
4194
                           Is NULL for BETWEEN and IN    
 
4195
    @param  usable_tables  Tables which can be used for key optimization
 
4196
    @param  sargables      IN/OUT Array of found sargable candidates
 
4197
 
 
4198
  @note
 
4199
    If field items f1 and f2 belong to the same multiple equality and
 
4200
    a key is added for f1, the the same key is added for f2.
 
4201
 
 
4202
  @returns
 
4203
    *key_fields is incremented if we stored a key in the array
 
4204
*/
 
4205
 
 
4206
static void
 
4207
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
 
4208
                     Item_func *cond, Item_field *field_item,
 
4209
                     bool eq_func, Item **val,
 
4210
                     uint num_values, table_map usable_tables,
 
4211
                     SARGABLE_PARAM **sargables)
 
4212
{
 
4213
  Field *field= field_item->field;
 
4214
  add_key_field(key_fields, and_level, cond, field,
 
4215
                eq_func, val, num_values, usable_tables, sargables);
 
4216
  Item_equal *item_equal= field_item->item_equal;
 
4217
  if (item_equal)
 
4218
  { 
 
4219
    /*
 
4220
      Add to the set of possible key values every substitution of
 
4221
      the field for an equal field included into item_equal
 
4222
    */
 
4223
    Item_equal_iterator it(*item_equal);
 
4224
    Item_field *item;
 
4225
    while ((item= it++))
 
4226
    {
 
4227
      if (!field->eq(item->field))
 
4228
      {
 
4229
        add_key_field(key_fields, and_level, cond, item->field,
 
4230
                      eq_func, val, num_values, usable_tables,
 
4231
                      sargables);
 
4232
      }
 
4233
    }
 
4234
  }
 
4235
}
 
4236
 
 
4237
static void
 
4238
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
 
4239
               COND *cond, table_map usable_tables,
 
4240
               SARGABLE_PARAM **sargables)
 
4241
{
 
4242
  if (cond->type() == Item_func::COND_ITEM)
 
4243
  {
 
4244
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4245
    KEY_FIELD *org_key_fields= *key_fields;
 
4246
 
 
4247
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4248
    {
 
4249
      Item *item;
 
4250
      while ((item=li++))
 
4251
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4252
                       sargables);
 
4253
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4254
        org_key_fields->level= *and_level;
 
4255
    }
 
4256
    else
 
4257
    {
 
4258
      (*and_level)++;
 
4259
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4260
                     sargables);
 
4261
      Item *item;
 
4262
      while ((item=li++))
 
4263
      {
 
4264
        KEY_FIELD *start_key_fields= *key_fields;
 
4265
        (*and_level)++;
 
4266
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4267
                       sargables);
 
4268
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4269
                                     *key_fields,++(*and_level));
 
4270
      }
 
4271
    }
 
4272
    return;
 
4273
  }
 
4274
 
 
4275
  /* 
 
4276
    Subquery optimization: Conditions that are pushed down into subqueries
 
4277
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4278
    but need to set cond_guard for KEYUSE elements generated from it.
 
4279
  */
 
4280
  {
 
4281
    if (cond->type() == Item::FUNC_ITEM &&
 
4282
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4283
    {
 
4284
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4285
      if (!join->group_list && !join->order &&
 
4286
          join->unit->item && 
 
4287
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4288
          !join->unit->is_union())
 
4289
      {
 
4290
        KEY_FIELD *save= *key_fields;
 
4291
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4292
                       sargables);
 
4293
        // Indicate that this ref access candidate is for subquery lookup:
 
4294
        for (; save != *key_fields; save++)
 
4295
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4296
      }
 
4297
      return;
 
4298
    }
 
4299
  }
 
4300
 
 
4301
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4302
  if (cond->type() != Item::FUNC_ITEM)
 
4303
    return;
 
4304
  Item_func *cond_func= (Item_func*) cond;
 
4305
  switch (cond_func->select_optimize()) {
 
4306
  case Item_func::OPTIMIZE_NONE:
 
4307
    break;
 
4308
  case Item_func::OPTIMIZE_KEY:
 
4309
  {
 
4310
    Item **values;
 
4311
    // BETWEEN, IN, NE
 
4312
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4313
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4314
    {
 
4315
      values= cond_func->arguments()+1;
 
4316
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4317
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4318
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4319
        values--;
 
4320
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4321
                  cond_func->argument_count() != 2);
 
4322
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4323
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4324
                           0, values, 
 
4325
                           cond_func->argument_count()-1,
 
4326
                           usable_tables, sargables);
 
4327
    }
 
4328
    if (cond_func->functype() == Item_func::BETWEEN)
 
4329
    {
 
4330
      values= cond_func->arguments();
 
4331
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
 
4332
      {
 
4333
        Item_field *field_item;
 
4334
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4335
            &&
 
4336
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4337
        {
 
4338
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4339
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4340
                               field_item, 0, values, 1, usable_tables, 
 
4341
                               sargables);
 
4342
        }
 
4343
      }  
 
4344
    }
 
4345
    break;
 
4346
  }
 
4347
  case Item_func::OPTIMIZE_OP:
 
4348
  {
 
4349
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4350
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4351
 
 
4352
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4353
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4354
    {
 
4355
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4356
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4357
                           equal_func,
 
4358
                           cond_func->arguments()+1, 1, usable_tables,
 
4359
                           sargables);
 
4360
    }
 
4361
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4362
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4363
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4364
    {
 
4365
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4366
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4367
                           equal_func,
 
4368
                           cond_func->arguments(),1,usable_tables,
 
4369
                           sargables);
 
4370
    }
 
4371
    break;
 
4372
  }
 
4373
  case Item_func::OPTIMIZE_NULL:
 
4374
    /* column_name IS [NOT] NULL */
 
4375
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4376
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4377
    {
 
4378
      Item *tmp=new Item_null;
 
4379
      if (unlikely(!tmp))                       // Should never be true
 
4380
        return;
 
4381
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4382
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4383
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4384
                           &tmp, 1, usable_tables, sargables);
 
4385
    }
 
4386
    break;
 
4387
  case Item_func::OPTIMIZE_EQUAL:
 
4388
    Item_equal *item_equal= (Item_equal *) cond;
 
4389
    Item *const_item= item_equal->get_const();
 
4390
    Item_equal_iterator it(*item_equal);
 
4391
    Item_field *item;
 
4392
    if (const_item)
 
4393
    {
 
4394
      /*
 
4395
        For each field field1 from item_equal consider the equality 
 
4396
        field1=const_item as a condition allowing an index access of the table
 
4397
        with field1 by the keys value of field1.
 
4398
      */   
 
4399
      while ((item= it++))
 
4400
      {
 
4401
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4402
                      true, &const_item, 1, usable_tables, sargables);
 
4403
      }
 
4404
    }
 
4405
    else 
 
4406
    {
 
4407
      /*
 
4408
        Consider all pairs of different fields included into item_equal.
 
4409
        For each of them (field1, field1) consider the equality 
 
4410
        field1=field2 as a condition allowing an index access of the table
 
4411
        with field1 by the keys value of field2.
 
4412
      */   
 
4413
      Item_equal_iterator fi(*item_equal);
 
4414
      while ((item= fi++))
 
4415
      {
 
4416
        Field *field= item->field;
 
4417
        while ((item= it++))
 
4418
        {
 
4419
          if (!field->eq(item->field))
 
4420
          {
 
4421
            add_key_field(key_fields, *and_level, cond_func, field,
 
4422
                          true, (Item **) &item, 1, usable_tables,
 
4423
                          sargables);
 
4424
          }
 
4425
        }
 
4426
        it.rewind();
 
4427
      }
 
4428
    }
 
4429
    break;
 
4430
  }
 
4431
}
488
4432
 
489
4433
/**
490
4434
  Add all keys with uses 'field' for some keypart.
491
4435
 
492
4436
  If field->and_level != and_level then only mark key_part as const_part.
493
4437
*/
494
 
uint32_t max_part_bit(key_part_map bits)
 
4438
 
 
4439
static uint
 
4440
max_part_bit(key_part_map bits)
495
4441
{
496
 
  uint32_t found;
 
4442
  uint found;
497
4443
  for (found=0; bits & 1 ; found++,bits>>=1) ;
498
4444
  return found;
499
4445
}
500
4446
 
501
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4447
static void
 
4448
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4449
{
 
4450
  Field *field=key_field->field;
 
4451
  TABLE *form= field->table;
 
4452
  KEYUSE keyuse;
 
4453
 
 
4454
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4455
  {
 
4456
    for (uint key=0 ; key < form->s->keys ; key++)
 
4457
    {
 
4458
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4459
        continue;
 
4460
 
 
4461
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4462
      for (uint part=0 ; part <  key_parts ; part++)
 
4463
      {
 
4464
        if (field->eq(form->key_info[key].key_part[part].field))
 
4465
        {
 
4466
          keyuse.table= field->table;
 
4467
          keyuse.val =  key_field->val;
 
4468
          keyuse.key =  key;
 
4469
          keyuse.keypart=part;
 
4470
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4471
          keyuse.used_tables=key_field->val->used_tables();
 
4472
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4473
          keyuse.null_rejecting= key_field->null_rejecting;
 
4474
          keyuse.cond_guard= key_field->cond_guard;
 
4475
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4476
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
 
4477
        }
 
4478
      }
 
4479
    }
 
4480
  }
 
4481
}
 
4482
 
 
4483
static int
 
4484
sort_keyuse(KEYUSE *a,KEYUSE *b)
502
4485
{
503
4486
  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()));
 
4487
  if (a->table->tablenr != b->table->tablenr)
 
4488
    return (int) (a->table->tablenr - b->table->tablenr);
 
4489
  if (a->key != b->key)
 
4490
    return (int) (a->key - b->key);
 
4491
  if (a->keypart != b->keypart)
 
4492
    return (int) (a->keypart - b->keypart);
510
4493
  // Place const values before other ones
511
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
512
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4494
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4495
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
513
4496
    return res;
514
4497
  /* 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)));
 
4498
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4499
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4500
}
 
4501
 
 
4502
 
 
4503
/*
 
4504
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4505
 
 
4506
    This function populates KEY_FIELD array with entries generated from the 
 
4507
    ON condition of the given nested join, and does the same for nested joins 
 
4508
    contained within this nested join.
 
4509
 
 
4510
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4511
  @param[in,out]  end                 End of the key field array
 
4512
  @param[in,out]  and_level           And-level
 
4513
  @param[in,out]  sargables           Array of found sargable candidates
 
4514
 
 
4515
 
 
4516
  @note
 
4517
    We can add accesses to the tables that are direct children of this nested 
 
4518
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4519
    
 
4520
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4521
    invoked for):
 
4522
    @code
 
4523
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4524
    @endcode
 
4525
    Example for #2:
 
4526
    @code
 
4527
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4528
    @endcode
 
4529
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4530
    t1 only.
 
4531
    Example #3:
 
4532
    @code
 
4533
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4534
    @endcode
 
4535
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4536
*/
 
4537
 
 
4538
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4539
                                  KEY_FIELD **end, uint *and_level,
 
4540
                                  SARGABLE_PARAM **sargables)
 
4541
{
 
4542
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4543
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
 
4544
  bool have_another = false;
 
4545
  table_map tables= 0;
 
4546
  TABLE_LIST *table;
 
4547
  assert(nested_join_table->nested_join);
 
4548
 
 
4549
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4550
                                            (table= li++))))
 
4551
  {
 
4552
    if (table->nested_join)
 
4553
    {
 
4554
      if (!table->on_expr)
 
4555
      {
 
4556
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4557
        have_another= true;
 
4558
        li2= li;
 
4559
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
 
4560
      }
 
4561
      else
 
4562
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4563
    }
 
4564
    else
 
4565
      if (!table->on_expr)
 
4566
        tables |= table->table->map;
 
4567
  }
 
4568
  if (nested_join_table->on_expr)
 
4569
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4570
                   sargables);
517
4571
}
518
4572
 
519
4573
 
520
4574
/**
521
4575
  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
 
4576
  
 
4577
  @param       thd 
 
4578
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
525
4579
  @param       join_tab       Array in tablenr_order
526
4580
  @param       tables         Number of tables in join
527
4581
  @param       cond           WHERE condition (note that the function analyzes
530
4584
                              for which we can make ref access based the WHERE
531
4585
                              clause)
532
4586
  @param       select_lex     current SELECT
533
 
  @param[out]  sargables      std::vector of found sargable candidates
534
 
 
 
4587
  @param[out]  sargables      Array of found sargable candidates
 
4588
      
535
4589
   @retval
536
4590
     0  OK
537
4591
   @retval
538
4592
     1  Out of memory.
539
4593
*/
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)
 
4594
 
 
4595
static bool
 
4596
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4597
                    uint tables, COND *cond,
 
4598
                    COND_EQUAL *cond_equal __attribute__((__unused__)),
 
4599
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4600
                    SARGABLE_PARAM **sargables)
549
4601
{
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
 
4602
  uint  and_level,i,found_eq_constant;
 
4603
  KEY_FIELD *key_fields, *end, *field;
 
4604
  uint sz;
 
4605
  uint m= max(select_lex->max_equal_elems,1);
 
4606
  
 
4607
  /* 
 
4608
    We use the same piece of memory to store both  KEY_FIELD 
 
4609
    and SARGABLE_PARAM structure.
 
4610
    KEY_FIELD values are placed at the beginning this memory
 
4611
    while  SARGABLE_PARAM values are put at the end.
 
4612
    All predicates that are used to fill arrays of KEY_FIELD
 
4613
    and SARGABLE_PARAM structures have at most 2 arguments
 
4614
    except BETWEEN predicates that have 3 arguments and 
559
4615
    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
 
4616
    This any predicate if it's not BETWEEN/IN can be used 
 
4617
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4618
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
563
4619
    can be filled as this predicate is considered as
564
4620
    saragable with respect to each of its argument.
565
4621
    An IN predicate can require at most 1 element as currently
566
4622
    it is considered as sargable only for its first argument.
567
4623
    Multiple equality can add  elements that are filled after
568
4624
    substitution of field arguments by equal fields. There
569
 
    can be not more than select_lex->max_equal_elems such
 
4625
    can be not more than select_lex->max_equal_elems such 
570
4626
    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;
 
4627
  */ 
 
4628
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4629
      (((thd->lex->current_select->cond_count+1)*2 +
 
4630
        thd->lex->current_select->between_count)*m+1);
 
4631
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4632
    return true; /* purecov: inspected */
577
4633
  and_level= 0;
578
4634
  field= end= key_fields;
 
4635
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4636
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4637
  /* set a barrier for the array of SARGABLE_PARAM */
 
4638
  (*sargables)[0].field= 0; 
579
4639
 
580
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4640
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
581
4641
    return true;
582
4642
  if (cond)
583
4643
  {
584
4644
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
585
4645
                   sargables);
586
 
    for (; field != end; field++)
 
4646
    for (; field != end ; field++)
587
4647
    {
588
 
      add_key_part(keyuse, field);
 
4648
      add_key_part(keyuse,field);
589
4649
      /* 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
 
      }
 
4650
      if (field->val->type() == Item::NULL_ITEM &&
 
4651
          !field->field->real_maybe_null())
 
4652
        field->field->table->reginfo.not_exists_optimize=1;
595
4653
    }
596
4654
  }
597
 
  for (uint32_t i= 0; i < tables; i++)
 
4655
  for (i=0 ; i < tables ; i++)
598
4656
  {
599
4657
    /*
600
4658
      Block the creation of keys for inner tables of outer joins.
604
4662
      In the future when we introduce conditional accesses
605
4663
      for inner tables in outer joins these keys will be taken
606
4664
      into account as well.
607
 
    */
 
4665
    */ 
608
4666
    if (*join_tab[i].on_expr_ref)
609
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4667
      add_key_fields(join_tab->join, &end, &and_level, 
610
4668
                     *join_tab[i].on_expr_ref,
611
4669
                     join_tab[i].table->map, sargables);
612
4670
  }
613
4671
 
614
4672
  /* Process ON conditions for the nested joins */
615
4673
  {
616
 
    List_iterator<TableList> li(*join_tab->join->join_list);
617
 
    TableList *table;
 
4674
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4675
    TABLE_LIST *table;
618
4676
    while ((table= li++))
619
4677
    {
620
 
      if (table->getNestedJoin())
621
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4678
      if (table->nested_join)
 
4679
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
622
4680
                              sargables);
623
4681
    }
624
4682
  }
635
4693
    - keyparts without previous keyparts
636
4694
      (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
637
4695
      used in the query, we drop the partial key parts from consideration).
 
4696
    Special treatment for ft-keys.
638
4697
  */
639
4698
  if (keyuse->elements)
640
4699
  {
641
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
642
 
 
643
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
644
 
                       (qsort_cmp) sort_keyuse);
645
 
 
646
 
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
647
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
648
 
 
649
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4700
    KEYUSE key_end,*prev,*save_pos,*use;
 
4701
 
 
4702
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4703
          (qsort_cmp) sort_keyuse);
 
4704
 
 
4705
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
 
4706
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
 
4707
 
 
4708
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
650
4709
    prev= &key_end;
651
 
    found_eq_constant= 0;
 
4710
    found_eq_constant=0;
 
4711
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
652
4712
    {
653
 
      uint32_t i;
654
 
 
655
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4713
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4714
        use->table->const_key_parts[use->key]|= use->keypart_map;
656
4715
      {
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;
 
4716
        if (use->key == prev->key && use->table == prev->table)
 
4717
        {
 
4718
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4719
            continue;                           /* remove */
 
4720
        }
 
4721
        else if (use->keypart != 0)             // First found must be 0
 
4722
          continue;
 
4723
      }
667
4724
 
668
 
#ifdef HAVE_VALGRIND
669
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
670
 
        if (save_pos != use)
 
4725
#ifdef HAVE_purify
 
4726
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4727
      if (save_pos != use)
671
4728
#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;
 
4729
        *save_pos= *use;
 
4730
      prev=use;
 
4731
      found_eq_constant= !use->used_tables;
 
4732
      /* Save ptr to first use */
 
4733
      if (!use->table->reginfo.join_tab->keyuse)
 
4734
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4735
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4736
      save_pos++;
684
4737
    }
 
4738
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4739
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
 
4740
    keyuse->elements=i;
685
4741
  }
686
4742
  return false;
687
4743
}
689
4745
/**
690
4746
  Update some values in keyuse for faster choose_plan() loop.
691
4747
*/
692
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4748
 
 
4749
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
693
4750
{
694
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
695
 
                                                  0, 
696
 
                                                  optimizer::KeyUse*);
 
4751
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
697
4752
 
698
4753
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
699
4754
  {
706
4761
      Constant tables are ignored.
707
4762
      To avoid bad matches, we don't make ref_table_rows less than 100.
708
4763
    */
709
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
710
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4764
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4765
    if (keyuse->used_tables &
 
4766
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4767
               ~OUTER_REF_TABLE_BIT)))
711
4768
    {
712
 
      uint32_t tablenr;
 
4769
      uint tablenr;
713
4770
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
714
4771
      if (map == 1)                     // Only one table
715
4772
      {
716
 
        Table *tmp_table=join->all_tables[tablenr];
717
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4773
        TABLE *tmp_table=join->all_tables[tablenr];
 
4774
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
718
4775
      }
719
4776
    }
720
4777
    /*
721
4778
      Outer reference (external field) is constant for single executing
722
4779
      of subquery
723
4780
    */
724
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
725
 
      keyuse->setTableRows(1);
 
4781
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4782
      keyuse->ref_table_rows= 1;
726
4783
  }
727
4784
}
728
4785
 
744
4801
  @return
745
4802
    None
746
4803
*/
747
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4804
 
 
4805
static void
 
4806
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
748
4807
{
749
4808
  List<Item_field> indexed_fields;
750
4809
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
751
 
  Order      *cur_group;
 
4810
  ORDER      *cur_group;
752
4811
  Item_field *cur_item;
753
4812
  key_map possible_keys(0);
754
4813
 
756
4815
  { /* Collect all query fields referenced in the GROUP clause. */
757
4816
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
758
4817
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
759
 
                               (unsigned char*) &indexed_fields);
 
4818
                               (uchar*) &indexed_fields);
760
4819
  }
761
4820
  else if (join->select_distinct)
762
4821
  { /* Collect all query fields referenced in the SELECT clause. */
765
4824
    Item *item;
766
4825
    while ((item= select_items_it++))
767
4826
      item->walk(&Item::collect_item_field_processor, 0,
768
 
                 (unsigned char*) &indexed_fields);
 
4827
                 (uchar*) &indexed_fields);
769
4828
  }
770
4829
  else
771
4830
    return;
775
4834
 
776
4835
  /* Intersect the keys of all group fields. */
777
4836
  cur_item= indexed_fields_it++;
778
 
  possible_keys|= cur_item->field->part_of_key;
 
4837
  possible_keys.merge(cur_item->field->part_of_key);
779
4838
  while ((cur_item= indexed_fields_it++))
780
4839
  {
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
 
4840
    possible_keys.intersect(cur_item->field->part_of_key);
 
4841
  }
 
4842
 
 
4843
  if (!possible_keys.is_clear_all())
 
4844
    join_tab->const_keys.merge(possible_keys);
 
4845
}
 
4846
 
 
4847
 
 
4848
/*****************************************************************************
 
4849
  Go through all combinations of not marked tables and find the one
 
4850
  which uses least records
 
4851
*****************************************************************************/
 
4852
 
 
4853
/** Save const tables first as used tables. */
 
4854
 
 
4855
static void
 
4856
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
 
4857
{
 
4858
  join->positions[idx].table= table;
 
4859
  join->positions[idx].key=key;
 
4860
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4861
  join->positions[idx].ref_depend_map= 0;
 
4862
 
 
4863
  /* Move the const table as down as possible in best_ref */
 
4864
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4865
  JOIN_TAB *next=join->best_ref[idx];
 
4866
  for (;next != table ; pos++)
 
4867
  {
 
4868
    JOIN_TAB *tmp=pos[0];
 
4869
    pos[0]=next;
 
4870
    next=tmp;
 
4871
  }
 
4872
  join->best_ref[idx]=table;
 
4873
}
 
4874
 
 
4875
 
 
4876
/*
 
4877
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4878
 
 
4879
  SYNOPSIS
 
4880
    get_bound_sj_equalities()
 
4881
      sj_nest           Semi-join nest
 
4882
      remaining_tables  Tables that are not yet bound
 
4883
 
 
4884
  DESCRIPTION
 
4885
    Given a semi-join nest, find out which of the IN-equalities have their
 
4886
    left part expression bound (i.e. the said expression doesn't refer to
 
4887
    any of remaining_tables and can be evaluated).
 
4888
 
 
4889
  RETURN
 
4890
    Bitmap of bound IN-equalities.
 
4891
*/
 
4892
 
 
4893
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest, 
 
4894
                                  table_map remaining_tables)
 
4895
{
 
4896
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4897
  Item *item;
 
4898
  uint i= 0;
 
4899
  ulonglong res= 0;
 
4900
  while ((item= li++))
 
4901
  {
 
4902
    /*
 
4903
      Q: should this take into account equality propagation and how?
 
4904
      A: If e->outer_side is an Item_field, walk over the equality
 
4905
         class and see if there is an element that is bound?
 
4906
      (this is an optional feature)
 
4907
    */
 
4908
    if (!(item->used_tables() & remaining_tables))
 
4909
    {
 
4910
      res |= 1ULL < i;
 
4911
    }
 
4912
  }
 
4913
  return res;
 
4914
}
 
4915
 
 
4916
 
 
4917
/**
 
4918
  Find the best access path for an extension of a partial execution
 
4919
  plan and add this path to the plan.
 
4920
 
 
4921
  The function finds the best access path to table 's' from the passed
 
4922
  partial plan where an access path is the general term for any means to
 
4923
  access the data in 's'. An access path may use either an index or a scan,
 
4924
  whichever is cheaper. The input partial plan is passed via the array
 
4925
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4926
  cost are stored in 'join->positions[idx]'.
 
4927
 
 
4928
  @param join             pointer to the structure providing all context info
 
4929
                          for the query
 
4930
  @param s                the table to be joined by the function
 
4931
  @param thd              thread for the connection that submitted the query
 
4932
  @param remaining_tables set of tables not included into the partial plan yet
 
4933
  @param idx              the length of the partial plan
 
4934
  @param record_count     estimate for the number of records returned by the
 
4935
                          partial plan
 
4936
  @param read_time        the cost of the partial plan
 
4937
 
 
4938
  @return
 
4939
    None
 
4940
*/
 
4941
 
 
4942
static void
 
4943
best_access_path(JOIN      *join,
 
4944
                 JOIN_TAB  *s,
 
4945
                 THD       *thd,
 
4946
                 table_map remaining_tables,
 
4947
                 uint      idx,
 
4948
                 double    record_count,
 
4949
                 double    read_time __attribute__((__unused__)))
 
4950
{
 
4951
  KEYUSE *best_key=         0;
 
4952
  uint best_max_key_part=   0;
 
4953
  my_bool found_constraint= 0;
 
4954
  double best=              DBL_MAX;
 
4955
  double best_time=         DBL_MAX;
 
4956
  double records=           DBL_MAX;
 
4957
  table_map best_ref_depends_map= 0;
 
4958
  double tmp;
 
4959
  ha_rows rec;
 
4960
  uint best_is_sj_inside_out=    0;
 
4961
 
 
4962
  if (s->keyuse)
 
4963
  {                                            /* Use key if possible */
 
4964
    TABLE *table= s->table;
 
4965
    KEYUSE *keyuse,*start_key=0;
 
4966
    double best_records= DBL_MAX;
 
4967
    uint max_key_part=0;
 
4968
    ulonglong bound_sj_equalities= 0;
 
4969
    bool try_sj_inside_out= false;
 
4970
    /*
 
4971
      Discover the bound equalites. We need to do this, if
 
4972
        1. The next table is an SJ-inner table, and
 
4973
        2. It is the first table from that semijoin, and
 
4974
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4975
           all or none of their tables in join_table_map), except
 
4976
           s->emb_sj_nest (which we've just entered).
 
4977
        3. All correlation references from this sj-nest are bound
 
4978
    */
 
4979
    if (s->emb_sj_nest &&                                                 // (1)
 
4980
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4981
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4982
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4983
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4984
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4985
    {
 
4986
      /* This table is an InsideOut scan candidate */
 
4987
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4988
                                                   remaining_tables);
 
4989
      try_sj_inside_out= true;
 
4990
    }
 
4991
 
 
4992
    /* Test how we can use keys */
 
4993
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4994
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4995
    {
 
4996
      key_part_map found_part= 0;
 
4997
      table_map found_ref= 0;
 
4998
      uint key= keyuse->key;
 
4999
      KEY *keyinfo= table->key_info+key;
 
5000
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
5001
      key_part_map const_part= 0;
 
5002
      /* The or-null keypart in ref-or-null access: */
 
5003
      key_part_map ref_or_null_part= 0;
 
5004
 
 
5005
      /* Calculate how many key segments of the current key we can use */
 
5006
      start_key= keyuse;
 
5007
      ulonglong handled_sj_equalities=0;
 
5008
      key_part_map sj_insideout_map= 0;
 
5009
 
 
5010
      do /* For each keypart */
 
5011
      {
 
5012
        uint keypart= keyuse->keypart;
 
5013
        table_map best_part_found_ref= 0;
 
5014
        double best_prev_record_reads= DBL_MAX;
 
5015
        
 
5016
        do /* For each way to access the keypart */
 
5017
        {
 
5018
 
 
5019
          /*
 
5020
            if 1. expression doesn't refer to forward tables
 
5021
               2. we won't get two ref-or-null's
 
5022
          */
 
5023
          if (!(remaining_tables & keyuse->used_tables) &&
 
5024
              !(ref_or_null_part && (keyuse->optimize &
 
5025
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5026
          {
 
5027
            found_part|= keyuse->keypart_map;
 
5028
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5029
              const_part|= keyuse->keypart_map;
 
5030
 
 
5031
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5032
                                                      keyuse->used_tables));
 
5033
            if (tmp2 < best_prev_record_reads)
 
5034
            {
 
5035
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5036
              best_prev_record_reads= tmp2;
 
5037
            }
 
5038
            if (rec > keyuse->ref_table_rows)
 
5039
              rec= keyuse->ref_table_rows;
 
5040
            /*
 
5041
              If there is one 'key_column IS NULL' expression, we can
 
5042
              use this ref_or_null optimisation of this field
 
5043
            */
 
5044
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5045
              ref_or_null_part |= keyuse->keypart_map;
 
5046
          }
 
5047
 
 
5048
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5049
          {
 
5050
            if (!(remaining_tables & keyuse->used_tables))
 
5051
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5052
            else
 
5053
            {
 
5054
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5055
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5056
            }
 
5057
          }
 
5058
 
 
5059
          keyuse++;
 
5060
        } while (keyuse->table == table && keyuse->key == key &&
 
5061
                 keyuse->keypart == keypart);
 
5062
        found_ref|= best_part_found_ref;
 
5063
      } while (keyuse->table == table && keyuse->key == key);
 
5064
 
 
5065
      /*
 
5066
        Assume that that each key matches a proportional part of table.
 
5067
      */
 
5068
      if (!found_part && !handled_sj_equalities)
 
5069
        continue;                               // Nothing usable found
 
5070
 
 
5071
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5072
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5073
 
 
5074
      bool sj_inside_out_scan= false;
 
5075
      {
 
5076
        found_constraint= 1;
 
5077
        /*
 
5078
          Check if InsideOut scan is applicable:
 
5079
          1. All IN-equalities are either "bound" or "handled"
 
5080
          2. Index keyparts are 
 
5081
             ...
 
5082
        */
 
5083
        if (try_sj_inside_out && 
 
5084
            table->covering_keys.is_set(key) &&
 
5085
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5086
            PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5087
        {
 
5088
          uint n_fixed_parts= max_part_bit(found_part);
 
5089
          if (n_fixed_parts != keyinfo->key_parts &&
 
5090
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5091
               PREV_BITS(uint, keyinfo->key_parts))
 
5092
          {
 
5093
            /*
 
5094
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5095
              check if all of them are covered.
 
5096
            */
 
5097
            sj_inside_out_scan= true;
 
5098
            if (!n_fixed_parts)
 
5099
            {
 
5100
              /*
 
5101
                It's a confluent ref scan.
 
5102
 
 
5103
                That is, all found KEYUSE elements refer to IN-equalities,
 
5104
                and there is really no ref access because there is no
 
5105
                  t.keypart0 = {bound expression}
 
5106
 
 
5107
                Calculate the cost of complete loose index scan.
 
5108
              */
 
5109
              records= (double)s->table->file->stats.records;
 
5110
 
 
5111
              /* The cost is entire index scan cost (divided by 2) */
 
5112
              best_time= s->table->file->index_only_read_time(key, records);
 
5113
 
 
5114
              /* Now figure how many different keys we will get */
 
5115
              ulong rpc;
 
5116
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5117
                records= records / rpc;
 
5118
              start_key= NULL;
 
5119
            }
 
5120
          }
 
5121
        }
 
5122
 
 
5123
        /*
 
5124
          Check if we found full key
 
5125
        */
 
5126
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5127
            !ref_or_null_part)
 
5128
        {                                         /* use eq key */
 
5129
          max_key_part= (uint) ~0;
 
5130
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5131
          {
 
5132
            tmp = prev_record_reads(join, idx, found_ref);
 
5133
            records=1.0;
 
5134
          }
 
5135
          else
 
5136
          {
 
5137
            if (!found_ref)
 
5138
            {                                     /* We found a const key */
 
5139
              /*
 
5140
                ReuseRangeEstimateForRef-1:
 
5141
                We get here if we've found a ref(const) (c_i are constants):
 
5142
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5143
                
 
5144
                If range optimizer was able to construct a "range" 
 
5145
                access on this index, then its condition "quick_cond" was
 
5146
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5147
                from the range optimizer.
 
5148
                
 
5149
                Proof of (*): By properties of range and ref optimizers 
 
5150
                quick_cond will be equal or tighther than ref_const_cond. 
 
5151
                ref_const_cond already covers "smallest" possible interval - 
 
5152
                a singlepoint interval over all keyparts. Therefore, 
 
5153
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5154
                empty interval we wouldn't have got here).
 
5155
              */
 
5156
              if (table->quick_keys.is_set(key))
 
5157
                records= (double) table->quick_rows[key];
 
5158
              else
 
5159
              {
 
5160
                /* quick_range couldn't use key! */
 
5161
                records= (double) s->records/rec;
 
5162
              }
 
5163
            }
 
5164
            else
 
5165
            {
 
5166
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5167
              {                                   /* Prefer longer keys */
 
5168
                records=
 
5169
                  ((double) s->records / (double) rec *
 
5170
                   (1.0 +
 
5171
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5172
                     (double) table->s->max_key_length)));
 
5173
                if (records < 2.0)
 
5174
                  records=2.0;               /* Can't be as good as a unique */
 
5175
              }
 
5176
              /*
 
5177
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5178
                E(#rows) from range optimizer. Make another try:
 
5179
                
 
5180
                If range optimizer produced E(#rows) for a prefix of the ref
 
5181
                access we're considering, and that E(#rows) is lower then our
 
5182
                current estimate, make an adjustment. The criteria of when we
 
5183
                can make an adjustment is a special case of the criteria used
 
5184
                in ReuseRangeEstimateForRef-3.
 
5185
              */
 
5186
              if (table->quick_keys.is_set(key) &&
 
5187
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5188
                  table->quick_n_ranges[key] == 1 &&
 
5189
                  records > (double) table->quick_rows[key])
 
5190
              {
 
5191
                records= (double) table->quick_rows[key];
 
5192
              }
 
5193
            }
 
5194
            /* Limit the number of matched rows */
 
5195
            tmp= records;
 
5196
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5197
            if (table->covering_keys.is_set(key))
 
5198
            {
 
5199
              /* we can use only index tree */
 
5200
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5201
            }
 
5202
            else
 
5203
              tmp= record_count*min(tmp,s->worst_seeks);
 
5204
          }
 
5205
        }
 
5206
        else
 
5207
        {
 
5208
          /*
 
5209
            Use as much key-parts as possible and a uniq key is better
 
5210
            than a not unique key
 
5211
            Set tmp to (previous record count) * (records / combination)
 
5212
          */
 
5213
          if ((found_part & 1) &&
 
5214
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5215
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5216
          {
 
5217
            max_key_part= max_part_bit(found_part);
 
5218
            /*
 
5219
              ReuseRangeEstimateForRef-3:
 
5220
              We're now considering a ref[or_null] access via
 
5221
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5222
              (same-as-above but with one cond replaced 
 
5223
               with "t.keypart_i IS NULL")]  (**)
 
5224
              
 
5225
              Try re-using E(#rows) from "range" optimizer:
 
5226
              We can do so if "range" optimizer used the same intervals as
 
5227
              in (**). The intervals used by range optimizer may be not 
 
5228
              available at this point (as "range" access might have choosen to
 
5229
              create quick select over another index), so we can't compare
 
5230
              them to (**). We'll make indirect judgements instead.
 
5231
              The sufficient conditions for re-use are:
 
5232
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5233
                   this is not satisfied we have no way to know which ranges
 
5234
                   will be actually scanned by 'ref' until we execute the 
 
5235
                   join)
 
5236
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5237
                   is apparently a necessary requirement)
 
5238
 
 
5239
              We also have a property that "range optimizer produces equal or 
 
5240
              tighter set of scan intervals than ref(const) optimizer". Each
 
5241
              of the intervals in (**) are "tightest possible" intervals when 
 
5242
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5243
              From here it follows that range access used either one, or
 
5244
              both of the (I1) and (I2) intervals:
 
5245
              
 
5246
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5247
               (same-as-above but with one cond replaced  
 
5248
                with "t.keypart_i IS NULL")               (I2)
 
5249
 
 
5250
              The remaining part is to exclude the situation where range
 
5251
              optimizer used one interval while we're considering
 
5252
              ref-or-null and looking for estimate for two intervals. This
 
5253
              is done by last limitation:
 
5254
 
 
5255
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5256
            */
 
5257
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5258
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5259
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
 
5260
            {
 
5261
              tmp= records= (double) table->quick_rows[key];
 
5262
            }
 
5263
            else
 
5264
            {
 
5265
              /* Check if we have statistic about the distribution */
 
5266
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5267
              {
 
5268
                /* 
 
5269
                  Fix for the case where the index statistics is too
 
5270
                  optimistic: If 
 
5271
                  (1) We're considering ref(const) and there is quick select
 
5272
                      on the same index, 
 
5273
                  (2) and that quick select uses more keyparts (i.e. it will
 
5274
                      scan equal/smaller interval then this ref(const))
 
5275
                  (3) and E(#rows) for quick select is higher then our
 
5276
                      estimate,
 
5277
                  Then 
 
5278
                    We'll use E(#rows) from quick select.
 
5279
 
 
5280
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5281
                  cheaper in some cases ?
 
5282
                  TODO: figure this out and adjust the plan choice if needed.
 
5283
                */
 
5284
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5285
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5286
                    records < (double)table->quick_rows[key])         // (3)
 
5287
                  records= (double)table->quick_rows[key];
 
5288
 
 
5289
                tmp= records;
 
5290
              }
 
5291
              else
 
5292
              {
 
5293
                /*
 
5294
                  Assume that the first key part matches 1% of the file
 
5295
                  and that the whole key matches 10 (duplicates) or 1
 
5296
                  (unique) records.
 
5297
                  Assume also that more key matches proportionally more
 
5298
                  records
 
5299
                  This gives the formula:
 
5300
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5301
 
 
5302
                  b = records matched by whole key
 
5303
                  a = records matched by first key part (1% of all records?)
 
5304
                  c = number of key parts in key
 
5305
                  x = used key parts (1 <= x <= c)
 
5306
                */
 
5307
                double rec_per_key;
 
5308
                if (!(rec_per_key=(double)
 
5309
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5310
                  rec_per_key=(double) s->records/rec+1;
 
5311
 
 
5312
                if (!s->records)
 
5313
                  tmp = 0;
 
5314
                else if (rec_per_key/(double) s->records >= 0.01)
 
5315
                  tmp = rec_per_key;
 
5316
                else
 
5317
                {
 
5318
                  double a=s->records*0.01;
 
5319
                  if (keyinfo->key_parts > 1)
 
5320
                    tmp= (max_key_part * (rec_per_key - a) +
 
5321
                          a*keyinfo->key_parts - rec_per_key)/
 
5322
                         (keyinfo->key_parts-1);
 
5323
                  else
 
5324
                    tmp= a;
 
5325
                  set_if_bigger(tmp,1.0);
 
5326
                }
 
5327
                records = (ulong) tmp;
 
5328
              }
 
5329
 
 
5330
              if (ref_or_null_part)
 
5331
              {
 
5332
                /* We need to do two key searches to find key */
 
5333
                tmp *= 2.0;
 
5334
                records *= 2.0;
 
5335
              }
 
5336
 
 
5337
              /*
 
5338
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5339
                E(#rows) from range optimizer. Make another try:
 
5340
                
 
5341
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5342
                access we're considering, and that E(#rows) is lower then our
 
5343
                current estimate, make the adjustment.
 
5344
 
 
5345
                The decision whether we can re-use the estimate from the range
 
5346
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5347
                applied to first table->quick_key_parts[key] key parts.
 
5348
              */
 
5349
              if (table->quick_keys.is_set(key) &&
 
5350
                  table->quick_key_parts[key] <= max_key_part &&
 
5351
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5352
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5353
                                                         const_part) &&
 
5354
                  records > (double) table->quick_rows[key])
 
5355
              {
 
5356
                tmp= records= (double) table->quick_rows[key];
 
5357
              }
 
5358
            }
 
5359
 
 
5360
            /* Limit the number of matched rows */
 
5361
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5362
            if (table->covering_keys.is_set(key))
 
5363
            {
 
5364
              /* we can use only index tree */
 
5365
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5366
            }
 
5367
            else
 
5368
              tmp= record_count * min(tmp,s->worst_seeks);
 
5369
          }
 
5370
          else
 
5371
            tmp= best_time;                    // Do nothing
 
5372
        }
 
5373
 
 
5374
        if (sj_inside_out_scan && !start_key)
 
5375
        {
 
5376
          tmp= tmp/2;
 
5377
          if (records)
 
5378
            records= records/2;
 
5379
        }
 
5380
 
 
5381
      }
 
5382
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5383
      {
 
5384
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5385
        best= tmp;
 
5386
        best_records= records;
 
5387
        best_key= start_key;
 
5388
        best_max_key_part= max_key_part;
 
5389
        best_ref_depends_map= found_ref;
 
5390
        best_is_sj_inside_out= sj_inside_out_scan;
 
5391
      }
 
5392
    }
 
5393
    records= best_records;
 
5394
  }
 
5395
 
 
5396
  /*
 
5397
    Don't test table scan if it can't be better.
 
5398
    Prefer key lookup if we would use the same key for scanning.
 
5399
 
 
5400
    Don't do a table scan on InnoDB tables, if we can read the used
 
5401
    parts of the row from any of the used index.
 
5402
    This is because table scans uses index and we would not win
 
5403
    anything by using a table scan.
 
5404
 
 
5405
    A word for word translation of the below if-statement in sergefp's
 
5406
    understanding: we check if we should use table scan if:
 
5407
    (1) The found 'ref' access produces more records than a table scan
 
5408
        (or index scan, or quick select), or 'ref' is more expensive than
 
5409
        any of them.
 
5410
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5411
        'range' access using index IDX, and the best way to perform 'ref' 
 
5412
        access is to use the same index IDX, with the same or more key parts.
 
5413
        (note: it is not clear how this rule is/should be extended to 
 
5414
        index_merge quick selects)
 
5415
    (3) See above note about InnoDB.
 
5416
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5417
             path, but there is no quick select)
 
5418
        If the condition in the above brackets holds, then the only possible
 
5419
        "table scan" access method is ALL/index (there is no quick select).
 
5420
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5421
        choose it over ALL/index, there is no need to consider a full table
 
5422
        scan.
 
5423
  */
 
5424
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5425
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5426
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5427
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5428
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5429
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5430
  {                                             // Check full join
 
5431
    ha_rows rnd_records= s->found_records;
 
5432
    /*
 
5433
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5434
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5435
      preceding this table in the join order we're now considering), then 
 
5436
      assume that 25% of the rows will be filtered out by this condition.
 
5437
 
 
5438
      This heuristic is supposed to force tables used in exprZ to be before
 
5439
      this table in join order.
 
5440
    */
 
5441
    if (found_constraint)
 
5442
      rnd_records-= rnd_records/4;
 
5443
 
 
5444
    /*
 
5445
      If applicable, get a more accurate estimate. Don't use the two
 
5446
      heuristics at once.
 
5447
    */
 
5448
    if (s->table->quick_condition_rows != s->found_records)
 
5449
      rnd_records= s->table->quick_condition_rows;
 
5450
 
 
5451
    /*
 
5452
      Range optimizer never proposes a RANGE if it isn't better
 
5453
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5454
      Here we estimate its cost.
 
5455
    */
 
5456
    if (s->quick)
 
5457
    {
 
5458
      /*
 
5459
        For each record we:
 
5460
        - read record range through 'quick'
 
5461
        - skip rows which does not satisfy WHERE constraints
 
5462
        TODO: 
 
5463
        We take into account possible use of join cache for ALL/index
 
5464
        access (see first else-branch below), but we don't take it into 
 
5465
        account here for range/index_merge access. Find out why this is so.
 
5466
      */
 
5467
      tmp= record_count *
 
5468
        (s->quick->read_time +
 
5469
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5470
    }
 
5471
    else
 
5472
    {
 
5473
      /* Estimate cost of reading table. */
 
5474
      tmp= s->table->file->scan_time();
 
5475
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5476
      {
 
5477
        /*
 
5478
          For each record we have to:
 
5479
          - read the whole table record 
 
5480
          - skip rows which does not satisfy join condition
 
5481
        */
 
5482
        tmp= record_count *
 
5483
          (tmp +
 
5484
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5485
      }
 
5486
      else
 
5487
      {
 
5488
        /* We read the table as many times as join buffer becomes full. */
 
5489
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5490
                           record_count /
 
5491
                           (double) thd->variables.join_buff_size));
 
5492
        /* 
 
5493
            We don't make full cartesian product between rows in the scanned
 
5494
           table and existing records because we skip all rows from the
 
5495
           scanned table, which does not satisfy join condition when 
 
5496
           we read the table (see flush_cached_records for details). Here we
 
5497
           take into account cost to read and skip these records.
 
5498
        */
 
5499
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5500
      }
 
5501
    }
 
5502
 
 
5503
    /*
 
5504
      We estimate the cost of evaluating WHERE clause for found records
 
5505
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5506
      tmp give us total cost of using TABLE SCAN
 
5507
    */
 
5508
    if (best == DBL_MAX ||
 
5509
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5510
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5511
    {
 
5512
      /*
 
5513
        If the table has a range (s->quick is set) make_join_select()
 
5514
        will ensure that this will be used
 
5515
      */
 
5516
      best= tmp;
 
5517
      records= rows2double(rnd_records);
 
5518
      best_key= 0;
 
5519
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5520
      best_ref_depends_map= 0;
 
5521
      best_is_sj_inside_out= false;
 
5522
    }
 
5523
  }
 
5524
 
 
5525
  /* Update the cost information for the current partial plan */
 
5526
  join->positions[idx].records_read= records;
 
5527
  join->positions[idx].read_time=    best;
 
5528
  join->positions[idx].key=          best_key;
 
5529
  join->positions[idx].table=        s;
 
5530
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5531
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5532
 
 
5533
  if (!best_key &&
 
5534
      idx == join->const_tables &&
 
5535
      s->table == join->sort_by_table &&
 
5536
      join->unit->select_limit_cnt >= records)
 
5537
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
 
5538
 
 
5539
  return;
 
5540
}
 
5541
 
 
5542
 
 
5543
/**
 
5544
  Selects and invokes a search strategy for an optimal query plan.
 
5545
 
 
5546
  The function checks user-configurable parameters that control the search
 
5547
  strategy for an optimal plan, selects the search method and then invokes
 
5548
  it. Each specific optimization procedure stores the final optimal plan in
 
5549
  the array 'join->best_positions', and the cost of the plan in
 
5550
  'join->best_read'.
 
5551
 
 
5552
  @param join         pointer to the structure providing all context info for
 
5553
                      the query
 
5554
  @param join_tables  set of the tables in the query
 
5555
 
 
5556
  @todo
 
5557
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5558
    the greedy version. Will be removed when greedy_search is approved.
 
5559
 
 
5560
  @retval
 
5561
    false       ok
 
5562
  @retval
 
5563
    true        Fatal error
 
5564
*/
 
5565
 
 
5566
static bool
 
5567
choose_plan(JOIN *join, table_map join_tables)
 
5568
{
 
5569
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5570
  uint prune_level=  join->thd->variables.optimizer_prune_level;
 
5571
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5572
 
 
5573
  join->cur_embedding_map= 0;
 
5574
  reset_nj_counters(join->join_list);
 
5575
  /*
 
5576
    if (SELECT_STRAIGHT_JOIN option is set)
 
5577
      reorder tables so dependent tables come after tables they depend 
 
5578
      on, otherwise keep tables in the order they were specified in the query 
 
5579
    else
 
5580
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5581
      records accessed.
 
5582
  */
 
5583
  my_qsort(join->best_ref + join->const_tables,
 
5584
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5585
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5586
  join->cur_emb_sj_nests= 0;
 
5587
  if (straight_join)
 
5588
  {
 
5589
    optimize_straight_join(join, join_tables);
 
5590
  }
 
5591
  else
 
5592
  {
 
5593
    if (search_depth == MAX_TABLES+2)
 
5594
    { /*
 
5595
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5596
        the greedy version. Will be removed when greedy_search is approved.
 
5597
      */
 
5598
      join->best_read= DBL_MAX;
 
5599
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5600
        return(true);
 
5601
    } 
 
5602
    else
 
5603
    {
 
5604
      if (search_depth == 0)
 
5605
        /* Automatically determine a reasonable value for 'search_depth' */
 
5606
        search_depth= determine_search_depth(join);
 
5607
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5608
        return(true);
 
5609
    }
 
5610
  }
 
5611
 
 
5612
  /* 
 
5613
    Store the cost of this query into a user variable
 
5614
    Don't update last_query_cost for statements that are not "flat joins" :
 
5615
    i.e. they have subqueries, unions or call stored procedures.
 
5616
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5617
  */
 
5618
  if (join->thd->lex->is_single_level_stmt())
 
5619
    join->thd->status_var.last_query_cost= join->best_read;
 
5620
  return(false);
 
5621
}
 
5622
 
 
5623
 
 
5624
/**
 
5625
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5626
 
 
5627
  @param ptr1 pointer to first JOIN_TAB object
 
5628
  @param ptr2 pointer to second JOIN_TAB object
793
5629
 
794
5630
  NOTES
795
5631
    The order relation implemented by join_tab_cmp() is not transitive,
801
5637
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
802
5638
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
803
5639
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
804
 
 
 
5640
     
805
5641
  @retval
806
5642
    1  if first is bigger
807
5643
  @retval
809
5645
  @retval
810
5646
    0  if equal
811
5647
*/
812
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5648
 
 
5649
static int
 
5650
join_tab_cmp(const void* ptr1, const void* ptr2)
813
5651
{
814
 
  JoinTable *jt1= *(JoinTable**) ptr1;
815
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5652
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5653
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
816
5654
 
817
5655
  if (jt1->dependent & jt2->table->map)
818
5656
    return 1;
819
5657
  if (jt2->dependent & jt1->table->map)
820
 
    return -1;
 
5658
    return -1;  
821
5659
  if (jt1->found_records > jt2->found_records)
822
5660
    return 1;
823
5661
  if (jt1->found_records < jt2->found_records)
824
 
    return -1;
 
5662
    return -1; 
825
5663
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
826
5664
}
827
5665
 
 
5666
 
828
5667
/**
829
5668
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
830
5669
*/
831
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5670
 
 
5671
static int
 
5672
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
832
5673
{
833
 
  JoinTable *jt1= *(JoinTable**) ptr1;
834
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5674
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5675
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
835
5676
 
836
5677
  if (jt1->dependent & jt2->table->map)
837
5678
    return 1;
841
5682
}
842
5683
 
843
5684
/**
 
5685
  Heuristic procedure to automatically guess a reasonable degree of
 
5686
  exhaustiveness for the greedy search procedure.
 
5687
 
 
5688
  The procedure estimates the optimization time and selects a search depth
 
5689
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5690
  find. If the number of tables in the query exceeds some constant, then
 
5691
  search_depth is set to this constant.
 
5692
 
 
5693
  @param join   pointer to the structure providing all context info for
 
5694
                the query
 
5695
 
 
5696
  @note
 
5697
    This is an extremely simplistic implementation that serves as a stub for a
 
5698
    more advanced analysis of the join. Ideally the search depth should be
 
5699
    determined by learning from previous query optimizations, because it will
 
5700
    depend on the CPU power (and other factors).
 
5701
 
 
5702
  @todo
 
5703
    this value should be determined dynamically, based on statistics:
 
5704
    uint max_tables_for_exhaustive_opt= 7;
 
5705
 
 
5706
  @todo
 
5707
    this value could be determined by some mapping of the form:
 
5708
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5709
 
 
5710
  @return
 
5711
    A positive integer that specifies the search depth (and thus the
 
5712
    exhaustiveness) of the depth-first search algorithm used by
 
5713
    'greedy_search'.
 
5714
*/
 
5715
 
 
5716
static uint
 
5717
determine_search_depth(JOIN *join)
 
5718
{
 
5719
  uint table_count=  join->tables - join->const_tables;
 
5720
  uint search_depth;
 
5721
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5722
  uint max_tables_for_exhaustive_opt= 7;
 
5723
 
 
5724
  if (table_count <= max_tables_for_exhaustive_opt)
 
5725
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5726
  else
 
5727
    /*
 
5728
      TODO: this value could be determined by some mapping of the form:
 
5729
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5730
    */
 
5731
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5732
 
 
5733
  return search_depth;
 
5734
}
 
5735
 
 
5736
 
 
5737
/**
 
5738
  Select the best ways to access the tables in a query without reordering them.
 
5739
 
 
5740
    Find the best access paths for each query table and compute their costs
 
5741
    according to their order in the array 'join->best_ref' (thus without
 
5742
    reordering the join tables). The function calls sequentially
 
5743
    'best_access_path' for each table in the query to select the best table
 
5744
    access method. The final optimal plan is stored in the array
 
5745
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5746
 
 
5747
  @param join          pointer to the structure providing all context info for
 
5748
                       the query
 
5749
  @param join_tables   set of the tables in the query
 
5750
 
 
5751
  @note
 
5752
    This function can be applied to:
 
5753
    - queries with STRAIGHT_JOIN
 
5754
    - internally to compute the cost of an arbitrary QEP
 
5755
  @par
 
5756
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5757
    optimization process to finalize a QEP as it is.
 
5758
*/
 
5759
 
 
5760
static void
 
5761
optimize_straight_join(JOIN *join, table_map join_tables)
 
5762
{
 
5763
  JOIN_TAB *s;
 
5764
  uint idx= join->const_tables;
 
5765
  double    record_count= 1.0;
 
5766
  double    read_time=    0.0;
 
5767
 
 
5768
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5769
  {
 
5770
    /* Find the best access method from 's' to the current partial plan */
 
5771
    advance_sj_state(join_tables, s);
 
5772
    best_access_path(join, s, join->thd, join_tables, idx,
 
5773
                     record_count, read_time);
 
5774
    /* compute the cost of the new plan extended with 's' */
 
5775
    record_count*= join->positions[idx].records_read;
 
5776
    read_time+=    join->positions[idx].read_time;
 
5777
    join_tables&= ~(s->table->map);
 
5778
    ++idx;
 
5779
  }
 
5780
 
 
5781
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5782
  if (join->sort_by_table &&
 
5783
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5784
    read_time+= record_count;  // We have to make a temp table
 
5785
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
5786
         sizeof(POSITION)*idx);
 
5787
  join->best_read= read_time;
 
5788
}
 
5789
 
 
5790
 
 
5791
/**
 
5792
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5793
 
 
5794
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5795
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5796
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5797
    selects the most promising table, and extends the current partial QEP with
 
5798
    that table.  Currenly the most 'promising' table is the one with least
 
5799
    expensive extension.\
 
5800
 
 
5801
    There are two extreme cases:
 
5802
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5803
    best complete continuation of the partial QEP. This continuation can be
 
5804
    used directly as a result of the search.
 
5805
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5806
    consideres the extension of the current QEP with each of the remaining
 
5807
    unoptimized tables.
 
5808
 
 
5809
    All other cases are in-between these two extremes. Thus the parameter
 
5810
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5811
    value, the longer the optimizaton time and possibly the better the
 
5812
    resulting plan. The lower the value, the fewer alternative plans are
 
5813
    estimated, but the more likely to get a bad QEP.
 
5814
 
 
5815
    All intermediate and final results of the procedure are stored in 'join':
 
5816
    - join->positions     : modified for every partial QEP that is explored
 
5817
    - join->best_positions: modified for the current best complete QEP
 
5818
    - join->best_read     : modified for the current best complete QEP
 
5819
    - join->best_ref      : might be partially reordered
 
5820
 
 
5821
    The final optimal plan is stored in 'join->best_positions', and its
 
5822
    corresponding cost in 'join->best_read'.
 
5823
 
 
5824
  @note
 
5825
    The following pseudocode describes the algorithm of 'greedy_search':
 
5826
 
 
5827
    @code
 
5828
    procedure greedy_search
 
5829
    input: remaining_tables
 
5830
    output: pplan;
 
5831
    {
 
5832
      pplan = <>;
 
5833
      do {
 
5834
        (t, a) = best_extension(pplan, remaining_tables);
 
5835
        pplan = concat(pplan, (t, a));
 
5836
        remaining_tables = remaining_tables - t;
 
5837
      } while (remaining_tables != {})
 
5838
      return pplan;
 
5839
    }
 
5840
 
 
5841
  @endcode
 
5842
    where 'best_extension' is a placeholder for a procedure that selects the
 
5843
    most "promising" of all tables in 'remaining_tables'.
 
5844
    Currently this estimate is performed by calling
 
5845
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5846
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5847
    mainly depends on that of 'best_extension_by_limited_search'.
 
5848
 
 
5849
  @par
 
5850
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5851
    worst-case complexity of this algorithm is <=
 
5852
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5853
    complexity of greedy_search is O(N!).
 
5854
 
 
5855
  @par
 
5856
    In the future, 'greedy_search' might be extended to support other
 
5857
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5858
 
 
5859
  @param join             pointer to the structure providing all context info
 
5860
                          for the query
 
5861
  @param remaining_tables set of tables not included into the partial plan yet
 
5862
  @param search_depth     controlls the exhaustiveness of the search
 
5863
  @param prune_level      the pruning heuristics that should be applied during
 
5864
                          search
 
5865
 
 
5866
  @retval
 
5867
    false       ok
 
5868
  @retval
 
5869
    true        Fatal error
 
5870
*/
 
5871
 
 
5872
static bool
 
5873
greedy_search(JOIN      *join,
 
5874
              table_map remaining_tables,
 
5875
              uint      search_depth,
 
5876
              uint      prune_level)
 
5877
{
 
5878
  double    record_count= 1.0;
 
5879
  double    read_time=    0.0;
 
5880
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5881
  uint      best_idx;
 
5882
  uint      size_remain;    // cardinality of remaining_tables
 
5883
  POSITION  best_pos;
 
5884
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5885
 
 
5886
  /* number of tables that remain to be optimized */
 
5887
  size_remain= my_count_bits(remaining_tables);
 
5888
 
 
5889
  do {
 
5890
    /* Find the extension of the current QEP with the lowest cost */
 
5891
    join->best_read= DBL_MAX;
 
5892
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5893
                                         read_time, search_depth, prune_level))
 
5894
      return(true);
 
5895
 
 
5896
    if (size_remain <= search_depth)
 
5897
    {
 
5898
      /*
 
5899
        'join->best_positions' contains a complete optimal extension of the
 
5900
        current partial QEP.
 
5901
      */
 
5902
      return(false);
 
5903
    }
 
5904
 
 
5905
    /* select the first table in the optimal extension as most promising */
 
5906
    best_pos= join->best_positions[idx];
 
5907
    best_table= best_pos.table;
 
5908
    /*
 
5909
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5910
      'join->positions' for cost estimates, therefore we have to update its
 
5911
      value.
 
5912
    */
 
5913
    join->positions[idx]= best_pos;
 
5914
 
 
5915
    /* find the position of 'best_table' in 'join->best_ref' */
 
5916
    best_idx= idx;
 
5917
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5918
    while (pos && best_table != pos)
 
5919
      pos= join->best_ref[++best_idx];
 
5920
    assert((pos != NULL)); // should always find 'best_table'
 
5921
    /* move 'best_table' at the first free position in the array of joins */
 
5922
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
 
5923
 
 
5924
    /* compute the cost of the new plan extended with 'best_table' */
 
5925
    record_count*= join->positions[idx].records_read;
 
5926
    read_time+=    join->positions[idx].read_time;
 
5927
 
 
5928
    remaining_tables&= ~(best_table->table->map);
 
5929
    --size_remain;
 
5930
    ++idx;
 
5931
  } while (true);
 
5932
}
 
5933
 
 
5934
 
 
5935
/**
 
5936
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5937
  exhaustive search.
 
5938
 
 
5939
    The procedure searches for the optimal ordering of the query tables in set
 
5940
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5941
    each table. The choice of a table order and an access path for each table
 
5942
    constitutes a query execution plan (QEP) that fully specifies how to
 
5943
    execute the query.
 
5944
   
 
5945
    The maximal size of the found plan is controlled by the parameter
 
5946
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5947
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5948
    of only some of the query tables. Such "partial" optimal plans are useful
 
5949
    only as input to query optimization procedures, and cannot be used directly
 
5950
    to execute a query.
 
5951
 
 
5952
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5953
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5954
    evaluates the cost of the partial plan extended by all access plans for
 
5955
    each of the relations in 'remaining_tables', expands the current partial
 
5956
    plan with the access plan that results in lowest cost of the expanded
 
5957
    partial plan, and removes the corresponding relation from
 
5958
    'remaining_tables'. The algorithm continues until it either constructs a
 
5959
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5960
    search_depth.
 
5961
 
 
5962
    The final optimal plan is stored in 'join->best_positions'. The
 
5963
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5964
 
 
5965
  @note
 
5966
    The procedure uses a recursive depth-first search where the depth of the
 
5967
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5968
    parameter 'search_depth'.
 
5969
 
 
5970
  @note
 
5971
    The pseudocode below describes the algorithm of
 
5972
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5973
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5974
    the complexity of greedy_search is O(N!).
 
5975
 
 
5976
    @code
 
5977
    procedure best_extension_by_limited_search(
 
5978
      pplan in,             // in, partial plan of tables-joined-so-far
 
5979
      pplan_cost,           // in, cost of pplan
 
5980
      remaining_tables,     // in, set of tables not referenced in pplan
 
5981
      best_plan_so_far,     // in/out, best plan found so far
 
5982
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5983
      search_depth)         // in, maximum size of the plans being considered
 
5984
    {
 
5985
      for each table T from remaining_tables
 
5986
      {
 
5987
        // Calculate the cost of using table T as above
 
5988
        cost = complex-series-of-calculations;
 
5989
 
 
5990
        // Add the cost to the cost so far.
 
5991
        pplan_cost+= cost;
 
5992
 
 
5993
        if (pplan_cost >= best_plan_so_far_cost)
 
5994
          // pplan_cost already too great, stop search
 
5995
          continue;
 
5996
 
 
5997
        pplan= expand pplan by best_access_method;
 
5998
        remaining_tables= remaining_tables - table T;
 
5999
        if (remaining_tables is not an empty set
 
6000
            and
 
6001
            search_depth > 1)
 
6002
        {
 
6003
          best_extension_by_limited_search(pplan, pplan_cost,
 
6004
                                           remaining_tables,
 
6005
                                           best_plan_so_far,
 
6006
                                           best_plan_so_far_cost,
 
6007
                                           search_depth - 1);
 
6008
        }
 
6009
        else
 
6010
        {
 
6011
          best_plan_so_far_cost= pplan_cost;
 
6012
          best_plan_so_far= pplan;
 
6013
        }
 
6014
      }
 
6015
    }
 
6016
    @endcode
 
6017
 
 
6018
  @note
 
6019
    When 'best_extension_by_limited_search' is called for the first time,
 
6020
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
6021
    The actual implementation provides a way to optionally use pruning
 
6022
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6023
    space by skipping some partial plans.
 
6024
 
 
6025
  @note
 
6026
    The parameter 'search_depth' provides control over the recursion
 
6027
    depth, and thus the size of the resulting optimal plan.
 
6028
 
 
6029
  @param join             pointer to the structure providing all context info
 
6030
                          for the query
 
6031
  @param remaining_tables set of tables not included into the partial plan yet
 
6032
  @param idx              length of the partial QEP in 'join->positions';
 
6033
                          since a depth-first search is used, also corresponds
 
6034
                          to the current depth of the search tree;
 
6035
                          also an index in the array 'join->best_ref';
 
6036
  @param record_count     estimate for the number of records returned by the
 
6037
                          best partial plan
 
6038
  @param read_time        the cost of the best partial plan
 
6039
  @param search_depth     maximum depth of the recursion and thus size of the
 
6040
                          found optimal plan
 
6041
                          (0 < search_depth <= join->tables+1).
 
6042
  @param prune_level      pruning heuristics that should be applied during
 
6043
                          optimization
 
6044
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6045
 
 
6046
  @retval
 
6047
    false       ok
 
6048
  @retval
 
6049
    true        Fatal error
 
6050
*/
 
6051
 
 
6052
static bool
 
6053
best_extension_by_limited_search(JOIN      *join,
 
6054
                                 table_map remaining_tables,
 
6055
                                 uint      idx,
 
6056
                                 double    record_count,
 
6057
                                 double    read_time,
 
6058
                                 uint      search_depth,
 
6059
                                 uint      prune_level)
 
6060
{
 
6061
  THD *thd= join->thd;
 
6062
  if (thd->killed)  // Abort
 
6063
    return(true);
 
6064
 
 
6065
  /* 
 
6066
     'join' is a partial plan with lower cost than the best plan so far,
 
6067
     so continue expanding it further with the tables in 'remaining_tables'.
 
6068
  */
 
6069
  JOIN_TAB *s;
 
6070
  double best_record_count= DBL_MAX;
 
6071
  double best_read_time=    DBL_MAX;
 
6072
 
 
6073
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6074
  {
 
6075
    table_map real_table_bit= s->table->map;
 
6076
    if ((remaining_tables & real_table_bit) && 
 
6077
        !(remaining_tables & s->dependent) && 
 
6078
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6079
    {
 
6080
      double current_record_count, current_read_time;
 
6081
      advance_sj_state(remaining_tables, s);
 
6082
 
 
6083
      /*
 
6084
        psergey-insideout-todo: 
 
6085
          when best_access_path() detects it could do an InsideOut scan or 
 
6086
          some other scan, have it return an insideout scan and a flag that 
 
6087
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6088
          when the depth is insufficient??)
 
6089
      */
 
6090
      /* Find the best access method from 's' to the current partial plan */
 
6091
      best_access_path(join, s, thd, remaining_tables, idx,
 
6092
                       record_count, read_time);
 
6093
      /* Compute the cost of extending the plan with 's' */
 
6094
      current_record_count= record_count * join->positions[idx].records_read;
 
6095
      current_read_time=    read_time + join->positions[idx].read_time;
 
6096
 
 
6097
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6098
      if ((current_read_time +
 
6099
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6100
      {
 
6101
        restore_prev_nj_state(s);
 
6102
        restore_prev_sj_state(remaining_tables, s);
 
6103
        continue;
 
6104
      }
 
6105
 
 
6106
      /*
 
6107
        Prune some less promising partial plans. This heuristic may miss
 
6108
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6109
      */
 
6110
      if (prune_level == 1)
 
6111
      {
 
6112
        if (best_record_count > current_record_count ||
 
6113
            best_read_time > current_read_time ||
 
6114
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6115
        {
 
6116
          if (best_record_count >= current_record_count &&
 
6117
              best_read_time >= current_read_time &&
 
6118
              /* TODO: What is the reasoning behind this condition? */
 
6119
              (!(s->key_dependent & remaining_tables) ||
 
6120
               join->positions[idx].records_read < 2.0))
 
6121
          {
 
6122
            best_record_count= current_record_count;
 
6123
            best_read_time=    current_read_time;
 
6124
          }
 
6125
        }
 
6126
        else
 
6127
        {
 
6128
          restore_prev_nj_state(s);
 
6129
          restore_prev_sj_state(remaining_tables, s);
 
6130
          continue;
 
6131
        }
 
6132
      }
 
6133
 
 
6134
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6135
      { /* Recursively expand the current partial plan */
 
6136
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6137
        if (best_extension_by_limited_search(join,
 
6138
                                             remaining_tables & ~real_table_bit,
 
6139
                                             idx + 1,
 
6140
                                             current_record_count,
 
6141
                                             current_read_time,
 
6142
                                             search_depth - 1,
 
6143
                                             prune_level))
 
6144
          return(true);
 
6145
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6146
      }
 
6147
      else
 
6148
      { /*
 
6149
          'join' is either the best partial QEP with 'search_depth' relations,
 
6150
          or the best complete QEP so far, whichever is smaller.
 
6151
        */
 
6152
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6153
        if (join->sort_by_table &&
 
6154
            join->sort_by_table !=
 
6155
            join->positions[join->const_tables].table->table)
 
6156
          /* We have to make a temp table */
 
6157
          current_read_time+= current_record_count;
 
6158
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6159
        {
 
6160
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
 
6161
                 sizeof(POSITION) * (idx + 1));
 
6162
          join->best_read= current_read_time - 0.001;
 
6163
        }
 
6164
      }
 
6165
      restore_prev_nj_state(s);
 
6166
      restore_prev_sj_state(remaining_tables, s);
 
6167
    }
 
6168
  }
 
6169
  return(false);
 
6170
}
 
6171
 
 
6172
 
 
6173
/**
 
6174
  @todo
 
6175
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6176
  tested and accepted.
 
6177
 
 
6178
  RETURN VALUES
 
6179
    false       ok
 
6180
    true        Fatal error
 
6181
*/
 
6182
static bool
 
6183
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
 
6184
          double read_time)
 
6185
{
 
6186
  THD *thd= join->thd;
 
6187
  if (thd->killed)
 
6188
    return(true);
 
6189
  if (!rest_tables)
 
6190
  {
 
6191
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6192
    if (join->sort_by_table &&
 
6193
        join->sort_by_table !=
 
6194
        join->positions[join->const_tables].table->table)
 
6195
      read_time+=record_count;                  // We have to make a temp table
 
6196
    if (read_time < join->best_read)
 
6197
    {
 
6198
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
 
6199
             sizeof(POSITION)*idx);
 
6200
      join->best_read= read_time - 0.001;
 
6201
    }
 
6202
    return(false);
 
6203
  }
 
6204
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6205
    return(false);                                      /* Found better before */
 
6206
 
 
6207
  JOIN_TAB *s;
 
6208
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6209
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6210
  {
 
6211
    table_map real_table_bit=s->table->map;
 
6212
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6213
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6214
    {
 
6215
      double records, best;
 
6216
      advance_sj_state(rest_tables, s);
 
6217
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6218
                       read_time);
 
6219
      records= join->positions[idx].records_read;
 
6220
      best= join->positions[idx].read_time;
 
6221
      /*
 
6222
        Go to the next level only if there hasn't been a better key on
 
6223
        this level! This will cut down the search for a lot simple cases!
 
6224
      */
 
6225
      double current_record_count=record_count*records;
 
6226
      double current_read_time=read_time+best;
 
6227
      if (best_record_count > current_record_count ||
 
6228
          best_read_time > current_read_time ||
 
6229
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6230
      {
 
6231
        if (best_record_count >= current_record_count &&
 
6232
            best_read_time >= current_read_time &&
 
6233
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6234
        {
 
6235
          best_record_count=current_record_count;
 
6236
          best_read_time=current_read_time;
 
6237
        }
 
6238
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6239
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6240
                      current_record_count,current_read_time))
 
6241
          return(true);
 
6242
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6243
      }
 
6244
      restore_prev_nj_state(s);
 
6245
      restore_prev_sj_state(rest_tables, s);
 
6246
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6247
        break;                          // Don't test all combinations
 
6248
    }
 
6249
  }
 
6250
  return(false);
 
6251
}
 
6252
 
 
6253
 
 
6254
/**
844
6255
  Find how much space the prevous read not const tables takes in cache.
845
6256
*/
846
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6257
 
 
6258
static void calc_used_field_length(THD *thd __attribute__((__unused__)),
 
6259
                                   JOIN_TAB *join_tab)
847
6260
{
848
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6261
  uint null_fields,blobs,fields,rec_length;
849
6262
  Field **f_ptr,*field;
 
6263
  MY_BITMAP *read_set= join_tab->table->read_set;;
850
6264
 
851
6265
  null_fields= blobs= fields= rec_length=0;
852
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6266
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
853
6267
  {
854
 
    if (field->isReadSet())
 
6268
    if (bitmap_is_set(read_set, field->field_index))
855
6269
    {
856
 
      uint32_t flags=field->flags;
 
6270
      uint flags=field->flags;
857
6271
      fields++;
858
6272
      rec_length+=field->pack_length();
859
6273
      if (flags & BLOB_FLAG)
860
 
        blobs++;
 
6274
        blobs++;
861
6275
      if (!(flags & NOT_NULL_FLAG))
862
 
        null_fields++;
 
6276
        null_fields++;
863
6277
    }
864
6278
  }
865
6279
  if (null_fields)
866
 
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
 
6280
    rec_length+=(join_tab->table->s->null_fields+7)/8;
867
6281
  if (join_tab->table->maybe_null)
868
 
    rec_length+=sizeof(bool);
 
6282
    rec_length+=sizeof(my_bool);
869
6283
  if (blobs)
870
6284
  {
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
 
}
 
6285
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6286
                             (join_tab->table->s->reclength- rec_length));
 
6287
    rec_length+=(uint) max(4,blob_length);
 
6288
  }
 
6289
  join_tab->used_fields=fields;
 
6290
  join_tab->used_fieldlength=rec_length;
 
6291
  join_tab->used_blobs=blobs;
 
6292
}
 
6293
 
 
6294
 
 
6295
static uint
 
6296
cache_record_length(JOIN *join,uint idx)
 
6297
{
 
6298
  uint length=0;
 
6299
  JOIN_TAB **pos,**end;
 
6300
  THD *thd=join->thd;
 
6301
 
 
6302
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6303
       pos != end ;
 
6304
       pos++)
 
6305
  {
 
6306
    JOIN_TAB *join_tab= *pos;
 
6307
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6308
      calc_used_field_length(thd, join_tab);
 
6309
    length+=join_tab->used_fieldlength;
 
6310
  }
 
6311
  return length;
 
6312
}
 
6313
 
 
6314
 
 
6315
/*
 
6316
  Get the number of different row combinations for subset of partial join
 
6317
 
 
6318
  SYNOPSIS
 
6319
    prev_record_reads()
 
6320
      join       The join structure
 
6321
      idx        Number of tables in the partial join order (i.e. the
 
6322
                 partial join order is in join->positions[0..idx-1])
 
6323
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6324
                 row combinations.
 
6325
 
 
6326
  DESCRIPTION
 
6327
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6328
    tables within that join order (specified in found_ref), find out how many
 
6329
    distinct row combinations of subset tables will be in the result of the
 
6330
    partial join order.
 
6331
     
 
6332
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6333
    method. The ref access depends on current rows of tables in found_ref.
 
6334
    We want to count # of different ref accesses. We assume two ref accesses
 
6335
    will be different if at least one of access parameters is different.
 
6336
    Example: consider a query
 
6337
 
 
6338
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6339
 
 
6340
    and a join order:
 
6341
      t1,  ref access on t1.key=c1
 
6342
      t2,  ref access on t2.key=c2       
 
6343
      t3,  ref access on t3.key=t1.field 
 
6344
    
 
6345
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6346
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6347
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6348
            n_distinct_ref_scans = #records_read(t1)
 
6349
    
 
6350
    The reason for having this function (at least the latest version of it)
 
6351
    is that we need to account for buffering in join execution. 
 
6352
    
 
6353
    An edge-case example: if we have a non-first table in join accessed via
 
6354
    ref(const) or ref(param) where there is a small number of different
 
6355
    values of param, then the access will likely hit the disk cache and will
 
6356
    not require any disk seeks.
 
6357
    
 
6358
    The proper solution would be to assume an LRU disk cache of some size,
 
6359
    calculate probability of cache hits, etc. For now we just count
 
6360
    identical ref accesses as one.
 
6361
 
 
6362
  RETURN 
 
6363
    Expected number of row combinations
 
6364
*/
 
6365
 
 
6366
static double
 
6367
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
 
6368
{
 
6369
  double found=1.0;
 
6370
  POSITION *pos_end= join->positions - 1;
 
6371
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6372
  {
 
6373
    if (pos->table->table->map & found_ref)
 
6374
    {
 
6375
      found_ref|= pos->ref_depend_map;
 
6376
      /* 
 
6377
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6378
        with no matching row we will get position[t2].records_read==0. 
 
6379
        Actually the size of output is one null-complemented row, therefore 
 
6380
        we will use value of 1 whenever we get records_read==0.
 
6381
 
 
6382
        Note
 
6383
        - the above case can't occur if inner part of outer join has more 
 
6384
          than one table: table with no matches will not be marked as const.
 
6385
 
 
6386
        - Ideally we should add 1 to records_read for every possible null-
 
6387
          complemented row. We're not doing it because: 1. it will require
 
6388
          non-trivial code and add overhead. 2. The value of records_read
 
6389
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6390
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6391
      */
 
6392
      if (pos->records_read > DBL_EPSILON)
 
6393
        found*= pos->records_read;
 
6394
    }
 
6395
  }
 
6396
  return found;
 
6397
}
 
6398
 
918
6399
 
919
6400
/**
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.
 
6401
  Set up join struct according to best position.
924
6402
*/
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)
 
6403
 
 
6404
static bool
 
6405
get_best_combination(JOIN *join)
 
6406
{
 
6407
  uint i,tablenr;
 
6408
  table_map used_tables;
 
6409
  JOIN_TAB *join_tab,*j;
 
6410
  KEYUSE *keyuse;
 
6411
  uint table_count;
 
6412
  THD *thd=join->thd;
 
6413
 
 
6414
  table_count=join->tables;
 
6415
  if (!(join->join_tab=join_tab=
 
6416
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6417
    return(true);
 
6418
 
 
6419
  join->full_join=0;
 
6420
 
 
6421
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6422
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
947
6423
  {
948
 
    Item *res;
949
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6424
    TABLE *form;
 
6425
    *j= *join->best_positions[tablenr].table;
 
6426
    form=join->table[tablenr]=j->table;
 
6427
    used_tables|= form->map;
 
6428
    form->reginfo.join_tab=j;
 
6429
    if (!*j->on_expr_ref)
 
6430
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6431
    if (j->type == JT_CONST)
 
6432
      continue;                                 // Handled in make_join_stat..
 
6433
 
 
6434
    j->ref.key = -1;
 
6435
    j->ref.key_parts=0;
 
6436
 
 
6437
    if (j->type == JT_SYSTEM)
 
6438
      continue;
 
6439
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
950
6440
    {
951
 
      *e1= res;
952
 
      res->quick_fix_field();
 
6441
      j->type=JT_ALL;
 
6442
      if (tablenr != join->const_tables)
 
6443
        join->full_join=1;
953
6444
    }
 
6445
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6446
      return(true);                        // Something went wrong
954
6447
  }
955
 
  else
956
 
    *e1= e2;
 
6448
 
 
6449
  for (i=0 ; i < table_count ; i++)
 
6450
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6451
  update_depend_map(join);
 
6452
  return(0);
957
6453
}
958
6454
 
959
 
bool create_ref_for_key(Join *join, 
960
 
                        JoinTable *j, 
961
 
                        optimizer::KeyUse *org_keyuse,
962
 
                        table_map used_tables)
 
6455
 
 
6456
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6457
                               table_map used_tables)
963
6458
{
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;
 
6459
  KEYUSE *keyuse=org_keyuse;
 
6460
  THD  *thd= join->thd;
 
6461
  uint keyparts,length,key;
 
6462
  TABLE *table;
 
6463
  KEY *keyinfo;
971
6464
 
972
6465
  /*  Use best key from find_best */
973
 
  table= j->table;
974
 
  key= keyuse->getKey();
975
 
  keyinfo= table->key_info + key;
 
6466
  table=j->table;
 
6467
  key=keyuse->key;
 
6468
  keyinfo=table->key_info+key;
976
6469
 
977
6470
  {
978
 
    keyparts= length= 0;
979
 
    uint32_t found_part_ref_or_null= 0;
 
6471
    keyparts=length=0;
 
6472
    uint found_part_ref_or_null= 0;
980
6473
    /*
981
6474
      Calculate length for the used key
982
6475
      Stop if there is a missing key part or when we find second key_part
984
6477
    */
985
6478
    do
986
6479
    {
987
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6480
      if (!(~used_tables & keyuse->used_tables))
988
6481
      {
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
 
        }
 
6482
        if (keyparts == keyuse->keypart &&
 
6483
            !(found_part_ref_or_null & keyuse->optimize))
 
6484
        {
 
6485
          keyparts++;
 
6486
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6487
          found_part_ref_or_null|= keyuse->optimize;
 
6488
        }
996
6489
      }
997
6490
      keyuse++;
998
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6491
    } while (keyuse->table == table && keyuse->key == key);
999
6492
  }
1000
6493
 
1001
6494
  /* set up fieldref */
1003
6496
  j->ref.key_parts=keyparts;
1004
6497
  j->ref.key_length=length;
1005
6498
  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)))
 
6499
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6500
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6501
                                                   (keyparts+1)))) ||
 
6502
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6503
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1011
6504
  {
1012
6505
    return(true);
1013
6506
  }
1017
6510
  j->ref.disable_cache= false;
1018
6511
  keyuse=org_keyuse;
1019
6512
 
1020
 
  StoredKey **ref_key= j->ref.key_copy;
1021
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6513
  store_key **ref_key= j->ref.key_copy;
 
6514
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
1022
6515
  bool keyuse_uses_no_tables= true;
1023
6516
  {
1024
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6517
    uint i;
 
6518
    for (i=0 ; i < keyparts ; keyuse++,i++)
1025
6519
    {
1026
 
      while (keyuse->getKeypart() != i ||
1027
 
             ((~used_tables) & keyuse->getUsedTables()))
1028
 
        keyuse++;       /* Skip other parts */
 
6520
      while (keyuse->keypart != i ||
 
6521
             ((~used_tables) & keyuse->used_tables))
 
6522
        keyuse++;                               /* Skip other parts */
1029
6523
 
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())
 
6524
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
 
6525
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6526
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6527
      if (keyuse->null_rejecting) 
1034
6528
        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,
 
6529
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6530
      if (!keyuse->used_tables &&
 
6531
          !(join->select_options & SELECT_DESCRIBE))
 
6532
      {                                 // Compare against constant
 
6533
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1039
6534
                           key_buff + maybe_null,
1040
6535
                           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();
 
6536
                           keyinfo->key_part[i].length, keyuse->val);
 
6537
        if (thd->is_fatal_error)
 
6538
          return(true);
 
6539
        tmp.copy();
1045
6540
      }
1046
6541
      else
1047
 
        *ref_key++= get_store_key(session,
1048
 
          keyuse,join->const_table_map,
1049
 
          &keyinfo->key_part[i],
1050
 
          key_buff, maybe_null);
 
6542
        *ref_key++= get_store_key(thd,
 
6543
                                  keyuse,join->const_table_map,
 
6544
                                  &keyinfo->key_part[i],
 
6545
                                  key_buff, maybe_null);
1051
6546
      /*
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
 
6547
        Remember if we are going to use REF_OR_NULL
 
6548
        But only if field _really_ can be null i.e. we force JT_REF
 
6549
        instead of JT_REF_OR_NULL in case if field can't be null
1055
6550
      */
1056
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1057
 
        null_ref_key= key_buff;
 
6551
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6552
        null_ref_key= key_buff;
1058
6553
      key_buff+=keyinfo->key_part[i].store_length;
1059
6554
    }
1060
6555
  }
1061
 
  *ref_key= 0;       // end_marker
1062
 
  if (j->type == AM_CONST)
 
6556
  *ref_key=0;                           // end_marker
 
6557
  if (j->type == JT_CONST)
1063
6558
    j->table->const_table= 1;
1064
6559
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1065
6560
           keyparts != keyinfo->key_parts || null_ref_key)
1066
6561
  {
1067
6562
    /* Must read with repeat */
1068
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6563
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1069
6564
    j->ref.null_ref_key= null_ref_key;
1070
6565
  }
1071
6566
  else if (keyuse_uses_no_tables)
1077
6572
      Here we should not mark the table as a 'const' as a field may
1078
6573
      have a 'normal' value or a NULL value.
1079
6574
    */
1080
 
    j->type= AM_CONST;
1081
 
  }
1082
 
  else
1083
 
    j->type= AM_EQ_REF;
1084
 
  return 0;
1085
 
}
 
6575
    j->type=JT_CONST;
 
6576
  }
 
6577
  else
 
6578
    j->type=JT_EQ_REF;
 
6579
  return(0);
 
6580
}
 
6581
 
 
6582
 
 
6583
 
 
6584
static store_key *
 
6585
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6586
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
 
6587
{
 
6588
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6589
  {
 
6590
    return new store_key_const_item(thd,
 
6591
                                    key_part->field,
 
6592
                                    key_buff + maybe_null,
 
6593
                                    maybe_null ? key_buff : 0,
 
6594
                                    key_part->length,
 
6595
                                    keyuse->val);
 
6596
  }
 
6597
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6598
           (keyuse->val->type() == Item::REF_ITEM &&
 
6599
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6600
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6601
             Item_ref::DIRECT_REF && 
 
6602
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6603
    return new store_key_field(thd,
 
6604
                               key_part->field,
 
6605
                               key_buff + maybe_null,
 
6606
                               maybe_null ? key_buff : 0,
 
6607
                               key_part->length,
 
6608
                               ((Item_field*) keyuse->val->real_item())->field,
 
6609
                               keyuse->val->full_name());
 
6610
  return new store_key_item(thd,
 
6611
                            key_part->field,
 
6612
                            key_buff + maybe_null,
 
6613
                            maybe_null ? key_buff : 0,
 
6614
                            key_part->length,
 
6615
                            keyuse->val);
 
6616
}
 
6617
 
 
6618
/**
 
6619
  This function is only called for const items on fields which are keys.
 
6620
 
 
6621
  @return
 
6622
    returns 1 if there was some conversion made when the field was stored.
 
6623
*/
 
6624
 
 
6625
bool
 
6626
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6627
{
 
6628
  bool error;
 
6629
  TABLE *table= field->table;
 
6630
  THD *thd= table->in_use;
 
6631
  ha_rows cuted_fields=thd->cuted_fields;
 
6632
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6633
                                                   table->write_set);
 
6634
 
 
6635
  /*
 
6636
    we should restore old value of count_cuted_fields because
 
6637
    store_val_in_field can be called from mysql_insert 
 
6638
    with select_insert, which make count_cuted_fields= 1
 
6639
   */
 
6640
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6641
  thd->count_cuted_fields= check_flag;
 
6642
  error= item->save_in_field(field, 1);
 
6643
  thd->count_cuted_fields= old_count_cuted_fields;
 
6644
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
6645
  return error || cuted_fields != thd->cuted_fields;
 
6646
}
 
6647
 
 
6648
 
 
6649
static bool
 
6650
make_simple_join(JOIN *join,TABLE *tmp_table)
 
6651
{
 
6652
  TABLE **tableptr;
 
6653
  JOIN_TAB *join_tab;
 
6654
 
 
6655
  /*
 
6656
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
 
6657
    to this function through JOIN::exec (may happen for sub-queries).
 
6658
  */
 
6659
  if (!join->table_reexec)
 
6660
  {
 
6661
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
 
6662
      return(true);                        /* purecov: inspected */
 
6663
    if (join->tmp_join)
 
6664
      join->tmp_join->table_reexec= join->table_reexec;
 
6665
  }
 
6666
  if (!join->join_tab_reexec)
 
6667
  {
 
6668
    if (!(join->join_tab_reexec=
 
6669
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6670
      return(true);                        /* purecov: inspected */
 
6671
    if (join->tmp_join)
 
6672
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6673
  }
 
6674
  tableptr= join->table_reexec;
 
6675
  join_tab= join->join_tab_reexec;
 
6676
 
 
6677
  join->join_tab=join_tab;
 
6678
  join->table=tableptr; tableptr[0]=tmp_table;
 
6679
  join->tables=1;
 
6680
  join->const_tables=0;
 
6681
  join->const_table_map=0;
 
6682
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6683
    join->tmp_table_param.func_count=0;
 
6684
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6685
  join->first_record=join->sort_and_group=0;
 
6686
  join->send_records=(ha_rows) 0;
 
6687
  join->group=0;
 
6688
  join->row_limit=join->unit->select_limit_cnt;
 
6689
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6690
 
 
6691
  join_tab->cache.buff=0;                       /* No caching */
 
6692
  join_tab->table=tmp_table;
 
6693
  join_tab->select=0;
 
6694
  join_tab->select_cond=0;
 
6695
  join_tab->quick=0;
 
6696
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6697
  join_tab->keys.init();
 
6698
  join_tab->keys.set_all();                     /* test everything in quick */
 
6699
  join_tab->info=0;
 
6700
  join_tab->on_expr_ref=0;
 
6701
  join_tab->last_inner= 0;
 
6702
  join_tab->first_unmatched= 0;
 
6703
  join_tab->ref.key = -1;
 
6704
  join_tab->not_used_in_distinct=0;
 
6705
  join_tab->read_first_record= join_init_read_record;
 
6706
  join_tab->join=join;
 
6707
  join_tab->ref.key_parts= 0;
 
6708
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6709
  join_tab->do_firstmatch= NULL;
 
6710
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
 
6711
  tmp_table->status=0;
 
6712
  tmp_table->null_row=0;
 
6713
  return(false);
 
6714
}
 
6715
 
 
6716
 
 
6717
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6718
{
 
6719
  if (*e1)
 
6720
  {
 
6721
    Item *res;
 
6722
    if ((res= new Item_cond_and(*e1, e2)))
 
6723
    {
 
6724
      *e1= res;
 
6725
      res->quick_fix_field();
 
6726
    }
 
6727
  }
 
6728
  else
 
6729
    *e1= e2;
 
6730
}
 
6731
 
1086
6732
 
1087
6733
/**
1088
6734
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1097
6743
    add "t1.field IS NOT NULL" to t1's table condition. @n
1098
6744
 
1099
6745
    Description of the optimization:
1100
 
 
 
6746
    
1101
6747
      We look through equalities choosen to perform ref/eq_ref access,
1102
6748
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1103
6749
      (where othertbl is a non-const table and othertbl.field may be NULL)
1125
6771
      This optimization doesn't affect the choices that ref, range, or join
1126
6772
      optimizer make. This was intentional because this was added after 4.1
1127
6773
      was GA.
1128
 
 
 
6774
      
1129
6775
    Implementation overview
1130
6776
      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.
 
6777
         predicates in in KEY_FIELD::null_rejecting
 
6778
      1.1 add_key_part saves these to KEYUSE.
 
6779
      2. create_ref_for_key copies them to TABLE_REF.
1134
6780
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1135
 
         appropiate JoinTable members.
 
6781
         appropiate JOIN_TAB members.
1136
6782
*/
1137
 
void add_not_null_conds(Join *join)
 
6783
 
 
6784
static void add_not_null_conds(JOIN *join)
1138
6785
{
1139
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6786
  for (uint i=join->const_tables ; i < join->tables ; i++)
1140
6787
  {
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) &&
 
6788
    JOIN_TAB *tab=join->join_tab+i;
 
6789
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6790
         tab->type == JT_REF_OR_NULL) &&
1144
6791
        !tab->table->maybe_null)
1145
6792
    {
1146
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6793
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
1147
6794
      {
1148
6795
        if (tab->ref.null_rejecting & (1 << keypart))
1149
6796
        {
1151
6798
          Item *notnull;
1152
6799
          assert(item->type() == Item::FIELD_ITEM);
1153
6800
          Item_field *not_null_item= (Item_field*)item;
1154
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6801
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1155
6802
          /*
1156
6803
            For UPDATE queries such as:
1157
6804
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1163
6810
            return;
1164
6811
          /*
1165
6812
            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
 
6813
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6814
            when it is called from make_join_select after this function is 
1168
6815
            called.
1169
6816
          */
1170
 
          if (notnull->fix_fields(join->session, &notnull))
 
6817
          if (notnull->fix_fields(join->thd, &notnull))
1171
6818
            return;
1172
6819
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1173
6820
        }
1192
6839
    -  pointer to the guarded predicate, if success
1193
6840
    -  0, otherwise
1194
6841
*/
1195
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6842
 
 
6843
static COND*
 
6844
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1196
6845
{
1197
6846
  COND *tmp;
1198
6847
  assert(cond != 0);
1208
6857
  return tmp;
1209
6858
}
1210
6859
 
 
6860
 
 
6861
/**
 
6862
  Fill in outer join related info for the execution plan structure.
 
6863
 
 
6864
    For each outer join operation left after simplification of the
 
6865
    original query the function set up the following pointers in the linear
 
6866
    structure join->join_tab representing the selected execution plan.
 
6867
    The first inner table t0 for the operation is set to refer to the last
 
6868
    inner table tk through the field t0->last_inner.
 
6869
    Any inner table ti for the operation are set to refer to the first
 
6870
    inner table ti->first_inner.
 
6871
    The first inner table t0 for the operation is set to refer to the
 
6872
    first inner table of the embedding outer join operation, if there is any,
 
6873
    through the field t0->first_upper.
 
6874
    The on expression for the outer join operation is attached to the
 
6875
    corresponding first inner table through the field t0->on_expr_ref.
 
6876
    Here ti are structures of the JOIN_TAB type.
 
6877
 
 
6878
  EXAMPLE. For the query: 
 
6879
  @code
 
6880
        SELECT * FROM t1
 
6881
                      LEFT JOIN
 
6882
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6883
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6884
          WHERE t1.c > 5,
 
6885
  @endcode
 
6886
 
 
6887
    given the execution plan with the table order t1,t2,t3,t4
 
6888
    is selected, the following references will be set;
 
6889
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6890
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6891
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6892
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6893
 
 
6894
  @param join   reference to the info fully describing the query
 
6895
 
 
6896
  @note
 
6897
    The function assumes that the simplification procedure has been
 
6898
    already applied to the join query (see simplify_joins).
 
6899
    This function can be called only after the execution plan
 
6900
    has been chosen.
 
6901
*/
 
6902
 
 
6903
static void
 
6904
make_outerjoin_info(JOIN *join)
 
6905
{
 
6906
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
6907
  {
 
6908
    JOIN_TAB *tab=join->join_tab+i;
 
6909
    TABLE *table=tab->table;
 
6910
    TABLE_LIST *tbl= table->pos_in_table_list;
 
6911
    TABLE_LIST *embedding= tbl->embedding;
 
6912
 
 
6913
    if (tbl->outer_join)
 
6914
    {
 
6915
      /* 
 
6916
        Table tab is the only one inner table for outer join.
 
6917
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6918
        is in the query above.)
 
6919
      */
 
6920
      tab->last_inner= tab->first_inner= tab;
 
6921
      tab->on_expr_ref= &tbl->on_expr;
 
6922
      tab->cond_equal= tbl->cond_equal;
 
6923
      if (embedding)
 
6924
        tab->first_upper= embedding->nested_join->first_nested;
 
6925
    }    
 
6926
    for ( ; embedding ; embedding= embedding->embedding)
 
6927
    {
 
6928
      /* Ignore sj-nests: */
 
6929
      if (!embedding->on_expr)
 
6930
        continue;
 
6931
      NESTED_JOIN *nested_join= embedding->nested_join;
 
6932
      if (!nested_join->counter_)
 
6933
      {
 
6934
        /* 
 
6935
          Table tab is the first inner table for nested_join.
 
6936
          Save reference to it in the nested join structure.
 
6937
        */ 
 
6938
        nested_join->first_nested= tab;
 
6939
        tab->on_expr_ref= &embedding->on_expr;
 
6940
        tab->cond_equal= tbl->cond_equal;
 
6941
        if (embedding->embedding)
 
6942
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6943
      }
 
6944
      if (!tab->first_inner)  
 
6945
        tab->first_inner= nested_join->first_nested;
 
6946
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6947
        break;
 
6948
      /* Table tab is the last inner table for nested join. */
 
6949
      nested_join->first_nested->last_inner= tab;
 
6950
    }
 
6951
  }
 
6952
  return;
 
6953
}
 
6954
 
 
6955
 
 
6956
static bool
 
6957
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6958
{
 
6959
  THD *thd= join->thd;
 
6960
  if (select)
 
6961
  {
 
6962
    add_not_null_conds(join);
 
6963
    table_map used_tables;
 
6964
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6965
    {                        /* there may be a select without a cond. */    
 
6966
      if (join->tables > 1)
 
6967
        cond->update_used_tables();             // Tablenr may have changed
 
6968
      if (join->const_tables == join->tables &&
 
6969
          thd->lex->current_select->master_unit() ==
 
6970
          &thd->lex->unit)              // not upper level SELECT
 
6971
        join->const_table_map|=RAND_TABLE_BIT;
 
6972
      {                                         // Check const tables
 
6973
        COND *const_cond=
 
6974
          make_cond_for_table(cond,
 
6975
                              join->const_table_map,
 
6976
                              (table_map) 0, 1);
 
6977
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6978
             tab < join->join_tab+join->tables ; tab++)
 
6979
        {
 
6980
          if (*tab->on_expr_ref)
 
6981
          {
 
6982
            JOIN_TAB *cond_tab= tab->first_inner;
 
6983
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6984
                                           join->const_table_map,
 
6985
                                           (  table_map) 0, 0);
 
6986
            if (!tmp)
 
6987
              continue;
 
6988
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6989
            if (!tmp)
 
6990
              return(1);
 
6991
            tmp->quick_fix_field();
 
6992
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6993
                                    new Item_cond_and(cond_tab->select_cond,
 
6994
                                                      tmp);
 
6995
            if (!cond_tab->select_cond)
 
6996
              return(1);
 
6997
            cond_tab->select_cond->quick_fix_field();
 
6998
          }       
 
6999
        }
 
7000
        if (const_cond && !const_cond->val_int())
 
7001
        {
 
7002
          return(1);     // Impossible const condition
 
7003
        }
 
7004
      }
 
7005
    }
 
7006
    used_tables=((select->const_tables=join->const_table_map) |
 
7007
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7008
    for (uint i=join->const_tables ; i < join->tables ; i++)
 
7009
    {
 
7010
      JOIN_TAB *tab=join->join_tab+i;
 
7011
      /*
 
7012
        first_inner is the X in queries like:
 
7013
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
7014
      */
 
7015
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
7016
      table_map current_map= tab->table->map;
 
7017
      bool use_quick_range=0;
 
7018
      COND *tmp;
 
7019
 
 
7020
      /*
 
7021
        Following force including random expression in last table condition.
 
7022
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
7023
      */
 
7024
      if (i == join->tables-1)
 
7025
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7026
      used_tables|=current_map;
 
7027
 
 
7028
      if (tab->type == JT_REF && tab->quick &&
 
7029
          (uint) tab->ref.key == tab->quick->index &&
 
7030
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7031
      {
 
7032
        /* Range uses longer key;  Use this instead of ref on key */
 
7033
        tab->type=JT_ALL;
 
7034
        use_quick_range=1;
 
7035
        tab->use_quick=1;
 
7036
        tab->ref.key= -1;
 
7037
        tab->ref.key_parts=0;           // Don't use ref key.
 
7038
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7039
        /* 
 
7040
          We will use join cache here : prevent sorting of the first
 
7041
          table only and sort at the end.
 
7042
        */
 
7043
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7044
          join->full_join= 1;
 
7045
      }
 
7046
 
 
7047
      tmp= NULL;
 
7048
      if (cond)
 
7049
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7050
      if (cond && !tmp && tab->quick)
 
7051
      {                                         // Outer join
 
7052
        if (tab->type != JT_ALL)
 
7053
        {
 
7054
          /*
 
7055
            Don't use the quick method
 
7056
            We come here in the case where we have 'key=constant' and
 
7057
            the test is removed by make_cond_for_table()
 
7058
          */
 
7059
          delete tab->quick;
 
7060
          tab->quick= 0;
 
7061
        }
 
7062
        else
 
7063
        {
 
7064
          /*
 
7065
            Hack to handle the case where we only refer to a table
 
7066
            in the ON part of an OUTER JOIN. In this case we want the code
 
7067
            below to check if we should use 'quick' instead.
 
7068
          */
 
7069
          tmp= new Item_int((longlong) 1,1);    // Always true
 
7070
        }
 
7071
 
 
7072
      }
 
7073
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7074
          tab->type == JT_EQ_REF)
 
7075
      {
 
7076
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7077
                                       thd->memdup((uchar*) select,
 
7078
                                                   sizeof(*select)));
 
7079
        if (!sel)
 
7080
          return(1);                    // End of memory
 
7081
        /*
 
7082
          If tab is an inner table of an outer join operation,
 
7083
          add a match guard to the pushed down predicate.
 
7084
          The guard will turn the predicate on only after
 
7085
          the first match for outer tables is encountered.
 
7086
        */        
 
7087
        if (cond && tmp)
 
7088
        {
 
7089
          /*
 
7090
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7091
            a cond, so neutralize the hack above.
 
7092
          */
 
7093
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7094
            return(1);
 
7095
          tab->select_cond=sel->cond=tmp;
 
7096
          /* Push condition to storage engine if this is enabled
 
7097
             and the condition is not guarded */
 
7098
          tab->table->file->pushed_cond= NULL;
 
7099
          if (thd->variables.engine_condition_pushdown)
 
7100
          {
 
7101
            COND *push_cond= 
 
7102
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7103
            if (push_cond)
 
7104
            {
 
7105
              /* Push condition to handler */
 
7106
              if (!tab->table->file->cond_push(push_cond))
 
7107
                tab->table->file->pushed_cond= push_cond;
 
7108
            }
 
7109
          }
 
7110
        }
 
7111
        else
 
7112
          tab->select_cond= sel->cond= NULL;
 
7113
 
 
7114
        sel->head=tab->table;
 
7115
        if (tab->quick)
 
7116
        {
 
7117
          /* Use quick key read if it's a constant and it's not used
 
7118
             with key reading */
 
7119
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7120
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7121
          {
 
7122
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7123
            sel->quick_keys.clear_all();
 
7124
            sel->needed_reg.clear_all();
 
7125
          }
 
7126
          else
 
7127
          {
 
7128
            delete tab->quick;
 
7129
          }
 
7130
          tab->quick=0;
 
7131
        }
 
7132
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7133
        if (i == join->const_tables && ref_key)
 
7134
        {
 
7135
          if (!tab->const_keys.is_clear_all() &&
 
7136
              tab->table->reginfo.impossible_range)
 
7137
            return(1);
 
7138
        }
 
7139
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7140
        {
 
7141
          if (!tab->const_keys.is_clear_all() &&
 
7142
              tab->table->reginfo.impossible_range)
 
7143
            return(1);                          // Impossible range
 
7144
          /*
 
7145
            We plan to scan all rows.
 
7146
            Check again if we should use an index.
 
7147
            We could have used an column from a previous table in
 
7148
            the index if we are using limit and this is the first table
 
7149
          */
 
7150
 
 
7151
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7152
              (!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)))
 
7153
          {
 
7154
            /* Join with outer join condition */
 
7155
            COND *orig_cond=sel->cond;
 
7156
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7157
 
 
7158
            /*
 
7159
              We can't call sel->cond->fix_fields,
 
7160
              as it will break tab->on_expr if it's AND condition
 
7161
              (fix_fields currently removes extra AND/OR levels).
 
7162
              Yet attributes of the just built condition are not needed.
 
7163
              Thus we call sel->cond->quick_fix_field for safety.
 
7164
            */
 
7165
            if (sel->cond && !sel->cond->fixed)
 
7166
              sel->cond->quick_fix_field();
 
7167
 
 
7168
            if (sel->test_quick_select(thd, tab->keys,
 
7169
                                       used_tables & ~ current_map,
 
7170
                                       (join->select_options &
 
7171
                                        OPTION_FOUND_ROWS ?
 
7172
                                        HA_POS_ERROR :
 
7173
                                        join->unit->select_limit_cnt), 0,
 
7174
                                        false) < 0)
 
7175
            {
 
7176
              /*
 
7177
                Before reporting "Impossible WHERE" for the whole query
 
7178
                we have to check isn't it only "impossible ON" instead
 
7179
              */
 
7180
              sel->cond=orig_cond;
 
7181
              if (!*tab->on_expr_ref ||
 
7182
                  sel->test_quick_select(thd, tab->keys,
 
7183
                                         used_tables & ~ current_map,
 
7184
                                         (join->select_options &
 
7185
                                          OPTION_FOUND_ROWS ?
 
7186
                                          HA_POS_ERROR :
 
7187
                                          join->unit->select_limit_cnt),0,
 
7188
                                          false) < 0)
 
7189
                return(1);                      // Impossible WHERE
 
7190
            }
 
7191
            else
 
7192
              sel->cond=orig_cond;
 
7193
 
 
7194
            /* Fix for EXPLAIN */
 
7195
            if (sel->quick)
 
7196
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7197
          }
 
7198
          else
 
7199
          {
 
7200
            sel->needed_reg=tab->needed_reg;
 
7201
            sel->quick_keys.clear_all();
 
7202
          }
 
7203
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7204
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7205
          {
 
7206
            tab->keys=sel->quick_keys;
 
7207
            tab->keys.merge(sel->needed_reg);
 
7208
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7209
                             (select->quick_keys.is_clear_all() ||
 
7210
                              (select->quick &&
 
7211
                               (select->quick->records >= 100L)))) ?
 
7212
              2 : 1;
 
7213
            sel->read_tables= used_tables & ~current_map;
 
7214
          }
 
7215
          if (i != join->const_tables && tab->use_quick != 2)
 
7216
          {                                     /* Read with cache */
 
7217
            if (cond &&
 
7218
                (tmp=make_cond_for_table(cond,
 
7219
                                         join->const_table_map |
 
7220
                                         current_map,
 
7221
                                         current_map, 0)))
 
7222
            {
 
7223
              tab->cache.select=(SQL_SELECT*)
 
7224
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
 
7225
              tab->cache.select->cond=tmp;
 
7226
              tab->cache.select->read_tables=join->const_table_map;
 
7227
            }
 
7228
          }
 
7229
        }
 
7230
      }
 
7231
      
 
7232
      /* 
 
7233
        Push down conditions from all on expressions.
 
7234
        Each of these conditions are guarded by a variable
 
7235
        that turns if off just before null complemented row for
 
7236
        outer joins is formed. Thus, the condition from an
 
7237
        'on expression' are guaranteed not to be checked for
 
7238
        the null complemented row.
 
7239
      */ 
 
7240
 
 
7241
      /* First push down constant conditions from on expressions */
 
7242
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7243
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7244
      {
 
7245
        if (*join_tab->on_expr_ref)
 
7246
        {
 
7247
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7248
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7249
                                         join->const_table_map,
 
7250
                                         (table_map) 0, 0);
 
7251
          if (!tmp)
 
7252
            continue;
 
7253
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7254
          if (!tmp)
 
7255
            return(1);
 
7256
          tmp->quick_fix_field();
 
7257
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7258
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7259
          if (!cond_tab->select_cond)
 
7260
            return(1);
 
7261
          cond_tab->select_cond->quick_fix_field();
 
7262
        }       
 
7263
      }
 
7264
 
 
7265
      /* Push down non-constant conditions from on expressions */
 
7266
      JOIN_TAB *last_tab= tab;
 
7267
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7268
      {  
 
7269
        /* 
 
7270
          Table tab is the last inner table of an outer join.
 
7271
          An on expression is always attached to it.
 
7272
        */     
 
7273
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7274
 
 
7275
        table_map used_tables2= (join->const_table_map |
 
7276
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7277
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7278
        {
 
7279
          current_map= tab->table->map;
 
7280
          used_tables2|= current_map;
 
7281
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7282
                                              current_map, 0);
 
7283
          if (tmp_cond)
 
7284
          {
 
7285
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7286
            /*
 
7287
              First add the guards for match variables of
 
7288
              all embedding outer join operations.
 
7289
            */
 
7290
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7291
                                                     tmp_cond,
 
7292
                                                     first_inner_tab)))
 
7293
              return(1);
 
7294
            /* 
 
7295
              Now add the guard turning the predicate off for 
 
7296
              the null complemented row.
 
7297
            */ 
 
7298
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7299
                                              &first_inner_tab->
 
7300
                                              not_null_compl);
 
7301
            if (tmp_cond)
 
7302
              tmp_cond->quick_fix_field();
 
7303
            /* Add the predicate to other pushed down predicates */
 
7304
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7305
                                  new Item_cond_and(cond_tab->select_cond,
 
7306
                                                    tmp_cond);
 
7307
            if (!cond_tab->select_cond)
 
7308
              return(1);
 
7309
            cond_tab->select_cond->quick_fix_field();
 
7310
          }              
 
7311
        }
 
7312
        first_inner_tab= first_inner_tab->first_upper;       
 
7313
      }
 
7314
    }
 
7315
  }
 
7316
  return(0);
 
7317
}
 
7318
 
 
7319
 
 
7320
/* 
 
7321
  Check if given expression uses only table fields covered by the given index
 
7322
 
 
7323
  SYNOPSIS
 
7324
    uses_index_fields_only()
 
7325
      item           Expression to check
 
7326
      tbl            The table having the index
 
7327
      keyno          The index number
 
7328
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7329
 
 
7330
  DESCRIPTION
 
7331
    Check if given expression only uses fields covered by index #keyno in the
 
7332
    table tbl. The expression can use any fields in any other tables.
 
7333
    
 
7334
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7335
    handled outside of this function.
 
7336
 
 
7337
  RETURN
 
7338
    true   Yes
 
7339
    false  No
 
7340
*/
 
7341
 
 
7342
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
 
7343
                            bool other_tbls_ok)
 
7344
{
 
7345
  if (item->const_item())
 
7346
    return true;
 
7347
 
 
7348
  /* 
 
7349
    Don't push down the triggered conditions. Nested outer joins execution 
 
7350
    code may need to evaluate a condition several times (both triggered and
 
7351
    untriggered), and there is no way to put thi
 
7352
    TODO: Consider cloning the triggered condition and using the copies for:
 
7353
      1. push the first copy down, to have most restrictive index condition
 
7354
         possible
 
7355
      2. Put the second copy into tab->select_cond. 
 
7356
  */
 
7357
  if (item->type() == Item::FUNC_ITEM && 
 
7358
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7359
    return false;
 
7360
 
 
7361
  if (!(item->used_tables() & tbl->map))
 
7362
    return other_tbls_ok;
 
7363
 
 
7364
  Item::Type item_type= item->type();
 
7365
  switch (item_type) {
 
7366
  case Item::FUNC_ITEM:
 
7367
    {
 
7368
      /* This is a function, apply condition recursively to arguments */
 
7369
      Item_func *item_func= (Item_func*)item;
 
7370
      Item **child;
 
7371
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7372
      for (child= item_func->arguments(); child != item_end; child++)
 
7373
      {
 
7374
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7375
          return false;
 
7376
      }
 
7377
      return true;
 
7378
    }
 
7379
  case Item::COND_ITEM:
 
7380
    {
 
7381
      /* This is a function, apply condition recursively to arguments */
 
7382
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7383
      Item *item;
 
7384
      while ((item=li++))
 
7385
      {
 
7386
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7387
          return false;
 
7388
      }
 
7389
      return true;
 
7390
    }
 
7391
  case Item::FIELD_ITEM:
 
7392
    {
 
7393
      Item_field *item_field= (Item_field*)item;
 
7394
      if (item_field->field->table != tbl) 
 
7395
        return true;
 
7396
      return item_field->field->part_of_key.is_set(keyno);
 
7397
    }
 
7398
  case Item::REF_ITEM:
 
7399
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7400
                                  other_tbls_ok);
 
7401
  default:
 
7402
    return false; /* Play it safe, don't push unknown non-const items */
 
7403
  }
 
7404
}
 
7405
 
 
7406
 
1211
7407
#define ICP_COND_USES_INDEX_ONLY 10
1212
7408
 
1213
 
 
1214
 
/**
1215
 
  cleanup JoinTable.
1216
 
*/
1217
 
void JoinTable::cleanup()
 
7409
/*
 
7410
  Get a part of the condition that can be checked using only index fields
 
7411
 
 
7412
  SYNOPSIS
 
7413
    make_cond_for_index()
 
7414
      cond           The source condition
 
7415
      table          The table that is partially available
 
7416
      keyno          The index in the above table. Only fields covered by the index
 
7417
                     are available
 
7418
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7419
 
 
7420
  DESCRIPTION
 
7421
    Get a part of the condition that can be checked when for the given table 
 
7422
    we have values only of fields covered by some index. The condition may
 
7423
    refer to other tables, it is assumed that we have values of all of their 
 
7424
    fields.
 
7425
 
 
7426
    Example:
 
7427
      make_cond_for_index(
 
7428
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7429
          t2, keyno(t2.key1)) 
 
7430
      will return
 
7431
        "cond(t1.field) AND cond(t2.key2)"
 
7432
 
 
7433
  RETURN
 
7434
    Index condition, or NULL if no condition could be inferred.
 
7435
*/
 
7436
 
 
7437
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
 
7438
                          bool other_tbls_ok)
 
7439
{
 
7440
  if (!cond)
 
7441
    return NULL;
 
7442
  if (cond->type() == Item::COND_ITEM)
 
7443
  {
 
7444
    uint n_marked= 0;
 
7445
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7446
    {
 
7447
      Item_cond_and *new_cond=new Item_cond_and;
 
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
          new_cond->argument_list()->push_back(fix);
 
7457
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7458
      }
 
7459
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7460
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7461
      switch (new_cond->argument_list()->elements) {
 
7462
      case 0:
 
7463
        return (COND*) 0;
 
7464
      case 1:
 
7465
        return new_cond->argument_list()->head();
 
7466
      default:
 
7467
        new_cond->quick_fix_field();
 
7468
        return new_cond;
 
7469
      }
 
7470
    }
 
7471
    else /* It's OR */
 
7472
    {
 
7473
      Item_cond_or *new_cond=new Item_cond_or;
 
7474
      if (!new_cond)
 
7475
        return (COND*) 0;
 
7476
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7477
      Item *item;
 
7478
      while ((item=li++))
 
7479
      {
 
7480
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7481
        if (!fix)
 
7482
          return (COND*) 0;
 
7483
        new_cond->argument_list()->push_back(fix);
 
7484
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7485
      }
 
7486
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7487
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7488
      new_cond->quick_fix_field();
 
7489
      new_cond->top_level_item();
 
7490
      return new_cond;
 
7491
    }
 
7492
  }
 
7493
 
 
7494
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7495
    return (COND*) 0;
 
7496
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7497
  return cond;
 
7498
}
 
7499
 
 
7500
 
 
7501
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7502
{
 
7503
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7504
    return 0; /* Already checked */
 
7505
 
 
7506
  if (cond->type() == Item::COND_ITEM)
 
7507
  {
 
7508
    table_map tbl_map= 0;
 
7509
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7510
    {
 
7511
      /* Create new top level AND item */
 
7512
      Item_cond_and *new_cond=new Item_cond_and;
 
7513
      if (!new_cond)
 
7514
        return (COND*) 0;
 
7515
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7516
      Item *item;
 
7517
      while ((item=li++))
 
7518
      {
 
7519
        Item *fix= make_cond_remainder(item, exclude_index);
 
7520
        if (fix)
 
7521
        {
 
7522
          new_cond->argument_list()->push_back(fix);
 
7523
          tbl_map |= fix->used_tables();
 
7524
        }
 
7525
      }
 
7526
      switch (new_cond->argument_list()->elements) {
 
7527
      case 0:
 
7528
        return (COND*) 0;
 
7529
      case 1:
 
7530
        return new_cond->argument_list()->head();
 
7531
      default:
 
7532
        new_cond->quick_fix_field();
 
7533
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7534
        return new_cond;
 
7535
      }
 
7536
    }
 
7537
    else /* It's OR */
 
7538
    {
 
7539
      Item_cond_or *new_cond=new Item_cond_or;
 
7540
      if (!new_cond)
 
7541
        return (COND*) 0;
 
7542
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7543
      Item *item;
 
7544
      while ((item=li++))
 
7545
      {
 
7546
        Item *fix= make_cond_remainder(item, false);
 
7547
        if (!fix)
 
7548
          return (COND*) 0;
 
7549
        new_cond->argument_list()->push_back(fix);
 
7550
        tbl_map |= fix->used_tables();
 
7551
      }
 
7552
      new_cond->quick_fix_field();
 
7553
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7554
      new_cond->top_level_item();
 
7555
      return new_cond;
 
7556
    }
 
7557
  }
 
7558
  return cond;
 
7559
}
 
7560
 
 
7561
 
 
7562
/*
 
7563
  Try to extract and push the index condition
 
7564
 
 
7565
  SYNOPSIS
 
7566
    push_index_cond()
 
7567
      tab            A join tab that has tab->table->file and its condition
 
7568
                     in tab->select_cond
 
7569
      keyno          Index for which extract and push the condition
 
7570
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7571
 
 
7572
  DESCRIPTION
 
7573
    Try to extract and push the index condition down to table handler
 
7574
*/
 
7575
 
 
7576
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
 
7577
{
 
7578
  Item *idx_cond;
 
7579
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7580
      tab->join->thd->variables.engine_condition_pushdown)
 
7581
  {
 
7582
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7583
                                  other_tbls_ok);
 
7584
 
 
7585
    if (idx_cond)
 
7586
    {
 
7587
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7588
      Item *idx_remainder_cond= 
 
7589
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7590
 
 
7591
      /*
 
7592
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7593
        condition. 
 
7594
        TODO: This check happens to work on current ICP implementations, but
 
7595
        there may exist a compliant implementation that will not work 
 
7596
        correctly with it. Sort this out when we stabilize the condition
 
7597
        pushdown APIs.
 
7598
      */
 
7599
      if (idx_remainder_cond != idx_cond)
 
7600
        tab->ref.disable_cache= true;
 
7601
 
 
7602
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7603
 
 
7604
      if (row_cond)
 
7605
      {
 
7606
        if (!idx_remainder_cond)
 
7607
          tab->select_cond= row_cond;
 
7608
        else
 
7609
        {
 
7610
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7611
          tab->select_cond->quick_fix_field();
 
7612
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7613
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7614
        }
 
7615
      }
 
7616
      else
 
7617
        tab->select_cond= idx_remainder_cond;
 
7618
      if (tab->select)
 
7619
      {
 
7620
        tab->select->cond= tab->select_cond;
 
7621
      }
 
7622
    }
 
7623
  }
 
7624
  return;
 
7625
}
 
7626
 
 
7627
 
 
7628
 
 
7629
    /*
 
7630
      Determine if the set is already ordered for ORDER BY, so it can 
 
7631
      disable join cache because it will change the ordering of the results.
 
7632
      Code handles sort table that is at any location (not only first after 
 
7633
      the const tables) despite the fact that it's currently prohibited.
 
7634
      We must disable join cache if the first non-const table alone is
 
7635
      ordered. If there is a temp table the ordering is done as a last
 
7636
      operation and doesn't prevent join cache usage.
 
7637
    */
 
7638
uint make_join_orderinfo(JOIN *join)
 
7639
{
 
7640
  uint i;
 
7641
  if (join->need_tmp)
 
7642
    return join->tables;
 
7643
 
 
7644
  for (i=join->const_tables ; i < join->tables ; i++)
 
7645
  {
 
7646
    JOIN_TAB *tab=join->join_tab+i;
 
7647
    TABLE *table=tab->table;
 
7648
    if ((table == join->sort_by_table && 
 
7649
         (!join->order || join->skip_sort_order)) ||
 
7650
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
 
7651
    {
 
7652
      break;
 
7653
    }
 
7654
  }
 
7655
  return i;
 
7656
}
 
7657
 
 
7658
 
 
7659
/*
 
7660
  Plan refinement stage: do various set ups for the executioner
 
7661
 
 
7662
  SYNOPSIS
 
7663
    make_join_readinfo()
 
7664
      join           Join being processed
 
7665
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7666
                     SELECT_NO_JOIN_CACHE)
 
7667
      no_jbuf_after  Don't use join buffering after table with this number.
 
7668
 
 
7669
  DESCRIPTION
 
7670
    Plan refinement stage: do various set ups for the executioner
 
7671
      - set up use of join buffering
 
7672
      - push index conditions
 
7673
      - increment counters
 
7674
      - etc
 
7675
 
 
7676
  RETURN 
 
7677
    false - OK
 
7678
    true  - Out of memory
 
7679
*/
 
7680
 
 
7681
static bool
 
7682
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
 
7683
{
 
7684
  uint i;
 
7685
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7686
  bool sorted= 1;
 
7687
 
 
7688
  for (i=join->const_tables ; i < join->tables ; i++)
 
7689
  {
 
7690
    JOIN_TAB *tab=join->join_tab+i;
 
7691
    TABLE *table=tab->table;
 
7692
    bool using_join_cache;
 
7693
    tab->read_record.table= table;
 
7694
    tab->read_record.file=table->file;
 
7695
    tab->next_select=sub_select;                /* normal select */
 
7696
    /* 
 
7697
      TODO: don't always instruct first table's ref/range access method to 
 
7698
      produce sorted output.
 
7699
    */
 
7700
    tab->sorted= sorted;
 
7701
    sorted= 0;                                  // only first must be sorted
 
7702
    if (tab->insideout_match_tab)
 
7703
    {
 
7704
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
 
7705
                                                         [tab->index].
 
7706
                                                         key_length)))
 
7707
        return true;
 
7708
    }
 
7709
    switch (tab->type) {
 
7710
    case JT_SYSTEM:                             // Only happens with left join
 
7711
      table->status=STATUS_NO_RECORD;
 
7712
      tab->read_first_record= join_read_system;
 
7713
      tab->read_record.read_record= join_no_more_records;
 
7714
      break;
 
7715
    case JT_CONST:                              // Only happens with left join
 
7716
      table->status=STATUS_NO_RECORD;
 
7717
      tab->read_first_record= join_read_const;
 
7718
      tab->read_record.read_record= join_no_more_records;
 
7719
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7720
          !table->no_keyread)
 
7721
      {
 
7722
        table->key_read=1;
 
7723
        table->file->extra(HA_EXTRA_KEYREAD);
 
7724
      }
 
7725
      break;
 
7726
    case JT_EQ_REF:
 
7727
      table->status=STATUS_NO_RECORD;
 
7728
      if (tab->select)
 
7729
      {
 
7730
        delete tab->select->quick;
 
7731
        tab->select->quick=0;
 
7732
      }
 
7733
      delete tab->quick;
 
7734
      tab->quick=0;
 
7735
      tab->read_first_record= join_read_key;
 
7736
      tab->read_record.read_record= join_no_more_records;
 
7737
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7738
          !table->no_keyread)
 
7739
      {
 
7740
        table->key_read=1;
 
7741
        table->file->extra(HA_EXTRA_KEYREAD);
 
7742
      }
 
7743
      else
 
7744
        push_index_cond(tab, tab->ref.key, true);
 
7745
      break;
 
7746
    case JT_REF_OR_NULL:
 
7747
    case JT_REF:
 
7748
      table->status=STATUS_NO_RECORD;
 
7749
      if (tab->select)
 
7750
      {
 
7751
        delete tab->select->quick;
 
7752
        tab->select->quick=0;
 
7753
      }
 
7754
      delete tab->quick;
 
7755
      tab->quick=0;
 
7756
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7757
          !table->no_keyread)
 
7758
      {
 
7759
        table->key_read=1;
 
7760
        table->file->extra(HA_EXTRA_KEYREAD);
 
7761
      }
 
7762
      else
 
7763
        push_index_cond(tab, tab->ref.key, true);
 
7764
      if (tab->type == JT_REF)
 
7765
      {
 
7766
        tab->read_first_record= join_read_always_key;
 
7767
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7768
           join_read_next_same_diff : join_read_next_same;
 
7769
      }
 
7770
      else
 
7771
      {
 
7772
        tab->read_first_record= join_read_always_key_or_null;
 
7773
        tab->read_record.read_record= join_read_next_same_or_null;
 
7774
      }
 
7775
      break;
 
7776
    case JT_ALL:
 
7777
      /*
 
7778
        If previous table use cache
 
7779
        If the incoming data set is already sorted don't use cache.
 
7780
      */
 
7781
      table->status=STATUS_NO_RECORD;
 
7782
      using_join_cache= false;
 
7783
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7784
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7785
          !tab->insideout_match_tab)
 
7786
      {
 
7787
        if ((options & SELECT_DESCRIBE) ||
 
7788
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7789
                             i-join->const_tables))
 
7790
        {
 
7791
          using_join_cache= true;
 
7792
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7793
        }
 
7794
      }
 
7795
      /* These init changes read_record */
 
7796
      if (tab->use_quick == 2)
 
7797
      {
 
7798
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7799
        tab->read_first_record= join_init_quick_read_record;
 
7800
        if (statistics)
 
7801
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7802
      }
 
7803
      else
 
7804
      {
 
7805
        tab->read_first_record= join_init_read_record;
 
7806
        if (i == join->const_tables)
 
7807
        {
 
7808
          if (tab->select && tab->select->quick)
 
7809
          {
 
7810
            if (statistics)
 
7811
              status_var_increment(join->thd->status_var.select_range_count);
 
7812
          }
 
7813
          else
 
7814
          {
 
7815
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7816
            if (statistics)
 
7817
              status_var_increment(join->thd->status_var.select_scan_count);
 
7818
          }
 
7819
        }
 
7820
        else
 
7821
        {
 
7822
          if (tab->select && tab->select->quick)
 
7823
          {
 
7824
            if (statistics)
 
7825
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7826
          }
 
7827
          else
 
7828
          {
 
7829
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7830
            if (statistics)
 
7831
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7832
          }
 
7833
        }
 
7834
        if (!table->no_keyread)
 
7835
        {
 
7836
          if (tab->select && tab->select->quick &&
 
7837
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7838
              table->covering_keys.is_set(tab->select->quick->index))
 
7839
          {
 
7840
            table->key_read=1;
 
7841
            table->file->extra(HA_EXTRA_KEYREAD);
 
7842
          }
 
7843
          else if (!table->covering_keys.is_clear_all() &&
 
7844
                   !(tab->select && tab->select->quick))
 
7845
          {                                     // Only read index tree
 
7846
            if (!tab->insideout_match_tab)
 
7847
            {
 
7848
              /*
 
7849
                See bug #26447: "Using the clustered index for a table scan
 
7850
                is always faster than using a secondary index".
 
7851
              */
 
7852
              if (table->s->primary_key != MAX_KEY &&
 
7853
                  table->file->primary_key_is_clustered())
 
7854
                tab->index= table->s->primary_key;
 
7855
              else
 
7856
                tab->index=find_shortest_key(table, & table->covering_keys);
 
7857
            }
 
7858
            tab->read_first_record= join_read_first;
 
7859
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7860
          }
 
7861
        }
 
7862
        if (tab->select && tab->select->quick &&
 
7863
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7864
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7865
      }
 
7866
      break;
 
7867
    default:
 
7868
      break;                                    /* purecov: deadcode */
 
7869
    case JT_UNKNOWN:
 
7870
    case JT_MAYBE_REF:
 
7871
      abort();                                  /* purecov: deadcode */
 
7872
    }
 
7873
  }
 
7874
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7875
  return(false);
 
7876
}
 
7877
 
 
7878
 
 
7879
/**
 
7880
  Give error if we some tables are done with a full join.
 
7881
 
 
7882
  This is used by multi_table_update and multi_table_delete when running
 
7883
  in safe mode.
 
7884
 
 
7885
  @param join           Join condition
 
7886
 
 
7887
  @retval
 
7888
    0   ok
 
7889
  @retval
 
7890
    1   Error (full join used)
 
7891
*/
 
7892
 
 
7893
bool error_if_full_join(JOIN *join)
 
7894
{
 
7895
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7896
       tab < end;
 
7897
       tab++)
 
7898
  {
 
7899
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7900
    {
 
7901
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7902
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7903
      return(1);
 
7904
    }
 
7905
  }
 
7906
  return(0);
 
7907
}
 
7908
 
 
7909
 
 
7910
/**
 
7911
  cleanup JOIN_TAB.
 
7912
*/
 
7913
 
 
7914
void JOIN_TAB::cleanup()
1218
7915
{
1219
7916
  delete select;
1220
7917
  select= 0;
1221
7918
  delete quick;
1222
7919
  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
 
  }
 
7920
  x_free(cache.buff);
1229
7921
  cache.buff= 0;
1230
7922
  limit= 0;
1231
7923
  if (table)
1233
7925
    if (table->key_read)
1234
7926
    {
1235
7927
      table->key_read= 0;
1236
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7928
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1237
7929
    }
1238
 
    table->cursor->ha_index_or_rnd_end();
 
7930
    table->file->ha_index_or_rnd_end();
1239
7931
    /*
1240
7932
      We need to reset this for next select
1241
7933
      (Tested in part_of_refkey)
1242
7934
    */
1243
7935
    table->reginfo.join_tab= 0;
1244
7936
  }
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
 
}
 
7937
  end_read_record(&read_record);
 
7938
}
 
7939
 
 
7940
 
 
7941
/**
 
7942
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7943
  (table cursors), free quick selects.
 
7944
 
 
7945
    This function is called in the end of execution of a JOIN, before the used
 
7946
    tables are unlocked and closed.
 
7947
 
 
7948
    For a join that is resolved using a temporary table, the first sweep is
 
7949
    performed against actual tables and an intermediate result is inserted
 
7950
    into the temprorary table.
 
7951
    The last sweep is performed against the temporary table. Therefore,
 
7952
    the base tables and associated buffers used to fill the temporary table
 
7953
    are no longer needed, and this function is called to free them.
 
7954
 
 
7955
    For a join that is performed without a temporary table, this function
 
7956
    is called after all rows are sent, but before EOF packet is sent.
 
7957
 
 
7958
    For a simple SELECT with no subqueries this function performs a full
 
7959
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7960
    tables.
 
7961
 
 
7962
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7963
    do the full cleanup and need to do a partial cleanup only.
 
7964
    - If a JOIN is not the top level join, we must not unlock the tables
 
7965
    because the outer select may not have been evaluated yet, and we
 
7966
    can't unlock only selected tables of a query.
 
7967
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7968
    should not free quick selects and join buffers because they will be
 
7969
    needed for the next execution of the correlated subquery.
 
7970
    - However, if this is a JOIN for a [sub]select, which is not
 
7971
    a correlated subquery itself, but has subqueries, we can free it
 
7972
    fully and also free JOINs of all its subqueries. The exception
 
7973
    is a subquery in SELECT list, e.g: @n
 
7974
    SELECT a, (select max(b) from t1) group by c @n
 
7975
    This subquery will not be evaluated at first sweep and its value will
 
7976
    not be inserted into the temporary table. Instead, it's evaluated
 
7977
    when selecting from the temporary table. Therefore, it can't be freed
 
7978
    here even though it's not correlated.
 
7979
 
 
7980
  @todo
 
7981
    Unlock tables even if the join isn't top level select in the tree
 
7982
*/
 
7983
 
 
7984
void JOIN::join_free()
 
7985
{
 
7986
  SELECT_LEX_UNIT *tmp_unit;
 
7987
  SELECT_LEX *sl;
 
7988
  /*
 
7989
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7990
    free all tables.
 
7991
  */
 
7992
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7993
  bool can_unlock= full;
 
7994
 
 
7995
  cleanup(full);
 
7996
 
 
7997
  for (tmp_unit= select_lex->first_inner_unit();
 
7998
       tmp_unit;
 
7999
       tmp_unit= tmp_unit->next_unit())
 
8000
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
8001
    {
 
8002
      Item_subselect *subselect= sl->master_unit()->item;
 
8003
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
8004
      /*
 
8005
        If this join is evaluated, we can fully clean it up and clean up all
 
8006
        its underlying joins even if they are correlated -- they will not be
 
8007
        used any more anyway.
 
8008
        If this join is not yet evaluated, we still must clean it up to
 
8009
        close its table cursors -- it may never get evaluated, as in case of
 
8010
        ... HAVING false OR a IN (SELECT ...))
 
8011
        but all table cursors must be closed before the unlock.
 
8012
      */
 
8013
      sl->cleanup_all_joins(full_local);
 
8014
      /* Can't unlock if at least one JOIN is still needed */
 
8015
      can_unlock= can_unlock && full_local;
 
8016
    }
 
8017
 
 
8018
  /*
 
8019
    We are not using tables anymore
 
8020
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8021
  */
 
8022
  if (can_unlock && lock && thd->lock &&
 
8023
      !(select_options & SELECT_NO_UNLOCK) &&
 
8024
      !select_lex->subquery_in_having &&
 
8025
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
8026
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
8027
  {
 
8028
    /*
 
8029
      TODO: unlock tables even if the join isn't top level select in the
 
8030
      tree.
 
8031
    */
 
8032
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8033
    lock= 0;
 
8034
  }
 
8035
 
 
8036
  return;
 
8037
}
 
8038
 
 
8039
 
 
8040
/**
 
8041
  Free resources of given join.
 
8042
 
 
8043
  @param fill   true if we should free all resources, call with full==1
 
8044
                should be last, before it this function can be called with
 
8045
                full==0
 
8046
 
 
8047
  @note
 
8048
    With subquery this function definitely will be called several times,
 
8049
    but even for simple query it can be called several times.
 
8050
*/
 
8051
 
 
8052
void JOIN::cleanup(bool full)
 
8053
{
 
8054
  if (table)
 
8055
  {
 
8056
    JOIN_TAB *tab,*end;
 
8057
    /*
 
8058
      Only a sorted table may be cached.  This sorted table is always the
 
8059
      first non const table in join->table
 
8060
    */
 
8061
    if (tables > const_tables) // Test for not-const tables
 
8062
    {
 
8063
      free_io_cache(table[const_tables]);
 
8064
      filesort_free_buffers(table[const_tables],full);
 
8065
    }
 
8066
 
 
8067
    if (full)
 
8068
    {
 
8069
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8070
        tab->cleanup();
 
8071
      table= 0;
 
8072
    }
 
8073
    else
 
8074
    {
 
8075
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8076
      {
 
8077
        if (tab->table)
 
8078
          tab->table->file->ha_index_or_rnd_end();
 
8079
      }
 
8080
    }
 
8081
    cleanup_sj_tmp_tables(this);//
 
8082
  }
 
8083
  /*
 
8084
    We are not using tables anymore
 
8085
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8086
  */
 
8087
  if (full)
 
8088
  {
 
8089
    if (tmp_join)
 
8090
      tmp_table_param.copy_field= 0;
 
8091
    group_fields.delete_elements();
 
8092
    /*
 
8093
      We can't call delete_elements() on copy_funcs as this will cause
 
8094
      problems in free_elements() as some of the elements are then deleted.
 
8095
    */
 
8096
    tmp_table_param.copy_funcs.empty();
 
8097
    /*
 
8098
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8099
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8100
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8101
      be removed in tmp_table_param.cleanup().
 
8102
    */
 
8103
    if (tmp_join &&
 
8104
        tmp_join != this &&
 
8105
        tmp_join->tmp_table_param.copy_field ==
 
8106
        tmp_table_param.copy_field)
 
8107
    {
 
8108
      tmp_join->tmp_table_param.copy_field=
 
8109
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8110
    }
 
8111
    tmp_table_param.cleanup();
 
8112
  }
 
8113
  return;
 
8114
}
 
8115
 
1257
8116
 
1258
8117
/**
1259
8118
  Remove the following expressions from ORDER BY and GROUP BY:
1260
8119
  Constant expressions @n
1261
8120
  Expression that only uses tables that are of type EQ_REF and the reference
1262
 
  is in the order_st list or if all refereed tables are of the above type.
 
8121
  is in the ORDER list or if all refereed tables are of the above type.
1263
8122
 
1264
8123
  In the following, the X field can be removed:
1265
8124
  @code
1274
8133
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1275
8134
  @endcode
1276
8135
*/
1277
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8136
 
 
8137
static bool
 
8138
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
1278
8139
{
1279
8140
  if (tab->cached_eq_ref_table)                 // If cached
1280
8141
    return tab->eq_ref_table;
1281
8142
  tab->cached_eq_ref_table=1;
1282
8143
  /* 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)
 
8144
  if (tab->type == JT_CONST && !tab->first_inner)
 
8145
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8146
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1286
8147
    return (tab->eq_ref_table=0);               // We must use this
1287
8148
  Item **ref_item=tab->ref.items;
1288
8149
  Item **end=ref_item+tab->ref.key_parts;
1289
 
  uint32_t found=0;
 
8150
  uint found=0;
1290
8151
  table_map map=tab->table->map;
1291
8152
 
1292
8153
  for (; ref_item != end ; ref_item++)
1293
8154
  {
1294
8155
    if (! (*ref_item)->const_item())
1295
8156
    {                                           // Not a const ref
1296
 
      Order *order;
 
8157
      ORDER *order;
1297
8158
      for (order=start_order ; order ; order=order->next)
1298
8159
      {
1299
 
        if ((*ref_item)->eq(order->item[0],0))
1300
 
          break;
 
8160
        if ((*ref_item)->eq(order->item[0],0))
 
8161
          break;
1301
8162
      }
1302
8163
      if (order)
1303
8164
      {
1304
 
        found++;
1305
 
        assert(!(order->used & map));
1306
 
        order->used|=map;
1307
 
        continue;                               // Used in order_st BY
 
8165
        found++;
 
8166
        assert(!(order->used & map));
 
8167
        order->used|=map;
 
8168
        continue;                               // Used in ORDER BY
1308
8169
      }
1309
8170
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1310
 
        return (tab->eq_ref_table= 0);
 
8171
        return (tab->eq_ref_table=0);
1311
8172
    }
1312
8173
  }
1313
8174
  /* Check that there was no reference to table before sort order */
1319
8180
      continue;
1320
8181
    }
1321
8182
    if (start_order->depend_map & map)
1322
 
      return (tab->eq_ref_table= 0);
1323
 
  }
1324
 
  return tab->eq_ref_table= 1;
1325
 
}
 
8183
      return (tab->eq_ref_table=0);
 
8184
  }
 
8185
  return tab->eq_ref_table=1;
 
8186
}
 
8187
 
 
8188
 
 
8189
static bool
 
8190
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
 
8191
{
 
8192
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8193
    return 0;                   // skip this optimize /* purecov: inspected */
 
8194
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8195
  {
 
8196
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8197
      return 0;
 
8198
  }
 
8199
  return 1;
 
8200
}
 
8201
 
 
8202
 
 
8203
/** Update the dependency map for the tables. */
 
8204
 
 
8205
static void update_depend_map(JOIN *join)
 
8206
{
 
8207
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8208
 
 
8209
  for (; join_tab != end ; join_tab++)
 
8210
  {
 
8211
    TABLE_REF *ref= &join_tab->ref;
 
8212
    table_map depend_map=0;
 
8213
    Item **item=ref->items;
 
8214
    uint i;
 
8215
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8216
      depend_map|=(*item)->used_tables();
 
8217
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8218
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8219
    for (JOIN_TAB **tab=join->map2table;
 
8220
         depend_map ;
 
8221
         tab++,depend_map>>=1 )
 
8222
    {
 
8223
      if (depend_map & 1)
 
8224
        ref->depend_map|=(*tab)->ref.depend_map;
 
8225
    }
 
8226
  }
 
8227
}
 
8228
 
 
8229
 
 
8230
/** Update the dependency map for the sort order. */
 
8231
 
 
8232
static void update_depend_map(JOIN *join, ORDER *order)
 
8233
{
 
8234
  for (; order ; order=order->next)
 
8235
  {
 
8236
    table_map depend_map;
 
8237
    order->item[0]->update_used_tables();
 
8238
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8239
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8240
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8241
        && !order->item[0]->with_sum_func)
 
8242
    {
 
8243
      for (JOIN_TAB **tab=join->map2table;
 
8244
           depend_map ;
 
8245
           tab++, depend_map>>=1)
 
8246
      {
 
8247
        if (depend_map & 1)
 
8248
          order->depend_map|=(*tab)->ref.depend_map;
 
8249
      }
 
8250
    }
 
8251
  }
 
8252
}
 
8253
 
 
8254
 
 
8255
/**
 
8256
  Remove all constants and check if ORDER only contains simple
 
8257
  expressions.
 
8258
 
 
8259
  simple_order is set to 1 if sort_order only uses fields from head table
 
8260
  and the head table is not a LEFT JOIN table.
 
8261
 
 
8262
  @param join                   Join handler
 
8263
  @param first_order            List of SORT or GROUP order
 
8264
  @param cond                   WHERE statement
 
8265
  @param change_list            Set to 1 if we should remove things from list.
 
8266
                               If this is not set, then only simple_order is
 
8267
                               calculated.
 
8268
  @param simple_order           Set to 1 if we are only using simple expressions
 
8269
 
 
8270
  @return
 
8271
    Returns new sort order
 
8272
*/
 
8273
 
 
8274
static ORDER *
 
8275
remove_const(JOIN *join,ORDER *first_order, COND *cond,
 
8276
             bool change_list, bool *simple_order)
 
8277
{
 
8278
  if (join->tables == join->const_tables)
 
8279
    return change_list ? 0 : first_order;               // No need to sort
 
8280
 
 
8281
  ORDER *order,**prev_ptr;
 
8282
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8283
  table_map not_const_tables= ~join->const_table_map;
 
8284
  table_map ref;
 
8285
 
 
8286
  prev_ptr= &first_order;
 
8287
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8288
 
 
8289
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8290
 
 
8291
  update_depend_map(join, first_order);
 
8292
  for (order=first_order; order ; order=order->next)
 
8293
  {
 
8294
    table_map order_tables=order->item[0]->used_tables();
 
8295
    if (order->item[0]->with_sum_func)
 
8296
      *simple_order=0;                          // Must do a temp table to sort
 
8297
    else if (!(order_tables & not_const_tables))
 
8298
    {
 
8299
      if (order->item[0]->with_subselect)
 
8300
        order->item[0]->val_str(&order->item[0]->str_value);
 
8301
      continue;                                 // skip const item
 
8302
    }
 
8303
    else
 
8304
    {
 
8305
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8306
        *simple_order=0;
 
8307
      else
 
8308
      {
 
8309
        Item *comp_item=0;
 
8310
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8311
        {
 
8312
          continue;
 
8313
        }
 
8314
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8315
        {
 
8316
          if (!(order_tables & first_table) &&
 
8317
              only_eq_ref_tables(join,first_order, ref))
 
8318
          {
 
8319
            continue;
 
8320
          }
 
8321
          *simple_order=0;                      // Must do a temp table to sort
 
8322
        }
 
8323
      }
 
8324
    }
 
8325
    if (change_list)
 
8326
      *prev_ptr= order;                         // use this entry
 
8327
    prev_ptr= &order->next;
 
8328
  }
 
8329
  if (change_list)
 
8330
    *prev_ptr=0;
 
8331
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8332
    *simple_order=1;
 
8333
  return(first_order);
 
8334
}
 
8335
 
 
8336
 
 
8337
static int
 
8338
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8339
                 List<Item> &fields, bool send_row, ulonglong select_options,
 
8340
                 const char *info, Item *having)
 
8341
{
 
8342
  if (select_options & SELECT_DESCRIBE)
 
8343
  {
 
8344
    select_describe(join, false, false, false, info);
 
8345
    return(0);
 
8346
  }
 
8347
 
 
8348
  join->join_free();
 
8349
 
 
8350
  if (send_row)
 
8351
  {
 
8352
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
 
8353
      mark_as_null_row(table->table);           // All fields are NULL
 
8354
    if (having && having->val_int() == 0)
 
8355
      send_row=0;
 
8356
  }
 
8357
  if (!(result->send_fields(fields,
 
8358
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8359
  {
 
8360
    if (send_row)
 
8361
    {
 
8362
      List_iterator_fast<Item> it(fields);
 
8363
      Item *item;
 
8364
      while ((item= it++))
 
8365
        item->no_rows_in_result();
 
8366
      result->send_data(fields);
 
8367
    }
 
8368
    result->send_eof();                         // Should be safe
 
8369
  }
 
8370
  /* Update results for FOUND_ROWS */
 
8371
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8372
  return(0);
 
8373
}
 
8374
 
 
8375
/*
 
8376
  used only in JOIN::clear
 
8377
*/
 
8378
static void clear_tables(JOIN *join)
 
8379
{
 
8380
  /* 
 
8381
    must clear only the non-const tables, as const tables
 
8382
    are not re-calculated.
 
8383
  */
 
8384
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
8385
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8386
}
 
8387
 
 
8388
/*****************************************************************************
 
8389
  Make som simple condition optimization:
 
8390
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8391
  Remove all dummy tests 'item = item', 'const op const'.
 
8392
  Remove all 'item is NULL', when item can never be null!
 
8393
  item->marker should be 0 for all items on entry
 
8394
  Return in cond_value false if condition is impossible (1 = 2)
 
8395
*****************************************************************************/
 
8396
 
 
8397
class COND_CMP :public ilink {
 
8398
public:
 
8399
  static void *operator new(size_t size)
 
8400
  {
 
8401
    return (void*) sql_alloc((uint) size);
 
8402
  }
 
8403
  static void operator delete(void *ptr __attribute__((unused)),
 
8404
                              size_t size __attribute__((unused)))
 
8405
  { TRASH(ptr, size); }
 
8406
 
 
8407
  Item *and_level;
 
8408
  Item_func *cmp_func;
 
8409
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8410
};
 
8411
 
 
8412
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8413
template class I_List<COND_CMP>;
 
8414
template class I_List_iterator<COND_CMP>;
 
8415
#endif
 
8416
 
1326
8417
 
1327
8418
/**
1328
8419
  Find the multiple equality predicate containing a field.
1342
8433
    - Item_equal for the found multiple equality predicate if a success;
1343
8434
    - NULL otherwise.
1344
8435
*/
1345
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8436
 
 
8437
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8438
                            bool *inherited_fl)
1346
8439
{
1347
8440
  Item_equal *item= 0;
1348
8441
  bool in_upper_level= false;
1363
8456
  return item;
1364
8457
}
1365
8458
 
 
8459
  
1366
8460
/**
1367
8461
  Check whether an equality can be used to build multiple equalities.
1368
8462
 
1389
8483
    the check_equality will be called for the following equality
1390
8484
    predicates a=b, b=c, b=2 and f=e.
1391
8485
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1392
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8486
      *cond_equal into (0,[Item_equal(a,b)]). 
1393
8487
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1394
8488
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1395
8489
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1402
8496
    the Field::eq_def method) are placed to the same multiple equalities.
1403
8497
    Because of this some equality predicates are not eliminated and
1404
8498
    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
 
8499
    We could weeken the equlity test as soon as at least one of the 
 
8500
    equal fields is to be equal to a constant. It would require a 
1407
8501
    more complicated implementation: we would have to store, in
1408
8502
    general case, its own constant for each fields from the multiple
1409
8503
    equality. But at the same time it would allow us to get rid
1421
8515
    containing just field1 and field2 is added to the existing
1422
8516
    multiple equalities.
1423
8517
    If the function processes the predicate of the form field1=const,
1424
 
    it looks for a multiple equality containing field1. If found, the
 
8518
    it looks for a multiple equality containing field1. If found, the 
1425
8519
    function checks the constant of the multiple equality. If the value
1426
8520
    is unknown, it is setup to const. Otherwise the value is compared with
1427
8521
    const and the evaluation of the equality predicate is performed.
1444
8538
  @retval
1445
8539
    false   otherwise
1446
8540
*/
1447
 
static bool check_simple_equality(Item *left_item,
1448
 
                                  Item *right_item,
1449
 
                                  Item *item,
1450
 
                                  COND_EQUAL *cond_equal)
 
8541
 
 
8542
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8543
                                  Item *item, COND_EQUAL *cond_equal)
1451
8544
{
 
8545
  if (left_item->type() == Item::REF_ITEM &&
 
8546
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8547
  {
 
8548
    if (((Item_ref*)left_item)->depended_from)
 
8549
      return false;
 
8550
    left_item= left_item->real_item();
 
8551
  }
 
8552
  if (right_item->type() == Item::REF_ITEM &&
 
8553
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8554
  {
 
8555
    if (((Item_ref*)right_item)->depended_from)
 
8556
      return false;
 
8557
    right_item= right_item->real_item();
 
8558
  }
1452
8559
  if (left_item->type() == Item::FIELD_ITEM &&
1453
8560
      right_item->type() == Item::FIELD_ITEM &&
1454
8561
      !((Item_field*)left_item)->depended_from &&
1466
8573
    bool left_copyfl, right_copyfl;
1467
8574
    Item_equal *left_item_equal=
1468
8575
               find_item_equal(cond_equal, left_field, &left_copyfl);
1469
 
    Item_equal *right_item_equal=
 
8576
    Item_equal *right_item_equal= 
1470
8577
               find_item_equal(cond_equal, right_field, &right_copyfl);
1471
8578
 
1472
8579
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1473
8580
    if (left_field->eq(right_field)) /* f = f */
1474
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8581
      return (!(left_field->maybe_null() && !left_item_equal)); 
1475
8582
 
1476
8583
    if (left_item_equal && left_item_equal == right_item_equal)
1477
8584
    {
1478
 
      /*
 
8585
      /* 
1479
8586
        The equality predicate is inference of one of the existing
1480
8587
        multiple equalities, i.e the condition is already covered
1481
8588
        by upper level equalities
1482
8589
      */
1483
8590
       return true;
1484
8591
    }
1485
 
 
1486
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8592
    
 
8593
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1487
8594
                              item->name < subq_sj_cond_name + 64);
1488
8595
    /* Copy the found multiple equalities at the current level if needed */
1489
8596
    if (left_copyfl)
1504
8611
    }
1505
8612
 
1506
8613
    if (left_item_equal)
1507
 
    {
 
8614
    { 
1508
8615
      /* left item was found in the current or one of the upper levels */
1509
8616
      if (! right_item_equal)
1510
8617
        left_item_equal->add((Item_field *) right_item);
1519
8626
      }
1520
8627
    }
1521
8628
    else
1522
 
    {
 
8629
    { 
1523
8630
      /* left item was not found neither the current nor in upper levels  */
1524
8631
      if (right_item_equal)
1525
8632
      {
1527
8634
        if (copy_item_name)
1528
8635
          right_item_equal->name = item->name;
1529
8636
      }
1530
 
      else
 
8637
      else 
1531
8638
      {
1532
8639
        /* None of the fields was found in multiple equalities */
1533
8640
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1566
8673
 
1567
8674
      if (field_item->result_type() == STRING_RESULT)
1568
8675
      {
1569
 
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
 
8676
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
1570
8677
        if (!item)
1571
8678
        {
1572
8679
          Item_func_eq *eq_item;
1575
8682
          eq_item->set_cmp_func();
1576
8683
          eq_item->quick_fix_field();
1577
8684
          item= eq_item;
1578
 
        }
 
8685
        }  
1579
8686
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1580
8687
            !cs->coll->propagate(cs, 0, 0))
1581
8688
          return false;
1590
8697
      }
1591
8698
      if (item_equal)
1592
8699
      {
1593
 
        /*
 
8700
        /* 
1594
8701
          The flag cond_false will be set to 1 after this, if item_equal
1595
8702
          already contains a constant and its value is  not equal to
1596
8703
          the value of const_item.
1608
8715
  return false;
1609
8716
}
1610
8717
 
 
8718
 
1611
8719
/**
1612
8720
  Convert row equalities into a conjunction of regular equalities.
1613
8721
 
1620
8728
    simple equality nor a row equality the item for this predicate is added
1621
8729
    to eq_list.
1622
8730
 
1623
 
  @param session        thread handle
 
8731
  @param thd        thread handle
1624
8732
  @param left_row   left term of the row equality to be processed
1625
8733
  @param right_row  right term of the row equality to be processed
1626
8734
  @param cond_equal multiple equalities that must hold together with the
1633
8741
  @retval
1634
8742
    false   otherwise
1635
8743
*/
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++)
 
8744
 
 
8745
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8746
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8747
 
8748
  uint n= left_row->cols();
 
8749
  for (uint i= 0 ; i < n; i++)
1644
8750
  {
1645
8751
    bool is_converted;
1646
8752
    Item *left_item= left_row->element_index(i);
1648
8754
    if (left_item->type() == Item::ROW_ITEM &&
1649
8755
        right_item->type() == Item::ROW_ITEM)
1650
8756
    {
1651
 
      is_converted= check_row_equality(session,
 
8757
      is_converted= check_row_equality(thd, 
1652
8758
                                       (Item_row *) left_item,
1653
8759
                                       (Item_row *) right_item,
1654
8760
                                       cond_equal, eq_list);
1655
8761
      if (!is_converted)
1656
 
        session->lex->current_select->cond_count++;
 
8762
        thd->lex->current_select->cond_count++;      
1657
8763
    }
1658
8764
    else
1659
 
    {
 
8765
    { 
1660
8766
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1661
 
      session->lex->current_select->cond_count++;
1662
 
    }
1663
 
 
 
8767
      thd->lex->current_select->cond_count++;
 
8768
    }  
 
8769
 
1664
8770
    if (!is_converted)
1665
8771
    {
1666
8772
      Item_func_eq *eq_item;
1674
8780
  return true;
1675
8781
}
1676
8782
 
 
8783
 
1677
8784
/**
1678
8785
  Eliminate row equalities and form multiple equalities predicates.
1679
8786
 
1688
8795
    equalities which are treated in the same way as original equality
1689
8796
    predicates.
1690
8797
 
1691
 
  @param session        thread handle
 
8798
  @param thd        thread handle
1692
8799
  @param item       predicate to process
1693
8800
  @param cond_equal multiple equalities that must hold together with the
1694
8801
                    predicate
1703
8810
           or, if the equality is neither a simple one nor a row equality,
1704
8811
           or, if the procedure fails by a fatal error.
1705
8812
*/
1706
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8813
 
 
8814
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8815
                           List<Item> *eq_list)
1707
8816
{
1708
8817
  if (item->type() == Item::FUNC_ITEM &&
1709
8818
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1714
8823
    if (left_item->type() == Item::ROW_ITEM &&
1715
8824
        right_item->type() == Item::ROW_ITEM)
1716
8825
    {
1717
 
      session->lex->current_select->cond_count--;
1718
 
      return check_row_equality(session,
 
8826
      thd->lex->current_select->cond_count--;
 
8827
      return check_row_equality(thd,
1719
8828
                                (Item_row *) left_item,
1720
8829
                                (Item_row *) right_item,
1721
8830
                                cond_equal, eq_list);
1722
8831
    }
1723
 
    else
 
8832
    else 
1724
8833
      return check_simple_equality(left_item, right_item, item, cond_equal);
1725
 
  }
 
8834
  } 
1726
8835
  return false;
1727
8836
}
1728
8837
 
 
8838
                          
1729
8839
/**
1730
8840
  Replace all equality predicates in a condition by multiple equality items.
1731
8841
 
1732
8842
    At each 'and' level the function detects items for equality predicates
1733
8843
    and replaced them by a set of multiple equality items of class Item_equal,
1734
 
    taking into account inherited equalities from upper levels.
 
8844
    taking into account inherited equalities from upper levels. 
1735
8845
    If an equality predicate is used not in a conjunction it's just
1736
8846
    replaced by a multiple equality predicate.
1737
8847
    For each 'and' level the function set a pointer to the inherited
1738
8848
    multiple equalities in the cond_equal field of the associated
1739
 
    object of the type Item_cond_and.
 
8849
    object of the type Item_cond_and.   
1740
8850
    The function also traverses the cond tree and and for each field reference
1741
8851
    sets a pointer to the multiple equality item containing the field, if there
1742
8852
    is any. If this multiple equality equates fields to a constant the
1743
 
    function replaces the field reference by the constant in the cases
 
8853
    function replaces the field reference by the constant in the cases 
1744
8854
    when the field is not of a string type or when the field reference is
1745
8855
    just an argument of a comparison predicate.
1746
 
    The function also determines the maximum number of members in
 
8856
    The function also determines the maximum number of members in 
1747
8857
    equality lists of each Item_cond_and object assigning it to
1748
 
    session->lex->current_select->max_equal_elems.
 
8858
    thd->lex->current_select->max_equal_elems.
1749
8859
 
1750
8860
  @note
1751
8861
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1757
8867
    in a conjuction for a minimal set of multiple equality predicates.
1758
8868
    This set can be considered as a canonical representation of the
1759
8869
    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
 
8870
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1761
8871
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1762
8872
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1763
8873
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1768
8878
    The function performs the substitution in a recursive descent by
1769
8879
    the condtion tree, passing to the next AND level a chain of multiple
1770
8880
    equality predicates which have been built at the upper levels.
1771
 
    The Item_equal items built at the level are attached to other
 
8881
    The Item_equal items built at the level are attached to other 
1772
8882
    non-equality conjucts as a sublist. The pointer to the inherited
1773
8883
    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
 
8884
    This chain allows us for any field reference occurence easyly to find a 
1775
8885
    multiple equality that must be held for this occurence.
1776
8886
    For each AND level we do the following:
1777
8887
    - scan it for all equality predicate (=) items
1778
8888
    - join them into disjoint Item_equal() groups
1779
 
    - process the included OR conditions recursively to do the same for
1780
 
      lower AND levels.
 
8889
    - process the included OR conditions recursively to do the same for 
 
8890
      lower AND levels. 
1781
8891
 
1782
8892
    We need to do things in this order as lower AND levels need to know about
1783
8893
    all possible Item_equal objects in upper levels.
1784
8894
 
1785
 
  @param session        thread handle
 
8895
  @param thd        thread handle
1786
8896
  @param cond       condition(expression) where to make replacement
1787
8897
  @param inherited  path to all inherited multiple equality items
1788
8898
 
1789
8899
  @return
1790
8900
    pointer to the transformed condition
1791
8901
*/
1792
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8902
 
 
8903
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8904
                                        COND_EQUAL *inherited)
1793
8905
{
1794
8906
  Item_equal *item_equal;
1795
8907
  COND_EQUAL cond_equal;
1801
8913
    bool and_level= ((Item_cond*) cond)->functype() ==
1802
8914
      Item_func::COND_AND_FUNC;
1803
8915
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1804
 
 
 
8916
    
1805
8917
    List_iterator<Item> li(*args);
1806
8918
    Item *item;
1807
8919
 
1810
8922
      /*
1811
8923
         Retrieve all conjucts of this level detecting the equality
1812
8924
         that are subject to substitution by multiple equality items and
1813
 
         removing each such predicate from the conjunction after having
 
8925
         removing each such predicate from the conjunction after having 
1814
8926
         found/created a multiple equality whose inference the predicate is.
1815
 
     */
 
8927
     */      
1816
8928
      while ((item= li++))
1817
8929
      {
1818
8930
        /*
1820
8932
          structure here because it's restored before each
1821
8933
          re-execution of any prepared statement/stored procedure.
1822
8934
        */
1823
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8935
        if (check_equality(thd, item, &cond_equal, &eq_list))
1824
8936
          li.remove();
1825
8937
      }
1826
8938
 
1829
8941
      {
1830
8942
        item_equal->fix_length_and_dec();
1831
8943
        item_equal->update_used_tables();
1832
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1833
 
                      item_equal->members());
 
8944
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8945
                      item_equal->members());  
1834
8946
      }
1835
8947
 
1836
8948
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1842
8954
    */
1843
8955
    li.rewind();
1844
8956
    while ((item= li++))
1845
 
    {
 
8957
    { 
1846
8958
      Item *new_item;
1847
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8959
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1848
8960
      {
1849
8961
        /* This replacement happens only for standalone equalities */
1850
8962
        /*
1872
8984
      (b=5) and (a=c) are standalone equalities.
1873
8985
      In general we can't leave alone standalone eqalities:
1874
8986
      for WHERE a=b AND c=d AND (b=c OR d=5)
1875
 
      b=c is replaced by =(a,b,c,d).
 
8987
      b=c is replaced by =(a,b,c,d).  
1876
8988
     */
1877
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8989
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1878
8990
    {
1879
8991
      int n= cond_equal.current_level.elements + eq_list.elements;
1880
8992
      if (n == 0)
1881
 
        return new Item_int((int64_t) 1,1);
 
8993
        return new Item_int((longlong) 1,1);
1882
8994
      else if (n == 1)
1883
8995
      {
1884
8996
        if ((item_equal= cond_equal.current_level.pop()))
1885
8997
        {
1886
8998
          item_equal->fix_length_and_dec();
1887
8999
          item_equal->update_used_tables();
1888
 
        }
 
9000
        }
1889
9001
        else
1890
9002
          item_equal= (Item_equal *) eq_list.pop();
1891
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1892
 
                      item_equal->members());
 
9003
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
9004
                      item_equal->members());  
1893
9005
        return item_equal;
1894
9006
      }
1895
9007
      else
1896
9008
      {
1897
 
        /*
 
9009
        /* 
1898
9010
          Here a new AND level must be created. It can happen only
1899
9011
          when a row equality is processed as a standalone predicate.
1900
 
        */
 
9012
        */
1901
9013
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1902
9014
        and_cond->quick_fix_field();
1903
9015
        List<Item> *args= and_cond->argument_list();
1906
9018
        {
1907
9019
          item_equal->fix_length_and_dec();
1908
9020
          item_equal->update_used_tables();
1909
 
          set_if_bigger(session->lex->current_select->max_equal_elems,
1910
 
                        item_equal->members());
 
9021
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
9022
                        item_equal->members());  
1911
9023
        }
1912
9024
        and_cond->cond_equal= cond_equal;
1913
9025
        args->concat((List<Item> *)&cond_equal.current_level);
1914
 
 
 
9026
        
1915
9027
        return and_cond;
1916
9028
      }
1917
9029
    }
1918
 
    /*
 
9030
    /* 
1919
9031
      For each field reference in cond, not from equal item predicates,
1920
9032
      set a pointer to the multiple equality it belongs to (if there is any)
1921
9033
      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;
 
9034
      an argument of a comparison predicate. 
 
9035
    */ 
 
9036
    uchar *is_subst_valid= (uchar *) 1;
1925
9037
    cond= cond->compile(&Item::subst_argument_checker,
1926
 
                        &is_subst_valid,
 
9038
                        &is_subst_valid, 
1927
9039
                        &Item::equal_fields_propagator,
1928
 
                        (unsigned char *) inherited);
 
9040
                        (uchar *) inherited);
1929
9041
    cond->update_used_tables();
1930
9042
  }
1931
9043
  return cond;
1932
9044
}
1933
9045
 
 
9046
 
1934
9047
/**
1935
9048
  Build multiple equalities for a condition and all on expressions that
1936
9049
  inherit these multiple equalities.
1976
9089
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1977
9090
        WHERE t1.a=t2.a
1978
9091
    @endcode
1979
 
    that is equivalent to:
 
9092
    that is equivalent to:   
1980
9093
    @code
1981
9094
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1982
9095
        WHERE t1.a=t2.a
1983
9096
    @endcode
1984
9097
    Thus, applying equalities from the where condition we basically
1985
9098
    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
 
9099
    Althogh we don't use this property now, it probably makes sense to use 
 
9100
    it in the future.    
 
9101
  @param thd                  Thread handler
1989
9102
  @param cond                condition to build the multiple equalities for
1990
9103
  @param inherited           path to all inherited multiple equality items
1991
9104
  @param join_list           list of join tables to which the condition
1996
9109
  @return
1997
9110
    pointer to the transformed condition containing multiple equalities
1998
9111
*/
1999
 
static COND *build_equal_items(Session *session, COND *cond,
 
9112
   
 
9113
static COND *build_equal_items(THD *thd, COND *cond,
2000
9114
                               COND_EQUAL *inherited,
2001
 
                               List<TableList> *join_list,
 
9115
                               List<TABLE_LIST> *join_list,
2002
9116
                               COND_EQUAL **cond_equal_ref)
2003
9117
{
2004
9118
  COND_EQUAL *cond_equal= 0;
2005
9119
 
2006
 
  if (cond)
 
9120
  if (cond) 
2007
9121
  {
2008
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9122
    cond= build_equal_items_for_cond(thd, cond, inherited);
2009
9123
    cond->update_used_tables();
2010
9124
    if (cond->type() == Item::COND_ITEM &&
2011
9125
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2026
9140
 
2027
9141
  if (join_list)
2028
9142
  {
2029
 
    TableList *table;
2030
 
    List_iterator<TableList> li(*join_list);
 
9143
    TABLE_LIST *table;
 
9144
    List_iterator<TABLE_LIST> li(*join_list);
2031
9145
 
2032
9146
    while ((table= li++))
2033
9147
    {
2034
9148
      if (table->on_expr)
2035
9149
      {
2036
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2037
 
          &table->getNestedJoin()->join_list : NULL;
 
9150
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
 
9151
          &table->nested_join->join_list : NULL;
2038
9152
        /*
2039
9153
          We can modify table->on_expr because its old value will
2040
9154
          be restored before re-execution of PS/SP.
2041
9155
        */
2042
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9156
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2043
9157
                                          nested_join_list,
2044
9158
                                          &table->cond_equal);
2045
9159
      }
2047
9161
  }
2048
9162
 
2049
9163
  return cond;
2050
 
}
 
9164
}    
 
9165
 
2051
9166
 
2052
9167
/**
2053
9168
  Compare field items by table order in the execution plan.
2054
9169
 
2055
9170
    field1 considered as better than field2 if the table containing
2056
 
    field1 is accessed earlier than the table containing field2.
 
9171
    field1 is accessed earlier than the table containing field2.   
2057
9172
    The function finds out what of two fields is better according
2058
9173
    this criteria.
2059
9174
 
2068
9183
  @retval
2069
9184
    0  otherwise
2070
9185
*/
 
9186
 
2071
9187
static int compare_fields_by_table_order(Item_field *field1,
2072
 
                                         Item_field *field2,
2073
 
                                         void *table_join_idx)
 
9188
                                  Item_field *field2,
 
9189
                                  void *table_join_idx)
2074
9190
{
2075
9191
  int cmp= 0;
2076
9192
  bool outer_ref= 0;
2077
9193
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2078
 
  {
 
9194
  {  
2079
9195
    outer_ref= 1;
2080
9196
    cmp= -1;
2081
9197
  }
2086
9202
  }
2087
9203
  if (outer_ref)
2088
9204
    return cmp;
2089
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2090
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9205
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9206
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2091
9207
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2092
9208
}
2093
9209
 
 
9210
 
2094
9211
/**
2095
9212
  Generate minimal set of simple equalities equivalent to a multiple equality.
2096
9213
 
2130
9247
    a pointer to the simple generated equality, if success.
2131
9248
    - 0, otherwise.
2132
9249
*/
2133
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9250
 
 
9251
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9252
                                  Item_equal *item_equal)
2134
9253
{
2135
9254
  List<Item> eq_list;
2136
9255
  Item_func_eq *eq_item= 0;
2137
9256
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2138
 
    return new Item_int((int64_t) 0,1);
 
9257
    return new Item_int((longlong) 0,1); 
2139
9258
  Item *item_const= item_equal->get_const();
2140
9259
  Item_equal_iterator it(*item_equal);
2141
9260
  Item *head;
2152
9271
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2153
9272
    Item_field *item= item_field;
2154
9273
    if (upper)
2155
 
    {
 
9274
    { 
2156
9275
      if (item_const && upper->get_const())
2157
9276
        item= 0;
2158
9277
      else
2180
9299
  if (!cond && !eq_list.head())
2181
9300
  {
2182
9301
    if (!eq_item)
2183
 
      return new Item_int((int64_t) 1,1);
 
9302
      return new Item_int((longlong) 1,1);
2184
9303
    return eq_item;
2185
9304
  }
2186
9305
 
2196
9315
 
2197
9316
  cond->quick_fix_field();
2198
9317
  cond->update_used_tables();
2199
 
 
 
9318
   
2200
9319
  return cond;
2201
9320
}
2202
9321
 
 
9322
 
2203
9323
/**
2204
9324
  Substitute every field reference in a condition by the best equal field
2205
9325
  and eliminate all multiple equality predicates.
2208
9328
    multiple equality predicate it sorts the field references in it
2209
9329
    according to the order of tables specified by the table_join_idx
2210
9330
    parameter. Then it eliminates the multiple equality predicate it
2211
 
    replacing it by the conjunction of simple equality predicates
 
9331
    replacing it by the conjunction of simple equality predicates 
2212
9332
    equating every field from the multiple equality to the first
2213
9333
    field in it, or to the constant, if there is any.
2214
9334
    After this the function retrieves all other conjuncted
2227
9347
  @return
2228
9348
    The transformed condition
2229
9349
*/
2230
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9350
 
 
9351
static COND* substitute_for_best_equal_field(COND *cond,
 
9352
                                             COND_EQUAL *cond_equal,
 
9353
                                             void *table_join_idx)
2231
9354
{
2232
9355
  Item_equal *item_equal;
2233
9356
 
2242
9365
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2243
9366
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2244
9367
 
2245
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
9368
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2246
9369
      while ((item_equal= it++))
2247
9370
      {
2248
9371
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2249
9372
      }
2250
9373
    }
2251
 
 
 
9374
    
2252
9375
    List_iterator<Item> li(*cond_list);
2253
9376
    Item *item;
2254
9377
    while ((item= li++))
2278
9401
    }
2279
9402
    if (cond->type() == Item::COND_ITEM &&
2280
9403
        !((Item_cond*)cond)->argument_list()->elements)
2281
 
      cond= new Item_int((int32_t)cond->val_bool());
 
9404
      cond= new Item_int((int32)cond->val_bool());
2282
9405
 
2283
9406
  }
2284
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9407
  else if (cond->type() == Item::FUNC_ITEM && 
2285
9408
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2286
9409
  {
2287
9410
    item_equal= (Item_equal *) cond;
2295
9418
  return cond;
2296
9419
}
2297
9420
 
 
9421
 
2298
9422
/**
2299
9423
  Check appearance of new constant items in multiple equalities
2300
9424
  of a condition after reading a constant table.
2307
9431
  @param cond       condition whose multiple equalities are to be checked
2308
9432
  @param table      constant table that has been read
2309
9433
*/
2310
 
static void update_const_equal_items(COND *cond, JoinTable *tab)
 
9434
 
 
9435
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2311
9436
{
2312
9437
  if (!(cond->used_tables() & tab->table->map))
2313
9438
    return;
2314
9439
 
2315
9440
  if (cond->type() == Item::COND_ITEM)
2316
9441
  {
2317
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
 
9442
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
2318
9443
    List_iterator_fast<Item> li(*cond_list);
2319
9444
    Item *item;
2320
9445
    while ((item= li++))
2321
9446
      update_const_equal_items(item, tab);
2322
9447
  }
2323
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9448
  else if (cond->type() == Item::FUNC_ITEM && 
2324
9449
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2325
9450
  {
2326
9451
    Item_equal *item_equal= (Item_equal *) cond;
2334
9459
      while ((item_field= it++))
2335
9460
      {
2336
9461
        Field *field= item_field->field;
2337
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9462
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2338
9463
        key_map possible_keys= field->key_start;
2339
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2340
 
        stat[0].const_keys|= possible_keys;
 
9464
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9465
        stat[0].const_keys.merge(possible_keys);
2341
9466
 
2342
9467
        /*
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
 
9468
          For each field in the multiple equality (for which we know that it 
 
9469
          is a constant) we have to find its corresponding key part, and set 
2345
9470
          that key part in const_key_parts.
2346
 
        */
2347
 
        if (possible_keys.any())
 
9471
        */  
 
9472
        if (!possible_keys.is_clear_all())
2348
9473
        {
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 ==
 
9474
          TABLE *tab= field->table;
 
9475
          KEYUSE *use;
 
9476
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9477
            if (possible_keys.is_set(use->key) && 
 
9478
                tab->key_info[use->key].key_part[use->keypart].field ==
2354
9479
                field)
2355
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9480
              tab->const_key_parts[use->key]|= use->keypart_map;
2356
9481
        }
2357
9482
      }
2358
9483
    }
2359
9484
  }
2360
9485
}
2361
9486
 
 
9487
 
2362
9488
/*
2363
9489
  change field = field to field = const for each found field = const in the
2364
9490
  and_level
2365
9491
*/
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)
 
9492
 
 
9493
static void
 
9494
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9495
                         Item *and_father, Item *cond,
 
9496
                         Item *field, Item *value)
2372
9497
{
2373
9498
  if (cond->type() == Item::COND_ITEM)
2374
9499
  {
2375
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9500
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9501
      Item_func::COND_AND_FUNC;
2376
9502
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2377
9503
    Item *item;
2378
9504
    while ((item=li++))
2379
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2380
 
 
 
9505
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9506
                               field, value);
2381
9507
    return;
2382
9508
  }
2383
9509
  if (cond->eq_cmp_result() == Item::COND_OK)
2396
9522
       left_item->collation.collation == value->collation.collation))
2397
9523
  {
2398
9524
    Item *tmp=value->clone_item();
 
9525
    tmp->collation.set(right_item->collation);
 
9526
    
2399
9527
    if (tmp)
2400
9528
    {
2401
 
      tmp->collation.set(right_item->collation);
2402
 
      session->change_item_tree(args + 1, tmp);
 
9529
      thd->change_item_tree(args + 1, tmp);
2403
9530
      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())
 
9531
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9532
          && and_father != cond && !left_item->const_item())
2407
9533
      {
2408
 
        cond->marker=1;
2409
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9534
        cond->marker=1;
 
9535
        COND_CMP *tmp2;
 
9536
        if ((tmp2=new COND_CMP(and_father,func)))
 
9537
          save_list->push_back(tmp2);
2410
9538
      }
2411
9539
      func->set_cmp_func();
2412
9540
    }
2418
9546
            right_item->collation.collation == value->collation.collation))
2419
9547
  {
2420
9548
    Item *tmp= value->clone_item();
 
9549
    tmp->collation.set(left_item->collation);
 
9550
    
2421
9551
    if (tmp)
2422
9552
    {
2423
 
      tmp->collation.set(left_item->collation);
2424
 
      session->change_item_tree(args, tmp);
 
9553
      thd->change_item_tree(args, tmp);
2425
9554
      value= tmp;
2426
9555
      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())
 
9556
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9557
          && and_father != cond && !right_item->const_item())
2430
9558
      {
2431
9559
        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) );
 
9560
        thd->change_item_tree(args + 1, value);
 
9561
        cond->marker=1;
 
9562
        COND_CMP *tmp2;
 
9563
        if ((tmp2=new COND_CMP(and_father,func)))
 
9564
          save_list->push_back(tmp2);
2435
9565
      }
2436
9566
      func->set_cmp_func();
2437
9567
    }
2446
9576
  @return
2447
9577
    new conditions
2448
9578
*/
2449
 
Item *remove_additional_cond(Item* conds)
 
9579
 
 
9580
static Item *remove_additional_cond(Item* conds)
2450
9581
{
2451
9582
  if (conds->name == in_additional_cond)
2452
9583
    return 0;
2469
9600
  return conds;
2470
9601
}
2471
9602
 
2472
 
static void propagate_cond_constants(Session *session, 
2473
 
                                     vector<COND_CMP>& save_list, 
2474
 
                                     COND *and_father, 
2475
 
                                     COND *cond)
 
9603
static void
 
9604
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9605
                         COND *and_father, COND *cond)
2476
9606
{
2477
9607
  if (cond->type() == Item::COND_ITEM)
2478
9608
  {
2479
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9609
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9610
      Item_func::COND_AND_FUNC;
2480
9611
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2481
9612
    Item *item;
2482
 
    vector<COND_CMP> save;
 
9613
    I_List<COND_CMP> save;
2483
9614
    while ((item=li++))
2484
9615
    {
2485
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9616
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2486
9617
    }
2487
9618
    if (and_level)
2488
 
    {
2489
 
      // Handle other found items
2490
 
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9619
    {                                           // Handle other found items
 
9620
      I_List_iterator<COND_CMP> cond_itr(save);
 
9621
      COND_CMP *cond_cmp;
 
9622
      while ((cond_cmp=cond_itr++))
2491
9623
      {
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
 
        }
 
9624
        Item **args= cond_cmp->cmp_func->arguments();
 
9625
        if (!args[0]->const_item())
 
9626
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9627
                                   cond_cmp->and_level, args[0], args[1]);
2498
9628
      }
2499
9629
    }
2500
9630
  }
2501
9631
  else if (and_father != cond && !cond->marker)         // In a AND group
2502
9632
  {
2503
9633
    if (cond->type() == Item::FUNC_ITEM &&
2504
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2505
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9634
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9635
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2506
9636
    {
2507
9637
      Item_func_eq *func=(Item_func_eq*) cond;
2508
9638
      Item **args= func->arguments();
2511
9641
      if (!(left_const && right_const) &&
2512
9642
          args[0]->result_type() == args[1]->result_type())
2513
9643
      {
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
 
}
 
9644
        if (right_const)
 
9645
        {
 
9646
          resolve_const_item(thd, &args[1], args[0]);
 
9647
          func->update_used_tables();
 
9648
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9649
                                   args[0], args[1]);
 
9650
        }
 
9651
        else if (left_const)
 
9652
        {
 
9653
          resolve_const_item(thd, &args[0], args[1]);
 
9654
          func->update_used_tables();
 
9655
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9656
                                   args[1], args[0]);
 
9657
        }
 
9658
      }
 
9659
    }
 
9660
  }
 
9661
}
 
9662
 
 
9663
 
 
9664
/**
 
9665
  Simplify joins replacing outer joins by inner joins whenever it's
 
9666
  possible.
 
9667
 
 
9668
    The function, during a retrieval of join_list,  eliminates those
 
9669
    outer joins that can be converted into inner join, possibly nested.
 
9670
    It also moves the on expressions for the converted outer joins
 
9671
    and from inner joins to conds.
 
9672
    The function also calculates some attributes for nested joins:
 
9673
    - used_tables    
 
9674
    - not_null_tables
 
9675
    - dep_tables.
 
9676
    - on_expr_dep_tables
 
9677
    The first two attributes are used to test whether an outer join can
 
9678
    be substituted for an inner join. The third attribute represents the
 
9679
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9680
    on table t1, then in any evaluated execution plan table access to
 
9681
    table t2 must precede access to table t2. This relation is used also
 
9682
    to check whether the query contains  invalid cross-references.
 
9683
    The forth attribute is an auxiliary one and is used to calculate
 
9684
    dep_tables.
 
9685
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9686
    execution plan, the dependencies required by the straight join
 
9687
    modifiers are reflected in this attribute as well.
 
9688
    The function also removes all braces that can be removed from the join
 
9689
    expression without changing its meaning.
 
9690
 
 
9691
  @note
 
9692
    An outer join can be replaced by an inner join if the where condition
 
9693
    or the on expression for an embedding nested join contains a conjunctive
 
9694
    predicate rejecting null values for some attribute of the inner tables.
 
9695
 
 
9696
    E.g. in the query:    
 
9697
    @code
 
9698
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9699
    @endcode
 
9700
    the predicate t2.b < 5 rejects nulls.
 
9701
    The query is converted first to:
 
9702
    @code
 
9703
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9704
    @endcode
 
9705
    then to the equivalent form:
 
9706
    @code
 
9707
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9708
    @endcode
 
9709
 
 
9710
 
 
9711
    Similarly the following query:
 
9712
    @code
 
9713
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9714
        WHERE t2.c < 5  
 
9715
    @endcode
 
9716
    is converted to:
 
9717
    @code
 
9718
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9719
 
 
9720
    @endcode
 
9721
 
 
9722
    One conversion might trigger another:
 
9723
    @code
 
9724
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9725
                       LEFT JOIN t3 ON t3.b=t2.b
 
9726
        WHERE t3 IS NOT NULL =>
 
9727
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9728
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9729
      SELECT * FROM t1, t2, t3
 
9730
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9731
  @endcode
 
9732
 
 
9733
    The function removes all unnecessary braces from the expression
 
9734
    produced by the conversions.
 
9735
    E.g.
 
9736
    @code
 
9737
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9738
    @endcode
 
9739
    finally is converted to: 
 
9740
    @code
 
9741
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9742
 
 
9743
    @endcode
 
9744
 
 
9745
 
 
9746
    It also will remove braces from the following queries:
 
9747
    @code
 
9748
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9749
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9750
    @endcode
 
9751
 
 
9752
    The benefit of this simplification procedure is that it might return 
 
9753
    a query for which the optimizer can evaluate execution plan with more
 
9754
    join orders. With a left join operation the optimizer does not
 
9755
    consider any plan where one of the inner tables is before some of outer
 
9756
    tables.
 
9757
 
 
9758
  IMPLEMENTATION
 
9759
    The function is implemented by a recursive procedure.  On the recursive
 
9760
    ascent all attributes are calculated, all outer joins that can be
 
9761
    converted are replaced and then all unnecessary braces are removed.
 
9762
    As join list contains join tables in the reverse order sequential
 
9763
    elimination of outer joins does not require extra recursive calls.
 
9764
 
 
9765
  SEMI-JOIN NOTES
 
9766
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9767
    an "ancestor" semi-join nest)
 
9768
 
 
9769
  EXAMPLES
 
9770
    Here is an example of a join query with invalid cross references:
 
9771
    @code
 
9772
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9773
    @endcode
 
9774
 
 
9775
  @param join        reference to the query info
 
9776
  @param join_list   list representation of the join to be converted
 
9777
  @param conds       conditions to add on expressions for converted joins
 
9778
  @param top         true <=> conds is the where condition
 
9779
 
 
9780
  @return
 
9781
    - The new condition, if success
 
9782
    - 0, otherwise
 
9783
*/
 
9784
 
 
9785
static COND *
 
9786
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
 
9787
               bool in_sj)
 
9788
{
 
9789
  TABLE_LIST *table;
 
9790
  NESTED_JOIN *nested_join;
 
9791
  TABLE_LIST *prev_table= 0;
 
9792
  List_iterator<TABLE_LIST> li(*join_list);
 
9793
 
 
9794
  /* 
 
9795
    Try to simplify join operations from join_list.
 
9796
    The most outer join operation is checked for conversion first. 
 
9797
  */
 
9798
  while ((table= li++))
 
9799
  {
 
9800
    table_map used_tables;
 
9801
    table_map not_null_tables= (table_map) 0;
 
9802
 
 
9803
    if ((nested_join= table->nested_join))
 
9804
    {
 
9805
      /* 
 
9806
         If the element of join_list is a nested join apply
 
9807
         the procedure to its nested join list first.
 
9808
      */
 
9809
      if (table->on_expr)
 
9810
      {
 
9811
        Item *expr= table->on_expr;
 
9812
        /* 
 
9813
           If an on expression E is attached to the table, 
 
9814
           check all null rejected predicates in this expression.
 
9815
           If such a predicate over an attribute belonging to
 
9816
           an inner table of an embedded outer join is found,
 
9817
           the outer join is converted to an inner join and
 
9818
           the corresponding on expression is added to E. 
 
9819
        */ 
 
9820
        expr= simplify_joins(join, &nested_join->join_list,
 
9821
                             expr, false, in_sj || table->sj_on_expr);
 
9822
 
 
9823
        if (!table->prep_on_expr || expr != table->on_expr)
 
9824
        {
 
9825
          assert(expr);
 
9826
 
 
9827
          table->on_expr= expr;
 
9828
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9829
        }
 
9830
      }
 
9831
      nested_join->used_tables= (table_map) 0;
 
9832
      nested_join->not_null_tables=(table_map) 0;
 
9833
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9834
                            in_sj || table->sj_on_expr);
 
9835
      used_tables= nested_join->used_tables;
 
9836
      not_null_tables= nested_join->not_null_tables;  
 
9837
    }
 
9838
    else
 
9839
    {
 
9840
      if (!table->prep_on_expr)
 
9841
        table->prep_on_expr= table->on_expr;
 
9842
      used_tables= table->table->map;
 
9843
      if (conds)
 
9844
        not_null_tables= conds->not_null_tables();
 
9845
    }
 
9846
      
 
9847
    if (table->embedding)
 
9848
    {
 
9849
      table->embedding->nested_join->used_tables|= used_tables;
 
9850
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9851
    }
 
9852
 
 
9853
    if (!table->outer_join || (used_tables & not_null_tables))
 
9854
    {
 
9855
      /* 
 
9856
        For some of the inner tables there are conjunctive predicates
 
9857
        that reject nulls => the outer join can be replaced by an inner join.
 
9858
      */
 
9859
      table->outer_join= 0;
 
9860
      if (table->on_expr)
 
9861
      {
 
9862
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9863
        if (conds)
 
9864
        {
 
9865
          conds= and_conds(conds, table->on_expr);
 
9866
          conds->top_level_item();
 
9867
          /* conds is always a new item as both cond and on_expr existed */
 
9868
          assert(!conds->fixed);
 
9869
          conds->fix_fields(join->thd, &conds);
 
9870
        }
 
9871
        else
 
9872
          conds= table->on_expr; 
 
9873
        table->prep_on_expr= table->on_expr= 0;
 
9874
      }
 
9875
    }
 
9876
    
 
9877
    if (!top)
 
9878
      continue;
 
9879
 
 
9880
    /* 
 
9881
      Only inner tables of non-convertible outer joins
 
9882
      remain with on_expr.
 
9883
    */ 
 
9884
    if (table->on_expr)
 
9885
    {
 
9886
      table->dep_tables|= table->on_expr->used_tables(); 
 
9887
      if (table->embedding)
 
9888
      {
 
9889
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9890
        /*
 
9891
           Embedding table depends on tables used
 
9892
           in embedded on expressions. 
 
9893
        */
 
9894
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9895
      }
 
9896
      else
 
9897
        table->dep_tables&= ~table->table->map;
 
9898
    }
 
9899
 
 
9900
    if (prev_table)
 
9901
    {
 
9902
      /* The order of tables is reverse: prev_table follows table */
 
9903
      if (prev_table->straight)
 
9904
        prev_table->dep_tables|= used_tables;
 
9905
      if (prev_table->on_expr)
 
9906
      {
 
9907
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9908
        table_map prev_used_tables= prev_table->nested_join ?
 
9909
                                    prev_table->nested_join->used_tables :
 
9910
                                    prev_table->table->map;
 
9911
        /* 
 
9912
          If on expression contains only references to inner tables
 
9913
          we still make the inner tables dependent on the outer tables.
 
9914
          It would be enough to set dependency only on one outer table
 
9915
          for them. Yet this is really a rare case.
 
9916
        */  
 
9917
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9918
          prev_table->dep_tables|= used_tables;
 
9919
      }
 
9920
    }
 
9921
    prev_table= table;
 
9922
  }
 
9923
    
 
9924
  /* 
 
9925
    Flatten nested joins that can be flattened.
 
9926
    no ON expression and not a semi-join => can be flattened.
 
9927
  */
 
9928
  li.rewind();
 
9929
  while ((table= li++))
 
9930
  {
 
9931
    nested_join= table->nested_join;
 
9932
    if (table->sj_on_expr && !in_sj)
 
9933
    {
 
9934
       /*
 
9935
         If this is a semi-join that is not contained within another semi-join, 
 
9936
         leave it intact (otherwise it is flattened)
 
9937
       */
 
9938
      join->select_lex->sj_nests.push_back(table);
 
9939
    }
 
9940
    else if (nested_join && !table->on_expr)
 
9941
    {
 
9942
      TABLE_LIST *tbl;
 
9943
      List_iterator<TABLE_LIST> it(nested_join->join_list);
 
9944
      while ((tbl= it++))
 
9945
      {
 
9946
        tbl->embedding= table->embedding;
 
9947
        tbl->join_list= table->join_list;
 
9948
      }      
 
9949
      li.replace(nested_join->join_list);
 
9950
    }
 
9951
  }
 
9952
  return(conds); 
 
9953
}
 
9954
 
 
9955
 
 
9956
/**
 
9957
  Assign each nested join structure a bit in nested_join_map.
 
9958
 
 
9959
    Assign each nested join structure (except "confluent" ones - those that
 
9960
    embed only one element) a bit in nested_join_map.
 
9961
 
 
9962
  @param join          Join being processed
 
9963
  @param join_list     List of tables
 
9964
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9965
                       call
 
9966
 
 
9967
  @note
 
9968
    This function is called after simplify_joins(), when there are no
 
9969
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9970
    we will not run out of bits in nested_join_map.
 
9971
 
 
9972
  @return
 
9973
    First unused bit in nested_join_map after the call.
 
9974
*/
 
9975
 
 
9976
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
9977
                                          uint first_unused)
 
9978
{
 
9979
  List_iterator<TABLE_LIST> li(*join_list);
 
9980
  TABLE_LIST *table;
 
9981
  while ((table= li++))
 
9982
  {
 
9983
    NESTED_JOIN *nested_join;
 
9984
    if ((nested_join= table->nested_join))
 
9985
    {
 
9986
      /*
 
9987
        It is guaranteed by simplify_joins() function that a nested join
 
9988
        that has only one child is either
 
9989
         - a single-table view (the child is the underlying table), or 
 
9990
         - a single-table semi-join nest
 
9991
 
 
9992
        We don't assign bits to such sj-nests because 
 
9993
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9994
            with anything)
 
9995
        2. we could run out bits in nested_join_map otherwise.
 
9996
      */
 
9997
      if (nested_join->join_list.elements != 1)
 
9998
      {
 
9999
        /* Don't assign bits to sj-nests */
 
10000
        if (table->on_expr)
 
10001
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
10002
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
10003
                                                    first_unused);
 
10004
      }
 
10005
    }
 
10006
  }
 
10007
  return(first_unused);
 
10008
}
 
10009
 
 
10010
 
 
10011
/**
 
10012
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
 
10013
 
 
10014
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
 
10015
    the passed join_list.
 
10016
 
 
10017
  @param join_list  List of nested joins to process. It may also contain base
 
10018
                    tables which will be ignored.
 
10019
*/
 
10020
 
 
10021
static void reset_nj_counters(List<TABLE_LIST> *join_list)
 
10022
{
 
10023
  List_iterator<TABLE_LIST> li(*join_list);
 
10024
  TABLE_LIST *table;
 
10025
  while ((table= li++))
 
10026
  {
 
10027
    NESTED_JOIN *nested_join;
 
10028
    if ((nested_join= table->nested_join))
 
10029
    {
 
10030
      nested_join->counter_= 0;
 
10031
      reset_nj_counters(&nested_join->join_list);
 
10032
    }
 
10033
  }
 
10034
  return;
 
10035
}
 
10036
 
2532
10037
 
2533
10038
/**
2534
10039
  Check interleaving with an inner tables of an outer join for
2535
10040
  extension table.
2536
10041
 
2537
 
    Check if table next_tab can be added to current partial join order, and
 
10042
    Check if table next_tab can be added to current partial join order, and 
2538
10043
    if yes, record that it has been added.
2539
10044
 
2540
10045
    The function assumes that both current partial join order and its
2541
10046
    extension with next_tab are valid wrt table dependencies.
2542
10047
 
2543
10048
  @verbatim
2544
 
     IMPLEMENTATION
2545
 
       LIMITATIONS ON JOIN order_st
 
10049
     IMPLEMENTATION 
 
10050
       LIMITATIONS ON JOIN ORDER
2546
10051
         The nested [outer] joins executioner algorithm imposes these limitations
2547
10052
         on join order:
2548
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10053
         1. "Outer tables first" -  any "outer" table must be before any 
2549
10054
             corresponding "inner" table.
2550
10055
         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
 
10056
            sequence in join order (i.e. the sequence must not be interrupted by 
2552
10057
            tables that are outside of this nested join).
2553
10058
 
2554
10059
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2555
10060
         been already checked.
2556
10061
 
2557
10062
       WHY NEED NON-INTERLEAVING
2558
 
         Consider an example:
 
10063
         Consider an example: 
2559
10064
 
2560
10065
           select * from t0 join t1 left join (t2 join t3) on cond1
2561
10066
 
2579
10084
         The limitations on join order can be rephrased as follows: for valid
2580
10085
         join order one must be able to:
2581
10086
           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
 
10087
           2. for each nested join, put one '(' and one ')' on the said line        
2583
10088
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2584
10089
           4. get a query equivalent to the query we're trying to execute.
2585
10090
 
2586
10091
         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
 
10092
         above described line from left to right. 
 
10093
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2589
10094
         table B and appropriate brackets on condition that table A and
2590
10095
         appropriate brackets is the last what was written. Graphically the
2591
10096
         transition is as follows:
2598
10103
                                                      position.
2599
10104
 
2600
10105
         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
 
10106
           The caller guarantees that there is no more then one X-bracket by 
 
10107
           checking "!(remaining_tables & s->dependent)" before calling this 
2603
10108
           function. X-bracket may have a pair in Y-bracket.
2604
10109
 
2605
10110
         When "writing" we store/update this auxilary info about the current
2606
10111
         position:
2607
10112
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2608
10113
             joins) we've opened but didn't close.
2609
 
          2. {each nested_join_st structure not simplified away}->counter - number
 
10114
          2. {each NESTED_JOIN structure not simplified away}->counter - number
2610
10115
             of this nested join's children that have already been added to to
2611
10116
             the partial join order.
2612
10117
  @endverbatim
2613
10118
 
2614
10119
  @param join       Join being processed
 
10120
  @param last_tab   Last table in current partial join order (this function is
 
10121
                    not called for empty partial join orders)
2615
10122
  @param next_tab   Table we're going to extend the current partial join with
2616
10123
 
2617
10124
  @retval
2620
10127
  @retval
2621
10128
    true   Requested join order extension not allowed.
2622
10129
*/
2623
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10130
 
 
10131
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2624
10132
{
2625
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2626
 
  Join *join= next_tab->join;
 
10133
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10134
  JOIN *join= last_tab->join;
2627
10135
 
2628
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10136
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2629
10137
  {
2630
 
    /*
 
10138
    /* 
2631
10139
      next_tab is outside of the "pair of brackets" we're currently in.
2632
10140
      Cannot add it.
2633
10141
    */
2634
10142
    return true;
2635
10143
  }
2636
 
 
 
10144
   
2637
10145
  /*
2638
10146
    Do update counters for "pairs of brackets" that we've left (marked as
2639
10147
    X,Y,Z in the above picture)
2640
10148
  */
2641
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10149
  for (;next_emb; next_emb= next_emb->embedding)
2642
10150
  {
2643
 
    next_emb->getNestedJoin()->counter_++;
2644
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10151
    next_emb->nested_join->counter_++;
 
10152
    if (next_emb->nested_join->counter_ == 1)
2645
10153
    {
2646
 
      /*
 
10154
      /* 
2647
10155
        next_emb is the first table inside a nested join we've "entered". In
2648
10156
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2649
10157
        X bracket might have Y pair bracket.
2650
10158
      */
2651
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10159
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2652
10160
    }
2653
 
 
2654
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2655
 
        next_emb->getNestedJoin()->counter_)
 
10161
    
 
10162
    if (next_emb->nested_join->join_list.elements !=
 
10163
        next_emb->nested_join->counter_)
2656
10164
      break;
2657
10165
 
2658
10166
    /*
2659
10167
      We're currently at Y or Z-bracket as depicted in the above picture.
2660
10168
      Mark that we've left it and continue walking up the brackets hierarchy.
2661
10169
    */
2662
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10170
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2663
10171
  }
2664
10172
  return false;
2665
10173
}
2666
10174
 
2667
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2668
 
{
2669
 
  Session *session= join->session;
 
10175
 
 
10176
/**
 
10177
  Nested joins perspective: Remove the last table from the join order.
 
10178
 
 
10179
    Remove the last table from the partial join order and update the nested
 
10180
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10181
    function for the first table in join order (for which 
 
10182
    check_interleaving_with_nj has not been called)
 
10183
 
 
10184
  @param last  join table to remove, it is assumed to be the last in current
 
10185
               partial join order.
 
10186
*/
 
10187
 
 
10188
static void restore_prev_nj_state(JOIN_TAB *last)
 
10189
{
 
10190
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
 
10191
  JOIN *join= last->join;
 
10192
  while (last_emb)
 
10193
  {
 
10194
    if (last_emb->on_expr)
 
10195
    {
 
10196
      if (!(--last_emb->nested_join->counter_))
 
10197
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10198
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10199
               last_emb->nested_join->counter_) 
 
10200
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10201
      else
 
10202
        break;
 
10203
    }
 
10204
    last_emb= last_emb->embedding;
 
10205
  }
 
10206
}
 
10207
 
 
10208
 
 
10209
 
 
10210
static 
 
10211
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10212
{
 
10213
  TABLE_LIST *emb_sj_nest;
 
10214
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10215
  {
 
10216
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10217
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10218
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10219
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10220
  }
 
10221
}
 
10222
 
 
10223
 
 
10224
/*
 
10225
  we assume remaining_tables doesnt contain @tab.
 
10226
*/
 
10227
 
 
10228
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10229
                                  const JOIN_TAB *tab)
 
10230
{
 
10231
  TABLE_LIST *emb_sj_nest;
 
10232
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10233
  {
 
10234
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10235
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10236
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10237
    {
 
10238
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10239
    }
 
10240
  }
 
10241
}
 
10242
 
 
10243
 
 
10244
static COND *
 
10245
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
 
10246
              Item::cond_result *cond_value)
 
10247
{
 
10248
  THD *thd= join->thd;
2670
10249
 
2671
10250
  if (!conds)
2672
10251
    *cond_value= Item::COND_TRUE;
2673
10252
  else
2674
10253
  {
2675
 
    /*
 
10254
    /* 
2676
10255
      Build all multiple equality predicates and eliminate equality
2677
10256
      predicates that can be inferred from these multiple equalities.
2678
10257
      For each reference of a field included into a multiple equality
2679
10258
      that occurs in a function set a pointer to the multiple equality
2680
10259
      predicate. Substitute a constant instead of this field if the
2681
10260
      multiple equality contains a constant.
2682
 
    */
2683
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10261
    */ 
 
10262
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2684
10263
                             &join->cond_equal);
2685
10264
 
2686
10265
    /* change field = field to field = const for each found field = const */
2687
 
    vector<COND_CMP> temp;
2688
 
    propagate_cond_constants(session, temp, conds, conds);
 
10266
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2689
10267
    /*
2690
10268
      Remove all instances of item == item
2691
10269
      Remove all and-levels where CONST item != CONST item
2692
10270
    */
2693
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10271
    conds= remove_eq_conds(thd, conds, cond_value) ;
2694
10272
  }
2695
10273
  return(conds);
2696
10274
}
2697
10275
 
 
10276
 
2698
10277
/**
2699
10278
  Remove const and eq items.
2700
10279
 
2705
10284
    - COND_TRUE   : always true ( 1 = 1 )
2706
10285
    - COND_FALSE  : always false        ( 1 = 2 )
2707
10286
*/
2708
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10287
 
 
10288
COND *
 
10289
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2709
10290
{
2710
10291
  if (cond->type() == Item::COND_ITEM)
2711
10292
  {
2712
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2713
 
 
 
10293
    bool and_level= ((Item_cond*) cond)->functype()
 
10294
      == Item_func::COND_AND_FUNC;
2714
10295
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2715
10296
    Item::cond_result tmp_cond_value;
2716
 
    bool should_fix_fields= false;
 
10297
    bool should_fix_fields=0;
2717
10298
 
2718
 
    *cond_value= Item::COND_UNDEF;
 
10299
    *cond_value=Item::COND_UNDEF;
2719
10300
    Item *item;
2720
 
    while ((item= li++))
 
10301
    while ((item=li++))
2721
10302
    {
2722
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2723
 
      if (! new_item)
2724
 
              li.remove();
 
10303
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10304
      if (!new_item)
 
10305
        li.remove();
2725
10306
      else if (item != new_item)
2726
10307
      {
2727
 
        li.replace(new_item);
2728
 
        should_fix_fields= true;
 
10308
        VOID(li.replace(new_item));
 
10309
        should_fix_fields=1;
2729
10310
      }
2730
10311
      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;
 
10312
        *cond_value=tmp_cond_value;
 
10313
      switch (tmp_cond_value) {
 
10314
      case Item::COND_OK:                       // Not true or false
 
10315
        if (and_level || *cond_value == Item::COND_FALSE)
 
10316
          *cond_value=tmp_cond_value;
 
10317
        break;
 
10318
      case Item::COND_FALSE:
 
10319
        if (and_level)
 
10320
        {
 
10321
          *cond_value=tmp_cond_value;
 
10322
          return (COND*) 0;                     // Always false
 
10323
        }
 
10324
        break;
 
10325
      case Item::COND_TRUE:
 
10326
        if (!and_level)
 
10327
        {
 
10328
          *cond_value= tmp_cond_value;
 
10329
          return (COND*) 0;                     // Always true
 
10330
        }
 
10331
        break;
 
10332
      case Item::COND_UNDEF:                    // Impossible
 
10333
        break; /* purecov: deadcode */
2755
10334
      }
2756
10335
    }
2757
 
 
2758
10336
    if (should_fix_fields)
2759
10337
      cond->update_used_tables();
2760
10338
 
2761
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2762
 
      return (COND*) NULL;
2763
 
 
 
10339
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10340
        *cond_value != Item::COND_OK)
 
10341
      return (COND*) 0;
2764
10342
    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 */
 
10343
    {                                           // Remove list
2767
10344
      item= ((Item_cond*) cond)->argument_list()->head();
2768
10345
      ((Item_cond*) cond)->argument_list()->empty();
2769
10346
      return item;
2770
10347
    }
2771
10348
  }
2772
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10349
  else if (cond->type() == Item::FUNC_ITEM &&
 
10350
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2773
10351
  {
2774
10352
    /*
2775
10353
      Handles this special case for some ODBC applications:
2781
10359
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2782
10360
    */
2783
10361
 
2784
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10362
    Item_func_isnull *func=(Item_func_isnull*) cond;
2785
10363
    Item **args= func->arguments();
2786
10364
    if (args[0]->type() == Item::FIELD_ITEM)
2787
10365
    {
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
 
          )
 
10366
      Field *field=((Item_field*) args[0])->field;
 
10367
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10368
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10369
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10370
           thd->substitute_null_with_insert_id))
2797
10371
      {
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;
 
10372
        COND *new_cond;
 
10373
        if ((new_cond= new Item_func_eq(args[0],
 
10374
                                        new Item_int("last_insert_id()",
 
10375
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10376
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10377
        {
 
10378
          cond=new_cond;
2804
10379
          /*
2805
10380
            Item_func_eq can't be fixed after creation so we do not check
2806
10381
            cond->fixed, also it do not need tables so we use 0 as second
2807
10382
            argument.
2808
10383
          */
2809
 
          cond->fix_fields(session, &cond);
2810
 
        }
 
10384
          cond->fix_fields(thd, &cond);
 
10385
        }
2811
10386
        /*
2812
10387
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2813
10388
          clear for next row
2814
10389
        */
2815
 
        session->substitute_null_with_insert_id= false;
 
10390
        thd->substitute_null_with_insert_id= false;
2816
10391
      }
2817
 
#ifdef NOTDEFINED
2818
10392
      /* 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)
 
10393
      else if (((field->type() == MYSQL_TYPE_NEWDATE) ||
 
10394
                (field->type() == MYSQL_TYPE_DATETIME)) &&
 
10395
                (field->flags & NOT_NULL_FLAG) &&
 
10396
               !field->table->maybe_null)
2823
10397
      {
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;
 
10398
        COND *new_cond;
 
10399
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10400
        {
 
10401
          cond=new_cond;
2828
10402
          /*
2829
10403
            Item_func_eq can't be fixed after creation so we do not check
2830
10404
            cond->fixed, also it do not need tables so we use 0 as second
2831
10405
            argument.
2832
10406
          */
2833
 
          cond->fix_fields(session, &cond);
2834
 
        }
 
10407
          cond->fix_fields(thd, &cond);
 
10408
        }
2835
10409
      }
2836
 
#endif /* NOTDEFINED */
2837
10410
    }
2838
10411
    if (cond->const_item())
2839
10412
    {
2840
10413
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2841
 
      return (COND *) NULL;
 
10414
      return (COND*) 0;
2842
10415
    }
2843
10416
  }
2844
10417
  else if (cond->const_item() && !cond->is_expensive())
2845
10418
  /*
2846
10419
    TODO:
2847
10420
    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
 
 
 
10421
    materialized IN because it is created later than this phase, and cannot be
 
10422
    evaluated at this point.
 
10423
    The condition should be something as (need to fix member access):
 
10424
      !(cond->type() == Item::FUNC_ITEM &&
 
10425
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10426
        ((Item_in_optimizer*)cond)->is_expensive()))
2854
10427
  */
2855
10428
  {
2856
10429
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2857
 
    return (COND *) NULL;
 
10430
    return (COND*) 0;
2858
10431
  }
2859
10432
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2860
 
  {                                             
2861
 
    /* boolan compare function */
 
10433
  {                                             // boolan compare function
2862
10434
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2863
10435
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2864
10436
    if (left_item->eq(right_item,1))
2865
10437
    {
2866
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2867
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10438
      if (!left_item->maybe_null ||
 
10439
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10440
        return (COND*) 0;                       // Compare of identical items
2868
10441
    }
2869
10442
  }
2870
 
  *cond_value= Item::COND_OK;
2871
 
  return cond;                                  /* Point at next and return into recursion */
 
10443
  *cond_value=Item::COND_OK;
 
10444
  return cond;                                  // Point at next and level
2872
10445
}
2873
10446
 
2874
 
/*
 
10447
/* 
2875
10448
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2876
 
 
 
10449
  
2877
10450
  SYNOPSIS
2878
10451
    test_if_equality_guarantees_uniqueness()
2879
10452
      l          the left comparison argument (a field if any)
2880
10453
      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
 
10454
  
 
10455
  DESCRIPTION    
 
10456
    Checks if an equality predicate can be used to take away 
 
10457
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2885
10458
    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
 
10459
    Arguments must be of the same type because e.g. 
 
10460
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10461
    the column. 
 
10462
    We must take into consideration and the optimization done for various 
2890
10463
    string constants when compared to dates etc (see Item_int_with_ref) as
2891
10464
    well as the collation of the arguments.
2892
 
 
2893
 
  RETURN VALUE
 
10465
  
 
10466
  RETURN VALUE  
2894
10467
    true    can be used
2895
10468
    false   cannot be used
2896
10469
*/
2897
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10470
static bool
 
10471
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2898
10472
{
2899
10473
  return r->const_item() &&
2900
10474
    /* elements must be compared as dates */
2909
10483
/**
2910
10484
  Return true if the item is a const value in all the WHERE clause.
2911
10485
*/
2912
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10486
 
 
10487
static bool
 
10488
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2913
10489
{
2914
10490
  if (cond->type() == Item::COND_ITEM)
2915
10491
  {
2922
10498
      bool res=const_expression_in_where(item, comp_item, const_item);
2923
10499
      if (res)                                  // Is a const value
2924
10500
      {
2925
 
        if (and_level)
2926
 
          return 1;
 
10501
        if (and_level)
 
10502
          return 1;
2927
10503
      }
2928
10504
      else if (!and_level)
2929
 
        return 0;
 
10505
        return 0;
2930
10506
    }
2931
10507
    return and_level ? 0 : 1;
2932
10508
  }
2934
10510
  {                                             // boolan compare function
2935
10511
    Item_func* func= (Item_func*) cond;
2936
10512
    if (func->functype() != Item_func::EQUAL_FUNC &&
2937
 
              func->functype() != Item_func::EQ_FUNC)
 
10513
        func->functype() != Item_func::EQ_FUNC)
2938
10514
      return 0;
2939
10515
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2940
10516
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2942
10518
    {
2943
10519
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2944
10520
      {
2945
 
        if (*const_item)
2946
 
          return right_item->eq(*const_item, 1);
2947
 
        *const_item=right_item;
2948
 
        return 1;
 
10521
        if (*const_item)
 
10522
          return right_item->eq(*const_item, 1);
 
10523
        *const_item=right_item;
 
10524
        return 1;
2949
10525
      }
2950
10526
    }
2951
10527
    else if (right_item->eq(comp_item,1))
2952
10528
    {
2953
10529
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2954
10530
      {
2955
 
        if (*const_item)
2956
 
          return left_item->eq(*const_item, 1);
2957
 
        *const_item=left_item;
2958
 
        return 1;
2959
 
      }
2960
 
    }
2961
 
  }
2962
 
  return 0;
2963
 
}
 
10531
        if (*const_item)
 
10532
          return left_item->eq(*const_item, 1);
 
10533
        *const_item=left_item;
 
10534
        return 1;
 
10535
      }
 
10536
    }
 
10537
  }
 
10538
  return 0;
 
10539
}
 
10540
 
 
10541
/****************************************************************************
 
10542
  Create internal temporary table
 
10543
****************************************************************************/
 
10544
 
 
10545
/**
 
10546
  Create field for temporary table from given field.
 
10547
 
 
10548
  @param thd           Thread handler
 
10549
  @param org_field    field from which new field will be created
 
10550
  @param name         New field name
 
10551
  @param table         Temporary table
 
10552
  @param item          !=NULL if item->result_field should point to new field.
 
10553
                      This is relevant for how fill_record() is going to work:
 
10554
                      If item != NULL then fill_record() will update
 
10555
                      the record in the original table.
 
10556
                      If item == NULL then fill_record() will update
 
10557
                      the temporary table
 
10558
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10559
                               field instead of blob.
 
10560
 
 
10561
  @retval
 
10562
    NULL                on error
 
10563
  @retval
 
10564
    new_created field
 
10565
*/
 
10566
 
 
10567
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
 
10568
                                   const char *name, TABLE *table,
 
10569
                                   Item_field *item, uint convert_blob_length)
 
10570
{
 
10571
  Field *new_field;
 
10572
 
 
10573
  /* 
 
10574
    Make sure that the blob fits into a Field_varstring which has 
 
10575
    2-byte lenght. 
 
10576
  */
 
10577
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
 
10578
      (org_field->flags & BLOB_FLAG))
 
10579
    new_field= new Field_varstring(convert_blob_length,
 
10580
                                   org_field->maybe_null(),
 
10581
                                   org_field->field_name, table->s,
 
10582
                                   org_field->charset());
 
10583
  else
 
10584
    new_field= org_field->new_field(thd->mem_root, table,
 
10585
                                    table == org_field->table);
 
10586
  if (new_field)
 
10587
  {
 
10588
    new_field->init(table);
 
10589
    new_field->orig_table= org_field->orig_table;
 
10590
    if (item)
 
10591
      item->result_field= new_field;
 
10592
    else
 
10593
      new_field->field_name= name;
 
10594
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
 
10595
    if (org_field->maybe_null() || (item && item->maybe_null))
 
10596
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
 
10597
    if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
 
10598
        org_field->type() == MYSQL_TYPE_VARCHAR)
 
10599
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
10600
    else if (org_field->type() == FIELD_TYPE_DOUBLE)
 
10601
      ((Field_double *) new_field)->not_fixed= true;
 
10602
  }
 
10603
  return new_field;
 
10604
}
 
10605
 
 
10606
/**
 
10607
  Create field for temporary table using type of given item.
 
10608
 
 
10609
  @param thd                   Thread handler
 
10610
  @param item                  Item to create a field for
 
10611
  @param table                 Temporary table
 
10612
  @param copy_func             If set and item is a function, store copy of
 
10613
                               item in this array
 
10614
  @param modify_item           1 if item->result_field should point to new
 
10615
                               item. This is relevent for how fill_record()
 
10616
                               is going to work:
 
10617
                               If modify_item is 1 then fill_record() will
 
10618
                               update the record in the original table.
 
10619
                               If modify_item is 0 then fill_record() will
 
10620
                               update the temporary table
 
10621
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
 
10622
                               field instead of blob.
 
10623
 
 
10624
  @retval
 
10625
    0  on error
 
10626
  @retval
 
10627
    new_created field
 
10628
*/
 
10629
 
 
10630
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)),
 
10631
                                         Item *item, TABLE *table,
 
10632
                                         Item ***copy_func, bool modify_item,
 
10633
                                         uint convert_blob_length)
 
10634
{
 
10635
  bool maybe_null= item->maybe_null;
 
10636
  Field *new_field;
 
10637
 
 
10638
  switch (item->result_type()) {
 
10639
  case REAL_RESULT:
 
10640
    new_field= new Field_double(item->max_length, maybe_null,
 
10641
                                item->name, item->decimals, true);
 
10642
    break;
 
10643
  case INT_RESULT:
 
10644
    /* 
 
10645
      Select an integer type with the minimal fit precision.
 
10646
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
 
10647
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
 
10648
      Field_long : make them Field_longlong.  
 
10649
    */
 
10650
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
10651
      new_field=new Field_longlong(item->max_length, maybe_null,
 
10652
                                   item->name, item->unsigned_flag);
 
10653
    else
 
10654
      new_field=new Field_long(item->max_length, maybe_null,
 
10655
                               item->name, item->unsigned_flag);
 
10656
    break;
 
10657
  case STRING_RESULT:
 
10658
    assert(item->collation.collation);
 
10659
  
 
10660
    enum enum_field_types type;
 
10661
    /*
 
10662
      DATE/TIME fields have STRING_RESULT result type. 
 
10663
      To preserve type they needed to be handled separately.
 
10664
    */
 
10665
    if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
 
10666
        type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
 
10667
        type == MYSQL_TYPE_TIMESTAMP)
 
10668
      new_field= item->tmp_table_field_from_field_type(table, 1);
 
10669
    /* 
 
10670
      Make sure that the blob fits into a Field_varstring which has 
 
10671
      2-byte lenght. 
 
10672
    */
 
10673
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
 
10674
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
10675
             convert_blob_length)
 
10676
      new_field= new Field_varstring(convert_blob_length, maybe_null,
 
10677
                                     item->name, table->s,
 
10678
                                     item->collation.collation);
 
10679
    else
 
10680
      new_field= item->make_string_field(table);
 
10681
    new_field->set_derivation(item->collation.derivation);
 
10682
    break;
 
10683
  case DECIMAL_RESULT:
 
10684
  {
 
10685
    uint8 dec= item->decimals;
 
10686
    uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
10687
    uint32 len= item->max_length;
 
10688
 
 
10689
    /*
 
10690
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
10691
      will always throw a warning. We must limit dec to
 
10692
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
10693
    */
 
10694
 
 
10695
    if (dec > 0)
 
10696
    {
 
10697
      signed int overflow;
 
10698
 
 
10699
      dec= min(dec, DECIMAL_MAX_SCALE);
 
10700
 
 
10701
      /*
 
10702
        If the value still overflows the field with the corrected dec,
 
10703
        we'll throw out decimals rather than integers. This is still
 
10704
        bad and of course throws a truncation warning.
 
10705
        +1: for decimal point
 
10706
      */
 
10707
 
 
10708
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
10709
                                               item->unsigned_flag) - len;
 
10710
 
 
10711
      if (overflow > 0)
 
10712
        dec= max(0, dec - overflow);            // too long, discard fract
 
10713
      else
 
10714
        len -= item->decimals - dec;            // corrected value fits
 
10715
    }
 
10716
 
 
10717
    new_field= new Field_new_decimal(len, maybe_null, item->name,
 
10718
                                     dec, item->unsigned_flag);
 
10719
    break;
 
10720
  }
 
10721
  case ROW_RESULT:
 
10722
  default:
 
10723
    // This case should never be choosen
 
10724
    assert(0);
 
10725
    new_field= 0;
 
10726
    break;
 
10727
  }
 
10728
  if (new_field)
 
10729
    new_field->init(table);
 
10730
    
 
10731
  if (copy_func && item->is_result_field())
 
10732
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
10733
  if (modify_item)
 
10734
    item->set_result_field(new_field);
 
10735
  if (item->type() == Item::NULL_ITEM)
 
10736
    new_field->is_created_from_null_item= true;
 
10737
  return new_field;
 
10738
}
 
10739
 
 
10740
 
 
10741
/**
 
10742
  Create field for information schema table.
 
10743
 
 
10744
  @param thd            Thread handler
 
10745
  @param table          Temporary table
 
10746
  @param item           Item to create a field for
 
10747
 
 
10748
  @retval
 
10749
    0                   on error
 
10750
  @retval
 
10751
    new_created field
 
10752
*/
 
10753
 
 
10754
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)),
 
10755
                                   Item *item, TABLE *table)
 
10756
{
 
10757
  if (item->field_type() == MYSQL_TYPE_VARCHAR)
 
10758
  {
 
10759
    Field *field;
 
10760
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
 
10761
      field= new Field_blob(item->max_length, item->maybe_null,
 
10762
                            item->name, item->collation.collation);
 
10763
    else
 
10764
      field= new Field_varstring(item->max_length, item->maybe_null,
 
10765
                                 item->name,
 
10766
                                 table->s, item->collation.collation);
 
10767
    if (field)
 
10768
      field->init(table);
 
10769
    return field;
 
10770
  }
 
10771
  return item->tmp_table_field_from_field_type(table, 0);
 
10772
}
 
10773
 
 
10774
 
 
10775
/**
 
10776
  Create field for temporary table.
 
10777
 
 
10778
  @param thd            Thread handler
 
10779
  @param table          Temporary table
 
10780
  @param item           Item to create a field for
 
10781
  @param type           Type of item (normally item->type)
 
10782
  @param copy_func      If set and item is a function, store copy of item
 
10783
                       in this array
 
10784
  @param from_field    if field will be created using other field as example,
 
10785
                       pointer example field will be written here
 
10786
  @param default_field  If field has a default value field, store it here
 
10787
  @param group          1 if we are going to do a relative group by on result
 
10788
  @param modify_item    1 if item->result_field should point to new item.
 
10789
                       This is relevent for how fill_record() is going to
 
10790
                       work:
 
10791
                       If modify_item is 1 then fill_record() will update
 
10792
                       the record in the original table.
 
10793
                       If modify_item is 0 then fill_record() will update
 
10794
                       the temporary table
 
10795
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
 
10796
                             field instead of blob.
 
10797
 
 
10798
  @retval
 
10799
    0                   on error
 
10800
  @retval
 
10801
    new_created field
 
10802
*/
 
10803
 
 
10804
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
 
10805
                        Item ***copy_func, Field **from_field,
 
10806
                        Field **default_field,
 
10807
                        bool group, bool modify_item,
 
10808
                        bool table_cant_handle_bit_fields __attribute__((__unused__)),
 
10809
                        bool make_copy_field,
 
10810
                        uint convert_blob_length)
 
10811
{
 
10812
  Field *result;
 
10813
  Item::Type orig_type= type;
 
10814
  Item *orig_item= 0;
 
10815
 
 
10816
  if (type != Item::FIELD_ITEM &&
 
10817
      item->real_item()->type() == Item::FIELD_ITEM)
 
10818
  {
 
10819
    orig_item= item;
 
10820
    item= item->real_item();
 
10821
    type= Item::FIELD_ITEM;
 
10822
  }
 
10823
 
 
10824
  switch (type) {
 
10825
  case Item::SUM_FUNC_ITEM:
 
10826
  {
 
10827
    Item_sum *item_sum=(Item_sum*) item;
 
10828
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
 
10829
    if (!result)
 
10830
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
10831
    return result;
 
10832
  }
 
10833
  case Item::FIELD_ITEM:
 
10834
  case Item::DEFAULT_VALUE_ITEM:
 
10835
  {
 
10836
    Item_field *field= (Item_field*) item;
 
10837
    bool orig_modify= modify_item;
 
10838
    if (orig_type == Item::REF_ITEM)
 
10839
      modify_item= 0;
 
10840
    /*
 
10841
      If item have to be able to store NULLs but underlaid field can't do it,
 
10842
      create_tmp_field_from_field() can't be used for tmp field creation.
 
10843
    */
 
10844
    if (field->maybe_null && !field->field->maybe_null())
 
10845
    {
 
10846
      result= create_tmp_field_from_item(thd, item, table, NULL,
 
10847
                                         modify_item, convert_blob_length);
 
10848
      *from_field= field->field;
 
10849
      if (result && modify_item)
 
10850
        field->result_field= result;
 
10851
    } 
 
10852
    else
 
10853
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
 
10854
                                          orig_item ? orig_item->name :
 
10855
                                          item->name,
 
10856
                                          table,
 
10857
                                          modify_item ? field :
 
10858
                                          NULL,
 
10859
                                          convert_blob_length);
 
10860
    if (orig_type == Item::REF_ITEM && orig_modify)
 
10861
      ((Item_ref*)orig_item)->set_result_field(result);
 
10862
    if (field->field->eq_def(result))
 
10863
      *default_field= field->field;
 
10864
    return result;
 
10865
  }
 
10866
  /* Fall through */
 
10867
  case Item::FUNC_ITEM:
 
10868
    /* Fall through */
 
10869
  case Item::COND_ITEM:
 
10870
  case Item::FIELD_AVG_ITEM:
 
10871
  case Item::FIELD_STD_ITEM:
 
10872
  case Item::SUBSELECT_ITEM:
 
10873
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
 
10874
  case Item::PROC_ITEM:
 
10875
  case Item::INT_ITEM:
 
10876
  case Item::REAL_ITEM:
 
10877
  case Item::DECIMAL_ITEM:
 
10878
  case Item::STRING_ITEM:
 
10879
  case Item::REF_ITEM:
 
10880
  case Item::NULL_ITEM:
 
10881
  case Item::VARBIN_ITEM:
 
10882
    if (make_copy_field)
 
10883
    {
 
10884
      assert(((Item_result_field*)item)->result_field);
 
10885
      *from_field= ((Item_result_field*)item)->result_field;
 
10886
    }
 
10887
    return create_tmp_field_from_item(thd, item, table,
 
10888
                                      (make_copy_field ? 0 : copy_func),
 
10889
                                       modify_item, convert_blob_length);
 
10890
  case Item::TYPE_HOLDER:  
 
10891
    result= ((Item_type_holder *)item)->make_field_by_type(table);
 
10892
    result->set_derivation(item->collation.derivation);
 
10893
    return result;
 
10894
  default:                                      // Dosen't have to be stored
 
10895
    return 0;
 
10896
  }
 
10897
}
 
10898
 
 
10899
/*
 
10900
  Set up column usage bitmaps for a temporary table
 
10901
 
 
10902
  IMPLEMENTATION
 
10903
    For temporary tables, we need one bitmap with all columns set and
 
10904
    a tmp_set bitmap to be used by things like filesort.
 
10905
*/
 
10906
 
 
10907
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
 
10908
{
 
10909
  uint field_count= table->s->fields;
 
10910
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
 
10911
              false);
 
10912
  bitmap_init(&table->tmp_set,
 
10913
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
 
10914
              field_count, false);
 
10915
  /* write_set and all_set are copies of read_set */
 
10916
  table->def_write_set= table->def_read_set;
 
10917
  table->s->all_set= table->def_read_set;
 
10918
  bitmap_set_all(&table->s->all_set);
 
10919
  table->default_column_bitmaps();
 
10920
}
 
10921
 
 
10922
 
 
10923
/**
 
10924
  Create a temp table according to a field list.
 
10925
 
 
10926
  Given field pointers are changed to point at tmp_table for
 
10927
  send_fields. The table object is self contained: it's
 
10928
  allocated in its own memory root, as well as Field objects
 
10929
  created for table columns.
 
10930
  This function will replace Item_sum items in 'fields' list with
 
10931
  corresponding Item_field items, pointing at the fields in the
 
10932
  temporary table, unless this was prohibited by true
 
10933
  value of argument save_sum_fields. The Item_field objects
 
10934
  are created in THD memory root.
 
10935
 
 
10936
  @param thd                  thread handle
 
10937
  @param param                a description used as input to create the table
 
10938
  @param fields               list of items that will be used to define
 
10939
                              column types of the table (also see NOTES)
 
10940
  @param group                TODO document
 
10941
  @param distinct             should table rows be distinct
 
10942
  @param save_sum_fields      see NOTES
 
10943
  @param select_options
 
10944
  @param rows_limit
 
10945
  @param table_alias          possible name of the temporary table that can
 
10946
                              be used for name resolving; can be "".
 
10947
*/
 
10948
 
 
10949
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
 
10950
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
 
10951
#define RATIO_TO_PACK_ROWS             2
 
10952
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
 
10953
 
 
10954
TABLE *
 
10955
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 
10956
                 ORDER *group, bool distinct, bool save_sum_fields,
 
10957
                 ulonglong select_options, ha_rows rows_limit,
 
10958
                 char *table_alias)
 
10959
{
 
10960
  MEM_ROOT *mem_root_save, own_root;
 
10961
  TABLE *table;
 
10962
  TABLE_SHARE *share;
 
10963
  uint  i,field_count,null_count,null_pack_length;
 
10964
  uint  copy_func_count= param->func_count;
 
10965
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
 
10966
  uint  blob_count,group_null_items, string_count;
 
10967
  uint  temp_pool_slot=MY_BIT_NONE;
 
10968
  uint fieldnr= 0;
 
10969
  ulong reclength, string_total_length;
 
10970
  bool  using_unique_constraint= 0;
 
10971
  bool  use_packed_rows= 0;
 
10972
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
 
10973
  char  *tmpname,path[FN_REFLEN];
 
10974
  uchar *pos, *group_buff, *bitmaps;
 
10975
  uchar *null_flags;
 
10976
  Field **reg_field, **from_field, **default_field;
 
10977
  uint *blob_field;
 
10978
  Copy_field *copy=0;
 
10979
  KEY *keyinfo;
 
10980
  KEY_PART_INFO *key_part_info;
 
10981
  Item **copy_func;
 
10982
  MI_COLUMNDEF *recinfo;
 
10983
  uint total_uneven_bit_length= 0;
 
10984
  bool force_copy_fields= param->force_copy_fields;
 
10985
 
 
10986
  status_var_increment(thd->status_var.created_tmp_tables);
 
10987
 
 
10988
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
10989
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
10990
 
 
10991
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
10992
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
10993
            current_pid, temp_pool_slot);
 
10994
  else
 
10995
  {
 
10996
    /* if we run out of slots or we are not using tempool */
 
10997
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
10998
            thd->thread_id, thd->tmp_table++);
 
10999
  }
 
11000
 
 
11001
  /*
 
11002
    No need to change table name to lower case as we are only creating
 
11003
    MyISAM or HEAP tables here
 
11004
  */
 
11005
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11006
 
 
11007
 
 
11008
  if (group)
 
11009
  {
 
11010
    if (!param->quick_group)
 
11011
      group=0;                                  // Can't use group key
 
11012
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
 
11013
    {
 
11014
      /*
 
11015
        marker == 4 means two things:
 
11016
        - store NULLs in the key, and
 
11017
        - convert BIT fields to 64-bit long, needed because MEMORY tables
 
11018
          can't index BIT fields.
 
11019
      */
 
11020
      (*tmp->item)->marker= 4;
 
11021
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
 
11022
        using_unique_constraint=1;
 
11023
    }
 
11024
    if (param->group_length >= MAX_BLOB_WIDTH)
 
11025
      using_unique_constraint=1;
 
11026
    if (group)
 
11027
      distinct=0;                               // Can't use distinct
 
11028
  }
 
11029
 
 
11030
  field_count=param->field_count+param->func_count+param->sum_func_count;
 
11031
  hidden_field_count=param->hidden_field_count;
 
11032
 
 
11033
  /*
 
11034
    When loose index scan is employed as access method, it already
 
11035
    computes all groups and the result of all aggregate functions. We
 
11036
    make space for the items of the aggregate function in the list of
 
11037
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
 
11038
    these items are stored in the temporary table.
 
11039
  */
 
11040
  if (param->precomputed_group_by)
 
11041
    copy_func_count+= param->sum_func_count;
 
11042
  
 
11043
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11044
 
 
11045
  if (!multi_alloc_root(&own_root,
 
11046
                        &table, sizeof(*table),
 
11047
                        &share, sizeof(*share),
 
11048
                        &reg_field, sizeof(Field*) * (field_count+1),
 
11049
                        &default_field, sizeof(Field*) * (field_count),
 
11050
                        &blob_field, sizeof(uint)*(field_count+1),
 
11051
                        &from_field, sizeof(Field*)*field_count,
 
11052
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
11053
                        &param->keyinfo, sizeof(*param->keyinfo),
 
11054
                        &key_part_info,
 
11055
                        sizeof(*key_part_info)*(param->group_parts+1),
 
11056
                        &param->start_recinfo,
 
11057
                        sizeof(*param->recinfo)*(field_count*2+4),
 
11058
                        &tmpname, (uint) strlen(path)+1,
 
11059
                        &group_buff, (group && ! using_unique_constraint ?
 
11060
                                      param->group_length : 0),
 
11061
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11062
                        NullS))
 
11063
  {
 
11064
    if (temp_pool_slot != MY_BIT_NONE)
 
11065
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11066
    return(NULL);                               /* purecov: inspected */
 
11067
  }
 
11068
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
 
11069
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
 
11070
  {
 
11071
    if (temp_pool_slot != MY_BIT_NONE)
 
11072
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11073
    free_root(&own_root, MYF(0));               /* purecov: inspected */
 
11074
    return(NULL);                               /* purecov: inspected */
 
11075
  }
 
11076
  param->items_to_copy= copy_func;
 
11077
  strmov(tmpname,path);
 
11078
  /* make table according to fields */
 
11079
 
 
11080
  bzero((char*) table,sizeof(*table));
 
11081
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
 
11082
  bzero((char*) default_field, sizeof(Field*) * (field_count));
 
11083
  bzero((char*) from_field,sizeof(Field*)*field_count);
 
11084
 
 
11085
  table->mem_root= own_root;
 
11086
  mem_root_save= thd->mem_root;
 
11087
  thd->mem_root= &table->mem_root;
 
11088
 
 
11089
  table->field=reg_field;
 
11090
  table->alias= table_alias;
 
11091
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11092
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11093
  table->map=1;
 
11094
  table->temp_pool_slot = temp_pool_slot;
 
11095
  table->copy_blobs= 1;
 
11096
  table->in_use= thd;
 
11097
  table->quick_keys.init();
 
11098
  table->covering_keys.init();
 
11099
  table->keys_in_use_for_query.init();
 
11100
 
 
11101
  table->s= share;
 
11102
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11103
  share->blob_field= blob_field;
 
11104
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11105
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11106
  share->table_charset= param->table_charset;
 
11107
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11108
  share->keys_for_keyread.init();
 
11109
  share->keys_in_use.init();
 
11110
 
 
11111
  /* Calculate which type of fields we will store in the temporary table */
 
11112
 
 
11113
  reclength= string_total_length= 0;
 
11114
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
 
11115
  param->using_indirect_summary_function=0;
 
11116
 
 
11117
  List_iterator_fast<Item> li(fields);
 
11118
  Item *item;
 
11119
  Field **tmp_from_field=from_field;
 
11120
  while ((item=li++))
 
11121
  {
 
11122
    Item::Type type=item->type();
 
11123
    if (not_all_columns)
 
11124
    {
 
11125
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
 
11126
      {
 
11127
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
 
11128
          item->update_used_tables();
 
11129
        if (type == Item::SUBSELECT_ITEM ||
 
11130
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
 
11131
        {
 
11132
          /*
 
11133
            Mark that the we have ignored an item that refers to a summary
 
11134
            function. We need to know this if someone is going to use
 
11135
            DISTINCT on the result.
 
11136
          */
 
11137
          param->using_indirect_summary_function=1;
 
11138
          continue;
 
11139
        }
 
11140
      }
 
11141
      if (item->const_item() && (int) hidden_field_count <= 0)
 
11142
        continue; // We don't have to store this
 
11143
    }
 
11144
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
 
11145
    {                                           /* Can't calc group yet */
 
11146
      ((Item_sum*) item)->result_field=0;
 
11147
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
11148
      {
 
11149
        Item **argp= ((Item_sum*) item)->args + i;
 
11150
        Item *arg= *argp;
 
11151
        if (!arg->const_item())
 
11152
        {
 
11153
          Field *new_field=
 
11154
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
 
11155
                             tmp_from_field, &default_field[fieldnr],
 
11156
                             group != 0,not_all_columns,
 
11157
                             distinct, 0,
 
11158
                             param->convert_blob_length);
 
11159
          if (!new_field)
 
11160
            goto err;                                   // Should be OOM
 
11161
          tmp_from_field++;
 
11162
          reclength+=new_field->pack_length();
 
11163
          if (new_field->flags & BLOB_FLAG)
 
11164
          {
 
11165
            *blob_field++= fieldnr;
 
11166
            blob_count++;
 
11167
          }
 
11168
          *(reg_field++)= new_field;
 
11169
          if (new_field->real_type() == MYSQL_TYPE_STRING ||
 
11170
              new_field->real_type() == MYSQL_TYPE_VARCHAR)
 
11171
          {
 
11172
            string_count++;
 
11173
            string_total_length+= new_field->pack_length();
 
11174
          }
 
11175
          thd->mem_root= mem_root_save;
 
11176
          thd->change_item_tree(argp, new Item_field(new_field));
 
11177
          thd->mem_root= &table->mem_root;
 
11178
          if (!(new_field->flags & NOT_NULL_FLAG))
 
11179
          {
 
11180
            null_count++;
 
11181
            /*
 
11182
              new_field->maybe_null() is still false, it will be
 
11183
              changed below. But we have to setup Item_field correctly
 
11184
            */
 
11185
            (*argp)->maybe_null=1;
 
11186
          }
 
11187
          new_field->field_index= fieldnr++;
 
11188
        }
 
11189
      }
 
11190
    }
 
11191
    else
 
11192
    {
 
11193
      /*
 
11194
        The last parameter to create_tmp_field() is a bit tricky:
 
11195
 
 
11196
        We need to set it to 0 in union, to get fill_record() to modify the
 
11197
        temporary table.
 
11198
        We need to set it to 1 on multi-table-update and in select to
 
11199
        write rows to the temporary table.
 
11200
        We here distinguish between UNION and multi-table-updates by the fact
 
11201
        that in the later case group is set to the row pointer.
 
11202
      */
 
11203
      Field *new_field= (param->schema_table) ?
 
11204
        create_tmp_field_for_schema(thd, item, table) :
 
11205
        create_tmp_field(thd, table, item, type, &copy_func,
 
11206
                         tmp_from_field, &default_field[fieldnr],
 
11207
                         group != 0,
 
11208
                         !force_copy_fields &&
 
11209
                           (not_all_columns || group !=0),
 
11210
                         /*
 
11211
                           If item->marker == 4 then we force create_tmp_field
 
11212
                           to create a 64-bit longs for BIT fields because HEAP
 
11213
                           tables can't index BIT fields directly. We do the same
 
11214
                           for distinct, as we want the distinct index to be
 
11215
                           usable in this case too.
 
11216
                         */
 
11217
                         item->marker == 4 || param->bit_fields_as_long,
 
11218
                         force_copy_fields,
 
11219
                         param->convert_blob_length);
 
11220
 
 
11221
      if (!new_field)
 
11222
      {
 
11223
        if (thd->is_fatal_error)
 
11224
          goto err;                             // Got OOM
 
11225
        continue;                               // Some kindf of const item
 
11226
      }
 
11227
      if (type == Item::SUM_FUNC_ITEM)
 
11228
        ((Item_sum *) item)->result_field= new_field;
 
11229
      tmp_from_field++;
 
11230
      reclength+=new_field->pack_length();
 
11231
      if (!(new_field->flags & NOT_NULL_FLAG))
 
11232
        null_count++;
 
11233
      if (new_field->flags & BLOB_FLAG)
 
11234
      {
 
11235
        *blob_field++= fieldnr;
 
11236
        blob_count++;
 
11237
      }
 
11238
      if (item->marker == 4 && item->maybe_null)
 
11239
      {
 
11240
        group_null_items++;
 
11241
        new_field->flags|= GROUP_FLAG;
 
11242
      }
 
11243
      new_field->field_index= fieldnr++;
 
11244
      *(reg_field++)= new_field;
 
11245
    }
 
11246
    if (!--hidden_field_count)
 
11247
    {
 
11248
      /*
 
11249
        This was the last hidden field; Remember how many hidden fields could
 
11250
        have null
 
11251
      */
 
11252
      hidden_null_count=null_count;
 
11253
      /*
 
11254
        We need to update hidden_field_count as we may have stored group
 
11255
        functions with constant arguments
 
11256
      */
 
11257
      param->hidden_field_count= fieldnr;
 
11258
      null_count= 0;
 
11259
    }
 
11260
  }
 
11261
  assert(fieldnr == (uint) (reg_field - table->field));
 
11262
  assert(field_count >= (uint) (reg_field - table->field));
 
11263
  field_count= fieldnr;
 
11264
  *reg_field= 0;
 
11265
  *blob_field= 0;                               // End marker
 
11266
  share->fields= field_count;
 
11267
 
 
11268
  /* If result table is small; use a heap */
 
11269
  /* future: storage engine selection can be made dynamic? */
 
11270
  if (blob_count || using_unique_constraint ||
 
11271
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
11272
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
 
11273
  {
 
11274
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11275
    table->file= get_new_handler(share, &table->mem_root,
 
11276
                                 share->db_type());
 
11277
    if (group &&
 
11278
        (param->group_parts > table->file->max_key_parts() ||
 
11279
         param->group_length > table->file->max_key_length()))
 
11280
      using_unique_constraint=1;
 
11281
  }
 
11282
  else
 
11283
  {
 
11284
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11285
    table->file= get_new_handler(share, &table->mem_root,
 
11286
                                 share->db_type());
 
11287
  }
 
11288
  if (!table->file)
 
11289
    goto err;
 
11290
 
 
11291
 
 
11292
  if (!using_unique_constraint)
 
11293
    reclength+= group_null_items;       // null flag is stored separately
 
11294
 
 
11295
  share->blob_fields= blob_count;
 
11296
  if (blob_count == 0)
 
11297
  {
 
11298
    /* We need to ensure that first byte is not 0 for the delete link */
 
11299
    if (param->hidden_field_count)
 
11300
      hidden_null_count++;
 
11301
    else
 
11302
      null_count++;
 
11303
  }
 
11304
  hidden_null_pack_length=(hidden_null_count+7)/8;
 
11305
  null_pack_length= (hidden_null_pack_length +
 
11306
                     (null_count + total_uneven_bit_length + 7) / 8);
 
11307
  reclength+=null_pack_length;
 
11308
  if (!reclength)
 
11309
    reclength=1;                                // Dummy select
 
11310
  /* Use packed rows if there is blobs or a lot of space to gain */
 
11311
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
 
11312
    use_packed_rows= 1;
 
11313
 
 
11314
  share->reclength= reclength;
 
11315
  {
 
11316
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
 
11317
    share->rec_buff_length= alloc_length;
 
11318
    if (!(table->record[0]= (uchar*)
 
11319
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11320
      goto err;
 
11321
    table->record[1]= table->record[0]+alloc_length;
 
11322
    share->default_values= table->record[1]+alloc_length;
 
11323
  }
 
11324
  copy_func[0]=0;                               // End marker
 
11325
  param->func_count= copy_func - param->items_to_copy; 
 
11326
 
 
11327
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11328
 
 
11329
  recinfo=param->start_recinfo;
 
11330
  null_flags=(uchar*) table->record[0];
 
11331
  pos=table->record[0]+ null_pack_length;
 
11332
  if (null_pack_length)
 
11333
  {
 
11334
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11335
    recinfo->type=FIELD_NORMAL;
 
11336
    recinfo->length=null_pack_length;
 
11337
    recinfo++;
 
11338
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11339
 
 
11340
    table->null_flags= (uchar*) table->record[0];
 
11341
    share->null_fields= null_count+ hidden_null_count;
 
11342
    share->null_bytes= null_pack_length;
 
11343
  }
 
11344
  null_count= (blob_count == 0) ? 1 : 0;
 
11345
  hidden_field_count=param->hidden_field_count;
 
11346
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
11347
  {
 
11348
    Field *field= *reg_field;
 
11349
    uint length;
 
11350
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11351
 
 
11352
    if (!(field->flags & NOT_NULL_FLAG))
 
11353
    {
 
11354
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
 
11355
      {
 
11356
        /*
 
11357
          We have to reserve one byte here for NULL bits,
 
11358
          as this is updated by 'end_update()'
 
11359
        */
 
11360
        *pos++=0;                               // Null is stored here
 
11361
        recinfo->length=1;
 
11362
        recinfo->type=FIELD_NORMAL;
 
11363
        recinfo++;
 
11364
        bzero((uchar*) recinfo,sizeof(*recinfo));
 
11365
      }
 
11366
      else
 
11367
      {
 
11368
        recinfo->null_bit= 1 << (null_count & 7);
 
11369
        recinfo->null_pos= null_count/8;
 
11370
      }
 
11371
      field->move_field(pos,null_flags+null_count/8,
 
11372
                        1 << (null_count & 7));
 
11373
      null_count++;
 
11374
    }
 
11375
    else
 
11376
      field->move_field(pos,(uchar*) 0,0);
 
11377
    field->reset();
 
11378
 
 
11379
    /*
 
11380
      Test if there is a default field value. The test for ->ptr is to skip
 
11381
      'offset' fields generated by initalize_tables
 
11382
    */
 
11383
    if (default_field[i] && default_field[i]->ptr)
 
11384
    {
 
11385
      /* 
 
11386
         default_field[i] is set only in the cases  when 'field' can
 
11387
         inherit the default value that is defined for the field referred
 
11388
         by the Item_field object from which 'field' has been created.
 
11389
      */
 
11390
      my_ptrdiff_t diff;
 
11391
      Field *orig_field= default_field[i];
 
11392
      /* Get the value from default_values */
 
11393
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
11394
                            orig_field->table->record[0]);
 
11395
      orig_field->move_field_offset(diff);      // Points now at default_values
 
11396
      if (orig_field->is_real_null())
 
11397
        field->set_null();
 
11398
      else
 
11399
      {
 
11400
        field->set_notnull();
 
11401
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
 
11402
      }
 
11403
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
11404
    } 
 
11405
 
 
11406
    if (from_field[i])
 
11407
    {                                           /* Not a table Item */
 
11408
      copy->set(field,from_field[i],save_sum_fields);
 
11409
      copy++;
 
11410
    }
 
11411
    length=field->pack_length();
 
11412
    pos+= length;
 
11413
 
 
11414
    /* Make entry for create table */
 
11415
    recinfo->length=length;
 
11416
    if (field->flags & BLOB_FLAG)
 
11417
      recinfo->type= (int) FIELD_BLOB;
 
11418
    else if (use_packed_rows &&
 
11419
             field->real_type() == MYSQL_TYPE_STRING &&
 
11420
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11421
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11422
    else
 
11423
      recinfo->type=FIELD_NORMAL;
 
11424
    if (!--hidden_field_count)
 
11425
      null_count=(null_count+7) & ~7;           // move to next byte
 
11426
 
 
11427
    // fix table name in field entry
 
11428
    field->table_name= &table->alias;
 
11429
  }
 
11430
 
 
11431
  param->copy_field_end=copy;
 
11432
  param->recinfo=recinfo;
 
11433
  store_record(table,s->default_values);        // Make empty default record
 
11434
 
 
11435
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
11436
    share->max_rows= ~(ha_rows) 0;
 
11437
  else
 
11438
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11439
                                 min(thd->variables.tmp_table_size,
 
11440
                                     thd->variables.max_heap_table_size) :
 
11441
                                 thd->variables.tmp_table_size) /
 
11442
                                 share->reclength);
 
11443
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11444
  /*
 
11445
    Push the LIMIT clause to the temporary table creation, so that we
 
11446
    materialize only up to 'rows_limit' records instead of all result records.
 
11447
  */
 
11448
  set_if_smaller(share->max_rows, rows_limit);
 
11449
  param->end_write_records= rows_limit;
 
11450
 
 
11451
  keyinfo= param->keyinfo;
 
11452
 
 
11453
  if (group)
 
11454
  {
 
11455
    table->group=group;                         /* Table is grouped by key */
 
11456
    param->group_buff=group_buff;
 
11457
    share->keys=1;
 
11458
    share->uniques= test(using_unique_constraint);
 
11459
    table->key_info=keyinfo;
 
11460
    keyinfo->key_part=key_part_info;
 
11461
    keyinfo->flags=HA_NOSAME;
 
11462
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
 
11463
    keyinfo->key_length=0;
 
11464
    keyinfo->rec_per_key=0;
 
11465
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11466
    keyinfo->name= (char*) "group_key";
 
11467
    ORDER *cur_group= group;
 
11468
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
 
11469
    {
 
11470
      Field *field=(*cur_group->item)->get_tmp_table_field();
 
11471
      bool maybe_null=(*cur_group->item)->maybe_null;
 
11472
      key_part_info->null_bit=0;
 
11473
      key_part_info->field=  field;
 
11474
      key_part_info->offset= field->offset(table->record[0]);
 
11475
      key_part_info->length= (uint16) field->key_length();
 
11476
      key_part_info->type=   (uint8) field->key_type();
 
11477
      key_part_info->key_type =
 
11478
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11479
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11480
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11481
        0 : FIELDFLAG_BINARY;
 
11482
      if (!using_unique_constraint)
 
11483
      {
 
11484
        cur_group->buff=(char*) group_buff;
 
11485
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
 
11486
                                                     group_buff +
 
11487
                                                     test(maybe_null),
 
11488
                                                     field->null_ptr,
 
11489
                                                     field->null_bit)))
 
11490
          goto err; /* purecov: inspected */
 
11491
        if (maybe_null)
 
11492
        {
 
11493
          /*
 
11494
            To be able to group on NULL, we reserved place in group_buff
 
11495
            for the NULL flag just before the column. (see above).
 
11496
            The field data is after this flag.
 
11497
            The NULL flag is updated in 'end_update()' and 'end_write()'
 
11498
          */
 
11499
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
 
11500
          key_part_info->null_bit=field->null_bit;
 
11501
          key_part_info->null_offset= (uint) (field->null_ptr -
 
11502
                                              (uchar*) table->record[0]);
 
11503
          cur_group->buff++;                        // Pointer to field data
 
11504
          group_buff++;                         // Skipp null flag
 
11505
        }
 
11506
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
 
11507
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
 
11508
        group_buff+= cur_group->field->pack_length();
 
11509
      }
 
11510
      keyinfo->key_length+=  key_part_info->length;
 
11511
    }
 
11512
  }
 
11513
 
 
11514
  if (distinct && field_count != param->hidden_field_count)
 
11515
  {
 
11516
    /*
 
11517
      Create an unique key or an unique constraint over all columns
 
11518
      that should be in the result.  In the temporary table, there are
 
11519
      'param->hidden_field_count' extra columns, whose null bits are stored
 
11520
      in the first 'hidden_null_pack_length' bytes of the row.
 
11521
    */
 
11522
    if (blob_count)
 
11523
    {
 
11524
      /*
 
11525
        Special mode for index creation in MyISAM used to support unique
 
11526
        indexes on blobs with arbitrary length. Such indexes cannot be
 
11527
        used for lookups.
 
11528
      */
 
11529
      share->uniques= 1;
 
11530
    }
 
11531
    null_pack_length-=hidden_null_pack_length;
 
11532
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
 
11533
                         (share->uniques ? test(null_pack_length) : 0));
 
11534
    table->distinct= 1;
 
11535
    share->keys= 1;
 
11536
    if (!(key_part_info= (KEY_PART_INFO*)
 
11537
          alloc_root(&table->mem_root,
 
11538
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
11539
      goto err;
 
11540
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
11541
    table->key_info=keyinfo;
 
11542
    keyinfo->key_part=key_part_info;
 
11543
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
 
11544
    keyinfo->key_length=(uint16) reclength;
 
11545
    keyinfo->name= (char*) "distinct_key";
 
11546
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11547
    keyinfo->rec_per_key=0;
 
11548
 
 
11549
    /*
 
11550
      Create an extra field to hold NULL bits so that unique indexes on
 
11551
      blobs can distinguish NULL from 0. This extra field is not needed
 
11552
      when we do not use UNIQUE indexes for blobs.
 
11553
    */
 
11554
    if (null_pack_length && share->uniques)
 
11555
    {
 
11556
      key_part_info->null_bit=0;
 
11557
      key_part_info->offset=hidden_null_pack_length;
 
11558
      key_part_info->length=null_pack_length;
 
11559
      key_part_info->field= new Field_string(table->record[0],
 
11560
                                             (uint32) key_part_info->length,
 
11561
                                             (uchar*) 0,
 
11562
                                             (uint) 0,
 
11563
                                             Field::NONE,
 
11564
                                             NullS, &my_charset_bin);
 
11565
      if (!key_part_info->field)
 
11566
        goto err;
 
11567
      key_part_info->field->init(table);
 
11568
      key_part_info->key_type=FIELDFLAG_BINARY;
 
11569
      key_part_info->type=    HA_KEYTYPE_BINARY;
 
11570
      key_part_info++;
 
11571
    }
 
11572
    /* Create a distinct key over the columns we are going to return */
 
11573
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
11574
         i < field_count;
 
11575
         i++, reg_field++, key_part_info++)
 
11576
    {
 
11577
      key_part_info->null_bit=0;
 
11578
      key_part_info->field=    *reg_field;
 
11579
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
11580
      key_part_info->length=   (uint16) (*reg_field)->pack_length();
 
11581
      /* TODO:
 
11582
        The below method of computing the key format length of the
 
11583
        key part is a copy/paste from opt_range.cc, and table.cc.
 
11584
        This should be factored out, e.g. as a method of Field.
 
11585
        In addition it is not clear if any of the Field::*_length
 
11586
        methods is supposed to compute the same length. If so, it
 
11587
        might be reused.
 
11588
      */
 
11589
      key_part_info->store_length= key_part_info->length;
 
11590
 
 
11591
      if ((*reg_field)->real_maybe_null())
 
11592
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
 
11593
      if ((*reg_field)->type() == MYSQL_TYPE_BLOB || 
 
11594
          (*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
 
11595
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
 
11596
 
 
11597
      key_part_info->type=     (uint8) (*reg_field)->key_type();
 
11598
      key_part_info->key_type =
 
11599
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
 
11600
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
 
11601
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
 
11602
        0 : FIELDFLAG_BINARY;
 
11603
    }
 
11604
  }
 
11605
 
 
11606
  if (thd->is_fatal_error)                              // If end of memory
 
11607
    goto err;                                    /* purecov: inspected */
 
11608
  share->db_record_offset= 1;
 
11609
  if (share->db_type() == myisam_hton)
 
11610
  {
 
11611
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
 
11612
                                &param->recinfo, select_options))
 
11613
      goto err;
 
11614
  }
 
11615
  if (open_tmp_table(table))
 
11616
    goto err;
 
11617
 
 
11618
  thd->mem_root= mem_root_save;
 
11619
 
 
11620
  return(table);
 
11621
 
 
11622
err:
 
11623
  thd->mem_root= mem_root_save;
 
11624
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11625
  if (temp_pool_slot != MY_BIT_NONE)
 
11626
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11627
  return(NULL);                         /* purecov: inspected */
 
11628
}
 
11629
 
 
11630
 
 
11631
 
 
11632
 
 
11633
/*
 
11634
  Create a temporary table to weed out duplicate rowid combinations
 
11635
 
 
11636
  SYNOPSIS
 
11637
 
 
11638
    create_duplicate_weedout_tmp_table()
 
11639
      thd
 
11640
      uniq_tuple_length_arg
 
11641
      SJ_TMP_TABLE 
 
11642
 
 
11643
  DESCRIPTION
 
11644
    Create a temporary table to weed out duplicate rowid combinations. The
 
11645
    table has a single column that is a concatenation of all rowids in the
 
11646
    combination. 
 
11647
 
 
11648
    Depending on the needed length, there are two cases:
 
11649
 
 
11650
    1. When the length of the column < max_key_length:
 
11651
 
 
11652
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
11653
 
 
11654
    2. Otherwise (not a valid SQL syntax but internally supported):
 
11655
 
 
11656
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
11657
 
 
11658
    The code in this function was produced by extraction of relevant parts
 
11659
    from create_tmp_table().
 
11660
 
 
11661
  RETURN
 
11662
    created table
 
11663
    NULL on error
 
11664
*/
 
11665
 
 
11666
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
 
11667
                                          uint uniq_tuple_length_arg,
 
11668
                                          SJ_TMP_TABLE *sjtbl)
 
11669
{
 
11670
  MEM_ROOT *mem_root_save, own_root;
 
11671
  TABLE *table;
 
11672
  TABLE_SHARE *share;
 
11673
  uint  temp_pool_slot=MY_BIT_NONE;
 
11674
  char  *tmpname,path[FN_REFLEN];
 
11675
  Field **reg_field;
 
11676
  KEY_PART_INFO *key_part_info;
 
11677
  KEY *keyinfo;
 
11678
  uchar *group_buff;
 
11679
  uchar *bitmaps;
 
11680
  uint *blob_field;
 
11681
  MI_COLUMNDEF *recinfo, *start_recinfo;
 
11682
  bool using_unique_constraint=false;
 
11683
  bool use_packed_rows= false;
 
11684
  Field *field, *key_field;
 
11685
  uint blob_count, null_pack_length, null_count;
 
11686
  uchar *null_flags;
 
11687
  uchar *pos;
 
11688
  
 
11689
  /*
 
11690
    STEP 1: Get temporary table name
 
11691
  */
 
11692
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
 
11693
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
 
11694
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
 
11695
 
 
11696
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
 
11697
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
 
11698
            current_pid, temp_pool_slot);
 
11699
  else
 
11700
  {
 
11701
    /* if we run out of slots or we are not using tempool */
 
11702
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
 
11703
            thd->thread_id, thd->tmp_table++);
 
11704
  }
 
11705
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
 
11706
 
 
11707
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
 
11708
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
 
11709
    using_unique_constraint= true;
 
11710
 
 
11711
  /* STEP 3: Allocate memory for temptable description */
 
11712
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
11713
  if (!multi_alloc_root(&own_root,
 
11714
                        &table, sizeof(*table),
 
11715
                        &share, sizeof(*share),
 
11716
                        &reg_field, sizeof(Field*) * (1+1),
 
11717
                        &blob_field, sizeof(uint)*2,
 
11718
                        &keyinfo, sizeof(*keyinfo),
 
11719
                        &key_part_info, sizeof(*key_part_info) * 2,
 
11720
                        &start_recinfo,
 
11721
                        sizeof(*recinfo)*(1*2+4),
 
11722
                        &tmpname, (uint) strlen(path)+1,
 
11723
                        &group_buff, (!using_unique_constraint ?
 
11724
                                      uniq_tuple_length_arg : 0),
 
11725
                        &bitmaps, bitmap_buffer_size(1)*2,
 
11726
                        NullS))
 
11727
  {
 
11728
    if (temp_pool_slot != MY_BIT_NONE)
 
11729
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11730
    return(NULL);
 
11731
  }
 
11732
  strmov(tmpname,path);
 
11733
  
 
11734
 
 
11735
  /* STEP 4: Create TABLE description */
 
11736
  bzero((char*) table,sizeof(*table));
 
11737
  bzero((char*) reg_field,sizeof(Field*)*2);
 
11738
 
 
11739
  table->mem_root= own_root;
 
11740
  mem_root_save= thd->mem_root;
 
11741
  thd->mem_root= &table->mem_root;
 
11742
 
 
11743
  table->field=reg_field;
 
11744
  table->alias= "weedout-tmp";
 
11745
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
 
11746
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
 
11747
  table->map=1;
 
11748
  table->temp_pool_slot = temp_pool_slot;
 
11749
  table->copy_blobs= 1;
 
11750
  table->in_use= thd;
 
11751
  table->quick_keys.init();
 
11752
  table->covering_keys.init();
 
11753
  table->keys_in_use_for_query.init();
 
11754
 
 
11755
  table->s= share;
 
11756
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
 
11757
  share->blob_field= blob_field;
 
11758
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
11759
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
11760
  share->table_charset= NULL;
 
11761
  share->primary_key= MAX_KEY;               // Indicate no primary key
 
11762
  share->keys_for_keyread.init();
 
11763
  share->keys_in_use.init();
 
11764
 
 
11765
  blob_count= 0;
 
11766
 
 
11767
  /* Create the field */
 
11768
  {
 
11769
    /*
 
11770
      For the sake of uniformity, always use Field_varstring (altough we could
 
11771
      use Field_string for shorter keys)
 
11772
    */
 
11773
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
 
11774
                               &my_charset_bin);
 
11775
    if (!field)
 
11776
      return(0);
 
11777
    field->table= table;
 
11778
    field->key_start.init(0);
 
11779
    field->part_of_key.init(0);
 
11780
    field->part_of_sortkey.init(0);
 
11781
    field->unireg_check= Field::NONE;
 
11782
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
 
11783
    field->reset_fields();
 
11784
    field->init(table);
 
11785
    field->orig_table= NULL;
 
11786
     
 
11787
    field->field_index= 0;
 
11788
    
 
11789
    *(reg_field++)= field;
 
11790
    *blob_field= 0;
 
11791
    *reg_field= 0;
 
11792
 
 
11793
    share->fields= 1;
 
11794
    share->blob_fields= 0;
 
11795
  }
 
11796
 
 
11797
  uint reclength= field->pack_length();
 
11798
  if (using_unique_constraint)
 
11799
  { 
 
11800
    share->db_plugin= ha_lock_engine(0, myisam_hton);
 
11801
    table->file= get_new_handler(share, &table->mem_root,
 
11802
                                 share->db_type());
 
11803
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
 
11804
  }
 
11805
  else
 
11806
  {
 
11807
    share->db_plugin= ha_lock_engine(0, heap_hton);
 
11808
    table->file= get_new_handler(share, &table->mem_root,
 
11809
                                 share->db_type());
 
11810
  }
 
11811
  if (!table->file)
 
11812
    goto err;
 
11813
 
 
11814
  null_count=1;
 
11815
  
 
11816
  null_pack_length= 1;
 
11817
  reclength += null_pack_length;
 
11818
 
 
11819
  share->reclength= reclength;
 
11820
  {
 
11821
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
11822
    share->rec_buff_length= alloc_length;
 
11823
    if (!(table->record[0]= (uchar*)
 
11824
                            alloc_root(&table->mem_root, alloc_length*3)))
 
11825
      goto err;
 
11826
    table->record[1]= table->record[0]+alloc_length;
 
11827
    share->default_values= table->record[1]+alloc_length;
 
11828
  }
 
11829
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
11830
 
 
11831
  recinfo= start_recinfo;
 
11832
  null_flags=(uchar*) table->record[0];
 
11833
  pos=table->record[0]+ null_pack_length;
 
11834
  if (null_pack_length)
 
11835
  {
 
11836
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11837
    recinfo->type=FIELD_NORMAL;
 
11838
    recinfo->length=null_pack_length;
 
11839
    recinfo++;
 
11840
    bfill(null_flags,null_pack_length,255);     // Set null fields
 
11841
 
 
11842
    table->null_flags= (uchar*) table->record[0];
 
11843
    share->null_fields= null_count;
 
11844
    share->null_bytes= null_pack_length;
 
11845
  }
 
11846
  null_count=1;
 
11847
 
 
11848
  {
 
11849
    //Field *field= *reg_field;
 
11850
    uint length;
 
11851
    bzero((uchar*) recinfo,sizeof(*recinfo));
 
11852
    field->move_field(pos,(uchar*) 0,0);
 
11853
 
 
11854
    field->reset();
 
11855
    /*
 
11856
      Test if there is a default field value. The test for ->ptr is to skip
 
11857
      'offset' fields generated by initalize_tables
 
11858
    */
 
11859
    // Initialize the table field:
 
11860
    bzero(field->ptr, field->pack_length());
 
11861
 
 
11862
    length=field->pack_length();
 
11863
    pos+= length;
 
11864
 
 
11865
    /* Make entry for create table */
 
11866
    recinfo->length=length;
 
11867
    if (field->flags & BLOB_FLAG)
 
11868
      recinfo->type= (int) FIELD_BLOB;
 
11869
    else if (use_packed_rows &&
 
11870
             field->real_type() == MYSQL_TYPE_STRING &&
 
11871
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
 
11872
      recinfo->type=FIELD_SKIP_ENDSPACE;
 
11873
    else
 
11874
      recinfo->type=FIELD_NORMAL;
 
11875
 
 
11876
    field->table_name= &table->alias;
 
11877
  }
 
11878
 
 
11879
  //param->recinfo=recinfo;
 
11880
  //store_record(table,s->default_values);        // Make empty default record
 
11881
 
 
11882
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)         // No limit
 
11883
    share->max_rows= ~(ha_rows) 0;
 
11884
  else
 
11885
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
 
11886
                                 min(thd->variables.tmp_table_size,
 
11887
                                     thd->variables.max_heap_table_size) :
 
11888
                                 thd->variables.tmp_table_size) /
 
11889
                                 share->reclength);
 
11890
  set_if_bigger(share->max_rows,1);             // For dummy start options
 
11891
 
 
11892
 
 
11893
  //// keyinfo= param->keyinfo;
 
11894
  if (true)
 
11895
  {
 
11896
    share->keys=1;
 
11897
    share->uniques= test(using_unique_constraint);
 
11898
    table->key_info=keyinfo;
 
11899
    keyinfo->key_part=key_part_info;
 
11900
    keyinfo->flags=HA_NOSAME;
 
11901
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
 
11902
    keyinfo->key_length=0;
 
11903
    keyinfo->rec_per_key=0;
 
11904
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
 
11905
    keyinfo->name= (char*) "weedout_key";
 
11906
    {
 
11907
      key_part_info->null_bit=0;
 
11908
      key_part_info->field=  field;
 
11909
      key_part_info->offset= field->offset(table->record[0]);
 
11910
      key_part_info->length= (uint16) field->key_length();
 
11911
      key_part_info->type=   (uint8) field->key_type();
 
11912
      key_part_info->key_type = FIELDFLAG_BINARY;
 
11913
      if (!using_unique_constraint)
 
11914
      {
 
11915
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
11916
                                              group_buff,
 
11917
                                              field->null_ptr,
 
11918
                                              field->null_bit)))
 
11919
          goto err;
 
11920
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
 
11921
      }
 
11922
      keyinfo->key_length+=  key_part_info->length;
 
11923
    }
 
11924
  }
 
11925
 
 
11926
  if (thd->is_fatal_error)                              // If end of memory
 
11927
    goto err;
 
11928
  share->db_record_offset= 1;
 
11929
  if (share->db_type() == myisam_hton)
 
11930
  {
 
11931
    recinfo++;
 
11932
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
 
11933
      goto err;
 
11934
  }
 
11935
  sjtbl->start_recinfo= start_recinfo;
 
11936
  sjtbl->recinfo=       recinfo;
 
11937
  if (open_tmp_table(table))
 
11938
    goto err;
 
11939
 
 
11940
  thd->mem_root= mem_root_save;
 
11941
  return(table);
 
11942
 
 
11943
err:
 
11944
  thd->mem_root= mem_root_save;
 
11945
  free_tmp_table(thd,table);                    /* purecov: inspected */
 
11946
  if (temp_pool_slot != MY_BIT_NONE)
 
11947
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
 
11948
  return(NULL);                         /* purecov: inspected */
 
11949
}
 
11950
 
 
11951
/****************************************************************************/
 
11952
 
 
11953
/**
 
11954
  Create a reduced TABLE object with properly set up Field list from a
 
11955
  list of field definitions.
 
11956
 
 
11957
    The created table doesn't have a table handler associated with
 
11958
    it, has no keys, no group/distinct, no copy_funcs array.
 
11959
    The sole purpose of this TABLE object is to use the power of Field
 
11960
    class to read/write data to/from table->record[0]. Then one can store
 
11961
    the record in any container (RB tree, hash, etc).
 
11962
    The table is created in THD mem_root, so are the table's fields.
 
11963
    Consequently, if you don't BLOB fields, you don't need to free it.
 
11964
 
 
11965
  @param thd         connection handle
 
11966
  @param field_list  list of column definitions
 
11967
 
 
11968
  @return
 
11969
    0 if out of memory, TABLE object in case of success
 
11970
*/
 
11971
 
 
11972
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
 
11973
{
 
11974
  uint field_count= field_list.elements;
 
11975
  uint blob_count= 0;
 
11976
  Field **field;
 
11977
  Create_field *cdef;                           /* column definition */
 
11978
  uint record_length= 0;
 
11979
  uint null_count= 0;                 /* number of columns which may be null */
 
11980
  uint null_pack_length;              /* NULL representation array length */
 
11981
  uint *blob_field;
 
11982
  uchar *bitmaps;
 
11983
  TABLE *table;
 
11984
  TABLE_SHARE *share;
 
11985
 
 
11986
  if (!multi_alloc_root(thd->mem_root,
 
11987
                        &table, sizeof(*table),
 
11988
                        &share, sizeof(*share),
 
11989
                        &field, (field_count + 1) * sizeof(Field*),
 
11990
                        &blob_field, (field_count+1) *sizeof(uint),
 
11991
                        &bitmaps, bitmap_buffer_size(field_count)*2,
 
11992
                        NullS))
 
11993
    return 0;
 
11994
 
 
11995
  bzero(table, sizeof(*table));
 
11996
  bzero(share, sizeof(*share));
 
11997
  table->field= field;
 
11998
  table->s= share;
 
11999
  share->blob_field= blob_field;
 
12000
  share->fields= field_count;
 
12001
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
12002
  setup_tmp_table_column_bitmaps(table, bitmaps);
 
12003
 
 
12004
  /* Create all fields and calculate the total length of record */
 
12005
  List_iterator_fast<Create_field> it(field_list);
 
12006
  while ((cdef= it++))
 
12007
  {
 
12008
    *field= make_field(share, 0, cdef->length,
 
12009
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
 
12010
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
 
12011
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
 
12012
                       cdef->unireg_check,
 
12013
                       cdef->interval, cdef->field_name);
 
12014
    if (!*field)
 
12015
      goto error;
 
12016
    (*field)->init(table);
 
12017
    record_length+= (*field)->pack_length();
 
12018
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
12019
      null_count++;
 
12020
 
 
12021
    if ((*field)->flags & BLOB_FLAG)
 
12022
      share->blob_field[blob_count++]= (uint) (field - table->field);
 
12023
 
 
12024
    field++;
 
12025
  }
 
12026
  *field= NULL;                             /* mark the end of the list */
 
12027
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
12028
  share->blob_fields= blob_count;
 
12029
 
 
12030
  null_pack_length= (null_count + 7)/8;
 
12031
  share->reclength= record_length + null_pack_length;
 
12032
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
 
12033
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
 
12034
  if (!table->record[0])
 
12035
    goto error;
 
12036
 
 
12037
  if (null_pack_length)
 
12038
  {
 
12039
    table->null_flags= (uchar*) table->record[0];
 
12040
    share->null_fields= null_count;
 
12041
    share->null_bytes= null_pack_length;
 
12042
  }
 
12043
 
 
12044
  table->in_use= thd;           /* field->reset() may access table->in_use */
 
12045
  {
 
12046
    /* Set up field pointers */
 
12047
    uchar *null_pos= table->record[0];
 
12048
    uchar *field_pos= null_pos + share->null_bytes;
 
12049
    uint null_bit= 1;
 
12050
 
 
12051
    for (field= table->field; *field; ++field)
 
12052
    {
 
12053
      Field *cur_field= *field;
 
12054
      if ((cur_field->flags & NOT_NULL_FLAG))
 
12055
        cur_field->move_field(field_pos);
 
12056
      else
 
12057
      {
 
12058
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
 
12059
        null_bit<<= 1;
 
12060
        if (null_bit == (1 << 8))
 
12061
        {
 
12062
          ++null_pos;
 
12063
          null_bit= 1;
 
12064
        }
 
12065
      }
 
12066
      cur_field->reset();
 
12067
 
 
12068
      field_pos+= cur_field->pack_length();
 
12069
    }
 
12070
  }
 
12071
  return table;
 
12072
error:
 
12073
  for (field= table->field; *field; ++field)
 
12074
    delete *field;                         /* just invokes field destructor */
 
12075
  return 0;
 
12076
}
 
12077
 
 
12078
 
 
12079
static bool open_tmp_table(TABLE *table)
 
12080
{
 
12081
  int error;
 
12082
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
 
12083
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
12084
  {
 
12085
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
 
12086
    table->db_stat=0;
 
12087
    return(1);
 
12088
  }
 
12089
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
 
12090
  return(0);
 
12091
}
 
12092
 
 
12093
 
 
12094
/*
 
12095
  Create MyISAM temporary table
 
12096
 
 
12097
  SYNOPSIS
 
12098
    create_myisam_tmp_table()
 
12099
      table           Table object that descrimes the table to be created
 
12100
      keyinfo         Description of the index (there is always one index)
 
12101
      start_recinfo   MyISAM's column descriptions
 
12102
      recinfo INOUT   End of MyISAM's column descriptions
 
12103
      options         Option bits
 
12104
   
 
12105
  DESCRIPTION
 
12106
    Create a MyISAM temporary table according to passed description. The is
 
12107
    assumed to have one unique index or constraint.
 
12108
 
 
12109
    The passed array or MI_COLUMNDEF structures must have this form:
 
12110
 
 
12111
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
12112
         when there are many nullable columns)
 
12113
      2. Table columns
 
12114
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
12115
   
 
12116
    This function may use the free element to create hash column for unique
 
12117
    constraint.
 
12118
 
 
12119
   RETURN
 
12120
     false - OK
 
12121
     true  - Error
 
12122
*/
 
12123
 
 
12124
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
 
12125
                                    MI_COLUMNDEF *start_recinfo,
 
12126
                                    MI_COLUMNDEF **recinfo, 
 
12127
                                    ulonglong options)
 
12128
{
 
12129
  int error;
 
12130
  MI_KEYDEF keydef;
 
12131
  MI_UNIQUEDEF uniquedef;
 
12132
  TABLE_SHARE *share= table->s;
 
12133
 
 
12134
  if (share->keys)
 
12135
  {                                             // Get keys for ni_create
 
12136
    bool using_unique_constraint=0;
 
12137
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
 
12138
                                            sizeof(*seg) * keyinfo->key_parts);
 
12139
    if (!seg)
 
12140
      goto err;
 
12141
 
 
12142
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
 
12143
    if (keyinfo->key_length >= table->file->max_key_length() ||
 
12144
        keyinfo->key_parts > table->file->max_key_parts() ||
 
12145
        share->uniques)
 
12146
    {
 
12147
      /* Can't create a key; Make a unique constraint instead of a key */
 
12148
      share->keys=    0;
 
12149
      share->uniques= 1;
 
12150
      using_unique_constraint=1;
 
12151
      bzero((char*) &uniquedef,sizeof(uniquedef));
 
12152
      uniquedef.keysegs=keyinfo->key_parts;
 
12153
      uniquedef.seg=seg;
 
12154
      uniquedef.null_are_equal=1;
 
12155
 
 
12156
      /* Create extra column for hash value */
 
12157
      bzero((uchar*) *recinfo,sizeof(**recinfo));
 
12158
      (*recinfo)->type= FIELD_CHECK;
 
12159
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
12160
      (*recinfo)++;
 
12161
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
12162
    }
 
12163
    else
 
12164
    {
 
12165
      /* Create an unique key */
 
12166
      bzero((char*) &keydef,sizeof(keydef));
 
12167
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
12168
      keydef.keysegs=  keyinfo->key_parts;
 
12169
      keydef.seg= seg;
 
12170
    }
 
12171
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
 
12172
    {
 
12173
      Field *field=keyinfo->key_part[i].field;
 
12174
      seg->flag=     0;
 
12175
      seg->language= field->charset()->number;
 
12176
      seg->length=   keyinfo->key_part[i].length;
 
12177
      seg->start=    keyinfo->key_part[i].offset;
 
12178
      if (field->flags & BLOB_FLAG)
 
12179
      {
 
12180
        seg->type=
 
12181
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
 
12182
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
12183
        seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
 
12184
        seg->flag= HA_BLOB_PART;
 
12185
        seg->length=0;                  // Whole blob in unique constraint
 
12186
      }
 
12187
      else
 
12188
      {
 
12189
        seg->type= keyinfo->key_part[i].type;
 
12190
        /* Tell handler if it can do suffic space compression */
 
12191
        if (field->real_type() == MYSQL_TYPE_STRING &&
 
12192
            keyinfo->key_part[i].length > 4)
 
12193
          seg->flag|= HA_SPACE_PACK;
 
12194
      }
 
12195
      if (!(field->flags & NOT_NULL_FLAG))
 
12196
      {
 
12197
        seg->null_bit= field->null_bit;
 
12198
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
12199
        /*
 
12200
          We are using a GROUP BY on something that contains NULL
 
12201
          In this case we have to tell MyISAM that two NULL should
 
12202
          on INSERT be regarded at the same value
 
12203
        */
 
12204
        if (!using_unique_constraint)
 
12205
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
12206
      }
 
12207
    }
 
12208
  }
 
12209
  MI_CREATE_INFO create_info;
 
12210
  bzero((char*) &create_info,sizeof(create_info));
 
12211
 
 
12212
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
12213
      OPTION_BIG_TABLES)
 
12214
    create_info.data_file_length= ~(ulonglong) 0;
 
12215
 
 
12216
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
 
12217
                       (uint) (*recinfo-start_recinfo),
 
12218
                       start_recinfo,
 
12219
                       share->uniques, &uniquedef,
 
12220
                       &create_info,
 
12221
                       HA_CREATE_TMP_TABLE)))
 
12222
  {
 
12223
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
 
12224
    table->db_stat=0;
 
12225
    goto err;
 
12226
  }
 
12227
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
 
12228
  share->db_record_offset= 1;
 
12229
  return(0);
 
12230
 err:
 
12231
  return(1);
 
12232
}
 
12233
 
 
12234
 
 
12235
void
 
12236
free_tmp_table(THD *thd, TABLE *entry)
 
12237
{
 
12238
  MEM_ROOT own_root= entry->mem_root;
 
12239
  const char *save_proc_info;
 
12240
 
 
12241
  save_proc_info=thd->proc_info;
 
12242
  thd_proc_info(thd, "removing tmp table");
 
12243
 
 
12244
  if (entry->file)
 
12245
  {
 
12246
    if (entry->db_stat)
 
12247
      entry->file->ha_drop_table(entry->s->table_name.str);
 
12248
    else
 
12249
      entry->file->ha_delete_table(entry->s->table_name.str);
 
12250
    delete entry->file;
 
12251
  }
 
12252
 
 
12253
  /* free blobs */
 
12254
  for (Field **ptr=entry->field ; *ptr ; ptr++)
 
12255
    (*ptr)->free();
 
12256
  free_io_cache(entry);
 
12257
 
 
12258
  if (entry->temp_pool_slot != MY_BIT_NONE)
 
12259
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
 
12260
 
 
12261
  plugin_unlock(0, entry->s->db_plugin);
 
12262
 
 
12263
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
 
12264
  thd_proc_info(thd, save_proc_info);
 
12265
 
 
12266
  return;
 
12267
}
 
12268
 
 
12269
/**
 
12270
  If a HEAP table gets full, create a MyISAM table and copy all rows
 
12271
  to this.
 
12272
*/
 
12273
 
 
12274
bool create_myisam_from_heap(THD *thd, TABLE *table,
 
12275
                             MI_COLUMNDEF *start_recinfo,
 
12276
                             MI_COLUMNDEF **recinfo, 
 
12277
                             int error, bool ignore_last_dupp_key_error)
 
12278
{
 
12279
  TABLE new_table;
 
12280
  TABLE_SHARE share;
 
12281
  const char *save_proc_info;
 
12282
  int write_err;
 
12283
 
 
12284
  if (table->s->db_type() != heap_hton || 
 
12285
      error != HA_ERR_RECORD_FILE_FULL)
 
12286
  {
 
12287
    table->file->print_error(error,MYF(0));
 
12288
    return(1);
 
12289
  }
 
12290
  new_table= *table;
 
12291
  share= *table->s;
 
12292
  new_table.s= &share;
 
12293
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
 
12294
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
 
12295
                                        new_table.s->db_type())))
 
12296
    return(1);                          // End of memory
 
12297
 
 
12298
  save_proc_info=thd->proc_info;
 
12299
  thd_proc_info(thd, "converting HEAP to MyISAM");
 
12300
 
 
12301
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
 
12302
                              recinfo, thd->lex->select_lex.options | 
 
12303
                                               thd->options))
 
12304
    goto err2;
 
12305
  if (open_tmp_table(&new_table))
 
12306
    goto err1;
 
12307
  if (table->file->indexes_are_disabled())
 
12308
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
12309
  table->file->ha_index_or_rnd_end();
 
12310
  table->file->ha_rnd_init(1);
 
12311
  if (table->no_rows)
 
12312
  {
 
12313
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
12314
    new_table.no_rows=1;
 
12315
  }
 
12316
 
 
12317
#ifdef TO_BE_DONE_LATER_IN_4_1
 
12318
  /*
 
12319
    To use start_bulk_insert() (which is new in 4.1) we need to find
 
12320
    all places where a corresponding end_bulk_insert() should be put.
 
12321
  */
 
12322
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
 
12323
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
 
12324
#else
 
12325
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
 
12326
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
12327
#endif
 
12328
 
 
12329
  /*
 
12330
    copy all old rows from heap table to MyISAM table
 
12331
    This is the only code that uses record[1] to read/write but this
 
12332
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
 
12333
  */
 
12334
  while (!table->file->rnd_next(new_table.record[1]))
 
12335
  {
 
12336
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
12337
    if (write_err)
 
12338
      goto err;
 
12339
  }
 
12340
  /* copy row that filled HEAP table */
 
12341
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
12342
  {
 
12343
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
12344
        !ignore_last_dupp_key_error)
 
12345
      goto err;
 
12346
  }
 
12347
 
 
12348
  /* remove heap table and change to use myisam table */
 
12349
  (void) table->file->ha_rnd_end();
 
12350
  (void) table->file->close();                  // This deletes the table !
 
12351
  delete table->file;
 
12352
  table->file=0;
 
12353
  plugin_unlock(0, table->s->db_plugin);
 
12354
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
 
12355
  new_table.s= table->s;                       // Keep old share
 
12356
  *table= new_table;
 
12357
  *table->s= share;
 
12358
  
 
12359
  table->file->change_table_ptr(table, table->s);
 
12360
  table->use_all_columns();
 
12361
  if (save_proc_info)
 
12362
  {
 
12363
    const char *new_proc_info=
 
12364
      (!strcmp(save_proc_info,"Copying to tmp table") ?
 
12365
      "Copying to tmp table on disk" : save_proc_info);
 
12366
    thd_proc_info(thd, new_proc_info);
 
12367
  }
 
12368
  return(0);
 
12369
 
 
12370
 err:
 
12371
  table->file->print_error(write_err, MYF(0));
 
12372
  (void) table->file->ha_rnd_end();
 
12373
  (void) new_table.file->close();
 
12374
 err1:
 
12375
  new_table.file->ha_delete_table(new_table.s->table_name.str);
 
12376
 err2:
 
12377
  delete new_table.file;
 
12378
  thd_proc_info(thd, save_proc_info);
 
12379
  table->mem_root= new_table.mem_root;
 
12380
  return(1);
 
12381
}
 
12382
 
2964
12383
 
2965
12384
/**
2966
12385
  @details
2973
12392
  @return
2974
12393
    end_select function to use. This function can't fail.
2975
12394
*/
2976
 
Next_select_func setup_end_select_func(Join *join)
 
12395
 
 
12396
Next_select_func setup_end_select_func(JOIN *join)
2977
12397
{
2978
 
  Table *table= join->tmp_table;
2979
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
12398
  TABLE *table= join->tmp_table;
 
12399
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2980
12400
  Next_select_func end_select;
2981
12401
 
2982
12402
  /* Set up select_end */
2983
12403
  if (table)
2984
12404
  {
2985
 
    if (table->group && tmp_tbl->sum_func_count &&
 
12405
    if (table->group && tmp_tbl->sum_func_count && 
2986
12406
        !tmp_tbl->precomputed_group_by)
2987
12407
    {
2988
 
      if (table->getShare()->sizeKeys())
 
12408
      if (table->s->keys)
2989
12409
      {
2990
 
        end_select= end_update;
 
12410
        end_select=end_update;
2991
12411
      }
2992
12412
      else
2993
12413
      {
2994
 
        end_select= end_unique_update;
 
12414
        end_select=end_unique_update;
2995
12415
      }
2996
12416
    }
2997
12417
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
2998
12418
    {
2999
 
      end_select= end_write_group;
 
12419
      end_select=end_write_group;
3000
12420
    }
3001
12421
    else
3002
12422
    {
3003
 
      end_select= end_write;
 
12423
      end_select=end_write;
3004
12424
      if (tmp_tbl->precomputed_group_by)
3005
12425
      {
3006
12426
        /*
3007
12427
          A preceding call to create_tmp_table in the case when loose
3008
12428
          index scan is used guarantees that
3009
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
12429
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3010
12430
          by functions. It is OK here to use memcpy since we copy
3011
12431
          Item_sum pointers into an array of Item pointers.
3012
12432
        */
3028
12448
  return end_select;
3029
12449
}
3030
12450
 
 
12451
 
3031
12452
/**
3032
12453
  Make a join of all tables and write it on socket or to table.
3033
12454
 
3038
12459
  @retval
3039
12460
    -1  if error should be sent
3040
12461
*/
3041
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
12462
 
 
12463
static int
 
12464
do_select(JOIN *join,List<Item> *fields,TABLE *table)
3042
12465
{
3043
12466
  int rc= 0;
3044
12467
  enum_nested_loop_state error= NESTED_LOOP_OK;
3045
 
  JoinTable *join_tab= NULL;
3046
 
 
 
12468
  JOIN_TAB *join_tab= NULL;
 
12469
  
3047
12470
  join->tmp_table= table;                       /* Save for easy recursion */
3048
12471
  join->fields= fields;
3049
12472
 
3050
12473
  if (table)
3051
12474
  {
3052
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3053
 
    table->emptyRecord();
 
12475
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
 
12476
    empty_record(table);
3054
12477
    if (table->group && join->tmp_table_param.sum_func_count &&
3055
 
        table->getShare()->sizeKeys() && !table->cursor->inited)
3056
 
      table->cursor->startIndexScan(0, 0);
 
12478
        table->s->keys && !table->file->inited)
 
12479
      table->file->ha_index_init(0, 0);
3057
12480
  }
3058
12481
  /* Set up select_end */
3059
12482
  Next_select_func end_select= setup_end_select_func(join);
3074
12497
    {
3075
12498
      error= (*end_select)(join, 0, 0);
3076
12499
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3077
 
              error= (*end_select)(join, 0, 1);
 
12500
        error= (*end_select)(join, 0, 1);
3078
12501
 
3079
12502
      /*
3080
12503
        If we don't go through evaluate_join_record(), do the counting
3082
12505
        so we don't touch it here.
3083
12506
      */
3084
12507
      join->examined_rows++;
3085
 
      join->session->row_count++;
 
12508
      join->thd->row_count++;
3086
12509
      assert(join->examined_rows <= 1);
3087
12510
    }
3088
12511
    else if (join->send_row_on_empty_set())
3112
12535
    if (!table)                                 // If sending data to client
3113
12536
    {
3114
12537
      /*
3115
 
        The following will unlock all cursors if the command wasn't an
3116
 
        update command
 
12538
        The following will unlock all cursors if the command wasn't an
 
12539
        update command
3117
12540
      */
3118
12541
      join->join_free();                        // Unlock all cursors
3119
12542
      if (join->result->send_eof())
3120
 
        rc= 1;                                  // Don't send error
 
12543
        rc= 1;                                  // Don't send error
3121
12544
    }
3122
12545
  }
3123
12546
  else
3125
12548
  if (table)
3126
12549
  {
3127
12550
    int tmp, new_errno= 0;
3128
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
12551
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3129
12552
    {
3130
12553
      new_errno= tmp;
3131
12554
    }
3132
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
12555
    if ((tmp=table->file->ha_index_or_rnd_end()))
3133
12556
    {
3134
12557
      new_errno= tmp;
3135
12558
    }
3136
12559
    if (new_errno)
3137
 
      table->print_error(new_errno,MYF(0));
 
12560
      table->file->print_error(new_errno,MYF(0));
3138
12561
  }
3139
 
  return(join->session->is_error() ? -1 : rc);
 
12562
  return(join->thd->is_error() ? -1 : rc);
3140
12563
}
3141
12564
 
3142
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
12565
 
 
12566
enum_nested_loop_state
 
12567
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3143
12568
{
3144
12569
  enum_nested_loop_state rc;
3145
12570
 
3150
12575
      rc= sub_select(join,join_tab,end_of_records);
3151
12576
    return rc;
3152
12577
  }
3153
 
  if (join->session->getKilled())               // If aborted by user
 
12578
  if (join->thd->killed)                // If aborted by user
3154
12579
  {
3155
 
    join->session->send_kill_message();
3156
 
    return NESTED_LOOP_KILLED;
 
12580
    join->thd->send_kill_message();
 
12581
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3157
12582
  }
3158
12583
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3159
12584
  {
3160
 
    if (! join_tab->cache.store_record_in_cache())
 
12585
    if (!store_record_in_cache(&join_tab->cache))
3161
12586
      return NESTED_LOOP_OK;                     // There is more room in cache
3162
12587
    return flush_cached_records(join,join_tab,false);
3163
12588
  }
3170
12595
/**
3171
12596
  Retrieve records ends with a given beginning from the result of a join.
3172
12597
 
3173
 
    For a given partial join record consisting of records from the tables
 
12598
    For a given partial join record consisting of records from the tables 
3174
12599
    preceding the table join_tab in the execution plan, the function
3175
12600
    retrieves all matching full records from the result set and
3176
 
    send them to the result set stream.
 
12601
    send them to the result set stream. 
3177
12602
 
3178
12603
  @note
3179
12604
    The function effectively implements the  final (n-k) nested loops
3213
12638
    first row with t3.a=t1.a has been encountered.
3214
12639
    Thus, the second predicate P2 is supplied with a guarded value that are
3215
12640
    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
 
12641
    (table t2). When the first row with t3.a=t1.a for the  current row 
3217
12642
    of table t1  appears, the value becomes true. For now on the predicate
3218
12643
    is evaluated immediately after the row of table t2 has been read.
3219
12644
    When the first row with t3.a=t1.a has been encountered all
3221
12646
    Only when all of them are true the row is sent to the output stream.
3222
12647
    If not, the function returns to the lowest nest level that has a false
3223
12648
    attached condition.
3224
 
    The predicates from on expressions are also pushed down. If in the
 
12649
    The predicates from on expressions are also pushed down. If in the 
3225
12650
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3226
12651
    then t1.a=t2.a would be pushed down to table t2, and without any
3227
12652
    guard.
3231
12656
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3232
12657
    are checked for the composed row almost in the same way as it had
3233
12658
    been done for the first row with a match. The only difference is
3234
 
    the predicates from on expressions are not checked.
 
12659
    the predicates from on expressions are not checked. 
3235
12660
 
3236
12661
  @par
3237
12662
  @b IMPLEMENTATION
3247
12672
    and a pointer to a guarding boolean variable.
3248
12673
    When the value of the guard variable is true the value of the object
3249
12674
    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
 
12675
    true. 
 
12676
    To carry out a return to a nested loop level of join table t the pointer 
3252
12677
    to t is remembered in the field 'return_tab' of the join structure.
3253
12678
    Consider the following query:
3254
12679
    @code
3265
12690
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3266
12691
    becomes 'activated', as well the predicate t4.a=t2.a. But
3267
12692
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3268
 
    t4.a=t2.a becomes true.
 
12693
    t4.a=t2.a becomes true. 
3269
12694
    In order not to re-evaluate the predicates that were already evaluated
3270
12695
    as attached pushed down predicates, a pointer to the the first
3271
12696
    most inner unmatched table is maintained in join_tab->first_unmatched.
3272
12697
    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.
 
12698
    this pointer for t5 is changed from t4 to t2.             
3274
12699
 
3275
12700
    @par
3276
12701
    @b STRUCTURE @b NOTES
3281
12706
  @param join      pointer to the structure providing all context info for
3282
12707
                   the query
3283
12708
  @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
 
12709
  @param end_records  true when we need to perform final steps of retrival   
3285
12710
 
3286
12711
  @return
3287
12712
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3288
12713
*/
3289
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
12714
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
12715
 
 
12716
enum_nested_loop_state
 
12717
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3290
12718
{
3291
12719
  join_tab->table->null_row=0;
3292
12720
  if (end_of_records)
3294
12722
 
3295
12723
  int error;
3296
12724
  enum_nested_loop_state rc;
3297
 
  ReadRecord *info= &join_tab->read_record;
 
12725
  READ_RECORD *info= &join_tab->read_record;
 
12726
 
 
12727
  if (join_tab->flush_weedout_table)
 
12728
  {
 
12729
    do_sj_reset(join_tab->flush_weedout_table);
 
12730
  }
3298
12731
 
3299
12732
  if (join->resume_nested_loop)
3300
12733
  {
3322
12755
      /* Set first_unmatched for the last inner table of this group */
3323
12756
      join_tab->last_inner->first_unmatched= join_tab;
3324
12757
    }
3325
 
    join->session->row_count= 0;
 
12758
    join->thd->row_count= 0;
3326
12759
 
3327
12760
    error= (*join_tab->read_first_record)(join_tab);
3328
12761
    rc= evaluate_join_record(join, join_tab, error);
3329
12762
  }
3330
 
 
3331
 
  /*
3332
 
    Note: psergey has added the 2nd part of the following condition; the
 
12763
  
 
12764
  /* 
 
12765
    Note: psergey has added the 2nd part of the following condition; the 
3333
12766
    change should probably be made in 5.1, too.
3334
12767
  */
3335
12768
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3347
12780
  return rc;
3348
12781
}
3349
12782
 
3350
 
int safe_index_read(JoinTable *tab)
3351
 
{
3352
 
  int error;
3353
 
  Table *table= tab->table;
3354
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
12783
 
 
12784
 
 
12785
 
 
12786
/*
 
12787
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
12788
 
 
12789
  SYNPOSIS
 
12790
    do_sj_dups_weedout()
 
12791
      
 
12792
  RETURN
 
12793
    -1  Error
 
12794
    1   The row combination is a duplicate (discard it)
 
12795
    0   The row combination is not a duplicate (continue)
 
12796
*/
 
12797
 
 
12798
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
12799
{
 
12800
  int error;
 
12801
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
12802
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
12803
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
12804
  uchar *nulls_ptr= ptr;
 
12805
  
 
12806
  /* Put the the rowids tuple into table->record[0]: */
 
12807
 
 
12808
  // 1. Store the length 
 
12809
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
12810
  {
 
12811
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
 
12812
    ptr++;
 
12813
  }
 
12814
  else
 
12815
  {
 
12816
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
12817
    ptr += 2;
 
12818
  }
 
12819
 
 
12820
  // 2. Zero the null bytes 
 
12821
  if (sjtbl->null_bytes)
 
12822
  {
 
12823
    bzero(ptr, sjtbl->null_bytes);
 
12824
    ptr += sjtbl->null_bytes; 
 
12825
  }
 
12826
 
 
12827
  // 3. Put the rowids
 
12828
  for (uint i=0; tab != tab_end; tab++, i++)
 
12829
  {
 
12830
    handler *h= tab->join_tab->table->file;
 
12831
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
12832
    {
 
12833
      /* It's a NULL-complemented row */
 
12834
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
12835
      bzero(ptr + tab->rowid_offset, h->ref_length);
 
12836
    }
 
12837
    else
 
12838
    {
 
12839
      /* Copy the rowid value */
 
12840
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
12841
        h->position(tab->join_tab->table->record[0]);
 
12842
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
12843
    }
 
12844
  }
 
12845
 
 
12846
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
12847
  if (error)
 
12848
  {
 
12849
    /* create_myisam_from_heap will generate error if needed */
 
12850
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
12851
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
12852
                                &sjtbl->recinfo, error, 1))
 
12853
      return -1;
 
12854
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
12855
    return 1;
 
12856
  }
 
12857
  return 0;
 
12858
}
 
12859
 
 
12860
 
 
12861
/*
 
12862
  SemiJoinDuplicateElimination: Reset the temporary table
 
12863
*/
 
12864
 
 
12865
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
12866
{
 
12867
  if (sj_tbl->tmp_table)
 
12868
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
12869
  return 0;
 
12870
}
 
12871
 
 
12872
/*
 
12873
  Process one record of the nested loop join.
 
12874
 
 
12875
    This function will evaluate parts of WHERE/ON clauses that are
 
12876
    applicable to the partial record on hand and in case of success
 
12877
    submit this record to the next level of the nested loop.
 
12878
*/
 
12879
 
 
12880
static enum_nested_loop_state
 
12881
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
12882
                     int error)
 
12883
{
 
12884
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
12885
  ha_rows found_records=join->found_records;
 
12886
  COND *select_cond= join_tab->select_cond;
 
12887
 
 
12888
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
12889
    return NESTED_LOOP_ERROR;
 
12890
  if (error < 0)
 
12891
    return NESTED_LOOP_NO_MORE_ROWS;
 
12892
  if (join->thd->killed)                        // Aborted by user
 
12893
  {
 
12894
    join->thd->send_kill_message();
 
12895
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
12896
  }
 
12897
  if (!select_cond || select_cond->val_int())
 
12898
  {
 
12899
    /*
 
12900
      There is no select condition or the attached pushed down
 
12901
      condition is true => a match is found.
 
12902
    */
 
12903
    bool found= 1;
 
12904
    while (join_tab->first_unmatched && found)
 
12905
    {
 
12906
      /*
 
12907
        The while condition is always false if join_tab is not
 
12908
        the last inner join table of an outer join operation.
 
12909
      */
 
12910
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
12911
      /*
 
12912
        Mark that a match for current outer table is found.
 
12913
        This activates push down conditional predicates attached
 
12914
        to the all inner tables of the outer join.
 
12915
      */
 
12916
      first_unmatched->found= 1;
 
12917
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
12918
      {
 
12919
        if (tab->table->reginfo.not_exists_optimize)
 
12920
          return NESTED_LOOP_NO_MORE_ROWS;
 
12921
        /* Check all predicates that has just been activated. */
 
12922
        /*
 
12923
          Actually all predicates non-guarded by first_unmatched->found
 
12924
          will be re-evaluated again. It could be fixed, but, probably,
 
12925
          it's not worth doing now.
 
12926
        */
 
12927
        if (tab->select_cond && !tab->select_cond->val_int())
 
12928
        {
 
12929
          /* The condition attached to table tab is false */
 
12930
          if (tab == join_tab)
 
12931
            found= 0;
 
12932
          else
 
12933
          {
 
12934
            /*
 
12935
              Set a return point if rejected predicate is attached
 
12936
              not to the last table of the current nest level.
 
12937
            */
 
12938
            join->return_tab= tab;
 
12939
            return NESTED_LOOP_OK;
 
12940
          }
 
12941
        }
 
12942
      }
 
12943
      /*
 
12944
        Check whether join_tab is not the last inner table
 
12945
        for another embedding outer join.
 
12946
      */
 
12947
      if ((first_unmatched= first_unmatched->first_upper) &&
 
12948
          first_unmatched->last_inner != join_tab)
 
12949
        first_unmatched= 0;
 
12950
      join_tab->first_unmatched= first_unmatched;
 
12951
    }
 
12952
 
 
12953
    JOIN_TAB *return_tab= join->return_tab;
 
12954
    join_tab->found_match= true;
 
12955
    if (join_tab->check_weed_out_table)
 
12956
    {
 
12957
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
12958
      if (res == -1)
 
12959
        return NESTED_LOOP_ERROR;
 
12960
      if (res == 1)
 
12961
        return NESTED_LOOP_OK;
 
12962
    }
 
12963
    else if (join_tab->do_firstmatch)
 
12964
    {
 
12965
      /* 
 
12966
        We should return to the join_tab->do_firstmatch after we have 
 
12967
        enumerated all the suffixes for current prefix row combination
 
12968
      */
 
12969
      return_tab= join_tab->do_firstmatch;
 
12970
    }
 
12971
 
 
12972
    /*
 
12973
      It was not just a return to lower loop level when one
 
12974
      of the newly activated predicates is evaluated as false
 
12975
      (See above join->return_tab= tab).
 
12976
    */
 
12977
    join->examined_rows++;
 
12978
    join->thd->row_count++;
 
12979
 
 
12980
    if (found)
 
12981
    {
 
12982
      enum enum_nested_loop_state rc;
 
12983
      /* A match from join_tab is found for the current partial join. */
 
12984
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
12985
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
12986
        return rc;
 
12987
      if (return_tab < join->return_tab)
 
12988
        join->return_tab= return_tab;
 
12989
 
 
12990
      if (join->return_tab < join_tab)
 
12991
        return NESTED_LOOP_OK;
 
12992
      /*
 
12993
        Test if this was a SELECT DISTINCT query on a table that
 
12994
        was not in the field list;  In this case we can abort if
 
12995
        we found a row, as no new rows can be added to the result.
 
12996
      */
 
12997
      if (not_used_in_distinct && found_records != join->found_records)
 
12998
        return NESTED_LOOP_NO_MORE_ROWS;
 
12999
    }
 
13000
    else
 
13001
      join_tab->read_record.file->unlock_row();
 
13002
  }
 
13003
  else
 
13004
  {
 
13005
    /*
 
13006
      The condition pushed down to the table join_tab rejects all rows
 
13007
      with the beginning coinciding with the current partial join.
 
13008
    */
 
13009
    join->examined_rows++;
 
13010
    join->thd->row_count++;
 
13011
    join_tab->read_record.file->unlock_row();
 
13012
  }
 
13013
  return NESTED_LOOP_OK;
 
13014
}
 
13015
 
 
13016
 
 
13017
/**
 
13018
 
 
13019
  @details
 
13020
    Construct a NULL complimented partial join record and feed it to the next
 
13021
    level of the nested loop. This function is used in case we have
 
13022
    an OUTER join and no matching record was found.
 
13023
*/
 
13024
 
 
13025
static enum_nested_loop_state
 
13026
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
13027
{
 
13028
  /*
 
13029
    The table join_tab is the first inner table of a outer join operation
 
13030
    and no matches has been found for the current outer row.
 
13031
  */
 
13032
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
13033
  /* Cache variables for faster loop */
 
13034
  COND *select_cond;
 
13035
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
13036
  {
 
13037
    /* Change the the values of guard predicate variables. */
 
13038
    join_tab->found= 1;
 
13039
    join_tab->not_null_compl= 0;
 
13040
    /* The outer row is complemented by nulls for each inner tables */
 
13041
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
13042
    mark_as_null_row(join_tab->table);       // For group by without error
 
13043
    select_cond= join_tab->select_cond;
 
13044
    /* Check all attached conditions for inner table rows. */
 
13045
    if (select_cond && !select_cond->val_int())
 
13046
      return NESTED_LOOP_OK;
 
13047
  }
 
13048
  join_tab--;
 
13049
  /*
 
13050
    The row complemented by nulls might be the first row
 
13051
    of embedding outer joins.
 
13052
    If so, perform the same actions as in the code
 
13053
    for the first regular outer join row above.
 
13054
  */
 
13055
  for ( ; ; )
 
13056
  {
 
13057
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
13058
    if ((first_unmatched= first_unmatched->first_upper) &&
 
13059
        first_unmatched->last_inner != join_tab)
 
13060
      first_unmatched= 0;
 
13061
    join_tab->first_unmatched= first_unmatched;
 
13062
    if (!first_unmatched)
 
13063
      break;
 
13064
    first_unmatched->found= 1;
 
13065
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
13066
    {
 
13067
      if (tab->select_cond && !tab->select_cond->val_int())
 
13068
      {
 
13069
        join->return_tab= tab;
 
13070
        return NESTED_LOOP_OK;
 
13071
      }
 
13072
    }
 
13073
  }
 
13074
  /*
 
13075
    The row complemented by nulls satisfies all conditions
 
13076
    attached to inner tables.
 
13077
    Send the row complemented by nulls to be joined with the
 
13078
    remaining tables.
 
13079
  */
 
13080
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
13081
}
 
13082
 
 
13083
 
 
13084
static enum_nested_loop_state
 
13085
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
13086
{
 
13087
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
13088
  int error;
 
13089
  READ_RECORD *info;
 
13090
 
 
13091
  join_tab->table->null_row= 0;
 
13092
  if (!join_tab->cache.records)
 
13093
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
13094
  if (skip_last)
 
13095
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
13096
  if (join_tab->use_quick == 2)
 
13097
  {
 
13098
    if (join_tab->select->quick)
 
13099
    {                                   /* Used quick select last. reset it */
 
13100
      delete join_tab->select->quick;
 
13101
      join_tab->select->quick=0;
 
13102
    }
 
13103
  }
 
13104
 /* read through all records */
 
13105
  if ((error=join_init_read_record(join_tab)))
 
13106
  {
 
13107
    reset_cache_write(&join_tab->cache);
 
13108
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
13109
  }
 
13110
 
 
13111
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
13112
  {
 
13113
    tmp->status=tmp->table->status;
 
13114
    tmp->table->status=0;
 
13115
  }
 
13116
 
 
13117
  info= &join_tab->read_record;
 
13118
  do
 
13119
  {
 
13120
    if (join->thd->killed)
 
13121
    {
 
13122
      join->thd->send_kill_message();
 
13123
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
13124
    }
 
13125
    SQL_SELECT *select=join_tab->select;
 
13126
    if (rc == NESTED_LOOP_OK &&
 
13127
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
13128
    {
 
13129
      uint i;
 
13130
      reset_cache_read(&join_tab->cache);
 
13131
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
13132
      {
 
13133
        read_cached_record(join_tab);
 
13134
        if (!select || !select->skip_record())
 
13135
        {
 
13136
          int res= 0;
 
13137
          if (!join_tab->check_weed_out_table || 
 
13138
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
13139
          {
 
13140
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
13141
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
13142
            {
 
13143
              reset_cache_write(&join_tab->cache);
 
13144
              return rc;
 
13145
            }
 
13146
          }
 
13147
          if (res == -1)
 
13148
            return NESTED_LOOP_ERROR;
 
13149
        }
 
13150
      }
 
13151
    }
 
13152
  } while (!(error=info->read_record(info)));
 
13153
 
 
13154
  if (skip_last)
 
13155
    read_cached_record(join_tab);               // Restore current record
 
13156
  reset_cache_write(&join_tab->cache);
 
13157
  if (error > 0)                                // Fatal error
 
13158
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
13159
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
13160
    tmp2->table->status=tmp2->status;
 
13161
  return NESTED_LOOP_OK;
 
13162
}
 
13163
 
 
13164
 
 
13165
/*****************************************************************************
 
13166
  The different ways to read a record
 
13167
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
13168
*****************************************************************************/
 
13169
 
 
13170
/** Help function when we get some an error from the table handler. */
 
13171
 
 
13172
int report_error(TABLE *table, int error)
 
13173
{
 
13174
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
13175
  {
 
13176
    table->status= STATUS_GARBAGE;
 
13177
    return -1;                                  // key not found; ok
 
13178
  }
 
13179
  /*
 
13180
    Locking reads can legally return also these errors, do not
 
13181
    print them to the .err log
 
13182
  */
 
13183
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
13184
    sql_print_error("Got error %d when reading table '%s'",
 
13185
                    error, table->s->path.str);
 
13186
  table->file->print_error(error,MYF(0));
 
13187
  return 1;
 
13188
}
 
13189
 
 
13190
 
 
13191
int safe_index_read(JOIN_TAB *tab)
 
13192
{
 
13193
  int error;
 
13194
  TABLE *table= tab->table;
 
13195
  if ((error=table->file->index_read_map(table->record[0],
3355
13196
                                         tab->ref.key_buff,
3356
13197
                                         make_prev_keypart_map(tab->ref.key_parts),
3357
13198
                                         HA_READ_KEY_EXACT)))
3358
 
    return table->report_error(error);
 
13199
    return report_error(table, error);
3359
13200
  return 0;
3360
13201
}
3361
13202
 
3362
 
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
13203
 
 
13204
static int
 
13205
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3363
13206
{
3364
13207
  int error;
3365
 
  Table *table=tab->table;
 
13208
  TABLE *table=tab->table;
3366
13209
  table->const_table=1;
3367
13210
  table->null_row=0;
3368
13211
  table->status=STATUS_NO_RECORD;
3369
 
 
3370
 
  if (tab->type == AM_SYSTEM)
 
13212
  
 
13213
  if (tab->type == JT_SYSTEM)
3371
13214
  {
3372
13215
    if ((error=join_read_system(tab)))
3373
13216
    {                                           // Info for DESCRIBE
3374
13217
      tab->info="const row not found";
3375
13218
      /* 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);
 
13219
      pos->records_read=0.0;
 
13220
      pos->ref_depend_map= 0;
 
13221
      if (!table->maybe_null || error > 0)
 
13222
        return(error);
3380
13223
    }
3381
13224
  }
3382
13225
  else
3383
13226
  {
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)
 
13227
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
13228
        !table->no_keyread &&
 
13229
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3388
13230
    {
3389
13231
      table->key_read=1;
3390
 
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
13232
      table->file->extra(HA_EXTRA_KEYREAD);
3391
13233
      tab->index= tab->ref.key;
3392
13234
    }
3393
13235
    error=join_read_const(tab);
3394
13236
    if (table->key_read)
3395
13237
    {
3396
13238
      table->key_read=0;
3397
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13239
      table->file->extra(HA_EXTRA_NO_KEYREAD);
3398
13240
    }
3399
13241
    if (error)
3400
13242
    {
3401
13243
      tab->info="unique row not found";
3402
13244
      /* Mark for EXPLAIN that the row was not found */
3403
 
      pos->setFanout(0.0);
3404
 
      pos->clearRefDependMap();
 
13245
      pos->records_read=0.0;
 
13246
      pos->ref_depend_map= 0;
3405
13247
      if (!table->maybe_null || error > 0)
3406
 
        return(error);
 
13248
        return(error);
3407
13249
    }
3408
13250
  }
3409
13251
  if (*tab->on_expr_ref && !table->null_row)
3410
13252
  {
3411
13253
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3412
 
      table->mark_as_null_row();
 
13254
      mark_as_null_row(table);  
3413
13255
  }
3414
13256
  if (!table->null_row)
3415
13257
    table->maybe_null=0;
3416
13258
 
3417
13259
  /* Check appearance of new constant items in Item_equal objects */
3418
 
  Join *join= tab->join;
 
13260
  JOIN *join= tab->join;
3419
13261
  if (join->conds)
3420
13262
    update_const_equal_items(join->conds, tab);
3421
 
  TableList *tbl;
 
13263
  TABLE_LIST *tbl;
3422
13264
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
3423
13265
  {
3424
 
    TableList *embedded;
3425
 
    TableList *embedding= tbl;
 
13266
    TABLE_LIST *embedded;
 
13267
    TABLE_LIST *embedding= tbl;
3426
13268
    do
3427
13269
    {
3428
13270
      embedded= embedding;
3429
13271
      if (embedded->on_expr)
3430
13272
         update_const_equal_items(embedded->on_expr, tab);
3431
 
      embedding= embedded->getEmbedding();
 
13273
      embedding= embedded->embedding;
3432
13274
    }
3433
13275
    while (embedding &&
3434
 
           embedding->getNestedJoin()->join_list.head() == embedded);
 
13276
           embedding->nested_join->join_list.head() == embedded);
3435
13277
  }
3436
13278
 
3437
13279
  return(0);
3438
13280
}
3439
13281
 
3440
 
int join_read_system(JoinTable *tab)
 
13282
 
 
13283
static int
 
13284
join_read_system(JOIN_TAB *tab)
3441
13285
{
3442
 
  Table *table= tab->table;
 
13286
  TABLE *table= tab->table;
3443
13287
  int error;
3444
13288
  if (table->status & STATUS_GARBAGE)           // If first read
3445
13289
  {
3446
 
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3447
 
                                           table->getShare()->getPrimaryKey())))
 
13290
    if ((error=table->file->read_first_row(table->record[0],
 
13291
                                           table->s->primary_key)))
3448
13292
    {
3449
13293
      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
 
13294
        return report_error(table, error);
 
13295
      mark_as_null_row(tab->table);
 
13296
      empty_record(table);                      // Make empty record
3453
13297
      return -1;
3454
13298
    }
3455
 
    table->storeRecord();
 
13299
    store_record(table,record[1]);
3456
13300
  }
3457
13301
  else if (!table->status)                      // Only happens with left join
3458
 
    table->restoreRecord();                     // restore old record
 
13302
    restore_record(table,record[1]);                    // restore old record
3459
13303
  table->null_row=0;
3460
13304
  return table->status ? -1 : 0;
3461
13305
}
3462
13306
 
 
13307
 
3463
13308
/**
3464
13309
  Read a (constant) table when there is at most one matching row.
3465
13310
 
3472
13317
  @retval
3473
13318
    1   Got an error (other than row not found) during read
3474
13319
*/
3475
 
int join_read_const(JoinTable *tab)
 
13320
 
 
13321
static int
 
13322
join_read_const(JOIN_TAB *tab)
3476
13323
{
3477
13324
  int error;
3478
 
  Table *table= tab->table;
 
13325
  TABLE *table= tab->table;
3479
13326
  if (table->status & STATUS_GARBAGE)           // If first read
3480
13327
  {
3481
13328
    table->status= 0;
3482
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3483
 
      error= HA_ERR_KEY_NOT_FOUND;
 
13329
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
13330
      error=HA_ERR_KEY_NOT_FOUND;
3484
13331
    else
3485
13332
    {
3486
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3487
 
                                            (unsigned char*) tab->ref.key_buff,
 
13333
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
13334
                                            (uchar*) tab->ref.key_buff,
3488
13335
                                            make_prev_keypart_map(tab->ref.key_parts),
3489
13336
                                            HA_READ_KEY_EXACT);
3490
13337
    }
3491
13338
    if (error)
3492
13339
    {
3493
13340
      table->status= STATUS_NOT_FOUND;
3494
 
      tab->table->mark_as_null_row();
3495
 
      table->emptyRecord();
 
13341
      mark_as_null_row(tab->table);
 
13342
      empty_record(table);
3496
13343
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3497
 
        return table->report_error(error);
 
13344
        return report_error(table, error);
3498
13345
      return -1;
3499
13346
    }
3500
 
    table->storeRecord();
 
13347
    store_record(table,record[1]);
3501
13348
  }
3502
13349
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3503
13350
  {
3504
13351
    table->status=0;
3505
 
    table->restoreRecord();                     // restore old record
 
13352
    restore_record(table,record[1]);                    // restore old record
3506
13353
  }
3507
13354
  table->null_row=0;
3508
13355
  return table->status ? -1 : 0;
3509
13356
}
3510
13357
 
 
13358
 
3511
13359
/*
3512
13360
  eq_ref access method implementation: "read_first" function
3513
13361
 
3514
13362
  SYNOPSIS
3515
13363
    join_read_key()
3516
 
      tab  JoinTable of the accessed table
 
13364
      tab  JOIN_TAB of the accessed table
3517
13365
 
3518
13366
  DESCRIPTION
3519
13367
    This is "read_fist" function for the "ref" access method. The difference
3521
13369
 
3522
13370
  RETURN
3523
13371
    0  - Ok
3524
 
   -1  - Row not found
 
13372
   -1  - Row not found 
3525
13373
    1  - Error
3526
13374
*/
3527
 
int join_read_key(JoinTable *tab)
 
13375
 
 
13376
static int
 
13377
join_read_key(JOIN_TAB *tab)
3528
13378
{
3529
13379
  int error;
3530
 
  Table *table= tab->table;
 
13380
  TABLE *table= tab->table;
3531
13381
 
3532
 
  if (!table->cursor->inited)
 
13382
  if (!table->file->inited)
3533
13383
  {
3534
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
13384
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3535
13385
  }
3536
13386
 
3537
13387
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3543
13393
      table->status=STATUS_NOT_FOUND;
3544
13394
      return -1;
3545
13395
    }
3546
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
13396
    error=table->file->index_read_map(table->record[0],
3547
13397
                                      tab->ref.key_buff,
3548
13398
                                      make_prev_keypart_map(tab->ref.key_parts),
3549
13399
                                      HA_READ_KEY_EXACT);
3550
13400
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3551
 
      return table->report_error(error);
 
13401
      return report_error(table, error);
3552
13402
  }
3553
13403
  table->null_row=0;
3554
13404
  return table->status ? -1 : 0;
3555
13405
}
3556
13406
 
 
13407
 
3557
13408
/*
3558
13409
  ref access method implementation: "read_first" function
3559
13410
 
3560
13411
  SYNOPSIS
3561
13412
    join_read_always_key()
3562
 
      tab  JoinTable of the accessed table
 
13413
      tab  JOIN_TAB of the accessed table
3563
13414
 
3564
13415
  DESCRIPTION
3565
 
    This is "read_first" function for the "ref" access method.
3566
 
 
 
13416
    This is "read_fist" function for the "ref" access method.
 
13417
   
3567
13418
    The functon must leave the index initialized when it returns.
3568
13419
    ref_or_null access implementation depends on that.
3569
13420
 
3570
13421
  RETURN
3571
13422
    0  - Ok
3572
 
   -1  - Row not found
 
13423
   -1  - Row not found 
3573
13424
    1  - Error
3574
13425
*/
3575
 
int join_read_always_key(JoinTable *tab)
 
13426
 
 
13427
static int
 
13428
join_read_always_key(JOIN_TAB *tab)
3576
13429
{
3577
13430
  int error;
3578
 
  Table *table= tab->table;
 
13431
  TABLE *table= tab->table;
3579
13432
 
3580
13433
  /* Initialize the index first */
3581
 
  if (!table->cursor->inited)
3582
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3583
 
 
 
13434
  if (!table->file->inited)
 
13435
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13436
 
3584
13437
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3585
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
13438
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
3586
13439
  {
3587
13440
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3588
13441
        return -1;
3589
13442
  }
3590
13443
 
3591
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
13444
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3592
13445
    return -1;
3593
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
13446
  if ((error=table->file->index_read_map(table->record[0],
3594
13447
                                         tab->ref.key_buff,
3595
13448
                                         make_prev_keypart_map(tab->ref.key_parts),
3596
13449
                                         HA_READ_KEY_EXACT)))
3597
13450
  {
3598
13451
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3599
 
      return table->report_error(error);
3600
 
    return -1;
 
13452
      return report_error(table, error);
 
13453
    return -1; /* purecov: inspected */
3601
13454
  }
3602
 
 
3603
13455
  return 0;
3604
13456
}
3605
13457
 
 
13458
 
3606
13459
/**
3607
 
  This function is used when optimizing away ORDER BY in
 
13460
  This function is used when optimizing away ORDER BY in 
3608
13461
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
3609
13462
*/
3610
 
int join_read_last_key(JoinTable *tab)
 
13463
  
 
13464
static int
 
13465
join_read_last_key(JOIN_TAB *tab)
3611
13466
{
3612
13467
  int error;
3613
 
  Table *table= tab->table;
 
13468
  TABLE *table= tab->table;
3614
13469
 
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))
 
13470
  if (!table->file->inited)
 
13471
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
13472
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3618
13473
    return -1;
3619
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
13474
  if ((error=table->file->index_read_last_map(table->record[0],
3620
13475
                                              tab->ref.key_buff,
3621
13476
                                              make_prev_keypart_map(tab->ref.key_parts))))
3622
13477
  {
3623
13478
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3624
 
      return table->report_error(error);
3625
 
    return -1;
 
13479
      return report_error(table, error);
 
13480
    return -1; /* purecov: inspected */
3626
13481
  }
3627
13482
  return 0;
3628
13483
}
3629
13484
 
3630
 
int join_no_more_records(ReadRecord *)
 
13485
 
 
13486
        /* ARGSUSED */
 
13487
static int
 
13488
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3631
13489
{
3632
13490
  return -1;
3633
13491
}
3634
13492
 
3635
 
int join_read_next_same_diff(ReadRecord *info)
 
13493
static int
 
13494
join_read_next_same_diff(READ_RECORD *info)
3636
13495
{
3637
 
  Table *table= info->table;
3638
 
  JoinTable *tab=table->reginfo.join_tab;
 
13496
  TABLE *table= info->table;
 
13497
  JOIN_TAB *tab=table->reginfo.join_tab;
3639
13498
  if (tab->insideout_match_tab->found_match)
3640
13499
  {
3641
 
    KeyInfo *key= tab->table->key_info + tab->index;
3642
 
    do
 
13500
    KEY *key= tab->table->key_info + tab->index;
 
13501
    do 
3643
13502
    {
3644
13503
      int error;
3645
13504
      /* Save index tuple from record to the buffer */
3646
13505
      key_copy(tab->insideout_buf, info->record, key, 0);
3647
13506
 
3648
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13507
      if ((error=table->file->index_next_same(table->record[0],
3649
13508
                                              tab->ref.key_buff,
3650
13509
                                              tab->ref.key_length)))
3651
13510
      {
3652
13511
        if (error != HA_ERR_END_OF_FILE)
3653
 
          return table->report_error(error);
 
13512
          return report_error(table, error);
3654
13513
        table->status= STATUS_GARBAGE;
3655
13514
        return -1;
3656
13515
      }
3657
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13516
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3658
13517
                      tab->insideout_buf, key->key_length));
3659
13518
    tab->insideout_match_tab->found_match= 0;
3660
13519
    return 0;
3663
13522
    return join_read_next_same(info);
3664
13523
}
3665
13524
 
3666
 
int join_read_next_same(ReadRecord *info)
 
13525
static int
 
13526
join_read_next_same(READ_RECORD *info)
3667
13527
{
3668
13528
  int error;
3669
 
  Table *table= info->table;
3670
 
  JoinTable *tab=table->reginfo.join_tab;
 
13529
  TABLE *table= info->table;
 
13530
  JOIN_TAB *tab=table->reginfo.join_tab;
3671
13531
 
3672
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
13532
  if ((error=table->file->index_next_same(table->record[0],
3673
13533
                                          tab->ref.key_buff,
3674
13534
                                          tab->ref.key_length)))
3675
13535
  {
3676
13536
    if (error != HA_ERR_END_OF_FILE)
3677
 
      return table->report_error(error);
 
13537
      return report_error(table, error);
3678
13538
    table->status= STATUS_GARBAGE;
3679
13539
    return -1;
3680
13540
  }
3681
 
 
3682
13541
  return 0;
3683
13542
}
3684
13543
 
3685
 
int join_read_prev_same(ReadRecord *info)
 
13544
 
 
13545
static int
 
13546
join_read_prev_same(READ_RECORD *info)
3686
13547
{
3687
13548
  int error;
3688
 
  Table *table= info->table;
3689
 
  JoinTable *tab=table->reginfo.join_tab;
 
13549
  TABLE *table= info->table;
 
13550
  JOIN_TAB *tab=table->reginfo.join_tab;
3690
13551
 
3691
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3692
 
    return table->report_error(error);
 
13552
  if ((error=table->file->index_prev(table->record[0])))
 
13553
    return report_error(table, error);
3693
13554
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3694
13555
                      tab->ref.key_length))
3695
13556
  {
3699
13560
  return error;
3700
13561
}
3701
13562
 
3702
 
int join_init_quick_read_record(JoinTable *tab)
 
13563
 
 
13564
static int
 
13565
join_init_quick_read_record(JOIN_TAB *tab)
3703
13566
{
3704
13567
  if (test_if_quick_select(tab) == -1)
3705
13568
    return -1;                                  /* No possible records */
3706
13569
  return join_init_read_record(tab);
3707
13570
}
3708
13571
 
3709
 
int init_read_record_seq(JoinTable *tab)
 
13572
 
 
13573
int rr_sequential(READ_RECORD *info);
 
13574
int init_read_record_seq(JOIN_TAB *tab)
3710
13575
{
3711
 
  tab->read_record.init_reard_record_sequential();
3712
 
 
3713
 
  if (tab->read_record.cursor->startTableScan(1))
 
13576
  tab->read_record.read_record= rr_sequential;
 
13577
  if (tab->read_record.file->ha_rnd_init(1))
3714
13578
    return 1;
3715
13579
  return (*tab->read_record.read_record)(&tab->read_record);
3716
13580
}
3717
13581
 
3718
 
int test_if_quick_select(JoinTable *tab)
 
13582
static int
 
13583
test_if_quick_select(JOIN_TAB *tab)
3719
13584
{
3720
13585
  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);
 
13586
  tab->select->quick=0;
 
13587
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
13588
                                        (table_map) 0, HA_POS_ERROR, 0,
 
13589
                                        false);
3724
13590
}
3725
13591
 
3726
 
int join_init_read_record(JoinTable *tab)
 
13592
 
 
13593
static int
 
13594
join_init_read_record(JOIN_TAB *tab)
3727
13595
{
3728
13596
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3729
13597
    return 1;
3730
 
 
3731
 
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
3732
 
 
 
13598
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
13599
                   tab->select,1,1);
3733
13600
  return (*tab->read_record.read_record)(&tab->read_record);
3734
13601
}
3735
13602
 
3736
 
int join_read_first(JoinTable *tab)
 
13603
 
 
13604
static int
 
13605
join_read_first(JOIN_TAB *tab)
3737
13606
{
3738
13607
  int error;
3739
 
  Table *table=tab->table;
3740
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13608
  TABLE *table=tab->table;
 
13609
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3741
13610
      !table->no_keyread)
3742
13611
  {
3743
 
    table->key_read= 1;
3744
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13612
    table->key_read=1;
 
13613
    table->file->extra(HA_EXTRA_KEYREAD);
3745
13614
  }
3746
 
  tab->table->status= 0;
 
13615
  tab->table->status=0;
3747
13616
  tab->read_record.table=table;
3748
 
  tab->read_record.cursor=table->cursor;
 
13617
  tab->read_record.file=table->file;
3749
13618
  tab->read_record.index=tab->index;
3750
 
  tab->read_record.record=table->getInsertRecord();
 
13619
  tab->read_record.record=table->record[0];
3751
13620
  if (tab->insideout_match_tab)
3752
13621
  {
3753
13622
    tab->read_record.do_insideout_scan= tab;
3760
13629
    tab->read_record.do_insideout_scan= 0;
3761
13630
  }
3762
13631
 
3763
 
  if (!table->cursor->inited)
3764
 
    table->cursor->startIndexScan(tab->index, tab->sorted);
3765
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
13632
  if (!table->file->inited)
 
13633
    table->file->ha_index_init(tab->index, tab->sorted);
 
13634
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3766
13635
  {
3767
13636
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3768
 
      table->report_error(error);
 
13637
      report_error(table, error);
3769
13638
    return -1;
3770
13639
  }
3771
 
 
3772
13640
  return 0;
3773
13641
}
3774
13642
 
3775
 
int join_read_next_different(ReadRecord *info)
 
13643
 
 
13644
static int
 
13645
join_read_next_different(READ_RECORD *info)
3776
13646
{
3777
 
  JoinTable *tab= info->do_insideout_scan;
 
13647
  JOIN_TAB *tab= info->do_insideout_scan;
3778
13648
  if (tab->insideout_match_tab->found_match)
3779
13649
  {
3780
 
    KeyInfo *key= tab->table->key_info + tab->index;
3781
 
    do
 
13650
    KEY *key= tab->table->key_info + tab->index;
 
13651
    do 
3782
13652
    {
3783
13653
      int error;
3784
13654
      /* Save index tuple from record to the buffer */
3785
13655
      key_copy(tab->insideout_buf, info->record, key, 0);
3786
13656
 
3787
 
      if ((error=info->cursor->index_next(info->record)))
3788
 
        return info->table->report_error(error);
3789
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
13657
      if ((error=info->file->index_next(info->record)))
 
13658
        return report_error(info->table, error);
 
13659
      
 
13660
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3790
13661
                      tab->insideout_buf, key->key_length));
3791
13662
    tab->insideout_match_tab->found_match= 0;
3792
13663
    return 0;
3795
13666
    return join_read_next(info);
3796
13667
}
3797
13668
 
3798
 
int join_read_next(ReadRecord *info)
 
13669
 
 
13670
static int
 
13671
join_read_next(READ_RECORD *info)
3799
13672
{
3800
13673
  int error;
3801
 
  if ((error=info->cursor->index_next(info->record)))
3802
 
    return info->table->report_error(error);
 
13674
  if ((error=info->file->index_next(info->record)))
 
13675
    return report_error(info->table, error);
3803
13676
  return 0;
3804
13677
}
3805
13678
 
3806
 
int join_read_last(JoinTable *tab)
 
13679
 
 
13680
static int
 
13681
join_read_last(JOIN_TAB *tab)
3807
13682
{
3808
 
  Table *table=tab->table;
 
13683
  TABLE *table=tab->table;
3809
13684
  int error;
3810
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
13685
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3811
13686
      !table->no_keyread)
3812
13687
  {
3813
13688
    table->key_read=1;
3814
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
13689
    table->file->extra(HA_EXTRA_KEYREAD);
3815
13690
  }
3816
13691
  tab->table->status=0;
3817
13692
  tab->read_record.read_record=join_read_prev;
3818
13693
  tab->read_record.table=table;
3819
 
  tab->read_record.cursor=table->cursor;
 
13694
  tab->read_record.file=table->file;
3820
13695
  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())))
3825
 
    return table->report_error(error);
3826
 
 
 
13696
  tab->read_record.record=table->record[0];
 
13697
  if (!table->file->inited)
 
13698
    table->file->ha_index_init(tab->index, 1);
 
13699
  if ((error= tab->table->file->index_last(tab->table->record[0])))
 
13700
    return report_error(table, error);
3827
13701
  return 0;
3828
13702
}
3829
13703
 
3830
 
int join_read_prev(ReadRecord *info)
 
13704
 
 
13705
static int
 
13706
join_read_prev(READ_RECORD *info)
3831
13707
{
3832
13708
  int error;
3833
 
  if ((error= info->cursor->index_prev(info->record)))
3834
 
    return info->table->report_error(error);
3835
 
 
 
13709
  if ((error= info->file->index_prev(info->record)))
 
13710
    return report_error(info->table, error);
3836
13711
  return 0;
3837
13712
}
3838
13713
 
3839
13714
/**
3840
13715
  Reading of key with key reference and one part that may be NULL.
3841
13716
*/
3842
 
int join_read_always_key_or_null(JoinTable *tab)
 
13717
 
 
13718
int
 
13719
join_read_always_key_or_null(JOIN_TAB *tab)
3843
13720
{
3844
13721
  int res;
3845
13722
 
3853
13730
  return safe_index_read(tab);
3854
13731
}
3855
13732
 
3856
 
int join_read_next_same_or_null(ReadRecord *info)
 
13733
 
 
13734
int
 
13735
join_read_next_same_or_null(READ_RECORD *info)
3857
13736
{
3858
13737
  int error;
3859
13738
  if ((error= join_read_next_same(info)) >= 0)
3860
13739
    return error;
3861
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
13740
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3862
13741
 
3863
13742
  /* Test if we have already done a read after null key */
3864
13743
  if (*tab->ref.null_ref_key)
3867
13746
  return safe_index_read(tab);                  // then read null keys
3868
13747
}
3869
13748
 
3870
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
13749
 
 
13750
/*****************************************************************************
 
13751
  DESCRIPTION
 
13752
    Functions that end one nested loop iteration. Different functions
 
13753
    are used to support GROUP BY clause and to redirect records
 
13754
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
13755
    network client.
 
13756
 
 
13757
  RETURN VALUES
 
13758
    NESTED_LOOP_OK           - the record has been successfully handled
 
13759
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
13760
                               was detected
 
13761
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
13762
                               the record
 
13763
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
13764
                               additionally, the nested loop produced the
 
13765
                               number of rows specified in the LIMIT clause
 
13766
                               for the query
 
13767
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
13768
                               additionally, there is a cursor and the nested
 
13769
                               loop algorithm produced the number of rows
 
13770
                               that is specified for current cursor fetch
 
13771
                               operation.
 
13772
   All return values except NESTED_LOOP_OK abort the nested loop.
 
13773
*****************************************************************************/
 
13774
 
 
13775
/* ARGSUSED */
 
13776
static enum_nested_loop_state
 
13777
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13778
         bool end_of_records)
 
13779
{
 
13780
  if (!end_of_records)
 
13781
  {
 
13782
    int error;
 
13783
    if (join->having && join->having->val_int() == 0)
 
13784
      return(NESTED_LOOP_OK);               // Didn't match having
 
13785
    error=0;
 
13786
    if (join->do_send_rows)
 
13787
      error=join->result->send_data(*join->fields);
 
13788
    if (error)
 
13789
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
13790
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
13791
        join->do_send_rows)
 
13792
    {
 
13793
      if (join->select_options & OPTION_FOUND_ROWS)
 
13794
      {
 
13795
        JOIN_TAB *jt=join->join_tab;
 
13796
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
13797
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
13798
            !(jt->select && jt->select->quick) &&
 
13799
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
13800
            (jt->ref.key < 0))
 
13801
        {
 
13802
          /* Join over all rows in table;  Return number of found rows */
 
13803
          TABLE *table=jt->table;
 
13804
 
 
13805
          join->select_options ^= OPTION_FOUND_ROWS;
 
13806
          if (table->sort.record_pointers ||
 
13807
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
13808
          {
 
13809
            /* Using filesort */
 
13810
            join->send_records= table->sort.found_records;
 
13811
          }
 
13812
          else
 
13813
          {
 
13814
            table->file->info(HA_STATUS_VARIABLE);
 
13815
            join->send_records= table->file->stats.records;
 
13816
          }
 
13817
        }
 
13818
        else 
 
13819
        {
 
13820
          join->do_send_rows= 0;
 
13821
          if (join->unit->fake_select_lex)
 
13822
            join->unit->fake_select_lex->select_limit= 0;
 
13823
          return(NESTED_LOOP_OK);
 
13824
        }
 
13825
      }
 
13826
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
13827
    }
 
13828
    else if (join->send_records >= join->fetch_limit)
 
13829
    {
 
13830
      /*
 
13831
        There is a server side cursor and all rows for
 
13832
        this fetch request are sent.
 
13833
      */
 
13834
      return(NESTED_LOOP_CURSOR_LIMIT);
 
13835
    }
 
13836
  }
 
13837
 
 
13838
  return(NESTED_LOOP_OK);
 
13839
}
 
13840
 
 
13841
 
 
13842
        /* ARGSUSED */
 
13843
enum_nested_loop_state
 
13844
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13845
               bool end_of_records)
3871
13846
{
3872
13847
  int idx= -1;
3873
13848
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3875
13850
  if (!join->first_record || end_of_records ||
3876
13851
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3877
13852
  {
3878
 
    if (join->first_record ||
 
13853
    if (join->first_record || 
3879
13854
        (end_of_records && !join->group && !join->group_optimized_away))
3880
13855
    {
3881
13856
      if (idx < (int) join->send_group_parts)
3882
13857
      {
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();
 
13858
        int error=0;
 
13859
        {
 
13860
          if (!join->first_record)
 
13861
          {
 
13862
            List_iterator_fast<Item> it(*join->fields);
 
13863
            Item *item;
 
13864
            /* No matching rows for group function */
 
13865
            join->clear();
3891
13866
 
3892
13867
            while ((item= it++))
3893
13868
              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;
 
13869
          }
 
13870
          if (join->having && join->having->val_int() == 0)
 
13871
            error= -1;                          // Didn't satisfy having
 
13872
          else
 
13873
          {
 
13874
            if (join->do_send_rows)
 
13875
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
13876
            join->send_records++;
 
13877
          }
 
13878
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
13879
          {
 
13880
            if (join->rollup_send_data((uint) (idx+1)))
 
13881
              error= 1;
 
13882
          }
 
13883
        }
 
13884
        if (error > 0)
 
13885
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
13886
        if (end_of_records)
 
13887
          return(NESTED_LOOP_OK);
 
13888
        if (join->send_records >= join->unit->select_limit_cnt &&
 
13889
            join->do_send_rows)
 
13890
        {
 
13891
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
13892
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
13893
          join->do_send_rows=0;
 
13894
          join->unit->select_limit_cnt = HA_POS_ERROR;
3920
13895
        }
3921
13896
        else if (join->send_records >= join->fetch_limit)
3922
13897
        {
3935
13910
    else
3936
13911
    {
3937
13912
      if (end_of_records)
3938
 
        return(NESTED_LOOP_OK);
 
13913
        return(NESTED_LOOP_OK);
3939
13914
      join->first_record=1;
3940
 
      test_if_item_cache_changed(join->group_fields);
 
13915
      VOID(test_if_item_cache_changed(join->group_fields));
3941
13916
    }
3942
13917
    if (idx < (int) join->send_group_parts)
3943
13918
    {
3947
13922
      */
3948
13923
      copy_fields(&join->tmp_table_param);
3949
13924
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
 
        return(NESTED_LOOP_ERROR);
 
13925
        return(NESTED_LOOP_ERROR);
3951
13926
      return(ok_code);
3952
13927
    }
3953
13928
  }
3956
13931
  return(NESTED_LOOP_OK);
3957
13932
}
3958
13933
 
3959
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
3960
 
{
3961
 
  Table *table=join->tmp_table;
 
13934
 
 
13935
        /* ARGSUSED */
 
13936
enum_nested_loop_state
 
13937
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
13938
          bool end_of_records)
 
13939
{
 
13940
  TABLE *table=join->tmp_table;
 
13941
 
 
13942
  if (join->thd->killed)                        // Aborted by user
 
13943
  {
 
13944
    join->thd->send_kill_message();
 
13945
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
13946
  }
 
13947
  if (!end_of_records)
 
13948
  {
 
13949
    copy_fields(&join->tmp_table_param);
 
13950
    copy_funcs(join->tmp_table_param.items_to_copy);
 
13951
#ifdef TO_BE_DELETED
 
13952
    if (!table->uniques)                        // If not unique handling
 
13953
    {
 
13954
      /* Copy null values from group to row */
 
13955
      ORDER   *group;
 
13956
      for (group=table->group ; group ; group=group->next)
 
13957
      {
 
13958
        Item *item= *group->item;
 
13959
        if (item->maybe_null)
 
13960
        {
 
13961
          Field *field=item->get_tmp_table_field();
 
13962
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
 
13963
        }
 
13964
      }
 
13965
    }
 
13966
#endif
 
13967
    if (!join->having || join->having->val_int())
 
13968
    {
 
13969
      int error;
 
13970
      join->found_records++;
 
13971
      if ((error=table->file->ha_write_row(table->record[0])))
 
13972
      {
 
13973
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
13974
          goto end;
 
13975
        if (create_myisam_from_heap(join->thd, table,
 
13976
                                    join->tmp_table_param.start_recinfo,
 
13977
                                    &join->tmp_table_param.recinfo,
 
13978
                                    error, 1))
 
13979
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
13980
        table->s->uniques=0;                    // To ensure rows are the same
 
13981
      }
 
13982
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
13983
          join->do_send_rows)
 
13984
      {
 
13985
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
13986
          return(NESTED_LOOP_QUERY_LIMIT);
 
13987
        join->do_send_rows=0;
 
13988
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
13989
        return(NESTED_LOOP_OK);
 
13990
      }
 
13991
    }
 
13992
  }
 
13993
end:
 
13994
  return(NESTED_LOOP_OK);
 
13995
}
 
13996
 
 
13997
/* ARGSUSED */
 
13998
/** Group by searching after group record and updating it if possible. */
 
13999
 
 
14000
static enum_nested_loop_state
 
14001
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14002
           bool end_of_records)
 
14003
{
 
14004
  TABLE *table=join->tmp_table;
 
14005
  ORDER   *group;
 
14006
  int     error;
 
14007
 
 
14008
  if (end_of_records)
 
14009
    return(NESTED_LOOP_OK);
 
14010
  if (join->thd->killed)                        // Aborted by user
 
14011
  {
 
14012
    join->thd->send_kill_message();
 
14013
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14014
  }
 
14015
 
 
14016
  join->found_records++;
 
14017
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
14018
  /* Make a key of group index */
 
14019
  for (group=table->group ; group ; group=group->next)
 
14020
  {
 
14021
    Item *item= *group->item;
 
14022
    item->save_org_in_field(group->field);
 
14023
    /* Store in the used key if the field was 0 */
 
14024
    if (item->maybe_null)
 
14025
      group->buff[-1]= (char) group->field->is_null();
 
14026
  }
 
14027
  if (!table->file->index_read_map(table->record[1],
 
14028
                                   join->tmp_table_param.group_buff,
 
14029
                                   HA_WHOLE_KEY,
 
14030
                                   HA_READ_KEY_EXACT))
 
14031
  {                                             /* Update old record */
 
14032
    restore_record(table,record[1]);
 
14033
    update_tmptable_sum_func(join->sum_funcs,table);
 
14034
    if ((error=table->file->ha_update_row(table->record[1],
 
14035
                                          table->record[0])))
 
14036
    {
 
14037
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14038
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14039
    }
 
14040
    return(NESTED_LOOP_OK);
 
14041
  }
 
14042
 
 
14043
  /*
 
14044
    Copy null bits from group key to table
 
14045
    We can't copy all data as the key may have different format
 
14046
    as the row data (for example as with VARCHAR keys)
 
14047
  */
 
14048
  KEY_PART_INFO *key_part;
 
14049
  for (group=table->group,key_part=table->key_info[0].key_part;
 
14050
       group ;
 
14051
       group=group->next,key_part++)
 
14052
  {
 
14053
    if (key_part->null_bit)
 
14054
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
14055
  }
 
14056
  init_tmptable_sum_functions(join->sum_funcs);
 
14057
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14058
  if ((error=table->file->ha_write_row(table->record[0])))
 
14059
  {
 
14060
    if (create_myisam_from_heap(join->thd, table,
 
14061
                                join->tmp_table_param.start_recinfo,
 
14062
                                &join->tmp_table_param.recinfo,
 
14063
                                error, 0))
 
14064
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
14065
    /* Change method to update rows */
 
14066
    table->file->ha_index_init(0, 0);
 
14067
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
14068
  }
 
14069
  join->send_records++;
 
14070
  return(NESTED_LOOP_OK);
 
14071
}
 
14072
 
 
14073
 
 
14074
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
14075
 
 
14076
static enum_nested_loop_state
 
14077
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14078
                  bool end_of_records)
 
14079
{
 
14080
  TABLE *table=join->tmp_table;
 
14081
  int     error;
 
14082
 
 
14083
  if (end_of_records)
 
14084
    return(NESTED_LOOP_OK);
 
14085
  if (join->thd->killed)                        // Aborted by user
 
14086
  {
 
14087
    join->thd->send_kill_message();
 
14088
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14089
  }
 
14090
 
 
14091
  init_tmptable_sum_functions(join->sum_funcs);
 
14092
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
14093
  copy_funcs(join->tmp_table_param.items_to_copy);
 
14094
 
 
14095
  if (!(error=table->file->ha_write_row(table->record[0])))
 
14096
    join->send_records++;                       // New group
 
14097
  else
 
14098
  {
 
14099
    if ((int) table->file->get_dup_key(error) < 0)
 
14100
    {
 
14101
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14102
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14103
    }
 
14104
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
14105
    {
 
14106
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14107
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14108
    }
 
14109
    restore_record(table,record[1]);
 
14110
    update_tmptable_sum_func(join->sum_funcs,table);
 
14111
    if ((error=table->file->ha_update_row(table->record[1],
 
14112
                                          table->record[0])))
 
14113
    {
 
14114
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
14115
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14116
    }
 
14117
  }
 
14118
  return(NESTED_LOOP_OK);
 
14119
}
 
14120
 
 
14121
 
 
14122
        /* ARGSUSED */
 
14123
enum_nested_loop_state
 
14124
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
14125
                bool end_of_records)
 
14126
{
 
14127
  TABLE *table=join->tmp_table;
3962
14128
  int     idx= -1;
3963
14129
 
3964
 
  if (join->session->getKilled())
 
14130
  if (join->thd->killed)
3965
14131
  {                                             // Aborted by user
3966
 
    join->session->send_kill_message();
3967
 
    return NESTED_LOOP_KILLED;
 
14132
    join->thd->send_kill_message();
 
14133
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3968
14134
  }
3969
14135
  if (!join->first_record || end_of_records ||
3970
14136
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3974
14140
      int send_group_parts= join->send_group_parts;
3975
14141
      if (idx < send_group_parts)
3976
14142
      {
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
 
          }
 
14143
        if (!join->first_record)
 
14144
        {
 
14145
          /* No matching rows for group function */
 
14146
          join->clear();
 
14147
        }
 
14148
        copy_sum_funcs(join->sum_funcs,
 
14149
                       join->sum_funcs_end[send_group_parts]);
 
14150
        if (!join->having || join->having->val_int())
 
14151
        {
 
14152
          int error= table->file->ha_write_row(table->record[0]);
 
14153
          if (error && create_myisam_from_heap(join->thd, table,
 
14154
                                               join->tmp_table_param.start_recinfo,
 
14155
                                                &join->tmp_table_param.recinfo,
 
14156
                                               error, 0))
 
14157
            return(NESTED_LOOP_ERROR);
3992
14158
        }
3993
14159
        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;
 
14160
        {
 
14161
          if (join->rollup_write_data((uint) (idx+1), table))
 
14162
            return(NESTED_LOOP_ERROR);
 
14163
        }
 
14164
        if (end_of_records)
 
14165
          return(NESTED_LOOP_OK);
4000
14166
      }
4001
14167
    }
4002
14168
    else
4003
14169
    {
4004
14170
      if (end_of_records)
4005
 
        return NESTED_LOOP_OK;
 
14171
        return(NESTED_LOOP_OK);
4006
14172
      join->first_record=1;
4007
 
      test_if_item_cache_changed(join->group_fields);
 
14173
      VOID(test_if_item_cache_changed(join->group_fields));
4008
14174
    }
4009
14175
    if (idx < (int) join->send_group_parts)
4010
14176
    {
4011
14177
      copy_fields(&join->tmp_table_param);
4012
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4013
 
        return NESTED_LOOP_ERROR;
 
14178
      copy_funcs(join->tmp_table_param.items_to_copy);
4014
14179
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4015
 
        return NESTED_LOOP_ERROR;
4016
 
      return NESTED_LOOP_OK;
 
14180
        return(NESTED_LOOP_ERROR);
 
14181
      return(NESTED_LOOP_OK);
4017
14182
    }
4018
14183
  }
4019
14184
  if (update_sum_func(join->sum_funcs))
4020
 
    return NESTED_LOOP_ERROR;
4021
 
  return NESTED_LOOP_OK;
 
14185
    return(NESTED_LOOP_ERROR);
 
14186
  return(NESTED_LOOP_OK);
4022
14187
}
4023
14188
 
 
14189
 
4024
14190
/*****************************************************************************
4025
14191
  Remove calculation with tables that aren't yet read. Remove also tests
4026
14192
  against fields that are read through key where the table is not a
4027
14193
  outer join table.
4028
14194
  We can't remove tests that are made against columns which are stored
4029
14195
  in sorted order.
 
14196
*****************************************************************************/
 
14197
 
 
14198
/**
4030
14199
  @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)
 
14200
    1 if right_item is used removable reference key on left_item
 
14201
*/
 
14202
 
 
14203
static bool test_if_ref(Item_field *left_item,Item *right_item)
4035
14204
{
4036
14205
  Field *field=left_item->field;
4037
14206
  // 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)
 
14207
  if (!field->table->const_table && !field->table->maybe_null)
4039
14208
  {
4040
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
14209
    Item *ref_item=part_of_refkey(field->table,field);
4041
14210
    if (ref_item && ref_item->eq(right_item,1))
4042
14211
    {
4043
14212
      right_item= right_item->real_item();
4044
14213
      if (right_item->type() == Item::FIELD_ITEM)
4045
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
14214
        return (field->eq_def(((Item_field *) right_item)->field));
4046
14215
      /* remove equalities injected by IN->EXISTS transformation */
4047
14216
      else if (right_item->type() == Item::CACHE_ITEM)
4048
14217
        return ((Item_cache *)right_item)->eq_def (field);
4049
14218
      if (right_item->const_item() && !(right_item->is_null()))
4050
14219
      {
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
 
14220
        /*
 
14221
          We can remove binary fields and numerical fields except float,
 
14222
          as float comparison isn't 100 % secure
 
14223
          We have to keep normal strings to be able to check for end spaces
4055
14224
 
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
 
        }
 
14225
          sergefp: the above seems to be too restrictive. Counterexample:
 
14226
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
14227
            insert into t100 values ('a'),('a ');
 
14228
            explain select * from t100 where v='a';
 
14229
          The EXPLAIN shows 'using Where'. Running the query returns both
 
14230
          rows, so it seems there are no problems with endspace in the most
 
14231
          frequent case?
 
14232
        */
 
14233
        if (field->binary() &&
 
14234
            field->real_type() != MYSQL_TYPE_STRING &&
 
14235
            field->real_type() != MYSQL_TYPE_VARCHAR &&
 
14236
            (field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
 
14237
        {
 
14238
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
14239
        }
4070
14240
      }
4071
14241
    }
4072
14242
  }
4073
 
  return 0;
 
14243
  return 0;                                     // keep test
 
14244
}
 
14245
 
 
14246
/**
 
14247
   @brief Replaces an expression destructively inside the expression tree of
 
14248
   the WHERE clase.
 
14249
 
 
14250
   @note Because of current requirements for semijoin flattening, we do not
 
14251
   need to recurse here, hence this function will only examine the top-level
 
14252
   AND conditions. (see JOIN::prepare, comment above the line 
 
14253
   'if (do_materialize)'
 
14254
   
 
14255
   @param join The top-level query.
 
14256
   @param old_cond The expression to be replaced.
 
14257
   @param new_cond The expression to be substituted.
 
14258
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
14259
   the new expression.
 
14260
   @return <code>true</code> if there was an error, <code>false</code> if
 
14261
   successful.
 
14262
*/
 
14263
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
14264
                                       Item *new_cond, bool do_fix_fields)
 
14265
{
 
14266
  if (join->conds == old_cond) {
 
14267
    join->conds= new_cond;
 
14268
    if (do_fix_fields)
 
14269
      new_cond->fix_fields(join->thd, &join->conds);
 
14270
    return false;
 
14271
  }
 
14272
  
 
14273
  if (join->conds->type() == Item::COND_ITEM) {
 
14274
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
14275
    Item *item;
 
14276
    while ((item= li++))
 
14277
      if (item == old_cond) 
 
14278
      {
 
14279
        li.replace(new_cond);
 
14280
        if (do_fix_fields)
 
14281
          new_cond->fix_fields(join->thd, li.ref());
 
14282
        return false;
 
14283
      }
 
14284
  }
 
14285
 
 
14286
  return true;
4074
14287
}
4075
14288
 
4076
14289
/*
4077
14290
  Extract a condition that can be checked after reading given table
4078
 
 
 
14291
  
4079
14292
  SYNOPSIS
4080
14293
    make_cond_for_table()
4081
14294
      cond         Condition to analyze
4082
14295
      tables       Tables for which "current field values" are available
4083
 
      used_table   Table that we're extracting the condition for (may
 
14296
      used_table   Table that we're extracting the condition for (may 
4084
14297
                   also include PSEUDO_TABLE_BITS
4085
14298
 
4086
14299
  DESCRIPTION
4090
14303
 
4091
14304
    The function assumes that
4092
14305
      - Constant parts of the condition has already been checked.
4093
 
      - Condition that could be checked for tables in 'tables' has already
 
14306
      - Condition that could be checked for tables in 'tables' has already 
4094
14307
        been checked.
4095
 
 
 
14308
        
4096
14309
    The function takes into account that some parts of the condition are
4097
14310
    guaranteed to be true by employed 'ref' access methods (the code that
4098
14311
    does this is located at the end, search down for "EQ_FUNC").
4099
14312
 
4100
14313
 
4101
 
  SEE ALSO
 
14314
  SEE ALSO 
4102
14315
    make_cond_for_info_schema uses similar algorithm
4103
14316
 
4104
14317
  RETURN
4105
14318
    Extracted condition
4106
14319
*/
4107
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
14320
 
 
14321
static COND *
 
14322
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
14323
                    bool exclude_expensive_cond)
4108
14324
{
4109
14325
  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()))
 
14326
      /*
 
14327
        Exclude constant conditions not checked at optimization time if
 
14328
        the table we are pushing conditions to is the first one.
 
14329
        As a result, such conditions are not considered as already checked
 
14330
        and will be checked at execution time, attached to the first table.
 
14331
      */
 
14332
      !((used_table & 1) && cond->is_expensive()))
4117
14333
    return (COND*) 0;                           // Already checked
4118
14334
  if (cond->type() == Item::COND_ITEM)
4119
14335
  {
4122
14338
      /* Create new top level AND item */
4123
14339
      Item_cond_and *new_cond=new Item_cond_and;
4124
14340
      if (!new_cond)
4125
 
        return (COND*) 0;
 
14341
        return (COND*) 0;                       // OOM /* purecov: inspected */
4126
14342
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4127
14343
      Item *item;
4128
14344
      while ((item=li++))
4129
14345
      {
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);
 
14346
        Item *fix=make_cond_for_table(item,tables,used_table,
 
14347
                                      exclude_expensive_cond);
 
14348
        if (fix)
 
14349
          new_cond->argument_list()->push_back(fix);
4134
14350
      }
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;
 
14351
      switch (new_cond->argument_list()->elements) {
 
14352
      case 0:
 
14353
        return (COND*) 0;                       // Always true
 
14354
      case 1:
 
14355
        return new_cond->argument_list()->head();
 
14356
      default:
 
14357
        /*
 
14358
          Item_cond_and do not need fix_fields for execution, its parameters
 
14359
          are fixed or do not need fix_fields, too
 
14360
        */
 
14361
        new_cond->quick_fix_field();
 
14362
        new_cond->used_tables_cache=
 
14363
          ((Item_cond_and*) cond)->used_tables_cache &
 
14364
          tables;
 
14365
        return new_cond;
4149
14366
      }
4150
14367
    }
4151
14368
    else
4152
14369
    {                                           // Or list
4153
14370
      Item_cond_or *new_cond=new Item_cond_or;
4154
14371
      if (!new_cond)
4155
 
        return (COND*) 0;
 
14372
        return (COND*) 0;                       // OOM /* purecov: inspected */
4156
14373
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4157
14374
      Item *item;
4158
14375
      while ((item=li++))
4159
14376
      {
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);
 
14377
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
14378
        if (!fix)
 
14379
          return (COND*) 0;                     // Always true
 
14380
        new_cond->argument_list()->push_back(fix);
4164
14381
      }
4165
14382
      /*
4166
 
        Item_cond_and do not need fix_fields for execution, its parameters
4167
 
        are fixed or do not need fix_fields, too
 
14383
        Item_cond_and do not need fix_fields for execution, its parameters
 
14384
        are fixed or do not need fix_fields, too
4168
14385
      */
4169
14386
      new_cond->quick_fix_field();
4170
14387
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4189
14406
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4190
14407
    return cond;                                // Not boolean op
4191
14408
 
4192
 
  /*
 
14409
  /* 
4193
14410
    Remove equalities that are guaranteed to be true by use of 'ref' access
4194
14411
    method
4195
14412
  */
4197
14414
  {
4198
14415
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4199
14416
    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))
 
14417
    if (left_item->type() == Item::FIELD_ITEM &&
 
14418
        test_if_ref((Item_field*) left_item,right_item))
4201
14419
    {
4202
14420
      cond->marker=3;                   // Checked when read
4203
14421
      return (COND*) 0;
4204
14422
    }
4205
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
14423
    if (right_item->type() == Item::FIELD_ITEM &&
 
14424
        test_if_ref((Item_field*) right_item,left_item))
4206
14425
    {
4207
14426
      cond->marker=3;                   // Checked when read
4208
14427
      return (COND*) 0;
4212
14431
  return cond;
4213
14432
}
4214
14433
 
4215
 
static Item *part_of_refkey(Table *table,Field *field)
 
14434
 
 
14435
static Item *
 
14436
part_of_refkey(TABLE *table,Field *field)
4216
14437
{
4217
14438
  if (!table->reginfo.join_tab)
4218
14439
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4219
14440
 
4220
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
14441
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
4221
14442
  if (ref_parts)
4222
14443
  {
4223
 
    KeyPartInfo *key_part=
 
14444
    KEY_PART_INFO *key_part=
4224
14445
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4225
 
    uint32_t part;
 
14446
    uint part;
4226
14447
 
4227
14448
    for (part=0 ; part < ref_parts ; part++)
4228
14449
    {
4231
14452
    }
4232
14453
 
4233
14454
    for (part=0 ; part < ref_parts ; part++,key_part++)
4234
 
    {
4235
14455
      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
 
      {
 
14456
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4241
14457
        return table->reginfo.join_tab->ref.items[part];
4242
 
      }
4243
 
    }
4244
14458
  }
4245
14459
  return (Item*) 0;
4246
14460
}
4247
14461
 
 
14462
 
4248
14463
/**
4249
 
  Test if one can use the key to resolve order_st BY.
 
14464
  Test if one can use the key to resolve ORDER BY.
4250
14465
 
4251
14466
  @param order                 Sort order
4252
14467
  @param table                 Table to sort
4265
14480
  @retval
4266
14481
    -1   Reverse key can be used
4267
14482
*/
4268
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
14483
 
 
14484
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
14485
                                uint *used_key_parts)
4269
14486
{
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;
 
14487
  KEY_PART_INFO *key_part,*key_part_end;
 
14488
  key_part=table->key_info[idx].key_part;
 
14489
  key_part_end=key_part+table->key_info[idx].key_parts;
4274
14490
  key_part_map const_key_parts=table->const_key_parts[idx];
4275
 
  int reverse= 0;
4276
 
  bool on_primary_key= false;
 
14491
  int reverse=0;
 
14492
  my_bool on_primary_key= false;
4277
14493
 
4278
14494
  for (; order ; order=order->next, const_key_parts>>=1)
4279
14495
  {
4285
14501
      These are already skipped in the ORDER BY by const_expression_in_where()
4286
14502
    */
4287
14503
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4288
 
      key_part++;
 
14504
      key_part++; 
4289
14505
 
4290
14506
    if (key_part == key_part_end)
4291
14507
    {
4292
 
      /*
 
14508
      /* 
4293
14509
        We are at the end of the key. Check if the engine has the primary
4294
14510
        key as a suffix to the secondary keys. If it has continue to check
4295
14511
        the primary key as a suffix.
4296
14512
      */
4297
14513
      if (!on_primary_key &&
4298
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4299
 
          table->getShare()->hasPrimaryKey())
 
14514
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
14515
          table->s->primary_key != MAX_KEY)
4300
14516
      {
4301
14517
        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()];
 
14518
        key_part= table->key_info[table->s->primary_key].key_part;
 
14519
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
14520
        const_key_parts=table->const_key_parts[table->s->primary_key];
4305
14521
 
4306
14522
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4307
 
          key_part++;
 
14523
          key_part++; 
4308
14524
        /*
4309
14525
         The primary and secondary key parts were all const (i.e. there's
4310
14526
         one row).  The sorting doesn't matter.
4328
14544
    key_part++;
4329
14545
  }
4330
14546
  *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) &
 
14547
    (uint) (key_part - table->key_info[idx].key_part);
 
14548
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4333
14549
                         HA_READ_PREV))
4334
14550
    reverse= 0;                                 // Index can't be used
4335
14551
  return(reverse);
4336
14552
}
4337
14553
 
 
14554
 
 
14555
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
14556
{
 
14557
  uint min_length= (uint) ~0;
 
14558
  uint best= MAX_KEY;
 
14559
  if (!usable_keys->is_clear_all())
 
14560
  {
 
14561
    for (uint nr=0; nr < table->s->keys ; nr++)
 
14562
    {
 
14563
      if (usable_keys->is_set(nr))
 
14564
      {
 
14565
        if (table->key_info[nr].key_length < min_length)
 
14566
        {
 
14567
          min_length=table->key_info[nr].key_length;
 
14568
          best=nr;
 
14569
        }
 
14570
      }
 
14571
    }
 
14572
  }
 
14573
  return best;
 
14574
}
 
14575
 
4338
14576
/**
4339
14577
  Test if a second key is the subkey of the first one.
4340
14578
 
4350
14588
  @retval
4351
14589
    0   no sub key
4352
14590
*/
4353
 
inline bool is_subkey(KeyPartInfo *key_part,
4354
 
                      KeyPartInfo *ref_key_part,
4355
 
                      KeyPartInfo *ref_key_part_end)
 
14591
 
 
14592
inline bool 
 
14593
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
14594
          KEY_PART_INFO *ref_key_part_end)
4356
14595
{
4357
14596
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4358
 
    if (! key_part->field->eq(ref_key_part->field))
 
14597
    if (!key_part->field->eq(ref_key_part->field))
4359
14598
      return 0;
4360
14599
  return 1;
4361
14600
}
4371
14610
    - MAX_KEY                   If we can't use other key
4372
14611
    - the number of found key   Otherwise
4373
14612
*/
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)
 
14613
 
 
14614
static uint
 
14615
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
 
14616
               const key_map *usable_keys)
4379
14617
{
4380
 
  uint32_t nr;
4381
 
  uint32_t min_length= UINT32_MAX;
4382
 
  uint32_t best= MAX_KEY;
4383
 
  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;
 
14618
  uint nr;
 
14619
  uint min_length= (uint) ~0;
 
14620
  uint best= MAX_KEY;
 
14621
  uint not_used;
 
14622
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
14623
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4386
14624
 
4387
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
14625
  for (nr= 0 ; nr < table->s->keys ; nr++)
4388
14626
  {
4389
 
    if (usable_keys->test(nr) &&
 
14627
    if (usable_keys->is_set(nr) &&
4390
14628
        table->key_info[nr].key_length < min_length &&
4391
14629
        table->key_info[nr].key_parts >= ref_key_parts &&
4392
14630
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4400
14638
  return best;
4401
14639
}
4402
14640
 
 
14641
 
4403
14642
/**
4404
14643
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4405
14644
  already known to be distinct.
4418
14657
    of the table are referenced by a list : either the select list
4419
14658
    through find_field_in_item_list or GROUP BY list through
4420
14659
    find_field_in_order_list.
4421
 
    If the above holds and the key parts cannot contain NULLs then we
 
14660
    If the above holds and the key parts cannot contain NULLs then we 
4422
14661
    can safely remove the GROUP BY/DISTINCT,
4423
14662
    as no result set can be more distinct than an unique key.
4424
14663
 
4431
14670
  @retval
4432
14671
    0                    not found.
4433
14672
*/
4434
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
14673
 
 
14674
static bool
 
14675
list_contains_unique_index(TABLE *table,
 
14676
                          bool (*find_func) (Field *, void *), void *data)
4435
14677
{
4436
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
14678
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
4437
14679
  {
4438
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
14680
    if (keynr == table->s->primary_key ||
4439
14681
         (table->key_info[keynr].flags & HA_NOSAME))
4440
14682
    {
4441
 
      KeyInfo *keyinfo= table->key_info + keynr;
4442
 
      KeyPartInfo *key_part= NULL;
4443
 
      KeyPartInfo *key_part_end= NULL;
 
14683
      KEY *keyinfo= table->key_info + keynr;
 
14684
      KEY_PART_INFO *key_part, *key_part_end;
4444
14685
 
4445
14686
      for (key_part=keyinfo->key_part,
4446
14687
           key_part_end=key_part+ keyinfo->key_parts;
4447
14688
           key_part < key_part_end;
4448
14689
           key_part++)
4449
14690
      {
4450
 
        if (key_part->field->maybe_null() ||
4451
 
            ! find_func(key_part->field, data))
 
14691
        if (key_part->field->maybe_null() || 
 
14692
            !find_func(key_part->field, data))
4452
14693
          break;
4453
14694
      }
4454
14695
      if (key_part == key_part_end)
4458
14699
  return 0;
4459
14700
}
4460
14701
 
 
14702
 
4461
14703
/**
4462
14704
  Helper function for list_contains_unique_index.
4463
 
  Find a field reference in a list of order_st structures.
 
14705
  Find a field reference in a list of ORDER structures.
4464
14706
  Finds a direct reference of the Field in the list.
4465
14707
 
4466
14708
  @param field                The field to search for.
4467
 
  @param data                 order_st *.The list to search in
 
14709
  @param data                 ORDER *.The list to search in
4468
14710
 
4469
14711
  @retval
4470
14712
    1                    found
4471
14713
  @retval
4472
14714
    0                    not found.
4473
14715
*/
4474
 
bool find_field_in_order_list (Field *field, void *data)
 
14716
 
 
14717
static bool
 
14718
find_field_in_order_list (Field *field, void *data)
4475
14719
{
4476
 
  Order *group= (Order *) data;
 
14720
  ORDER *group= (ORDER *) data;
4477
14721
  bool part_found= 0;
4478
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
14722
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4479
14723
  {
4480
14724
    Item *item= (*tmp_group->item)->real_item();
4481
14725
    if (item->type() == Item::FIELD_ITEM &&
4488
14732
  return part_found;
4489
14733
}
4490
14734
 
 
14735
 
4491
14736
/**
4492
14737
  Helper function for list_contains_unique_index.
4493
14738
  Find a field reference in a dynamic list of Items.
4501
14746
  @retval
4502
14747
    0                    not found.
4503
14748
*/
4504
 
bool find_field_in_item_list (Field *field, void *data)
 
14749
 
 
14750
static bool
 
14751
find_field_in_item_list (Field *field, void *data)
4505
14752
{
4506
14753
  List<Item> *fields= (List<Item> *) data;
4507
14754
  bool part_found= 0;
4520
14767
  return part_found;
4521
14768
}
4522
14769
 
 
14770
 
4523
14771
/**
4524
14772
  Test if we can skip the ORDER BY by using an index.
4525
14773
 
4531
14779
      no_changes
4532
14780
      map
4533
14781
 
4534
 
  If we can use an index, the JoinTable / tab->select struct
 
14782
  If we can use an index, the JOIN_TAB / tab->select struct
4535
14783
  is changed to use the index.
4536
14784
 
4537
14785
  The index must cover all fields in <order>, or it will not be considered.
4538
14786
 
4539
14787
  @todo
4540
 
    - sergeyp: Results of all index merge selects actually are ordered
 
14788
    - sergeyp: Results of all index merge selects actually are ordered 
4541
14789
    by clustered PK values.
4542
14790
 
4543
14791
  @retval
4545
14793
  @retval
4546
14794
    1    We can use an index.
4547
14795
*/
4548
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
14796
 
 
14797
static bool
 
14798
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
 
14799
                        bool no_changes, const key_map *map)
4549
14800
{
4550
 
  int32_t ref_key;
4551
 
  uint32_t ref_key_parts;
 
14801
  int ref_key;
 
14802
  uint ref_key_parts;
4552
14803
  int order_direction;
4553
 
  uint32_t used_key_parts;
4554
 
  Table *table=tab->table;
4555
 
  optimizer::SqlSelect *select= tab->select;
 
14804
  uint used_key_parts;
 
14805
  TABLE *table=tab->table;
 
14806
  SQL_SELECT *select=tab->select;
4556
14807
  key_map usable_keys;
4557
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
14808
  QUICK_SELECT_I *save_quick= 0;
4558
14809
 
4559
14810
  /*
4560
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
14811
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
4561
14812
    been taken into account.
4562
14813
  */
4563
14814
  usable_keys= *map;
4564
14815
 
4565
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
14816
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4566
14817
  {
4567
14818
    Item *item= (*tmp_order->item)->real_item();
4568
14819
    if (item->type() != Item::FIELD_ITEM)
4569
14820
    {
4570
 
      usable_keys.reset();
 
14821
      usable_keys.clear_all();
4571
14822
      return(0);
4572
14823
    }
4573
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4574
 
    if (usable_keys.none())
 
14824
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
14825
    if (usable_keys.is_clear_all())
4575
14826
      return(0);                                        // No usable keys
4576
14827
  }
4577
14828
 
4581
14832
  {
4582
14833
    ref_key=       tab->ref.key;
4583
14834
    ref_key_parts= tab->ref.key_parts;
4584
 
    if (tab->type == AM_REF_OR_NULL)
 
14835
    if (tab->type == JT_REF_OR_NULL)
4585
14836
      return(0);
4586
14837
  }
4587
 
  else if (select && select->quick)             // Range found by optimizer/range
 
14838
  else if (select && select->quick)             // Range found by opt_range
4588
14839
  {
4589
14840
    int quick_type= select->quick->get_type();
4590
14841
    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
 
14842
    /* 
 
14843
      assume results are not ordered when index merge is used 
 
14844
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4594
14845
      by clustered PK values.
4595
14846
    */
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)
 
14847
  
 
14848
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
14849
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
14850
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4600
14851
      return(0);
4601
14852
    ref_key=       select->quick->index;
4602
14853
    ref_key_parts= select->quick->used_key_parts;
4607
14858
    /*
4608
14859
      We come here when there is a REF key.
4609
14860
    */
4610
 
    if (! usable_keys.test(ref_key))
 
14861
    if (!usable_keys.is_set(ref_key))
4611
14862
    {
4612
14863
      /*
4613
 
        We come here when ref_key is not among usable_keys
 
14864
        We come here when ref_key is not among usable_keys
4614
14865
      */
4615
 
      uint32_t new_ref_key;
 
14866
      uint new_ref_key;
4616
14867
      /*
4617
 
        If using index only read, only consider other possible index only
4618
 
        keys
 
14868
        If using index only read, only consider other possible index only
 
14869
        keys
4619
14870
      */
4620
 
      if (table->covering_keys.test(ref_key))
4621
 
        usable_keys&= table->covering_keys;
 
14871
      if (table->covering_keys.is_set(ref_key))
 
14872
        usable_keys.intersect(table->covering_keys);
4622
14873
      if (tab->pre_idx_push_select_cond)
4623
14874
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4624
14875
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4625
14876
                                       &usable_keys)) < MAX_KEY)
4626
14877
      {
4627
 
        /* Found key that can be used to retrieve data in sorted order */
4628
 
        if (tab->ref.key >= 0)
4629
 
        {
 
14878
        /* Found key that can be used to retrieve data in sorted order */
 
14879
        if (tab->ref.key >= 0)
 
14880
        {
4630
14881
          /*
4631
 
            We'll use ref access method on key new_ref_key. In general case
 
14882
            We'll use ref access method on key new_ref_key. In general case 
4632
14883
            the index search tuple for new_ref_key will be different (e.g.
4633
14884
            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".
 
14885
            (part1, part2(N), ...) and the WHERE clause contains 
 
14886
            "part1 = const1 AND part2=const2". 
4636
14887
            So we build tab->ref from scratch here.
4637
14888
          */
4638
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4639
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
14889
          KEYUSE *keyuse= tab->keyuse;
 
14890
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4640
14891
            keyuse++;
4641
14892
 
4642
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
14893
          if (create_ref_for_key(tab->join, tab, keyuse, 
4643
14894
                                 tab->join->const_table_map))
4644
14895
            return(0);
4645
 
        }
4646
 
        else
4647
 
        {
 
14896
        }
 
14897
        else
 
14898
        {
4648
14899
          /*
4649
 
            The range optimizer constructed QuickRange for ref_key, and
 
14900
            The range optimizer constructed QUICK_RANGE for ref_key, and
4650
14901
            we want to use instead new_ref_key as the index. We can't
4651
14902
            just change the index of the quick select, because this may
4652
14903
            result in an incosistent QUICK_SELECT object. Below we
4654
14905
            parameres are set correctly by the range optimizer.
4655
14906
           */
4656
14907
          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.
 
14908
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
14909
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4659
14910
 
4660
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
14911
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4661
14912
                                        (tab->join->select_options &
4662
14913
                                         OPTION_FOUND_ROWS) ?
4663
14914
                                        HA_POS_ERROR :
4665
14916
                                        true) <=
4666
14917
              0)
4667
14918
            return(0);
4668
 
        }
 
14919
        }
4669
14920
        ref_key= new_ref_key;
4670
14921
      }
4671
14922
    }
4672
14923
    /* Check if we get the rows in requested sorted order by using the key */
4673
 
    if (usable_keys.test(ref_key) &&
 
14924
    if (usable_keys.is_set(ref_key) &&
4674
14925
        (order_direction= test_if_order_by_key(order,table,ref_key,
4675
14926
                                               &used_key_parts)))
4676
14927
      goto check_reverse_order;
4680
14931
      Check whether there is an index compatible with the given order
4681
14932
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
4682
14933
      or a table scan.
4683
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
14934
      It may be the case if ORDER/GROUP BY is used with LIMIT.
4684
14935
    */
4685
 
    uint32_t nr;
 
14936
    uint nr;
4686
14937
    key_map keys;
4687
 
    uint32_t best_key_parts= 0;
 
14938
    uint best_key_parts= 0;
4688
14939
    int best_key_direction= 0;
4689
14940
    ha_rows best_records= 0;
4690
14941
    double read_time;
4691
14942
    int best_key= -1;
4692
14943
    bool is_best_covering= false;
4693
14944
    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;
 
14945
    JOIN *join= tab->join;
 
14946
    uint tablenr= tab - join->join_tab;
 
14947
    ha_rows table_records= table->file->stats.records;
4697
14948
    bool group= join->group && order == join->group_list;
4698
 
    optimizer::Position cur_pos;
4699
14949
 
4700
14950
    /*
4701
14951
      If not used with LIMIT, only use keys if the whole query can be
4704
14954
    */
4705
14955
    if (select_limit >= table_records)
4706
14956
    {
4707
 
      /*
4708
 
        filesort() and join cache are usually faster than reading in
 
14957
      /* 
 
14958
        filesort() and join cache are usually faster than reading in 
4709
14959
        index order and not using join cache
4710
14960
        */
4711
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
14961
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4712
14962
        return(0);
4713
 
      keys= *table->cursor->keys_to_use_for_scanning();
4714
 
      keys|= table->covering_keys;
 
14963
      keys= *table->file->keys_to_use_for_scanning();
 
14964
      keys.merge(table->covering_keys);
4715
14965
 
4716
14966
      /*
4717
 
        We are adding here also the index specified in FORCE INDEX clause,
4718
 
        if any.
4719
 
        This is to allow users to use index in order_st BY.
 
14967
        We are adding here also the index specified in FORCE INDEX clause, 
 
14968
        if any.
 
14969
        This is to allow users to use index in ORDER BY.
4720
14970
      */
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;
 
14971
      if (table->force_index) 
 
14972
        keys.merge(group ? table->keys_in_use_for_group_by :
 
14973
                           table->keys_in_use_for_order_by);
 
14974
      keys.intersect(usable_keys);
4725
14975
    }
4726
14976
    else
4727
14977
      keys= usable_keys;
4728
14978
 
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
 
    }
 
14979
    read_time= join->best_positions[tablenr].read_time;
 
14980
    for (uint i= tablenr+1; i < join->tables; i++)
 
14981
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4736
14982
 
4737
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
14983
    for (nr=0; nr < table->s->keys ; nr++)
4738
14984
    {
4739
14985
      int direction;
4740
 
      if (keys.test(nr) &&
 
14986
      if (keys.is_set(nr) &&
4741
14987
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4742
14988
      {
4743
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4744
 
 
4745
 
        /*
 
14989
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
14990
        
 
14991
        /* 
4746
14992
          Don't use an index scan with ORDER BY without limit.
4747
14993
          For GROUP BY without limit always use index scan
4748
 
          if there is a suitable index.
 
14994
          if there is a suitable index. 
4749
14995
          Why we hold to this asymmetry hardly can be explained
4750
14996
          rationally. It's easy to demonstrate that using
4751
14997
          temporary table + filesort could be cheaper for grouping
4752
14998
          queries too.
4753
 
        */
 
14999
        */ 
4754
15000
        if (is_covering ||
4755
 
            select_limit != HA_POS_ERROR ||
 
15001
            select_limit != HA_POS_ERROR || 
4756
15002
            (ref_key < 0 && (group || table->force_index)))
4757
 
        {
 
15003
        { 
4758
15004
          double rec_per_key;
4759
15005
          double index_scan_time;
4760
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
15006
          KEY *keyinfo= tab->table->key_info+nr;
4761
15007
          if (select_limit == HA_POS_ERROR)
4762
15008
            select_limit= table_records;
4763
15009
          if (group)
4764
15010
          {
4765
15011
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4766
 
            set_if_bigger(rec_per_key, 1.0);
 
15012
            set_if_bigger(rec_per_key, 1);
4767
15013
            /*
4768
15014
              With a grouping query each group containing on average
4769
15015
              rec_per_key records produces only one row that will
4770
15016
              be included into the result set.
4771
 
            */
 
15017
            */  
4772
15018
            if (select_limit > table_records/rec_per_key)
4773
15019
                select_limit= table_records;
4774
15020
            else
4775
15021
              select_limit= (ha_rows) (select_limit*rec_per_key);
4776
15022
          }
4777
 
          /*
 
15023
          /* 
4778
15024
            If tab=tk is not the last joined table tn then to get first
4779
15025
            L records from the result set we can expect to retrieve
4780
15026
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4783
15029
            So the estimate for L/fanout(tk,tn) will be too optimistic
4784
15030
            and as result we'll choose an index scan when using ref/range
4785
15031
            access + filesort will be cheaper.
4786
 
          */
 
15032
          */
4787
15033
          select_limit= (ha_rows) (select_limit < fanout ?
4788
15034
                                   1 : select_limit/fanout);
4789
15035
          /*
4790
15036
            We assume that each of the tested indexes is not correlated
4791
15037
            with ref_key. Thus, to select first N records we have to scan
4792
 
            N/selectivity(ref_key) index entries.
 
15038
            N/selectivity(ref_key) index entries. 
4793
15039
            selectivity(ref_key) = #scanned_records/#table_records =
4794
15040
            table->quick_condition_rows/table_records.
4795
15041
            In any case we can't select more than #table_records.
4796
 
            N/(table->quick_condition_rows/table_records) > table_records
 
15042
            N/(table->quick_condition_rows/table_records) > table_records 
4797
15043
            <=> N > table->quick_condition_rows.
4798
 
          */
 
15044
          */ 
4799
15045
          if (select_limit > table->quick_condition_rows)
4800
15046
            select_limit= table_records;
4801
15047
          else
4803
15049
                                     (double) table_records /
4804
15050
                                      table->quick_condition_rows);
4805
15051
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4806
 
          set_if_bigger(rec_per_key, 1.0);
 
15052
          set_if_bigger(rec_per_key, 1);
4807
15053
          /*
4808
15054
            Here we take into account the fact that rows are
4809
15055
            accessed in sequences rec_per_key records in each.
4810
15056
            Rows in such a sequence are supposed to be ordered
4811
15057
            by rowid/primary key. When reading the data
4812
15058
            in a sequence we'll touch not more pages than the
4813
 
            table cursor contains.
 
15059
            table file contains.
4814
15060
            TODO. Use the formula for a disk sweep sequential access
4815
 
            to calculate the cost of accessing data rows for one
 
15061
            to calculate the cost of accessing data rows for one 
4816
15062
            index entry.
4817
 
          */
 
15063
          */
4818
15064
          index_scan_time= select_limit/rec_per_key *
4819
 
                           min(rec_per_key, table->cursor->scan_time());
 
15065
                           min(rec_per_key, table->file->scan_time());
4820
15066
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4821
15067
              index_scan_time < read_time)
4822
15068
          {
4823
15069
            ha_rows quick_records= table_records;
4824
15070
            if (is_best_covering && !is_covering)
4825
15071
              continue;
4826
 
            if (table->quick_keys.test(nr))
 
15072
            if (table->quick_keys.is_set(nr))
4827
15073
              quick_records= table->quick_rows[nr];
4828
15074
            if (best_key < 0 ||
4829
15075
                (select_limit <= min(quick_records,best_records) ?
4834
15080
              best_key_parts= keyinfo->key_parts;
4835
15081
              best_records= quick_records;
4836
15082
              is_best_covering= is_covering;
4837
 
              best_key_direction= direction;
 
15083
              best_key_direction= direction; 
4838
15084
            }
4839
 
          }
4840
 
        }
 
15085
          }   
 
15086
        }      
4841
15087
      }
4842
15088
    }
4843
15089
    if (best_key >= 0)
4844
15090
    {
4845
15091
      bool quick_created= false;
4846
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
15092
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4847
15093
      {
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,
 
15094
        key_map map;
 
15095
        map.clear_all();       // Force the creation of quick select
 
15096
        map.set_bit(best_key); // only best_key.
 
15097
        quick_created=         
 
15098
          select->test_quick_select(join->thd, map, 0,
4853
15099
                                    join->select_options & OPTION_FOUND_ROWS ?
4854
15100
                                    HA_POS_ERROR :
4855
15101
                                    join->unit->select_limit_cnt,
4858
15104
      if (!no_changes)
4859
15105
      {
4860
15106
        if (!quick_created)
4861
 
        {
 
15107
        {
4862
15108
          tab->index= best_key;
4863
15109
          tab->read_first_record= best_key_direction > 0 ?
4864
15110
                                  join_read_first:join_read_last;
4865
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
15111
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4866
15112
          if (select && select->quick)
4867
15113
          {
4868
15114
            delete select->quick;
4869
15115
            select->quick= 0;
4870
15116
          }
4871
 
          if (table->covering_keys.test(best_key))
 
15117
          if (table->covering_keys.is_set(best_key))
4872
15118
          {
4873
15119
            table->key_read=1;
4874
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
15120
            table->file->extra(HA_EXTRA_KEYREAD);
4875
15121
          }
4876
 
          table->cursor->ha_index_or_rnd_end();
 
15122
          table->file->ha_index_or_rnd_end();
4877
15123
          if (join->select_options & SELECT_DESCRIBE)
4878
15124
          {
4879
15125
            tab->ref.key= -1;
4880
15126
            tab->ref.key_parts= 0;
4881
 
            if (select_limit < table_records)
 
15127
            if (select_limit < table_records) 
4882
15128
              tab->limit= select_limit;
4883
15129
          }
4884
15130
        }
4885
 
        else if (tab->type != AM_ALL)
 
15131
        else if (tab->type != JT_ALL)
4886
15132
        {
4887
15133
          /*
4888
15134
            We're about to use a quick access to the table.
4890
15136
            method is actually used.
4891
15137
          */
4892
15138
          assert(tab->select->quick);
4893
 
          tab->type= AM_ALL;
 
15139
          tab->type=JT_ALL;
4894
15140
          tab->use_quick=1;
4895
15141
          tab->ref.key= -1;
4896
15142
          tab->ref.key_parts=0;         // Don't use ref key.
4897
15143
          tab->read_first_record= join_init_read_record;
 
15144
          /*
 
15145
            TODO: update the number of records in join->best_positions[tablenr]
 
15146
          */
4898
15147
        }
4899
15148
      }
4900
15149
      used_key_parts= best_key_parts;
4901
15150
      order_direction= best_key_direction;
4902
15151
    }
4903
15152
    else
4904
 
      return(0);
4905
 
  }
 
15153
      return(0); 
 
15154
  } 
4906
15155
 
4907
 
check_reverse_order:
 
15156
check_reverse_order:                  
4908
15157
  if (order_direction == -1)            // If ORDER BY ... DESC
4909
15158
  {
4910
15159
    if (select && select->quick)
4911
15160
    {
4912
15161
      /*
4913
 
        Don't reverse the sort order, if it's already done.
 
15162
        Don't reverse the sort order, if it's already done.
4914
15163
        (In some cases test_if_order_by_key() can be called multiple times
4915
15164
      */
4916
 
      if (! select->quick->reverse_sorted())
 
15165
      if (!select->quick->reverse_sorted())
4917
15166
      {
4918
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
15167
        QUICK_SELECT_DESC *tmp;
4919
15168
        bool error= false;
4920
15169
        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)
 
15170
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
15171
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
15172
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
15173
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
4925
15174
        {
4926
15175
          tab->limit= 0;
4927
15176
          select->quick= save_quick;
4928
 
          return 0; // Use filesort
 
15177
          return(0);                   // Use filesort
4929
15178
        }
4930
 
 
 
15179
            
4931
15180
        /* 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;
 
15181
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
15182
                                    used_key_parts, &error);
 
15183
        if (!tmp || error)
 
15184
        {
 
15185
          delete tmp;
4938
15186
          select->quick= save_quick;
4939
15187
          tab->limit= 0;
4940
 
          return 0; // Reverse sort not supported
4941
 
        }
4942
 
        select->quick=tmp;
 
15188
          return(0);            // Reverse sort not supported
 
15189
        }
 
15190
        select->quick=tmp;
4943
15191
      }
4944
15192
    }
4945
 
    else if (tab->type != AM_NEXT &&
 
15193
    else if (tab->type != JT_NEXT && 
4946
15194
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4947
15195
    {
4948
15196
      /*
4949
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
15197
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
4950
15198
 
4951
 
        Use a traversal function that starts by reading the last row
4952
 
        with key part (A) and then traverse the index backwards.
 
15199
        Use a traversal function that starts by reading the last row
 
15200
        with key part (A) and then traverse the index backwards.
4953
15201
      */
4954
15202
      tab->read_first_record= join_read_last_key;
4955
15203
      tab->read_record.read_record= join_read_prev_same;
4957
15205
  }
4958
15206
  else if (select && select->quick)
4959
15207
    select->quick->sorted= 1;
4960
 
  return 1;
 
15208
  return(1);
4961
15209
}
4962
15210
 
 
15211
 
4963
15212
/*
4964
15213
  If not selecting by given key, create an index how records should be read
4965
15214
 
4966
15215
  SYNOPSIS
4967
15216
   create_sort_index()
4968
 
     session            Thread Cursor
 
15217
     thd                Thread handler
4969
15218
     tab                Table to sort (in join structure)
4970
15219
     order              How table should be sorted
4971
15220
     filesort_limit     Max number of rows that needs to be sorted
4972
15221
     select_limit       Max number of rows in final output
4973
15222
                        Used to decide if we should use index or not
4974
 
     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
 
15223
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
 
15224
                        Used to decide if we should use index or not     
4976
15225
 
4977
15226
 
4978
15227
  IMPLEMENTATION
4979
15228
   - If there is an index that can be used, 'tab' is modified to use
4980
15229
     this index.
4981
 
   - If no index, create with filesort() an index cursor that can be used to
 
15230
   - If no index, create with filesort() an index file that can be used to
4982
15231
     retrieve rows in order (should be done with 'read_record').
4983
15232
     The sorted data is stored in tab->table and will be freed when calling
4984
 
     tab->table->free_io_cache().
 
15233
     free_io_cache(tab->table).
4985
15234
 
4986
15235
  RETURN VALUES
4987
15236
    0           ok
4988
15237
    -1          Some fatal error
4989
15238
    1           No records
4990
15239
*/
4991
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
15240
 
 
15241
static int
 
15242
create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
15243
                  ha_rows filesort_limit, ha_rows select_limit,
 
15244
                  bool is_order_by)
4992
15245
{
4993
 
  uint32_t length= 0;
 
15246
  uint length= 0;
4994
15247
  ha_rows examined_rows;
4995
 
  Table *table;
4996
 
  optimizer::SqlSelect *select= NULL;
4997
 
  JoinTable *tab;
 
15248
  TABLE *table;
 
15249
  SQL_SELECT *select;
 
15250
  JOIN_TAB *tab;
4998
15251
 
4999
15252
  if (join->tables == join->const_tables)
5000
15253
    return(0);                          // One row, no need to sort
5008
15261
    is going to be used as it is applied now only for one table queries
5009
15262
    with covering indexes.
5010
15263
  */
5011
 
  if ((order != join->group_list ||
 
15264
  if ((order != join->group_list || 
5012
15265
       !(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,
 
15266
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
15267
      test_if_skip_sort_order(tab,order,select_limit,0, 
5015
15268
                              is_order_by ?  &table->keys_in_use_for_order_by :
5016
15269
                              &table->keys_in_use_for_group_by))
5017
15270
    return(0);
5018
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
15271
  for (ORDER *ord= join->order; ord; ord= ord->next)
5019
15272
    length++;
5020
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
5021
 
  {
5022
 
    return(-1);
5023
 
  }
 
15273
  if (!(join->sortorder= 
 
15274
        make_unireg_sortorder(order, &length, join->sortorder)))
 
15275
    goto err;                           /* purecov: inspected */
5024
15276
 
5025
 
  table->sort.io_cache= new internal::IO_CACHE;
 
15277
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
15278
                                             MYF(MY_WME | MY_ZEROFILL));
5026
15279
  table->status=0;                              // May be wrong if quick_select
5027
15280
 
5028
15281
  // If table has a range, move it to select
5032
15285
    {
5033
15286
      select->quick=tab->quick;
5034
15287
      tab->quick=0;
5035
 
      /*
 
15288
      /* 
5036
15289
        We can only use 'Only index' if quick key is same as ref_key
5037
15290
        and in index_merge 'Only index' cannot be used
5038
15291
      */
5039
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
15292
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
5040
15293
      {
5041
 
        table->key_read=0;
5042
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15294
        table->key_read=0;
 
15295
        table->file->extra(HA_EXTRA_NO_KEYREAD);
5043
15296
      }
5044
15297
    }
5045
15298
    else
5046
15299
    {
5047
15300
      /*
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.
 
15301
        We have a ref on a const;  Change this to a range that filesort
 
15302
        can use.
 
15303
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
15304
        field, quick will contain an empty record set.
5052
15305
      */
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
 
      }
 
15306
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
15307
                                                     tab->found_records))))
 
15308
        goto err;
5060
15309
    }
5061
15310
  }
5062
15311
 
5063
 
  if (table->getShare()->getType())
5064
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
15312
  /* Fill schema tables with data before filesort if it's necessary */
 
15313
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
15314
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
15315
    goto err;
5065
15316
 
5066
 
  FileSort filesort(*session);
5067
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5068
 
                                         select, filesort_limit, 0,
5069
 
                                         examined_rows);
 
15317
  if (table->s->tmp_table)
 
15318
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
15319
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
15320
                                     select, filesort_limit, 0,
 
15321
                                     &examined_rows);
5070
15322
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5071
15323
  if (select)
5072
15324
  {
5076
15328
  tab->select_cond=0;
5077
15329
  tab->last_inner= 0;
5078
15330
  tab->first_unmatched= 0;
5079
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
15331
  tab->type=JT_ALL;                             // Read with normal read_record
5080
15332
  tab->read_first_record= join_init_read_record;
5081
15333
  tab->join->examined_rows+=examined_rows;
5082
15334
  if (table->key_read)                          // Restore if we used indexes
5083
15335
  {
5084
15336
    table->key_read=0;
5085
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
15337
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5086
15338
  }
5087
 
 
5088
15339
  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;
 
15340
err:
 
15341
  return(-1);
 
15342
}
 
15343
 
 
15344
/*****************************************************************************
 
15345
  Remove duplicates from tmp table
 
15346
  This should be recoded to add a unique index to the table and remove
 
15347
  duplicates
 
15348
  Table is a locked single thread table
 
15349
  fields is the number of fields to check (from the end)
 
15350
*****************************************************************************/
 
15351
 
 
15352
static bool compare_record(TABLE *table, Field **ptr)
 
15353
{
 
15354
  for (; *ptr ; ptr++)
 
15355
  {
 
15356
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
15357
      return 1;
 
15358
  }
 
15359
  return 0;
 
15360
}
 
15361
 
 
15362
static bool copy_blobs(Field **ptr)
 
15363
{
 
15364
  for (; *ptr ; ptr++)
 
15365
  {
 
15366
    if ((*ptr)->flags & BLOB_FLAG)
 
15367
      if (((Field_blob *) (*ptr))->copy())
 
15368
        return 1;                               // Error
 
15369
  }
 
15370
  return 0;
 
15371
}
 
15372
 
 
15373
static void free_blobs(Field **ptr)
 
15374
{
 
15375
  for (; *ptr ; ptr++)
 
15376
  {
 
15377
    if ((*ptr)->flags & BLOB_FLAG)
 
15378
      ((Field_blob *) (*ptr))->free();
 
15379
  }
 
15380
}
 
15381
 
 
15382
 
 
15383
static int
 
15384
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
 
15385
{
 
15386
  int error;
 
15387
  ulong reclength,offset;
 
15388
  uint field_count;
 
15389
  THD *thd= join->thd;
 
15390
 
 
15391
  entry->reginfo.lock_type=TL_WRITE;
 
15392
 
 
15393
  /* Calculate how many saved fields there is in list */
 
15394
  field_count=0;
 
15395
  List_iterator<Item> it(fields);
 
15396
  Item *item;
 
15397
  while ((item=it++))
 
15398
  {
 
15399
    if (item->get_tmp_table_field() && ! item->const_item())
 
15400
      field_count++;
 
15401
  }
 
15402
 
 
15403
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
15404
  {                    // only const items with no OPTION_FOUND_ROWS
 
15405
    join->unit->select_limit_cnt= 1;            // Only send first row
 
15406
    return(0);
 
15407
  }
 
15408
  Field **first_field=entry->field+entry->s->fields - field_count;
 
15409
  offset= (field_count ? 
 
15410
           entry->field[entry->s->fields - field_count]->
 
15411
           offset(entry->record[0]) : 0);
 
15412
  reclength=entry->s->reclength-offset;
 
15413
 
 
15414
  free_io_cache(entry);                         // Safety
 
15415
  entry->file->info(HA_STATUS_VARIABLE);
 
15416
  if (entry->s->db_type() == heap_hton ||
 
15417
      (!entry->s->blob_fields &&
 
15418
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
15419
        thd->variables.sortbuff_size)))
 
15420
    error=remove_dup_with_hash_index(join->thd, entry,
 
15421
                                     field_count, first_field,
 
15422
                                     reclength, having);
 
15423
  else
 
15424
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
15425
                                  having);
 
15426
 
 
15427
  free_blobs(first_field);
 
15428
  return(error);
 
15429
}
 
15430
 
 
15431
 
 
15432
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
 
15433
                                   ulong offset, Item *having)
 
15434
{
 
15435
  handler *file=table->file;
5094
15436
  char *org_record,*new_record;
5095
 
  unsigned char *record;
 
15437
  uchar *record;
5096
15438
  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);
 
15439
  ulong reclength= table->s->reclength-offset;
 
15440
 
 
15441
  org_record=(char*) (record=table->record[0])+offset;
 
15442
  new_record=(char*) table->record[1]+offset;
 
15443
 
 
15444
  file->ha_rnd_init(1);
 
15445
  error=file->rnd_next(record);
5104
15446
  for (;;)
5105
15447
  {
5106
 
    if (session->getKilled())
 
15448
    if (thd->killed)
5107
15449
    {
5108
 
      session->send_kill_message();
 
15450
      thd->send_kill_message();
5109
15451
      error=0;
5110
15452
      goto err;
5111
15453
    }
5112
15454
    if (error)
5113
15455
    {
5114
15456
      if (error == HA_ERR_RECORD_DELETED)
5115
 
        continue;
 
15457
        continue;
5116
15458
      if (error == HA_ERR_END_OF_FILE)
5117
 
        break;
 
15459
        break;
5118
15460
      goto err;
5119
15461
    }
5120
15462
    if (having && !having->val_int())
5121
15463
    {
5122
 
      if ((error=cursor->deleteRecord(record)))
5123
 
        goto err;
5124
 
      error=cursor->rnd_next(record);
 
15464
      if ((error=file->ha_delete_row(record)))
 
15465
        goto err;
 
15466
      error=file->rnd_next(record);
5125
15467
      continue;
5126
15468
    }
5127
15469
    if (copy_blobs(first_field))
5132
15474
    }
5133
15475
    memcpy(new_record,org_record,reclength);
5134
15476
 
5135
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
15477
    /* Read through rest of file and mark duplicated rows deleted */
5136
15478
    bool found=0;
5137
15479
    for (;;)
5138
15480
    {
5139
 
      if ((error=cursor->rnd_next(record)))
 
15481
      if ((error=file->rnd_next(record)))
5140
15482
      {
5141
 
        if (error == HA_ERR_RECORD_DELETED)
5142
 
          continue;
5143
 
        if (error == HA_ERR_END_OF_FILE)
5144
 
          break;
5145
 
        goto err;
 
15483
        if (error == HA_ERR_RECORD_DELETED)
 
15484
          continue;
 
15485
        if (error == HA_ERR_END_OF_FILE)
 
15486
          break;
 
15487
        goto err;
5146
15488
      }
5147
 
      if (table->compare_record(first_field) == 0)
 
15489
      if (compare_record(table, first_field) == 0)
5148
15490
      {
5149
 
        if ((error=cursor->deleteRecord(record)))
5150
 
          goto err;
 
15491
        if ((error=file->ha_delete_row(record)))
 
15492
          goto err;
5151
15493
      }
5152
15494
      else if (!found)
5153
15495
      {
5154
 
        found= 1;
5155
 
        cursor->position(record);       // Remember position
 
15496
        found=1;
 
15497
        file->position(record); // Remember position
5156
15498
      }
5157
15499
    }
5158
15500
    if (!found)
5159
 
      break;                                    // End of cursor
5160
 
    /* Move current position to the next row */
5161
 
    error= cursor->rnd_pos(record, cursor->ref);
 
15501
      break;                                    // End of file
 
15502
    /* Restart search on next row */
 
15503
    error=file->restart_rnd_next(record,file->ref);
5162
15504
  }
5163
15505
 
5164
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15506
  file->extra(HA_EXTRA_NO_CACHE);
5165
15507
  return(0);
5166
15508
err:
5167
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
15509
  file->extra(HA_EXTRA_NO_CACHE);
5168
15510
  if (error)
5169
 
    table->print_error(error,MYF(0));
 
15511
    file->print_error(error,MYF(0));
5170
15512
  return(1);
5171
15513
}
5172
15514
 
 
15515
 
5173
15516
/**
5174
15517
  Generate a hash index for each row to quickly find duplicate rows.
5175
15518
 
5176
15519
  @note
5177
15520
    Note that this will not work on tables with blobs!
5178
15521
*/
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)
 
15522
 
 
15523
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
15524
                                      uint field_count,
 
15525
                                      Field **first_field,
 
15526
                                      ulong key_length,
 
15527
                                      Item *having)
5185
15528
{
5186
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
15529
  uchar *key_buffer, *key_pos, *record=table->record[0];
5187
15530
  int error;
5188
 
  Cursor *cursor= table->cursor;
5189
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5190
 
  uint32_t *field_length;
 
15531
  handler *file= table->file;
 
15532
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
15533
  uint *field_lengths,*field_length;
5191
15534
  HASH hash;
5192
 
  std::vector<unsigned char> key_buffer;
5193
 
  std::vector<uint32_t> field_lengths;
5194
15535
 
5195
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5196
 
  field_lengths.resize(field_count);
 
15536
  if (!my_multi_malloc(MYF(MY_WME),
 
15537
                       &key_buffer,
 
15538
                       (uint) ((key_length + extra_length) *
 
15539
                               (long) file->stats.records),
 
15540
                       &field_lengths,
 
15541
                       (uint) (field_count*sizeof(*field_lengths)),
 
15542
                       NullS))
 
15543
    return(1);
5197
15544
 
5198
15545
  {
5199
15546
    Field **ptr;
5200
 
    uint32_t total_length= 0;
5201
 
 
5202
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
15547
    ulong total_length= 0;
 
15548
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5203
15549
    {
5204
 
      uint32_t length= (*ptr)->sort_length();
 
15550
      uint length= (*ptr)->sort_length();
5205
15551
      (*field_length++)= length;
5206
15552
      total_length+= length;
5207
15553
    }
5210
15556
    extra_length= ALIGN_SIZE(key_length)-key_length;
5211
15557
  }
5212
15558
 
5213
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
15559
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5214
15560
                key_length, (hash_get_key) 0, 0, 0))
5215
15561
  {
 
15562
    my_free((char*) key_buffer,MYF(0));
5216
15563
    return(1);
5217
15564
  }
5218
15565
 
5219
 
  cursor->startTableScan(1);
5220
 
  key_pos= &key_buffer[0];
 
15566
  file->ha_rnd_init(1);
 
15567
  key_pos=key_buffer;
5221
15568
  for (;;)
5222
15569
  {
5223
 
    unsigned char *org_key_pos;
5224
 
    if (session->getKilled())
 
15570
    uchar *org_key_pos;
 
15571
    if (thd->killed)
5225
15572
    {
5226
 
      session->send_kill_message();
 
15573
      thd->send_kill_message();
5227
15574
      error=0;
5228
15575
      goto err;
5229
15576
    }
5230
 
    if ((error=cursor->rnd_next(record)))
 
15577
    if ((error=file->rnd_next(record)))
5231
15578
    {
5232
15579
      if (error == HA_ERR_RECORD_DELETED)
5233
 
        continue;
 
15580
        continue;
5234
15581
      if (error == HA_ERR_END_OF_FILE)
5235
 
        break;
 
15582
        break;
5236
15583
      goto err;
5237
15584
    }
5238
15585
    if (having && !having->val_int())
5239
15586
    {
5240
 
      if ((error=cursor->deleteRecord(record)))
5241
 
        goto err;
 
15587
      if ((error=file->ha_delete_row(record)))
 
15588
        goto err;
5242
15589
      continue;
5243
15590
    }
5244
15591
 
5245
15592
    /* copy fields to key buffer */
5246
15593
    org_key_pos= key_pos;
5247
 
    field_length= &field_lengths[0];
 
15594
    field_length=field_lengths;
5248
15595
    for (Field **ptr= first_field ; *ptr ; ptr++)
5249
15596
    {
5250
15597
      (*ptr)->sort_string(key_pos,*field_length);
5254
15601
    if (hash_search(&hash, org_key_pos, key_length))
5255
15602
    {
5256
15603
      /* Duplicated found ; Remove the row */
5257
 
      if ((error=cursor->deleteRecord(record)))
5258
 
        goto err;
 
15604
      if ((error=file->ha_delete_row(record)))
 
15605
        goto err;
5259
15606
    }
5260
15607
    else
5261
15608
      (void) my_hash_insert(&hash, org_key_pos);
5262
15609
    key_pos+=extra_length;
5263
15610
  }
 
15611
  my_free((char*) key_buffer,MYF(0));
5264
15612
  hash_free(&hash);
5265
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5266
 
  (void) cursor->endTableScan();
 
15613
  file->extra(HA_EXTRA_NO_CACHE);
 
15614
  (void) file->ha_rnd_end();
5267
15615
  return(0);
5268
15616
 
5269
15617
err:
 
15618
  my_free((char*) key_buffer,MYF(0));
5270
15619
  hash_free(&hash);
5271
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5272
 
  (void) cursor->endTableScan();
 
15620
  file->extra(HA_EXTRA_NO_CACHE);
 
15621
  (void) file->ha_rnd_end();
5273
15622
  if (error)
5274
 
    table->print_error(error,MYF(0));
 
15623
    file->print_error(error,MYF(0));
5275
15624
  return(1);
5276
15625
}
5277
15626
 
5278
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
15627
 
 
15628
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
 
15629
                                  SORT_FIELD *sortorder)
5279
15630
{
5280
 
  uint32_t count;
5281
 
  SortField *sort,*pos;
 
15631
  uint count;
 
15632
  SORT_FIELD *sort,*pos;
5282
15633
 
5283
15634
  count=0;
5284
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
15635
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
5285
15636
    count++;
5286
15637
  if (!sortorder)
5287
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
15638
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
5288
15639
                                       (max(count, *length) + 1));
5289
15640
  pos= sort= sortorder;
5290
15641
 
5311
15662
  return(sort);
5312
15663
}
5313
15664
 
 
15665
 
 
15666
/*****************************************************************************
 
15667
  Fill join cache with packed records
 
15668
  Records are stored in tab->cache.buffer and last record in
 
15669
  last record is stored with pointers to blobs to support very big
 
15670
  records
 
15671
******************************************************************************/
 
15672
 
 
15673
static int
 
15674
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
 
15675
{
 
15676
  register unsigned int i;
 
15677
  unsigned int length, blobs;
 
15678
  size_t size;
 
15679
  CACHE_FIELD *copy,**blob_ptr;
 
15680
  JOIN_CACHE  *cache;
 
15681
  JOIN_TAB *join_tab;
 
15682
 
 
15683
  cache= &tables[table_count].cache;
 
15684
  cache->fields=blobs=0;
 
15685
 
 
15686
  join_tab=tables;
 
15687
  for (i=0 ; i < table_count ; i++,join_tab++)
 
15688
  {
 
15689
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
15690
      calc_used_field_length(thd, join_tab);
 
15691
    cache->fields+=join_tab->used_fields;
 
15692
    blobs+=join_tab->used_blobs;
 
15693
 
 
15694
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
15695
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15696
    {
 
15697
      cache->fields++;
 
15698
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
15699
    }
 
15700
  }
 
15701
  if (!(cache->field=(CACHE_FIELD*)
 
15702
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
15703
 
 
15704
                  sizeof(CACHE_FIELD*))))
 
15705
  {
 
15706
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
 
15707
    cache->buff=0;                              /* purecov: inspected */
 
15708
    return(1);                          /* purecov: inspected */
 
15709
  }
 
15710
  copy=cache->field;
 
15711
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
15712
    (cache->field+cache->fields+table_count*2);
 
15713
 
 
15714
  length=0;
 
15715
  for (i=0 ; i < table_count ; i++)
 
15716
  {
 
15717
    uint null_fields=0,used_fields;
 
15718
    Field **f_ptr,*field;
 
15719
    MY_BITMAP *read_set= tables[i].table->read_set;
 
15720
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
15721
         used_fields ;
 
15722
         f_ptr++)
 
15723
    {
 
15724
      field= *f_ptr;
 
15725
      if (bitmap_is_set(read_set, field->field_index))
 
15726
      {
 
15727
        used_fields--;
 
15728
        length+=field->fill_cache_field(copy);
 
15729
        if (copy->blob_field)
 
15730
          (*blob_ptr++)=copy;
 
15731
        if (field->maybe_null())
 
15732
          null_fields++;
 
15733
        copy->get_rowid= NULL;
 
15734
        copy++;
 
15735
      }
 
15736
    }
 
15737
    /* Copy null bits from table */
 
15738
    if (null_fields && tables[i].table->s->null_fields)
 
15739
    {                                           /* must copy null bits */
 
15740
      copy->str= tables[i].table->null_flags;
 
15741
      copy->length= tables[i].table->s->null_bytes;
 
15742
      copy->strip=0;
 
15743
      copy->blob_field=0;
 
15744
      copy->get_rowid= NULL;
 
15745
      length+=copy->length;
 
15746
      copy++;
 
15747
      cache->fields++;
 
15748
    }
 
15749
    /* If outer join table, copy null_row flag */
 
15750
    if (tables[i].table->maybe_null)
 
15751
    {
 
15752
      copy->str= (uchar*) &tables[i].table->null_row;
 
15753
      copy->length=sizeof(tables[i].table->null_row);
 
15754
      copy->strip=0;
 
15755
      copy->blob_field=0;
 
15756
      copy->get_rowid= NULL;
 
15757
      length+=copy->length;
 
15758
      copy++;
 
15759
      cache->fields++;
 
15760
    }
 
15761
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
15762
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
15763
    {
 
15764
      copy->str= tables[i].table->file->ref;
 
15765
      copy->length= tables[i].table->file->ref_length;
 
15766
      copy->strip=0;
 
15767
      copy->blob_field=0;
 
15768
      copy->get_rowid= NULL;
 
15769
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
15770
      {
 
15771
        /* We will need to call h->position(): */
 
15772
        copy->get_rowid= tables[i].table;
 
15773
        /* And those after us won't have to: */
 
15774
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
15775
      }
 
15776
      copy++;
 
15777
    }
 
15778
  }
 
15779
 
 
15780
  cache->length=length+blobs*sizeof(char*);
 
15781
  cache->blobs=blobs;
 
15782
  *blob_ptr=0;                                  /* End sequentel */
 
15783
  size=max(thd->variables.join_buff_size, cache->length);
 
15784
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
15785
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
15786
  cache->end=cache->buff+size;
 
15787
  reset_cache_write(cache);
 
15788
  return(0);
 
15789
}
 
15790
 
 
15791
 
 
15792
static ulong
 
15793
used_blob_length(CACHE_FIELD **ptr)
 
15794
{
 
15795
  uint length,blob_length;
 
15796
  for (length=0 ; *ptr ; ptr++)
 
15797
  {
 
15798
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
15799
    length+=blob_length;
 
15800
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
15801
  }
 
15802
  return length;
 
15803
}
 
15804
 
 
15805
 
 
15806
static bool
 
15807
store_record_in_cache(JOIN_CACHE *cache)
 
15808
{
 
15809
  uint length;
 
15810
  uchar *pos;
 
15811
  CACHE_FIELD *copy,*end_field;
 
15812
  bool last_record;
 
15813
 
 
15814
  pos=cache->pos;
 
15815
  end_field=cache->field+cache->fields;
 
15816
 
 
15817
  length=cache->length;
 
15818
  if (cache->blobs)
 
15819
    length+=used_blob_length(cache->blob_ptr);
 
15820
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
15821
    cache->ptr_record=cache->records;
 
15822
  /*
 
15823
    There is room in cache. Put record there
 
15824
  */
 
15825
  cache->records++;
 
15826
  for (copy=cache->field ; copy < end_field; copy++)
 
15827
  {
 
15828
    if (copy->blob_field)
 
15829
    {
 
15830
      if (last_record)
 
15831
      {
 
15832
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
15833
                                    copy->blob_field->charset());
 
15834
        pos+=copy->length+sizeof(char*);
 
15835
      }
 
15836
      else
 
15837
      {
 
15838
        copy->blob_field->get_image(pos, copy->length, // blob length
 
15839
                                    copy->blob_field->charset());
 
15840
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
15841
        pos+=copy->length+copy->blob_length;
 
15842
      }
 
15843
    }
 
15844
    else
 
15845
    {
 
15846
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
15847
      if (copy->get_rowid)
 
15848
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
15849
 
 
15850
      if (copy->strip)
 
15851
      {
 
15852
        uchar *str,*end;
 
15853
        for (str=copy->str,end= str+copy->length;
 
15854
             end > str && end[-1] == ' ' ;
 
15855
             end--) ;
 
15856
        length=(uint) (end-str);
 
15857
        memcpy(pos+2, str, length);
 
15858
        int2store(pos, length);
 
15859
        pos+= length+2;
 
15860
      }
 
15861
      else
 
15862
      {
 
15863
        memcpy(pos,copy->str,copy->length);
 
15864
        pos+=copy->length;
 
15865
      }
 
15866
    }
 
15867
  }
 
15868
  cache->pos=pos;
 
15869
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
15870
}
 
15871
 
 
15872
 
 
15873
static void
 
15874
reset_cache_read(JOIN_CACHE *cache)
 
15875
{
 
15876
  cache->record_nr=0;
 
15877
  cache->pos=cache->buff;
 
15878
}
 
15879
 
 
15880
 
 
15881
static void reset_cache_write(JOIN_CACHE *cache)
 
15882
{
 
15883
  reset_cache_read(cache);
 
15884
  cache->records= 0;
 
15885
  cache->ptr_record= (uint) ~0;
 
15886
}
 
15887
 
 
15888
 
 
15889
static void
 
15890
read_cached_record(JOIN_TAB *tab)
 
15891
{
 
15892
  uchar *pos;
 
15893
  uint length;
 
15894
  bool last_record;
 
15895
  CACHE_FIELD *copy,*end_field;
 
15896
 
 
15897
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
15898
  pos=tab->cache.pos;
 
15899
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
15900
       copy < end_field;
 
15901
       copy++)
 
15902
  {
 
15903
    if (copy->blob_field)
 
15904
    {
 
15905
      if (last_record)
 
15906
      {
 
15907
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
15908
                                    copy->blob_field->charset());
 
15909
        pos+=copy->length+sizeof(char*);
 
15910
      }
 
15911
      else
 
15912
      {
 
15913
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
15914
        pos+=copy->length+copy->blob_field->get_length();
 
15915
      }
 
15916
    }
 
15917
    else
 
15918
    {
 
15919
      if (copy->strip)
 
15920
      {
 
15921
        length= uint2korr(pos);
 
15922
        memcpy(copy->str, pos+2, length);
 
15923
        memset(copy->str+length, ' ', copy->length-length);
 
15924
        pos+= 2 + length;
 
15925
      }
 
15926
      else
 
15927
      {
 
15928
        memcpy(copy->str,pos,copy->length);
 
15929
        pos+=copy->length;
 
15930
      }
 
15931
    }
 
15932
  }
 
15933
  tab->cache.pos=pos;
 
15934
  return;
 
15935
}
 
15936
 
 
15937
 
5314
15938
/*
5315
15939
  eq_ref: Create the lookup key and check if it is the same as saved key
5316
15940
 
5317
15941
  SYNOPSIS
5318
15942
    cmp_buffer_with_ref()
5319
15943
      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
 
15944
 
 
15945
  DESCRIPTION 
 
15946
    Used by eq_ref access method: create the index lookup key and check if 
5323
15947
    we've used this key at previous lookup (If yes, we don't need to repeat
5324
15948
    the lookup - the record has been already fetched)
5325
15949
 
5326
 
  RETURN
 
15950
  RETURN 
5327
15951
    true   No cached record for the key, or failed to create the key (due to
5328
15952
           out-of-domain error)
5329
 
    false  The created key is the same as the previous one (and the record
 
15953
    false  The created key is the same as the previous one (and the record 
5330
15954
           is already in table->record)
5331
15955
*/
5332
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
15956
 
 
15957
static bool
 
15958
cmp_buffer_with_ref(JOIN_TAB *tab)
5333
15959
{
5334
15960
  bool no_prev_key;
5335
15961
  if (!tab->ref.disable_cache)
5340
15966
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5341
15967
    }
5342
15968
  }
5343
 
  else
 
15969
  else 
5344
15970
    no_prev_key= true;
5345
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
15971
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
15972
                                            &tab->ref)) ||
5346
15973
      no_prev_key)
5347
15974
    return 1;
5348
15975
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5349
15976
    != 0;
5350
15977
}
5351
15978
 
5352
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
15979
 
 
15980
bool
 
15981
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
5353
15982
{
5354
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5355
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15983
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
15984
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
15985
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
5356
15986
  bool result= 0;
5357
15987
 
5358
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
15988
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5359
15989
  {
5360
15990
    if ((*copy)->copy() & 1)
5361
15991
    {
5363
15993
      break;
5364
15994
    }
5365
15995
  }
5366
 
  session->count_cuted_fields= save_count_cuted_fields;
 
15996
  thd->count_cuted_fields= save_count_cuted_fields;
 
15997
  dbug_tmp_restore_column_map(table->write_set, old_map);
5367
15998
  return result;
5368
15999
}
5369
16000
 
 
16001
 
5370
16002
/*****************************************************************************
5371
16003
  Group and order functions
5372
16004
*****************************************************************************/
5374
16006
/**
5375
16007
  Resolve an ORDER BY or GROUP BY column reference.
5376
16008
 
5377
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
16009
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
5378
16010
  BY clause, find the actual column it represents. If the column being
5379
16011
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5380
16012
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5387
16019
 
5388
16020
  ref_pointer_array and all_fields are updated.
5389
16021
 
5390
 
  @param[in] session                 Pointer to current thread structure
 
16022
  @param[in] thd                     Pointer to current thread structure
5391
16023
  @param[in,out] ref_pointer_array  All select, group and order by fields
5392
16024
  @param[in] tables                 List of tables to search in (usually
5393
16025
    FROM clause)
5396
16028
    SELECT list)
5397
16029
  @param[in,out] all_fields         All select, group and order by fields
5398
16030
  @param[in] is_group_field         True if order is a GROUP field, false if
5399
 
    order_st by field
 
16031
    ORDER by field
5400
16032
 
5401
16033
  @retval
5402
16034
    false if OK
5403
16035
  @retval
5404
16036
    true  if error occurred
5405
16037
*/
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)
 
16038
 
 
16039
static bool
 
16040
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16041
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
 
16042
                   bool is_group_field)
5413
16043
{
5414
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
16044
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
5415
16045
  Item::Type order_item_type;
5416
16046
  Item **select_item; /* The corresponding item from the SELECT clause. */
5417
16047
  Field *from_field;  /* The corresponding field from the FROM clause. */
5418
 
  uint32_t counter;
 
16048
  uint counter;
5419
16049
  enum_resolution_type resolution;
5420
16050
 
5421
16051
  /*
5424
16054
  */
5425
16055
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5426
16056
  {                                             /* Order by position */
5427
 
    uint32_t count= (uint32_t) order_item->val_int();
 
16057
    uint count= (uint) order_item->val_int();
5428
16058
    if (!count || count > fields.elements)
5429
16059
    {
5430
16060
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5431
 
               order_item->full_name(), session->where);
 
16061
               order_item->full_name(), thd->where);
5432
16062
      return true;
5433
16063
    }
5434
16064
    order->item= ref_pointer_array + count - 1;
5437
16067
    order->counter_used= 1;
5438
16068
    return false;
5439
16069
  }
5440
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5441
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
16070
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
 
16071
  select_item= find_item_in_list(order_item, fields, &counter,
5442
16072
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5443
16073
  if (!select_item)
5444
16074
    return true; /* The item is not unique, or some other error occured. */
5454
16084
      for this name (in case if we would perform lookup in all tables).
5455
16085
    */
5456
16086
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5457
 
        order_item->fix_fields(session, order->item))
 
16087
        order_item->fix_fields(thd, order->item))
5458
16088
      return true;
5459
16089
 
5460
16090
    /* Lookup the current GROUP field in the FROM clause. */
5463
16093
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5464
16094
        order_item_type == Item::REF_ITEM)
5465
16095
    {
5466
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5467
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
16096
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
16097
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
16098
                                       false);
5468
16099
      if (!from_field)
5469
16100
        from_field= (Field*) not_found_field;
5470
16101
    }
5502
16133
        warning so the user knows that the field from the FROM clause
5503
16134
        overshadows the column reference from the SELECT list.
5504
16135
      */
5505
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
16136
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5506
16137
                          ER(ER_NON_UNIQ_ERROR),
5507
16138
                          ((Item_ident*) order_item)->field_name,
5508
 
                          session->where);
 
16139
                          current_thd->where);
5509
16140
    }
5510
16141
  }
5511
16142
 
5522
16153
    arguments for which fix_fields already was called.
5523
16154
  */
5524
16155
  if (!order_item->fixed &&
5525
 
      (order_item->fix_fields(session, order->item) ||
 
16156
      (order_item->fix_fields(thd, order->item) ||
5526
16157
       (order_item= *order->item)->check_cols(1) ||
5527
 
       session->is_fatal_error))
 
16158
       thd->is_fatal_error))
5528
16159
    return true; /* Wrong field. */
5529
16160
 
5530
 
  uint32_t el= all_fields.elements;
 
16161
  uint el= all_fields.elements;
5531
16162
  all_fields.push_front(order_item); /* Add new field to field list. */
5532
16163
  ref_pointer_array[el]= order_item;
5533
16164
  order->item= ref_pointer_array + el;
5534
16165
  return false;
5535
16166
}
5536
16167
 
 
16168
 
5537
16169
/**
5538
16170
  Change order to point at item in select list.
5539
16171
 
5540
16172
  If item isn't a number and doesn't exits in the select list, add it the
5541
16173
  the field list.
5542
16174
*/
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)
 
16175
 
 
16176
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16177
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
5549
16178
{
5550
 
  session->where="order clause";
 
16179
  thd->where="order clause";
5551
16180
  for (; order; order=order->next)
5552
16181
  {
5553
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
16182
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5554
16183
                           all_fields, false))
5555
16184
      return 1;
5556
16185
  }
5557
16186
  return 0;
5558
16187
}
5559
16188
 
 
16189
 
5560
16190
/**
5561
16191
  Intitialize the GROUP BY list.
5562
16192
 
5563
 
  @param session                        Thread Cursor
 
16193
  @param thd                    Thread handler
5564
16194
  @param ref_pointer_array      We store references to all fields that was
5565
16195
                               not in 'fields' here.
5566
16196
  @param fields         All fields in the select part. Any item in
5582
16212
  @retval
5583
16213
    1  error (probably out of memory)
5584
16214
*/
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)
 
16215
 
 
16216
int
 
16217
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
16218
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
 
16219
            bool *hidden_group_fields)
5592
16220
{
5593
16221
  *hidden_group_fields=0;
5594
 
  Order *ord;
 
16222
  ORDER *ord;
5595
16223
 
5596
16224
  if (!order)
5597
16225
    return 0;                           /* Everything is ok */
5598
16226
 
5599
 
  uint32_t org_fields=all_fields.elements;
 
16227
  uint org_fields=all_fields.elements;
5600
16228
 
5601
 
  session->where="group statement";
 
16229
  thd->where="group statement";
5602
16230
  for (ord= order; ord; ord= ord->next)
5603
16231
  {
5604
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
16232
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5605
16233
                           all_fields, true))
5606
16234
      return 1;
5607
16235
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5632
16260
    Item_field *field;
5633
16261
    int cur_pos_in_select_list= 0;
5634
16262
    List_iterator<Item> li(fields);
5635
 
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
 
16263
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5636
16264
 
5637
16265
    field= naf_it++;
5638
16266
    while (field && (item=li++))
5681
16309
  Try to use the fields in the order given by 'order' to allow one to
5682
16310
  optimize away 'order by'.
5683
16311
*/
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)
 
16312
 
 
16313
static ORDER *
 
16314
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
16315
                      ORDER *order_list, List<Item> &fields,
 
16316
                      List<Item> &all_fields __attribute__((__unused__)),
 
16317
                      bool *all_order_by_fields_used)
5690
16318
{
5691
16319
  List_iterator<Item> li(fields);
5692
16320
  Item *item;
5693
 
  Order *order,*group,**prev;
 
16321
  ORDER *order,*group,**prev;
5694
16322
 
5695
16323
  *all_order_by_fields_used= 1;
5696
16324
  while ((item=li++))
5701
16329
  {
5702
16330
    if (order->in_field_list)
5703
16331
    {
5704
 
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
 
16332
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
5705
16333
      if (!ord)
5706
 
        return 0;
 
16334
        return 0;
5707
16335
      *prev=ord;
5708
16336
      prev= &ord->next;
5709
16337
      (*ord->item)->marker=1;
5717
16345
  {
5718
16346
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5719
16347
    {
5720
 
      /*
5721
 
        Don't put duplicate columns from the SELECT list into the
 
16348
      /* 
 
16349
        Don't put duplicate columns from the SELECT list into the 
5722
16350
        GROUP BY list.
5723
16351
      */
5724
 
      Order *ord_iter;
 
16352
      ORDER *ord_iter;
5725
16353
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5726
16354
        if ((*ord_iter->item)->eq(item, 1))
5727
16355
          goto next_item;
5728
 
 
5729
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
16356
      
 
16357
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
5730
16358
      if (!ord)
5731
 
        return 0;
 
16359
        return 0;
5732
16360
 
5733
16361
      /*
5734
16362
        We have here only field_list (not all_field_list), so we can use
5747
16375
  return group;
5748
16376
}
5749
16377
 
 
16378
 
5750
16379
/**
5751
16380
  Update join with count of the different type of fields.
5752
16381
*/
5753
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
16382
 
 
16383
void
 
16384
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
16385
                  List<Item> &fields, bool reset_with_sum_func)
5754
16386
{
5755
16387
  List_iterator<Item> li(fields);
5756
16388
  Item *field;
5767
16399
    {
5768
16400
      if (! field->const_item())
5769
16401
      {
5770
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
16402
        Item_sum *sum_item=(Item_sum*) field->real_item();
5771
16403
        if (!sum_item->depended_from() ||
5772
16404
            sum_item->depended_from() == select_lex)
5773
16405
        {
5775
16407
            param->quick_group=0;                       // UDF SUM function
5776
16408
          param->sum_func_count++;
5777
16409
 
5778
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
16410
          for (uint i=0 ; i < sum_item->arg_count ; i++)
5779
16411
          {
5780
16412
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
5781
16413
              param->field_count++;
5790
16422
    {
5791
16423
      param->func_count++;
5792
16424
      if (reset_with_sum_func)
5793
 
        field->with_sum_func=0;
5794
 
    }
5795
 
  }
5796
 
}
 
16425
        field->with_sum_func=0;
 
16426
    }
 
16427
  }
 
16428
}
 
16429
 
 
16430
 
 
16431
/**
 
16432
  Return 1 if second is a subpart of first argument.
 
16433
 
 
16434
  If first parts has different direction, change it to second part
 
16435
  (group is sorted like order)
 
16436
*/
 
16437
 
 
16438
static bool
 
16439
test_if_subpart(ORDER *a,ORDER *b)
 
16440
{
 
16441
  for (; a && b; a=a->next,b=b->next)
 
16442
  {
 
16443
    if ((*a->item)->eq(*b->item,1))
 
16444
      a->asc=b->asc;
 
16445
    else
 
16446
      return 0;
 
16447
  }
 
16448
  return test(!b);
 
16449
}
 
16450
 
 
16451
/**
 
16452
  Return table number if there is only one table in sort order
 
16453
  and group and order is compatible, else return 0.
 
16454
*/
 
16455
 
 
16456
static TABLE *
 
16457
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
 
16458
{
 
16459
  table_map map= (table_map) 0;
 
16460
 
 
16461
  if (!a)
 
16462
    a=b;                                        // Only one need to be given
 
16463
  else if (!b)
 
16464
    b=a;
 
16465
 
 
16466
  for (; a && b; a=a->next,b=b->next)
 
16467
  {
 
16468
    if (!(*a->item)->eq(*b->item,1))
 
16469
      return(0);
 
16470
    map|=a->item[0]->used_tables();
 
16471
  }
 
16472
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
16473
    return(0);
 
16474
 
 
16475
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
16476
  if (map != tables->table->map)
 
16477
    return(0);                          // More than one table
 
16478
  return(tables->table);
 
16479
}
 
16480
 
 
16481
 
 
16482
/**
 
16483
  calc how big buffer we need for comparing group entries.
 
16484
*/
 
16485
 
 
16486
static void
 
16487
calc_group_buffer(JOIN *join,ORDER *group)
 
16488
{
 
16489
  uint key_length=0, parts=0, null_parts=0;
 
16490
 
 
16491
  if (group)
 
16492
    join->group= 1;
 
16493
  for (; group ; group=group->next)
 
16494
  {
 
16495
    Item *group_item= *group->item;
 
16496
    Field *field= group_item->get_tmp_table_field();
 
16497
    if (field)
 
16498
    {
 
16499
      enum_field_types type;
 
16500
      if ((type= field->type()) == MYSQL_TYPE_BLOB)
 
16501
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
16502
      else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
 
16503
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
16504
      else
 
16505
        key_length+= field->pack_length();
 
16506
    }
 
16507
    else
 
16508
    { 
 
16509
      switch (group_item->result_type()) {
 
16510
      case REAL_RESULT:
 
16511
        key_length+= sizeof(double);
 
16512
        break;
 
16513
      case INT_RESULT:
 
16514
        key_length+= sizeof(longlong);
 
16515
        break;
 
16516
      case DECIMAL_RESULT:
 
16517
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
16518
                                                (group_item->decimals ? 1 : 0),
 
16519
                                                group_item->decimals);
 
16520
        break;
 
16521
      case STRING_RESULT:
 
16522
      {
 
16523
        enum enum_field_types type= group_item->field_type();
 
16524
        /*
 
16525
          As items represented as DATE/TIME fields in the group buffer
 
16526
          have STRING_RESULT result type, we increase the length 
 
16527
          by 8 as maximum pack length of such fields.
 
16528
        */
 
16529
        if (type == MYSQL_TYPE_TIME ||
 
16530
            type == MYSQL_TYPE_NEWDATE ||
 
16531
            type == MYSQL_TYPE_DATETIME ||
 
16532
            type == MYSQL_TYPE_TIMESTAMP)
 
16533
        {
 
16534
          key_length+= 8;
 
16535
        }
 
16536
        else
 
16537
        {
 
16538
          /*
 
16539
            Group strings are taken as varstrings and require an length field.
 
16540
            A field is not yet created by create_tmp_field()
 
16541
            and the sizes should match up.
 
16542
          */
 
16543
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
16544
        }
 
16545
        break;
 
16546
      }
 
16547
      default:
 
16548
        /* This case should never be choosen */
 
16549
        assert(0);
 
16550
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
16551
      }
 
16552
    }
 
16553
    parts++;
 
16554
    if (group_item->maybe_null)
 
16555
      null_parts++;
 
16556
  }
 
16557
  join->tmp_table_param.group_length=key_length+null_parts;
 
16558
  join->tmp_table_param.group_parts=parts;
 
16559
  join->tmp_table_param.group_null_parts=null_parts;
 
16560
}
 
16561
 
 
16562
 
 
16563
/**
 
16564
  allocate group fields or take prepared (cached).
 
16565
 
 
16566
  @param main_join   join of current select
 
16567
  @param curr_join   current join (join of current select or temporary copy
 
16568
                     of it)
 
16569
 
 
16570
  @retval
 
16571
    0   ok
 
16572
  @retval
 
16573
    1   failed
 
16574
*/
 
16575
 
 
16576
static bool
 
16577
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
16578
{
 
16579
  if (main_join->group_fields_cache.elements)
 
16580
  {
 
16581
    curr_join->group_fields= main_join->group_fields_cache;
 
16582
    curr_join->sort_and_group= 1;
 
16583
  }
 
16584
  else
 
16585
  {
 
16586
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
16587
      return (1);
 
16588
    main_join->group_fields_cache= curr_join->group_fields;
 
16589
  }
 
16590
  return (0);
 
16591
}
 
16592
 
 
16593
 
 
16594
/**
 
16595
  Get a list of buffers for saveing last group.
 
16596
 
 
16597
  Groups are saved in reverse order for easyer check loop.
 
16598
*/
 
16599
 
 
16600
static bool
 
16601
alloc_group_fields(JOIN *join,ORDER *group)
 
16602
{
 
16603
  if (group)
 
16604
  {
 
16605
    for (; group ; group=group->next)
 
16606
    {
 
16607
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
16608
      if (!tmp || join->group_fields.push_front(tmp))
 
16609
        return true;
 
16610
    }
 
16611
  }
 
16612
  join->sort_and_group=1;                       /* Mark for do_select */
 
16613
  return false;
 
16614
}
 
16615
 
5797
16616
 
5798
16617
/*
5799
16618
  Test if a single-row cache of items changed, and update the cache.
5801
16620
  @details Test if a list of items that typically represents a result
5802
16621
  row has changed. If the value of some item changed, update the cached
5803
16622
  value for this item.
5804
 
 
 
16623
  
5805
16624
  @param list list of <item, cached_value> pairs stored as Cached_item.
5806
16625
 
5807
16626
  @return -1 if no item changed
5808
16627
  @return index of the first item that changed
5809
16628
*/
 
16629
 
5810
16630
int test_if_item_cache_changed(List<Cached_item> &list)
5811
16631
{
5812
16632
  List_iterator<Cached_item> li(list);
5821
16641
  return(idx);
5822
16642
}
5823
16643
 
 
16644
 
5824
16645
/**
5825
16646
  Setup copy_fields to save fields at start of new group.
5826
16647
 
5830
16651
  Change old item_field to use a new field with points at saved fieldvalue
5831
16652
  This function is only called before use of send_fields.
5832
16653
 
5833
 
  @param session                   Session pointer
 
16654
  @param thd                   THD pointer
5834
16655
  @param param                 temporary table parameters
5835
16656
  @param ref_pointer_array     array of pointers to top elements of filed list
5836
16657
  @param res_selected_fields   new list of items of select item list
5849
16670
  @retval
5850
16671
    !=0   error
5851
16672
*/
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)
 
16673
 
 
16674
bool
 
16675
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
16676
                  Item **ref_pointer_array,
 
16677
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
16678
                  uint elements, List<Item> &all_fields)
5859
16679
{
5860
16680
  Item *pos;
5861
16681
  List_iterator_fast<Item> li(all_fields);
5862
 
  CopyField *copy= NULL;
 
16682
  Copy_field *copy= NULL;
5863
16683
  res_selected_fields.empty();
5864
16684
  res_all_fields.empty();
5865
16685
  List_iterator_fast<Item> itr(res_all_fields);
5866
16686
  List<Item> extra_funcs;
5867
 
  uint32_t i, border= all_fields.elements - elements;
 
16687
  uint i, border= all_fields.elements - elements;
5868
16688
 
5869
 
  if (param->field_count &&
5870
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
16689
  if (param->field_count && 
 
16690
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5871
16691
    goto err2;
5872
16692
 
5873
16693
  param->copy_funcs.empty();
5874
16694
  for (i= 0; (pos= li++); i++)
5875
16695
  {
5876
16696
    Field *field;
5877
 
    unsigned char *tmp;
 
16697
    uchar *tmp;
5878
16698
    Item *real_pos= pos->real_item();
5879
16699
    if (real_pos->type() == Item::FIELD_ITEM)
5880
16700
    {
5881
16701
      Item_field *item;
5882
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5883
 
        goto err;
 
16702
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
16703
        goto err;
5884
16704
      if (pos->type() == Item::REF_ITEM)
5885
16705
      {
5886
16706
        /* preserve the names of the ref when dereferncing */
5892
16712
      pos= item;
5893
16713
      if (item->field->flags & BLOB_FLAG)
5894
16714
      {
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;
 
16715
        if (!(pos= new Item_copy_string(pos)))
 
16716
          goto err;
 
16717
       /*
 
16718
         Item_copy_string::copy for function can call 
 
16719
         Item_copy_string::val_int for blob via Item_ref.
 
16720
         But if Item_copy_string::copy for blob isn't called before,
 
16721
         it's value will be wrong
 
16722
         so let's insert Item_copy_string for blobs in the beginning of 
 
16723
         copy_funcs
 
16724
         (to see full test case look at having.test, BUG #4358) 
 
16725
       */
 
16726
        if (param->copy_funcs.push_front(pos))
 
16727
          goto err;
5908
16728
      }
5909
16729
      else
5910
16730
      {
 
16731
        /* 
 
16732
           set up save buffer and change result_field to point at 
 
16733
           saved value
 
16734
        */
 
16735
        field= item->field;
 
16736
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5911
16737
        /*
5912
 
          set up save buffer and change result_field to point at
5913
 
          saved value
 
16738
          We need to allocate one extra byte for null handling and
 
16739
          another extra byte to not get warnings from purify in
 
16740
          Field_string::val_int
5914
16741
        */
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;
 
16742
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
 
16743
          goto err;
5924
16744
        if (copy)
5925
16745
        {
5926
16746
          copy->set(tmp, item->result_field);
5927
16747
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5928
 
#ifdef HAVE_VALGRIND
 
16748
#ifdef HAVE_purify
5929
16749
          copy->to_ptr[copy->from_length]= 0;
5930
16750
#endif
5931
16751
          copy++;
5940
16760
    {                                           // Save for send fields
5941
16761
      pos= real_pos;
5942
16762
      /* 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(*))
 
16763
         In most cases this result will be sent to the user.
 
16764
         This should be changed to use copy_int or copy_real depending
 
16765
         on how the value is to be used: In some cases this may be an
 
16766
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5947
16767
      */
5948
16768
      if (!(pos=new Item_copy_string(pos)))
5949
 
        goto err;
5950
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
16769
        goto err;
 
16770
      if (i < border)                           // HAVING, ORDER and GROUP BY
5951
16771
      {
5952
16772
        if (extra_funcs.push_back(pos))
5953
16773
          goto err;
5954
16774
      }
5955
16775
      else if (param->copy_funcs.push_back(pos))
5956
 
        goto err;
 
16776
        goto err;
5957
16777
    }
5958
16778
    res_all_fields.push_back(pos);
5959
16779
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5972
16792
 
5973
16793
  return(0);
5974
16794
 
5975
 
err:
 
16795
 err:
5976
16796
  if (copy)
5977
16797
    delete [] param->copy_field;                        // This is never 0
5978
16798
  param->copy_field=0;
5980
16800
  return(true);
5981
16801
}
5982
16802
 
 
16803
 
5983
16804
/**
5984
16805
  Make a copy of all simple SELECT'ed items.
5985
16806
 
5986
16807
  This is done at the start of a new group so that we can retrieve
5987
16808
  these later when the group changes.
5988
16809
*/
5989
 
void copy_fields(Tmp_Table_Param *param)
 
16810
 
 
16811
void
 
16812
copy_fields(TMP_TABLE_PARAM *param)
5990
16813
{
5991
 
  CopyField *ptr= param->copy_field;
5992
 
  CopyField *end= param->copy_field_end;
 
16814
  Copy_field *ptr=param->copy_field;
 
16815
  Copy_field *end=param->copy_field_end;
5993
16816
 
5994
16817
  for (; ptr != end; ptr++)
5995
16818
    (*ptr->do_copy)(ptr);
6000
16823
    item->copy();
6001
16824
}
6002
16825
 
 
16826
 
 
16827
/**
 
16828
  Make an array of pointers to sum_functions to speed up
 
16829
  sum_func calculation.
 
16830
 
 
16831
  @retval
 
16832
    0   ok
 
16833
  @retval
 
16834
    1   Error
 
16835
*/
 
16836
 
 
16837
bool JOIN::alloc_func_list()
 
16838
{
 
16839
  uint func_count, group_parts;
 
16840
 
 
16841
  func_count= tmp_table_param.sum_func_count;
 
16842
  /*
 
16843
    If we are using rollup, we need a copy of the summary functions for
 
16844
    each level
 
16845
  */
 
16846
  if (rollup.state != ROLLUP::STATE_NONE)
 
16847
    func_count*= (send_group_parts+1);
 
16848
 
 
16849
  group_parts= send_group_parts;
 
16850
  /*
 
16851
    If distinct, reserve memory for possible
 
16852
    disctinct->group_by optimization
 
16853
  */
 
16854
  if (select_distinct)
 
16855
  {
 
16856
    group_parts+= fields_list.elements;
 
16857
    /*
 
16858
      If the ORDER clause is specified then it's possible that
 
16859
      it also will be optimized, so reserve space for it too
 
16860
    */
 
16861
    if (order)
 
16862
    {
 
16863
      ORDER *ord;
 
16864
      for (ord= order; ord; ord= ord->next)
 
16865
        group_parts++;
 
16866
    }
 
16867
  }
 
16868
 
 
16869
  /* This must use calloc() as rollup_make_fields depends on this */
 
16870
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
16871
                                      sizeof(Item_sum***) * (group_parts+1));
 
16872
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
16873
  return(sum_funcs == 0);
 
16874
}
 
16875
 
 
16876
 
 
16877
/**
 
16878
  Initialize 'sum_funcs' array with all Item_sum objects.
 
16879
 
 
16880
  @param field_list        All items
 
16881
  @param send_fields       Items in select list
 
16882
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
16883
  @param recompute         Set to true if sum_funcs must be recomputed
 
16884
 
 
16885
  @retval
 
16886
    0  ok
 
16887
  @retval
 
16888
    1  error
 
16889
*/
 
16890
 
 
16891
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
16892
                              bool before_group_by, bool recompute)
 
16893
{
 
16894
  List_iterator_fast<Item> it(field_list);
 
16895
  Item_sum **func;
 
16896
  Item *item;
 
16897
 
 
16898
  if (*sum_funcs && !recompute)
 
16899
    return(false); /* We have already initialized sum_funcs. */
 
16900
 
 
16901
  func= sum_funcs;
 
16902
  while ((item=it++))
 
16903
  {
 
16904
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
16905
        (!((Item_sum*) item)->depended_from() ||
 
16906
         ((Item_sum *)item)->depended_from() == select_lex))
 
16907
      *func++= (Item_sum*) item;
 
16908
  }
 
16909
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
16910
  {
 
16911
    rollup.state= ROLLUP::STATE_READY;
 
16912
    if (rollup_make_fields(field_list, send_fields, &func))
 
16913
      return(true);                     // Should never happen
 
16914
  }
 
16915
  else if (rollup.state == ROLLUP::STATE_NONE)
 
16916
  {
 
16917
    for (uint i=0 ; i <= send_group_parts ;i++)
 
16918
      sum_funcs_end[i]= func;
 
16919
  }
 
16920
  else if (rollup.state == ROLLUP::STATE_READY)
 
16921
    return(false);                         // Don't put end marker
 
16922
  *func=0;                                      // End marker
 
16923
  return(false);
 
16924
}
 
16925
 
 
16926
 
6003
16927
/**
6004
16928
  Change all funcs and sum_funcs to fields in tmp table, and create
6005
16929
  new list of all items.
6006
16930
 
6007
 
  @param session                   Session pointer
 
16931
  @param thd                   THD pointer
6008
16932
  @param ref_pointer_array     array of pointers to top elements of filed list
6009
16933
  @param res_selected_fields   new list of items of select item list
6010
16934
  @param res_all_fields        new list of all items
6016
16940
  @retval
6017
16941
    !=0   error
6018
16942
*/
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)
 
16943
 
 
16944
static bool
 
16945
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
16946
                         List<Item> &res_selected_fields,
 
16947
                         List<Item> &res_all_fields,
 
16948
                         uint elements, List<Item> &all_fields)
6025
16949
{
6026
16950
  List_iterator_fast<Item> it(all_fields);
6027
16951
  Item *item_field,*item;
6029
16953
  res_selected_fields.empty();
6030
16954
  res_all_fields.empty();
6031
16955
 
6032
 
  uint32_t i, border= all_fields.elements - elements;
 
16956
  uint i, border= all_fields.elements - elements;
6033
16957
  for (i= 0; (item= it++); i++)
6034
16958
  {
6035
16959
    Field *field;
6042
16966
    {
6043
16967
      if (item->type() == Item::FIELD_ITEM)
6044
16968
      {
6045
 
        item_field= item->get_tmp_table_item(session);
 
16969
        item_field= item->get_tmp_table_item(thd);
6046
16970
      }
6047
16971
      else if ((field= item->get_tmp_table_field()))
6048
16972
      {
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
 
16973
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
16974
          item_field= ((Item_sum*) item)->result_item(field);
 
16975
        else
 
16976
          item_field= (Item*) new Item_field(field);
 
16977
        if (!item_field)
 
16978
          return(true);                    // Fatal error
6055
16979
 
6056
16980
        if (item->real_item()->type() != Item::FIELD_ITEM)
6057
16981
          field->orig_table= 0;
6058
 
        item_field->name= item->name;
 
16982
        item_field->name= item->name;
6059
16983
        if (item->type() == Item::REF_ITEM)
6060
16984
        {
6061
16985
          Item_field *ifield= (Item_field *) item_field;
6065
16989
        }
6066
16990
      }
6067
16991
      else
6068
 
        item_field= item;
 
16992
        item_field= item;
6069
16993
    }
6070
16994
    res_all_fields.push_back(item_field);
6071
16995
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6079
17003
  return(false);
6080
17004
}
6081
17005
 
 
17006
 
6082
17007
/**
6083
17008
  Change all sum_func refs to fields to point at fields in tmp table.
6084
17009
  Change all funcs to be fields in tmp table.
6085
17010
 
6086
 
  @param session                   Session pointer
 
17011
  @param thd                   THD pointer
6087
17012
  @param ref_pointer_array     array of pointers to top elements of filed list
6088
17013
  @param res_selected_fields   new list of items of select item list
6089
17014
  @param res_all_fields        new list of all items
6095
17020
  @retval
6096
17021
    1   error
6097
17022
*/
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)
 
17023
 
 
17024
static bool
 
17025
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
17026
                          List<Item> &res_selected_fields,
 
17027
                          List<Item> &res_all_fields, uint elements,
 
17028
                          List<Item> &all_fields)
6104
17029
{
6105
17030
  List_iterator_fast<Item> it(all_fields);
6106
17031
  Item *item, *new_item;
6107
17032
  res_selected_fields.empty();
6108
17033
  res_all_fields.empty();
6109
17034
 
6110
 
  uint32_t i, border= all_fields.elements - elements;
 
17035
  uint i, border= all_fields.elements - elements;
6111
17036
  for (i= 0; (item= it++); i++)
6112
17037
  {
6113
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
17038
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6114
17039
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6115
17040
      new_item;
6116
17041
  }
6120
17045
    itr++;
6121
17046
  itr.sublist(res_selected_fields, elements);
6122
17047
 
6123
 
  return session->is_fatal_error;
 
17048
  return thd->is_fatal_error;
6124
17049
}
6125
17050
 
 
17051
 
 
17052
 
6126
17053
/******************************************************************************
6127
17054
  Code for calculating functions
6128
17055
******************************************************************************/
6129
17056
 
 
17057
 
6130
17058
/**
6131
17059
  Call ::setup for all sum functions.
6132
17060
 
6133
 
  @param session           thread Cursor
 
17061
  @param thd           thread handler
6134
17062
  @param func_ptr      sum function list
6135
17063
 
6136
17064
  @retval
6138
17066
  @retval
6139
17067
    true   error
6140
17068
*/
6141
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
17069
 
 
17070
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6142
17071
{
6143
17072
  Item_sum *func;
6144
17073
  while ((func= *(func_ptr++)))
6145
17074
  {
6146
 
    if (func->setup(session))
 
17075
    if (func->setup(thd))
6147
17076
      return(true);
6148
17077
  }
6149
17078
  return(false);
6150
17079
}
6151
17080
 
6152
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
17081
 
 
17082
static void
 
17083
init_tmptable_sum_functions(Item_sum **func_ptr)
6153
17084
{
6154
17085
  Item_sum *func;
6155
17086
  while ((func= *(func_ptr++)))
6156
17087
    func->reset_field();
6157
17088
}
6158
17089
 
 
17090
 
6159
17091
/** Update record 0 in tmp_table from record 1. */
6160
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
17092
 
 
17093
static void
 
17094
update_tmptable_sum_func(Item_sum **func_ptr,
 
17095
                         TABLE *tmp_table __attribute__((unused)))
6161
17096
{
6162
17097
  Item_sum *func;
6163
17098
  while ((func= *(func_ptr++)))
6164
17099
    func->update_field();
6165
17100
}
6166
17101
 
 
17102
 
6167
17103
/** Copy result of sum functions to record in tmp_table. */
6168
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
17104
 
 
17105
static void
 
17106
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6169
17107
{
6170
17108
  for (; func_ptr != end_ptr ; func_ptr++)
6171
17109
    (void) (*func_ptr)->save_in_result_field(1);
6172
17110
  return;
6173
17111
}
6174
17112
 
6175
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
17113
 
 
17114
static bool
 
17115
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6176
17116
{
6177
17117
  for (; func_ptr != end_ptr ;func_ptr++)
6178
17118
  {
6188
17128
  return 0;
6189
17129
}
6190
17130
 
6191
 
bool update_sum_func(Item_sum **func_ptr)
 
17131
 
 
17132
static bool
 
17133
update_sum_func(Item_sum **func_ptr)
6192
17134
{
6193
17135
  Item_sum *func;
6194
17136
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6198
17140
}
6199
17141
 
6200
17142
/** Copy result of functions to record in tmp_table. */
6201
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
17143
 
 
17144
void
 
17145
copy_funcs(Item **func_ptr)
6202
17146
{
6203
17147
  Item *func;
6204
17148
  for (; (func = *func_ptr) ; func_ptr++)
6205
 
  {
6206
17149
    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
 
}
 
17150
}
 
17151
 
 
17152
 
 
17153
/**
 
17154
  Create a condition for a const reference and add this to the
 
17155
  currenct select for the table.
 
17156
*/
 
17157
 
 
17158
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
17159
{
 
17160
  if (!join_tab->ref.key_parts)
 
17161
    return(false);
 
17162
 
 
17163
  Item_cond_and *cond=new Item_cond_and();
 
17164
  TABLE *table=join_tab->table;
 
17165
  int error;
 
17166
  if (!cond)
 
17167
    return(true);
 
17168
 
 
17169
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
 
17170
  {
 
17171
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
17172
                              fieldnr-1];
 
17173
    Item *value=join_tab->ref.items[i];
 
17174
    cond->add(new Item_func_equal(new Item_field(field), value));
 
17175
  }
 
17176
  if (thd->is_fatal_error)
 
17177
    return(true);
 
17178
 
 
17179
  if (!cond->fixed)
 
17180
    cond->fix_fields(thd, (Item**)&cond);
 
17181
  if (join_tab->select)
 
17182
  {
 
17183
    error=(int) cond->add(join_tab->select->cond);
 
17184
    join_tab->select_cond=join_tab->select->cond=cond;
 
17185
  }
 
17186
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
17187
                                          &error)))
 
17188
    join_tab->select_cond=cond;
 
17189
 
 
17190
  return(error ? true : false);
 
17191
}
 
17192
 
6218
17193
 
6219
17194
/**
6220
17195
  Free joins of subselect of this select.
6221
17196
 
6222
 
  @param session      Session pointer
6223
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
17197
  @param thd      THD pointer
 
17198
  @param select   pointer to st_select_lex which subselects joins we will free
6224
17199
*/
6225
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
17200
 
 
17201
void free_underlaid_joins(THD *thd __attribute__((__unused__)),
 
17202
                          SELECT_LEX *select)
6226
17203
{
6227
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
17204
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6228
17205
       unit;
6229
17206
       unit= unit->next_unit())
6230
17207
    unit->cleanup();
6246
17223
  @b EXAMPLES
6247
17224
    @code
6248
17225
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6249
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
17226
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6250
17227
  @endcode
6251
17228
 
6252
17229
  @b IMPLEMENTATION
6259
17236
    This substitution is needed GROUP BY queries with ROLLUP if
6260
17237
    SELECT list contains expressions over group by attributes.
6261
17238
 
6262
 
  @param session                  reference to the context
 
17239
  @param thd                  reference to the context
6263
17240
  @param expr                 expression to make replacement
6264
17241
  @param group_list           list of references to group by items
6265
17242
  @param changed        out:  returns 1 if item contains a replaced field item
6266
17243
 
6267
17244
  @todo
6268
17245
    - TODO: Some functions are not null-preserving. For those functions
6269
 
    updating of the maybe_null attribute is an overkill.
 
17246
    updating of the maybe_null attribute is an overkill. 
6270
17247
 
6271
17248
  @retval
6272
17249
    0   if ok
6273
17250
  @retval
6274
17251
    1   on error
6275
17252
*/
6276
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
17253
 
 
17254
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
 
17255
                             bool *changed)
6277
17256
{
6278
17257
  if (expr->arg_count)
6279
17258
  {
6280
 
    Name_resolution_context *context= &session->lex->current_select->context;
 
17259
    Name_resolution_context *context= &thd->lex->current_select->context;
6281
17260
    Item **arg,**arg_end;
6282
17261
    bool arg_changed= false;
6283
17262
    for (arg= expr->arguments(),
6287
17266
      Item *item= *arg;
6288
17267
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6289
17268
      {
6290
 
        Order *group_tmp;
 
17269
        ORDER *group_tmp;
6291
17270
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6292
17271
        {
6293
17272
          if (item->eq(*group_tmp->item,0))
6296
17275
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6297
17276
                                        item->name)))
6298
17277
              return 1;                                 // fatal_error is set
6299
 
            session->change_item_tree(arg, new_item);
 
17278
            thd->change_item_tree(arg, new_item);
6300
17279
            arg_changed= true;
6301
17280
          }
6302
17281
        }
6303
17282
      }
6304
17283
      else if (item->type() == Item::FUNC_ITEM)
6305
17284
      {
6306
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
17285
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6307
17286
          return 1;
6308
17287
      }
6309
17288
    }
6317
17296
}
6318
17297
 
6319
17298
 
6320
 
static void print_table_array(Session *session, String *str, TableList **table,
6321
 
                              TableList **end)
6322
 
{
6323
 
  (*table)->print(session, str, QT_ORDINARY);
6324
 
 
6325
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6326
 
  {
6327
 
    TableList *curr= *tbl;
 
17299
/** Allocate memory needed for other rollup functions. */
 
17300
 
 
17301
bool JOIN::rollup_init()
 
17302
{
 
17303
  uint i,j;
 
17304
  Item **ref_array;
 
17305
 
 
17306
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
17307
  rollup.state= ROLLUP::STATE_INITED;
 
17308
 
 
17309
  /*
 
17310
    Create pointers to the different sum function groups
 
17311
    These are updated by rollup_make_fields()
 
17312
  */
 
17313
  tmp_table_param.group_parts= send_group_parts;
 
17314
 
 
17315
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
17316
                                                sizeof(Item**) +
 
17317
                                                sizeof(List<Item>) +
 
17318
                                                ref_pointer_array_size)
 
17319
                                                * send_group_parts )))
 
17320
    return 1;
 
17321
  
 
17322
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
17323
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
17324
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
17325
 
 
17326
  /*
 
17327
    Prepare space for field list for the different levels
 
17328
    These will be filled up in rollup_make_fields()
 
17329
  */
 
17330
  for (i= 0 ; i < send_group_parts ; i++)
 
17331
  {
 
17332
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
17333
    List<Item> *rollup_fields= &rollup.fields[i];
 
17334
    rollup_fields->empty();
 
17335
    rollup.ref_pointer_arrays[i]= ref_array;
 
17336
    ref_array+= all_fields.elements;
 
17337
  }
 
17338
  for (i= 0 ; i < send_group_parts; i++)
 
17339
  {
 
17340
    for (j=0 ; j < fields_list.elements ; j++)
 
17341
      rollup.fields[i].push_back(rollup.null_items[i]);
 
17342
  }
 
17343
  List_iterator<Item> it(all_fields);
 
17344
  Item *item;
 
17345
  while ((item= it++))
 
17346
  {
 
17347
    ORDER *group_tmp;
 
17348
    bool found_in_group= 0;
 
17349
 
 
17350
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
17351
    {
 
17352
      if (*group_tmp->item == item)
 
17353
      {
 
17354
        item->maybe_null= 1;
 
17355
        found_in_group= 1;
 
17356
        if (item->const_item())
 
17357
        {
 
17358
          /*
 
17359
            For ROLLUP queries each constant item referenced in GROUP BY list
 
17360
            is wrapped up into an Item_func object yielding the same value
 
17361
            as the constant item. The objects of the wrapper class are never
 
17362
            considered as constant items and besides they inherit all
 
17363
            properties of the Item_result_field class.
 
17364
            This wrapping allows us to ensure writing constant items
 
17365
            into temporary tables whenever the result of the ROLLUP
 
17366
            operation has to be written into a temporary table, e.g. when
 
17367
            ROLLUP is used together with DISTINCT in the SELECT list.
 
17368
            Usually when creating temporary tables for a intermidiate
 
17369
            result we do not include fields for constant expressions.
 
17370
          */           
 
17371
          Item* new_item= new Item_func_rollup_const(item);
 
17372
          if (!new_item)
 
17373
            return 1;
 
17374
          new_item->fix_fields(thd, (Item **) 0);
 
17375
          thd->change_item_tree(it.ref(), new_item);
 
17376
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
 
17377
          { 
 
17378
            if (*tmp->item == item)
 
17379
              thd->change_item_tree(tmp->item, new_item);
 
17380
          }
 
17381
        }
 
17382
      }
 
17383
    }
 
17384
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
17385
    {
 
17386
      bool changed= false;
 
17387
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
17388
        return 1;
 
17389
      /*
 
17390
        We have to prevent creation of a field in a temporary table for
 
17391
        an expression that contains GROUP BY attributes.
 
17392
        Marking the expression item as 'with_sum_func' will ensure this.
 
17393
      */ 
 
17394
      if (changed)
 
17395
        item->with_sum_func= 1;
 
17396
    }
 
17397
  }
 
17398
  return 0;
 
17399
}
 
17400
  
 
17401
 
 
17402
/**
 
17403
  Fill up rollup structures with pointers to fields to use.
 
17404
 
 
17405
  Creates copies of item_sum items for each sum level.
 
17406
 
 
17407
  @param fields_arg             List of all fields (hidden and real ones)
 
17408
  @param sel_fields             Pointer to selected fields
 
17409
  @param func                   Store here a pointer to all fields
 
17410
 
 
17411
  @retval
 
17412
    0   if ok;
 
17413
    In this case func is pointing to next not used element.
 
17414
  @retval
 
17415
    1    on error
 
17416
*/
 
17417
 
 
17418
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
17419
                              Item_sum ***func)
 
17420
{
 
17421
  List_iterator_fast<Item> it(fields_arg);
 
17422
  Item *first_field= sel_fields.head();
 
17423
  uint level;
 
17424
 
 
17425
  /*
 
17426
    Create field lists for the different levels
 
17427
 
 
17428
    The idea here is to have a separate field list for each rollup level to
 
17429
    avoid all runtime checks of which columns should be NULL.
 
17430
 
 
17431
    The list is stored in reverse order to get sum function in such an order
 
17432
    in func that it makes it easy to reset them with init_sum_functions()
 
17433
 
 
17434
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17435
 
 
17436
    rollup.fields[0] will contain list where a,b,c is NULL
 
17437
    rollup.fields[1] will contain list where b,c is NULL
 
17438
    ...
 
17439
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
17440
    ...
 
17441
    sum_funcs_end[0] points to all sum functions
 
17442
    sum_funcs_end[1] points to all sum functions, except grand totals
 
17443
    ...
 
17444
  */
 
17445
 
 
17446
  for (level=0 ; level < send_group_parts ; level++)
 
17447
  {
 
17448
    uint i;
 
17449
    uint pos= send_group_parts - level -1;
 
17450
    bool real_fields= 0;
 
17451
    Item *item;
 
17452
    List_iterator<Item> new_it(rollup.fields[pos]);
 
17453
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
17454
    ORDER *start_group;
 
17455
 
 
17456
    /* Point to first hidden field */
 
17457
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
17458
 
 
17459
    /* Remember where the sum functions ends for the previous level */
 
17460
    sum_funcs_end[pos+1]= *func;
 
17461
 
 
17462
    /* Find the start of the group for this level */
 
17463
    for (i= 0, start_group= group_list ;
 
17464
         i++ < pos ;
 
17465
         start_group= start_group->next)
 
17466
      ;
 
17467
 
 
17468
    it.rewind();
 
17469
    while ((item= it++))
 
17470
    {
 
17471
      if (item == first_field)
 
17472
      {
 
17473
        real_fields= 1;                         // End of hidden fields
 
17474
        ref_array= ref_array_start;
 
17475
      }
 
17476
 
 
17477
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
17478
          (!((Item_sum*) item)->depended_from() ||
 
17479
           ((Item_sum *)item)->depended_from() == select_lex))
 
17480
          
 
17481
      {
 
17482
        /*
 
17483
          This is a top level summary function that must be replaced with
 
17484
          a sum function that is reset for this level.
 
17485
 
 
17486
          NOTE: This code creates an object which is not that nice in a
 
17487
          sub select.  Fortunately it's not common to have rollup in
 
17488
          sub selects.
 
17489
        */
 
17490
        item= item->copy_or_same(thd);
 
17491
        ((Item_sum*) item)->make_unique();
 
17492
        *(*func)= (Item_sum*) item;
 
17493
        (*func)++;
 
17494
      }
 
17495
      else 
 
17496
      {
 
17497
        /* Check if this is something that is part of this group by */
 
17498
        ORDER *group_tmp;
 
17499
        for (group_tmp= start_group, i= pos ;
 
17500
             group_tmp ; group_tmp= group_tmp->next, i++)
 
17501
        {
 
17502
          if (*group_tmp->item == item)
 
17503
          {
 
17504
            /*
 
17505
              This is an element that is used by the GROUP BY and should be
 
17506
              set to NULL in this level
 
17507
            */
 
17508
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
17509
            if (!null_item)
 
17510
              return 1;
 
17511
            item->maybe_null= 1;                // Value will be null sometimes
 
17512
            null_item->result_field= item->get_tmp_table_field();
 
17513
            item= null_item;
 
17514
            break;
 
17515
          }
 
17516
        }
 
17517
      }
 
17518
      *ref_array= item;
 
17519
      if (real_fields)
 
17520
      {
 
17521
        (void) new_it++;                        // Point to next item
 
17522
        new_it.replace(item);                   // Replace previous
 
17523
        ref_array++;
 
17524
      }
 
17525
      else
 
17526
        ref_array--;
 
17527
    }
 
17528
  }
 
17529
  sum_funcs_end[0]= *func;                      // Point to last function
 
17530
  return 0;
 
17531
}
 
17532
 
 
17533
/**
 
17534
  Send all rollup levels higher than the current one to the client.
 
17535
 
 
17536
  @b SAMPLE
 
17537
    @code
 
17538
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
17539
  @endcode
 
17540
 
 
17541
  @param idx            Level we are on:
 
17542
                        - 0 = Total sum level
 
17543
                        - 1 = First group changed  (a)
 
17544
                        - 2 = Second group changed (a,b)
 
17545
 
 
17546
  @retval
 
17547
    0   ok
 
17548
  @retval
 
17549
    1   If send_data_failed()
 
17550
*/
 
17551
 
 
17552
int JOIN::rollup_send_data(uint idx)
 
17553
{
 
17554
  uint i;
 
17555
  for (i= send_group_parts ; i-- > idx ; )
 
17556
  {
 
17557
    /* Get reference pointers to sum functions in place */
 
17558
    memcpy((char*) ref_pointer_array,
 
17559
           (char*) rollup.ref_pointer_arrays[i],
 
17560
           ref_pointer_array_size);
 
17561
    if ((!having || having->val_int()))
 
17562
    {
 
17563
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
17564
          result->send_data(rollup.fields[i]))
 
17565
        return 1;
 
17566
      send_records++;
 
17567
    }
 
17568
  }
 
17569
  /* Restore ref_pointer_array */
 
17570
  set_items_ref_array(current_ref_pointer_array);
 
17571
  return 0;
 
17572
}
 
17573
 
 
17574
/**
 
17575
  Write all rollup levels higher than the current one to a temp table.
 
17576
 
 
17577
  @b SAMPLE
 
17578
    @code
 
17579
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
17580
  @endcode
 
17581
 
 
17582
  @param idx                 Level we are on:
 
17583
                               - 0 = Total sum level
 
17584
                               - 1 = First group changed  (a)
 
17585
                               - 2 = Second group changed (a,b)
 
17586
  @param table               reference to temp table
 
17587
 
 
17588
  @retval
 
17589
    0   ok
 
17590
  @retval
 
17591
    1   if write_data_failed()
 
17592
*/
 
17593
 
 
17594
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
 
17595
{
 
17596
  uint i;
 
17597
  for (i= send_group_parts ; i-- > idx ; )
 
17598
  {
 
17599
    /* Get reference pointers to sum functions in place */
 
17600
    memcpy((char*) ref_pointer_array,
 
17601
           (char*) rollup.ref_pointer_arrays[i],
 
17602
           ref_pointer_array_size);
 
17603
    if ((!having || having->val_int()))
 
17604
    {
 
17605
      int write_error;
 
17606
      Item *item;
 
17607
      List_iterator_fast<Item> it(rollup.fields[i]);
 
17608
      while ((item= it++))
 
17609
      {
 
17610
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
17611
          item->save_in_result_field(1);
 
17612
      }
 
17613
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
17614
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
17615
      {
 
17616
        if (create_myisam_from_heap(thd, table_arg, 
 
17617
                                    tmp_table_param.start_recinfo,
 
17618
                                    &tmp_table_param.recinfo,
 
17619
                                    write_error, 0))
 
17620
          return 1;                  
 
17621
      }
 
17622
    }
 
17623
  }
 
17624
  /* Restore ref_pointer_array */
 
17625
  set_items_ref_array(current_ref_pointer_array);
 
17626
  return 0;
 
17627
}
 
17628
 
 
17629
/**
 
17630
  clear results if there are not rows found for group
 
17631
  (end_send_group/end_write_group)
 
17632
*/
 
17633
 
 
17634
void JOIN::clear()
 
17635
{
 
17636
  clear_tables(this);
 
17637
  copy_fields(&tmp_table_param);
 
17638
 
 
17639
  if (sum_funcs)
 
17640
  {
 
17641
    Item_sum *func, **func_ptr= sum_funcs;
 
17642
    while ((func= *(func_ptr++)))
 
17643
      func->clear();
 
17644
  }
 
17645
}
 
17646
 
 
17647
/**
 
17648
  EXPLAIN handling.
 
17649
 
 
17650
  Send a description about what how the select will be done to stdout.
 
17651
*/
 
17652
 
 
17653
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
17654
                     bool distinct,const char *message)
 
17655
{
 
17656
  List<Item> field_list;
 
17657
  List<Item> item_list;
 
17658
  THD *thd=join->thd;
 
17659
  select_result *result=join->result;
 
17660
  Item *item_null= new Item_null();
 
17661
  CHARSET_INFO *cs= system_charset_info;
 
17662
  int quick_type;
 
17663
  /* Don't log this into the slow query log */
 
17664
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
17665
  join->unit->offset_limit_cnt= 0;
 
17666
 
 
17667
  /* 
 
17668
    NOTE: the number/types of items pushed into item_list must be in sync with
 
17669
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
17670
  */
 
17671
  if (message)
 
17672
  {
 
17673
    item_list.push_back(new Item_int((int32)
 
17674
                                     join->select_lex->select_number));
 
17675
    item_list.push_back(new Item_string(join->select_lex->type,
 
17676
                                        strlen(join->select_lex->type), cs));
 
17677
    for (uint i=0 ; i < 7; i++)
 
17678
      item_list.push_back(item_null);
 
17679
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17680
      item_list.push_back(item_null);
 
17681
  
 
17682
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
17683
    if (result->send_data(item_list))
 
17684
      join->error= 1;
 
17685
  }
 
17686
  else if (join->select_lex == join->unit->fake_select_lex)
 
17687
  {
 
17688
    /* 
 
17689
      here we assume that the query will return at least two rows, so we
 
17690
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
17691
      and no filesort will be actually done, but executing all selects in
 
17692
      the UNION to provide precise EXPLAIN information will hardly be
 
17693
      appreciated :)
 
17694
    */
 
17695
    char table_name_buffer[NAME_LEN];
 
17696
    item_list.empty();
 
17697
    /* id */
 
17698
    item_list.push_back(new Item_null);
 
17699
    /* select_type */
 
17700
    item_list.push_back(new Item_string(join->select_lex->type,
 
17701
                                        strlen(join->select_lex->type),
 
17702
                                        cs));
 
17703
    /* table */
 
17704
    {
 
17705
      SELECT_LEX *sl= join->unit->first_select();
 
17706
      uint len= 6, lastop= 0;
 
17707
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
17708
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
17709
      {
 
17710
        len+= lastop;
 
17711
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
17712
                         "%u,", sl->select_number);
 
17713
      }
 
17714
      if (sl || len + lastop >= NAME_LEN)
 
17715
      {
 
17716
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
17717
        len+= 4;
 
17718
      }
 
17719
      else
 
17720
      {
 
17721
        len+= lastop;
 
17722
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
17723
      }
 
17724
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17725
    }
 
17726
    /* type */
 
17727
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
17728
                                          strlen(join_type_str[JT_ALL]),
 
17729
                                          cs));
 
17730
    /* possible_keys */
 
17731
    item_list.push_back(item_null);
 
17732
    /* key*/
 
17733
    item_list.push_back(item_null);
 
17734
    /* key_len */
 
17735
    item_list.push_back(item_null);
 
17736
    /* ref */
 
17737
    item_list.push_back(item_null);
 
17738
    /* in_rows */
 
17739
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17740
      item_list.push_back(item_null);
 
17741
    /* rows */
 
17742
    item_list.push_back(item_null);
 
17743
    /* extra */
 
17744
    if (join->unit->global_parameters->order_list.first)
 
17745
      item_list.push_back(new Item_string("Using filesort",
 
17746
                                          14, cs));
 
17747
    else
 
17748
      item_list.push_back(new Item_string("", 0, cs));
 
17749
 
 
17750
    if (result->send_data(item_list))
 
17751
      join->error= 1;
 
17752
  }
 
17753
  else
 
17754
  {
 
17755
    table_map used_tables=0;
 
17756
    for (uint i=0 ; i < join->tables ; i++)
 
17757
    {
 
17758
      JOIN_TAB *tab=join->join_tab+i;
 
17759
      TABLE *table=tab->table;
 
17760
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
 
17761
      char buff[512]; 
 
17762
      char buff1[512], buff2[512], buff3[512];
 
17763
      char keylen_str_buf[64];
 
17764
      String extra(buff, sizeof(buff),cs);
 
17765
      char table_name_buffer[NAME_LEN];
 
17766
      String tmp1(buff1,sizeof(buff1),cs);
 
17767
      String tmp2(buff2,sizeof(buff2),cs);
 
17768
      String tmp3(buff3,sizeof(buff3),cs);
 
17769
      extra.length(0);
 
17770
      tmp1.length(0);
 
17771
      tmp2.length(0);
 
17772
      tmp3.length(0);
 
17773
 
 
17774
      quick_type= -1;
 
17775
      item_list.empty();
 
17776
      /* id */
 
17777
      item_list.push_back(new Item_uint((uint32)
 
17778
                                       join->select_lex->select_number));
 
17779
      /* select_type */
 
17780
      item_list.push_back(new Item_string(join->select_lex->type,
 
17781
                                          strlen(join->select_lex->type),
 
17782
                                          cs));
 
17783
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
17784
      {
 
17785
        quick_type= tab->select->quick->get_type();
 
17786
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
17787
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
17788
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
17789
          tab->type = JT_INDEX_MERGE;
 
17790
        else
 
17791
          tab->type = JT_RANGE;
 
17792
      }
 
17793
      /* table */
 
17794
      if (table->derived_select_number)
 
17795
      {
 
17796
        /* Derived table name generation */
 
17797
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
17798
                          "<derived%u>",
 
17799
                          table->derived_select_number);
 
17800
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
17801
      }
 
17802
      else
 
17803
      {
 
17804
        TABLE_LIST *real_table= table->pos_in_table_list; 
 
17805
        item_list.push_back(new Item_string(real_table->alias,
 
17806
                                            strlen(real_table->alias),
 
17807
                                            cs));
 
17808
      }
 
17809
      /* "type" column */
 
17810
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
17811
                                          strlen(join_type_str[tab->type]),
 
17812
                                          cs));
 
17813
      /* Build "possible_keys" value and add it to item_list */
 
17814
      if (!tab->keys.is_clear_all())
 
17815
      {
 
17816
        uint j;
 
17817
        for (j=0 ; j < table->s->keys ; j++)
 
17818
        {
 
17819
          if (tab->keys.is_set(j))
 
17820
          {
 
17821
            if (tmp1.length())
 
17822
              tmp1.append(',');
 
17823
            tmp1.append(table->key_info[j].name, 
 
17824
                        strlen(table->key_info[j].name),
 
17825
                        system_charset_info);
 
17826
          }
 
17827
        }
 
17828
      }
 
17829
      if (tmp1.length())
 
17830
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
17831
      else
 
17832
        item_list.push_back(item_null);
 
17833
 
 
17834
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
17835
      if (tab->ref.key_parts)
 
17836
      {
 
17837
        KEY *key_info=table->key_info+ tab->ref.key;
 
17838
        register uint length;
 
17839
        item_list.push_back(new Item_string(key_info->name,
 
17840
                                            strlen(key_info->name),
 
17841
                                            system_charset_info));
 
17842
        length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
17843
                keylen_str_buf;
 
17844
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
17845
                                            system_charset_info));
 
17846
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
17847
        {
 
17848
          if (tmp2.length())
 
17849
            tmp2.append(',');
 
17850
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
17851
                      system_charset_info);
 
17852
        }
 
17853
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17854
      }
 
17855
      else if (tab->type == JT_NEXT)
 
17856
      {
 
17857
        KEY *key_info=table->key_info+ tab->index;
 
17858
        register uint length;
 
17859
        item_list.push_back(new Item_string(key_info->name,
 
17860
                                            strlen(key_info->name),cs));
 
17861
        length= longlong2str(key_info->key_length, keylen_str_buf, 10) - 
 
17862
                keylen_str_buf;
 
17863
        item_list.push_back(new Item_string(keylen_str_buf, 
 
17864
                                            length,
 
17865
                                            system_charset_info));
 
17866
        item_list.push_back(item_null);
 
17867
      }
 
17868
      else if (tab->select && tab->select->quick)
 
17869
      {
 
17870
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
17871
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17872
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
17873
        item_list.push_back(item_null);
 
17874
      }
 
17875
      else
 
17876
      {
 
17877
        if (table_list->schema_table &&
 
17878
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
17879
        {
 
17880
          const char *tmp_buff;
 
17881
          int f_idx;
 
17882
          if (table_list->has_db_lookup_value)
 
17883
          {
 
17884
            f_idx= table_list->schema_table->idx_field1;
 
17885
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17886
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17887
          }          
 
17888
          if (table_list->has_table_lookup_value)
 
17889
          {
 
17890
            if (table_list->has_db_lookup_value)
 
17891
              tmp2.append(',');
 
17892
            f_idx= table_list->schema_table->idx_field2;
 
17893
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
17894
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
17895
          }
 
17896
          if (tmp2.length())
 
17897
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
17898
          else
 
17899
            item_list.push_back(item_null);
 
17900
        }
 
17901
        else
 
17902
          item_list.push_back(item_null);
 
17903
        item_list.push_back(item_null);
 
17904
        item_list.push_back(item_null);
 
17905
      }
 
17906
      
 
17907
      /* Add "rows" field to item_list. */
 
17908
      if (table_list->schema_table)
 
17909
      {
 
17910
        /* in_rows */
 
17911
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17912
          item_list.push_back(item_null);
 
17913
        /* rows */
 
17914
        item_list.push_back(item_null);
 
17915
      }
 
17916
      else
 
17917
      {
 
17918
        double examined_rows;
 
17919
        if (tab->select && tab->select->quick)
 
17920
          examined_rows= rows2double(tab->select->quick->records);
 
17921
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
17922
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
17923
                                     tab->table->file->records());
 
17924
        else
 
17925
          examined_rows= join->best_positions[i].records_read; 
 
17926
 
 
17927
        item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows, 
 
17928
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
17929
 
 
17930
        /* Add "filtered" field to item_list. */
 
17931
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
17932
        {
 
17933
          float f= 0.0; 
 
17934
          if (examined_rows)
 
17935
            f= (float) (100.0 * join->best_positions[i].records_read /
 
17936
                        examined_rows);
 
17937
          item_list.push_back(new Item_float(f, 2));
 
17938
        }
 
17939
      }
 
17940
 
 
17941
      /* Build "Extra" field and add it to item_list. */
 
17942
      my_bool key_read=table->key_read;
 
17943
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
17944
          table->covering_keys.is_set(tab->index))
 
17945
        key_read=1;
 
17946
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
17947
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
17948
        key_read=1;
 
17949
        
 
17950
      if (tab->info)
 
17951
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
17952
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
17953
      {
 
17954
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
17955
          extra.append(STRING_WITH_LEN("; Using index"));
 
17956
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
17957
          extra.append(STRING_WITH_LEN("; Using where"));
 
17958
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
17959
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
17960
        /* Skip initial "; "*/
 
17961
        const char *str= extra.ptr();
 
17962
        uint32 len= extra.length();
 
17963
        if (len)
 
17964
        {
 
17965
          str += 2;
 
17966
          len -= 2;
 
17967
        }
 
17968
        item_list.push_back(new Item_string(str, len, cs));
 
17969
      }
 
17970
      else
 
17971
      {
 
17972
        uint keyno= MAX_KEY;
 
17973
        if (tab->ref.key_parts)
 
17974
          keyno= tab->ref.key;
 
17975
        else if (tab->select && tab->select->quick)
 
17976
          keyno = tab->select->quick->index;
 
17977
 
 
17978
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
17979
            table->file->pushed_idx_cond)
 
17980
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
17981
 
 
17982
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
17983
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
17984
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
17985
        {
 
17986
          extra.append(STRING_WITH_LEN("; Using "));
 
17987
          tab->select->quick->add_info_string(&extra);
 
17988
        }
 
17989
          if (tab->select)
 
17990
        {
 
17991
          if (tab->use_quick == 2)
 
17992
          {
 
17993
            /* 4 bits per 1 hex digit + terminating '\0' */
 
17994
            char buf[MAX_KEY / 4 + 1];
 
17995
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
17996
                                         "record (index map: 0x"));
 
17997
            extra.append(tab->keys.print(buf));
 
17998
            extra.append(')');
 
17999
          }
 
18000
          else if (tab->select->cond)
 
18001
          {
 
18002
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
18003
 
 
18004
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
18005
            {
 
18006
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
18007
                                           "condition"));
 
18008
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
18009
              {
 
18010
                extra.append(STRING_WITH_LEN(": "));
 
18011
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
18012
              }
 
18013
            }
 
18014
            else
 
18015
              extra.append(STRING_WITH_LEN("; Using where"));
 
18016
          }
 
18017
        }
 
18018
        if (key_read)
 
18019
        {
 
18020
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
18021
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
18022
          else
 
18023
            extra.append(STRING_WITH_LEN("; Using index"));
 
18024
        }
 
18025
        if (table->reginfo.not_exists_optimize)
 
18026
          extra.append(STRING_WITH_LEN("; Not exists"));
 
18027
          
 
18028
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
18029
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
18030
             HA_MRR_USE_DEFAULT_IMPL))
 
18031
        {
 
18032
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
18033
        }
 
18034
 
 
18035
        if (table_list->schema_table &&
 
18036
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
18037
        {
 
18038
          if (!table_list->table_open_method)
 
18039
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
18040
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
18041
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
18042
          else
 
18043
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
18044
          if (table_list->has_db_lookup_value &&
 
18045
              table_list->has_table_lookup_value)
 
18046
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
18047
          else if (table_list->has_db_lookup_value ||
 
18048
                   table_list->has_table_lookup_value)
 
18049
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
18050
          else
 
18051
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
18052
        }
 
18053
        if (need_tmp_table)
 
18054
        {
 
18055
          need_tmp_table=0;
 
18056
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
18057
        }
 
18058
        if (need_order)
 
18059
        {
 
18060
          need_order=0;
 
18061
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
18062
        }
 
18063
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
18064
          extra.append(STRING_WITH_LEN("; Distinct"));
 
18065
 
 
18066
        if (tab->insideout_match_tab)
 
18067
        {
 
18068
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
18069
        }
 
18070
 
 
18071
        if (tab->flush_weedout_table)
 
18072
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
18073
        else if (tab->check_weed_out_table)
 
18074
          extra.append(STRING_WITH_LEN("; End temporary"));
 
18075
        else if (tab->do_firstmatch)
 
18076
        {
 
18077
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
18078
          TABLE *prev_table=tab->do_firstmatch->table;
 
18079
          if (prev_table->derived_select_number)
 
18080
          {
 
18081
            char namebuf[NAME_LEN];
 
18082
            /* Derived table name generation */
 
18083
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
18084
                              "<derived%u>",
 
18085
                              prev_table->derived_select_number);
 
18086
            extra.append(namebuf, len);
 
18087
          }
 
18088
          else
 
18089
            extra.append(prev_table->pos_in_table_list->alias);
 
18090
          extra.append(STRING_WITH_LEN(")"));
 
18091
        }
 
18092
 
 
18093
        for (uint part= 0; part < tab->ref.key_parts; part++)
 
18094
        {
 
18095
          if (tab->ref.cond_guards[part])
 
18096
          {
 
18097
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
18098
            break;
 
18099
          }
 
18100
        }
 
18101
 
 
18102
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
18103
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
18104
 
 
18105
        /* Skip initial "; "*/
 
18106
        const char *str= extra.ptr();
 
18107
        uint32 len= extra.length();
 
18108
        if (len)
 
18109
        {
 
18110
          str += 2;
 
18111
          len -= 2;
 
18112
        }
 
18113
        item_list.push_back(new Item_string(str, len, cs));
 
18114
      }
 
18115
      // For next iteration
 
18116
      used_tables|=table->map;
 
18117
      if (result->send_data(item_list))
 
18118
        join->error= 1;
 
18119
    }
 
18120
  }
 
18121
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
18122
       unit;
 
18123
       unit= unit->next_unit())
 
18124
  {
 
18125
    if (mysql_explain_union(thd, unit, result))
 
18126
      return;
 
18127
  }
 
18128
  return;
 
18129
}
 
18130
 
 
18131
 
 
18132
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
18133
{
 
18134
  bool res= 0;
 
18135
  SELECT_LEX *first= unit->first_select();
 
18136
 
 
18137
  for (SELECT_LEX *sl= first;
 
18138
       sl;
 
18139
       sl= sl->next_select())
 
18140
  {
 
18141
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
18142
    uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
18143
    sl->type= (((&thd->lex->select_lex)==sl)?
 
18144
               (sl->first_inner_unit() || sl->next_select() ? 
 
18145
                "PRIMARY" : "SIMPLE"):
 
18146
               ((sl == first)?
 
18147
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
18148
                 "DERIVED":
 
18149
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18150
                  "DEPENDENT SUBQUERY":
 
18151
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
18152
                   "SUBQUERY"))):
 
18153
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
18154
                 "DEPENDENT UNION":
 
18155
                 uncacheable?"UNCACHEABLE UNION":
 
18156
                 "UNION")));
 
18157
    sl->options|= SELECT_DESCRIBE;
 
18158
  }
 
18159
  if (unit->is_union())
 
18160
  {
 
18161
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
18162
    unit->fake_select_lex->type= "UNION RESULT";
 
18163
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
18164
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
18165
      res= unit->exec();
 
18166
    res|= unit->cleanup();
 
18167
  }
 
18168
  else
 
18169
  {
 
18170
    thd->lex->current_select= first;
 
18171
    unit->set_limit(unit->global_parameters);
 
18172
    res= mysql_select(thd, &first->ref_pointer_array,
 
18173
                        (TABLE_LIST*) first->table_list.first,
 
18174
                        first->with_wild, first->item_list,
 
18175
                        first->where,
 
18176
                        first->order_list.elements +
 
18177
                        first->group_list.elements,
 
18178
                        (ORDER*) first->order_list.first,
 
18179
                        (ORDER*) first->group_list.first,
 
18180
                        first->having,
 
18181
                        (ORDER*) thd->lex->proc_list.first,
 
18182
                        first->options | thd->options | SELECT_DESCRIBE,
 
18183
                        result, unit, first);
 
18184
  }
 
18185
  return(res || thd->is_error());
 
18186
}
 
18187
 
 
18188
 
 
18189
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
18190
                              TABLE_LIST **end)
 
18191
{
 
18192
  (*table)->print(thd, str, QT_ORDINARY);
 
18193
 
 
18194
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
 
18195
  {
 
18196
    TABLE_LIST *curr= *tbl;
6328
18197
    if (curr->outer_join)
6329
18198
    {
6330
18199
      /* MySQL converts right to left joins */
6332
18201
    }
6333
18202
    else if (curr->straight)
6334
18203
      str->append(STRING_WITH_LEN(" straight_join "));
 
18204
    else if (curr->sj_inner_tables)
 
18205
      str->append(STRING_WITH_LEN(" semi join "));
6335
18206
    else
6336
18207
      str->append(STRING_WITH_LEN(" join "));
6337
 
    curr->print(session, str, QT_ORDINARY);
 
18208
    curr->print(thd, str, QT_ORDINARY);
6338
18209
    if (curr->on_expr)
6339
18210
    {
6340
18211
      str->append(STRING_WITH_LEN(" on("));
6344
18215
  }
6345
18216
}
6346
18217
 
 
18218
 
6347
18219
/**
6348
18220
  Print joins from the FROM clause.
6349
 
  @param session     thread Cursor
 
18221
  @param thd     thread handler
6350
18222
  @param str     string where table should be printed
6351
18223
  @param tables  list of tables in join
6352
18224
  @query_type    type of the query is being generated
6353
18225
*/
6354
 
void print_join(Session *session, String *str,
6355
 
                List<TableList> *tables, enum_query_type)
 
18226
 
 
18227
static void print_join(THD *thd,
 
18228
                       String *str,
 
18229
                       List<TABLE_LIST> *tables,
 
18230
                       enum_query_type query_type __attribute__((__unused__)))
6356
18231
{
6357
18232
  /* List is reversed => we should reverse it before using */
6358
 
  List_iterator_fast<TableList> ti(*tables);
6359
 
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
 
18233
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
18234
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
6360
18235
                                                tables->elements);
6361
18236
  if (table == 0)
6362
18237
    return;  // out of memory
6363
18238
 
6364
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
18239
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
6365
18240
    *t= ti++;
 
18241
  
 
18242
  /* 
 
18243
    If the first table is a semi-join nest, swap it with something that is
 
18244
    not a semi-join nest.
 
18245
  */
 
18246
  if ((*table)->sj_inner_tables)
 
18247
  {
 
18248
    TABLE_LIST **end= table + tables->elements;
 
18249
    for (TABLE_LIST **t2= table; t2!=end; t2++)
 
18250
    {
 
18251
      if (!(*t2)->sj_inner_tables)
 
18252
      {
 
18253
        TABLE_LIST *tmp= *t2;
 
18254
        *t2= *table;
 
18255
        *table= tmp;
 
18256
        break;
 
18257
      }
 
18258
    }
 
18259
  }
6366
18260
  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
 
 
 
18261
  print_table_array(thd, str, table, table + tables->elements);
 
18262
}
 
18263
 
 
18264
 
 
18265
/**
 
18266
  @brief Print an index hint
 
18267
 
 
18268
  @details Prints out the USE|FORCE|IGNORE index hint.
 
18269
 
 
18270
  @param      thd         the current thread
 
18271
  @param[out] str         appends the index hint here
 
18272
  @param      hint        what the hint is (as string : "USE INDEX"|
 
18273
                          "FORCE INDEX"|"IGNORE INDEX")
 
18274
  @param      hint_length the length of the string in 'hint'
 
18275
  @param      indexes     a list of index names for the hint
 
18276
*/
 
18277
 
 
18278
void 
 
18279
Index_hint::print(THD *thd, String *str)
 
18280
{
 
18281
  switch (type)
 
18282
  {
 
18283
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
18284
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
18285
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
18286
  }
 
18287
  str->append (STRING_WITH_LEN(" ("));
 
18288
  if (key_name.length)
 
18289
  {
 
18290
    if (thd && !my_strnncoll(system_charset_info,
 
18291
                             (const uchar *)key_name.str, key_name.length, 
 
18292
                             (const uchar *)primary_key_name, 
 
18293
                             strlen(primary_key_name)))
 
18294
      str->append(primary_key_name);
 
18295
    else
 
18296
      append_identifier(thd, str, key_name.str, key_name.length);
 
18297
  }
 
18298
  str->append(')');
 
18299
}
 
18300
 
 
18301
 
 
18302
/**
 
18303
  Print table as it should be in join list.
 
18304
 
 
18305
  @param str   string where table should be printed
 
18306
*/
 
18307
 
 
18308
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
 
18309
{
 
18310
  if (nested_join)
 
18311
  {
 
18312
    str->append('(');
 
18313
    print_join(thd, str, &nested_join->join_list, query_type);
 
18314
    str->append(')');
 
18315
  }
 
18316
  else
 
18317
  {
 
18318
    const char *cmp_name;                         // Name to compare with alias
 
18319
    if (derived)
 
18320
    {
 
18321
      // A derived table
 
18322
      str->append('(');
 
18323
      derived->print(str, query_type);
 
18324
      str->append(')');
 
18325
      cmp_name= "";                               // Force printing of alias
 
18326
    }
 
18327
    else
 
18328
    {
 
18329
      // A normal table
 
18330
      {
 
18331
        append_identifier(thd, str, db, db_length);
 
18332
        str->append('.');
 
18333
      }
 
18334
      if (schema_table)
 
18335
      {
 
18336
        append_identifier(thd, str, schema_table_name,
 
18337
                          strlen(schema_table_name));
 
18338
        cmp_name= schema_table_name;
 
18339
      }
 
18340
      else
 
18341
      {
 
18342
        append_identifier(thd, str, table_name, table_name_length);
 
18343
        cmp_name= table_name;
 
18344
      }
 
18345
    }
 
18346
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
18347
    {
 
18348
      char t_alias_buff[MAX_ALIAS_NAME];
 
18349
      const char *t_alias= alias;
 
18350
 
 
18351
      str->append(' ');
 
18352
      if (lower_case_table_names== 1)
 
18353
      {
 
18354
        if (alias && alias[0])
 
18355
        {
 
18356
          strmov(t_alias_buff, alias);
 
18357
          my_casedn_str(files_charset_info, t_alias_buff);
 
18358
          t_alias= t_alias_buff;
 
18359
        }
 
18360
      }
 
18361
 
 
18362
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
18363
    }
 
18364
 
 
18365
    if (index_hints)
 
18366
    {
 
18367
      List_iterator<Index_hint> it(*index_hints);
 
18368
      Index_hint *hint;
 
18369
 
 
18370
      while ((hint= it++))
 
18371
      {
 
18372
        str->append (STRING_WITH_LEN(" "));
 
18373
        hint->print (thd, str);
 
18374
      }
 
18375
    }
 
18376
  }
 
18377
}
 
18378
 
 
18379
 
 
18380
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
18381
{
 
18382
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
18383
  if (!thd)
 
18384
    thd= current_thd;
6376
18385
 
6377
18386
  str->append(STRING_WITH_LEN("select "));
6378
18387
 
6379
18388
  /* First add options */
6380
18389
  if (options & SELECT_STRAIGHT_JOIN)
6381
18390
    str->append(STRING_WITH_LEN("straight_join "));
 
18391
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
18392
      (this == &thd->lex->select_lex))
 
18393
    str->append(STRING_WITH_LEN("high_priority "));
6382
18394
  if (options & SELECT_DISTINCT)
6383
18395
    str->append(STRING_WITH_LEN("distinct "));
6384
18396
  if (options & SELECT_SMALL_RESULT)
6411
18423
  {
6412
18424
    str->append(STRING_WITH_LEN(" from "));
6413
18425
    /* go through join tree */
6414
 
    print_join(session, str, &top_join_list, query_type);
 
18426
    print_join(thd, str, &top_join_list, query_type);
6415
18427
  }
6416
18428
  else if (where)
6417
18429
  {
6418
18430
    /*
6419
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
18431
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6420
18432
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6421
18433
    */
6422
18434
    str->append(STRING_WITH_LEN(" from DUAL "));
6439
18451
  if (group_list.elements)
6440
18452
  {
6441
18453
    str->append(STRING_WITH_LEN(" group by "));
6442
 
    print_order(str, (Order *) group_list.first, query_type);
 
18454
    print_order(str, (ORDER *) group_list.first, query_type);
6443
18455
    switch (olap)
6444
18456
    {
6445
18457
      case CUBE_TYPE:
6470
18482
  if (order_list.elements)
6471
18483
  {
6472
18484
    str->append(STRING_WITH_LEN(" order by "));
6473
 
    print_order(str, (Order *) order_list.first, query_type);
 
18485
    print_order(str, (ORDER *) order_list.first, query_type);
6474
18486
  }
6475
18487
 
6476
18488
  // limit
6477
 
  print_limit(session, str, query_type);
 
18489
  print_limit(thd, str, query_type);
6478
18490
 
6479
18491
  // PROCEDURE unsupported here
6480
18492
}
6481
18493
 
 
18494
 
 
18495
/**
 
18496
  change select_result object of JOIN.
 
18497
 
 
18498
  @param res            new select_result object
 
18499
 
 
18500
  @retval
 
18501
    false   OK
 
18502
  @retval
 
18503
    true    error
 
18504
*/
 
18505
 
 
18506
bool JOIN::change_result(select_result *res)
 
18507
{
 
18508
  result= res;
 
18509
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
18510
                     result->prepare2())
 
18511
  {
 
18512
    return(true);
 
18513
  }
 
18514
  return(false);
 
18515
}
 
18516
 
6482
18517
/**
6483
18518
  @} (end of group Query_Optimizer)
6484
18519
*/
6485
 
 
6486
 
} /* namespace drizzled */