~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Mark Atwood
  • Date: 2008-10-16 11:33:16 UTC
  • mto: (520.1.13 drizzle)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: mark@fallenpegasus.com-20081016113316-ff6jdt31ck90sjdh
an implemention of the errmsg plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/**
17
17
  @file
19
19
  @brief
20
20
  mysql_select and join optimization
21
21
 
 
22
 
22
23
  @defgroup Query_Optimizer  Query Optimizer
23
24
  @{
24
25
*/
25
 
#include "config.h"
26
 
 
27
 
#include <string>
28
 
#include <iostream>
29
 
#include <algorithm>
30
 
#include <vector>
31
 
 
32
 
#include "drizzled/sql_select.h" /* include join.h */
33
 
 
34
 
#include "drizzled/error.h"
35
 
#include "drizzled/gettext.h"
36
 
#include "drizzled/util/test.h"
37
 
#include "drizzled/name_resolution_context_state.h"
38
 
#include "drizzled/nested_join.h"
39
 
#include "drizzled/probes.h"
40
 
#include "drizzled/show.h"
41
 
#include "drizzled/item/cache.h"
42
 
#include "drizzled/item/cmpfunc.h"
43
 
#include "drizzled/item/copy_string.h"
44
 
#include "drizzled/item/uint.h"
45
 
#include "drizzled/cached_item.h"
46
 
#include "drizzled/sql_base.h"
47
 
#include "drizzled/field/blob.h"
48
 
#include "drizzled/check_stack_overrun.h"
49
 
#include "drizzled/lock.h"
50
 
#include "drizzled/item/outer_ref.h"
51
 
#include "drizzled/index_hint.h"
52
 
#include "drizzled/records.h"
53
 
#include "drizzled/internal/iocache.h"
54
 
#include "drizzled/drizzled.h"
55
 
 
56
 
#include "drizzled/sql_union.h"
57
 
#include "drizzled/optimizer/key_field.h"
58
 
#include "drizzled/optimizer/position.h"
59
 
#include "drizzled/optimizer/sargable_param.h"
60
 
#include "drizzled/optimizer/key_use.h"
61
 
#include "drizzled/optimizer/range.h"
62
 
#include "drizzled/optimizer/quick_range_select.h"
63
 
#include "drizzled/optimizer/quick_ror_intersect_select.h"
64
 
 
65
 
#include "drizzled/filesort.h"
66
 
 
67
 
using namespace std;
68
 
 
69
 
namespace drizzled
70
 
{
71
 
 
72
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
73
 
static COND *build_equal_items(Session *session, COND *cond,
 
26
#include <drizzled/server_includes.h>
 
27
#include <drizzled/sql_select.h>
 
28
#include "sj_tmp_table.h"
 
29
 
 
30
#include <mysys/my_bit.h>
 
31
#include <drizzled/drizzled_error_messages.h>
 
32
#include <libdrizzle/gettext.h>
 
33
#include <drizzled/util/test.h>
 
34
 
 
35
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
36
                              "MAYBE_REF","ALL","range","index",
 
37
                              "ref_or_null","unique_subquery","index_subquery",
 
38
                              "index_merge"
 
39
};
 
40
 
 
41
struct st_sargable_param;
 
42
 
 
43
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
44
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
 
45
                                 DYNAMIC_ARRAY *keyuse);
 
46
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
47
                                JOIN_TAB *join_tab,
 
48
                                uint32_t tables, COND *conds,
 
49
                                COND_EQUAL *cond_equal,
 
50
                                table_map table_map, SELECT_LEX *select_lex,
 
51
                                st_sargable_param **sargables);
 
52
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
53
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
54
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
55
                               table_map used_tables);
 
56
static bool choose_plan(JOIN *join,table_map join_tables);
 
57
 
 
58
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
59
                             table_map remaining_tables, uint32_t idx,
 
60
                             double record_count, double read_time);
 
61
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
62
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
63
                             uint32_t depth, uint32_t prune_level);
 
64
static bool best_extension_by_limited_search(JOIN *join,
 
65
                                             table_map remaining_tables,
 
66
                                             uint32_t idx, double record_count,
 
67
                                             double read_time, uint32_t depth,
 
68
                                             uint32_t prune_level);
 
69
static uint32_t determine_search_depth(JOIN* join);
 
70
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
71
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
72
/*
 
73
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
74
  tested and approved.
 
75
*/
 
76
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
 
77
                      double record_count,double read_time);
 
78
static uint32_t cache_record_length(JOIN *join,uint32_t index);
 
79
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
80
static bool get_best_combination(JOIN *join);
 
81
static store_key *get_store_key(THD *thd,
 
82
                                KEYUSE *keyuse, table_map used_tables,
 
83
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
 
84
                                uint32_t maybe_null);
 
85
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
86
static void make_outerjoin_info(JOIN *join);
 
87
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
88
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
89
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
 
90
static void update_depend_map(JOIN *join);
 
91
static void update_depend_map(JOIN *join, order_st *order);
 
92
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
 
93
                           bool change_list, bool *simple_order);
 
94
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
 
95
                            List<Item> &fields, bool send_row,
 
96
                            uint64_t select_options, const char *info,
 
97
                            Item *having);
 
98
static COND *build_equal_items(THD *thd, COND *cond,
74
99
                               COND_EQUAL *inherited,
75
100
                               List<TableList> *join_list,
76
101
                               COND_EQUAL **cond_equal_ref);
77
 
 
 
102
static COND* substitute_for_best_equal_field(COND *cond,
 
103
                                             COND_EQUAL *cond_equal,
 
104
                                             void *table_join_idx);
 
105
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
 
106
                            COND *conds, bool top, bool in_sj);
 
107
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
108
static void restore_prev_nj_state(JOIN_TAB *last);
 
109
static void reset_nj_counters(List<TableList> *join_list);
 
110
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
 
111
                                          uint32_t first_unused);
 
112
 
 
113
static 
 
114
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
115
static void restore_prev_sj_state(const table_map remaining_tables, 
 
116
                                  const JOIN_TAB *tab);
 
117
 
 
118
static COND *optimize_cond(JOIN *join, COND *conds,
 
119
                           List<TableList> *join_list,
 
120
                           Item::cond_result *cond_value);
 
121
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
122
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
 
123
 
 
124
static enum_nested_loop_state
 
125
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
126
                     int error);
 
127
static enum_nested_loop_state
 
128
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
129
static enum_nested_loop_state
 
130
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
131
static enum_nested_loop_state
 
132
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
133
static enum_nested_loop_state
 
134
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
135
static enum_nested_loop_state
 
136
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
137
static enum_nested_loop_state
 
138
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
139
 
 
140
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
141
static int join_read_system(JOIN_TAB *tab);
 
142
static int join_read_const(JOIN_TAB *tab);
 
143
static int join_read_key(JOIN_TAB *tab);
 
144
static int join_read_always_key(JOIN_TAB *tab);
 
145
static int join_read_last_key(JOIN_TAB *tab);
 
146
static int join_no_more_records(READ_RECORD *info);
 
147
static int join_read_next(READ_RECORD *info);
 
148
static int join_read_next_different(READ_RECORD *info);
 
149
static int join_init_quick_read_record(JOIN_TAB *tab);
 
150
static int test_if_quick_select(JOIN_TAB *tab);
 
151
static int join_init_read_record(JOIN_TAB *tab);
 
152
static int join_read_first(JOIN_TAB *tab);
 
153
static int join_read_next_same(READ_RECORD *info);
 
154
static int join_read_next_same_diff(READ_RECORD *info);
 
155
static int join_read_last(JOIN_TAB *tab);
 
156
static int join_read_prev_same(READ_RECORD *info);
 
157
static int join_read_prev(READ_RECORD *info);
 
158
int join_read_always_key_or_null(JOIN_TAB *tab);
 
159
int join_read_next_same_or_null(READ_RECORD *info);
 
160
static COND *make_cond_for_table(COND *cond,table_map table,
 
161
                                 table_map used_table,
 
162
                                 bool exclude_expensive_cond);
78
163
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);
 
164
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
 
165
                                    ha_rows select_limit, bool no_changes,
 
166
                                    const key_map *map);
 
167
static bool list_contains_unique_index(Table *table,
 
168
                          bool (*find_func) (Field *, void *), void *data);
 
169
static bool find_field_in_item_list (Field *field, void *data);
 
170
static bool find_field_in_order_list (Field *field, void *data);
 
171
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
 
172
                             ha_rows filesort_limit, ha_rows select_limit,
 
173
                             bool is_order_by);
 
174
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
 
175
                             Item *having);
 
176
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
 
177
                                   ulong offset,Item *having);
 
178
static int remove_dup_with_hash_index(THD *thd,Table *table,
 
179
                                      uint32_t field_count, Field **first_field,
87
180
 
88
 
static bool eval_const_cond(COND *cond)
89
 
{
90
 
    return ((Item_func*) cond)->val_int() ? true : false;
91
 
}
 
181
                                      ulong key_length,Item *having);
 
182
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
 
183
static ulong used_blob_length(CACHE_FIELD **ptr);
 
184
static bool store_record_in_cache(JOIN_CACHE *cache);
 
185
static void reset_cache_read(JOIN_CACHE *cache);
 
186
static void reset_cache_write(JOIN_CACHE *cache);
 
187
static void read_cached_record(JOIN_TAB *tab);
 
188
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
189
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
190
                                    order_st *order, List<Item> &fields,
 
191
                                    List<Item> &all_fields,
 
192
                                    bool *all_order_by_fields_used);
 
193
static bool test_if_subpart(order_st *a,order_st *b);
 
194
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
195
static void calc_group_buffer(JOIN *join,order_st *group);
 
196
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
197
static bool alloc_group_fields(JOIN *join,order_st *group);
 
198
// Create list for using with tempory table
 
199
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
200
                                     List<Item> &new_list1,
 
201
                                     List<Item> &new_list2,
 
202
                                     uint32_t elements, List<Item> &items);
 
203
// Create list for using with tempory table
 
204
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
205
                                      List<Item> &new_list1,
 
206
                                      List<Item> &new_list2,
 
207
                                      uint32_t elements, List<Item> &items);
 
208
static void init_tmptable_sum_functions(Item_sum **func);
 
209
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
 
210
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
211
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
212
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
213
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
214
static bool update_sum_func(Item_sum **func);
 
215
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
216
                            bool distinct, const char *message=NULL);
 
217
static Item *remove_additional_cond(Item* conds);
 
218
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
219
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
220
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
221
                                       Item *new_cond, bool fix_fields);
92
222
 
93
223
/*
94
224
  This is used to mark equalities that were made from i-th IN-equality.
98
228
const char *subq_sj_cond_name=
99
229
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
100
230
 
101
 
static bool copy_blobs(Field **ptr)
 
231
static bool bitmap_covers(const table_map x, const table_map y)
102
232
{
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;
 
233
  return !test(y & ~x);
110
234
}
111
235
 
112
236
/**
113
237
  This handles SELECT with and without UNION.
114
238
*/
115
 
bool handle_select(Session *session, LEX *lex, select_result *result,
116
 
                   uint64_t setup_tables_done_option)
 
239
 
 
240
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
241
                   ulong setup_tables_done_option)
117
242
{
118
243
  bool res;
119
 
  register Select_Lex *select_lex= &lex->select_lex;
120
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
244
  register SELECT_LEX *select_lex = &lex->select_lex;
 
245
  DRIZZLE_SELECT_START();
121
246
 
122
 
  if (select_lex->master_unit()->is_union() ||
 
247
  if (select_lex->master_unit()->is_union() || 
123
248
      select_lex->master_unit()->fake_select_lex)
124
 
  {
125
 
    res= drizzle_union(session, lex, result, &lex->unit,
126
 
                       setup_tables_done_option);
127
 
  }
 
249
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
128
250
  else
129
251
  {
130
 
    Select_Lex_Unit *unit= &lex->unit;
 
252
    SELECT_LEX_UNIT *unit= &lex->unit;
131
253
    unit->set_limit(unit->global_parameters);
132
 
    session->session_marker= 0;
 
254
    thd->thd_marker= 0;
133
255
    /*
134
256
      '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
 
257
      every PS/SP execution new, we will not need reset this flag if 
136
258
      setup_tables_done_option changed for next rexecution
137
259
    */
138
 
    res= mysql_select(session, &select_lex->ref_pointer_array,
 
260
    res= mysql_select(thd, &select_lex->ref_pointer_array,
139
261
                      (TableList*) select_lex->table_list.first,
140
262
                      select_lex->with_wild, select_lex->item_list,
141
263
                      select_lex->where,
142
264
                      select_lex->order_list.elements +
143
265
                      select_lex->group_list.elements,
144
 
                      (Order*) select_lex->order_list.first,
145
 
                      (Order*) select_lex->group_list.first,
 
266
                      (order_st*) select_lex->order_list.first,
 
267
                      (order_st*) select_lex->group_list.first,
146
268
                      select_lex->having,
147
 
                      select_lex->options | session->options |
 
269
                      (order_st*) lex->proc_list.first,
 
270
                      select_lex->options | thd->options |
148
271
                      setup_tables_done_option,
149
272
                      result, unit, select_lex);
150
273
  }
151
 
  res|= session->is_error();
 
274
  res|= thd->is_error();
152
275
  if (unlikely(res))
153
276
    result->abort();
154
277
 
155
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
156
 
  return res;
 
278
  DRIZZLE_SELECT_END();
 
279
  return(res);
157
280
}
158
281
 
 
282
 
159
283
/*
160
284
  Fix fields referenced from inner selects.
161
285
 
162
286
  SYNOPSIS
163
287
    fix_inner_refs()
164
 
    session               Thread handle
 
288
    thd               Thread handle
165
289
    all_fields        List of all fields used in select
166
290
    select            Current select
167
291
    ref_pointer_array Array of references to Items used in current select
196
320
    true  an error occured
197
321
    false ok
198
322
*/
199
 
bool fix_inner_refs(Session *session, 
200
 
                    List<Item> &all_fields, 
201
 
                    Select_Lex *select, 
202
 
                    Item **ref_pointer_array)
 
323
 
 
324
bool
 
325
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
326
                 Item **ref_pointer_array)
203
327
{
204
328
  Item_outer_ref *ref;
205
329
  bool res= false;
259
383
    ref->outer_ref= new_ref;
260
384
    ref->ref= &ref->outer_ref;
261
385
 
262
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
386
    if (!ref->fixed && ref->fix_fields(thd, 0))
263
387
      return true;
264
 
    session->used_tables|= item->used_tables();
 
388
    thd->used_tables|= item->used_tables();
265
389
  }
266
390
  return res;
267
391
}
268
392
 
 
393
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
394
/**
 
395
  Function to setup clauses without sum functions.
 
396
*/
 
397
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
398
                               TableList *tables,
 
399
                               TableList *leaves,
 
400
                               List<Item> &fields,
 
401
                               List<Item> &all_fields,
 
402
                               COND **conds,
 
403
                               order_st *order,
 
404
                               order_st *group, bool *hidden_group_fields)
 
405
{
 
406
  int res;
 
407
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
408
 
 
409
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
410
  res= setup_conds(thd, tables, leaves, conds);
 
411
 
 
412
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
413
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
414
                          order);
 
415
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
416
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
417
                          group, hidden_group_fields);
 
418
  thd->lex->allow_sum_func= save_allow_sum_func;
 
419
  return(res);
 
420
}
 
421
 
269
422
/*****************************************************************************
270
423
  Check fields, find best join, do the select and output fields.
271
424
  mysql_select assumes that all tables are already opened
272
425
*****************************************************************************/
273
426
 
 
427
/**
 
428
  Prepare of whole select (including sub queries in future).
 
429
 
 
430
  @todo
 
431
    Add check of calculation of GROUP functions and fields:
 
432
    SELECT COUNT(*)+table.col1 from table1;
 
433
 
 
434
  @retval
 
435
    -1   on error
 
436
  @retval
 
437
    0   on success
 
438
*/
 
439
int
 
440
JOIN::prepare(Item ***rref_pointer_array,
 
441
              TableList *tables_init,
 
442
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
 
443
              order_st *order_init, order_st *group_init,
 
444
              Item *having_init,
 
445
              order_st *proc_param_init, SELECT_LEX *select_lex_arg,
 
446
              SELECT_LEX_UNIT *unit_arg)
 
447
{
 
448
  // to prevent double initialization on EXPLAIN
 
449
  if (optimized)
 
450
    return(0);
 
451
 
 
452
  conds= conds_init;
 
453
  order= order_init;
 
454
  group_list= group_init;
 
455
  having= having_init;
 
456
  proc_param= proc_param_init;
 
457
  tables_list= tables_init;
 
458
  select_lex= select_lex_arg;
 
459
  select_lex->join= this;
 
460
  join_list= &select_lex->top_join_list;
 
461
  union_part= unit_arg->is_union();
 
462
 
 
463
  thd->lex->current_select->is_item_list_lookup= 1;
 
464
  /*
 
465
    If we have already executed SELECT, then it have not sense to prevent
 
466
    its table from update (see unique_table())
 
467
  */
 
468
  if (thd->derived_tables_processing)
 
469
    select_lex->exclude_from_table_unique_test= true;
 
470
 
 
471
  /* Check that all tables, fields, conds and order are ok */
 
472
 
 
473
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
474
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
475
                                    tables_list, &select_lex->leaf_tables,
 
476
                                    false))
 
477
      return(-1);
 
478
 
 
479
  TableList *table_ptr;
 
480
  for (table_ptr= select_lex->leaf_tables;
 
481
       table_ptr;
 
482
       table_ptr= table_ptr->next_leaf)
 
483
    tables++;
 
484
 
 
485
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
486
      select_lex->setup_ref_array(thd, og_num) ||
 
487
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
488
                   &all_fields, 1) ||
 
489
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
490
                          select_lex->leaf_tables, fields_list,
 
491
                          all_fields, &conds, order, group_list,
 
492
                          &hidden_group_fields))
 
493
    return(-1);                         /* purecov: inspected */
 
494
 
 
495
  ref_pointer_array= *rref_pointer_array;
 
496
  
 
497
  if (having)
 
498
  {
 
499
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
500
    thd->where="having clause";
 
501
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
502
    select_lex->having_fix_field= 1;
 
503
    bool having_fix_rc= (!having->fixed &&
 
504
                         (having->fix_fields(thd, &having) ||
 
505
                          having->check_cols(1)));
 
506
    select_lex->having_fix_field= 0;
 
507
    if (having_fix_rc || thd->is_error())
 
508
      return(-1);                               /* purecov: inspected */
 
509
    thd->lex->allow_sum_func= save_allow_sum_func;
 
510
  }
 
511
 
 
512
  {
 
513
    Item_subselect *subselect;
 
514
    Item_in_subselect *in_subs= NULL;
 
515
    /*
 
516
      Are we in a subquery predicate?
 
517
      TODO: the block below will be executed for every PS execution without need.
 
518
    */
 
519
    if ((subselect= select_lex->master_unit()->item))
 
520
    {
 
521
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
522
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
523
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
524
        in_subs= (Item_in_subselect*)subselect;
 
525
 
 
526
      /*
 
527
        Check if we're in subquery that is a candidate for flattening into a
 
528
        semi-join (which is done done in flatten_subqueries()). The
 
529
        requirements are:
 
530
          1. Subquery predicate is an IN/=ANY subq predicate
 
531
          2. Subquery is a single SELECT (not a UNION)
 
532
          3. Subquery does not have GROUP BY or order_st BY
 
533
          4. Subquery does not use aggregate functions or HAVING
 
534
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
535
          6. No execution method was already chosen (by a prepared statement).
 
536
 
 
537
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
538
               doesn't have a JOIN (TODO: We should handle this at some
 
539
               point by switching to multi-table UPDATE/DELETE)
 
540
 
 
541
          (**). We're not in a confluent table-less subquery, like
 
542
                "SELECT 1". 
 
543
      */
 
544
      if (in_subs &&                                                    // 1
 
545
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
546
          !select_lex->group_list.elements && !order &&                 // 3
 
547
          !having && !select_lex->with_sum_func &&                      // 4
 
548
          thd->thd_marker &&                                            // 5
 
549
          select_lex->outer_select()->join &&                           // (*)
 
550
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
551
          do_semijoin &&
 
552
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
553
      {
 
554
        {
 
555
          if (!in_subs->left_expr->fixed &&
 
556
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
557
          {
 
558
            return(-1);
 
559
          }
 
560
          /*
 
561
            Check that the right part of the subselect contains no more than one
 
562
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
563
          */
 
564
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
565
             (select_lex->item_list.elements != 
 
566
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
567
          {
 
568
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
569
            return(-1);
 
570
          }
 
571
        }
 
572
 
 
573
        /* Register the subquery for further processing */
 
574
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
575
        in_subs->expr_join_nest= (TableList*)thd->thd_marker;
 
576
      }
 
577
      else
 
578
      {
 
579
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
580
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
581
        /*
 
582
          Check if the subquery predicate can be executed via materialization.
 
583
          The required conditions are:
 
584
          1. Subquery predicate is an IN/=ANY subq predicate
 
585
          2. Subquery is a single SELECT (not a UNION)
 
586
          3. Subquery is not a table-less query. In this case there is no
 
587
             point in materializing.
 
588
          4. Subquery predicate is a top-level predicate
 
589
             (this implies it is not negated)
 
590
             TODO: this is a limitation that should be lifeted once we
 
591
             implement correct NULL semantics (WL#3830)
 
592
          5. Subquery is non-correlated
 
593
             TODO:
 
594
             This is an overly restrictive condition. It can be extended to:
 
595
             (Subquery is non-correlated ||
 
596
              Subquery is correlated to any query outer to IN predicate ||
 
597
              (Subquery is correlated to the immediate outer query &&
 
598
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
599
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
600
          6. No execution method was already chosen (by a prepared statement).
 
601
 
 
602
          (*) The subquery must be part of a SELECT statement. The current
 
603
               condition also excludes multi-table update statements.
 
604
 
 
605
          We have to determine whether we will perform subquery materialization
 
606
          before calling the IN=>EXISTS transformation, so that we know whether to
 
607
          perform the whole transformation or only that part of it which wraps
 
608
          Item_in_subselect in an Item_in_optimizer.
 
609
        */
 
610
        if (do_materialize && 
 
611
            in_subs  &&                                                   // 1
 
612
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
613
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
614
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
615
        {
 
616
          if (in_subs->is_top_level_item() &&                             // 4
 
617
              !in_subs->is_correlated &&                                  // 5
 
618
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
619
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
620
        }
 
621
 
 
622
        Item_subselect::trans_res trans_res;
 
623
        if ((trans_res= subselect->select_transformer(this)) !=
 
624
            Item_subselect::RES_OK)
 
625
        {
 
626
          return((trans_res == Item_subselect::RES_ERROR));
 
627
        }
 
628
      }
 
629
    }
 
630
  }
 
631
 
 
632
  if (order)
 
633
  {
 
634
    order_st *ord;
 
635
    for (ord= order; ord; ord= ord->next)
 
636
    {
 
637
      Item *item= *ord->item;
 
638
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
639
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
640
    }
 
641
  }
 
642
 
 
643
  if (having && having->with_sum_func)
 
644
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
645
                            &having, true);
 
646
  if (select_lex->inner_sum_func_list)
 
647
  {
 
648
    Item_sum *end=select_lex->inner_sum_func_list;
 
649
    Item_sum *item_sum= end;  
 
650
    do
 
651
    { 
 
652
      item_sum= item_sum->next;
 
653
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
654
                                all_fields, item_sum->ref_by, false);
 
655
    } while (item_sum != end);
 
656
  }
 
657
 
 
658
  if (select_lex->inner_refs_list.elements &&
 
659
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
660
    return(-1);
 
661
 
 
662
  /*
 
663
    Check if there are references to un-aggregated columns when computing 
 
664
    aggregate functions with implicit grouping (there is no GROUP BY).
 
665
 
 
666
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
667
  */
 
668
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
669
  {
 
670
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
671
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
672
    return(-1);
 
673
  }
 
674
  {
 
675
    /* Caclulate the number of groups */
 
676
    send_group_parts= 0;
 
677
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
678
      send_group_parts++;
 
679
  }
 
680
  
 
681
  if (error)
 
682
    goto err;                                   /* purecov: inspected */
 
683
 
 
684
  if (result && result->prepare(fields_list, unit_arg))
 
685
    goto err;                                   /* purecov: inspected */
 
686
 
 
687
  /* Init join struct */
 
688
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
689
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
690
  this->group= group_list != 0;
 
691
  unit= unit_arg;
 
692
 
 
693
#ifdef RESTRICTED_GROUP
 
694
  if (sum_func_count && !group_list && (func_count || field_count))
 
695
  {
 
696
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
697
    goto err;
 
698
  }
 
699
#endif
 
700
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
701
    goto err;
 
702
  if (alloc_func_list())
 
703
    goto err;
 
704
 
 
705
  return(0); // All OK
 
706
 
 
707
err:
 
708
  return(-1);                           /* purecov: inspected */
 
709
}
 
710
 
 
711
 
 
712
/*
 
713
  Remove the predicates pushed down into the subquery
 
714
 
 
715
  SYNOPSIS
 
716
    JOIN::remove_subq_pushed_predicates()
 
717
      where   IN  Must be NULL
 
718
              OUT The remaining WHERE condition, or NULL
 
719
 
 
720
  DESCRIPTION
 
721
    Given that this join will be executed using (unique|index)_subquery,
 
722
    without "checking NULL", remove the predicates that were pushed down
 
723
    into the subquery.
 
724
 
 
725
    If the subquery compares scalar values, we can remove the condition that
 
726
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
727
    engine)
 
728
 
 
729
    If the subquery compares row values, we need to keep the wrapped
 
730
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
731
    and non-NULL values, we'll do a full table scan and will rely on the
 
732
    equalities corresponding to non-NULL parts of left tuple to filter out
 
733
    non-matching records.
 
734
 
 
735
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
736
    fact that subquery engine will be using index lookup. This must be done only
 
737
    for cases where there are no conversion errors of significance, e.g. 257
 
738
    that is searched in a byte. But this requires homogenization of the return 
 
739
    codes of all Field*::store() methods.
 
740
*/
 
741
 
 
742
void JOIN::remove_subq_pushed_predicates(Item **where)
 
743
{
 
744
  if (conds->type() == Item::FUNC_ITEM &&
 
745
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
746
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
747
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
748
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
749
                   ((Item_func *)conds)->arguments()[0]))
 
750
  {
 
751
    *where= 0;
 
752
    return;
 
753
  }
 
754
}
 
755
 
 
756
 
274
757
/*
275
758
  Index lookup-based subquery: save some flags for EXPLAIN output
276
759
 
283
766
  DESCRIPTION
284
767
    For index lookup-based subquery (i.e. one executed with
285
768
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
286
 
    check its EXPLAIN output row should contain
287
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
769
    check its EXPLAIN output row should contain 
 
770
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
288
771
      "Using Where" (TAB_INFO_USING_WHERE)
289
772
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
290
773
    and set appropriate flags in join_tab->packed_info.
291
774
*/
292
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
775
 
 
776
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
293
777
{
294
778
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
295
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
779
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
296
780
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
297
781
  if (where)
298
782
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
306
790
  }
307
791
}
308
792
 
 
793
 
 
794
 
 
795
 
 
796
/*
 
797
  Check if the table's rowid is included in the temptable
 
798
 
 
799
  SYNOPSIS
 
800
    sj_table_is_included()
 
801
      join      The join
 
802
      join_tab  The table to be checked
 
803
 
 
804
  DESCRIPTION
 
805
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
806
    in the temptable. This is so if
 
807
 
 
808
    1. The table is not embedded within some semi-join nest
 
809
    2. The has been pulled out of a semi-join nest, or
 
810
 
 
811
    3. The table is functionally dependent on some previous table
 
812
 
 
813
    [4. This is also true for constant tables that can't be
 
814
        NULL-complemented but this function is not called for such tables]
 
815
 
 
816
  RETURN
 
817
    true  - Include table's rowid
 
818
    false - Don't
 
819
*/
 
820
 
 
821
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
822
{
 
823
  if (join_tab->emb_sj_nest)
 
824
    return false;
 
825
  
 
826
  /* Check if this table is functionally dependent on the tables that
 
827
     are within the same outer join nest
 
828
  */
 
829
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
830
  if (join_tab->type == JT_EQ_REF)
 
831
  {
 
832
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
833
    uint32_t idx;
 
834
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
835
    {
 
836
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
837
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
838
        return true;
 
839
    }
 
840
    /* Ok, functionally dependent */
 
841
    return false;
 
842
  }
 
843
  /* Not functionally dependent => need to include*/
 
844
  return true;
 
845
}
 
846
 
 
847
 
 
848
/*
 
849
  Setup the strategies to eliminate semi-join duplicates.
 
850
  
 
851
  SYNOPSIS
 
852
    setup_semijoin_dups_elimination()
 
853
      join           Join to process
 
854
      options        Join options (needed to see if join buffering will be 
 
855
                     used or not)
 
856
      no_jbuf_after  Another bit of information re where join buffering will
 
857
                     be used.
 
858
 
 
859
  DESCRIPTION
 
860
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
861
    strategies:
 
862
 
 
863
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
864
                         of row combinations)
 
865
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
866
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
867
                  together and picking the 1st one)
 
868
    
 
869
    The join order has "duplicate-generating ranges", and every range is
 
870
    served by one strategy or a combination of FirstMatch with with some
 
871
    other strategy.
 
872
    
 
873
    "Duplicate-generating range" is defined as a range within the join order
 
874
    that contains all of the inner tables of a semi-join. All ranges must be
 
875
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
876
    are joined together, which is equivalent to converting
 
877
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
878
    to
 
879
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
880
    .
 
881
 
 
882
    Applicability conditions are as follows:
 
883
 
 
884
    DuplicateWeedout strategy
 
885
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
886
 
 
887
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
888
      +------+  +=========================+  +---+
 
889
        (1)                 (2)               (3)
 
890
 
 
891
       (1) - Prefix of OuterTables (those that participate in 
 
892
             IN-equality and/or are correlated with subquery) and outer 
 
893
             Noncorrelated Tables.
 
894
       (2) - The handled range. The range starts with the first sj-inner
 
895
             table, and covers all sj-inner and outer tables 
 
896
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
897
             may follow in any order.
 
898
       (3) - The suffix of outer Noncorrelated tables.
 
899
    
 
900
    FirstMatch strategy
 
901
    ~~~~~~~~~~~~~~~~~~~
 
902
 
 
903
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
904
      +------+  +==================+  +---+
 
905
        (1)             (2)          (3)
 
906
 
 
907
      (1) - Prefix of outer and non-correlated tables
 
908
      (2) - The handled range, which may contain only inner and
 
909
            non-correlated tables.
 
910
      (3) - The suffix of outer Noncorrelated tables.
 
911
 
 
912
    InsideOut strategy 
 
913
    ~~~~~~~~~~~~~~~~~~
 
914
 
 
915
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
916
     +--------+   +===========+ +=============+   +------+
 
917
        (1)           (2)          (3)              (4)
 
918
     
 
919
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
920
            all the non-trivially correlated outer tables. (non-trivially means
 
921
            that the correlation is not just through the IN-equality).
 
922
      
 
923
      (2) - Inner table for which the InsideOut scan is performed.
 
924
 
 
925
      (3) - The remainder of the duplicate-generating range. It is served by 
 
926
            application of FirstMatch strategy, with the exception that
 
927
            outer IN-correlated tables are considered to be non-correlated.
 
928
 
 
929
      (4) - THe suffix of outer and outer non-correlated tables.
 
930
 
 
931
    If several strategies are applicable, their relative priorities are:
 
932
      1. InsideOut
 
933
      2. FirstMatch 
 
934
      3. DuplicateWeedout
 
935
 
 
936
    This function walks over the join order and sets up the strategies by
 
937
    setting appropriate members in join_tab structures.
 
938
 
 
939
  RETURN
 
940
    false  OK 
 
941
    true   Out of memory error
 
942
*/
 
943
 
 
944
static
 
945
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
946
{
 
947
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
948
  struct {
 
949
    /* 
 
950
      0 - invalid (EOF marker), 
 
951
      1 - InsideOut, 
 
952
      2 - Temptable (maybe confluent),
 
953
      3 - Temptable with join buffering
 
954
    */
 
955
    uint32_t strategy;
 
956
    uint32_t start_idx; /* Left range bound */
 
957
    uint32_t end_idx;   /* Right range bound */
 
958
    /* 
 
959
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
960
      all involved join nests.
 
961
    */
 
962
    table_map outer_tables;
 
963
  } dups_ranges [MAX_TABLES];
 
964
 
 
965
  TableList *emb_insideout_nest= NULL;
 
966
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
967
                               tables) whose ranges we're in */
 
968
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
969
  table_map range_start_map= 0; /* table_map at current range start */
 
970
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
971
  int cur_range= 0;
 
972
  uint32_t i;
 
973
 
 
974
  /*
 
975
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
976
  */
 
977
  for (i=join->const_tables ; i < join->tables ; i++)
 
978
  {
 
979
    JOIN_TAB *tab=join->join_tab+i;
 
980
    Table *table=tab->table;
 
981
    cur_map |= table->map;
 
982
 
 
983
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
984
    {
 
985
      if (!emb_sj_map)
 
986
      {
 
987
        dups_ranges[cur_range].start_idx= i;
 
988
        range_start_map= cur_map & ~table->map;
 
989
        /*
 
990
          Remember if this is a possible start of range that is covered by
 
991
          the InsideOut strategy (the reason that it is not covered could
 
992
          be that it overlaps with anther semi-join's range. we don't
 
993
          support InsideOut for joined ranges)
 
994
        */
 
995
        if (join->best_positions[i].use_insideout_scan)
 
996
          emb_insideout_nest= tab->emb_sj_nest;
 
997
      }
 
998
 
 
999
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
1000
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
1001
 
 
1002
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1003
      {
 
1004
        /*
 
1005
          Two different semi-joins interleave. This cannot be handled by
 
1006
          InsideOut strategy.
 
1007
        */
 
1008
        emb_insideout_nest= NULL;
 
1009
      }
 
1010
    }
 
1011
 
 
1012
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1013
    {
 
1014
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1015
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1016
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1017
      {
 
1018
        /*
 
1019
          This table uses join buffering, which makes use of FirstMatch or 
 
1020
          InsideOut strategies impossible for the current and (we assume) 
 
1021
          preceding duplicate-producing ranges.
 
1022
          That is, for the join order:
 
1023
 
 
1024
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1025
                  |     |     |     |          | \
 
1026
                  +-----+     +-----+          |  join buffering use
 
1027
                     r1          r2         we're here
 
1028
 
 
1029
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1030
          strategy that spans all the tables, starting from the very 1st
 
1031
          one.
 
1032
        */
 
1033
        dealing_with_jbuf= true;
 
1034
        emb_insideout_nest= false;
 
1035
 
 
1036
        /* 
 
1037
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1038
          do not matter: 
 
1039
        */
 
1040
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1041
        {
 
1042
          dups_ranges[cur_range].outer_tables |= 
 
1043
            dups_ranges[prev_range].outer_tables;
 
1044
        }
 
1045
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1046
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1047
        cur_range=  0;
 
1048
      }
 
1049
 
 
1050
      /*
 
1051
        Check if we are at the end of duplicate-producing range. We are if
 
1052
 
 
1053
        1. It's an InsideOut range (which presumes all correlated tables are
 
1054
           in the prefix), and all inner tables are in the join order prefix,
 
1055
           or
 
1056
        2. It's a DuplicateElimination range (possibly covering several
 
1057
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1058
           sj-nests are in the join order prefix.
 
1059
      */
 
1060
      bool end_of_range= false;
 
1061
      if (emb_insideout_nest && 
 
1062
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1063
      {
 
1064
        /* Save that this range is handled with InsideOut: */
 
1065
        dups_ranges[cur_range].strategy= 1;
 
1066
        end_of_range= true;
 
1067
      }
 
1068
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1069
      {
 
1070
        /*
 
1071
          This is a complete range to be handled with either DuplicateWeedout 
 
1072
          or FirstMatch
 
1073
        */
 
1074
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1075
        /* 
 
1076
          This will hold tables from within the range that need to be put 
 
1077
          into the join buffer before we can use the FirstMatch on its tail.
 
1078
        */
 
1079
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1080
                                             ~range_start_map;
 
1081
        end_of_range= true;
 
1082
      }
 
1083
 
 
1084
      if (end_of_range)
 
1085
      {
 
1086
        dups_ranges[cur_range].end_idx= i+1;
 
1087
        emb_sj_map= emb_outer_tables= 0;
 
1088
        emb_insideout_nest= NULL;
 
1089
        dealing_with_jbuf= false;
 
1090
        dups_ranges[++cur_range].strategy= 0;
 
1091
      }
 
1092
    }
 
1093
  }
 
1094
 
 
1095
  THD *thd= join->thd;
 
1096
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1097
  /*
 
1098
    Second pass: setup the chosen strategies    
 
1099
  */
 
1100
  for (int j= 0; j < cur_range; j++)
 
1101
  {
 
1102
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1103
    JOIN_TAB *jump_to;
 
1104
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1105
    {
 
1106
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1107
      jump_to= tab++;
 
1108
    }
 
1109
    else // DuplicateWeedout strategy
 
1110
    {
 
1111
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1112
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1113
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1114
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
1115
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1116
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1117
      JOIN_TAB *last_outer_tab= tab - 1;
 
1118
      /*
 
1119
        Walk through the range and remember
 
1120
         - tables that need their rowids to be put into temptable
 
1121
         - the last outer table
 
1122
      */
 
1123
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1124
      {
 
1125
        if (sj_table_is_included(join, tab))
 
1126
        {
 
1127
          last_tab->join_tab= tab;
 
1128
          last_tab->rowid_offset= jt_rowid_offset;
 
1129
          jt_rowid_offset += tab->table->file->ref_length;
 
1130
          if (tab->table->maybe_null)
 
1131
          {
 
1132
            last_tab->null_byte= jt_null_bits / 8;
 
1133
            last_tab->null_bit= jt_null_bits++;
 
1134
          }
 
1135
          last_tab++;
 
1136
          tab->table->prepare_for_position();
 
1137
          tab->rowid_keep_flags= rowid_keep_flags;
 
1138
        }
 
1139
        cur_map |= tab->table->map;
 
1140
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1141
                                               dups_ranges[j].outer_tables))
 
1142
          last_outer_tab= tab;
 
1143
      }
 
1144
 
 
1145
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1146
      {
 
1147
        SJ_TMP_TABLE *sjtbl;
 
1148
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1149
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1150
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1151
          return(true);
 
1152
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1153
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1154
        sjtbl->rowid_len= jt_rowid_offset;
 
1155
        sjtbl->null_bits= jt_null_bits;
 
1156
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1157
 
 
1158
        *next_sjtbl_ptr= sjtbl;
 
1159
        next_sjtbl_ptr= &(sjtbl->next);
 
1160
        sjtbl->next= NULL;
 
1161
 
 
1162
        sjtbl->tmp_table= 
 
1163
          create_duplicate_weedout_tmp_table(thd, 
 
1164
                                             sjtbl->rowid_len + 
 
1165
                                             sjtbl->null_bytes,
 
1166
                                             sjtbl);
 
1167
 
 
1168
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1169
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1170
      }
 
1171
      tab= last_outer_tab + 1;
 
1172
      jump_to= last_outer_tab;
 
1173
    }
 
1174
 
 
1175
    /* Create the FirstMatch tail */
 
1176
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1177
    {
 
1178
      if (tab->emb_sj_nest)
 
1179
        tab->do_firstmatch= jump_to; 
 
1180
      else
 
1181
        jump_to= tab;
 
1182
    }
 
1183
  }
 
1184
  return(false);
 
1185
}
 
1186
 
 
1187
 
 
1188
static void cleanup_sj_tmp_tables(JOIN *join)
 
1189
{
 
1190
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1191
       sj_tbl= sj_tbl->next)
 
1192
  {
 
1193
    if (sj_tbl->tmp_table)
 
1194
    {
 
1195
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1196
    }
 
1197
  }
 
1198
  join->sj_tmp_tables= NULL;
 
1199
}
 
1200
 
 
1201
uint32_t make_join_orderinfo(JOIN *join);
 
1202
 
 
1203
/**
 
1204
  global select optimisation.
 
1205
 
 
1206
  @note
 
1207
    error code saved in field 'error'
 
1208
 
 
1209
  @retval
 
1210
    0   success
 
1211
  @retval
 
1212
    1   error
 
1213
*/
 
1214
 
 
1215
int
 
1216
JOIN::optimize()
 
1217
{
 
1218
  // to prevent double initialization on EXPLAIN
 
1219
  if (optimized)
 
1220
    return(0);
 
1221
  optimized= 1;
 
1222
 
 
1223
  thd->set_proc_info("optimizing");
 
1224
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1225
              unit->select_limit_cnt);
 
1226
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1227
  select_limit= unit->select_limit_cnt;
 
1228
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1229
    select_limit= HA_POS_ERROR;
 
1230
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1231
  // Ignore errors of execution if option IGNORE present
 
1232
  if (thd->lex->ignore)
 
1233
    thd->lex->current_select->no_error= 1;
 
1234
 
 
1235
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1236
  /* Add HAVING to WHERE if possible */
 
1237
  if (having && !group_list && !sum_func_count)
 
1238
  {
 
1239
    if (!conds)
 
1240
    {
 
1241
      conds= having;
 
1242
      having= 0;
 
1243
    }
 
1244
    else if ((conds=new Item_cond_and(conds,having)))
 
1245
    {
 
1246
      /*
 
1247
        Item_cond_and can't be fixed after creation, so we do not check
 
1248
        conds->fixed
 
1249
      */
 
1250
      conds->fix_fields(thd, &conds);
 
1251
      conds->change_ref_to_fields(thd, tables_list);
 
1252
      conds->top_level_item();
 
1253
      having= 0;
 
1254
    }
 
1255
  }
 
1256
#endif
 
1257
 
 
1258
  /* Convert all outer joins to inner joins if possible */
 
1259
  conds= simplify_joins(this, join_list, conds, true, false);
 
1260
  build_bitmap_for_nested_joins(join_list, 0);
 
1261
 
 
1262
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1263
  if (thd->is_error())
 
1264
  {
 
1265
    error= 1;
 
1266
    return(1);
 
1267
  }
 
1268
 
 
1269
  {
 
1270
    having= optimize_cond(this, having, join_list, &having_value);
 
1271
    if (thd->is_error())
 
1272
    {
 
1273
      error= 1;
 
1274
      return(1);
 
1275
    }
 
1276
    if (select_lex->where)
 
1277
      select_lex->cond_value= cond_value;
 
1278
    if (select_lex->having)
 
1279
      select_lex->having_value= having_value;
 
1280
 
 
1281
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1282
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1283
    {                                           /* Impossible cond */
 
1284
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1285
                           "Impossible HAVING" : "Impossible WHERE";
 
1286
      error= 0;
 
1287
      return(0);
 
1288
    }
 
1289
  }
 
1290
 
 
1291
  /* Optimize count(*), cmin() and cmax() */
 
1292
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1293
  {
 
1294
    int res;
 
1295
    /*
 
1296
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1297
      to the WHERE conditions,
 
1298
      or 1 if all items were resolved,
 
1299
      or 0, or an error number HA_ERR_...
 
1300
    */
 
1301
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1302
    {
 
1303
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1304
      {
 
1305
        zero_result_cause= "No matching min/max row";
 
1306
        error=0;
 
1307
        return(0);
 
1308
      }
 
1309
      if (res > 1)
 
1310
      {
 
1311
        error= res;
 
1312
        return(1);
 
1313
      }
 
1314
      if (res < 0)
 
1315
      {
 
1316
        zero_result_cause= "No matching min/max row";
 
1317
        error=0;
 
1318
        return(0);
 
1319
      }
 
1320
      zero_result_cause= "Select tables optimized away";
 
1321
      tables_list= 0;                           // All tables resolved
 
1322
      /*
 
1323
        Extract all table-independent conditions and replace the WHERE
 
1324
        clause with them. All other conditions were computed by opt_sum_query
 
1325
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1326
        so there is no need to compute the whole WHERE clause again.
 
1327
        Notice that make_cond_for_table() will always succeed to remove all
 
1328
        computed conditions, because opt_sum_query() is applicable only to
 
1329
        conjunctions.
 
1330
        Preserve conditions for EXPLAIN.
 
1331
      */
 
1332
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1333
      {
 
1334
        COND *table_independent_conds=
 
1335
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1336
        conds= table_independent_conds;
 
1337
      }
 
1338
    }
 
1339
  }
 
1340
  if (!tables_list)
 
1341
  {
 
1342
    error= 0;
 
1343
    return(0);
 
1344
  }
 
1345
  error= -1;                                    // Error is sent to client
 
1346
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1347
 
 
1348
  /* Calculate how to do the join */
 
1349
  thd->set_proc_info("statistics");
 
1350
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1351
      thd->is_fatal_error)
 
1352
  {
 
1353
    return(1);
 
1354
  }
 
1355
 
 
1356
  /* Remove distinct if only const tables */
 
1357
  select_distinct= select_distinct && (const_tables != tables);
 
1358
  thd->set_proc_info("preparing");
 
1359
  if (result->initialize_tables(this))
 
1360
  {
 
1361
    return(1);                          // error == -1
 
1362
  }
 
1363
  if (const_table_map != found_const_table_map &&
 
1364
      !(select_options & SELECT_DESCRIBE) &&
 
1365
      (!conds ||
 
1366
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1367
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1368
  {
 
1369
    zero_result_cause= "no matching row in const table";
 
1370
    error= 0;
 
1371
    return(0);
 
1372
  }
 
1373
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1374
      best_read > (double) thd->variables.max_join_size &&
 
1375
      !(select_options & SELECT_DESCRIBE))
 
1376
  {                                             /* purecov: inspected */
 
1377
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1378
    error= -1;
 
1379
    return(1);
 
1380
  }
 
1381
  if (const_tables && !thd->locked_tables &&
 
1382
      !(select_options & SELECT_NO_UNLOCK))
 
1383
    mysql_unlock_some_tables(thd, table, const_tables);
 
1384
  if (!conds && outer_join)
 
1385
  {
 
1386
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1387
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1388
  }
 
1389
  select= make_select(*table, const_table_map,
 
1390
                      const_table_map, conds, 1, &error);
 
1391
  if (error)
 
1392
  {                                             /* purecov: inspected */
 
1393
    error= -1;                                  /* purecov: inspected */
 
1394
    return(1);
 
1395
  }
 
1396
  
 
1397
  reset_nj_counters(join_list);
 
1398
  make_outerjoin_info(this);
 
1399
 
 
1400
  /*
 
1401
    Among the equal fields belonging to the same multiple equality
 
1402
    choose the one that is to be retrieved first and substitute
 
1403
    all references to these in where condition for a reference for
 
1404
    the selected field.
 
1405
  */
 
1406
  if (conds)
 
1407
  {
 
1408
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1409
    conds->update_used_tables();
 
1410
  }
 
1411
 
 
1412
  /*
 
1413
    Permorm the the optimization on fields evaluation mentioned above
 
1414
    for all on expressions.
 
1415
  */ 
 
1416
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1417
  {
 
1418
    if (*tab->on_expr_ref)
 
1419
    {
 
1420
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1421
                                                         tab->cond_equal,
 
1422
                                                         map2table);
 
1423
      (*tab->on_expr_ref)->update_used_tables();
 
1424
    }
 
1425
  }
 
1426
 
 
1427
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1428
      (select_options & SELECT_DESCRIBE) &&
 
1429
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1430
  {
 
1431
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1432
  }
 
1433
  if (make_join_select(this, select, conds))
 
1434
  {
 
1435
    zero_result_cause=
 
1436
      "Impossible WHERE noticed after reading const tables";
 
1437
    return(0);                          // error == 0
 
1438
  }
 
1439
 
 
1440
  error= -1;                                    /* if goto err */
 
1441
 
 
1442
  /* Optimize distinct away if possible */
 
1443
  {
 
1444
    order_st *org_order= order;
 
1445
    order=remove_const(this, order,conds,1, &simple_order);
 
1446
    if (thd->is_error())
 
1447
    {
 
1448
      error= 1;
 
1449
      return(1);
 
1450
    }
 
1451
 
 
1452
    /*
 
1453
      If we are using order_st BY NULL or order_st BY const_expression,
 
1454
      return result in any order (even if we are using a GROUP BY)
 
1455
    */
 
1456
    if (!order && org_order)
 
1457
      skip_sort_order= 1;
 
1458
  }
 
1459
  /*
 
1460
     Check if we can optimize away GROUP BY/DISTINCT.
 
1461
     We can do that if there are no aggregate functions, the
 
1462
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1463
     (if present) contain direct references to all key parts of
 
1464
     an unique index (in whatever order) and if the key parts of the
 
1465
     unique index cannot contain NULLs.
 
1466
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1467
     be the same (as long as they are unique).
 
1468
 
 
1469
     The FROM clause must contain a single non-constant table.
 
1470
  */
 
1471
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1472
      !tmp_table_param.sum_func_count &&
 
1473
      (!join_tab[const_tables].select ||
 
1474
       !join_tab[const_tables].select->quick ||
 
1475
       join_tab[const_tables].select->quick->get_type() != 
 
1476
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1477
  {
 
1478
    if (group_list &&
 
1479
       list_contains_unique_index(join_tab[const_tables].table,
 
1480
                                 find_field_in_order_list,
 
1481
                                 (void *) group_list))
 
1482
    {
 
1483
      /*
 
1484
        We have found that grouping can be removed since groups correspond to
 
1485
        only one row anyway, but we still have to guarantee correct result
 
1486
        order. The line below effectively rewrites the query from GROUP BY
 
1487
        <fields> to order_st BY <fields>. There are two exceptions:
 
1488
        - if skip_sort_order is set (see above), then we can simply skip
 
1489
          GROUP BY;
 
1490
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
1491
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1492
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
1493
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1494
          order_st BY fields.
 
1495
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
1496
          'order' as is.
 
1497
       */
 
1498
      if (!order || test_if_subpart(group_list, order))
 
1499
          order= skip_sort_order ? 0 : group_list;
 
1500
      /*
 
1501
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1502
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
1503
      */
 
1504
      join_tab->table->keys_in_use_for_order_by=
 
1505
        join_tab->table->keys_in_use_for_group_by;
 
1506
      group_list= 0;
 
1507
      group= 0;
 
1508
    }
 
1509
    if (select_distinct &&
 
1510
       list_contains_unique_index(join_tab[const_tables].table,
 
1511
                                 find_field_in_item_list,
 
1512
                                 (void *) &fields_list))
 
1513
    {
 
1514
      select_distinct= 0;
 
1515
    }
 
1516
  }
 
1517
  if (group_list || tmp_table_param.sum_func_count)
 
1518
  {
 
1519
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1520
      select_distinct=0;
 
1521
  }
 
1522
  else if (select_distinct && tables - const_tables == 1)
 
1523
  {
 
1524
    /*
 
1525
      We are only using one table. In this case we change DISTINCT to a
 
1526
      GROUP BY query if:
 
1527
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
1528
        BY only uses selected fields.
 
1529
        (In this case we can later optimize away GROUP BY and order_st BY)
 
1530
      - We are scanning the whole table without LIMIT
 
1531
        This can happen if:
 
1532
        - We are using CALC_FOUND_ROWS
 
1533
        - We are using an order_st BY that can't be optimized away.
 
1534
 
 
1535
      We don't want to use this optimization when we are using LIMIT
 
1536
      because in this case we can just create a temporary table that
 
1537
      holds LIMIT rows and stop when this table is full.
 
1538
    */
 
1539
    JOIN_TAB *tab= &join_tab[const_tables];
 
1540
    bool all_order_fields_used;
 
1541
    if (order)
 
1542
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1543
        &tab->table->keys_in_use_for_order_by);
 
1544
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1545
                                          order, fields_list, all_fields,
 
1546
                                          &all_order_fields_used)))
 
1547
    {
 
1548
      bool skip_group= (skip_sort_order &&
 
1549
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1550
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1551
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1552
      if ((skip_group && all_order_fields_used) ||
 
1553
          select_limit == HA_POS_ERROR ||
 
1554
          (order && !skip_sort_order))
 
1555
      {
 
1556
        /*  Change DISTINCT to GROUP BY */
 
1557
        select_distinct= 0;
 
1558
        no_order= !order;
 
1559
        if (all_order_fields_used)
 
1560
        {
 
1561
          if (order && skip_sort_order)
 
1562
          {
 
1563
            /*
 
1564
              Force MySQL to read the table in sorted order to get result in
 
1565
              order_st BY order.
 
1566
            */
 
1567
            tmp_table_param.quick_group=0;
 
1568
          }
 
1569
          order=0;
 
1570
        }
 
1571
        group=1;                                // For end_write_group
 
1572
      }
 
1573
      else
 
1574
        group_list= 0;
 
1575
    }
 
1576
    else if (thd->is_fatal_error)                       // End of memory
 
1577
      return(1);
 
1578
  }
 
1579
  simple_group= 0;
 
1580
  {
 
1581
    order_st *old_group_list;
 
1582
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1583
                             rollup.state == ROLLUP::STATE_NONE,
 
1584
                             &simple_group);
 
1585
    if (thd->is_error())
 
1586
    {
 
1587
      error= 1;
 
1588
      return(1);
 
1589
    }
 
1590
    if (old_group_list && !group_list)
 
1591
      select_distinct= 0;
 
1592
  }
 
1593
  if (!group_list && group)
 
1594
  {
 
1595
    order=0;                                    // The output has only one row
 
1596
    simple_order=1;
 
1597
    select_distinct= 0;                       // No need in distinct for 1 row
 
1598
    group_optimized_away= 1;
 
1599
  }
 
1600
 
 
1601
  calc_group_buffer(this, group_list);
 
1602
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1603
 
 
1604
  if (test_if_subpart(group_list, order) ||
 
1605
      (!group_list && tmp_table_param.sum_func_count))
 
1606
    order=0;
 
1607
 
 
1608
  // Can't use sort on head table if using row cache
 
1609
  if (full_join)
 
1610
  {
 
1611
    if (group_list)
 
1612
      simple_group=0;
 
1613
    if (order)
 
1614
      simple_order=0;
 
1615
  }
 
1616
 
 
1617
  /*
 
1618
    Check if we need to create a temporary table.
 
1619
    This has to be done if all tables are not already read (const tables)
 
1620
    and one of the following conditions holds:
 
1621
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1622
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
1623
    - We are using different order_st BY and GROUP BY orders
 
1624
    - The user wants us to buffer the result.
 
1625
  */
 
1626
  need_tmp= (const_tables != tables &&
 
1627
             ((select_distinct || !simple_order || !simple_group) ||
 
1628
              (group_list && order) ||
 
1629
              test(select_options & OPTION_BUFFER_RESULT)));
 
1630
 
 
1631
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
1632
  uint64_t select_opts_for_readinfo= 
 
1633
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1634
 
 
1635
  sj_tmp_tables= NULL;
 
1636
  if (!select_lex->sj_nests.is_empty())
 
1637
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1638
                                    no_jbuf_after);
 
1639
 
 
1640
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1641
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1642
    return(1);
 
1643
 
 
1644
  /* Create all structures needed for materialized subquery execution. */
 
1645
  if (setup_subquery_materialization())
 
1646
    return(1);
 
1647
 
 
1648
  /*
 
1649
    is this simple IN subquery?
 
1650
  */
 
1651
  if (!group_list && !order &&
 
1652
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1653
      tables == 1 && conds &&
 
1654
      !unit->is_union())
 
1655
  {
 
1656
    if (!having)
 
1657
    {
 
1658
      Item *where= conds;
 
1659
      if (join_tab[0].type == JT_EQ_REF &&
 
1660
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1661
      {
 
1662
        remove_subq_pushed_predicates(&where);
 
1663
        save_index_subquery_explain_info(join_tab, where);
 
1664
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1665
        error= 0;
 
1666
        return(unit->item->
 
1667
                    change_engine(new
 
1668
                                  subselect_uniquesubquery_engine(thd,
 
1669
                                                                  join_tab,
 
1670
                                                                  unit->item,
 
1671
                                                                  where)));
 
1672
      }
 
1673
      else if (join_tab[0].type == JT_REF &&
 
1674
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1675
      {
 
1676
        remove_subq_pushed_predicates(&where);
 
1677
        save_index_subquery_explain_info(join_tab, where);
 
1678
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1679
        error= 0;
 
1680
        return(unit->item->
 
1681
                    change_engine(new
 
1682
                                  subselect_indexsubquery_engine(thd,
 
1683
                                                                 join_tab,
 
1684
                                                                 unit->item,
 
1685
                                                                 where,
 
1686
                                                                 NULL,
 
1687
                                                                 0)));
 
1688
      }
 
1689
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1690
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1691
               having->name == in_having_cond)
 
1692
    {
 
1693
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1694
      error= 0;
 
1695
      conds= remove_additional_cond(conds);
 
1696
      save_index_subquery_explain_info(join_tab, conds);
 
1697
      return(unit->item->
 
1698
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1699
                                                                   join_tab,
 
1700
                                                                   unit->item,
 
1701
                                                                   conds,
 
1702
                                                                   having,
 
1703
                                                                   1)));
 
1704
    }
 
1705
 
 
1706
  }
 
1707
  /*
 
1708
    Need to tell handlers that to play it safe, it should fetch all
 
1709
    columns of the primary key of the tables: this is because MySQL may
 
1710
    build row pointers for the rows, and for all columns of the primary key
 
1711
    the read set has not necessarily been set by the server code.
 
1712
  */
 
1713
  if (need_tmp || select_distinct || group_list || order)
 
1714
  {
 
1715
    for (uint32_t i = const_tables; i < tables; i++)
 
1716
      join_tab[i].table->prepare_for_position();
 
1717
  }
 
1718
 
 
1719
  if (const_tables != tables)
 
1720
  {
 
1721
    /*
 
1722
      Because filesort always does a full table scan or a quick range scan
 
1723
      we must add the removed reference to the select for the table.
 
1724
      We only need to do this when we have a simple_order or simple_group
 
1725
      as in other cases the join is done before the sort.
 
1726
    */
 
1727
    if ((order || group_list) &&
 
1728
        (join_tab[const_tables].type != JT_ALL) &&
 
1729
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1730
        ((order && simple_order) || (group_list && simple_group)))
 
1731
    {
 
1732
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1733
        return(1);
 
1734
      }
 
1735
    }
 
1736
    
 
1737
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1738
        ((group_list &&
 
1739
          (!simple_group ||
 
1740
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1741
                                    unit->select_limit_cnt, 0, 
 
1742
                                    &join_tab[const_tables].table->
 
1743
                                    keys_in_use_for_group_by))) ||
 
1744
         select_distinct) &&
 
1745
        tmp_table_param.quick_group)
 
1746
    {
 
1747
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1748
    }
 
1749
    if (order)
 
1750
    {
 
1751
      /*
 
1752
        Force using of tmp table if sorting by a SP or UDF function due to
 
1753
        their expensive and probably non-deterministic nature.
 
1754
      */
 
1755
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1756
      {
 
1757
        Item *item= *tmp_order->item;
 
1758
        if (item->is_expensive())
 
1759
        {
 
1760
          /* Force tmp table without sort */
 
1761
          need_tmp=1; simple_order=simple_group=0;
 
1762
          break;
 
1763
        }
 
1764
      }
 
1765
    }
 
1766
  }
 
1767
 
 
1768
  tmp_having= having;
 
1769
  if (select_options & SELECT_DESCRIBE)
 
1770
  {
 
1771
    error= 0;
 
1772
    return(0);
 
1773
  }
 
1774
  having= 0;
 
1775
 
 
1776
  /*
 
1777
    The loose index scan access method guarantees that all grouping or
 
1778
    duplicate row elimination (for distinct) is already performed
 
1779
    during data retrieval, and that all MIN/MAX functions are already
 
1780
    computed for each group. Thus all MIN/MAX functions should be
 
1781
    treated as regular functions, and there is no need to perform
 
1782
    grouping in the main execution loop.
 
1783
    Notice that currently loose index scan is applicable only for
 
1784
    single table queries, thus it is sufficient to test only the first
 
1785
    join_tab element of the plan for its access method.
 
1786
  */
 
1787
  if (join_tab->is_using_loose_index_scan())
 
1788
    tmp_table_param.precomputed_group_by= true;
 
1789
 
 
1790
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1791
  if (need_tmp)
 
1792
  {
 
1793
    thd->set_proc_info("Creating tmp table");
 
1794
 
 
1795
    init_items_ref_array();
 
1796
 
 
1797
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1798
                                         fields_list.elements);
 
1799
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1800
                                                             (order_st*) 0);
 
1801
    /*
 
1802
      Pushing LIMIT to the temporary table creation is not applicable
 
1803
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1804
      there are aggregate functions, because in all these cases we need
 
1805
      all result rows.
 
1806
    */
 
1807
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1808
                             !tmp_group &&
 
1809
                             !thd->lex->current_select->with_sum_func) ?
 
1810
                            select_limit : HA_POS_ERROR;
 
1811
 
 
1812
    if (!(exec_tmp_table1=
 
1813
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1814
                           tmp_group,
 
1815
                           group_list ? 0 : select_distinct,
 
1816
                           group_list && simple_group,
 
1817
                           select_options,
 
1818
                           tmp_rows_limit,
 
1819
                           (char *) "")))
 
1820
                {
 
1821
      return(1);
 
1822
    }
 
1823
 
 
1824
    /*
 
1825
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1826
      - we are sorting the table and writing complete group rows to the
 
1827
        temp table.
 
1828
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1829
        on all columns.
 
1830
      
 
1831
      If having is not handled here, it will be checked before the row
 
1832
      is sent to the client.
 
1833
    */    
 
1834
    if (tmp_having && 
 
1835
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1836
      having= tmp_having;
 
1837
 
 
1838
    /* if group or order on first table, sort first */
 
1839
    if (group_list && simple_group)
 
1840
    {
 
1841
      thd->set_proc_info("Sorting for group");
 
1842
      if (create_sort_index(thd, this, group_list,
 
1843
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1844
          alloc_group_fields(this, group_list) ||
 
1845
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1846
          setup_sum_funcs(thd, sum_funcs))
 
1847
      {
 
1848
        return(1);
 
1849
      }
 
1850
      group_list=0;
 
1851
    }
 
1852
    else
 
1853
    {
 
1854
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1855
          setup_sum_funcs(thd, sum_funcs))
 
1856
      {
 
1857
        return(1);
 
1858
      }
 
1859
 
 
1860
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1861
      {
 
1862
        thd->set_proc_info("Sorting for order");
 
1863
        if (create_sort_index(thd, this, order,
 
1864
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1865
        {
 
1866
          return(1);
 
1867
        }
 
1868
        order=0;
 
1869
      }
 
1870
    }
 
1871
    
 
1872
    /*
 
1873
      Optimize distinct when used on some of the tables
 
1874
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1875
      In this case we can stop scanning t2 when we have found one t1.a
 
1876
    */
 
1877
 
 
1878
    if (exec_tmp_table1->distinct)
 
1879
    {
 
1880
      table_map used_tables= thd->used_tables;
 
1881
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1882
      do
 
1883
      {
 
1884
        if (used_tables & last_join_tab->table->map)
 
1885
          break;
 
1886
        last_join_tab->not_used_in_distinct=1;
 
1887
      } while (last_join_tab-- != join_tab);
 
1888
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1889
      if (order && skip_sort_order)
 
1890
      {
 
1891
        /* Should always succeed */
 
1892
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1893
                                    order, unit->select_limit_cnt, 0, 
 
1894
                                    &join_tab[const_tables].table->
 
1895
                                      keys_in_use_for_order_by))
 
1896
          order=0;
 
1897
      }
 
1898
    }
 
1899
 
 
1900
    /* 
 
1901
      If this join belongs to an uncacheable subquery save 
 
1902
      the original join 
 
1903
    */
 
1904
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1905
        init_save_join_tab())
 
1906
      return(-1);                         /* purecov: inspected */
 
1907
  }
 
1908
 
 
1909
  error= 0;
 
1910
  return(0);
 
1911
}
 
1912
 
 
1913
 
 
1914
/**
 
1915
  Restore values in temporary join.
 
1916
*/
 
1917
void JOIN::restore_tmp()
 
1918
{
 
1919
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1920
}
 
1921
 
 
1922
 
 
1923
int
 
1924
JOIN::reinit()
 
1925
{
 
1926
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1927
                                    select_lex->offset_limit->val_uint() :
 
1928
                                    0UL);
 
1929
 
 
1930
  first_record= 0;
 
1931
 
 
1932
  if (exec_tmp_table1)
 
1933
  {
 
1934
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1935
    exec_tmp_table1->file->ha_delete_all_rows();
 
1936
    free_io_cache(exec_tmp_table1);
 
1937
    filesort_free_buffers(exec_tmp_table1,0);
 
1938
  }
 
1939
  if (exec_tmp_table2)
 
1940
  {
 
1941
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1942
    exec_tmp_table2->file->ha_delete_all_rows();
 
1943
    free_io_cache(exec_tmp_table2);
 
1944
    filesort_free_buffers(exec_tmp_table2,0);
 
1945
  }
 
1946
  if (items0)
 
1947
    set_items_ref_array(items0);
 
1948
 
 
1949
  if (join_tab_save)
 
1950
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1951
 
 
1952
  if (tmp_join)
 
1953
    restore_tmp();
 
1954
 
 
1955
  /* Reset of sum functions */
 
1956
  if (sum_funcs)
 
1957
  {
 
1958
    Item_sum *func, **func_ptr= sum_funcs;
 
1959
    while ((func= *(func_ptr++)))
 
1960
      func->clear();
 
1961
  }
 
1962
 
 
1963
  return(0);
 
1964
}
 
1965
 
 
1966
/**
 
1967
   @brief Save the original join layout
 
1968
      
 
1969
   @details Saves the original join layout so it can be reused in 
 
1970
   re-execution and for EXPLAIN.
 
1971
             
 
1972
   @return Operation status
 
1973
   @retval 0      success.
 
1974
   @retval 1      error occurred.
 
1975
*/
 
1976
 
 
1977
bool
 
1978
JOIN::init_save_join_tab()
 
1979
{
 
1980
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
1981
    return 1;                                  /* purecov: inspected */
 
1982
  error= 0;                                    // Ensure that tmp_join.error= 0
 
1983
  restore_tmp();
 
1984
  return 0;
 
1985
}
 
1986
 
 
1987
 
 
1988
bool
 
1989
JOIN::save_join_tab()
 
1990
{
 
1991
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
1992
  {
 
1993
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((unsigned char*) join_tab,
 
1994
                                                sizeof(JOIN_TAB) * tables)))
 
1995
      return 1;
 
1996
  }
 
1997
  return 0;
 
1998
}
 
1999
 
 
2000
 
 
2001
/**
 
2002
  Exec select.
 
2003
 
 
2004
  @todo
 
2005
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2006
    finally replace sorting with index scan if there is a LIMIT clause in
 
2007
    the query.  It's never shown in EXPLAIN!
 
2008
 
 
2009
  @todo
 
2010
    When can we have here thd->net.report_error not zero?
 
2011
*/
 
2012
void
 
2013
JOIN::exec()
 
2014
{
 
2015
  List<Item> *columns_list= &fields_list;
 
2016
  int      tmp_error;
 
2017
 
 
2018
  thd->set_proc_info("executing");
 
2019
  error= 0;
 
2020
  (void) result->prepare2(); // Currently, this cannot fail.
 
2021
 
 
2022
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2023
  {                                           // Only test of functions
 
2024
    if (select_options & SELECT_DESCRIBE)
 
2025
      select_describe(this, false, false, false,
 
2026
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2027
    else
 
2028
    {
 
2029
      result->send_fields(*columns_list,
 
2030
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2031
      /*
 
2032
        We have to test for 'conds' here as the WHERE may not be constant
 
2033
        even if we don't have any tables for prepared statements or if
 
2034
        conds uses something like 'rand()'.
 
2035
      */
 
2036
      if (cond_value != Item::COND_FALSE &&
 
2037
          (!conds || conds->val_int()) &&
 
2038
          (!having || having->val_int()))
 
2039
      {
 
2040
        if (do_send_rows && result->send_data(fields_list))
 
2041
          error= 1;
 
2042
        else
 
2043
        {
 
2044
          error= (int) result->send_eof();
 
2045
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2046
                         thd->sent_row_count);
 
2047
        }
 
2048
      }
 
2049
      else
 
2050
      {
 
2051
        error=(int) result->send_eof();
 
2052
        send_records= 0;
 
2053
      }
 
2054
    }
 
2055
    /* Single select (without union) always returns 0 or 1 row */
 
2056
    thd->limit_found_rows= send_records;
 
2057
    thd->examined_row_count= 0;
 
2058
    return;
 
2059
  }
 
2060
  /*
 
2061
    Don't reset the found rows count if there're no tables as
 
2062
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2063
    It must be accumulated from all join iterations of all join parts.
 
2064
  */
 
2065
  if (tables)
 
2066
    thd->limit_found_rows= 0;
 
2067
 
 
2068
  if (zero_result_cause)
 
2069
  {
 
2070
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2071
                            *columns_list,
 
2072
                            send_row_on_empty_set(),
 
2073
                            select_options,
 
2074
                            zero_result_cause,
 
2075
                            having);
 
2076
    return;
 
2077
  }
 
2078
 
 
2079
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2080
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2081
    return;
 
2082
 
 
2083
  if (select_options & SELECT_DESCRIBE)
 
2084
  {
 
2085
    /*
 
2086
      Check if we managed to optimize order_st BY away and don't use temporary
 
2087
      table to resolve order_st BY: in that case, we only may need to do
 
2088
      filesort for GROUP BY.
 
2089
    */
 
2090
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2091
    {
 
2092
      /*
 
2093
        Reset 'order' to 'group_list' and reinit variables describing
 
2094
        'order'
 
2095
      */
 
2096
      order= group_list;
 
2097
      simple_order= simple_group;
 
2098
      skip_sort_order= 0;
 
2099
    }
 
2100
    if (order && 
 
2101
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2102
        (const_tables == tables ||
 
2103
         ((simple_order || skip_sort_order) &&
 
2104
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2105
                                  select_limit, 0, 
 
2106
                                  &join_tab[const_tables].table->
 
2107
                                    keys_in_use_for_query))))
 
2108
      order=0;
 
2109
    having= tmp_having;
 
2110
    select_describe(this, need_tmp,
 
2111
                    order != 0 && !skip_sort_order,
 
2112
                    select_distinct,
 
2113
                    !tables ? "No tables used" : NULL);
 
2114
    return;
 
2115
  }
 
2116
 
 
2117
  JOIN *curr_join= this;
 
2118
  List<Item> *curr_all_fields= &all_fields;
 
2119
  List<Item> *curr_fields_list= &fields_list;
 
2120
  Table *curr_tmp_table= 0;
 
2121
  /*
 
2122
    Initialize examined rows here because the values from all join parts
 
2123
    must be accumulated in examined_row_count. Hence every join
 
2124
    iteration must count from zero.
 
2125
  */
 
2126
  curr_join->examined_rows= 0;
 
2127
 
 
2128
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2129
  if (need_tmp)
 
2130
  {
 
2131
    if (tmp_join)
 
2132
    {
 
2133
      /*
 
2134
        We are in a non cacheable sub query. Get the saved join structure
 
2135
        after optimization.
 
2136
        (curr_join may have been modified during last exection and we need
 
2137
        to reset it)
 
2138
      */
 
2139
      curr_join= tmp_join;
 
2140
    }
 
2141
    curr_tmp_table= exec_tmp_table1;
 
2142
 
 
2143
    /* Copy data to the temporary table */
 
2144
    thd->set_proc_info("Copying to tmp table");
 
2145
    if (!curr_join->sort_and_group &&
 
2146
        curr_join->const_tables != curr_join->tables)
 
2147
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2148
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2149
    {
 
2150
      error= tmp_error;
 
2151
      return;
 
2152
    }
 
2153
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2154
    
 
2155
    if (curr_join->having)
 
2156
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2157
    
 
2158
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2159
    curr_join->all_fields= *curr_all_fields;
 
2160
    if (!items1)
 
2161
    {
 
2162
      items1= items0 + all_fields.elements;
 
2163
      if (sort_and_group || curr_tmp_table->group)
 
2164
      {
 
2165
        if (change_to_use_tmp_fields(thd, items1,
 
2166
                                     tmp_fields_list1, tmp_all_fields1,
 
2167
                                     fields_list.elements, all_fields))
 
2168
          return;
 
2169
      }
 
2170
      else
 
2171
      {
 
2172
        if (change_refs_to_tmp_fields(thd, items1,
 
2173
                                      tmp_fields_list1, tmp_all_fields1,
 
2174
                                      fields_list.elements, all_fields))
 
2175
          return;
 
2176
      }
 
2177
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2178
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2179
      curr_join->items1= items1;
 
2180
    }
 
2181
    curr_all_fields= &tmp_all_fields1;
 
2182
    curr_fields_list= &tmp_fields_list1;
 
2183
    curr_join->set_items_ref_array(items1);
 
2184
    
 
2185
    if (sort_and_group || curr_tmp_table->group)
 
2186
    {
 
2187
      curr_join->tmp_table_param.field_count+= 
 
2188
        curr_join->tmp_table_param.sum_func_count+
 
2189
        curr_join->tmp_table_param.func_count;
 
2190
      curr_join->tmp_table_param.sum_func_count= 
 
2191
        curr_join->tmp_table_param.func_count= 0;
 
2192
    }
 
2193
    else
 
2194
    {
 
2195
      curr_join->tmp_table_param.field_count+= 
 
2196
        curr_join->tmp_table_param.func_count;
 
2197
      curr_join->tmp_table_param.func_count= 0;
 
2198
    }
 
2199
    
 
2200
    if (curr_tmp_table->group)
 
2201
    {                                           // Already grouped
 
2202
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2203
        curr_join->order= curr_join->group_list;  /* order by group */
 
2204
      curr_join->group_list= 0;
 
2205
    }
 
2206
    
 
2207
    /*
 
2208
      If we have different sort & group then we must sort the data by group
 
2209
      and copy it to another tmp table
 
2210
      This code is also used if we are using distinct something
 
2211
      we haven't been able to store in the temporary table yet
 
2212
      like SEC_TO_TIME(SUM(...)).
 
2213
    */
 
2214
 
 
2215
    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))
 
2216
    {                                   /* Must copy to another table */
 
2217
      /* Free first data from old join */
 
2218
      curr_join->join_free();
 
2219
      if (make_simple_join(curr_join, curr_tmp_table))
 
2220
        return;
 
2221
      calc_group_buffer(curr_join, group_list);
 
2222
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2223
                        curr_join->tmp_all_fields1,
 
2224
                        curr_join->select_distinct && !curr_join->group_list);
 
2225
      curr_join->tmp_table_param.hidden_field_count= 
 
2226
        (curr_join->tmp_all_fields1.elements-
 
2227
         curr_join->tmp_fields_list1.elements);
 
2228
      
 
2229
      
 
2230
      if (exec_tmp_table2)
 
2231
        curr_tmp_table= exec_tmp_table2;
 
2232
      else
 
2233
      {
 
2234
        /* group data to new table */
 
2235
 
 
2236
        /*
 
2237
          If the access method is loose index scan then all MIN/MAX
 
2238
          functions are precomputed, and should be treated as regular
 
2239
          functions. See extended comment in JOIN::exec.
 
2240
        */
 
2241
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2242
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2243
 
 
2244
        if (!(curr_tmp_table=
 
2245
              exec_tmp_table2= create_tmp_table(thd,
 
2246
                                                &curr_join->tmp_table_param,
 
2247
                                                *curr_all_fields,
 
2248
                                                (order_st*) 0,
 
2249
                                                curr_join->select_distinct && 
 
2250
                                                !curr_join->group_list,
 
2251
                                                1, curr_join->select_options,
 
2252
                                                HA_POS_ERROR,
 
2253
                                                (char *) "")))
 
2254
          return;
 
2255
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2256
      }
 
2257
      if (curr_join->group_list)
 
2258
      {
 
2259
        thd->set_proc_info("Creating sort index");
 
2260
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2261
        {
 
2262
          return;
 
2263
        }
 
2264
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2265
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2266
            make_group_fields(this, curr_join))
 
2267
        {
 
2268
          return;
 
2269
        }
 
2270
        sortorder= curr_join->sortorder;
 
2271
      }
 
2272
      
 
2273
      thd->set_proc_info("Copying to group table");
 
2274
      tmp_error= -1;
 
2275
      if (curr_join != this)
 
2276
      {
 
2277
        if (sum_funcs2)
 
2278
        {
 
2279
          curr_join->sum_funcs= sum_funcs2;
 
2280
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2281
        }
 
2282
        else
 
2283
        {
 
2284
          curr_join->alloc_func_list();
 
2285
          sum_funcs2= curr_join->sum_funcs;
 
2286
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2287
        }
 
2288
      }
 
2289
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2290
                                        1, true))
 
2291
        return;
 
2292
      curr_join->group_list= 0;
 
2293
      if (!curr_join->sort_and_group &&
 
2294
          curr_join->const_tables != curr_join->tables)
 
2295
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2296
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2297
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2298
      {
 
2299
        error= tmp_error;
 
2300
        return;
 
2301
      }
 
2302
      end_read_record(&curr_join->join_tab->read_record);
 
2303
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2304
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2305
      
 
2306
      // No sum funcs anymore
 
2307
      if (!items2)
 
2308
      {
 
2309
        items2= items1 + all_fields.elements;
 
2310
        if (change_to_use_tmp_fields(thd, items2,
 
2311
                                     tmp_fields_list2, tmp_all_fields2, 
 
2312
                                     fields_list.elements, tmp_all_fields1))
 
2313
          return;
 
2314
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2315
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2316
      }
 
2317
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2318
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2319
      curr_join->set_items_ref_array(items2);
 
2320
      curr_join->tmp_table_param.field_count+= 
 
2321
        curr_join->tmp_table_param.sum_func_count;
 
2322
      curr_join->tmp_table_param.sum_func_count= 0;
 
2323
    }
 
2324
    if (curr_tmp_table->distinct)
 
2325
      curr_join->select_distinct=0;             /* Each row is unique */
 
2326
    
 
2327
    curr_join->join_free();                     /* Free quick selects */
 
2328
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2329
    {
 
2330
      thd->set_proc_info("Removing duplicates");
 
2331
      if (curr_join->tmp_having)
 
2332
        curr_join->tmp_having->update_used_tables();
 
2333
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2334
                            *curr_fields_list, curr_join->tmp_having))
 
2335
        return;
 
2336
      curr_join->tmp_having=0;
 
2337
      curr_join->select_distinct=0;
 
2338
    }
 
2339
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2340
    if (make_simple_join(curr_join, curr_tmp_table))
 
2341
      return;
 
2342
    calc_group_buffer(curr_join, curr_join->group_list);
 
2343
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2344
                      *curr_all_fields, 0);
 
2345
    
 
2346
  }
 
2347
  
 
2348
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2349
  {
 
2350
    if (make_group_fields(this, curr_join))
 
2351
    {
 
2352
      return;
 
2353
    }
 
2354
    if (!items3)
 
2355
    {
 
2356
      if (!items0)
 
2357
        init_items_ref_array();
 
2358
      items3= ref_pointer_array + (all_fields.elements*4);
 
2359
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2360
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2361
                        curr_fields_list->elements, *curr_all_fields);
 
2362
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2363
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2364
      tmp_table_param.save_copy_field_end=
 
2365
        curr_join->tmp_table_param.copy_field_end;
 
2366
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2367
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2368
    }
 
2369
    else
 
2370
    {
 
2371
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2372
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2373
      curr_join->tmp_table_param.copy_field_end=
 
2374
        tmp_table_param.save_copy_field_end;
 
2375
    }
 
2376
    curr_fields_list= &tmp_fields_list3;
 
2377
    curr_all_fields= &tmp_all_fields3;
 
2378
    curr_join->set_items_ref_array(items3);
 
2379
 
 
2380
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2381
                                      1, true) || 
 
2382
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2383
        thd->is_fatal_error)
 
2384
      return;
 
2385
  }
 
2386
  if (curr_join->group_list || curr_join->order)
 
2387
  {
 
2388
    thd->set_proc_info("Sorting result");
 
2389
    /* If we have already done the group, add HAVING to sorted table */
 
2390
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2391
        ! curr_join->sort_and_group)
 
2392
    {
 
2393
      // Some tables may have been const
 
2394
      curr_join->tmp_having->update_used_tables();
 
2395
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2396
      table_map used_tables= (curr_join->const_table_map |
 
2397
                              curr_table->table->map);
 
2398
 
 
2399
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2400
                                                 used_tables,
 
2401
                                                 used_tables, 0);
 
2402
      if (sort_table_cond)
 
2403
      {
 
2404
        if (!curr_table->select)
 
2405
          if (!(curr_table->select= new SQL_SELECT))
 
2406
            return;
 
2407
        if (!curr_table->select->cond)
 
2408
          curr_table->select->cond= sort_table_cond;
 
2409
        else                                    // This should never happen
 
2410
        {
 
2411
          if (!(curr_table->select->cond=
 
2412
                new Item_cond_and(curr_table->select->cond,
 
2413
                                  sort_table_cond)))
 
2414
            return;
 
2415
          /*
 
2416
            Item_cond_and do not need fix_fields for execution, its parameters
 
2417
            are fixed or do not need fix_fields, too
 
2418
          */
 
2419
          curr_table->select->cond->quick_fix_field();
 
2420
        }
 
2421
        curr_table->select_cond= curr_table->select->cond;
 
2422
        curr_table->select_cond->top_level_item();
 
2423
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2424
                                                   ~ (table_map) 0,
 
2425
                                                   ~used_tables, 0);
 
2426
      }
 
2427
    }
 
2428
    {
 
2429
      if (group)
 
2430
        curr_join->select_limit= HA_POS_ERROR;
 
2431
      else
 
2432
      {
 
2433
        /*
 
2434
          We can abort sorting after thd->select_limit rows if we there is no
 
2435
          WHERE clause for any tables after the sorted one.
 
2436
        */
 
2437
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2438
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2439
        for (; curr_table < end_table ; curr_table++)
 
2440
        {
 
2441
          /*
 
2442
            table->keyuse is set in the case there was an original WHERE clause
 
2443
            on the table that was optimized away.
 
2444
          */
 
2445
          if (curr_table->select_cond ||
 
2446
              (curr_table->keyuse && !curr_table->first_inner))
 
2447
          {
 
2448
            /* We have to sort all rows */
 
2449
            curr_join->select_limit= HA_POS_ERROR;
 
2450
            break;
 
2451
          }
 
2452
        }
 
2453
      }
 
2454
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2455
      {
 
2456
        return;
 
2457
      }
 
2458
      /*
 
2459
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
2460
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2461
        suitable index present.
 
2462
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2463
        finally replace sorting with index scan if there is a LIMIT clause in
 
2464
        the query. XXX: it's never shown in EXPLAIN!
 
2465
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2466
      */
 
2467
      if (create_sort_index(thd, curr_join,
 
2468
                            curr_join->group_list ? 
 
2469
                            curr_join->group_list : curr_join->order,
 
2470
                            curr_join->select_limit,
 
2471
                            (select_options & OPTION_FOUND_ROWS ?
 
2472
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2473
                            curr_join->group_list ? true : false))
 
2474
        return;
 
2475
      sortorder= curr_join->sortorder;
 
2476
      if (curr_join->const_tables != curr_join->tables &&
 
2477
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2478
      {
 
2479
        /*
 
2480
          If no IO cache exists for the first table then we are using an
 
2481
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2482
          attribute on the INDEX SCAN.
 
2483
        */
 
2484
        skip_sort_order= 1;
 
2485
      }
 
2486
    }
 
2487
  }
 
2488
  /* XXX: When can we have here thd->is_error() not zero? */
 
2489
  if (thd->is_error())
 
2490
  {
 
2491
    error= thd->is_error();
 
2492
    return;
 
2493
  }
 
2494
  curr_join->having= curr_join->tmp_having;
 
2495
  curr_join->fields= curr_fields_list;
 
2496
 
 
2497
  {
 
2498
    thd->set_proc_info("Sending data");
 
2499
    result->send_fields(*curr_fields_list,
 
2500
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2501
    error= do_select(curr_join, curr_fields_list, NULL);
 
2502
    thd->limit_found_rows= curr_join->send_records;
 
2503
  }
 
2504
 
 
2505
  /* Accumulate the counts from all join iterations of all join parts. */
 
2506
  thd->examined_row_count+= curr_join->examined_rows;
 
2507
 
 
2508
  /* 
 
2509
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2510
    for a derived table which is always materialized.
 
2511
    Otherwise we would not be able to print the query  correctly.
 
2512
  */ 
 
2513
  if (items0 &&
 
2514
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2515
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2516
    set_items_ref_array(items0);
 
2517
 
 
2518
  return;
 
2519
}
 
2520
 
 
2521
 
 
2522
/**
 
2523
  Clean up join.
 
2524
 
 
2525
  @return
 
2526
    Return error that hold JOIN.
 
2527
*/
 
2528
 
 
2529
int
 
2530
JOIN::destroy()
 
2531
{
 
2532
  select_lex->join= 0;
 
2533
 
 
2534
  if (tmp_join)
 
2535
  {
 
2536
    if (join_tab != tmp_join->join_tab)
 
2537
    {
 
2538
      JOIN_TAB *tab, *end;
 
2539
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2540
        tab->cleanup();
 
2541
    }
 
2542
    tmp_join->tmp_join= 0;
 
2543
    tmp_table_param.copy_field=0;
 
2544
    return(tmp_join->destroy());
 
2545
  }
 
2546
  cond_equal= 0;
 
2547
 
 
2548
  cleanup(1);
 
2549
  if (exec_tmp_table1)
 
2550
    exec_tmp_table1->free_tmp_table(thd);
 
2551
  if (exec_tmp_table2)
 
2552
    exec_tmp_table2->free_tmp_table(thd);
 
2553
  delete select;
 
2554
  delete_dynamic(&keyuse);
 
2555
  return(error);
 
2556
}
 
2557
 
 
2558
 
 
2559
 
309
2560
/**
310
2561
  An entry point to single-unit select (a select without UNION).
311
2562
 
312
 
  @param session                  thread Cursor
 
2563
  @param thd                  thread handler
313
2564
  @param rref_pointer_array   a reference to ref_pointer_array of
314
2565
                              the top-level select_lex for this query
315
2566
  @param tables               list of all tables used in this query.
316
2567
                              The tables have been pre-opened.
317
 
  @param wild_num             number of wildcards used in the top level
 
2568
  @param wild_num             number of wildcards used in the top level 
318
2569
                              select of this query.
319
2570
                              For example statement
320
2571
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
325
2576
                              for a, b and c in this list.
326
2577
  @param conds                top level item of an expression representing
327
2578
                              WHERE clause of the top level select
328
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
2579
  @param og_num               total number of order_st BY and GROUP BY clauses
329
2580
                              arguments
330
 
  @param order                linked list of ORDER BY agruments
 
2581
  @param order                linked list of order_st BY agruments
331
2582
  @param group                linked list of GROUP BY arguments
332
2583
  @param having               top level item of HAVING expression
 
2584
  @param proc_param           list of PROCEDUREs
333
2585
  @param select_options       select options (BIG_RESULT, etc)
334
2586
  @param result               an instance of result set handling class.
335
2587
                              This object is responsible for send result
336
2588
                              set rows to the client or inserting them
337
2589
                              into a table.
338
 
  @param select_lex           the only Select_Lex of this query
 
2590
  @param select_lex           the only SELECT_LEX of this query
339
2591
  @param unit                 top-level UNIT of this query
340
2592
                              UNIT is an artificial object created by the
341
2593
                              parser for every SELECT clause.
348
2600
  @retval
349
2601
    true   an error
350
2602
*/
351
 
bool mysql_select(Session *session,
352
 
                  Item ***rref_pointer_array,
353
 
                  TableList *tables, 
354
 
                  uint32_t wild_num, 
355
 
                  List<Item> &fields,
356
 
                  COND *conds, 
357
 
                  uint32_t og_num,  
358
 
                  Order *order,
359
 
                  Order *group,
360
 
                  Item *having, 
361
 
                  uint64_t select_options,
362
 
                  select_result *result, 
363
 
                  Select_Lex_Unit *unit,
364
 
                  Select_Lex *select_lex)
 
2603
 
 
2604
bool
 
2605
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2606
             TableList *tables, uint32_t wild_num, List<Item> &fields,
 
2607
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
 
2608
             Item *having, order_st *proc_param, uint64_t select_options,
 
2609
             select_result *result, SELECT_LEX_UNIT *unit,
 
2610
             SELECT_LEX *select_lex)
365
2611
{
366
2612
  bool err;
367
2613
  bool free_join= 1;
368
2614
 
369
2615
  select_lex->context.resolve_in_select_list= true;
370
 
  Join *join;
 
2616
  JOIN *join;
371
2617
  if (select_lex->join != 0)
372
2618
  {
373
2619
    join= select_lex->join;
376
2622
      creation
377
2623
    */
378
2624
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
379
 
        (select_options & SELECT_DESCRIBE))
 
2625
        (select_options & SELECT_DESCRIBE))
380
2626
    {
381
2627
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
382
2628
      {
383
 
        //here is EXPLAIN of subselect or derived table
384
 
        if (join->change_result(result))
385
 
        {
386
 
          return(true);
387
 
        }
 
2629
        //here is EXPLAIN of subselect or derived table
 
2630
        if (join->change_result(result))
 
2631
        {
 
2632
          return(true);
 
2633
        }
388
2634
      }
389
2635
      else
390
2636
      {
391
2637
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
392
 
                               conds, og_num, order, group, having, select_lex, unit)))
393
 
        {
394
 
          goto err;
395
 
        }
 
2638
                               conds, og_num, order, group, having, proc_param,
 
2639
                               select_lex, unit)))
 
2640
        {
 
2641
          goto err;
 
2642
        }
396
2643
      }
397
2644
    }
398
2645
    free_join= 0;
400
2647
  }
401
2648
  else
402
2649
  {
403
 
    if (!(join= new Join(session, fields, select_options, result)))
404
 
      return(true);
405
 
    session->set_proc_info("init");
406
 
    session->used_tables=0;                         // Updated by setup_fields
 
2650
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2651
        return(true);
 
2652
    thd->set_proc_info("init");
 
2653
    thd->used_tables=0;                         // Updated by setup_fields
407
2654
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
408
 
                           conds, og_num, order, group, having,
 
2655
                           conds, og_num, order, group, having, proc_param,
409
2656
                           select_lex, unit)) == true)
410
2657
    {
411
2658
      goto err;
412
2659
    }
413
2660
  }
414
2661
 
415
 
  err= join->optimize();
416
 
  if (err)
417
 
  {
418
 
    goto err; // 1
419
 
  }
420
 
 
421
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2662
  /* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
 
2663
  if (join->flatten_subqueries())
 
2664
  {
 
2665
    err= 1;
 
2666
    goto err;
 
2667
  }
 
2668
  /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
2669
 
 
2670
  if ((err= join->optimize()))
 
2671
  {
 
2672
    goto err;                                   // 1
 
2673
  }
 
2674
 
 
2675
  if (thd->lex->describe & DESCRIBE_EXTENDED)
422
2676
  {
423
2677
    join->conds_history= join->conds;
424
2678
    join->having_history= (join->having?join->having:join->tmp_having);
425
2679
  }
426
2680
 
427
 
  if (session->is_error())
 
2681
  if (thd->is_error())
428
2682
    goto err;
429
2683
 
430
2684
  join->exec();
431
2685
 
432
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2686
  if (thd->lex->describe & DESCRIBE_EXTENDED)
433
2687
  {
434
2688
    select_lex->where= join->conds_history;
435
2689
    select_lex->having= join->having_history;
438
2692
err:
439
2693
  if (free_join)
440
2694
  {
441
 
    session->set_proc_info("end");
 
2695
    thd->set_proc_info("end");
442
2696
    err|= select_lex->cleanup();
443
 
    return(err || session->is_error());
 
2697
    return(err || thd->is_error());
444
2698
  }
445
2699
  return(join->error);
446
2700
}
447
2701
 
448
 
inline Item *and_items(Item* cond, Item *item)
 
2702
 
 
2703
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2704
                          Item_in_subselect* const *el2)
 
2705
{
 
2706
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2707
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2708
}
 
2709
 
 
2710
 
 
2711
inline Item * and_items(Item* cond, Item *item)
449
2712
{
450
2713
  return (cond? (new Item_cond_and(cond, item)) : item);
451
2714
}
452
2715
 
 
2716
 
 
2717
static TableList *alloc_join_nest(THD *thd)
 
2718
{
 
2719
  TableList *tbl;
 
2720
  if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2721
                                       sizeof(nested_join_st))))
 
2722
    return NULL;
 
2723
  tbl->nested_join= (nested_join_st*) ((unsigned char*)tbl + 
 
2724
                                    ALIGN_SIZE(sizeof(TableList)));
 
2725
  return tbl;
 
2726
}
 
2727
 
 
2728
 
 
2729
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
 
2730
{
 
2731
  List_iterator<TableList> it(*tlist);
 
2732
  TableList *table;
 
2733
  while ((table= it++))
 
2734
  {
 
2735
    if (table->on_expr)
 
2736
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
2737
    if (table->nested_join)
 
2738
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
2739
  }
 
2740
}
 
2741
 
 
2742
 
 
2743
/*
 
2744
  Convert a subquery predicate into a TableList semi-join nest
 
2745
 
 
2746
  SYNOPSIS
 
2747
    convert_subq_to_sj()
 
2748
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2749
                    clause
 
2750
       subq_pred    Subquery predicate to be converted
 
2751
  
 
2752
  DESCRIPTION
 
2753
    Convert a subquery predicate into a TableList semi-join nest. All the 
 
2754
    prerequisites are already checked, so the conversion is always successfull.
 
2755
 
 
2756
    Prepared Statements: the transformation is permanent:
 
2757
     - Changes in TableList structures are naturally permanent
 
2758
     - Item tree changes are performed on statement MEM_ROOT:
 
2759
        = we activate statement MEM_ROOT 
 
2760
        = this function is called before the first fix_prepare_information
 
2761
          call.
 
2762
 
 
2763
    This is intended because the criteria for subquery-to-sj conversion remain
 
2764
    constant for the lifetime of the Prepared Statement.
 
2765
 
 
2766
  RETURN
 
2767
    false  OK
 
2768
    true   Out of memory error
 
2769
*/
 
2770
 
 
2771
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2772
{
 
2773
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2774
  TableList *emb_tbl_nest= NULL;
 
2775
  List<TableList> *emb_join_list= &parent_lex->top_join_list;
 
2776
  THD *thd= parent_join->thd;
 
2777
 
 
2778
  /*
 
2779
    1. Find out where to put the predicate into.
 
2780
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2781
  */
 
2782
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2783
  {
 
2784
    if (subq_pred->expr_join_nest->nested_join)
 
2785
    {
 
2786
      /*
 
2787
        We're dealing with
 
2788
 
 
2789
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2790
 
 
2791
        The sj-nest will be inserted into the brackets nest.
 
2792
      */
 
2793
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2794
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2795
    }
 
2796
    else if (!subq_pred->expr_join_nest->outer_join)
 
2797
    {
 
2798
      /*
 
2799
        We're dealing with
 
2800
 
 
2801
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2802
 
 
2803
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2804
        parent. This is ok because tblX is joined as an inner join.
 
2805
      */
 
2806
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2807
      if (emb_tbl_nest)
 
2808
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2809
    }
 
2810
    else if (!subq_pred->expr_join_nest->nested_join)
 
2811
    {
 
2812
      TableList *outer_tbl= subq_pred->expr_join_nest;      
 
2813
      TableList *wrap_nest;
 
2814
      /*
 
2815
        We're dealing with
 
2816
 
 
2817
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2818
 
 
2819
        we'll need to convert it into:
 
2820
 
 
2821
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2822
                        |                      |
 
2823
                        |<----- wrap_nest ---->|
 
2824
        
 
2825
        Q:  other subqueries may be pointing to this element. What to do?
 
2826
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2827
            But we'll need to fix other pointers.
 
2828
        A2: Another way: have TableList::next_ptr so the following
 
2829
            subqueries know the table has been nested.
 
2830
        A3: changes in the TableList::outer_join will make everything work
 
2831
            automatically.
 
2832
      */
 
2833
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2834
      {
 
2835
        return(true);
 
2836
      }
 
2837
      wrap_nest->embedding= outer_tbl->embedding;
 
2838
      wrap_nest->join_list= outer_tbl->join_list;
 
2839
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2840
 
 
2841
      wrap_nest->nested_join->join_list.empty();
 
2842
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2843
 
 
2844
      outer_tbl->embedding= wrap_nest;
 
2845
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2846
 
 
2847
      /*
 
2848
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2849
        and on_expr
 
2850
      */
 
2851
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2852
      outer_tbl->outer_join= 0;
 
2853
 
 
2854
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2855
      outer_tbl->on_expr= NULL;
 
2856
 
 
2857
      List_iterator<TableList> li(*wrap_nest->join_list);
 
2858
      TableList *tbl;
 
2859
      while ((tbl= li++))
 
2860
      {
 
2861
        if (tbl == outer_tbl)
 
2862
        {
 
2863
          li.replace(wrap_nest);
 
2864
          break;
 
2865
        }
 
2866
      }
 
2867
      /*
 
2868
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2869
        semi-join nest into it
 
2870
      */
 
2871
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2872
      emb_tbl_nest=  wrap_nest;
 
2873
    }
 
2874
  }
 
2875
 
 
2876
  TableList *sj_nest;
 
2877
  nested_join_st *nested_join;
 
2878
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2879
  {
 
2880
    return(true);
 
2881
  }
 
2882
  nested_join= sj_nest->nested_join;
 
2883
 
 
2884
  sj_nest->join_list= emb_join_list;
 
2885
  sj_nest->embedding= emb_tbl_nest;
 
2886
  sj_nest->alias= (char*) "(sj-nest)";
 
2887
  /* Nests do not participate in those 'chains', so: */
 
2888
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2889
  emb_join_list->push_back(sj_nest);
 
2890
 
 
2891
  /* 
 
2892
    nested_join->used_tables and nested_join->not_null_tables are
 
2893
    initialized in simplify_joins().
 
2894
  */
 
2895
  
 
2896
  /* 
 
2897
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2898
       sj-nest.
 
2899
  */
 
2900
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2901
  nested_join->join_list.empty();
 
2902
  List_iterator_fast<TableList> li(subq_lex->top_join_list);
 
2903
  TableList *tl, *last_leaf;
 
2904
  while ((tl= li++))
 
2905
  {
 
2906
    tl->embedding= sj_nest;
 
2907
    tl->join_list= &nested_join->join_list;
 
2908
    nested_join->join_list.push_back(tl);
 
2909
  }
 
2910
  
 
2911
  /*
 
2912
    Reconnect the next_leaf chain.
 
2913
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2914
          Inserting them at the beginning would be a bit faster.
 
2915
    NOTE: We actually insert them at the front! That's because the order is
 
2916
          reversed in this list.
 
2917
  */
 
2918
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2919
  tl->next_leaf= subq_lex->leaf_tables;
 
2920
  last_leaf= tl;
 
2921
 
 
2922
  /*
 
2923
    Same as above for next_local chain
 
2924
    (a theory: a next_local chain always starts with ::leaf_tables
 
2925
     because view's tables are inserted after the view)
 
2926
  */
 
2927
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2928
  tl->next_local= subq_lex->leaf_tables;
 
2929
 
 
2930
  /* A theory: no need to re-connect the next_global chain */
 
2931
 
 
2932
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2933
 
 
2934
  // The subqueries were replaced for Item_int(1) earlier
 
2935
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2936
  /*TODO: also reset the 'with_subselect' there. */
 
2937
 
 
2938
  /* n. Adjust the parent_join->tables counter */
 
2939
  uint32_t table_no= parent_join->tables;
 
2940
  /* n. Walk through child's tables and adjust table->map */
 
2941
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2942
  {
 
2943
    tl->table->tablenr= table_no;
 
2944
    tl->table->map= ((table_map)1) << table_no;
 
2945
    SELECT_LEX *old_sl= tl->select_lex;
 
2946
    tl->select_lex= parent_join->select_lex; 
 
2947
    for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2948
      emb->select_lex= parent_join->select_lex;
 
2949
  }
 
2950
  parent_join->tables += subq_lex->join->tables;
 
2951
 
 
2952
  /* 
 
2953
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2954
    Add the subquery-induced equalities too.
 
2955
  */
 
2956
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2957
  thd->lex->current_select=subq_lex;
 
2958
  if (!subq_pred->left_expr->fixed &&
 
2959
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2960
    return(true);
 
2961
  thd->lex->current_select=save_lex;
 
2962
 
 
2963
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2964
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2965
                                        subq_pred->left_expr->used_tables();
 
2966
  sj_nest->sj_on_expr= subq_lex->where;
 
2967
 
 
2968
  /*
 
2969
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2970
    Additionally, for InsideOut strategy
 
2971
     - Record the number of IN-equalities.
 
2972
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2973
       see which of the expressions are bound and which are not (for those
 
2974
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2975
 
 
2976
       (TODO: can we just create a list of pointers and hope the expressions
 
2977
       will not substitute themselves on fix_fields()? or we need to wrap
 
2978
       them into Item_direct_view_refs and store pointers to those. The
 
2979
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
2980
       Item_direct_view_refs doesn't substitute itself with anything in 
 
2981
       Item_direct_view_ref::fix_fields.
 
2982
  */
 
2983
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
2984
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
2985
 
 
2986
  if (subq_pred->left_expr->cols() == 1)
 
2987
  {
 
2988
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
2989
 
 
2990
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
2991
                                    subq_lex->ref_pointer_array[0]);
 
2992
    item_eq->name= (char*)subq_sj_cond_name;
 
2993
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
2994
  }
 
2995
  else
 
2996
  {
 
2997
    for (uint32_t i= 0; i < subq_pred->left_expr->cols(); i++)
 
2998
    {
 
2999
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3000
                                                element_index(i));
 
3001
      Item *item_eq= 
 
3002
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3003
                         subq_lex->ref_pointer_array[i]);
 
3004
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3005
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3006
    }
 
3007
  }
 
3008
  /* Fix the created equality and AND */
 
3009
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3010
 
 
3011
  /*
 
3012
    Walk through sj nest's WHERE and ON expressions and call
 
3013
    item->fix_table_changes() for all items.
 
3014
  */
 
3015
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3016
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3017
 
 
3018
 
 
3019
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3020
  subq_lex->master_unit()->exclude_level();
 
3021
 
 
3022
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3023
  if (emb_tbl_nest)
 
3024
  {
 
3025
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3026
                                     sj_nest->sj_on_expr);
 
3027
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3028
  }
 
3029
  else
 
3030
  {
 
3031
    /* Inject into the WHERE */
 
3032
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3033
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3034
    parent_join->select_lex->where= parent_join->conds;
 
3035
  }
 
3036
 
 
3037
  return(false);
 
3038
}
 
3039
 
 
3040
 
 
3041
/*
 
3042
  Convert candidate subquery predicates to semi-joins
 
3043
 
 
3044
  SYNOPSIS
 
3045
    JOIN::flatten_subqueries()
 
3046
 
 
3047
  DESCRIPTION
 
3048
    Convert candidate subquery predicates to semi-joins.
 
3049
 
 
3050
  RETURN 
 
3051
    false  OK
 
3052
    true   Error
 
3053
*/
 
3054
 
 
3055
bool JOIN::flatten_subqueries()
 
3056
{
 
3057
  Item_in_subselect **in_subq;
 
3058
  Item_in_subselect **in_subq_end;
 
3059
 
 
3060
  if (sj_subselects.elements() == 0)
 
3061
    return(false);
 
3062
 
 
3063
  /* 1. Fix children subqueries */
 
3064
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3065
       in_subq != in_subq_end; in_subq++)
 
3066
  {
 
3067
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3068
    child_join->outer_tables = child_join->tables;
 
3069
    if (child_join->flatten_subqueries())
 
3070
      return(true);
 
3071
    (*in_subq)->sj_convert_priority= 
 
3072
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3073
  }
 
3074
 
 
3075
  //dump_TableList_struct(select_lex, select_lex->leaf_tables);
 
3076
  /* 
 
3077
    2. Pick which subqueries to convert:
 
3078
      sort the subquery array
 
3079
      - prefer correlated subqueries over uncorrelated;
 
3080
      - prefer subqueries that have greater number of outer tables;
 
3081
  */
 
3082
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3083
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3084
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3085
  for (in_subq= sj_subselects.front(); 
 
3086
       in_subq != in_subq_end && 
 
3087
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3088
       in_subq++)
 
3089
  {
 
3090
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3091
      return(true);
 
3092
  }
 
3093
 
 
3094
  for (in_subq= sj_subselects.front(); 
 
3095
       in_subq != in_subq_end && 
 
3096
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3097
       in_subq++)
 
3098
  {
 
3099
    if (convert_subq_to_sj(this, *in_subq))
 
3100
      return(true);
 
3101
  }
 
3102
 
 
3103
  /* 3. Finalize those we didn't convert */
 
3104
  for (; in_subq!= in_subq_end; in_subq++)
 
3105
  {
 
3106
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3107
    Item_subselect::trans_res res;
 
3108
    (*in_subq)->changed= 0;
 
3109
    (*in_subq)->fixed= 0;
 
3110
    res= (*in_subq)->select_transformer(child_join);
 
3111
    if (res == Item_subselect::RES_ERROR)
 
3112
      return(true);
 
3113
 
 
3114
    (*in_subq)->changed= 1;
 
3115
    (*in_subq)->fixed= 1;
 
3116
 
 
3117
    Item *substitute= (*in_subq)->substitution;
 
3118
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3119
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3120
      return(true);
 
3121
 
 
3122
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3123
    //  return(true);
 
3124
  }
 
3125
  sj_subselects.clear();
 
3126
  return(false);
 
3127
}
 
3128
 
 
3129
 
 
3130
/**
 
3131
  Setup for execution all subqueries of a query, for which the optimizer
 
3132
  chose hash semi-join.
 
3133
 
 
3134
  @details Iterate over all subqueries of the query, and if they are under an
 
3135
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3136
  - try to initialize all data structures needed for the materialized execution
 
3137
    of the IN predicate,
 
3138
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3139
    previously blocked during JOIN::prepare.
 
3140
 
 
3141
  This method is part of the "code generation" query processing phase.
 
3142
 
 
3143
  This phase must be called after substitute_for_best_equal_field() because
 
3144
  that function may replace items with other items from a multiple equality,
 
3145
  and we need to reference the correct items in the index access method of the
 
3146
  IN predicate.
 
3147
 
 
3148
  @return Operation status
 
3149
  @retval false     success.
 
3150
  @retval true      error occurred.
 
3151
*/
 
3152
 
 
3153
bool JOIN::setup_subquery_materialization()
 
3154
{
 
3155
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3156
       un= un->next_unit())
 
3157
  {
 
3158
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3159
    {
 
3160
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3161
      if (subquery_predicate &&
 
3162
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3163
      {
 
3164
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3165
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3166
            in_subs->setup_engine())
 
3167
          return true;
 
3168
      }
 
3169
    }
 
3170
  }
 
3171
  return false;
 
3172
}
 
3173
 
 
3174
 
 
3175
/*
 
3176
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3177
 
 
3178
  SYNOPSIS
 
3179
    find_eq_ref_candidate()
 
3180
      table             Table to be checked
 
3181
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3182
                        count.
 
3183
 
 
3184
  DESCRIPTION
 
3185
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3186
 
 
3187
  TODO
 
3188
    Check again if it is feasible to factor common parts with constant table
 
3189
    search
 
3190
 
 
3191
  RETURN
 
3192
    true  - There exists an eq_ref(outer-tables) candidate
 
3193
    false - Otherwise
 
3194
*/
 
3195
 
 
3196
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
 
3197
{
 
3198
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3199
  uint32_t key;
 
3200
 
 
3201
  if (keyuse)
 
3202
  {
 
3203
    while (1) /* For each key */
 
3204
    {
 
3205
      key= keyuse->key;
 
3206
      KEY *keyinfo= table->key_info + key;
 
3207
      key_part_map bound_parts= 0;
 
3208
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3209
      {
 
3210
        do  /* For all equalities on all key parts */
 
3211
        {
 
3212
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3213
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3214
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3215
          {
 
3216
            bound_parts |= 1 << keyuse->keypart;
 
3217
          }
 
3218
          keyuse++;
 
3219
        } while (keyuse->key == key && keyuse->table == table);
 
3220
 
 
3221
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3222
          return true;
 
3223
        if (keyuse->table != table)
 
3224
          return false;
 
3225
      }
 
3226
      else
 
3227
      {
 
3228
        do
 
3229
        {
 
3230
          keyuse++;
 
3231
          if (keyuse->table != table)
 
3232
            return false;
 
3233
        }
 
3234
        while (keyuse->key == key);
 
3235
      }
 
3236
    }
 
3237
  }
 
3238
  return false;
 
3239
}
 
3240
 
 
3241
 
 
3242
/*
 
3243
  Pull tables out of semi-join nests, if possible
 
3244
 
 
3245
  SYNOPSIS
 
3246
    pull_out_semijoin_tables()
 
3247
      join  The join where to do the semi-join flattening
 
3248
 
 
3249
  DESCRIPTION
 
3250
    Try to pull tables out of semi-join nests.
 
3251
     
 
3252
    PRECONDITIONS
 
3253
    When this function is called, the join may have several semi-join nests
 
3254
    (possibly within different semi-join nests), but it is guaranteed that
 
3255
    one semi-join nest does not contain another.
 
3256
   
 
3257
    ACTION
 
3258
    A table can be pulled out of the semi-join nest if
 
3259
     - It is a constant table
 
3260
     - It is accessed 
 
3261
 
 
3262
    POSTCONDITIONS
 
3263
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3264
       tables)
 
3265
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3266
     * Semi-join nests TableList::sj_inner_tables
 
3267
 
 
3268
    This operation is (and should be) performed at each PS execution since
 
3269
    tables may become/cease to be constant across PS reexecutions.
 
3270
 
 
3271
  RETURN 
 
3272
    0 - OK
 
3273
    1 - Out of memory error
 
3274
*/
 
3275
 
 
3276
int pull_out_semijoin_tables(JOIN *join)
 
3277
{
 
3278
  TableList *sj_nest;
 
3279
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
3280
   
 
3281
  /* Try pulling out of the each of the semi-joins */
 
3282
  while ((sj_nest= sj_list_it++))
 
3283
  {
 
3284
    /* Action #1: Mark the constant tables to be pulled out */
 
3285
    table_map pulled_tables= 0;
 
3286
     
 
3287
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
3288
    TableList *tbl;
 
3289
    while ((tbl= child_li++))
 
3290
    {
 
3291
      if (tbl->table)
 
3292
      {
 
3293
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3294
        if (tbl->table->map & join->const_table_map)
 
3295
        {
 
3296
          pulled_tables |= tbl->table->map;
 
3297
        }
 
3298
      }
 
3299
    }
 
3300
    
 
3301
    /*
 
3302
      Action #2: Find which tables we can pull out based on
 
3303
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3304
      us to pull out some other tables too.
 
3305
    */
 
3306
    bool pulled_a_table;
 
3307
    do 
 
3308
    {
 
3309
      pulled_a_table= false;
 
3310
      child_li.rewind();
 
3311
      while ((tbl= child_li++))
 
3312
      {
 
3313
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3314
        {
 
3315
          if (find_eq_ref_candidate(tbl->table, 
 
3316
                                    sj_nest->nested_join->used_tables & 
 
3317
                                    ~pulled_tables))
 
3318
          {
 
3319
            pulled_a_table= true;
 
3320
            pulled_tables |= tbl->table->map;
 
3321
          }
 
3322
        }
 
3323
      }
 
3324
    } while (pulled_a_table);
 
3325
 
 
3326
    child_li.rewind();
 
3327
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3328
    {
 
3329
      (sj_nest)->sj_inner_tables= 0;
 
3330
      while ((tbl= child_li++))
 
3331
      {
 
3332
        if (tbl->table)
 
3333
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3334
      }
 
3335
    }
 
3336
    else
 
3337
    {
 
3338
      /* Record the bitmap of inner tables, mark the inner tables */
 
3339
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3340
                             ~pulled_tables;
 
3341
      (sj_nest)->sj_inner_tables= inner_tables;
 
3342
      while ((tbl= child_li++))
 
3343
      {
 
3344
        if (tbl->table)
 
3345
        {
 
3346
          if (inner_tables & tbl->table->map)
 
3347
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3348
          else
 
3349
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3350
        }
 
3351
      }
 
3352
    }
 
3353
  }
 
3354
  return(0);
 
3355
}
 
3356
 
453
3357
/*****************************************************************************
454
 
  Create JoinTableS, make a guess about the table types,
 
3358
  Create JOIN_TABS, make a guess about the table types,
455
3359
  Approximate how many records will be used in each table
456
3360
*****************************************************************************/
457
 
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
 
3361
 
 
3362
 
 
3363
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3364
                                      Table *table,
 
3365
                                      const key_map *keys,ha_rows limit)
458
3366
{
459
3367
  int error;
460
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3368
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
461
3369
    return(0);                           // Fatal error flag is set
462
3370
  if (select)
463
3371
  {
464
3372
    select->head=table;
465
3373
    table->reginfo.impossible_range=0;
466
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3374
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
467
3375
                                          limit, 0, false)) == 1)
468
3376
      return(select->quick->records);
469
3377
    if (error == -1)
475
3383
  return(HA_POS_ERROR);                 /* This shouldn't happend */
476
3384
}
477
3385
 
 
3386
/*
 
3387
   This structure is used to collect info on potentially sargable
 
3388
   predicates in order to check whether they become sargable after
 
3389
   reading const tables.
 
3390
   We form a bitmap of indexes that can be used for sargable predicates.
 
3391
   Only such indexes are involved in range analysis.
 
3392
*/
 
3393
typedef struct st_sargable_param
 
3394
{
 
3395
  Field *field;              /* field against which to check sargability */
 
3396
  Item **arg_value;          /* values of potential keys for lookups     */
 
3397
  uint32_t num_values;           /* number of values in the above array      */
 
3398
} SARGABLE_PARAM;  
 
3399
 
 
3400
/**
 
3401
  Calculate the best possible join and initialize the join structure.
 
3402
 
 
3403
  @retval
 
3404
    0   ok
 
3405
  @retval
 
3406
    1   Fatal error
 
3407
*/
 
3408
 
 
3409
static bool
 
3410
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
 
3411
                     DYNAMIC_ARRAY *keyuse_array)
 
3412
{
 
3413
  int error;
 
3414
  Table *table;
 
3415
  uint32_t i,table_count,const_count,key;
 
3416
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3417
  key_map const_ref, eq_part;
 
3418
  Table **table_vector;
 
3419
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3420
  KEYUSE *keyuse,*start_keyuse;
 
3421
  table_map outer_join=0;
 
3422
  SARGABLE_PARAM *sargables= 0;
 
3423
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3424
 
 
3425
  table_count=join->tables;
 
3426
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3427
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3428
  table_vector=(Table**) join->thd->alloc(sizeof(Table*)*(table_count*2));
 
3429
  if (!stat || !stat_ref || !table_vector)
 
3430
    return(1);                          // Eom /* purecov: inspected */
 
3431
 
 
3432
  join->best_ref=stat_vector;
 
3433
 
 
3434
  stat_end=stat+table_count;
 
3435
  found_const_table_map= all_table_map=0;
 
3436
  const_count=0;
 
3437
 
 
3438
  for (s= stat, i= 0;
 
3439
       tables;
 
3440
       s++, tables= tables->next_leaf, i++)
 
3441
  {
 
3442
    TableList *embedding= tables->embedding;
 
3443
    stat_vector[i]=s;
 
3444
    s->keys.init();
 
3445
    s->const_keys.init();
 
3446
    s->checked_keys.init();
 
3447
    s->needed_reg.init();
 
3448
    table_vector[i]=s->table=table=tables->table;
 
3449
    table->pos_in_table_list= tables;
 
3450
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3451
    if(error)
 
3452
    {
 
3453
        table->file->print_error(error, MYF(0));
 
3454
        return(1);
 
3455
    }
 
3456
    table->quick_keys.clear_all();
 
3457
    table->reginfo.join_tab=s;
 
3458
    table->reginfo.not_exists_optimize=0;
 
3459
    memset(table->const_key_parts, 0,
 
3460
           sizeof(key_part_map)*table->s->keys);
 
3461
    all_table_map|= table->map;
 
3462
    s->join=join;
 
3463
    s->info=0;                                  // For describe
 
3464
 
 
3465
    s->dependent= tables->dep_tables;
 
3466
    s->key_dependent= 0;
 
3467
    if (tables->schema_table)
 
3468
      table->file->stats.records= 2;
 
3469
    table->quick_condition_rows= table->file->stats.records;
 
3470
 
 
3471
    s->on_expr_ref= &tables->on_expr;
 
3472
    if (*s->on_expr_ref)
 
3473
    {
 
3474
      /* s is the only inner table of an outer join */
 
3475
      if (!table->file->stats.records && !embedding)
 
3476
      {                                         // Empty table
 
3477
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3478
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3479
        continue;
 
3480
      }
 
3481
      outer_join|= table->map;
 
3482
      s->embedding_map= 0;
 
3483
      for (;embedding; embedding= embedding->embedding)
 
3484
        s->embedding_map|= embedding->nested_join->nj_map;
 
3485
      continue;
 
3486
    }
 
3487
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3488
    {
 
3489
      /* s belongs to a nested join, maybe to several embedded joins */
 
3490
      s->embedding_map= 0;
 
3491
      do
 
3492
      {
 
3493
        nested_join_st *nested_join= embedding->nested_join;
 
3494
        s->embedding_map|=nested_join->nj_map;
 
3495
        s->dependent|= embedding->dep_tables;
 
3496
        embedding= embedding->embedding;
 
3497
        outer_join|= nested_join->used_tables;
 
3498
      }
 
3499
      while (embedding);
 
3500
      continue;
 
3501
    }
 
3502
    if ((table->file->stats.records <= 1) &&
 
3503
        !s->dependent &&
 
3504
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3505
    {
 
3506
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3507
    }
 
3508
  }
 
3509
  stat_vector[i]=0;
 
3510
  join->outer_join=outer_join;
 
3511
 
 
3512
  if (join->outer_join)
 
3513
  {
 
3514
    /* 
 
3515
       Build transitive closure for relation 'to be dependent on'.
 
3516
       This will speed up the plan search for many cases with outer joins,
 
3517
       as well as allow us to catch illegal cross references/
 
3518
       Warshall's algorithm is used to build the transitive closure.
 
3519
       As we use bitmaps to represent the relation the complexity
 
3520
       of the algorithm is O((number of tables)^2). 
 
3521
    */
 
3522
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3523
    {
 
3524
      for (uint32_t j= 0 ; j < table_count ; j++)
 
3525
      {
 
3526
        table= stat[j].table;
 
3527
        if (s->dependent & table->map)
 
3528
          s->dependent |= table->reginfo.join_tab->dependent;
 
3529
      }
 
3530
      if (s->dependent)
 
3531
        s->table->maybe_null= 1;
 
3532
    }
 
3533
    /* Catch illegal cross references for outer joins */
 
3534
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3535
    {
 
3536
      if (s->dependent & s->table->map)
 
3537
      {
 
3538
        join->tables=0;                 // Don't use join->table
 
3539
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3540
        return(1);
 
3541
      }
 
3542
      s->key_dependent= s->dependent;
 
3543
    }
 
3544
  }
 
3545
 
 
3546
  if (conds || outer_join)
 
3547
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3548
                            conds, join->cond_equal,
 
3549
                            ~outer_join, join->select_lex, &sargables))
 
3550
      return(1);
 
3551
 
 
3552
  /* Read tables with 0 or 1 rows (system tables) */
 
3553
  join->const_table_map= 0;
 
3554
 
 
3555
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3556
       p_pos < p_end ;
 
3557
       p_pos++)
 
3558
  {
 
3559
    int tmp;
 
3560
    s= p_pos->table;
 
3561
    s->type=JT_SYSTEM;
 
3562
    join->const_table_map|=s->table->map;
 
3563
    if ((tmp=join_read_const_table(s, p_pos)))
 
3564
    {
 
3565
      if (tmp > 0)
 
3566
        return(1);                      // Fatal error
 
3567
    }
 
3568
    else
 
3569
      found_const_table_map|= s->table->map;
 
3570
  }
 
3571
 
 
3572
  /* loop until no more const tables are found */
 
3573
  int ref_changed;
 
3574
  do
 
3575
  {
 
3576
  more_const_tables_found:
 
3577
    ref_changed = 0;
 
3578
    found_ref=0;
 
3579
 
 
3580
    /*
 
3581
      We only have to loop from stat_vector + const_count as
 
3582
      set_position() will move all const_tables first in stat_vector
 
3583
    */
 
3584
 
 
3585
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3586
    {
 
3587
      table=s->table;
 
3588
 
 
3589
      /* 
 
3590
        If equi-join condition by a key is null rejecting and after a
 
3591
        substitution of a const table the key value happens to be null
 
3592
        then we can state that there are no matches for this equi-join.
 
3593
      */  
 
3594
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3595
      {
 
3596
        /* 
 
3597
          When performing an outer join operation if there are no matching rows
 
3598
          for the single row of the outer table all the inner tables are to be
 
3599
          null complemented and thus considered as constant tables.
 
3600
          Here we apply this consideration to the case of outer join operations 
 
3601
          with a single inner table only because the case with nested tables
 
3602
          would require a more thorough analysis.
 
3603
          TODO. Apply single row substitution to null complemented inner tables
 
3604
          for nested outer join operations. 
 
3605
        */              
 
3606
        while (keyuse->table == table)
 
3607
        {
 
3608
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3609
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3610
          {
 
3611
            s->type= JT_CONST;
 
3612
            mark_as_null_row(table);
 
3613
            found_const_table_map|= table->map;
 
3614
            join->const_table_map|= table->map;
 
3615
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3616
            goto more_const_tables_found;
 
3617
           }
 
3618
          keyuse++;
 
3619
        }
 
3620
      }
 
3621
 
 
3622
      if (s->dependent)                         // If dependent on some table
 
3623
      {
 
3624
        // All dep. must be constants
 
3625
        if (s->dependent & ~(found_const_table_map))
 
3626
          continue;
 
3627
        if (table->file->stats.records <= 1L &&
 
3628
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3629
            !table->pos_in_table_list->embedding)
 
3630
        {                                       // system table
 
3631
          int tmp= 0;
 
3632
          s->type=JT_SYSTEM;
 
3633
          join->const_table_map|=table->map;
 
3634
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3635
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3636
          {
 
3637
            if (tmp > 0)
 
3638
              return(1);                        // Fatal error
 
3639
          }
 
3640
          else
 
3641
            found_const_table_map|= table->map;
 
3642
          continue;
 
3643
        }
 
3644
      }
 
3645
      /* check if table can be read by key or table only uses const refs */
 
3646
      if ((keyuse=s->keyuse))
 
3647
      {
 
3648
        s->type= JT_REF;
 
3649
        while (keyuse->table == table)
 
3650
        {
 
3651
          start_keyuse=keyuse;
 
3652
          key=keyuse->key;
 
3653
          s->keys.set_bit(key);               // QQ: remove this ?
 
3654
 
 
3655
          refs=0;
 
3656
          const_ref.clear_all();
 
3657
          eq_part.clear_all();
 
3658
          do
 
3659
          {
 
3660
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3661
            {
 
3662
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3663
                const_ref.set_bit(keyuse->keypart);
 
3664
              else
 
3665
                refs|=keyuse->used_tables;
 
3666
              eq_part.set_bit(keyuse->keypart);
 
3667
            }
 
3668
            keyuse++;
 
3669
          } while (keyuse->table == table && keyuse->key == key);
 
3670
 
 
3671
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3672
              !table->pos_in_table_list->embedding)
 
3673
          {
 
3674
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3675
                 == HA_NOSAME)
 
3676
            {
 
3677
              if (const_ref == eq_part)
 
3678
              {                                 // Found everything for ref.
 
3679
                int tmp;
 
3680
                ref_changed = 1;
 
3681
                s->type= JT_CONST;
 
3682
                join->const_table_map|=table->map;
 
3683
                set_position(join,const_count++,s,start_keyuse);
 
3684
                if (create_ref_for_key(join, s, start_keyuse,
 
3685
                                       found_const_table_map))
 
3686
                  return(1);
 
3687
                if ((tmp=join_read_const_table(s,
 
3688
                                               join->positions+const_count-1)))
 
3689
                {
 
3690
                  if (tmp > 0)
 
3691
                    return(1);                  // Fatal error
 
3692
                }
 
3693
                else
 
3694
                  found_const_table_map|= table->map;
 
3695
                break;
 
3696
              }
 
3697
              else
 
3698
                found_ref|= refs;      // Table is const if all refs are const
 
3699
            }
 
3700
            else if (const_ref == eq_part)
 
3701
              s->const_keys.set_bit(key);
 
3702
          }
 
3703
        }
 
3704
      }
 
3705
    }
 
3706
  } while (join->const_table_map & found_ref && ref_changed);
 
3707
 
 
3708
  /* 
 
3709
    Update info on indexes that can be used for search lookups as
 
3710
    reading const tables may has added new sargable predicates. 
 
3711
  */
 
3712
  if (const_count && sargables)
 
3713
  {
 
3714
    for( ; sargables->field ; sargables++)
 
3715
    {
 
3716
      Field *field= sargables->field;
 
3717
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3718
      key_map possible_keys= field->key_start;
 
3719
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3720
      bool is_const= 1;
 
3721
      for (uint32_t j=0; j < sargables->num_values; j++)
 
3722
        is_const&= sargables->arg_value[j]->const_item();
 
3723
      if (is_const)
 
3724
        join_tab[0].const_keys.merge(possible_keys);
 
3725
    }
 
3726
  }
 
3727
 
 
3728
  if (pull_out_semijoin_tables(join))
 
3729
    return(true);
 
3730
 
 
3731
  /* Calc how many (possible) matched records in each table */
 
3732
 
 
3733
  for (s=stat ; s < stat_end ; s++)
 
3734
  {
 
3735
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3736
    {
 
3737
      /* Only one matching row */
 
3738
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3739
      continue;
 
3740
    }
 
3741
    /* Approximate found rows and time to read them */
 
3742
    s->found_records=s->records=s->table->file->stats.records;
 
3743
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3744
 
 
3745
    /*
 
3746
      Set a max range of how many seeks we can expect when using keys
 
3747
      This is can't be to high as otherwise we are likely to use
 
3748
      table scan.
 
3749
    */
 
3750
    s->worst_seeks= cmin((double) s->found_records / 10,
 
3751
                        (double) s->read_time*3);
 
3752
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3753
      s->worst_seeks=2.0;
 
3754
 
 
3755
    /*
 
3756
      Add to stat->const_keys those indexes for which all group fields or
 
3757
      all select distinct fields participate in one index.
 
3758
    */
 
3759
    add_group_and_distinct_keys(join, s);
 
3760
 
 
3761
    if (!s->const_keys.is_clear_all() &&
 
3762
        !s->table->pos_in_table_list->embedding)
 
3763
    {
 
3764
      ha_rows records;
 
3765
      SQL_SELECT *select;
 
3766
      select= make_select(s->table, found_const_table_map,
 
3767
                          found_const_table_map,
 
3768
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3769
                          1, &error);
 
3770
      if (!select)
 
3771
        return(1);
 
3772
      records= get_quick_record_count(join->thd, select, s->table,
 
3773
                                      &s->const_keys, join->row_limit);
 
3774
      s->quick=select->quick;
 
3775
      s->needed_reg=select->needed_reg;
 
3776
      select->quick=0;
 
3777
      if (records == 0 && s->table->reginfo.impossible_range)
 
3778
      {
 
3779
        /*
 
3780
          Impossible WHERE or ON expression
 
3781
          In case of ON, we mark that the we match one empty NULL row.
 
3782
          In case of WHERE, don't set found_const_table_map to get the
 
3783
          caller to abort with a zero row result.
 
3784
        */
 
3785
        join->const_table_map|= s->table->map;
 
3786
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3787
        s->type= JT_CONST;
 
3788
        if (*s->on_expr_ref)
 
3789
        {
 
3790
          /* Generate empty row */
 
3791
          s->info= "Impossible ON condition";
 
3792
          found_const_table_map|= s->table->map;
 
3793
          s->type= JT_CONST;
 
3794
          mark_as_null_row(s->table);           // All fields are NULL
 
3795
        }
 
3796
      }
 
3797
      if (records != HA_POS_ERROR)
 
3798
      {
 
3799
        s->found_records=records;
 
3800
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3801
      }
 
3802
      delete select;
 
3803
    }
 
3804
  }
 
3805
 
 
3806
  join->join_tab=stat;
 
3807
  join->map2table=stat_ref;
 
3808
  join->table= join->all_tables=table_vector;
 
3809
  join->const_tables=const_count;
 
3810
  join->found_const_table_map=found_const_table_map;
 
3811
 
 
3812
  /* Find an optimal join order of the non-constant tables. */
 
3813
  if (join->const_tables != join->tables)
 
3814
  {
 
3815
    optimize_keyuse(join, keyuse_array);
 
3816
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3817
      return(true);
 
3818
  }
 
3819
  else
 
3820
  {
 
3821
    memcpy(join->best_positions, join->positions,
 
3822
           sizeof(POSITION)*join->const_tables);
 
3823
    join->best_read=1.0;
 
3824
  }
 
3825
  /* Generate an execution plan from the found optimal join order. */
 
3826
  return(join->thd->killed || get_best_combination(join));
 
3827
}
 
3828
 
 
3829
 
478
3830
/*****************************************************************************
479
3831
  Check with keys are used and with tables references with tables
480
3832
  Updates in stat:
483
3835
          keyuse     Pointer to possible keys
484
3836
*****************************************************************************/
485
3837
 
 
3838
/// Used when finding key fields
 
3839
typedef struct key_field_t {
 
3840
  Field         *field;
 
3841
  Item          *val;                   ///< May be empty if diff constant
 
3842
  uint          level;
 
3843
  uint          optimize; // KEY_OPTIMIZE_*
 
3844
  bool          eq_func;
 
3845
  /**
 
3846
    If true, the condition this struct represents will not be satisfied
 
3847
    when val IS NULL.
 
3848
  */
 
3849
  bool          null_rejecting; 
 
3850
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3851
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3852
} KEY_FIELD;
 
3853
 
 
3854
/**
 
3855
  Merge new key definitions to old ones, remove those not used in both.
 
3856
 
 
3857
  This is called for OR between different levels.
 
3858
 
 
3859
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3860
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3861
  that are internally transformed to something like:.
 
3862
 
 
3863
  @code
 
3864
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3865
  @endcode
 
3866
 
 
3867
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3868
  result has null_rejecting=true if it is set for both ORed references.
 
3869
  for example:
 
3870
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3871
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3872
 
 
3873
  @todo
 
3874
    The result of this is that we're missing some 'ref' accesses.
 
3875
    OptimizerTeam: Fix this
 
3876
*/
 
3877
 
 
3878
static KEY_FIELD *
 
3879
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3880
                 uint32_t and_level)
 
3881
{
 
3882
  if (start == new_fields)
 
3883
    return start;                               // Impossible or
 
3884
  if (new_fields == end)
 
3885
    return start;                               // No new fields, skip all
 
3886
 
 
3887
  KEY_FIELD *first_free=new_fields;
 
3888
 
 
3889
  /* Mark all found fields in old array */
 
3890
  for (; new_fields != end ; new_fields++)
 
3891
  {
 
3892
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3893
    {
 
3894
      if (old->field == new_fields->field)
 
3895
      {
 
3896
        /*
 
3897
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3898
          it can return false where it is feasible to make it return true.
 
3899
          
 
3900
          The cause is as follows: Some of the tables are already known to be
 
3901
          const tables (the detection code is in make_join_statistics(),
 
3902
          above the update_ref_and_keys() call), but we didn't propagate 
 
3903
          information about this: Table::const_table is not set to true, and
 
3904
          Item::update_used_tables() hasn't been called for each item.
 
3905
          The result of this is that we're missing some 'ref' accesses.
 
3906
          TODO: OptimizerTeam: Fix this
 
3907
        */
 
3908
        if (!new_fields->val->const_item())
 
3909
        {
 
3910
          /*
 
3911
            If the value matches, we can use the key reference.
 
3912
            If not, we keep it until we have examined all new values
 
3913
          */
 
3914
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3915
          {
 
3916
            old->level= and_level;
 
3917
            old->optimize= ((old->optimize & new_fields->optimize &
 
3918
                             KEY_OPTIMIZE_EXISTS) |
 
3919
                            ((old->optimize | new_fields->optimize) &
 
3920
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3921
            old->null_rejecting= (old->null_rejecting &&
 
3922
                                  new_fields->null_rejecting);
 
3923
          }
 
3924
        }
 
3925
        else if (old->eq_func && new_fields->eq_func &&
 
3926
                 old->val->eq_by_collation(new_fields->val, 
 
3927
                                           old->field->binary(),
 
3928
                                           old->field->charset()))
 
3929
 
 
3930
        {
 
3931
          old->level= and_level;
 
3932
          old->optimize= ((old->optimize & new_fields->optimize &
 
3933
                           KEY_OPTIMIZE_EXISTS) |
 
3934
                          ((old->optimize | new_fields->optimize) &
 
3935
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3936
          old->null_rejecting= (old->null_rejecting &&
 
3937
                                new_fields->null_rejecting);
 
3938
        }
 
3939
        else if (old->eq_func && new_fields->eq_func &&
 
3940
                 ((old->val->const_item() && old->val->is_null()) || 
 
3941
                  new_fields->val->is_null()))
 
3942
        {
 
3943
          /* field = expression OR field IS NULL */
 
3944
          old->level= and_level;
 
3945
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3946
          /*
 
3947
            Remember the NOT NULL value unless the value does not depend
 
3948
            on other tables.
 
3949
          */
 
3950
          if (!old->val->used_tables() && old->val->is_null())
 
3951
            old->val= new_fields->val;
 
3952
          /* The referred expression can be NULL: */ 
 
3953
          old->null_rejecting= 0;
 
3954
        }
 
3955
        else
 
3956
        {
 
3957
          /*
 
3958
            We are comparing two different const.  In this case we can't
 
3959
            use a key-lookup on this so it's better to remove the value
 
3960
            and let the range optimzier handle it
 
3961
          */
 
3962
          if (old == --first_free)              // If last item
 
3963
            break;
 
3964
          *old= *first_free;                    // Remove old value
 
3965
          old--;                                // Retry this value
 
3966
        }
 
3967
      }
 
3968
    }
 
3969
  }
 
3970
  /* Remove all not used items */
 
3971
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3972
  {
 
3973
    if (old->level != and_level)
 
3974
    {                                           // Not used in all levels
 
3975
      if (old == --first_free)
 
3976
        break;
 
3977
      *old= *first_free;                        // Remove old value
 
3978
      continue;
 
3979
    }
 
3980
    old++;
 
3981
  }
 
3982
  return first_free;
 
3983
}
 
3984
 
 
3985
 
 
3986
/**
 
3987
  Add a possible key to array of possible keys if it's usable as a key
 
3988
 
 
3989
    @param key_fields      Pointer to add key, if usable
 
3990
    @param and_level       And level, to be stored in KEY_FIELD
 
3991
    @param cond            Condition predicate
 
3992
    @param field           Field used in comparision
 
3993
    @param eq_func         True if we used =, <=> or IS NULL
 
3994
    @param value           Value used for comparison with field
 
3995
    @param usable_tables   Tables which can be used for key optimization
 
3996
    @param sargables       IN/OUT Array of found sargable candidates
 
3997
 
 
3998
  @note
 
3999
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4000
    table, we store this to be able to do not exists optimization later.
 
4001
 
 
4002
  @returns
 
4003
    *key_fields is incremented if we stored a key in the array
 
4004
*/
 
4005
 
 
4006
static void
 
4007
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
 
4008
              Field *field, bool eq_func, Item **value, uint32_t num_values,
 
4009
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4010
{
 
4011
  uint32_t exists_optimize= 0;
 
4012
  if (!(field->flags & PART_KEY_FLAG))
 
4013
  {
 
4014
    // Don't remove column IS NULL on a LEFT JOIN table
 
4015
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4016
        !field->table->maybe_null || field->null_ptr)
 
4017
      return;                                   // Not a key. Skip it
 
4018
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4019
    assert(num_values == 1);
 
4020
  }
 
4021
  else
 
4022
  {
 
4023
    table_map used_tables=0;
 
4024
    bool optimizable=0;
 
4025
    for (uint32_t i=0; i<num_values; i++)
 
4026
    {
 
4027
      used_tables|=(value[i])->used_tables();
 
4028
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4029
        optimizable=1;
 
4030
    }
 
4031
    if (!optimizable)
 
4032
      return;
 
4033
    if (!(usable_tables & field->table->map))
 
4034
    {
 
4035
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4036
          !field->table->maybe_null || field->null_ptr)
 
4037
        return;                                 // Can't use left join optimize
 
4038
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4039
    }
 
4040
    else
 
4041
    {
 
4042
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4043
      key_map possible_keys=field->key_start;
 
4044
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4045
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4046
 
 
4047
      /*
 
4048
        Save the following cases:
 
4049
        Field op constant
 
4050
        Field LIKE constant where constant doesn't start with a wildcard
 
4051
        Field = field2 where field2 is in a different table
 
4052
        Field op formula
 
4053
        Field IS NULL
 
4054
        Field IS NOT NULL
 
4055
         Field BETWEEN ...
 
4056
         Field IN ...
 
4057
      */
 
4058
      stat[0].key_dependent|=used_tables;
 
4059
 
 
4060
      bool is_const=1;
 
4061
      for (uint32_t i=0; i<num_values; i++)
 
4062
      {
 
4063
        if (!(is_const&= value[i]->const_item()))
 
4064
          break;
 
4065
      }
 
4066
      if (is_const)
 
4067
        stat[0].const_keys.merge(possible_keys);
 
4068
      else if (!eq_func)
 
4069
      {
 
4070
        /* 
 
4071
          Save info to be able check whether this predicate can be 
 
4072
          considered as sargable for range analisis after reading const tables.
 
4073
          We do not save info about equalities as update_const_equal_items
 
4074
          will take care of updating info on keys from sargable equalities. 
 
4075
        */
 
4076
        (*sargables)--;
 
4077
        (*sargables)->field= field;
 
4078
        (*sargables)->arg_value= value;
 
4079
        (*sargables)->num_values= num_values;
 
4080
      }
 
4081
      /*
 
4082
        We can't always use indexes when comparing a string index to a
 
4083
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4084
        eq_func is NEVER true when num_values > 1
 
4085
       */
 
4086
      if (!eq_func)
 
4087
      {
 
4088
        /* 
 
4089
          Additional optimization: if we're processing
 
4090
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4091
          "t.key = c1".
 
4092
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4093
          There are 2 options:
 
4094
          A) Make equality propagation code be able to handle BETWEEN
 
4095
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4096
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4097
             and use them in equality propagation process (see details in
 
4098
             OptimizerKBAndTodo)
 
4099
        */
 
4100
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4101
            ((Item_func_between*) cond)->negated ||
 
4102
            !value[0]->eq(value[1], field->binary()))
 
4103
          return;
 
4104
        eq_func= true;
 
4105
      }
 
4106
 
 
4107
      if (field->result_type() == STRING_RESULT)
 
4108
      {
 
4109
        if ((*value)->result_type() != STRING_RESULT)
 
4110
        {
 
4111
          if (field->cmp_type() != (*value)->result_type())
 
4112
            return;
 
4113
        }
 
4114
        else
 
4115
        {
 
4116
          /*
 
4117
            We can't use indexes if the effective collation
 
4118
            of the operation differ from the field collation.
 
4119
          */
 
4120
          if (field->cmp_type() == STRING_RESULT &&
 
4121
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4122
            return;
 
4123
        }
 
4124
      }
 
4125
    }
 
4126
  }
 
4127
  /*
 
4128
    For the moment eq_func is always true. This slot is reserved for future
 
4129
    extensions where we want to remembers other things than just eq comparisons
 
4130
  */
 
4131
  assert(eq_func);
 
4132
  /* Store possible eq field */
 
4133
  (*key_fields)->field=         field;
 
4134
  (*key_fields)->eq_func=       eq_func;
 
4135
  (*key_fields)->val=           *value;
 
4136
  (*key_fields)->level=         and_level;
 
4137
  (*key_fields)->optimize=      exists_optimize;
 
4138
  /*
 
4139
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4140
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4141
    has NULL value.
 
4142
    We use null_rejecting in add_not_null_conds() to add
 
4143
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4144
  */
 
4145
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4146
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4147
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4148
                                  ((Item_field*)*value)->field->maybe_null());
 
4149
  (*key_fields)->cond_guard= NULL;
 
4150
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4151
                              cond->name < subq_sj_cond_name + 64)? 
 
4152
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4153
  (*key_fields)++;
 
4154
}
 
4155
 
 
4156
/**
 
4157
  Add possible keys to array of possible keys originated from a simple
 
4158
  predicate.
 
4159
 
 
4160
    @param  key_fields     Pointer to add key, if usable
 
4161
    @param  and_level      And level, to be stored in KEY_FIELD
 
4162
    @param  cond           Condition predicate
 
4163
    @param  field          Field used in comparision
 
4164
    @param  eq_func        True if we used =, <=> or IS NULL
 
4165
    @param  value          Value used for comparison with field
 
4166
                           Is NULL for BETWEEN and IN    
 
4167
    @param  usable_tables  Tables which can be used for key optimization
 
4168
    @param  sargables      IN/OUT Array of found sargable candidates
 
4169
 
 
4170
  @note
 
4171
    If field items f1 and f2 belong to the same multiple equality and
 
4172
    a key is added for f1, the the same key is added for f2.
 
4173
 
 
4174
  @returns
 
4175
    *key_fields is incremented if we stored a key in the array
 
4176
*/
 
4177
 
 
4178
static void
 
4179
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
 
4180
                     Item_func *cond, Item_field *field_item,
 
4181
                     bool eq_func, Item **val,
 
4182
                     uint32_t num_values, table_map usable_tables,
 
4183
                     SARGABLE_PARAM **sargables)
 
4184
{
 
4185
  Field *field= field_item->field;
 
4186
  add_key_field(key_fields, and_level, cond, field,
 
4187
                eq_func, val, num_values, usable_tables, sargables);
 
4188
  Item_equal *item_equal= field_item->item_equal;
 
4189
  if (item_equal)
 
4190
  { 
 
4191
    /*
 
4192
      Add to the set of possible key values every substitution of
 
4193
      the field for an equal field included into item_equal
 
4194
    */
 
4195
    Item_equal_iterator it(*item_equal);
 
4196
    Item_field *item;
 
4197
    while ((item= it++))
 
4198
    {
 
4199
      if (!field->eq(item->field))
 
4200
      {
 
4201
        add_key_field(key_fields, and_level, cond, item->field,
 
4202
                      eq_func, val, num_values, usable_tables,
 
4203
                      sargables);
 
4204
      }
 
4205
    }
 
4206
  }
 
4207
}
 
4208
 
 
4209
static void
 
4210
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
 
4211
               COND *cond, table_map usable_tables,
 
4212
               SARGABLE_PARAM **sargables)
 
4213
{
 
4214
  if (cond->type() == Item_func::COND_ITEM)
 
4215
  {
 
4216
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4217
    KEY_FIELD *org_key_fields= *key_fields;
 
4218
 
 
4219
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4220
    {
 
4221
      Item *item;
 
4222
      while ((item=li++))
 
4223
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4224
                       sargables);
 
4225
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4226
        org_key_fields->level= *and_level;
 
4227
    }
 
4228
    else
 
4229
    {
 
4230
      (*and_level)++;
 
4231
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4232
                     sargables);
 
4233
      Item *item;
 
4234
      while ((item=li++))
 
4235
      {
 
4236
        KEY_FIELD *start_key_fields= *key_fields;
 
4237
        (*and_level)++;
 
4238
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4239
                       sargables);
 
4240
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4241
                                     *key_fields,++(*and_level));
 
4242
      }
 
4243
    }
 
4244
    return;
 
4245
  }
 
4246
 
 
4247
  /* 
 
4248
    Subquery optimization: Conditions that are pushed down into subqueries
 
4249
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4250
    but need to set cond_guard for KEYUSE elements generated from it.
 
4251
  */
 
4252
  {
 
4253
    if (cond->type() == Item::FUNC_ITEM &&
 
4254
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4255
    {
 
4256
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4257
      if (!join->group_list && !join->order &&
 
4258
          join->unit->item && 
 
4259
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4260
          !join->unit->is_union())
 
4261
      {
 
4262
        KEY_FIELD *save= *key_fields;
 
4263
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4264
                       sargables);
 
4265
        // Indicate that this ref access candidate is for subquery lookup:
 
4266
        for (; save != *key_fields; save++)
 
4267
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4268
      }
 
4269
      return;
 
4270
    }
 
4271
  }
 
4272
 
 
4273
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4274
  if (cond->type() != Item::FUNC_ITEM)
 
4275
    return;
 
4276
  Item_func *cond_func= (Item_func*) cond;
 
4277
  switch (cond_func->select_optimize()) {
 
4278
  case Item_func::OPTIMIZE_NONE:
 
4279
    break;
 
4280
  case Item_func::OPTIMIZE_KEY:
 
4281
  {
 
4282
    Item **values;
 
4283
    // BETWEEN, IN, NE
 
4284
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4285
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4286
    {
 
4287
      values= cond_func->arguments()+1;
 
4288
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4289
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4290
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4291
        values--;
 
4292
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4293
                  cond_func->argument_count() != 2);
 
4294
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4295
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4296
                           0, values, 
 
4297
                           cond_func->argument_count()-1,
 
4298
                           usable_tables, sargables);
 
4299
    }
 
4300
    if (cond_func->functype() == Item_func::BETWEEN)
 
4301
    {
 
4302
      values= cond_func->arguments();
 
4303
      for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
 
4304
      {
 
4305
        Item_field *field_item;
 
4306
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4307
            &&
 
4308
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4309
        {
 
4310
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4311
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4312
                               field_item, 0, values, 1, usable_tables, 
 
4313
                               sargables);
 
4314
        }
 
4315
      }  
 
4316
    }
 
4317
    break;
 
4318
  }
 
4319
  case Item_func::OPTIMIZE_OP:
 
4320
  {
 
4321
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4322
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4323
 
 
4324
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4325
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4326
    {
 
4327
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4328
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4329
                           equal_func,
 
4330
                           cond_func->arguments()+1, 1, usable_tables,
 
4331
                           sargables);
 
4332
    }
 
4333
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4334
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4335
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4336
    {
 
4337
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4338
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4339
                           equal_func,
 
4340
                           cond_func->arguments(),1,usable_tables,
 
4341
                           sargables);
 
4342
    }
 
4343
    break;
 
4344
  }
 
4345
  case Item_func::OPTIMIZE_NULL:
 
4346
    /* column_name IS [NOT] NULL */
 
4347
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4348
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4349
    {
 
4350
      Item *tmp=new Item_null;
 
4351
      if (unlikely(!tmp))                       // Should never be true
 
4352
        return;
 
4353
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4354
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4355
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4356
                           &tmp, 1, usable_tables, sargables);
 
4357
    }
 
4358
    break;
 
4359
  case Item_func::OPTIMIZE_EQUAL:
 
4360
    Item_equal *item_equal= (Item_equal *) cond;
 
4361
    Item *const_item= item_equal->get_const();
 
4362
    Item_equal_iterator it(*item_equal);
 
4363
    Item_field *item;
 
4364
    if (const_item)
 
4365
    {
 
4366
      /*
 
4367
        For each field field1 from item_equal consider the equality 
 
4368
        field1=const_item as a condition allowing an index access of the table
 
4369
        with field1 by the keys value of field1.
 
4370
      */   
 
4371
      while ((item= it++))
 
4372
      {
 
4373
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4374
                      true, &const_item, 1, usable_tables, sargables);
 
4375
      }
 
4376
    }
 
4377
    else 
 
4378
    {
 
4379
      /*
 
4380
        Consider all pairs of different fields included into item_equal.
 
4381
        For each of them (field1, field1) consider the equality 
 
4382
        field1=field2 as a condition allowing an index access of the table
 
4383
        with field1 by the keys value of field2.
 
4384
      */   
 
4385
      Item_equal_iterator fi(*item_equal);
 
4386
      while ((item= fi++))
 
4387
      {
 
4388
        Field *field= item->field;
 
4389
        while ((item= it++))
 
4390
        {
 
4391
          if (!field->eq(item->field))
 
4392
          {
 
4393
            add_key_field(key_fields, *and_level, cond_func, field,
 
4394
                          true, (Item **) &item, 1, usable_tables,
 
4395
                          sargables);
 
4396
          }
 
4397
        }
 
4398
        it.rewind();
 
4399
      }
 
4400
    }
 
4401
    break;
 
4402
  }
 
4403
}
486
4404
 
487
4405
/**
488
4406
  Add all keys with uses 'field' for some keypart.
489
4407
 
490
4408
  If field->and_level != and_level then only mark key_part as const_part.
491
4409
*/
492
 
uint32_t max_part_bit(key_part_map bits)
 
4410
 
 
4411
static uint
 
4412
max_part_bit(key_part_map bits)
493
4413
{
494
4414
  uint32_t found;
495
4415
  for (found=0; bits & 1 ; found++,bits>>=1) ;
496
4416
  return found;
497
4417
}
498
4418
 
499
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4419
static void
 
4420
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4421
{
 
4422
  Field *field=key_field->field;
 
4423
  Table *form= field->table;
 
4424
  KEYUSE keyuse;
 
4425
 
 
4426
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4427
  {
 
4428
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
 
4429
    {
 
4430
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4431
        continue;
 
4432
 
 
4433
      uint32_t key_parts= (uint) form->key_info[key].key_parts;
 
4434
      for (uint32_t part=0 ; part <  key_parts ; part++)
 
4435
      {
 
4436
        if (field->eq(form->key_info[key].key_part[part].field))
 
4437
        {
 
4438
          keyuse.table= field->table;
 
4439
          keyuse.val =  key_field->val;
 
4440
          keyuse.key =  key;
 
4441
          keyuse.keypart=part;
 
4442
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4443
          keyuse.used_tables=key_field->val->used_tables();
 
4444
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4445
          keyuse.null_rejecting= key_field->null_rejecting;
 
4446
          keyuse.cond_guard= key_field->cond_guard;
 
4447
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4448
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
4449
        }
 
4450
      }
 
4451
    }
 
4452
  }
 
4453
}
 
4454
 
 
4455
static int
 
4456
sort_keyuse(KEYUSE *a,KEYUSE *b)
500
4457
{
501
4458
  int res;
502
 
  if (a->getTable()->tablenr != b->getTable()->tablenr)
503
 
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
504
 
  if (a->getKey() != b->getKey())
505
 
    return static_cast<int>((a->getKey() - b->getKey()));
506
 
  if (a->getKeypart() != b->getKeypart())
507
 
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
4459
  if (a->table->tablenr != b->table->tablenr)
 
4460
    return (int) (a->table->tablenr - b->table->tablenr);
 
4461
  if (a->key != b->key)
 
4462
    return (int) (a->key - b->key);
 
4463
  if (a->keypart != b->keypart)
 
4464
    return (int) (a->keypart - b->keypart);
508
4465
  // Place const values before other ones
509
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
510
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4466
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4467
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
511
4468
    return res;
512
4469
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
513
 
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
514
 
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
 
4470
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4471
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4472
}
 
4473
 
 
4474
 
 
4475
/*
 
4476
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4477
 
 
4478
    This function populates KEY_FIELD array with entries generated from the 
 
4479
    ON condition of the given nested join, and does the same for nested joins 
 
4480
    contained within this nested join.
 
4481
 
 
4482
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4483
  @param[in,out]  end                 End of the key field array
 
4484
  @param[in,out]  and_level           And-level
 
4485
  @param[in,out]  sargables           Array of found sargable candidates
 
4486
 
 
4487
 
 
4488
  @note
 
4489
    We can add accesses to the tables that are direct children of this nested 
 
4490
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4491
    
 
4492
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4493
    invoked for):
 
4494
    @code
 
4495
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4496
    @endcode
 
4497
    Example for #2:
 
4498
    @code
 
4499
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4500
    @endcode
 
4501
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4502
    t1 only.
 
4503
    Example #3:
 
4504
    @code
 
4505
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4506
    @endcode
 
4507
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4508
*/
 
4509
 
 
4510
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
 
4511
                                  KEY_FIELD **end, uint32_t *and_level,
 
4512
                                  SARGABLE_PARAM **sargables)
 
4513
{
 
4514
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
 
4515
  List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
 
4516
  bool have_another = false;
 
4517
  table_map tables= 0;
 
4518
  TableList *table;
 
4519
  assert(nested_join_table->nested_join);
 
4520
 
 
4521
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4522
                                            (table= li++))))
 
4523
  {
 
4524
    if (table->nested_join)
 
4525
    {
 
4526
      if (!table->on_expr)
 
4527
      {
 
4528
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4529
        have_another= true;
 
4530
        li2= li;
 
4531
        li= List_iterator<TableList>(table->nested_join->join_list); 
 
4532
      }
 
4533
      else
 
4534
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4535
    }
 
4536
    else
 
4537
      if (!table->on_expr)
 
4538
        tables |= table->table->map;
 
4539
  }
 
4540
  if (nested_join_table->on_expr)
 
4541
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4542
                   sargables);
515
4543
}
516
4544
 
517
4545
 
518
4546
/**
519
4547
  Update keyuse array with all possible keys we can use to fetch rows.
520
 
 
521
 
  @param       session
522
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4548
  
 
4549
  @param       thd 
 
4550
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
523
4551
  @param       join_tab       Array in tablenr_order
524
4552
  @param       tables         Number of tables in join
525
4553
  @param       cond           WHERE condition (note that the function analyzes
528
4556
                              for which we can make ref access based the WHERE
529
4557
                              clause)
530
4558
  @param       select_lex     current SELECT
531
 
  @param[out]  sargables      std::vector of found sargable candidates
532
 
 
 
4559
  @param[out]  sargables      Array of found sargable candidates
 
4560
      
533
4561
   @retval
534
4562
     0  OK
535
4563
   @retval
536
4564
     1  Out of memory.
537
4565
*/
538
 
bool update_ref_and_keys(Session *session,
539
 
                         DYNAMIC_ARRAY *keyuse,
540
 
                         JoinTable *join_tab,
541
 
                         uint32_t tables,
542
 
                         COND *cond, 
543
 
                         COND_EQUAL *,
544
 
                         table_map normal_tables,
545
 
                         Select_Lex *select_lex,
546
 
                         vector<optimizer::SargableParam> &sargables)
 
4566
 
 
4567
static bool
 
4568
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4569
                    uint32_t tables, COND *cond,
 
4570
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4571
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4572
                    SARGABLE_PARAM **sargables)
547
4573
{
548
 
  uint  and_level,found_eq_constant;
549
 
  optimizer::KeyField *key_fields, *end, *field;
 
4574
  uint  and_level,i,found_eq_constant;
 
4575
  KEY_FIELD *key_fields, *end, *field;
550
4576
  uint32_t sz;
551
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
552
 
 
553
 
  /*
554
 
    All predicates that are used to fill arrays of KeyField
555
 
    and SargableParam classes have at most 2 arguments
556
 
    except BETWEEN predicates that have 3 arguments and
 
4577
  uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
 
4578
  
 
4579
  /* 
 
4580
    We use the same piece of memory to store both  KEY_FIELD 
 
4581
    and SARGABLE_PARAM structure.
 
4582
    KEY_FIELD values are placed at the beginning this memory
 
4583
    while  SARGABLE_PARAM values are put at the end.
 
4584
    All predicates that are used to fill arrays of KEY_FIELD
 
4585
    and SARGABLE_PARAM structures have at most 2 arguments
 
4586
    except BETWEEN predicates that have 3 arguments and 
557
4587
    IN predicates.
558
 
    This any predicate if it's not BETWEEN/IN can be used
559
 
    directly to fill at most 2 array elements, either of KeyField 
560
 
    or SargableParam type. For a BETWEEN predicate 3 elements
 
4588
    This any predicate if it's not BETWEEN/IN can be used 
 
4589
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4590
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
561
4591
    can be filled as this predicate is considered as
562
4592
    saragable with respect to each of its argument.
563
4593
    An IN predicate can require at most 1 element as currently
564
4594
    it is considered as sargable only for its first argument.
565
4595
    Multiple equality can add  elements that are filled after
566
4596
    substitution of field arguments by equal fields. There
567
 
    can be not more than select_lex->max_equal_elems such
 
4597
    can be not more than select_lex->max_equal_elems such 
568
4598
    substitutions.
569
 
  */
570
 
  sz= sizeof(optimizer::KeyField) *
571
 
      (((session->lex->current_select->cond_count+1)*2 +
572
 
        session->lex->current_select->between_count)*m+1);
573
 
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
574
 
    return true;
 
4599
  */ 
 
4600
  sz= cmax(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4601
      (((thd->lex->current_select->cond_count+1)*2 +
 
4602
        thd->lex->current_select->between_count)*m+1);
 
4603
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4604
    return true; /* purecov: inspected */
575
4605
  and_level= 0;
576
4606
  field= end= key_fields;
 
4607
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4608
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4609
  /* set a barrier for the array of SARGABLE_PARAM */
 
4610
  (*sargables)[0].field= 0; 
577
4611
 
578
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4612
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
579
4613
    return true;
580
4614
  if (cond)
581
4615
  {
582
4616
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
583
4617
                   sargables);
584
 
    for (; field != end; field++)
 
4618
    for (; field != end ; field++)
585
4619
    {
586
 
      add_key_part(keyuse, field);
 
4620
      add_key_part(keyuse,field);
587
4621
      /* Mark that we can optimize LEFT JOIN */
588
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
589
 
          ! field->getField()->real_maybe_null())
590
 
      {
591
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
592
 
      }
 
4622
      if (field->val->type() == Item::NULL_ITEM &&
 
4623
          !field->field->real_maybe_null())
 
4624
        field->field->table->reginfo.not_exists_optimize=1;
593
4625
    }
594
4626
  }
595
 
  for (uint32_t i= 0; i < tables; i++)
 
4627
  for (i=0 ; i < tables ; i++)
596
4628
  {
597
4629
    /*
598
4630
      Block the creation of keys for inner tables of outer joins.
602
4634
      In the future when we introduce conditional accesses
603
4635
      for inner tables in outer joins these keys will be taken
604
4636
      into account as well.
605
 
    */
 
4637
    */ 
606
4638
    if (*join_tab[i].on_expr_ref)
607
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4639
      add_key_fields(join_tab->join, &end, &and_level, 
608
4640
                     *join_tab[i].on_expr_ref,
609
4641
                     join_tab[i].table->map, sargables);
610
4642
  }
615
4647
    TableList *table;
616
4648
    while ((table= li++))
617
4649
    {
618
 
      if (table->getNestedJoin())
619
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4650
      if (table->nested_join)
 
4651
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
620
4652
                              sargables);
621
4653
    }
622
4654
  }
636
4668
  */
637
4669
  if (keyuse->elements)
638
4670
  {
639
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4671
    KEYUSE key_end,*prev,*save_pos,*use;
640
4672
 
641
 
    internal::my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
642
 
                       (qsort_cmp) sort_keyuse);
 
4673
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
4674
          (qsort_cmp) sort_keyuse);
643
4675
 
644
4676
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
645
4677
    insert_dynamic(keyuse,(unsigned char*) &key_end);
646
4678
 
647
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4679
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
648
4680
    prev= &key_end;
649
 
    found_eq_constant= 0;
 
4681
    found_eq_constant=0;
 
4682
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
650
4683
    {
651
 
      uint32_t i;
652
 
 
653
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
4684
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4685
        use->table->const_key_parts[use->key]|= use->keypart_map;
654
4686
      {
655
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
656
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
657
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
658
 
        {
659
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
660
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
661
 
            continue;                           /* remove */
662
 
        }
663
 
        else if (use->getKeypart() != 0)                // First found must be 0
664
 
          continue;
 
4687
        if (use->key == prev->key && use->table == prev->table)
 
4688
        {
 
4689
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4690
            continue;                           /* remove */
 
4691
        }
 
4692
        else if (use->keypart != 0)             // First found must be 0
 
4693
          continue;
 
4694
      }
665
4695
 
666
 
#ifdef HAVE_VALGRIND
667
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
668
 
        if (save_pos != use)
 
4696
#ifdef HAVE_purify
 
4697
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
4698
      if (save_pos != use)
669
4699
#endif
670
 
          *save_pos= *use;
671
 
        prev=use;
672
 
        found_eq_constant= ! use->getUsedTables();
673
 
        /* Save ptr to first use */
674
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
675
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
676
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
677
 
        save_pos++;
678
 
      }
679
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
680
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
681
 
      keyuse->elements= i;
 
4700
        *save_pos= *use;
 
4701
      prev=use;
 
4702
      found_eq_constant= !use->used_tables;
 
4703
      /* Save ptr to first use */
 
4704
      if (!use->table->reginfo.join_tab->keyuse)
 
4705
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4706
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
4707
      save_pos++;
682
4708
    }
 
4709
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4710
    set_dynamic(keyuse,(unsigned char*) &key_end,i);
 
4711
    keyuse->elements=i;
683
4712
  }
684
4713
  return false;
685
4714
}
687
4716
/**
688
4717
  Update some values in keyuse for faster choose_plan() loop.
689
4718
*/
690
 
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
 
4719
 
 
4720
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
691
4721
{
692
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
693
 
                                                  0, 
694
 
                                                  optimizer::KeyUse*);
 
4722
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
695
4723
 
696
4724
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
697
4725
  {
704
4732
      Constant tables are ignored.
705
4733
      To avoid bad matches, we don't make ref_table_rows less than 100.
706
4734
    */
707
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
708
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4735
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4736
    if (keyuse->used_tables &
 
4737
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4738
               ~OUTER_REF_TABLE_BIT)))
709
4739
    {
710
4740
      uint32_t tablenr;
711
4741
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
712
4742
      if (map == 1)                     // Only one table
713
4743
      {
714
 
        Table *tmp_table=join->all_tables[tablenr];
715
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4744
        Table *tmp_table=join->all_tables[tablenr];
 
4745
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
716
4746
      }
717
4747
    }
718
4748
    /*
719
4749
      Outer reference (external field) is constant for single executing
720
4750
      of subquery
721
4751
    */
722
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
723
 
      keyuse->setTableRows(1);
 
4752
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4753
      keyuse->ref_table_rows= 1;
724
4754
  }
725
4755
}
726
4756
 
742
4772
  @return
743
4773
    None
744
4774
*/
745
 
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
 
4775
 
 
4776
static void
 
4777
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
746
4778
{
747
4779
  List<Item_field> indexed_fields;
748
4780
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
749
 
  Order      *cur_group;
 
4781
  order_st      *cur_group;
750
4782
  Item_field *cur_item;
751
4783
  key_map possible_keys(0);
752
4784
 
773
4805
 
774
4806
  /* Intersect the keys of all group fields. */
775
4807
  cur_item= indexed_fields_it++;
776
 
  possible_keys|= cur_item->field->part_of_key;
 
4808
  possible_keys.merge(cur_item->field->part_of_key);
777
4809
  while ((cur_item= indexed_fields_it++))
778
4810
  {
779
 
    possible_keys&= cur_item->field->part_of_key;
780
 
  }
781
 
 
782
 
  if (possible_keys.any())
783
 
    join_tab->const_keys|= possible_keys;
784
 
}
785
 
 
786
 
/**
787
 
  Compare two JoinTable objects based on the number of accessed records.
788
 
 
789
 
  @param ptr1 pointer to first JoinTable object
790
 
  @param ptr2 pointer to second JoinTable object
 
4811
    possible_keys.intersect(cur_item->field->part_of_key);
 
4812
  }
 
4813
 
 
4814
  if (!possible_keys.is_clear_all())
 
4815
    join_tab->const_keys.merge(possible_keys);
 
4816
}
 
4817
 
 
4818
 
 
4819
/*****************************************************************************
 
4820
  Go through all combinations of not marked tables and find the one
 
4821
  which uses least records
 
4822
*****************************************************************************/
 
4823
 
 
4824
/** Save const tables first as used tables. */
 
4825
 
 
4826
static void
 
4827
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
4828
{
 
4829
  join->positions[idx].table= table;
 
4830
  join->positions[idx].key=key;
 
4831
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4832
  join->positions[idx].ref_depend_map= 0;
 
4833
 
 
4834
  /* Move the const table as down as possible in best_ref */
 
4835
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4836
  JOIN_TAB *next=join->best_ref[idx];
 
4837
  for (;next != table ; pos++)
 
4838
  {
 
4839
    JOIN_TAB *tmp=pos[0];
 
4840
    pos[0]=next;
 
4841
    next=tmp;
 
4842
  }
 
4843
  join->best_ref[idx]=table;
 
4844
}
 
4845
 
 
4846
 
 
4847
/*
 
4848
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4849
 
 
4850
  SYNOPSIS
 
4851
    get_bound_sj_equalities()
 
4852
      sj_nest           Semi-join nest
 
4853
      remaining_tables  Tables that are not yet bound
 
4854
 
 
4855
  DESCRIPTION
 
4856
    Given a semi-join nest, find out which of the IN-equalities have their
 
4857
    left part expression bound (i.e. the said expression doesn't refer to
 
4858
    any of remaining_tables and can be evaluated).
 
4859
 
 
4860
  RETURN
 
4861
    Bitmap of bound IN-equalities.
 
4862
*/
 
4863
 
 
4864
uint64_t get_bound_sj_equalities(TableList *sj_nest, 
 
4865
                                  table_map remaining_tables)
 
4866
{
 
4867
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4868
  Item *item;
 
4869
  uint32_t i= 0;
 
4870
  uint64_t res= 0;
 
4871
  while ((item= li++))
 
4872
  {
 
4873
    /*
 
4874
      Q: should this take into account equality propagation and how?
 
4875
      A: If e->outer_side is an Item_field, walk over the equality
 
4876
         class and see if there is an element that is bound?
 
4877
      (this is an optional feature)
 
4878
    */
 
4879
    if (!(item->used_tables() & remaining_tables))
 
4880
    {
 
4881
      res |= 1UL < i;
 
4882
    }
 
4883
  }
 
4884
  return res;
 
4885
}
 
4886
 
 
4887
 
 
4888
/**
 
4889
  Find the best access path for an extension of a partial execution
 
4890
  plan and add this path to the plan.
 
4891
 
 
4892
  The function finds the best access path to table 's' from the passed
 
4893
  partial plan where an access path is the general term for any means to
 
4894
  access the data in 's'. An access path may use either an index or a scan,
 
4895
  whichever is cheaper. The input partial plan is passed via the array
 
4896
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4897
  cost are stored in 'join->positions[idx]'.
 
4898
 
 
4899
  @param join             pointer to the structure providing all context info
 
4900
                          for the query
 
4901
  @param s                the table to be joined by the function
 
4902
  @param thd              thread for the connection that submitted the query
 
4903
  @param remaining_tables set of tables not included into the partial plan yet
 
4904
  @param idx              the length of the partial plan
 
4905
  @param record_count     estimate for the number of records returned by the
 
4906
                          partial plan
 
4907
  @param read_time        the cost of the partial plan
 
4908
 
 
4909
  @return
 
4910
    None
 
4911
*/
 
4912
 
 
4913
static void
 
4914
best_access_path(JOIN      *join,
 
4915
                 JOIN_TAB  *s,
 
4916
                 THD       *thd,
 
4917
                 table_map remaining_tables,
 
4918
                 uint32_t      idx,
 
4919
                 double    record_count,
 
4920
                 double    read_time __attribute__((unused)))
 
4921
{
 
4922
  KEYUSE *best_key=         0;
 
4923
  uint32_t best_max_key_part=   0;
 
4924
  bool found_constraint= 0;
 
4925
  double best=              DBL_MAX;
 
4926
  double best_time=         DBL_MAX;
 
4927
  double records=           DBL_MAX;
 
4928
  table_map best_ref_depends_map= 0;
 
4929
  double tmp;
 
4930
  ha_rows rec;
 
4931
  uint32_t best_is_sj_inside_out=    0;
 
4932
 
 
4933
  if (s->keyuse)
 
4934
  {                                            /* Use key if possible */
 
4935
    Table *table= s->table;
 
4936
    KEYUSE *keyuse,*start_key=0;
 
4937
    double best_records= DBL_MAX;
 
4938
    uint32_t max_key_part=0;
 
4939
    uint64_t bound_sj_equalities= 0;
 
4940
    bool try_sj_inside_out= false;
 
4941
    /*
 
4942
      Discover the bound equalites. We need to do this, if
 
4943
        1. The next table is an SJ-inner table, and
 
4944
        2. It is the first table from that semijoin, and
 
4945
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4946
           all or none of their tables in join_table_map), except
 
4947
           s->emb_sj_nest (which we've just entered).
 
4948
        3. All correlation references from this sj-nest are bound
 
4949
    */
 
4950
    if (s->emb_sj_nest &&                                                 // (1)
 
4951
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4952
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4953
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4954
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4955
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4956
    {
 
4957
      /* This table is an InsideOut scan candidate */
 
4958
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4959
                                                   remaining_tables);
 
4960
      try_sj_inside_out= true;
 
4961
    }
 
4962
 
 
4963
    /* Test how we can use keys */
 
4964
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4965
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4966
    {
 
4967
      key_part_map found_part= 0;
 
4968
      table_map found_ref= 0;
 
4969
      uint32_t key= keyuse->key;
 
4970
      KEY *keyinfo= table->key_info+key;
 
4971
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4972
      key_part_map const_part= 0;
 
4973
      /* The or-null keypart in ref-or-null access: */
 
4974
      key_part_map ref_or_null_part= 0;
 
4975
 
 
4976
      /* Calculate how many key segments of the current key we can use */
 
4977
      start_key= keyuse;
 
4978
      uint64_t handled_sj_equalities=0;
 
4979
      key_part_map sj_insideout_map= 0;
 
4980
 
 
4981
      do /* For each keypart */
 
4982
      {
 
4983
        uint32_t keypart= keyuse->keypart;
 
4984
        table_map best_part_found_ref= 0;
 
4985
        double best_prev_record_reads= DBL_MAX;
 
4986
        
 
4987
        do /* For each way to access the keypart */
 
4988
        {
 
4989
 
 
4990
          /*
 
4991
            if 1. expression doesn't refer to forward tables
 
4992
               2. we won't get two ref-or-null's
 
4993
          */
 
4994
          if (!(remaining_tables & keyuse->used_tables) &&
 
4995
              !(ref_or_null_part && (keyuse->optimize &
 
4996
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
4997
          {
 
4998
            found_part|= keyuse->keypart_map;
 
4999
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5000
              const_part|= keyuse->keypart_map;
 
5001
 
 
5002
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5003
                                                      keyuse->used_tables));
 
5004
            if (tmp2 < best_prev_record_reads)
 
5005
            {
 
5006
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5007
              best_prev_record_reads= tmp2;
 
5008
            }
 
5009
            if (rec > keyuse->ref_table_rows)
 
5010
              rec= keyuse->ref_table_rows;
 
5011
            /*
 
5012
              If there is one 'key_column IS NULL' expression, we can
 
5013
              use this ref_or_null optimisation of this field
 
5014
            */
 
5015
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5016
              ref_or_null_part |= keyuse->keypart_map;
 
5017
          }
 
5018
 
 
5019
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5020
          {
 
5021
            if (!(remaining_tables & keyuse->used_tables))
 
5022
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5023
            else
 
5024
            {
 
5025
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
5026
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5027
            }
 
5028
          }
 
5029
 
 
5030
          keyuse++;
 
5031
        } while (keyuse->table == table && keyuse->key == key &&
 
5032
                 keyuse->keypart == keypart);
 
5033
        found_ref|= best_part_found_ref;
 
5034
      } while (keyuse->table == table && keyuse->key == key);
 
5035
 
 
5036
      /*
 
5037
        Assume that that each key matches a proportional part of table.
 
5038
      */
 
5039
      if (!found_part && !handled_sj_equalities)
 
5040
        continue;                               // Nothing usable found
 
5041
 
 
5042
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5043
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5044
 
 
5045
      bool sj_inside_out_scan= false;
 
5046
      {
 
5047
        found_constraint= 1;
 
5048
        /*
 
5049
          Check if InsideOut scan is applicable:
 
5050
          1. All IN-equalities are either "bound" or "handled"
 
5051
          2. Index keyparts are 
 
5052
             ...
 
5053
        */
 
5054
        if (try_sj_inside_out && 
 
5055
            table->covering_keys.is_set(key) &&
 
5056
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5057
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5058
        {
 
5059
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
5060
          if (n_fixed_parts != keyinfo->key_parts &&
 
5061
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5062
               PREV_BITS(uint, keyinfo->key_parts))
 
5063
          {
 
5064
            /*
 
5065
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5066
              check if all of them are covered.
 
5067
            */
 
5068
            sj_inside_out_scan= true;
 
5069
            if (!n_fixed_parts)
 
5070
            {
 
5071
              /*
 
5072
                It's a confluent ref scan.
 
5073
 
 
5074
                That is, all found KEYUSE elements refer to IN-equalities,
 
5075
                and there is really no ref access because there is no
 
5076
                  t.keypart0 = {bound expression}
 
5077
 
 
5078
                Calculate the cost of complete loose index scan.
 
5079
              */
 
5080
              records= (double)s->table->file->stats.records;
 
5081
 
 
5082
              /* The cost is entire index scan cost (divided by 2) */
 
5083
              best_time= s->table->file->index_only_read_time(key, records);
 
5084
 
 
5085
              /* Now figure how many different keys we will get */
 
5086
              ulong rpc;
 
5087
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5088
                records= records / rpc;
 
5089
              start_key= NULL;
 
5090
            }
 
5091
          }
 
5092
        }
 
5093
 
 
5094
        /*
 
5095
          Check if we found full key
 
5096
        */
 
5097
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5098
            !ref_or_null_part)
 
5099
        {                                         /* use eq key */
 
5100
          max_key_part= UINT32_MAX;
 
5101
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5102
          {
 
5103
            tmp = prev_record_reads(join, idx, found_ref);
 
5104
            records=1.0;
 
5105
          }
 
5106
          else
 
5107
          {
 
5108
            if (!found_ref)
 
5109
            {                                     /* We found a const key */
 
5110
              /*
 
5111
                ReuseRangeEstimateForRef-1:
 
5112
                We get here if we've found a ref(const) (c_i are constants):
 
5113
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5114
                
 
5115
                If range optimizer was able to construct a "range" 
 
5116
                access on this index, then its condition "quick_cond" was
 
5117
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5118
                from the range optimizer.
 
5119
                
 
5120
                Proof of (*): By properties of range and ref optimizers 
 
5121
                quick_cond will be equal or tighther than ref_const_cond. 
 
5122
                ref_const_cond already covers "smallest" possible interval - 
 
5123
                a singlepoint interval over all keyparts. Therefore, 
 
5124
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5125
                empty interval we wouldn't have got here).
 
5126
              */
 
5127
              if (table->quick_keys.is_set(key))
 
5128
                records= (double) table->quick_rows[key];
 
5129
              else
 
5130
              {
 
5131
                /* quick_range couldn't use key! */
 
5132
                records= (double) s->records/rec;
 
5133
              }
 
5134
            }
 
5135
            else
 
5136
            {
 
5137
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5138
              {                                   /* Prefer longer keys */
 
5139
                records=
 
5140
                  ((double) s->records / (double) rec *
 
5141
                   (1.0 +
 
5142
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5143
                     (double) table->s->max_key_length)));
 
5144
                if (records < 2.0)
 
5145
                  records=2.0;               /* Can't be as good as a unique */
 
5146
              }
 
5147
              /*
 
5148
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5149
                E(#rows) from range optimizer. Make another try:
 
5150
                
 
5151
                If range optimizer produced E(#rows) for a prefix of the ref
 
5152
                access we're considering, and that E(#rows) is lower then our
 
5153
                current estimate, make an adjustment. The criteria of when we
 
5154
                can make an adjustment is a special case of the criteria used
 
5155
                in ReuseRangeEstimateForRef-3.
 
5156
              */
 
5157
              if (table->quick_keys.is_set(key) &&
 
5158
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5159
                  table->quick_n_ranges[key] == 1 &&
 
5160
                  records > (double) table->quick_rows[key])
 
5161
              {
 
5162
                records= (double) table->quick_rows[key];
 
5163
              }
 
5164
            }
 
5165
            /* Limit the number of matched rows */
 
5166
            tmp= records;
 
5167
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5168
            if (table->covering_keys.is_set(key))
 
5169
            {
 
5170
              /* we can use only index tree */
 
5171
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5172
            }
 
5173
            else
 
5174
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
5175
          }
 
5176
        }
 
5177
        else
 
5178
        {
 
5179
          /*
 
5180
            Use as much key-parts as possible and a uniq key is better
 
5181
            than a not unique key
 
5182
            Set tmp to (previous record count) * (records / combination)
 
5183
          */
 
5184
          if ((found_part & 1) &&
 
5185
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5186
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5187
          {
 
5188
            max_key_part= max_part_bit(found_part);
 
5189
            /*
 
5190
              ReuseRangeEstimateForRef-3:
 
5191
              We're now considering a ref[or_null] access via
 
5192
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5193
              (same-as-above but with one cond replaced 
 
5194
               with "t.keypart_i IS NULL")]  (**)
 
5195
              
 
5196
              Try re-using E(#rows) from "range" optimizer:
 
5197
              We can do so if "range" optimizer used the same intervals as
 
5198
              in (**). The intervals used by range optimizer may be not 
 
5199
              available at this point (as "range" access might have choosen to
 
5200
              create quick select over another index), so we can't compare
 
5201
              them to (**). We'll make indirect judgements instead.
 
5202
              The sufficient conditions for re-use are:
 
5203
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5204
                   this is not satisfied we have no way to know which ranges
 
5205
                   will be actually scanned by 'ref' until we execute the 
 
5206
                   join)
 
5207
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5208
                   is apparently a necessary requirement)
 
5209
 
 
5210
              We also have a property that "range optimizer produces equal or 
 
5211
              tighter set of scan intervals than ref(const) optimizer". Each
 
5212
              of the intervals in (**) are "tightest possible" intervals when 
 
5213
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5214
              From here it follows that range access used either one, or
 
5215
              both of the (I1) and (I2) intervals:
 
5216
              
 
5217
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5218
               (same-as-above but with one cond replaced  
 
5219
                with "t.keypart_i IS NULL")               (I2)
 
5220
 
 
5221
              The remaining part is to exclude the situation where range
 
5222
              optimizer used one interval while we're considering
 
5223
              ref-or-null and looking for estimate for two intervals. This
 
5224
              is done by last limitation:
 
5225
 
 
5226
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5227
            */
 
5228
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5229
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5230
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
5231
            {
 
5232
              tmp= records= (double) table->quick_rows[key];
 
5233
            }
 
5234
            else
 
5235
            {
 
5236
              /* Check if we have statistic about the distribution */
 
5237
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5238
              {
 
5239
                /* 
 
5240
                  Fix for the case where the index statistics is too
 
5241
                  optimistic: If 
 
5242
                  (1) We're considering ref(const) and there is quick select
 
5243
                      on the same index, 
 
5244
                  (2) and that quick select uses more keyparts (i.e. it will
 
5245
                      scan equal/smaller interval then this ref(const))
 
5246
                  (3) and E(#rows) for quick select is higher then our
 
5247
                      estimate,
 
5248
                  Then 
 
5249
                    We'll use E(#rows) from quick select.
 
5250
 
 
5251
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5252
                  cheaper in some cases ?
 
5253
                  TODO: figure this out and adjust the plan choice if needed.
 
5254
                */
 
5255
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5256
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5257
                    records < (double)table->quick_rows[key])         // (3)
 
5258
                  records= (double)table->quick_rows[key];
 
5259
 
 
5260
                tmp= records;
 
5261
              }
 
5262
              else
 
5263
              {
 
5264
                /*
 
5265
                  Assume that the first key part matches 1% of the file
 
5266
                  and that the whole key matches 10 (duplicates) or 1
 
5267
                  (unique) records.
 
5268
                  Assume also that more key matches proportionally more
 
5269
                  records
 
5270
                  This gives the formula:
 
5271
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5272
 
 
5273
                  b = records matched by whole key
 
5274
                  a = records matched by first key part (1% of all records?)
 
5275
                  c = number of key parts in key
 
5276
                  x = used key parts (1 <= x <= c)
 
5277
                */
 
5278
                double rec_per_key;
 
5279
                if (!(rec_per_key=(double)
 
5280
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5281
                  rec_per_key=(double) s->records/rec+1;
 
5282
 
 
5283
                if (!s->records)
 
5284
                  tmp = 0;
 
5285
                else if (rec_per_key/(double) s->records >= 0.01)
 
5286
                  tmp = rec_per_key;
 
5287
                else
 
5288
                {
 
5289
                  double a=s->records*0.01;
 
5290
                  if (keyinfo->key_parts > 1)
 
5291
                    tmp= (max_key_part * (rec_per_key - a) +
 
5292
                          a*keyinfo->key_parts - rec_per_key)/
 
5293
                         (keyinfo->key_parts-1);
 
5294
                  else
 
5295
                    tmp= a;
 
5296
                  set_if_bigger(tmp,1.0);
 
5297
                }
 
5298
                records = (ulong) tmp;
 
5299
              }
 
5300
 
 
5301
              if (ref_or_null_part)
 
5302
              {
 
5303
                /* We need to do two key searches to find key */
 
5304
                tmp *= 2.0;
 
5305
                records *= 2.0;
 
5306
              }
 
5307
 
 
5308
              /*
 
5309
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5310
                E(#rows) from range optimizer. Make another try:
 
5311
                
 
5312
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5313
                access we're considering, and that E(#rows) is lower then our
 
5314
                current estimate, make the adjustment.
 
5315
 
 
5316
                The decision whether we can re-use the estimate from the range
 
5317
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5318
                applied to first table->quick_key_parts[key] key parts.
 
5319
              */
 
5320
              if (table->quick_keys.is_set(key) &&
 
5321
                  table->quick_key_parts[key] <= max_key_part &&
 
5322
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5323
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
5324
                                                     const_part) ? 1 : 0) &&
 
5325
                  records > (double) table->quick_rows[key])
 
5326
              {
 
5327
                tmp= records= (double) table->quick_rows[key];
 
5328
              }
 
5329
            }
 
5330
 
 
5331
            /* Limit the number of matched rows */
 
5332
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5333
            if (table->covering_keys.is_set(key))
 
5334
            {
 
5335
              /* we can use only index tree */
 
5336
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5337
            }
 
5338
            else
 
5339
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
5340
          }
 
5341
          else
 
5342
            tmp= best_time;                    // Do nothing
 
5343
        }
 
5344
 
 
5345
        if (sj_inside_out_scan && !start_key)
 
5346
        {
 
5347
          tmp= tmp/2;
 
5348
          if (records)
 
5349
            records= records/2;
 
5350
        }
 
5351
 
 
5352
      }
 
5353
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5354
      {
 
5355
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5356
        best= tmp;
 
5357
        best_records= records;
 
5358
        best_key= start_key;
 
5359
        best_max_key_part= max_key_part;
 
5360
        best_ref_depends_map= found_ref;
 
5361
        best_is_sj_inside_out= sj_inside_out_scan;
 
5362
      }
 
5363
    }
 
5364
    records= best_records;
 
5365
  }
 
5366
 
 
5367
  /*
 
5368
    Don't test table scan if it can't be better.
 
5369
    Prefer key lookup if we would use the same key for scanning.
 
5370
 
 
5371
    Don't do a table scan on InnoDB tables, if we can read the used
 
5372
    parts of the row from any of the used index.
 
5373
    This is because table scans uses index and we would not win
 
5374
    anything by using a table scan.
 
5375
 
 
5376
    A word for word translation of the below if-statement in sergefp's
 
5377
    understanding: we check if we should use table scan if:
 
5378
    (1) The found 'ref' access produces more records than a table scan
 
5379
        (or index scan, or quick select), or 'ref' is more expensive than
 
5380
        any of them.
 
5381
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5382
        'range' access using index IDX, and the best way to perform 'ref' 
 
5383
        access is to use the same index IDX, with the same or more key parts.
 
5384
        (note: it is not clear how this rule is/should be extended to 
 
5385
        index_merge quick selects)
 
5386
    (3) See above note about InnoDB.
 
5387
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5388
             path, but there is no quick select)
 
5389
        If the condition in the above brackets holds, then the only possible
 
5390
        "table scan" access method is ALL/index (there is no quick select).
 
5391
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5392
        choose it over ALL/index, there is no need to consider a full table
 
5393
        scan.
 
5394
  */
 
5395
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5396
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5397
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5398
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5399
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5400
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5401
  {                                             // Check full join
 
5402
    ha_rows rnd_records= s->found_records;
 
5403
    /*
 
5404
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5405
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5406
      preceding this table in the join order we're now considering), then 
 
5407
      assume that 25% of the rows will be filtered out by this condition.
 
5408
 
 
5409
      This heuristic is supposed to force tables used in exprZ to be before
 
5410
      this table in join order.
 
5411
    */
 
5412
    if (found_constraint)
 
5413
      rnd_records-= rnd_records/4;
 
5414
 
 
5415
    /*
 
5416
      If applicable, get a more accurate estimate. Don't use the two
 
5417
      heuristics at once.
 
5418
    */
 
5419
    if (s->table->quick_condition_rows != s->found_records)
 
5420
      rnd_records= s->table->quick_condition_rows;
 
5421
 
 
5422
    /*
 
5423
      Range optimizer never proposes a RANGE if it isn't better
 
5424
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5425
      Here we estimate its cost.
 
5426
    */
 
5427
    if (s->quick)
 
5428
    {
 
5429
      /*
 
5430
        For each record we:
 
5431
        - read record range through 'quick'
 
5432
        - skip rows which does not satisfy WHERE constraints
 
5433
        TODO: 
 
5434
        We take into account possible use of join cache for ALL/index
 
5435
        access (see first else-branch below), but we don't take it into 
 
5436
        account here for range/index_merge access. Find out why this is so.
 
5437
      */
 
5438
      tmp= record_count *
 
5439
        (s->quick->read_time +
 
5440
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5441
    }
 
5442
    else
 
5443
    {
 
5444
      /* Estimate cost of reading table. */
 
5445
      tmp= s->table->file->scan_time();
 
5446
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5447
      {
 
5448
        /*
 
5449
          For each record we have to:
 
5450
          - read the whole table record 
 
5451
          - skip rows which does not satisfy join condition
 
5452
        */
 
5453
        tmp= record_count *
 
5454
          (tmp +
 
5455
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5456
      }
 
5457
      else
 
5458
      {
 
5459
        /* We read the table as many times as join buffer becomes full. */
 
5460
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5461
                           record_count /
 
5462
                           (double) thd->variables.join_buff_size));
 
5463
        /* 
 
5464
            We don't make full cartesian product between rows in the scanned
 
5465
           table and existing records because we skip all rows from the
 
5466
           scanned table, which does not satisfy join condition when 
 
5467
           we read the table (see flush_cached_records for details). Here we
 
5468
           take into account cost to read and skip these records.
 
5469
        */
 
5470
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5471
      }
 
5472
    }
 
5473
 
 
5474
    /*
 
5475
      We estimate the cost of evaluating WHERE clause for found records
 
5476
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5477
      tmp give us total cost of using Table SCAN
 
5478
    */
 
5479
    if (best == DBL_MAX ||
 
5480
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5481
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5482
    {
 
5483
      /*
 
5484
        If the table has a range (s->quick is set) make_join_select()
 
5485
        will ensure that this will be used
 
5486
      */
 
5487
      best= tmp;
 
5488
      records= rows2double(rnd_records);
 
5489
      best_key= 0;
 
5490
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5491
      best_ref_depends_map= 0;
 
5492
      best_is_sj_inside_out= false;
 
5493
    }
 
5494
  }
 
5495
 
 
5496
  /* Update the cost information for the current partial plan */
 
5497
  join->positions[idx].records_read= records;
 
5498
  join->positions[idx].read_time=    best;
 
5499
  join->positions[idx].key=          best_key;
 
5500
  join->positions[idx].table=        s;
 
5501
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5502
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5503
 
 
5504
  if (!best_key &&
 
5505
      idx == join->const_tables &&
 
5506
      s->table == join->sort_by_table &&
 
5507
      join->unit->select_limit_cnt >= records)
 
5508
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
5509
 
 
5510
  return;
 
5511
}
 
5512
 
 
5513
 
 
5514
/**
 
5515
  Selects and invokes a search strategy for an optimal query plan.
 
5516
 
 
5517
  The function checks user-configurable parameters that control the search
 
5518
  strategy for an optimal plan, selects the search method and then invokes
 
5519
  it. Each specific optimization procedure stores the final optimal plan in
 
5520
  the array 'join->best_positions', and the cost of the plan in
 
5521
  'join->best_read'.
 
5522
 
 
5523
  @param join         pointer to the structure providing all context info for
 
5524
                      the query
 
5525
  @param join_tables  set of the tables in the query
 
5526
 
 
5527
  @todo
 
5528
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5529
    the greedy version. Will be removed when greedy_search is approved.
 
5530
 
 
5531
  @retval
 
5532
    false       ok
 
5533
  @retval
 
5534
    true        Fatal error
 
5535
*/
 
5536
 
 
5537
static bool
 
5538
choose_plan(JOIN *join, table_map join_tables)
 
5539
{
 
5540
  uint32_t search_depth= join->thd->variables.optimizer_search_depth;
 
5541
  uint32_t prune_level=  join->thd->variables.optimizer_prune_level;
 
5542
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5543
 
 
5544
  join->cur_embedding_map= 0;
 
5545
  reset_nj_counters(join->join_list);
 
5546
  /*
 
5547
    if (SELECT_STRAIGHT_JOIN option is set)
 
5548
      reorder tables so dependent tables come after tables they depend 
 
5549
      on, otherwise keep tables in the order they were specified in the query 
 
5550
    else
 
5551
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5552
      records accessed.
 
5553
  */
 
5554
  my_qsort(join->best_ref + join->const_tables,
 
5555
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5556
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5557
  join->cur_emb_sj_nests= 0;
 
5558
  if (straight_join)
 
5559
  {
 
5560
    optimize_straight_join(join, join_tables);
 
5561
  }
 
5562
  else
 
5563
  {
 
5564
    if (search_depth == MAX_TABLES+2)
 
5565
    { /*
 
5566
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5567
        the greedy version. Will be removed when greedy_search is approved.
 
5568
      */
 
5569
      join->best_read= DBL_MAX;
 
5570
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5571
        return(true);
 
5572
    } 
 
5573
    else
 
5574
    {
 
5575
      if (search_depth == 0)
 
5576
        /* Automatically determine a reasonable value for 'search_depth' */
 
5577
        search_depth= determine_search_depth(join);
 
5578
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5579
        return(true);
 
5580
    }
 
5581
  }
 
5582
 
 
5583
  /* 
 
5584
    Store the cost of this query into a user variable
 
5585
    Don't update last_query_cost for statements that are not "flat joins" :
 
5586
    i.e. they have subqueries, unions or call stored procedures.
 
5587
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5588
  */
 
5589
  if (join->thd->lex->is_single_level_stmt())
 
5590
    join->thd->status_var.last_query_cost= join->best_read;
 
5591
  return(false);
 
5592
}
 
5593
 
 
5594
 
 
5595
/**
 
5596
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5597
 
 
5598
  @param ptr1 pointer to first JOIN_TAB object
 
5599
  @param ptr2 pointer to second JOIN_TAB object
791
5600
 
792
5601
  NOTES
793
5602
    The order relation implemented by join_tab_cmp() is not transitive,
799
5608
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
800
5609
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
801
5610
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
802
 
 
 
5611
     
803
5612
  @retval
804
5613
    1  if first is bigger
805
5614
  @retval
807
5616
  @retval
808
5617
    0  if equal
809
5618
*/
810
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5619
 
 
5620
static int
 
5621
join_tab_cmp(const void* ptr1, const void* ptr2)
811
5622
{
812
 
  JoinTable *jt1= *(JoinTable**) ptr1;
813
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5623
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5624
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
814
5625
 
815
5626
  if (jt1->dependent & jt2->table->map)
816
5627
    return 1;
817
5628
  if (jt2->dependent & jt1->table->map)
818
 
    return -1;
 
5629
    return -1;  
819
5630
  if (jt1->found_records > jt2->found_records)
820
5631
    return 1;
821
5632
  if (jt1->found_records < jt2->found_records)
822
 
    return -1;
 
5633
    return -1; 
823
5634
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
824
5635
}
825
5636
 
 
5637
 
826
5638
/**
827
5639
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
828
5640
*/
829
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5641
 
 
5642
static int
 
5643
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
830
5644
{
831
 
  JoinTable *jt1= *(JoinTable**) ptr1;
832
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5645
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5646
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
833
5647
 
834
5648
  if (jt1->dependent & jt2->table->map)
835
5649
    return 1;
839
5653
}
840
5654
 
841
5655
/**
 
5656
  Heuristic procedure to automatically guess a reasonable degree of
 
5657
  exhaustiveness for the greedy search procedure.
 
5658
 
 
5659
  The procedure estimates the optimization time and selects a search depth
 
5660
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5661
  find. If the number of tables in the query exceeds some constant, then
 
5662
  search_depth is set to this constant.
 
5663
 
 
5664
  @param join   pointer to the structure providing all context info for
 
5665
                the query
 
5666
 
 
5667
  @note
 
5668
    This is an extremely simplistic implementation that serves as a stub for a
 
5669
    more advanced analysis of the join. Ideally the search depth should be
 
5670
    determined by learning from previous query optimizations, because it will
 
5671
    depend on the CPU power (and other factors).
 
5672
 
 
5673
  @todo
 
5674
    this value should be determined dynamically, based on statistics:
 
5675
    uint32_t max_tables_for_exhaustive_opt= 7;
 
5676
 
 
5677
  @todo
 
5678
    this value could be determined by some mapping of the form:
 
5679
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5680
 
 
5681
  @return
 
5682
    A positive integer that specifies the search depth (and thus the
 
5683
    exhaustiveness) of the depth-first search algorithm used by
 
5684
    'greedy_search'.
 
5685
*/
 
5686
 
 
5687
static uint
 
5688
determine_search_depth(JOIN *join)
 
5689
{
 
5690
  uint32_t table_count=  join->tables - join->const_tables;
 
5691
  uint32_t search_depth;
 
5692
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5693
  uint32_t max_tables_for_exhaustive_opt= 7;
 
5694
 
 
5695
  if (table_count <= max_tables_for_exhaustive_opt)
 
5696
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5697
  else
 
5698
    /*
 
5699
      TODO: this value could be determined by some mapping of the form:
 
5700
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5701
    */
 
5702
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5703
 
 
5704
  return search_depth;
 
5705
}
 
5706
 
 
5707
 
 
5708
/**
 
5709
  Select the best ways to access the tables in a query without reordering them.
 
5710
 
 
5711
    Find the best access paths for each query table and compute their costs
 
5712
    according to their order in the array 'join->best_ref' (thus without
 
5713
    reordering the join tables). The function calls sequentially
 
5714
    'best_access_path' for each table in the query to select the best table
 
5715
    access method. The final optimal plan is stored in the array
 
5716
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5717
 
 
5718
  @param join          pointer to the structure providing all context info for
 
5719
                       the query
 
5720
  @param join_tables   set of the tables in the query
 
5721
 
 
5722
  @note
 
5723
    This function can be applied to:
 
5724
    - queries with STRAIGHT_JOIN
 
5725
    - internally to compute the cost of an arbitrary QEP
 
5726
  @par
 
5727
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5728
    optimization process to finalize a QEP as it is.
 
5729
*/
 
5730
 
 
5731
static void
 
5732
optimize_straight_join(JOIN *join, table_map join_tables)
 
5733
{
 
5734
  JOIN_TAB *s;
 
5735
  uint32_t idx= join->const_tables;
 
5736
  double    record_count= 1.0;
 
5737
  double    read_time=    0.0;
 
5738
 
 
5739
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5740
  {
 
5741
    /* Find the best access method from 's' to the current partial plan */
 
5742
    advance_sj_state(join_tables, s);
 
5743
    best_access_path(join, s, join->thd, join_tables, idx,
 
5744
                     record_count, read_time);
 
5745
    /* compute the cost of the new plan extended with 's' */
 
5746
    record_count*= join->positions[idx].records_read;
 
5747
    read_time+=    join->positions[idx].read_time;
 
5748
    join_tables&= ~(s->table->map);
 
5749
    ++idx;
 
5750
  }
 
5751
 
 
5752
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5753
  if (join->sort_by_table &&
 
5754
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5755
    read_time+= record_count;  // We have to make a temp table
 
5756
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5757
  join->best_read= read_time;
 
5758
}
 
5759
 
 
5760
 
 
5761
/**
 
5762
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5763
 
 
5764
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5765
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5766
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5767
    selects the most promising table, and extends the current partial QEP with
 
5768
    that table.  Currenly the most 'promising' table is the one with least
 
5769
    expensive extension.\
 
5770
 
 
5771
    There are two extreme cases:
 
5772
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5773
    best complete continuation of the partial QEP. This continuation can be
 
5774
    used directly as a result of the search.
 
5775
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5776
    consideres the extension of the current QEP with each of the remaining
 
5777
    unoptimized tables.
 
5778
 
 
5779
    All other cases are in-between these two extremes. Thus the parameter
 
5780
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5781
    value, the longer the optimizaton time and possibly the better the
 
5782
    resulting plan. The lower the value, the fewer alternative plans are
 
5783
    estimated, but the more likely to get a bad QEP.
 
5784
 
 
5785
    All intermediate and final results of the procedure are stored in 'join':
 
5786
    - join->positions     : modified for every partial QEP that is explored
 
5787
    - join->best_positions: modified for the current best complete QEP
 
5788
    - join->best_read     : modified for the current best complete QEP
 
5789
    - join->best_ref      : might be partially reordered
 
5790
 
 
5791
    The final optimal plan is stored in 'join->best_positions', and its
 
5792
    corresponding cost in 'join->best_read'.
 
5793
 
 
5794
  @note
 
5795
    The following pseudocode describes the algorithm of 'greedy_search':
 
5796
 
 
5797
    @code
 
5798
    procedure greedy_search
 
5799
    input: remaining_tables
 
5800
    output: pplan;
 
5801
    {
 
5802
      pplan = <>;
 
5803
      do {
 
5804
        (t, a) = best_extension(pplan, remaining_tables);
 
5805
        pplan = concat(pplan, (t, a));
 
5806
        remaining_tables = remaining_tables - t;
 
5807
      } while (remaining_tables != {})
 
5808
      return pplan;
 
5809
    }
 
5810
 
 
5811
  @endcode
 
5812
    where 'best_extension' is a placeholder for a procedure that selects the
 
5813
    most "promising" of all tables in 'remaining_tables'.
 
5814
    Currently this estimate is performed by calling
 
5815
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5816
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5817
    mainly depends on that of 'best_extension_by_limited_search'.
 
5818
 
 
5819
  @par
 
5820
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5821
    worst-case complexity of this algorithm is <=
 
5822
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5823
    complexity of greedy_search is O(N!).
 
5824
 
 
5825
  @par
 
5826
    In the future, 'greedy_search' might be extended to support other
 
5827
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5828
 
 
5829
  @param join             pointer to the structure providing all context info
 
5830
                          for the query
 
5831
  @param remaining_tables set of tables not included into the partial plan yet
 
5832
  @param search_depth     controlls the exhaustiveness of the search
 
5833
  @param prune_level      the pruning heuristics that should be applied during
 
5834
                          search
 
5835
 
 
5836
  @retval
 
5837
    false       ok
 
5838
  @retval
 
5839
    true        Fatal error
 
5840
*/
 
5841
 
 
5842
static bool
 
5843
greedy_search(JOIN      *join,
 
5844
              table_map remaining_tables,
 
5845
              uint32_t      search_depth,
 
5846
              uint32_t      prune_level)
 
5847
{
 
5848
  double    record_count= 1.0;
 
5849
  double    read_time=    0.0;
 
5850
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
5851
  uint32_t      best_idx;
 
5852
  uint32_t      size_remain;    // cardinality of remaining_tables
 
5853
  POSITION  best_pos;
 
5854
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5855
 
 
5856
  /* number of tables that remain to be optimized */
 
5857
  size_remain= my_count_bits(remaining_tables);
 
5858
 
 
5859
  do {
 
5860
    /* Find the extension of the current QEP with the lowest cost */
 
5861
    join->best_read= DBL_MAX;
 
5862
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5863
                                         read_time, search_depth, prune_level))
 
5864
      return(true);
 
5865
 
 
5866
    if (size_remain <= search_depth)
 
5867
    {
 
5868
      /*
 
5869
        'join->best_positions' contains a complete optimal extension of the
 
5870
        current partial QEP.
 
5871
      */
 
5872
      return(false);
 
5873
    }
 
5874
 
 
5875
    /* select the first table in the optimal extension as most promising */
 
5876
    best_pos= join->best_positions[idx];
 
5877
    best_table= best_pos.table;
 
5878
    /*
 
5879
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5880
      'join->positions' for cost estimates, therefore we have to update its
 
5881
      value.
 
5882
    */
 
5883
    join->positions[idx]= best_pos;
 
5884
 
 
5885
    /* find the position of 'best_table' in 'join->best_ref' */
 
5886
    best_idx= idx;
 
5887
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5888
    while (pos && best_table != pos)
 
5889
      pos= join->best_ref[++best_idx];
 
5890
    assert((pos != NULL)); // should always find 'best_table'
 
5891
    /* move 'best_table' at the first free position in the array of joins */
 
5892
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
5893
 
 
5894
    /* compute the cost of the new plan extended with 'best_table' */
 
5895
    record_count*= join->positions[idx].records_read;
 
5896
    read_time+=    join->positions[idx].read_time;
 
5897
 
 
5898
    remaining_tables&= ~(best_table->table->map);
 
5899
    --size_remain;
 
5900
    ++idx;
 
5901
  } while (true);
 
5902
}
 
5903
 
 
5904
 
 
5905
/**
 
5906
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5907
  exhaustive search.
 
5908
 
 
5909
    The procedure searches for the optimal ordering of the query tables in set
 
5910
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5911
    each table. The choice of a table order and an access path for each table
 
5912
    constitutes a query execution plan (QEP) that fully specifies how to
 
5913
    execute the query.
 
5914
   
 
5915
    The maximal size of the found plan is controlled by the parameter
 
5916
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5917
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5918
    of only some of the query tables. Such "partial" optimal plans are useful
 
5919
    only as input to query optimization procedures, and cannot be used directly
 
5920
    to execute a query.
 
5921
 
 
5922
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5923
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5924
    evaluates the cost of the partial plan extended by all access plans for
 
5925
    each of the relations in 'remaining_tables', expands the current partial
 
5926
    plan with the access plan that results in lowest cost of the expanded
 
5927
    partial plan, and removes the corresponding relation from
 
5928
    'remaining_tables'. The algorithm continues until it either constructs a
 
5929
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5930
    search_depth.
 
5931
 
 
5932
    The final optimal plan is stored in 'join->best_positions'. The
 
5933
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5934
 
 
5935
  @note
 
5936
    The procedure uses a recursive depth-first search where the depth of the
 
5937
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5938
    parameter 'search_depth'.
 
5939
 
 
5940
  @note
 
5941
    The pseudocode below describes the algorithm of
 
5942
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5943
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5944
    the complexity of greedy_search is O(N!).
 
5945
 
 
5946
    @code
 
5947
    procedure best_extension_by_limited_search(
 
5948
      pplan in,             // in, partial plan of tables-joined-so-far
 
5949
      pplan_cost,           // in, cost of pplan
 
5950
      remaining_tables,     // in, set of tables not referenced in pplan
 
5951
      best_plan_so_far,     // in/out, best plan found so far
 
5952
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5953
      search_depth)         // in, maximum size of the plans being considered
 
5954
    {
 
5955
      for each table T from remaining_tables
 
5956
      {
 
5957
        // Calculate the cost of using table T as above
 
5958
        cost = complex-series-of-calculations;
 
5959
 
 
5960
        // Add the cost to the cost so far.
 
5961
        pplan_cost+= cost;
 
5962
 
 
5963
        if (pplan_cost >= best_plan_so_far_cost)
 
5964
          // pplan_cost already too great, stop search
 
5965
          continue;
 
5966
 
 
5967
        pplan= expand pplan by best_access_method;
 
5968
        remaining_tables= remaining_tables - table T;
 
5969
        if (remaining_tables is not an empty set
 
5970
            and
 
5971
            search_depth > 1)
 
5972
        {
 
5973
          best_extension_by_limited_search(pplan, pplan_cost,
 
5974
                                           remaining_tables,
 
5975
                                           best_plan_so_far,
 
5976
                                           best_plan_so_far_cost,
 
5977
                                           search_depth - 1);
 
5978
        }
 
5979
        else
 
5980
        {
 
5981
          best_plan_so_far_cost= pplan_cost;
 
5982
          best_plan_so_far= pplan;
 
5983
        }
 
5984
      }
 
5985
    }
 
5986
    @endcode
 
5987
 
 
5988
  @note
 
5989
    When 'best_extension_by_limited_search' is called for the first time,
 
5990
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
5991
    The actual implementation provides a way to optionally use pruning
 
5992
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
5993
    space by skipping some partial plans.
 
5994
 
 
5995
  @note
 
5996
    The parameter 'search_depth' provides control over the recursion
 
5997
    depth, and thus the size of the resulting optimal plan.
 
5998
 
 
5999
  @param join             pointer to the structure providing all context info
 
6000
                          for the query
 
6001
  @param remaining_tables set of tables not included into the partial plan yet
 
6002
  @param idx              length of the partial QEP in 'join->positions';
 
6003
                          since a depth-first search is used, also corresponds
 
6004
                          to the current depth of the search tree;
 
6005
                          also an index in the array 'join->best_ref';
 
6006
  @param record_count     estimate for the number of records returned by the
 
6007
                          best partial plan
 
6008
  @param read_time        the cost of the best partial plan
 
6009
  @param search_depth     maximum depth of the recursion and thus size of the
 
6010
                          found optimal plan
 
6011
                          (0 < search_depth <= join->tables+1).
 
6012
  @param prune_level      pruning heuristics that should be applied during
 
6013
                          optimization
 
6014
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6015
 
 
6016
  @retval
 
6017
    false       ok
 
6018
  @retval
 
6019
    true        Fatal error
 
6020
*/
 
6021
 
 
6022
static bool
 
6023
best_extension_by_limited_search(JOIN      *join,
 
6024
                                 table_map remaining_tables,
 
6025
                                 uint32_t      idx,
 
6026
                                 double    record_count,
 
6027
                                 double    read_time,
 
6028
                                 uint32_t      search_depth,
 
6029
                                 uint32_t      prune_level)
 
6030
{
 
6031
  THD *thd= join->thd;
 
6032
  if (thd->killed)  // Abort
 
6033
    return(true);
 
6034
 
 
6035
  /* 
 
6036
     'join' is a partial plan with lower cost than the best plan so far,
 
6037
     so continue expanding it further with the tables in 'remaining_tables'.
 
6038
  */
 
6039
  JOIN_TAB *s;
 
6040
  double best_record_count= DBL_MAX;
 
6041
  double best_read_time=    DBL_MAX;
 
6042
 
 
6043
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6044
  {
 
6045
    table_map real_table_bit= s->table->map;
 
6046
    if ((remaining_tables & real_table_bit) && 
 
6047
        !(remaining_tables & s->dependent) && 
 
6048
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6049
    {
 
6050
      double current_record_count, current_read_time;
 
6051
      advance_sj_state(remaining_tables, s);
 
6052
 
 
6053
      /*
 
6054
        psergey-insideout-todo: 
 
6055
          when best_access_path() detects it could do an InsideOut scan or 
 
6056
          some other scan, have it return an insideout scan and a flag that 
 
6057
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6058
          when the depth is insufficient??)
 
6059
      */
 
6060
      /* Find the best access method from 's' to the current partial plan */
 
6061
      best_access_path(join, s, thd, remaining_tables, idx,
 
6062
                       record_count, read_time);
 
6063
      /* Compute the cost of extending the plan with 's' */
 
6064
      current_record_count= record_count * join->positions[idx].records_read;
 
6065
      current_read_time=    read_time + join->positions[idx].read_time;
 
6066
 
 
6067
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6068
      if ((current_read_time +
 
6069
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6070
      {
 
6071
        restore_prev_nj_state(s);
 
6072
        restore_prev_sj_state(remaining_tables, s);
 
6073
        continue;
 
6074
      }
 
6075
 
 
6076
      /*
 
6077
        Prune some less promising partial plans. This heuristic may miss
 
6078
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6079
      */
 
6080
      if (prune_level == 1)
 
6081
      {
 
6082
        if (best_record_count > current_record_count ||
 
6083
            best_read_time > current_read_time ||
 
6084
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6085
        {
 
6086
          if (best_record_count >= current_record_count &&
 
6087
              best_read_time >= current_read_time &&
 
6088
              /* TODO: What is the reasoning behind this condition? */
 
6089
              (!(s->key_dependent & remaining_tables) ||
 
6090
               join->positions[idx].records_read < 2.0))
 
6091
          {
 
6092
            best_record_count= current_record_count;
 
6093
            best_read_time=    current_read_time;
 
6094
          }
 
6095
        }
 
6096
        else
 
6097
        {
 
6098
          restore_prev_nj_state(s);
 
6099
          restore_prev_sj_state(remaining_tables, s);
 
6100
          continue;
 
6101
        }
 
6102
      }
 
6103
 
 
6104
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6105
      { /* Recursively expand the current partial plan */
 
6106
        std::swap(join->best_ref[idx], *pos);
 
6107
        if (best_extension_by_limited_search(join,
 
6108
                                             remaining_tables & ~real_table_bit,
 
6109
                                             idx + 1,
 
6110
                                             current_record_count,
 
6111
                                             current_read_time,
 
6112
                                             search_depth - 1,
 
6113
                                             prune_level))
 
6114
          return(true);
 
6115
        std::swap(join->best_ref[idx], *pos);
 
6116
      }
 
6117
      else
 
6118
      { /*
 
6119
          'join' is either the best partial QEP with 'search_depth' relations,
 
6120
          or the best complete QEP so far, whichever is smaller.
 
6121
        */
 
6122
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6123
        if (join->sort_by_table &&
 
6124
            join->sort_by_table !=
 
6125
            join->positions[join->const_tables].table->table)
 
6126
          /* We have to make a temp table */
 
6127
          current_read_time+= current_record_count;
 
6128
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6129
        {
 
6130
          memcpy(join->best_positions, join->positions,
 
6131
                 sizeof(POSITION) * (idx + 1));
 
6132
          join->best_read= current_read_time - 0.001;
 
6133
        }
 
6134
      }
 
6135
      restore_prev_nj_state(s);
 
6136
      restore_prev_sj_state(remaining_tables, s);
 
6137
    }
 
6138
  }
 
6139
  return(false);
 
6140
}
 
6141
 
 
6142
 
 
6143
/**
 
6144
  @todo
 
6145
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6146
  tested and accepted.
 
6147
 
 
6148
  RETURN VALUES
 
6149
    false       ok
 
6150
    true        Fatal error
 
6151
*/
 
6152
static bool
 
6153
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
 
6154
          double read_time)
 
6155
{
 
6156
  THD *thd= join->thd;
 
6157
  if (thd->killed)
 
6158
    return(true);
 
6159
  if (!rest_tables)
 
6160
  {
 
6161
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6162
    if (join->sort_by_table &&
 
6163
        join->sort_by_table !=
 
6164
        join->positions[join->const_tables].table->table)
 
6165
      read_time+=record_count;                  // We have to make a temp table
 
6166
    if (read_time < join->best_read)
 
6167
    {
 
6168
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6169
      join->best_read= read_time - 0.001;
 
6170
    }
 
6171
    return(false);
 
6172
  }
 
6173
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6174
    return(false);                                      /* Found better before */
 
6175
 
 
6176
  JOIN_TAB *s;
 
6177
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6178
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6179
  {
 
6180
    table_map real_table_bit=s->table->map;
 
6181
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6182
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6183
    {
 
6184
      double records, best;
 
6185
      advance_sj_state(rest_tables, s);
 
6186
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6187
                       read_time);
 
6188
      records= join->positions[idx].records_read;
 
6189
      best= join->positions[idx].read_time;
 
6190
      /*
 
6191
        Go to the next level only if there hasn't been a better key on
 
6192
        this level! This will cut down the search for a lot simple cases!
 
6193
      */
 
6194
      double current_record_count=record_count*records;
 
6195
      double current_read_time=read_time+best;
 
6196
      if (best_record_count > current_record_count ||
 
6197
          best_read_time > current_read_time ||
 
6198
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6199
      {
 
6200
        if (best_record_count >= current_record_count &&
 
6201
            best_read_time >= current_read_time &&
 
6202
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6203
        {
 
6204
          best_record_count=current_record_count;
 
6205
          best_read_time=current_read_time;
 
6206
        }
 
6207
        std::swap(join->best_ref[idx], *pos);
 
6208
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6209
                      current_record_count,current_read_time))
 
6210
          return(true);
 
6211
        std::swap(join->best_ref[idx], *pos);
 
6212
      }
 
6213
      restore_prev_nj_state(s);
 
6214
      restore_prev_sj_state(rest_tables, s);
 
6215
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6216
        break;                          // Don't test all combinations
 
6217
    }
 
6218
  }
 
6219
  return(false);
 
6220
}
 
6221
 
 
6222
 
 
6223
/**
842
6224
  Find how much space the prevous read not const tables takes in cache.
843
6225
*/
844
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6226
 
 
6227
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6228
                                   JOIN_TAB *join_tab)
845
6229
{
846
6230
  uint32_t null_fields,blobs,fields,rec_length;
847
6231
  Field **f_ptr,*field;
 
6232
  MY_BITMAP *read_set= join_tab->table->read_set;;
848
6233
 
849
6234
  null_fields= blobs= fields= rec_length=0;
850
 
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
 
6235
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
851
6236
  {
852
 
    if (field->isReadSet())
 
6237
    if (bitmap_is_set(read_set, field->field_index))
853
6238
    {
854
6239
      uint32_t flags=field->flags;
855
6240
      fields++;
856
6241
      rec_length+=field->pack_length();
857
6242
      if (flags & BLOB_FLAG)
858
 
        blobs++;
 
6243
        blobs++;
859
6244
      if (!(flags & NOT_NULL_FLAG))
860
 
        null_fields++;
 
6245
        null_fields++;
861
6246
    }
862
6247
  }
863
6248
  if (null_fields)
866
6251
    rec_length+=sizeof(bool);
867
6252
  if (blobs)
868
6253
  {
869
 
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
870
 
                                     (join_tab->table->getRecordLength()- rec_length));
871
 
    rec_length+= max((uint32_t)4,blob_length);
872
 
  }
873
 
  join_tab->used_fields= fields;
874
 
  join_tab->used_fieldlength= rec_length;
875
 
  join_tab->used_blobs= blobs;
876
 
}
877
 
 
878
 
StoredKey *get_store_key(Session *session,
879
 
                         optimizer::KeyUse *keyuse,
880
 
                         table_map used_tables,
881
 
                         KeyPartInfo *key_part,
882
 
                         unsigned char *key_buff,
883
 
                         uint32_t maybe_null)
884
 
{
885
 
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
886
 
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
887
 
  {
888
 
    return new store_key_const_item(session,
889
 
                                    key_part->field,
890
 
                                    key_buff + maybe_null,
891
 
                                    maybe_null ? key_buff : 0,
892
 
                                    key_part->length,
893
 
                                    key_use_val);
894
 
  }
895
 
  else if (key_use_val->type() == Item::FIELD_ITEM ||
896
 
           (key_use_val->type() == Item::REF_ITEM &&
897
 
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
898
 
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
899
 
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
900
 
  {
901
 
    return new store_key_field(session,
902
 
                               key_part->field,
903
 
                               key_buff + maybe_null,
904
 
                               maybe_null ? key_buff : 0,
905
 
                               key_part->length,
906
 
                               ((Item_field*) key_use_val->real_item())->field,
907
 
                               key_use_val->full_name());
908
 
  }
909
 
  return new store_key_item(session,
910
 
                            key_part->field,
911
 
                            key_buff + maybe_null,
912
 
                            maybe_null ? key_buff : 0,
913
 
                            key_part->length,
914
 
                            key_use_val);
915
 
}
 
6254
    uint32_t blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6255
                             (join_tab->table->getRecordLength()- rec_length));
 
6256
    rec_length+=(uint) cmax((uint)4,blob_length);
 
6257
  }
 
6258
  join_tab->used_fields=fields;
 
6259
  join_tab->used_fieldlength=rec_length;
 
6260
  join_tab->used_blobs=blobs;
 
6261
}
 
6262
 
 
6263
 
 
6264
static uint
 
6265
cache_record_length(JOIN *join,uint32_t idx)
 
6266
{
 
6267
  uint32_t length=0;
 
6268
  JOIN_TAB **pos,**end;
 
6269
  THD *thd=join->thd;
 
6270
 
 
6271
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6272
       pos != end ;
 
6273
       pos++)
 
6274
  {
 
6275
    JOIN_TAB *join_tab= *pos;
 
6276
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6277
      calc_used_field_length(thd, join_tab);
 
6278
    length+=join_tab->used_fieldlength;
 
6279
  }
 
6280
  return length;
 
6281
}
 
6282
 
 
6283
 
 
6284
/*
 
6285
  Get the number of different row combinations for subset of partial join
 
6286
 
 
6287
  SYNOPSIS
 
6288
    prev_record_reads()
 
6289
      join       The join structure
 
6290
      idx        Number of tables in the partial join order (i.e. the
 
6291
                 partial join order is in join->positions[0..idx-1])
 
6292
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6293
                 row combinations.
 
6294
 
 
6295
  DESCRIPTION
 
6296
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6297
    tables within that join order (specified in found_ref), find out how many
 
6298
    distinct row combinations of subset tables will be in the result of the
 
6299
    partial join order.
 
6300
     
 
6301
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6302
    method. The ref access depends on current rows of tables in found_ref.
 
6303
    We want to count # of different ref accesses. We assume two ref accesses
 
6304
    will be different if at least one of access parameters is different.
 
6305
    Example: consider a query
 
6306
 
 
6307
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6308
 
 
6309
    and a join order:
 
6310
      t1,  ref access on t1.key=c1
 
6311
      t2,  ref access on t2.key=c2       
 
6312
      t3,  ref access on t3.key=t1.field 
 
6313
    
 
6314
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6315
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6316
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6317
            n_distinct_ref_scans = #records_read(t1)
 
6318
    
 
6319
    The reason for having this function (at least the latest version of it)
 
6320
    is that we need to account for buffering in join execution. 
 
6321
    
 
6322
    An edge-case example: if we have a non-first table in join accessed via
 
6323
    ref(const) or ref(param) where there is a small number of different
 
6324
    values of param, then the access will likely hit the disk cache and will
 
6325
    not require any disk seeks.
 
6326
    
 
6327
    The proper solution would be to assume an LRU disk cache of some size,
 
6328
    calculate probability of cache hits, etc. For now we just count
 
6329
    identical ref accesses as one.
 
6330
 
 
6331
  RETURN 
 
6332
    Expected number of row combinations
 
6333
*/
 
6334
 
 
6335
static double
 
6336
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
6337
{
 
6338
  double found=1.0;
 
6339
  POSITION *pos_end= join->positions - 1;
 
6340
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6341
  {
 
6342
    if (pos->table->table->map & found_ref)
 
6343
    {
 
6344
      found_ref|= pos->ref_depend_map;
 
6345
      /* 
 
6346
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6347
        with no matching row we will get position[t2].records_read==0. 
 
6348
        Actually the size of output is one null-complemented row, therefore 
 
6349
        we will use value of 1 whenever we get records_read==0.
 
6350
 
 
6351
        Note
 
6352
        - the above case can't occur if inner part of outer join has more 
 
6353
          than one table: table with no matches will not be marked as const.
 
6354
 
 
6355
        - Ideally we should add 1 to records_read for every possible null-
 
6356
          complemented row. We're not doing it because: 1. it will require
 
6357
          non-trivial code and add overhead. 2. The value of records_read
 
6358
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6359
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6360
      */
 
6361
      if (pos->records_read > DBL_EPSILON)
 
6362
        found*= pos->records_read;
 
6363
    }
 
6364
  }
 
6365
  return found;
 
6366
}
 
6367
 
916
6368
 
917
6369
/**
918
 
  This function is only called for const items on fields which are keys.
919
 
 
920
 
  @return
921
 
    returns 1 if there was some conversion made when the field was stored.
 
6370
  Set up join struct according to best position.
922
6371
*/
923
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
924
 
{
925
 
  bool error;
926
 
  Table *table= field->getTable();
927
 
  Session *session= table->in_use;
928
 
  ha_rows cuted_fields=session->cuted_fields;
929
 
 
930
 
  /*
931
 
    we should restore old value of count_cuted_fields because
932
 
    store_val_in_field can be called from mysql_insert
933
 
    with select_insert, which make count_cuted_fields= 1
934
 
   */
935
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
936
 
  session->count_cuted_fields= check_flag;
937
 
  error= item->save_in_field(field, 1);
938
 
  session->count_cuted_fields= old_count_cuted_fields;
939
 
  return error || cuted_fields != session->cuted_fields;
940
 
}
941
 
 
942
 
inline void add_cond_and_fix(Item **e1, Item *e2)
943
 
{
944
 
  if (*e1)
 
6372
 
 
6373
static bool
 
6374
get_best_combination(JOIN *join)
 
6375
{
 
6376
  uint32_t i,tablenr;
 
6377
  table_map used_tables;
 
6378
  JOIN_TAB *join_tab,*j;
 
6379
  KEYUSE *keyuse;
 
6380
  uint32_t table_count;
 
6381
  THD *thd=join->thd;
 
6382
 
 
6383
  table_count=join->tables;
 
6384
  if (!(join->join_tab=join_tab=
 
6385
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6386
    return(true);
 
6387
 
 
6388
  join->full_join=0;
 
6389
 
 
6390
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6391
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
945
6392
  {
946
 
    Item *res;
947
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6393
    Table *form;
 
6394
    *j= *join->best_positions[tablenr].table;
 
6395
    form=join->table[tablenr]=j->table;
 
6396
    used_tables|= form->map;
 
6397
    form->reginfo.join_tab=j;
 
6398
    if (!*j->on_expr_ref)
 
6399
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6400
    if (j->type == JT_CONST)
 
6401
      continue;                                 // Handled in make_join_stat..
 
6402
 
 
6403
    j->ref.key = -1;
 
6404
    j->ref.key_parts=0;
 
6405
 
 
6406
    if (j->type == JT_SYSTEM)
 
6407
      continue;
 
6408
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
948
6409
    {
949
 
      *e1= res;
950
 
      res->quick_fix_field();
 
6410
      j->type=JT_ALL;
 
6411
      if (tablenr != join->const_tables)
 
6412
        join->full_join=1;
951
6413
    }
 
6414
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6415
      return(true);                        // Something went wrong
952
6416
  }
953
 
  else
954
 
    *e1= e2;
 
6417
 
 
6418
  for (i=0 ; i < table_count ; i++)
 
6419
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6420
  update_depend_map(join);
 
6421
  return(0);
955
6422
}
956
6423
 
957
 
bool create_ref_for_key(Join *join, 
958
 
                        JoinTable *j, 
959
 
                        optimizer::KeyUse *org_keyuse,
960
 
                        table_map used_tables)
 
6424
 
 
6425
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6426
                               table_map used_tables)
961
6427
{
962
 
  optimizer::KeyUse *keyuse= org_keyuse;
963
 
  Session  *session= join->session;
964
 
  uint32_t keyparts;
965
 
  uint32_t length;
966
 
  uint32_t key;
967
 
  Table *table= NULL;
968
 
  KeyInfo *keyinfo= NULL;
 
6428
  KEYUSE *keyuse=org_keyuse;
 
6429
  THD  *thd= join->thd;
 
6430
  uint32_t keyparts,length,key;
 
6431
  Table *table;
 
6432
  KEY *keyinfo;
969
6433
 
970
6434
  /*  Use best key from find_best */
971
 
  table= j->table;
972
 
  key= keyuse->getKey();
973
 
  keyinfo= table->key_info + key;
 
6435
  table=j->table;
 
6436
  key=keyuse->key;
 
6437
  keyinfo=table->key_info+key;
974
6438
 
975
6439
  {
976
 
    keyparts= length= 0;
 
6440
    keyparts=length=0;
977
6441
    uint32_t found_part_ref_or_null= 0;
978
6442
    /*
979
6443
      Calculate length for the used key
982
6446
    */
983
6447
    do
984
6448
    {
985
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6449
      if (!(~used_tables & keyuse->used_tables))
986
6450
      {
987
 
        if (keyparts == keyuse->getKeypart() &&
988
 
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
989
 
        {
990
 
          keyparts++;
991
 
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
992
 
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
993
 
        }
 
6451
        if (keyparts == keyuse->keypart &&
 
6452
            !(found_part_ref_or_null & keyuse->optimize))
 
6453
        {
 
6454
          keyparts++;
 
6455
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6456
          found_part_ref_or_null|= keyuse->optimize;
 
6457
        }
994
6458
      }
995
6459
      keyuse++;
996
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6460
    } while (keyuse->table == table && keyuse->key == key);
997
6461
  }
998
6462
 
999
6463
  /* set up fieldref */
1001
6465
  j->ref.key_parts=keyparts;
1002
6466
  j->ref.key_length=length;
1003
6467
  j->ref.key=(int) key;
1004
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1005
 
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1006
 
               (keyparts+1)))) ||
1007
 
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
1008
 
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6468
  if (!(j->ref.key_buff= (unsigned char*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6469
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6470
                                                   (keyparts+1)))) ||
 
6471
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6472
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1009
6473
  {
1010
6474
    return(true);
1011
6475
  }
1015
6479
  j->ref.disable_cache= false;
1016
6480
  keyuse=org_keyuse;
1017
6481
 
1018
 
  StoredKey **ref_key= j->ref.key_copy;
1019
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6482
  store_key **ref_key= j->ref.key_copy;
 
6483
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
1020
6484
  bool keyuse_uses_no_tables= true;
1021
6485
  {
1022
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6486
    uint32_t i;
 
6487
    for (i=0 ; i < keyparts ; keyuse++,i++)
1023
6488
    {
1024
 
      while (keyuse->getKeypart() != i ||
1025
 
             ((~used_tables) & keyuse->getUsedTables()))
1026
 
        keyuse++;       /* Skip other parts */
 
6489
      while (keyuse->keypart != i ||
 
6490
             ((~used_tables) & keyuse->used_tables))
 
6491
        keyuse++;                               /* Skip other parts */
1027
6492
 
1028
6493
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1029
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1030
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1031
 
      if (keyuse->isNullRejected())
 
6494
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6495
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6496
      if (keyuse->null_rejecting) 
1032
6497
        j->ref.null_rejecting |= 1 << i;
1033
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1034
 
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1035
 
      {         // Compare against constant
1036
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6498
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6499
      if (!keyuse->used_tables &&
 
6500
          !(join->select_options & SELECT_DESCRIBE))
 
6501
      {                                 // Compare against constant
 
6502
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1037
6503
                           key_buff + maybe_null,
1038
6504
                           maybe_null ?  key_buff : 0,
1039
 
                           keyinfo->key_part[i].length, keyuse->getVal());
1040
 
        if (session->is_fatal_error)
1041
 
          return(true);
1042
 
        tmp.copy();
 
6505
                           keyinfo->key_part[i].length, keyuse->val);
 
6506
        if (thd->is_fatal_error)
 
6507
          return(true);
 
6508
        tmp.copy();
1043
6509
      }
1044
6510
      else
1045
 
        *ref_key++= get_store_key(session,
1046
 
          keyuse,join->const_table_map,
1047
 
          &keyinfo->key_part[i],
1048
 
          key_buff, maybe_null);
 
6511
        *ref_key++= get_store_key(thd,
 
6512
                                  keyuse,join->const_table_map,
 
6513
                                  &keyinfo->key_part[i],
 
6514
                                  key_buff, maybe_null);
1049
6515
      /*
1050
 
        Remember if we are going to use REF_OR_NULL
1051
 
        But only if field _really_ can be null i.e. we force AM_REF
1052
 
        instead of AM_REF_OR_NULL in case if field can't be null
 
6516
        Remember if we are going to use REF_OR_NULL
 
6517
        But only if field _really_ can be null i.e. we force JT_REF
 
6518
        instead of JT_REF_OR_NULL in case if field can't be null
1053
6519
      */
1054
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1055
 
        null_ref_key= key_buff;
 
6520
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6521
        null_ref_key= key_buff;
1056
6522
      key_buff+=keyinfo->key_part[i].store_length;
1057
6523
    }
1058
6524
  }
1059
 
  *ref_key= 0;       // end_marker
1060
 
  if (j->type == AM_CONST)
 
6525
  *ref_key=0;                           // end_marker
 
6526
  if (j->type == JT_CONST)
1061
6527
    j->table->const_table= 1;
1062
6528
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1063
6529
           keyparts != keyinfo->key_parts || null_ref_key)
1064
6530
  {
1065
6531
    /* Must read with repeat */
1066
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6532
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1067
6533
    j->ref.null_ref_key= null_ref_key;
1068
6534
  }
1069
6535
  else if (keyuse_uses_no_tables)
1075
6541
      Here we should not mark the table as a 'const' as a field may
1076
6542
      have a 'normal' value or a NULL value.
1077
6543
    */
1078
 
    j->type= AM_CONST;
1079
 
  }
1080
 
  else
1081
 
    j->type= AM_EQ_REF;
1082
 
  return 0;
1083
 
}
 
6544
    j->type=JT_CONST;
 
6545
  }
 
6546
  else
 
6547
    j->type=JT_EQ_REF;
 
6548
  return(0);
 
6549
}
 
6550
 
 
6551
 
 
6552
 
 
6553
static store_key *
 
6554
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6555
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
 
6556
{
 
6557
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6558
  {
 
6559
    return new store_key_const_item(thd,
 
6560
                                    key_part->field,
 
6561
                                    key_buff + maybe_null,
 
6562
                                    maybe_null ? key_buff : 0,
 
6563
                                    key_part->length,
 
6564
                                    keyuse->val);
 
6565
  }
 
6566
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6567
           (keyuse->val->type() == Item::REF_ITEM &&
 
6568
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6569
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6570
             Item_ref::DIRECT_REF && 
 
6571
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6572
    return new store_key_field(thd,
 
6573
                               key_part->field,
 
6574
                               key_buff + maybe_null,
 
6575
                               maybe_null ? key_buff : 0,
 
6576
                               key_part->length,
 
6577
                               ((Item_field*) keyuse->val->real_item())->field,
 
6578
                               keyuse->val->full_name());
 
6579
  return new store_key_item(thd,
 
6580
                            key_part->field,
 
6581
                            key_buff + maybe_null,
 
6582
                            maybe_null ? key_buff : 0,
 
6583
                            key_part->length,
 
6584
                            keyuse->val);
 
6585
}
 
6586
 
 
6587
/**
 
6588
  This function is only called for const items on fields which are keys.
 
6589
 
 
6590
  @return
 
6591
    returns 1 if there was some conversion made when the field was stored.
 
6592
*/
 
6593
 
 
6594
bool
 
6595
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6596
{
 
6597
  bool error;
 
6598
  Table *table= field->table;
 
6599
  THD *thd= table->in_use;
 
6600
  ha_rows cuted_fields=thd->cuted_fields;
 
6601
 
 
6602
  /*
 
6603
    we should restore old value of count_cuted_fields because
 
6604
    store_val_in_field can be called from mysql_insert 
 
6605
    with select_insert, which make count_cuted_fields= 1
 
6606
   */
 
6607
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6608
  thd->count_cuted_fields= check_flag;
 
6609
  error= item->save_in_field(field, 1);
 
6610
  thd->count_cuted_fields= old_count_cuted_fields;
 
6611
  return error || cuted_fields != thd->cuted_fields;
 
6612
}
 
6613
 
 
6614
 
 
6615
static bool
 
6616
make_simple_join(JOIN *join,Table *tmp_table)
 
6617
{
 
6618
  Table **tableptr;
 
6619
  JOIN_TAB *join_tab;
 
6620
 
 
6621
  /*
 
6622
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
6623
    to this function through JOIN::exec (may happen for sub-queries).
 
6624
  */
 
6625
  if (!join->table_reexec)
 
6626
  {
 
6627
    if (!(join->table_reexec= (Table**) join->thd->alloc(sizeof(Table*))))
 
6628
      return(true);                        /* purecov: inspected */
 
6629
    if (join->tmp_join)
 
6630
      join->tmp_join->table_reexec= join->table_reexec;
 
6631
  }
 
6632
  if (!join->join_tab_reexec)
 
6633
  {
 
6634
    if (!(join->join_tab_reexec=
 
6635
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6636
      return(true);                        /* purecov: inspected */
 
6637
    if (join->tmp_join)
 
6638
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6639
  }
 
6640
  tableptr= join->table_reexec;
 
6641
  join_tab= join->join_tab_reexec;
 
6642
 
 
6643
  join->join_tab=join_tab;
 
6644
  join->table=tableptr; tableptr[0]=tmp_table;
 
6645
  join->tables=1;
 
6646
  join->const_tables=0;
 
6647
  join->const_table_map=0;
 
6648
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6649
    join->tmp_table_param.func_count=0;
 
6650
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6651
  join->first_record=join->sort_and_group=0;
 
6652
  join->send_records=(ha_rows) 0;
 
6653
  join->group=0;
 
6654
  join->row_limit=join->unit->select_limit_cnt;
 
6655
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6656
 
 
6657
  join_tab->cache.buff=0;                       /* No caching */
 
6658
  join_tab->table=tmp_table;
 
6659
  join_tab->select=0;
 
6660
  join_tab->select_cond=0;
 
6661
  join_tab->quick=0;
 
6662
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6663
  join_tab->keys.init();
 
6664
  join_tab->keys.set_all();                     /* test everything in quick */
 
6665
  join_tab->info=0;
 
6666
  join_tab->on_expr_ref=0;
 
6667
  join_tab->last_inner= 0;
 
6668
  join_tab->first_unmatched= 0;
 
6669
  join_tab->ref.key = -1;
 
6670
  join_tab->not_used_in_distinct=0;
 
6671
  join_tab->read_first_record= join_init_read_record;
 
6672
  join_tab->join=join;
 
6673
  join_tab->ref.key_parts= 0;
 
6674
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6675
  join_tab->do_firstmatch= NULL;
 
6676
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6677
  tmp_table->status=0;
 
6678
  tmp_table->null_row=0;
 
6679
  return(false);
 
6680
}
 
6681
 
 
6682
 
 
6683
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6684
{
 
6685
  if (*e1)
 
6686
  {
 
6687
    Item *res;
 
6688
    if ((res= new Item_cond_and(*e1, e2)))
 
6689
    {
 
6690
      *e1= res;
 
6691
      res->quick_fix_field();
 
6692
    }
 
6693
  }
 
6694
  else
 
6695
    *e1= e2;
 
6696
}
 
6697
 
1084
6698
 
1085
6699
/**
1086
6700
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1095
6709
    add "t1.field IS NOT NULL" to t1's table condition. @n
1096
6710
 
1097
6711
    Description of the optimization:
1098
 
 
 
6712
    
1099
6713
      We look through equalities choosen to perform ref/eq_ref access,
1100
6714
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1101
6715
      (where othertbl is a non-const table and othertbl.field may be NULL)
1123
6737
      This optimization doesn't affect the choices that ref, range, or join
1124
6738
      optimizer make. This was intentional because this was added after 4.1
1125
6739
      was GA.
1126
 
 
 
6740
      
1127
6741
    Implementation overview
1128
6742
      1. update_ref_and_keys() accumulates info about null-rejecting
1129
 
         predicates in in KeyField::null_rejecting
1130
 
      1.1 add_key_part saves these to KeyUse.
1131
 
      2. create_ref_for_key copies them to table_reference_st.
 
6743
         predicates in in KEY_FIELD::null_rejecting
 
6744
      1.1 add_key_part saves these to KEYUSE.
 
6745
      2. create_ref_for_key copies them to TABLE_REF.
1132
6746
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1133
 
         appropiate JoinTable members.
 
6747
         appropiate JOIN_TAB members.
1134
6748
*/
1135
 
void add_not_null_conds(Join *join)
 
6749
 
 
6750
static void add_not_null_conds(JOIN *join)
1136
6751
{
1137
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6752
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
1138
6753
  {
1139
 
    JoinTable *tab=join->join_tab+i;
1140
 
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1141
 
         tab->type == AM_REF_OR_NULL) &&
 
6754
    JOIN_TAB *tab=join->join_tab+i;
 
6755
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6756
         tab->type == JT_REF_OR_NULL) &&
1142
6757
        !tab->table->maybe_null)
1143
6758
    {
1144
6759
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1149
6764
          Item *notnull;
1150
6765
          assert(item->type() == Item::FIELD_ITEM);
1151
6766
          Item_field *not_null_item= (Item_field*)item;
1152
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
6767
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1153
6768
          /*
1154
6769
            For UPDATE queries such as:
1155
6770
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1161
6776
            return;
1162
6777
          /*
1163
6778
            We need to do full fix_fields() call here in order to have correct
1164
 
            notnull->const_item(). This is needed e.g. by test_quick_select
1165
 
            when it is called from make_join_select after this function is
 
6779
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6780
            when it is called from make_join_select after this function is 
1166
6781
            called.
1167
6782
          */
1168
 
          if (notnull->fix_fields(join->session, &notnull))
 
6783
          if (notnull->fix_fields(join->thd, &notnull))
1169
6784
            return;
1170
6785
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1171
6786
        }
1190
6805
    -  pointer to the guarded predicate, if success
1191
6806
    -  0, otherwise
1192
6807
*/
1193
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6808
 
 
6809
static COND*
 
6810
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1194
6811
{
1195
6812
  COND *tmp;
1196
6813
  assert(cond != 0);
1206
6823
  return tmp;
1207
6824
}
1208
6825
 
 
6826
 
 
6827
/**
 
6828
  Fill in outer join related info for the execution plan structure.
 
6829
 
 
6830
    For each outer join operation left after simplification of the
 
6831
    original query the function set up the following pointers in the linear
 
6832
    structure join->join_tab representing the selected execution plan.
 
6833
    The first inner table t0 for the operation is set to refer to the last
 
6834
    inner table tk through the field t0->last_inner.
 
6835
    Any inner table ti for the operation are set to refer to the first
 
6836
    inner table ti->first_inner.
 
6837
    The first inner table t0 for the operation is set to refer to the
 
6838
    first inner table of the embedding outer join operation, if there is any,
 
6839
    through the field t0->first_upper.
 
6840
    The on expression for the outer join operation is attached to the
 
6841
    corresponding first inner table through the field t0->on_expr_ref.
 
6842
    Here ti are structures of the JOIN_TAB type.
 
6843
 
 
6844
  EXAMPLE. For the query: 
 
6845
  @code
 
6846
        SELECT * FROM t1
 
6847
                      LEFT JOIN
 
6848
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6849
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6850
          WHERE t1.c > 5,
 
6851
  @endcode
 
6852
 
 
6853
    given the execution plan with the table order t1,t2,t3,t4
 
6854
    is selected, the following references will be set;
 
6855
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6856
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6857
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6858
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6859
 
 
6860
  @param join   reference to the info fully describing the query
 
6861
 
 
6862
  @note
 
6863
    The function assumes that the simplification procedure has been
 
6864
    already applied to the join query (see simplify_joins).
 
6865
    This function can be called only after the execution plan
 
6866
    has been chosen.
 
6867
*/
 
6868
 
 
6869
static void
 
6870
make_outerjoin_info(JOIN *join)
 
6871
{
 
6872
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6873
  {
 
6874
    JOIN_TAB *tab=join->join_tab+i;
 
6875
    Table *table=tab->table;
 
6876
    TableList *tbl= table->pos_in_table_list;
 
6877
    TableList *embedding= tbl->embedding;
 
6878
 
 
6879
    if (tbl->outer_join)
 
6880
    {
 
6881
      /* 
 
6882
        Table tab is the only one inner table for outer join.
 
6883
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6884
        is in the query above.)
 
6885
      */
 
6886
      tab->last_inner= tab->first_inner= tab;
 
6887
      tab->on_expr_ref= &tbl->on_expr;
 
6888
      tab->cond_equal= tbl->cond_equal;
 
6889
      if (embedding)
 
6890
        tab->first_upper= embedding->nested_join->first_nested;
 
6891
    }    
 
6892
    for ( ; embedding ; embedding= embedding->embedding)
 
6893
    {
 
6894
      /* Ignore sj-nests: */
 
6895
      if (!embedding->on_expr)
 
6896
        continue;
 
6897
      nested_join_st *nested_join= embedding->nested_join;
 
6898
      if (!nested_join->counter_)
 
6899
      {
 
6900
        /* 
 
6901
          Table tab is the first inner table for nested_join.
 
6902
          Save reference to it in the nested join structure.
 
6903
        */ 
 
6904
        nested_join->first_nested= tab;
 
6905
        tab->on_expr_ref= &embedding->on_expr;
 
6906
        tab->cond_equal= tbl->cond_equal;
 
6907
        if (embedding->embedding)
 
6908
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6909
      }
 
6910
      if (!tab->first_inner)  
 
6911
        tab->first_inner= nested_join->first_nested;
 
6912
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6913
        break;
 
6914
      /* Table tab is the last inner table for nested join. */
 
6915
      nested_join->first_nested->last_inner= tab;
 
6916
    }
 
6917
  }
 
6918
  return;
 
6919
}
 
6920
 
 
6921
 
 
6922
static bool
 
6923
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6924
{
 
6925
  THD *thd= join->thd;
 
6926
  if (select)
 
6927
  {
 
6928
    add_not_null_conds(join);
 
6929
    table_map used_tables;
 
6930
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6931
    {                        /* there may be a select without a cond. */    
 
6932
      if (join->tables > 1)
 
6933
        cond->update_used_tables();             // Tablenr may have changed
 
6934
      if (join->const_tables == join->tables &&
 
6935
          thd->lex->current_select->master_unit() ==
 
6936
          &thd->lex->unit)              // not upper level SELECT
 
6937
        join->const_table_map|=RAND_TABLE_BIT;
 
6938
      {                                         // Check const tables
 
6939
        COND *const_cond=
 
6940
          make_cond_for_table(cond,
 
6941
                              join->const_table_map,
 
6942
                              (table_map) 0, 1);
 
6943
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6944
             tab < join->join_tab+join->tables ; tab++)
 
6945
        {
 
6946
          if (*tab->on_expr_ref)
 
6947
          {
 
6948
            JOIN_TAB *cond_tab= tab->first_inner;
 
6949
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6950
                                           join->const_table_map,
 
6951
                                           (  table_map) 0, 0);
 
6952
            if (!tmp)
 
6953
              continue;
 
6954
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6955
            if (!tmp)
 
6956
              return(1);
 
6957
            tmp->quick_fix_field();
 
6958
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6959
                                    new Item_cond_and(cond_tab->select_cond,
 
6960
                                                      tmp);
 
6961
            if (!cond_tab->select_cond)
 
6962
              return(1);
 
6963
            cond_tab->select_cond->quick_fix_field();
 
6964
          }       
 
6965
        }
 
6966
        if (const_cond && !const_cond->val_int())
 
6967
        {
 
6968
          return(1);     // Impossible const condition
 
6969
        }
 
6970
      }
 
6971
    }
 
6972
    used_tables=((select->const_tables=join->const_table_map) |
 
6973
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
6974
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
6975
    {
 
6976
      JOIN_TAB *tab=join->join_tab+i;
 
6977
      /*
 
6978
        first_inner is the X in queries like:
 
6979
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
6980
      */
 
6981
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
6982
      table_map current_map= tab->table->map;
 
6983
      bool use_quick_range=0;
 
6984
      COND *tmp;
 
6985
 
 
6986
      /*
 
6987
        Following force including random expression in last table condition.
 
6988
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
6989
      */
 
6990
      if (i == join->tables-1)
 
6991
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
6992
      used_tables|=current_map;
 
6993
 
 
6994
      if (tab->type == JT_REF && tab->quick &&
 
6995
          (uint) tab->ref.key == tab->quick->index &&
 
6996
          tab->ref.key_length < tab->quick->max_used_key_length)
 
6997
      {
 
6998
        /* Range uses longer key;  Use this instead of ref on key */
 
6999
        tab->type=JT_ALL;
 
7000
        use_quick_range=1;
 
7001
        tab->use_quick=1;
 
7002
        tab->ref.key= -1;
 
7003
        tab->ref.key_parts=0;           // Don't use ref key.
 
7004
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7005
        /* 
 
7006
          We will use join cache here : prevent sorting of the first
 
7007
          table only and sort at the end.
 
7008
        */
 
7009
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7010
          join->full_join= 1;
 
7011
      }
 
7012
 
 
7013
      tmp= NULL;
 
7014
      if (cond)
 
7015
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7016
      if (cond && !tmp && tab->quick)
 
7017
      {                                         // Outer join
 
7018
        if (tab->type != JT_ALL)
 
7019
        {
 
7020
          /*
 
7021
            Don't use the quick method
 
7022
            We come here in the case where we have 'key=constant' and
 
7023
            the test is removed by make_cond_for_table()
 
7024
          */
 
7025
          delete tab->quick;
 
7026
          tab->quick= 0;
 
7027
        }
 
7028
        else
 
7029
        {
 
7030
          /*
 
7031
            Hack to handle the case where we only refer to a table
 
7032
            in the ON part of an OUTER JOIN. In this case we want the code
 
7033
            below to check if we should use 'quick' instead.
 
7034
          */
 
7035
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7036
        }
 
7037
 
 
7038
      }
 
7039
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7040
          tab->type == JT_EQ_REF)
 
7041
      {
 
7042
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7043
                                       thd->memdup((unsigned char*) select,
 
7044
                                                   sizeof(*select)));
 
7045
        if (!sel)
 
7046
          return(1);                    // End of memory
 
7047
        /*
 
7048
          If tab is an inner table of an outer join operation,
 
7049
          add a match guard to the pushed down predicate.
 
7050
          The guard will turn the predicate on only after
 
7051
          the first match for outer tables is encountered.
 
7052
        */        
 
7053
        if (cond && tmp)
 
7054
        {
 
7055
          /*
 
7056
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7057
            a cond, so neutralize the hack above.
 
7058
          */
 
7059
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7060
            return(1);
 
7061
          tab->select_cond=sel->cond=tmp;
 
7062
          /* Push condition to storage engine if this is enabled
 
7063
             and the condition is not guarded */
 
7064
          tab->table->file->pushed_cond= NULL;
 
7065
          if (thd->variables.engine_condition_pushdown)
 
7066
          {
 
7067
            COND *push_cond= 
 
7068
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7069
            if (push_cond)
 
7070
            {
 
7071
              /* Push condition to handler */
 
7072
              if (!tab->table->file->cond_push(push_cond))
 
7073
                tab->table->file->pushed_cond= push_cond;
 
7074
            }
 
7075
          }
 
7076
        }
 
7077
        else
 
7078
          tab->select_cond= sel->cond= NULL;
 
7079
 
 
7080
        sel->head=tab->table;
 
7081
        if (tab->quick)
 
7082
        {
 
7083
          /* Use quick key read if it's a constant and it's not used
 
7084
             with key reading */
 
7085
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7086
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7087
          {
 
7088
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7089
            sel->quick_keys.clear_all();
 
7090
            sel->needed_reg.clear_all();
 
7091
          }
 
7092
          else
 
7093
          {
 
7094
            delete tab->quick;
 
7095
          }
 
7096
          tab->quick=0;
 
7097
        }
 
7098
        uint32_t ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7099
        if (i == join->const_tables && ref_key)
 
7100
        {
 
7101
          if (!tab->const_keys.is_clear_all() &&
 
7102
              tab->table->reginfo.impossible_range)
 
7103
            return(1);
 
7104
        }
 
7105
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7106
        {
 
7107
          if (!tab->const_keys.is_clear_all() &&
 
7108
              tab->table->reginfo.impossible_range)
 
7109
            return(1);                          // Impossible range
 
7110
          /*
 
7111
            We plan to scan all rows.
 
7112
            Check again if we should use an index.
 
7113
            We could have used an column from a previous table in
 
7114
            the index if we are using limit and this is the first table
 
7115
          */
 
7116
 
 
7117
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7118
              (!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)))
 
7119
          {
 
7120
            /* Join with outer join condition */
 
7121
            COND *orig_cond=sel->cond;
 
7122
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7123
 
 
7124
            /*
 
7125
              We can't call sel->cond->fix_fields,
 
7126
              as it will break tab->on_expr if it's AND condition
 
7127
              (fix_fields currently removes extra AND/OR levels).
 
7128
              Yet attributes of the just built condition are not needed.
 
7129
              Thus we call sel->cond->quick_fix_field for safety.
 
7130
            */
 
7131
            if (sel->cond && !sel->cond->fixed)
 
7132
              sel->cond->quick_fix_field();
 
7133
 
 
7134
            if (sel->test_quick_select(thd, tab->keys,
 
7135
                                       used_tables & ~ current_map,
 
7136
                                       (join->select_options &
 
7137
                                        OPTION_FOUND_ROWS ?
 
7138
                                        HA_POS_ERROR :
 
7139
                                        join->unit->select_limit_cnt), 0,
 
7140
                                        false) < 0)
 
7141
            {
 
7142
              /*
 
7143
                Before reporting "Impossible WHERE" for the whole query
 
7144
                we have to check isn't it only "impossible ON" instead
 
7145
              */
 
7146
              sel->cond=orig_cond;
 
7147
              if (!*tab->on_expr_ref ||
 
7148
                  sel->test_quick_select(thd, tab->keys,
 
7149
                                         used_tables & ~ current_map,
 
7150
                                         (join->select_options &
 
7151
                                          OPTION_FOUND_ROWS ?
 
7152
                                          HA_POS_ERROR :
 
7153
                                          join->unit->select_limit_cnt),0,
 
7154
                                          false) < 0)
 
7155
                return(1);                      // Impossible WHERE
 
7156
            }
 
7157
            else
 
7158
              sel->cond=orig_cond;
 
7159
 
 
7160
            /* Fix for EXPLAIN */
 
7161
            if (sel->quick)
 
7162
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7163
          }
 
7164
          else
 
7165
          {
 
7166
            sel->needed_reg=tab->needed_reg;
 
7167
            sel->quick_keys.clear_all();
 
7168
          }
 
7169
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7170
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7171
          {
 
7172
            tab->keys=sel->quick_keys;
 
7173
            tab->keys.merge(sel->needed_reg);
 
7174
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7175
                             (select->quick_keys.is_clear_all() ||
 
7176
                              (select->quick &&
 
7177
                               (select->quick->records >= 100L)))) ?
 
7178
              2 : 1;
 
7179
            sel->read_tables= used_tables & ~current_map;
 
7180
          }
 
7181
          if (i != join->const_tables && tab->use_quick != 2)
 
7182
          {                                     /* Read with cache */
 
7183
            if (cond &&
 
7184
                (tmp=make_cond_for_table(cond,
 
7185
                                         join->const_table_map |
 
7186
                                         current_map,
 
7187
                                         current_map, 0)))
 
7188
            {
 
7189
              tab->cache.select=(SQL_SELECT*)
 
7190
                thd->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
7191
              tab->cache.select->cond=tmp;
 
7192
              tab->cache.select->read_tables=join->const_table_map;
 
7193
            }
 
7194
          }
 
7195
        }
 
7196
      }
 
7197
      
 
7198
      /* 
 
7199
        Push down conditions from all on expressions.
 
7200
        Each of these conditions are guarded by a variable
 
7201
        that turns if off just before null complemented row for
 
7202
        outer joins is formed. Thus, the condition from an
 
7203
        'on expression' are guaranteed not to be checked for
 
7204
        the null complemented row.
 
7205
      */ 
 
7206
 
 
7207
      /* First push down constant conditions from on expressions */
 
7208
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7209
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7210
      {
 
7211
        if (*join_tab->on_expr_ref)
 
7212
        {
 
7213
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7214
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7215
                                         join->const_table_map,
 
7216
                                         (table_map) 0, 0);
 
7217
          if (!tmp)
 
7218
            continue;
 
7219
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7220
          if (!tmp)
 
7221
            return(1);
 
7222
          tmp->quick_fix_field();
 
7223
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7224
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7225
          if (!cond_tab->select_cond)
 
7226
            return(1);
 
7227
          cond_tab->select_cond->quick_fix_field();
 
7228
        }       
 
7229
      }
 
7230
 
 
7231
      /* Push down non-constant conditions from on expressions */
 
7232
      JOIN_TAB *last_tab= tab;
 
7233
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7234
      {  
 
7235
        /* 
 
7236
          Table tab is the last inner table of an outer join.
 
7237
          An on expression is always attached to it.
 
7238
        */     
 
7239
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7240
 
 
7241
        table_map used_tables2= (join->const_table_map |
 
7242
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7243
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7244
        {
 
7245
          current_map= tab->table->map;
 
7246
          used_tables2|= current_map;
 
7247
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7248
                                              current_map, 0);
 
7249
          if (tmp_cond)
 
7250
          {
 
7251
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7252
            /*
 
7253
              First add the guards for match variables of
 
7254
              all embedding outer join operations.
 
7255
            */
 
7256
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7257
                                                     tmp_cond,
 
7258
                                                     first_inner_tab)))
 
7259
              return(1);
 
7260
            /* 
 
7261
              Now add the guard turning the predicate off for 
 
7262
              the null complemented row.
 
7263
            */ 
 
7264
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7265
                                              &first_inner_tab->
 
7266
                                              not_null_compl);
 
7267
            if (tmp_cond)
 
7268
              tmp_cond->quick_fix_field();
 
7269
            /* Add the predicate to other pushed down predicates */
 
7270
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7271
                                  new Item_cond_and(cond_tab->select_cond,
 
7272
                                                    tmp_cond);
 
7273
            if (!cond_tab->select_cond)
 
7274
              return(1);
 
7275
            cond_tab->select_cond->quick_fix_field();
 
7276
          }              
 
7277
        }
 
7278
        first_inner_tab= first_inner_tab->first_upper;       
 
7279
      }
 
7280
    }
 
7281
  }
 
7282
  return(0);
 
7283
}
 
7284
 
 
7285
 
 
7286
/* 
 
7287
  Check if given expression uses only table fields covered by the given index
 
7288
 
 
7289
  SYNOPSIS
 
7290
    uses_index_fields_only()
 
7291
      item           Expression to check
 
7292
      tbl            The table having the index
 
7293
      keyno          The index number
 
7294
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7295
 
 
7296
  DESCRIPTION
 
7297
    Check if given expression only uses fields covered by index #keyno in the
 
7298
    table tbl. The expression can use any fields in any other tables.
 
7299
    
 
7300
    The expression is guaranteed not to be AND or OR - those constructs are 
 
7301
    handled outside of this function.
 
7302
 
 
7303
  RETURN
 
7304
    true   Yes
 
7305
    false  No
 
7306
*/
 
7307
 
 
7308
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, 
 
7309
                            bool other_tbls_ok)
 
7310
{
 
7311
  if (item->const_item())
 
7312
    return true;
 
7313
 
 
7314
  /* 
 
7315
    Don't push down the triggered conditions. Nested outer joins execution 
 
7316
    code may need to evaluate a condition several times (both triggered and
 
7317
    untriggered), and there is no way to put thi
 
7318
    TODO: Consider cloning the triggered condition and using the copies for:
 
7319
      1. push the first copy down, to have most restrictive index condition
 
7320
         possible
 
7321
      2. Put the second copy into tab->select_cond. 
 
7322
  */
 
7323
  if (item->type() == Item::FUNC_ITEM && 
 
7324
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
7325
    return false;
 
7326
 
 
7327
  if (!(item->used_tables() & tbl->map))
 
7328
    return other_tbls_ok;
 
7329
 
 
7330
  Item::Type item_type= item->type();
 
7331
  switch (item_type) {
 
7332
  case Item::FUNC_ITEM:
 
7333
    {
 
7334
      /* This is a function, apply condition recursively to arguments */
 
7335
      Item_func *item_func= (Item_func*)item;
 
7336
      Item **child;
 
7337
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
7338
      for (child= item_func->arguments(); child != item_end; child++)
 
7339
      {
 
7340
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
7341
          return false;
 
7342
      }
 
7343
      return true;
 
7344
    }
 
7345
  case Item::COND_ITEM:
 
7346
    {
 
7347
      /* This is a function, apply condition recursively to arguments */
 
7348
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
7349
      Item *item;
 
7350
      while ((item=li++))
 
7351
      {
 
7352
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
7353
          return false;
 
7354
      }
 
7355
      return true;
 
7356
    }
 
7357
  case Item::FIELD_ITEM:
 
7358
    {
 
7359
      Item_field *item_field= (Item_field*)item;
 
7360
      if (item_field->field->table != tbl) 
 
7361
        return true;
 
7362
      return item_field->field->part_of_key.is_set(keyno);
 
7363
    }
 
7364
  case Item::REF_ITEM:
 
7365
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
7366
                                  other_tbls_ok);
 
7367
  default:
 
7368
    return false; /* Play it safe, don't push unknown non-const items */
 
7369
  }
 
7370
}
 
7371
 
 
7372
 
1209
7373
#define ICP_COND_USES_INDEX_ONLY 10
1210
7374
 
1211
 
 
1212
 
/**
1213
 
  cleanup JoinTable.
1214
 
*/
1215
 
void JoinTable::cleanup()
 
7375
/*
 
7376
  Get a part of the condition that can be checked using only index fields
 
7377
 
 
7378
  SYNOPSIS
 
7379
    make_cond_for_index()
 
7380
      cond           The source condition
 
7381
      table          The table that is partially available
 
7382
      keyno          The index in the above table. Only fields covered by the index
 
7383
                     are available
 
7384
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7385
 
 
7386
  DESCRIPTION
 
7387
    Get a part of the condition that can be checked when for the given table 
 
7388
    we have values only of fields covered by some index. The condition may
 
7389
    refer to other tables, it is assumed that we have values of all of their 
 
7390
    fields.
 
7391
 
 
7392
    Example:
 
7393
      make_cond_for_index(
 
7394
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
7395
          t2, keyno(t2.key1)) 
 
7396
      will return
 
7397
        "cond(t1.field) AND cond(t2.key2)"
 
7398
 
 
7399
  RETURN
 
7400
    Index condition, or NULL if no condition could be inferred.
 
7401
*/
 
7402
 
 
7403
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
 
7404
                          bool other_tbls_ok)
 
7405
{
 
7406
  if (!cond)
 
7407
    return NULL;
 
7408
  if (cond->type() == Item::COND_ITEM)
 
7409
  {
 
7410
    uint32_t n_marked= 0;
 
7411
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7412
    {
 
7413
      Item_cond_and *new_cond=new Item_cond_and;
 
7414
      if (!new_cond)
 
7415
        return (COND*) 0;
 
7416
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7417
      Item *item;
 
7418
      while ((item=li++))
 
7419
      {
 
7420
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7421
        if (fix)
 
7422
          new_cond->argument_list()->push_back(fix);
 
7423
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7424
      }
 
7425
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7426
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7427
      switch (new_cond->argument_list()->elements) {
 
7428
      case 0:
 
7429
        return (COND*) 0;
 
7430
      case 1:
 
7431
        return new_cond->argument_list()->head();
 
7432
      default:
 
7433
        new_cond->quick_fix_field();
 
7434
        return new_cond;
 
7435
      }
 
7436
    }
 
7437
    else /* It's OR */
 
7438
    {
 
7439
      Item_cond_or *new_cond=new Item_cond_or;
 
7440
      if (!new_cond)
 
7441
        return (COND*) 0;
 
7442
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7443
      Item *item;
 
7444
      while ((item=li++))
 
7445
      {
 
7446
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7447
        if (!fix)
 
7448
          return (COND*) 0;
 
7449
        new_cond->argument_list()->push_back(fix);
 
7450
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
7451
      }
 
7452
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
7453
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7454
      new_cond->quick_fix_field();
 
7455
      new_cond->top_level_item();
 
7456
      return new_cond;
 
7457
    }
 
7458
  }
 
7459
 
 
7460
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
7461
    return (COND*) 0;
 
7462
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
7463
  return cond;
 
7464
}
 
7465
 
 
7466
 
 
7467
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7468
{
 
7469
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
7470
    return 0; /* Already checked */
 
7471
 
 
7472
  if (cond->type() == Item::COND_ITEM)
 
7473
  {
 
7474
    table_map tbl_map= 0;
 
7475
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
7476
    {
 
7477
      /* Create new top level AND item */
 
7478
      Item_cond_and *new_cond=new Item_cond_and;
 
7479
      if (!new_cond)
 
7480
        return (COND*) 0;
 
7481
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7482
      Item *item;
 
7483
      while ((item=li++))
 
7484
      {
 
7485
        Item *fix= make_cond_remainder(item, exclude_index);
 
7486
        if (fix)
 
7487
        {
 
7488
          new_cond->argument_list()->push_back(fix);
 
7489
          tbl_map |= fix->used_tables();
 
7490
        }
 
7491
      }
 
7492
      switch (new_cond->argument_list()->elements) {
 
7493
      case 0:
 
7494
        return (COND*) 0;
 
7495
      case 1:
 
7496
        return new_cond->argument_list()->head();
 
7497
      default:
 
7498
        new_cond->quick_fix_field();
 
7499
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7500
        return new_cond;
 
7501
      }
 
7502
    }
 
7503
    else /* It's OR */
 
7504
    {
 
7505
      Item_cond_or *new_cond=new Item_cond_or;
 
7506
      if (!new_cond)
 
7507
        return (COND*) 0;
 
7508
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
7509
      Item *item;
 
7510
      while ((item=li++))
 
7511
      {
 
7512
        Item *fix= make_cond_remainder(item, false);
 
7513
        if (!fix)
 
7514
          return (COND*) 0;
 
7515
        new_cond->argument_list()->push_back(fix);
 
7516
        tbl_map |= fix->used_tables();
 
7517
      }
 
7518
      new_cond->quick_fix_field();
 
7519
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
7520
      new_cond->top_level_item();
 
7521
      return new_cond;
 
7522
    }
 
7523
  }
 
7524
  return cond;
 
7525
}
 
7526
 
 
7527
 
 
7528
/*
 
7529
  Try to extract and push the index condition
 
7530
 
 
7531
  SYNOPSIS
 
7532
    push_index_cond()
 
7533
      tab            A join tab that has tab->table->file and its condition
 
7534
                     in tab->select_cond
 
7535
      keyno          Index for which extract and push the condition
 
7536
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7537
 
 
7538
  DESCRIPTION
 
7539
    Try to extract and push the index condition down to table handler
 
7540
*/
 
7541
 
 
7542
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
7543
{
 
7544
  Item *idx_cond;
 
7545
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7546
      tab->join->thd->variables.engine_condition_pushdown)
 
7547
  {
 
7548
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7549
                                  other_tbls_ok);
 
7550
 
 
7551
    if (idx_cond)
 
7552
    {
 
7553
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7554
      Item *idx_remainder_cond= 
 
7555
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7556
 
 
7557
      /*
 
7558
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7559
        condition. 
 
7560
        TODO: This check happens to work on current ICP implementations, but
 
7561
        there may exist a compliant implementation that will not work 
 
7562
        correctly with it. Sort this out when we stabilize the condition
 
7563
        pushdown APIs.
 
7564
      */
 
7565
      if (idx_remainder_cond != idx_cond)
 
7566
        tab->ref.disable_cache= true;
 
7567
 
 
7568
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7569
 
 
7570
      if (row_cond)
 
7571
      {
 
7572
        if (!idx_remainder_cond)
 
7573
          tab->select_cond= row_cond;
 
7574
        else
 
7575
        {
 
7576
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7577
          tab->select_cond->quick_fix_field();
 
7578
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7579
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7580
        }
 
7581
      }
 
7582
      else
 
7583
        tab->select_cond= idx_remainder_cond;
 
7584
      if (tab->select)
 
7585
      {
 
7586
        tab->select->cond= tab->select_cond;
 
7587
      }
 
7588
    }
 
7589
  }
 
7590
  return;
 
7591
}
 
7592
 
 
7593
 
 
7594
 
 
7595
    /*
 
7596
      Determine if the set is already ordered for order_st BY, so it can 
 
7597
      disable join cache because it will change the ordering of the results.
 
7598
      Code handles sort table that is at any location (not only first after 
 
7599
      the const tables) despite the fact that it's currently prohibited.
 
7600
      We must disable join cache if the first non-const table alone is
 
7601
      ordered. If there is a temp table the ordering is done as a last
 
7602
      operation and doesn't prevent join cache usage.
 
7603
    */
 
7604
uint32_t make_join_orderinfo(JOIN *join)
 
7605
{
 
7606
  uint32_t i;
 
7607
  if (join->need_tmp)
 
7608
    return join->tables;
 
7609
 
 
7610
  for (i=join->const_tables ; i < join->tables ; i++)
 
7611
  {
 
7612
    JOIN_TAB *tab=join->join_tab+i;
 
7613
    Table *table=tab->table;
 
7614
    if ((table == join->sort_by_table && 
 
7615
         (!join->order || join->skip_sort_order)) ||
 
7616
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
 
7617
    {
 
7618
      break;
 
7619
    }
 
7620
  }
 
7621
  return i;
 
7622
}
 
7623
 
 
7624
 
 
7625
/*
 
7626
  Plan refinement stage: do various set ups for the executioner
 
7627
 
 
7628
  SYNOPSIS
 
7629
    make_join_readinfo()
 
7630
      join           Join being processed
 
7631
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7632
                     SELECT_NO_JOIN_CACHE)
 
7633
      no_jbuf_after  Don't use join buffering after table with this number.
 
7634
 
 
7635
  DESCRIPTION
 
7636
    Plan refinement stage: do various set ups for the executioner
 
7637
      - set up use of join buffering
 
7638
      - push index conditions
 
7639
      - increment counters
 
7640
      - etc
 
7641
 
 
7642
  RETURN 
 
7643
    false - OK
 
7644
    true  - Out of memory
 
7645
*/
 
7646
 
 
7647
static bool
 
7648
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
7649
{
 
7650
  uint32_t i;
 
7651
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7652
  bool sorted= 1;
 
7653
 
 
7654
  for (i=join->const_tables ; i < join->tables ; i++)
 
7655
  {
 
7656
    JOIN_TAB *tab=join->join_tab+i;
 
7657
    Table *table=tab->table;
 
7658
    bool using_join_cache;
 
7659
    tab->read_record.table= table;
 
7660
    tab->read_record.file=table->file;
 
7661
    tab->next_select=sub_select;                /* normal select */
 
7662
    /* 
 
7663
      TODO: don't always instruct first table's ref/range access method to 
 
7664
      produce sorted output.
 
7665
    */
 
7666
    tab->sorted= sorted;
 
7667
    sorted= 0;                                  // only first must be sorted
 
7668
    if (tab->insideout_match_tab)
 
7669
    {
 
7670
      if (!(tab->insideout_buf= (unsigned char*)join->thd->alloc(tab->table->key_info
 
7671
                                                         [tab->index].
 
7672
                                                         key_length)))
 
7673
        return true;
 
7674
    }
 
7675
    switch (tab->type) {
 
7676
    case JT_SYSTEM:                             // Only happens with left join
 
7677
      table->status=STATUS_NO_RECORD;
 
7678
      tab->read_first_record= join_read_system;
 
7679
      tab->read_record.read_record= join_no_more_records;
 
7680
      break;
 
7681
    case JT_CONST:                              // Only happens with left join
 
7682
      table->status=STATUS_NO_RECORD;
 
7683
      tab->read_first_record= join_read_const;
 
7684
      tab->read_record.read_record= join_no_more_records;
 
7685
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7686
          !table->no_keyread)
 
7687
      {
 
7688
        table->key_read=1;
 
7689
        table->file->extra(HA_EXTRA_KEYREAD);
 
7690
      }
 
7691
      break;
 
7692
    case JT_EQ_REF:
 
7693
      table->status=STATUS_NO_RECORD;
 
7694
      if (tab->select)
 
7695
      {
 
7696
        delete tab->select->quick;
 
7697
        tab->select->quick=0;
 
7698
      }
 
7699
      delete tab->quick;
 
7700
      tab->quick=0;
 
7701
      tab->read_first_record= join_read_key;
 
7702
      tab->read_record.read_record= join_no_more_records;
 
7703
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7704
          !table->no_keyread)
 
7705
      {
 
7706
        table->key_read=1;
 
7707
        table->file->extra(HA_EXTRA_KEYREAD);
 
7708
      }
 
7709
      else
 
7710
        push_index_cond(tab, tab->ref.key, true);
 
7711
      break;
 
7712
    case JT_REF_OR_NULL:
 
7713
    case JT_REF:
 
7714
      table->status=STATUS_NO_RECORD;
 
7715
      if (tab->select)
 
7716
      {
 
7717
        delete tab->select->quick;
 
7718
        tab->select->quick=0;
 
7719
      }
 
7720
      delete tab->quick;
 
7721
      tab->quick=0;
 
7722
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7723
          !table->no_keyread)
 
7724
      {
 
7725
        table->key_read=1;
 
7726
        table->file->extra(HA_EXTRA_KEYREAD);
 
7727
      }
 
7728
      else
 
7729
        push_index_cond(tab, tab->ref.key, true);
 
7730
      if (tab->type == JT_REF)
 
7731
      {
 
7732
        tab->read_first_record= join_read_always_key;
 
7733
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7734
           join_read_next_same_diff : join_read_next_same;
 
7735
      }
 
7736
      else
 
7737
      {
 
7738
        tab->read_first_record= join_read_always_key_or_null;
 
7739
        tab->read_record.read_record= join_read_next_same_or_null;
 
7740
      }
 
7741
      break;
 
7742
    case JT_ALL:
 
7743
      /*
 
7744
        If previous table use cache
 
7745
        If the incoming data set is already sorted don't use cache.
 
7746
      */
 
7747
      table->status=STATUS_NO_RECORD;
 
7748
      using_join_cache= false;
 
7749
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7750
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7751
          !tab->insideout_match_tab)
 
7752
      {
 
7753
        if ((options & SELECT_DESCRIBE) ||
 
7754
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7755
                             i-join->const_tables))
 
7756
        {
 
7757
          using_join_cache= true;
 
7758
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7759
        }
 
7760
      }
 
7761
      /* These init changes read_record */
 
7762
      if (tab->use_quick == 2)
 
7763
      {
 
7764
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7765
        tab->read_first_record= join_init_quick_read_record;
 
7766
        if (statistics)
 
7767
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7768
      }
 
7769
      else
 
7770
      {
 
7771
        tab->read_first_record= join_init_read_record;
 
7772
        if (i == join->const_tables)
 
7773
        {
 
7774
          if (tab->select && tab->select->quick)
 
7775
          {
 
7776
            if (statistics)
 
7777
              status_var_increment(join->thd->status_var.select_range_count);
 
7778
          }
 
7779
          else
 
7780
          {
 
7781
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7782
            if (statistics)
 
7783
              status_var_increment(join->thd->status_var.select_scan_count);
 
7784
          }
 
7785
        }
 
7786
        else
 
7787
        {
 
7788
          if (tab->select && tab->select->quick)
 
7789
          {
 
7790
            if (statistics)
 
7791
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7792
          }
 
7793
          else
 
7794
          {
 
7795
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7796
            if (statistics)
 
7797
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7798
          }
 
7799
        }
 
7800
        if (!table->no_keyread)
 
7801
        {
 
7802
          if (tab->select && tab->select->quick &&
 
7803
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7804
              table->covering_keys.is_set(tab->select->quick->index))
 
7805
          {
 
7806
            table->key_read=1;
 
7807
            table->file->extra(HA_EXTRA_KEYREAD);
 
7808
          }
 
7809
          else if (!table->covering_keys.is_clear_all() &&
 
7810
                   !(tab->select && tab->select->quick))
 
7811
          {                                     // Only read index tree
 
7812
            if (!tab->insideout_match_tab)
 
7813
            {
 
7814
              /*
 
7815
                See bug #26447: "Using the clustered index for a table scan
 
7816
                is always faster than using a secondary index".
 
7817
              */
 
7818
              if (table->s->primary_key != MAX_KEY &&
 
7819
                  table->file->primary_key_is_clustered())
 
7820
                tab->index= table->s->primary_key;
 
7821
              else
 
7822
                tab->index= table->find_shortest_key(&table->covering_keys);
 
7823
            }
 
7824
            tab->read_first_record= join_read_first;
 
7825
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7826
          }
 
7827
        }
 
7828
        if (tab->select && tab->select->quick &&
 
7829
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7830
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7831
      }
 
7832
      break;
 
7833
    default:
 
7834
      break;                                    /* purecov: deadcode */
 
7835
    case JT_UNKNOWN:
 
7836
    case JT_MAYBE_REF:
 
7837
      abort();                                  /* purecov: deadcode */
 
7838
    }
 
7839
  }
 
7840
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7841
  return(false);
 
7842
}
 
7843
 
 
7844
 
 
7845
/**
 
7846
  Give error if we some tables are done with a full join.
 
7847
 
 
7848
  This is used by multi_table_update and multi_table_delete when running
 
7849
  in safe mode.
 
7850
 
 
7851
  @param join           Join condition
 
7852
 
 
7853
  @retval
 
7854
    0   ok
 
7855
  @retval
 
7856
    1   Error (full join used)
 
7857
*/
 
7858
 
 
7859
bool error_if_full_join(JOIN *join)
 
7860
{
 
7861
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7862
       tab < end;
 
7863
       tab++)
 
7864
  {
 
7865
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7866
    {
 
7867
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7868
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7869
      return(1);
 
7870
    }
 
7871
  }
 
7872
  return(0);
 
7873
}
 
7874
 
 
7875
 
 
7876
/**
 
7877
  cleanup JOIN_TAB.
 
7878
*/
 
7879
 
 
7880
void JOIN_TAB::cleanup()
1216
7881
{
1217
7882
  delete select;
1218
7883
  select= 0;
1219
7884
  delete quick;
1220
7885
  quick= 0;
1221
7886
  if (cache.buff)
1222
 
  {
1223
 
    size_t size= cache.end - cache.buff;
1224
 
    global_join_buffer.sub(size);
1225
7887
    free(cache.buff);
1226
 
  }
1227
7888
  cache.buff= 0;
1228
7889
  limit= 0;
1229
7890
  if (table)
1231
7892
    if (table->key_read)
1232
7893
    {
1233
7894
      table->key_read= 0;
1234
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7895
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1235
7896
    }
1236
 
    table->cursor->ha_index_or_rnd_end();
 
7897
    table->file->ha_index_or_rnd_end();
1237
7898
    /*
1238
7899
      We need to reset this for next select
1239
7900
      (Tested in part_of_refkey)
1240
7901
    */
1241
7902
    table->reginfo.join_tab= 0;
1242
7903
  }
1243
 
  read_record.end_read_record();
1244
 
}
1245
 
 
1246
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1247
 
{
1248
 
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1249
 
  {
1250
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1251
 
      return 0;
1252
 
  }
1253
 
  return 1;
1254
 
}
1255
 
 
1256
 
/**
1257
 
  Remove the following expressions from ORDER BY and GROUP BY:
 
7904
  end_read_record(&read_record);
 
7905
}
 
7906
 
 
7907
 
 
7908
/**
 
7909
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7910
  (table cursors), free quick selects.
 
7911
 
 
7912
    This function is called in the end of execution of a JOIN, before the used
 
7913
    tables are unlocked and closed.
 
7914
 
 
7915
    For a join that is resolved using a temporary table, the first sweep is
 
7916
    performed against actual tables and an intermediate result is inserted
 
7917
    into the temprorary table.
 
7918
    The last sweep is performed against the temporary table. Therefore,
 
7919
    the base tables and associated buffers used to fill the temporary table
 
7920
    are no longer needed, and this function is called to free them.
 
7921
 
 
7922
    For a join that is performed without a temporary table, this function
 
7923
    is called after all rows are sent, but before EOF packet is sent.
 
7924
 
 
7925
    For a simple SELECT with no subqueries this function performs a full
 
7926
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7927
    tables.
 
7928
 
 
7929
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7930
    do the full cleanup and need to do a partial cleanup only.
 
7931
    - If a JOIN is not the top level join, we must not unlock the tables
 
7932
    because the outer select may not have been evaluated yet, and we
 
7933
    can't unlock only selected tables of a query.
 
7934
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7935
    should not free quick selects and join buffers because they will be
 
7936
    needed for the next execution of the correlated subquery.
 
7937
    - However, if this is a JOIN for a [sub]select, which is not
 
7938
    a correlated subquery itself, but has subqueries, we can free it
 
7939
    fully and also free JOINs of all its subqueries. The exception
 
7940
    is a subquery in SELECT list, e.g: @n
 
7941
    SELECT a, (select cmax(b) from t1) group by c @n
 
7942
    This subquery will not be evaluated at first sweep and its value will
 
7943
    not be inserted into the temporary table. Instead, it's evaluated
 
7944
    when selecting from the temporary table. Therefore, it can't be freed
 
7945
    here even though it's not correlated.
 
7946
 
 
7947
  @todo
 
7948
    Unlock tables even if the join isn't top level select in the tree
 
7949
*/
 
7950
 
 
7951
void JOIN::join_free()
 
7952
{
 
7953
  SELECT_LEX_UNIT *tmp_unit;
 
7954
  SELECT_LEX *sl;
 
7955
  /*
 
7956
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7957
    free all tables.
 
7958
  */
 
7959
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7960
  bool can_unlock= full;
 
7961
 
 
7962
  cleanup(full);
 
7963
 
 
7964
  for (tmp_unit= select_lex->first_inner_unit();
 
7965
       tmp_unit;
 
7966
       tmp_unit= tmp_unit->next_unit())
 
7967
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7968
    {
 
7969
      Item_subselect *subselect= sl->master_unit()->item;
 
7970
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7971
      /*
 
7972
        If this join is evaluated, we can fully clean it up and clean up all
 
7973
        its underlying joins even if they are correlated -- they will not be
 
7974
        used any more anyway.
 
7975
        If this join is not yet evaluated, we still must clean it up to
 
7976
        close its table cursors -- it may never get evaluated, as in case of
 
7977
        ... HAVING false OR a IN (SELECT ...))
 
7978
        but all table cursors must be closed before the unlock.
 
7979
      */
 
7980
      sl->cleanup_all_joins(full_local);
 
7981
      /* Can't unlock if at least one JOIN is still needed */
 
7982
      can_unlock= can_unlock && full_local;
 
7983
    }
 
7984
 
 
7985
  /*
 
7986
    We are not using tables anymore
 
7987
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
7988
  */
 
7989
  if (can_unlock && lock && thd->lock &&
 
7990
      !(select_options & SELECT_NO_UNLOCK) &&
 
7991
      !select_lex->subquery_in_having &&
 
7992
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
7993
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
7994
  {
 
7995
    /*
 
7996
      TODO: unlock tables even if the join isn't top level select in the
 
7997
      tree.
 
7998
    */
 
7999
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8000
    lock= 0;
 
8001
  }
 
8002
 
 
8003
  return;
 
8004
}
 
8005
 
 
8006
 
 
8007
/**
 
8008
  Free resources of given join.
 
8009
 
 
8010
  @param fill   true if we should free all resources, call with full==1
 
8011
                should be last, before it this function can be called with
 
8012
                full==0
 
8013
 
 
8014
  @note
 
8015
    With subquery this function definitely will be called several times,
 
8016
    but even for simple query it can be called several times.
 
8017
*/
 
8018
 
 
8019
void JOIN::cleanup(bool full)
 
8020
{
 
8021
  if (table)
 
8022
  {
 
8023
    JOIN_TAB *tab,*end;
 
8024
    /*
 
8025
      Only a sorted table may be cached.  This sorted table is always the
 
8026
      first non const table in join->table
 
8027
    */
 
8028
    if (tables > const_tables) // Test for not-const tables
 
8029
    {
 
8030
      free_io_cache(table[const_tables]);
 
8031
      filesort_free_buffers(table[const_tables],full);
 
8032
    }
 
8033
 
 
8034
    if (full)
 
8035
    {
 
8036
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8037
        tab->cleanup();
 
8038
      table= 0;
 
8039
    }
 
8040
    else
 
8041
    {
 
8042
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8043
      {
 
8044
        if (tab->table)
 
8045
          tab->table->file->ha_index_or_rnd_end();
 
8046
      }
 
8047
    }
 
8048
    cleanup_sj_tmp_tables(this);//
 
8049
  }
 
8050
  /*
 
8051
    We are not using tables anymore
 
8052
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8053
  */
 
8054
  if (full)
 
8055
  {
 
8056
    if (tmp_join)
 
8057
      tmp_table_param.copy_field= 0;
 
8058
    group_fields.delete_elements();
 
8059
    /*
 
8060
      We can't call delete_elements() on copy_funcs as this will cause
 
8061
      problems in free_elements() as some of the elements are then deleted.
 
8062
    */
 
8063
    tmp_table_param.copy_funcs.empty();
 
8064
    /*
 
8065
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8066
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8067
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8068
      be removed in tmp_table_param.cleanup().
 
8069
    */
 
8070
    if (tmp_join &&
 
8071
        tmp_join != this &&
 
8072
        tmp_join->tmp_table_param.copy_field ==
 
8073
        tmp_table_param.copy_field)
 
8074
    {
 
8075
      tmp_join->tmp_table_param.copy_field=
 
8076
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8077
    }
 
8078
    tmp_table_param.cleanup();
 
8079
  }
 
8080
  return;
 
8081
}
 
8082
 
 
8083
 
 
8084
/**
 
8085
  Remove the following expressions from order_st BY and GROUP BY:
1258
8086
  Constant expressions @n
1259
8087
  Expression that only uses tables that are of type EQ_REF and the reference
1260
8088
  is in the order_st list or if all refereed tables are of the above type.
1261
8089
 
1262
8090
  In the following, the X field can be removed:
1263
8091
  @code
1264
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
1265
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
 
8092
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
8093
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1266
8094
  @endcode
1267
8095
 
1268
8096
  These can't be optimized:
1269
8097
  @code
1270
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
1271
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
1272
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
 
8098
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
8099
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
8100
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1273
8101
  @endcode
1274
8102
*/
1275
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
8103
 
 
8104
static bool
 
8105
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
1276
8106
{
1277
8107
  if (tab->cached_eq_ref_table)                 // If cached
1278
8108
    return tab->eq_ref_table;
1279
8109
  tab->cached_eq_ref_table=1;
1280
8110
  /* We can skip const tables only if not an outer table */
1281
 
  if (tab->type == AM_CONST && !tab->first_inner)
1282
 
    return (tab->eq_ref_table=1);
1283
 
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
8111
  if (tab->type == JT_CONST && !tab->first_inner)
 
8112
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8113
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1284
8114
    return (tab->eq_ref_table=0);               // We must use this
1285
8115
  Item **ref_item=tab->ref.items;
1286
8116
  Item **end=ref_item+tab->ref.key_parts;
1291
8121
  {
1292
8122
    if (! (*ref_item)->const_item())
1293
8123
    {                                           // Not a const ref
1294
 
      Order *order;
 
8124
      order_st *order;
1295
8125
      for (order=start_order ; order ; order=order->next)
1296
8126
      {
1297
 
        if ((*ref_item)->eq(order->item[0],0))
1298
 
          break;
 
8127
        if ((*ref_item)->eq(order->item[0],0))
 
8128
          break;
1299
8129
      }
1300
8130
      if (order)
1301
8131
      {
1302
 
        found++;
1303
 
        assert(!(order->used & map));
1304
 
        order->used|=map;
1305
 
        continue;                               // Used in order_st BY
 
8132
        found++;
 
8133
        assert(!(order->used & map));
 
8134
        order->used|=map;
 
8135
        continue;                               // Used in order_st BY
1306
8136
      }
1307
8137
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1308
 
        return (tab->eq_ref_table= 0);
 
8138
        return (tab->eq_ref_table=0);
1309
8139
    }
1310
8140
  }
1311
8141
  /* Check that there was no reference to table before sort order */
1317
8147
      continue;
1318
8148
    }
1319
8149
    if (start_order->depend_map & map)
1320
 
      return (tab->eq_ref_table= 0);
1321
 
  }
1322
 
  return tab->eq_ref_table= 1;
1323
 
}
 
8150
      return (tab->eq_ref_table=0);
 
8151
  }
 
8152
  return tab->eq_ref_table=1;
 
8153
}
 
8154
 
 
8155
 
 
8156
static bool
 
8157
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
8158
{
 
8159
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8160
  {
 
8161
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8162
      return 0;
 
8163
  }
 
8164
  return 1;
 
8165
}
 
8166
 
 
8167
 
 
8168
/** Update the dependency map for the tables. */
 
8169
 
 
8170
static void update_depend_map(JOIN *join)
 
8171
{
 
8172
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8173
 
 
8174
  for (; join_tab != end ; join_tab++)
 
8175
  {
 
8176
    TABLE_REF *ref= &join_tab->ref;
 
8177
    table_map depend_map=0;
 
8178
    Item **item=ref->items;
 
8179
    uint32_t i;
 
8180
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8181
      depend_map|=(*item)->used_tables();
 
8182
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8183
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8184
    for (JOIN_TAB **tab=join->map2table;
 
8185
         depend_map ;
 
8186
         tab++,depend_map>>=1 )
 
8187
    {
 
8188
      if (depend_map & 1)
 
8189
        ref->depend_map|=(*tab)->ref.depend_map;
 
8190
    }
 
8191
  }
 
8192
}
 
8193
 
 
8194
 
 
8195
/** Update the dependency map for the sort order. */
 
8196
 
 
8197
static void update_depend_map(JOIN *join, order_st *order)
 
8198
{
 
8199
  for (; order ; order=order->next)
 
8200
  {
 
8201
    table_map depend_map;
 
8202
    order->item[0]->update_used_tables();
 
8203
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8204
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8205
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8206
        && !order->item[0]->with_sum_func)
 
8207
    {
 
8208
      for (JOIN_TAB **tab=join->map2table;
 
8209
           depend_map ;
 
8210
           tab++, depend_map>>=1)
 
8211
      {
 
8212
        if (depend_map & 1)
 
8213
          order->depend_map|=(*tab)->ref.depend_map;
 
8214
      }
 
8215
    }
 
8216
  }
 
8217
}
 
8218
 
 
8219
 
 
8220
/**
 
8221
  Remove all constants and check if order_st only contains simple
 
8222
  expressions.
 
8223
 
 
8224
  simple_order is set to 1 if sort_order only uses fields from head table
 
8225
  and the head table is not a LEFT JOIN table.
 
8226
 
 
8227
  @param join                   Join handler
 
8228
  @param first_order            List of SORT or GROUP order
 
8229
  @param cond                   WHERE statement
 
8230
  @param change_list            Set to 1 if we should remove things from list.
 
8231
                               If this is not set, then only simple_order is
 
8232
                               calculated.
 
8233
  @param simple_order           Set to 1 if we are only using simple expressions
 
8234
 
 
8235
  @return
 
8236
    Returns new sort order
 
8237
*/
 
8238
 
 
8239
static order_st *
 
8240
remove_const(JOIN *join,order_st *first_order, COND *cond,
 
8241
             bool change_list, bool *simple_order)
 
8242
{
 
8243
  if (join->tables == join->const_tables)
 
8244
    return change_list ? 0 : first_order;               // No need to sort
 
8245
 
 
8246
  order_st *order,**prev_ptr;
 
8247
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8248
  table_map not_const_tables= ~join->const_table_map;
 
8249
  table_map ref;
 
8250
 
 
8251
  prev_ptr= &first_order;
 
8252
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8253
 
 
8254
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8255
 
 
8256
  update_depend_map(join, first_order);
 
8257
  for (order=first_order; order ; order=order->next)
 
8258
  {
 
8259
    table_map order_tables=order->item[0]->used_tables();
 
8260
    if (order->item[0]->with_sum_func)
 
8261
      *simple_order=0;                          // Must do a temp table to sort
 
8262
    else if (!(order_tables & not_const_tables))
 
8263
    {
 
8264
      if (order->item[0]->with_subselect)
 
8265
        order->item[0]->val_str(&order->item[0]->str_value);
 
8266
      continue;                                 // skip const item
 
8267
    }
 
8268
    else
 
8269
    {
 
8270
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8271
        *simple_order=0;
 
8272
      else
 
8273
      {
 
8274
        Item *comp_item=0;
 
8275
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8276
        {
 
8277
          continue;
 
8278
        }
 
8279
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8280
        {
 
8281
          if (!(order_tables & first_table) &&
 
8282
              only_eq_ref_tables(join,first_order, ref))
 
8283
          {
 
8284
            continue;
 
8285
          }
 
8286
          *simple_order=0;                      // Must do a temp table to sort
 
8287
        }
 
8288
      }
 
8289
    }
 
8290
    if (change_list)
 
8291
      *prev_ptr= order;                         // use this entry
 
8292
    prev_ptr= &order->next;
 
8293
  }
 
8294
  if (change_list)
 
8295
    *prev_ptr=0;
 
8296
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8297
    *simple_order=1;
 
8298
  return(first_order);
 
8299
}
 
8300
 
 
8301
 
 
8302
static int
 
8303
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
 
8304
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8305
                 const char *info, Item *having)
 
8306
{
 
8307
  if (select_options & SELECT_DESCRIBE)
 
8308
  {
 
8309
    select_describe(join, false, false, false, info);
 
8310
    return(0);
 
8311
  }
 
8312
 
 
8313
  join->join_free();
 
8314
 
 
8315
  if (send_row)
 
8316
  {
 
8317
    for (TableList *table= tables; table; table= table->next_leaf)
 
8318
      mark_as_null_row(table->table);           // All fields are NULL
 
8319
    if (having && having->val_int() == 0)
 
8320
      send_row=0;
 
8321
  }
 
8322
  if (!(result->send_fields(fields,
 
8323
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8324
  {
 
8325
    if (send_row)
 
8326
    {
 
8327
      List_iterator_fast<Item> it(fields);
 
8328
      Item *item;
 
8329
      while ((item= it++))
 
8330
        item->no_rows_in_result();
 
8331
      result->send_data(fields);
 
8332
    }
 
8333
    result->send_eof();                         // Should be safe
 
8334
  }
 
8335
  /* Update results for FOUND_ROWS */
 
8336
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8337
  return(0);
 
8338
}
 
8339
 
 
8340
/*
 
8341
  used only in JOIN::clear
 
8342
*/
 
8343
static void clear_tables(JOIN *join)
 
8344
{
 
8345
  /* 
 
8346
    must clear only the non-const tables, as const tables
 
8347
    are not re-calculated.
 
8348
  */
 
8349
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
8350
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8351
}
 
8352
 
 
8353
/*****************************************************************************
 
8354
  Make som simple condition optimization:
 
8355
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8356
  Remove all dummy tests 'item = item', 'const op const'.
 
8357
  Remove all 'item is NULL', when item can never be null!
 
8358
  item->marker should be 0 for all items on entry
 
8359
  Return in cond_value false if condition is impossible (1 = 2)
 
8360
*****************************************************************************/
 
8361
 
 
8362
class COND_CMP :public ilink {
 
8363
public:
 
8364
  static void *operator new(size_t size)
 
8365
  {
 
8366
    return (void*) sql_alloc((uint) size);
 
8367
  }
 
8368
  static void operator delete(void *ptr __attribute__((unused)),
 
8369
                              size_t size __attribute__((unused)))
 
8370
  { TRASH(ptr, size); }
 
8371
 
 
8372
  Item *and_level;
 
8373
  Item_func *cmp_func;
 
8374
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8375
};
 
8376
 
 
8377
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8378
template class I_List<COND_CMP>;
 
8379
template class I_List_iterator<COND_CMP>;
 
8380
#endif
 
8381
 
1324
8382
 
1325
8383
/**
1326
8384
  Find the multiple equality predicate containing a field.
1340
8398
    - Item_equal for the found multiple equality predicate if a success;
1341
8399
    - NULL otherwise.
1342
8400
*/
1343
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8401
 
 
8402
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8403
                            bool *inherited_fl)
1344
8404
{
1345
8405
  Item_equal *item= 0;
1346
8406
  bool in_upper_level= false;
1361
8421
  return item;
1362
8422
}
1363
8423
 
 
8424
  
1364
8425
/**
1365
8426
  Check whether an equality can be used to build multiple equalities.
1366
8427
 
1387
8448
    the check_equality will be called for the following equality
1388
8449
    predicates a=b, b=c, b=2 and f=e.
1389
8450
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1390
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8451
      *cond_equal into (0,[Item_equal(a,b)]). 
1391
8452
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1392
8453
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1393
8454
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1400
8461
    the Field::eq_def method) are placed to the same multiple equalities.
1401
8462
    Because of this some equality predicates are not eliminated and
1402
8463
    can be used in the constant propagation procedure.
1403
 
    We could weeken the equlity test as soon as at least one of the
1404
 
    equal fields is to be equal to a constant. It would require a
 
8464
    We could weeken the equlity test as soon as at least one of the 
 
8465
    equal fields is to be equal to a constant. It would require a 
1405
8466
    more complicated implementation: we would have to store, in
1406
8467
    general case, its own constant for each fields from the multiple
1407
8468
    equality. But at the same time it would allow us to get rid
1419
8480
    containing just field1 and field2 is added to the existing
1420
8481
    multiple equalities.
1421
8482
    If the function processes the predicate of the form field1=const,
1422
 
    it looks for a multiple equality containing field1. If found, the
 
8483
    it looks for a multiple equality containing field1. If found, the 
1423
8484
    function checks the constant of the multiple equality. If the value
1424
8485
    is unknown, it is setup to const. Otherwise the value is compared with
1425
8486
    const and the evaluation of the equality predicate is performed.
1442
8503
  @retval
1443
8504
    false   otherwise
1444
8505
*/
1445
 
static bool check_simple_equality(Item *left_item,
1446
 
                                  Item *right_item,
1447
 
                                  Item *item,
1448
 
                                  COND_EQUAL *cond_equal)
 
8506
 
 
8507
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8508
                                  Item *item, COND_EQUAL *cond_equal)
1449
8509
{
 
8510
  if (left_item->type() == Item::REF_ITEM &&
 
8511
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8512
  {
 
8513
    if (((Item_ref*)left_item)->depended_from)
 
8514
      return false;
 
8515
    left_item= left_item->real_item();
 
8516
  }
 
8517
  if (right_item->type() == Item::REF_ITEM &&
 
8518
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8519
  {
 
8520
    if (((Item_ref*)right_item)->depended_from)
 
8521
      return false;
 
8522
    right_item= right_item->real_item();
 
8523
  }
1450
8524
  if (left_item->type() == Item::FIELD_ITEM &&
1451
8525
      right_item->type() == Item::FIELD_ITEM &&
1452
8526
      !((Item_field*)left_item)->depended_from &&
1464
8538
    bool left_copyfl, right_copyfl;
1465
8539
    Item_equal *left_item_equal=
1466
8540
               find_item_equal(cond_equal, left_field, &left_copyfl);
1467
 
    Item_equal *right_item_equal=
 
8541
    Item_equal *right_item_equal= 
1468
8542
               find_item_equal(cond_equal, right_field, &right_copyfl);
1469
8543
 
1470
8544
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1471
8545
    if (left_field->eq(right_field)) /* f = f */
1472
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8546
      return (!(left_field->maybe_null() && !left_item_equal)); 
1473
8547
 
1474
8548
    if (left_item_equal && left_item_equal == right_item_equal)
1475
8549
    {
1476
 
      /*
 
8550
      /* 
1477
8551
        The equality predicate is inference of one of the existing
1478
8552
        multiple equalities, i.e the condition is already covered
1479
8553
        by upper level equalities
1480
8554
      */
1481
8555
       return true;
1482
8556
    }
1483
 
 
1484
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8557
    
 
8558
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1485
8559
                              item->name < subq_sj_cond_name + 64);
1486
8560
    /* Copy the found multiple equalities at the current level if needed */
1487
8561
    if (left_copyfl)
1502
8576
    }
1503
8577
 
1504
8578
    if (left_item_equal)
1505
 
    {
 
8579
    { 
1506
8580
      /* left item was found in the current or one of the upper levels */
1507
8581
      if (! right_item_equal)
1508
8582
        left_item_equal->add((Item_field *) right_item);
1517
8591
      }
1518
8592
    }
1519
8593
    else
1520
 
    {
 
8594
    { 
1521
8595
      /* left item was not found neither the current nor in upper levels  */
1522
8596
      if (right_item_equal)
1523
8597
      {
1525
8599
        if (copy_item_name)
1526
8600
          right_item_equal->name = item->name;
1527
8601
      }
1528
 
      else
 
8602
      else 
1529
8603
      {
1530
8604
        /* None of the fields was found in multiple equalities */
1531
8605
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1573
8647
          eq_item->set_cmp_func();
1574
8648
          eq_item->quick_fix_field();
1575
8649
          item= eq_item;
1576
 
        }
 
8650
        }  
1577
8651
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1578
8652
            !cs->coll->propagate(cs, 0, 0))
1579
8653
          return false;
1588
8662
      }
1589
8663
      if (item_equal)
1590
8664
      {
1591
 
        /*
 
8665
        /* 
1592
8666
          The flag cond_false will be set to 1 after this, if item_equal
1593
8667
          already contains a constant and its value is  not equal to
1594
8668
          the value of const_item.
1606
8680
  return false;
1607
8681
}
1608
8682
 
 
8683
 
1609
8684
/**
1610
8685
  Convert row equalities into a conjunction of regular equalities.
1611
8686
 
1618
8693
    simple equality nor a row equality the item for this predicate is added
1619
8694
    to eq_list.
1620
8695
 
1621
 
  @param session        thread handle
 
8696
  @param thd        thread handle
1622
8697
  @param left_row   left term of the row equality to be processed
1623
8698
  @param right_row  right term of the row equality to be processed
1624
8699
  @param cond_equal multiple equalities that must hold together with the
1631
8706
  @retval
1632
8707
    false   otherwise
1633
8708
*/
1634
 
static bool check_row_equality(Session *session,
1635
 
                               Item *left_row, 
1636
 
                               Item_row *right_row,
1637
 
                               COND_EQUAL *cond_equal,
1638
 
                               List<Item>* eq_list)
1639
 
{
 
8709
 
 
8710
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8711
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8712
1640
8713
  uint32_t n= left_row->cols();
1641
8714
  for (uint32_t i= 0 ; i < n; i++)
1642
8715
  {
1646
8719
    if (left_item->type() == Item::ROW_ITEM &&
1647
8720
        right_item->type() == Item::ROW_ITEM)
1648
8721
    {
1649
 
      is_converted= check_row_equality(session,
 
8722
      is_converted= check_row_equality(thd, 
1650
8723
                                       (Item_row *) left_item,
1651
8724
                                       (Item_row *) right_item,
1652
8725
                                       cond_equal, eq_list);
1653
8726
      if (!is_converted)
1654
 
        session->lex->current_select->cond_count++;
 
8727
        thd->lex->current_select->cond_count++;      
1655
8728
    }
1656
8729
    else
1657
 
    {
 
8730
    { 
1658
8731
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1659
 
      session->lex->current_select->cond_count++;
1660
 
    }
1661
 
 
 
8732
      thd->lex->current_select->cond_count++;
 
8733
    }  
 
8734
 
1662
8735
    if (!is_converted)
1663
8736
    {
1664
8737
      Item_func_eq *eq_item;
1672
8745
  return true;
1673
8746
}
1674
8747
 
 
8748
 
1675
8749
/**
1676
8750
  Eliminate row equalities and form multiple equalities predicates.
1677
8751
 
1686
8760
    equalities which are treated in the same way as original equality
1687
8761
    predicates.
1688
8762
 
1689
 
  @param session        thread handle
 
8763
  @param thd        thread handle
1690
8764
  @param item       predicate to process
1691
8765
  @param cond_equal multiple equalities that must hold together with the
1692
8766
                    predicate
1701
8775
           or, if the equality is neither a simple one nor a row equality,
1702
8776
           or, if the procedure fails by a fatal error.
1703
8777
*/
1704
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8778
 
 
8779
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8780
                           List<Item> *eq_list)
1705
8781
{
1706
8782
  if (item->type() == Item::FUNC_ITEM &&
1707
8783
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1712
8788
    if (left_item->type() == Item::ROW_ITEM &&
1713
8789
        right_item->type() == Item::ROW_ITEM)
1714
8790
    {
1715
 
      session->lex->current_select->cond_count--;
1716
 
      return check_row_equality(session,
 
8791
      thd->lex->current_select->cond_count--;
 
8792
      return check_row_equality(thd,
1717
8793
                                (Item_row *) left_item,
1718
8794
                                (Item_row *) right_item,
1719
8795
                                cond_equal, eq_list);
1720
8796
    }
1721
 
    else
 
8797
    else 
1722
8798
      return check_simple_equality(left_item, right_item, item, cond_equal);
1723
 
  }
 
8799
  } 
1724
8800
  return false;
1725
8801
}
1726
8802
 
 
8803
                          
1727
8804
/**
1728
8805
  Replace all equality predicates in a condition by multiple equality items.
1729
8806
 
1730
8807
    At each 'and' level the function detects items for equality predicates
1731
8808
    and replaced them by a set of multiple equality items of class Item_equal,
1732
 
    taking into account inherited equalities from upper levels.
 
8809
    taking into account inherited equalities from upper levels. 
1733
8810
    If an equality predicate is used not in a conjunction it's just
1734
8811
    replaced by a multiple equality predicate.
1735
8812
    For each 'and' level the function set a pointer to the inherited
1736
8813
    multiple equalities in the cond_equal field of the associated
1737
 
    object of the type Item_cond_and.
 
8814
    object of the type Item_cond_and.   
1738
8815
    The function also traverses the cond tree and and for each field reference
1739
8816
    sets a pointer to the multiple equality item containing the field, if there
1740
8817
    is any. If this multiple equality equates fields to a constant the
1741
 
    function replaces the field reference by the constant in the cases
 
8818
    function replaces the field reference by the constant in the cases 
1742
8819
    when the field is not of a string type or when the field reference is
1743
8820
    just an argument of a comparison predicate.
1744
 
    The function also determines the maximum number of members in
 
8821
    The function also determines the maximum number of members in 
1745
8822
    equality lists of each Item_cond_and object assigning it to
1746
 
    session->lex->current_select->max_equal_elems.
 
8823
    thd->lex->current_select->max_equal_elems.
1747
8824
 
1748
8825
  @note
1749
8826
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1755
8832
    in a conjuction for a minimal set of multiple equality predicates.
1756
8833
    This set can be considered as a canonical representation of the
1757
8834
    sub-conjunction of the equality predicates.
1758
 
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
 
8835
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
1759
8836
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
1760
8837
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
1761
8838
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
1766
8843
    The function performs the substitution in a recursive descent by
1767
8844
    the condtion tree, passing to the next AND level a chain of multiple
1768
8845
    equality predicates which have been built at the upper levels.
1769
 
    The Item_equal items built at the level are attached to other
 
8846
    The Item_equal items built at the level are attached to other 
1770
8847
    non-equality conjucts as a sublist. The pointer to the inherited
1771
8848
    multiple equalities is saved in the and condition object (Item_cond_and).
1772
 
    This chain allows us for any field reference occurence easyly to find a
 
8849
    This chain allows us for any field reference occurence easyly to find a 
1773
8850
    multiple equality that must be held for this occurence.
1774
8851
    For each AND level we do the following:
1775
8852
    - scan it for all equality predicate (=) items
1776
8853
    - join them into disjoint Item_equal() groups
1777
 
    - process the included OR conditions recursively to do the same for
1778
 
      lower AND levels.
 
8854
    - process the included OR conditions recursively to do the same for 
 
8855
      lower AND levels. 
1779
8856
 
1780
8857
    We need to do things in this order as lower AND levels need to know about
1781
8858
    all possible Item_equal objects in upper levels.
1782
8859
 
1783
 
  @param session        thread handle
 
8860
  @param thd        thread handle
1784
8861
  @param cond       condition(expression) where to make replacement
1785
8862
  @param inherited  path to all inherited multiple equality items
1786
8863
 
1787
8864
  @return
1788
8865
    pointer to the transformed condition
1789
8866
*/
1790
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8867
 
 
8868
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8869
                                        COND_EQUAL *inherited)
1791
8870
{
1792
8871
  Item_equal *item_equal;
1793
8872
  COND_EQUAL cond_equal;
1799
8878
    bool and_level= ((Item_cond*) cond)->functype() ==
1800
8879
      Item_func::COND_AND_FUNC;
1801
8880
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1802
 
 
 
8881
    
1803
8882
    List_iterator<Item> li(*args);
1804
8883
    Item *item;
1805
8884
 
1808
8887
      /*
1809
8888
         Retrieve all conjucts of this level detecting the equality
1810
8889
         that are subject to substitution by multiple equality items and
1811
 
         removing each such predicate from the conjunction after having
 
8890
         removing each such predicate from the conjunction after having 
1812
8891
         found/created a multiple equality whose inference the predicate is.
1813
 
     */
 
8892
     */      
1814
8893
      while ((item= li++))
1815
8894
      {
1816
8895
        /*
1818
8897
          structure here because it's restored before each
1819
8898
          re-execution of any prepared statement/stored procedure.
1820
8899
        */
1821
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8900
        if (check_equality(thd, item, &cond_equal, &eq_list))
1822
8901
          li.remove();
1823
8902
      }
1824
8903
 
1827
8906
      {
1828
8907
        item_equal->fix_length_and_dec();
1829
8908
        item_equal->update_used_tables();
1830
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1831
 
                      item_equal->members());
 
8909
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8910
                      item_equal->members());  
1832
8911
      }
1833
8912
 
1834
8913
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
1840
8919
    */
1841
8920
    li.rewind();
1842
8921
    while ((item= li++))
1843
 
    {
 
8922
    { 
1844
8923
      Item *new_item;
1845
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8924
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
1846
8925
      {
1847
8926
        /* This replacement happens only for standalone equalities */
1848
8927
        /*
1870
8949
      (b=5) and (a=c) are standalone equalities.
1871
8950
      In general we can't leave alone standalone eqalities:
1872
8951
      for WHERE a=b AND c=d AND (b=c OR d=5)
1873
 
      b=c is replaced by =(a,b,c,d).
 
8952
      b=c is replaced by =(a,b,c,d).  
1874
8953
     */
1875
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8954
    if (check_equality(thd, cond, &cond_equal, &eq_list))
1876
8955
    {
1877
8956
      int n= cond_equal.current_level.elements + eq_list.elements;
1878
8957
      if (n == 0)
1883
8962
        {
1884
8963
          item_equal->fix_length_and_dec();
1885
8964
          item_equal->update_used_tables();
1886
 
        }
 
8965
        }
1887
8966
        else
1888
8967
          item_equal= (Item_equal *) eq_list.pop();
1889
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
1890
 
                      item_equal->members());
 
8968
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8969
                      item_equal->members());  
1891
8970
        return item_equal;
1892
8971
      }
1893
8972
      else
1894
8973
      {
1895
 
        /*
 
8974
        /* 
1896
8975
          Here a new AND level must be created. It can happen only
1897
8976
          when a row equality is processed as a standalone predicate.
1898
 
        */
 
8977
        */
1899
8978
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1900
8979
        and_cond->quick_fix_field();
1901
8980
        List<Item> *args= and_cond->argument_list();
1904
8983
        {
1905
8984
          item_equal->fix_length_and_dec();
1906
8985
          item_equal->update_used_tables();
1907
 
          set_if_bigger(session->lex->current_select->max_equal_elems,
1908
 
                        item_equal->members());
 
8986
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8987
                        item_equal->members());  
1909
8988
        }
1910
8989
        and_cond->cond_equal= cond_equal;
1911
8990
        args->concat((List<Item> *)&cond_equal.current_level);
1912
 
 
 
8991
        
1913
8992
        return and_cond;
1914
8993
      }
1915
8994
    }
1916
 
    /*
 
8995
    /* 
1917
8996
      For each field reference in cond, not from equal item predicates,
1918
8997
      set a pointer to the multiple equality it belongs to (if there is any)
1919
8998
      as soon the field is not of a string type or the field reference is
1920
 
      an argument of a comparison predicate.
1921
 
    */
 
8999
      an argument of a comparison predicate. 
 
9000
    */ 
1922
9001
    unsigned char *is_subst_valid= (unsigned char *) 1;
1923
9002
    cond= cond->compile(&Item::subst_argument_checker,
1924
 
                        &is_subst_valid,
 
9003
                        &is_subst_valid, 
1925
9004
                        &Item::equal_fields_propagator,
1926
9005
                        (unsigned char *) inherited);
1927
9006
    cond->update_used_tables();
1929
9008
  return cond;
1930
9009
}
1931
9010
 
 
9011
 
1932
9012
/**
1933
9013
  Build multiple equalities for a condition and all on expressions that
1934
9014
  inherit these multiple equalities.
1974
9054
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
1975
9055
        WHERE t1.a=t2.a
1976
9056
    @endcode
1977
 
    that is equivalent to:
 
9057
    that is equivalent to:   
1978
9058
    @code
1979
9059
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
1980
9060
        WHERE t1.a=t2.a
1981
9061
    @endcode
1982
9062
    Thus, applying equalities from the where condition we basically
1983
9063
    can get more freedom in performing join operations.
1984
 
    Althogh we don't use this property now, it probably makes sense to use
1985
 
    it in the future.
1986
 
  @param session                      Thread Cursor
 
9064
    Althogh we don't use this property now, it probably makes sense to use 
 
9065
    it in the future.    
 
9066
  @param thd                  Thread handler
1987
9067
  @param cond                condition to build the multiple equalities for
1988
9068
  @param inherited           path to all inherited multiple equality items
1989
9069
  @param join_list           list of join tables to which the condition
1994
9074
  @return
1995
9075
    pointer to the transformed condition containing multiple equalities
1996
9076
*/
1997
 
static COND *build_equal_items(Session *session, COND *cond,
 
9077
   
 
9078
static COND *build_equal_items(THD *thd, COND *cond,
1998
9079
                               COND_EQUAL *inherited,
1999
9080
                               List<TableList> *join_list,
2000
9081
                               COND_EQUAL **cond_equal_ref)
2001
9082
{
2002
9083
  COND_EQUAL *cond_equal= 0;
2003
9084
 
2004
 
  if (cond)
 
9085
  if (cond) 
2005
9086
  {
2006
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9087
    cond= build_equal_items_for_cond(thd, cond, inherited);
2007
9088
    cond->update_used_tables();
2008
9089
    if (cond->type() == Item::COND_ITEM &&
2009
9090
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2031
9112
    {
2032
9113
      if (table->on_expr)
2033
9114
      {
2034
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2035
 
          &table->getNestedJoin()->join_list : NULL;
 
9115
        List<TableList> *nested_join_list= table->nested_join ?
 
9116
          &table->nested_join->join_list : NULL;
2036
9117
        /*
2037
9118
          We can modify table->on_expr because its old value will
2038
9119
          be restored before re-execution of PS/SP.
2039
9120
        */
2040
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9121
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2041
9122
                                          nested_join_list,
2042
9123
                                          &table->cond_equal);
2043
9124
      }
2045
9126
  }
2046
9127
 
2047
9128
  return cond;
2048
 
}
 
9129
}    
 
9130
 
2049
9131
 
2050
9132
/**
2051
9133
  Compare field items by table order in the execution plan.
2052
9134
 
2053
9135
    field1 considered as better than field2 if the table containing
2054
 
    field1 is accessed earlier than the table containing field2.
 
9136
    field1 is accessed earlier than the table containing field2.   
2055
9137
    The function finds out what of two fields is better according
2056
9138
    this criteria.
2057
9139
 
2066
9148
  @retval
2067
9149
    0  otherwise
2068
9150
*/
 
9151
 
2069
9152
static int compare_fields_by_table_order(Item_field *field1,
2070
 
                                         Item_field *field2,
2071
 
                                         void *table_join_idx)
 
9153
                                  Item_field *field2,
 
9154
                                  void *table_join_idx)
2072
9155
{
2073
9156
  int cmp= 0;
2074
9157
  bool outer_ref= 0;
2075
9158
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2076
 
  {
 
9159
  {  
2077
9160
    outer_ref= 1;
2078
9161
    cmp= -1;
2079
9162
  }
2084
9167
  }
2085
9168
  if (outer_ref)
2086
9169
    return cmp;
2087
 
  JoinTable **idx= (JoinTable **) table_join_idx;
2088
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
9170
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
9171
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2089
9172
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2090
9173
}
2091
9174
 
 
9175
 
2092
9176
/**
2093
9177
  Generate minimal set of simple equalities equivalent to a multiple equality.
2094
9178
 
2128
9212
    a pointer to the simple generated equality, if success.
2129
9213
    - 0, otherwise.
2130
9214
*/
2131
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9215
 
 
9216
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9217
                                  Item_equal *item_equal)
2132
9218
{
2133
9219
  List<Item> eq_list;
2134
9220
  Item_func_eq *eq_item= 0;
2135
9221
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2136
 
    return new Item_int((int64_t) 0,1);
 
9222
    return new Item_int((int64_t) 0,1); 
2137
9223
  Item *item_const= item_equal->get_const();
2138
9224
  Item_equal_iterator it(*item_equal);
2139
9225
  Item *head;
2150
9236
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2151
9237
    Item_field *item= item_field;
2152
9238
    if (upper)
2153
 
    {
 
9239
    { 
2154
9240
      if (item_const && upper->get_const())
2155
9241
        item= 0;
2156
9242
      else
2194
9280
 
2195
9281
  cond->quick_fix_field();
2196
9282
  cond->update_used_tables();
2197
 
 
 
9283
   
2198
9284
  return cond;
2199
9285
}
2200
9286
 
 
9287
 
2201
9288
/**
2202
9289
  Substitute every field reference in a condition by the best equal field
2203
9290
  and eliminate all multiple equality predicates.
2206
9293
    multiple equality predicate it sorts the field references in it
2207
9294
    according to the order of tables specified by the table_join_idx
2208
9295
    parameter. Then it eliminates the multiple equality predicate it
2209
 
    replacing it by the conjunction of simple equality predicates
 
9296
    replacing it by the conjunction of simple equality predicates 
2210
9297
    equating every field from the multiple equality to the first
2211
9298
    field in it, or to the constant, if there is any.
2212
9299
    After this the function retrieves all other conjuncted
2225
9312
  @return
2226
9313
    The transformed condition
2227
9314
*/
2228
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9315
 
 
9316
static COND* substitute_for_best_equal_field(COND *cond,
 
9317
                                             COND_EQUAL *cond_equal,
 
9318
                                             void *table_join_idx)
2229
9319
{
2230
9320
  Item_equal *item_equal;
2231
9321
 
2240
9330
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2241
9331
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2242
9332
 
2243
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
9333
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2244
9334
      while ((item_equal= it++))
2245
9335
      {
2246
9336
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2247
9337
      }
2248
9338
    }
2249
 
 
 
9339
    
2250
9340
    List_iterator<Item> li(*cond_list);
2251
9341
    Item *item;
2252
9342
    while ((item= li++))
2279
9369
      cond= new Item_int((int32_t)cond->val_bool());
2280
9370
 
2281
9371
  }
2282
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9372
  else if (cond->type() == Item::FUNC_ITEM && 
2283
9373
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2284
9374
  {
2285
9375
    item_equal= (Item_equal *) cond;
2293
9383
  return cond;
2294
9384
}
2295
9385
 
 
9386
 
2296
9387
/**
2297
9388
  Check appearance of new constant items in multiple equalities
2298
9389
  of a condition after reading a constant table.
2305
9396
  @param cond       condition whose multiple equalities are to be checked
2306
9397
  @param table      constant table that has been read
2307
9398
*/
2308
 
static void update_const_equal_items(COND *cond, JoinTable *tab)
 
9399
 
 
9400
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2309
9401
{
2310
9402
  if (!(cond->used_tables() & tab->table->map))
2311
9403
    return;
2312
9404
 
2313
9405
  if (cond->type() == Item::COND_ITEM)
2314
9406
  {
2315
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
 
9407
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
2316
9408
    List_iterator_fast<Item> li(*cond_list);
2317
9409
    Item *item;
2318
9410
    while ((item= li++))
2319
9411
      update_const_equal_items(item, tab);
2320
9412
  }
2321
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9413
  else if (cond->type() == Item::FUNC_ITEM && 
2322
9414
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2323
9415
  {
2324
9416
    Item_equal *item_equal= (Item_equal *) cond;
2332
9424
      while ((item_field= it++))
2333
9425
      {
2334
9426
        Field *field= item_field->field;
2335
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
9427
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2336
9428
        key_map possible_keys= field->key_start;
2337
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
2338
 
        stat[0].const_keys|= possible_keys;
 
9429
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9430
        stat[0].const_keys.merge(possible_keys);
2339
9431
 
2340
9432
        /*
2341
 
          For each field in the multiple equality (for which we know that it
2342
 
          is a constant) we have to find its corresponding key part, and set
 
9433
          For each field in the multiple equality (for which we know that it 
 
9434
          is a constant) we have to find its corresponding key part, and set 
2343
9435
          that key part in const_key_parts.
2344
 
        */
2345
 
        if (possible_keys.any())
 
9436
        */  
 
9437
        if (!possible_keys.is_clear_all())
2346
9438
        {
2347
 
          Table *field_tab= field->getTable();
2348
 
          optimizer::KeyUse *use;
2349
 
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2350
 
            if (possible_keys.test(use->getKey()) &&
2351
 
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
 
9439
          Table *tab= field->table;
 
9440
          KEYUSE *use;
 
9441
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9442
            if (possible_keys.is_set(use->key) && 
 
9443
                tab->key_info[use->key].key_part[use->keypart].field ==
2352
9444
                field)
2353
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9445
              tab->const_key_parts[use->key]|= use->keypart_map;
2354
9446
        }
2355
9447
      }
2356
9448
    }
2357
9449
  }
2358
9450
}
2359
9451
 
 
9452
 
2360
9453
/*
2361
9454
  change field = field to field = const for each found field = const in the
2362
9455
  and_level
2363
9456
*/
2364
 
static void change_cond_ref_to_const(Session *session,
2365
 
                                     vector<COND_CMP>& save_list,
2366
 
                                     Item *and_father,
2367
 
                                     Item *cond,
2368
 
                                     Item *field,
2369
 
                                     Item *value)
 
9457
 
 
9458
static void
 
9459
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9460
                         Item *and_father, Item *cond,
 
9461
                         Item *field, Item *value)
2370
9462
{
2371
9463
  if (cond->type() == Item::COND_ITEM)
2372
9464
  {
2373
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9465
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9466
      Item_func::COND_AND_FUNC;
2374
9467
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2375
9468
    Item *item;
2376
9469
    while ((item=li++))
2377
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
 
9470
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9471
                               field, value);
2378
9472
    return;
2379
9473
  }
2380
9474
  if (cond->eq_cmp_result() == Item::COND_OK)
2393
9487
       left_item->collation.collation == value->collation.collation))
2394
9488
  {
2395
9489
    Item *tmp=value->clone_item();
 
9490
    tmp->collation.set(right_item->collation);
 
9491
    
2396
9492
    if (tmp)
2397
9493
    {
2398
 
      tmp->collation.set(right_item->collation);
2399
 
      session->change_item_tree(args + 1, tmp);
 
9494
      thd->change_item_tree(args + 1, tmp);
2400
9495
      func->update_used_tables();
2401
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2402
 
                and_father != cond && 
2403
 
          ! left_item->const_item())
 
9496
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9497
          && and_father != cond && !left_item->const_item())
2404
9498
      {
2405
 
        cond->marker=1;
2406
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9499
        cond->marker=1;
 
9500
        COND_CMP *tmp2;
 
9501
        if ((tmp2=new COND_CMP(and_father,func)))
 
9502
          save_list->push_back(tmp2);
2407
9503
      }
2408
9504
      func->set_cmp_func();
2409
9505
    }
2415
9511
            right_item->collation.collation == value->collation.collation))
2416
9512
  {
2417
9513
    Item *tmp= value->clone_item();
 
9514
    tmp->collation.set(left_item->collation);
 
9515
    
2418
9516
    if (tmp)
2419
9517
    {
2420
 
      tmp->collation.set(left_item->collation);
2421
 
      session->change_item_tree(args, tmp);
 
9518
      thd->change_item_tree(args, tmp);
2422
9519
      value= tmp;
2423
9520
      func->update_used_tables();
2424
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2425
 
          and_father != cond && 
2426
 
          ! right_item->const_item())
 
9521
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9522
          && and_father != cond && !right_item->const_item())
2427
9523
      {
2428
9524
        args[0]= args[1];                       // For easy check
2429
 
        session->change_item_tree(args + 1, value);
2430
 
        cond->marker=1;
2431
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9525
        thd->change_item_tree(args + 1, value);
 
9526
        cond->marker=1;
 
9527
        COND_CMP *tmp2;
 
9528
        if ((tmp2=new COND_CMP(and_father,func)))
 
9529
          save_list->push_back(tmp2);
2432
9530
      }
2433
9531
      func->set_cmp_func();
2434
9532
    }
2443
9541
  @return
2444
9542
    new conditions
2445
9543
*/
2446
 
Item *remove_additional_cond(Item* conds)
 
9544
 
 
9545
static Item *remove_additional_cond(Item* conds)
2447
9546
{
2448
9547
  if (conds->name == in_additional_cond)
2449
9548
    return 0;
2466
9565
  return conds;
2467
9566
}
2468
9567
 
2469
 
static void propagate_cond_constants(Session *session, 
2470
 
                                     vector<COND_CMP>& save_list, 
2471
 
                                     COND *and_father, 
2472
 
                                     COND *cond)
 
9568
static void
 
9569
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9570
                         COND *and_father, COND *cond)
2473
9571
{
2474
9572
  if (cond->type() == Item::COND_ITEM)
2475
9573
  {
2476
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9574
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9575
      Item_func::COND_AND_FUNC;
2477
9576
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2478
9577
    Item *item;
2479
 
    vector<COND_CMP> save;
 
9578
    I_List<COND_CMP> save;
2480
9579
    while ((item=li++))
2481
9580
    {
2482
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9581
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2483
9582
    }
2484
9583
    if (and_level)
2485
 
    {
2486
 
      // Handle other found items
2487
 
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9584
    {                                           // Handle other found items
 
9585
      I_List_iterator<COND_CMP> cond_itr(save);
 
9586
      COND_CMP *cond_cmp;
 
9587
      while ((cond_cmp=cond_itr++))
2488
9588
      {
2489
 
        Item **args= iter->cmp_func->arguments();
 
9589
        Item **args= cond_cmp->cmp_func->arguments();
2490
9590
        if (!args[0]->const_item())
2491
 
        {
2492
 
          change_cond_ref_to_const( session, save, iter->and_level,
2493
 
                                    iter->and_level, args[0], args[1] );
2494
 
        }
 
9591
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9592
                                   cond_cmp->and_level, args[0], args[1]);
2495
9593
      }
2496
9594
    }
2497
9595
  }
2498
9596
  else if (and_father != cond && !cond->marker)         // In a AND group
2499
9597
  {
2500
9598
    if (cond->type() == Item::FUNC_ITEM &&
2501
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2502
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9599
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9600
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2503
9601
    {
2504
9602
      Item_func_eq *func=(Item_func_eq*) cond;
2505
9603
      Item **args= func->arguments();
2508
9606
      if (!(left_const && right_const) &&
2509
9607
          args[0]->result_type() == args[1]->result_type())
2510
9608
      {
2511
 
        if (right_const)
2512
 
        {
2513
 
                resolve_const_item(session, &args[1], args[0]);
2514
 
          func->update_used_tables();
2515
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2516
 
                                        args[0], args[1]);
2517
 
        }
2518
 
        else if (left_const)
2519
 
        {
2520
 
                resolve_const_item(session, &args[0], args[1]);
2521
 
          func->update_used_tables();
2522
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2523
 
                                        args[1], args[0]);
2524
 
        }
2525
 
      }
2526
 
    }
2527
 
  }
2528
 
}
 
9609
        if (right_const)
 
9610
        {
 
9611
          resolve_const_item(thd, &args[1], args[0]);
 
9612
          func->update_used_tables();
 
9613
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9614
                                   args[0], args[1]);
 
9615
        }
 
9616
        else if (left_const)
 
9617
        {
 
9618
          resolve_const_item(thd, &args[0], args[1]);
 
9619
          func->update_used_tables();
 
9620
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9621
                                   args[1], args[0]);
 
9622
        }
 
9623
      }
 
9624
    }
 
9625
  }
 
9626
}
 
9627
 
 
9628
 
 
9629
/**
 
9630
  Simplify joins replacing outer joins by inner joins whenever it's
 
9631
  possible.
 
9632
 
 
9633
    The function, during a retrieval of join_list,  eliminates those
 
9634
    outer joins that can be converted into inner join, possibly nested.
 
9635
    It also moves the on expressions for the converted outer joins
 
9636
    and from inner joins to conds.
 
9637
    The function also calculates some attributes for nested joins:
 
9638
    - used_tables    
 
9639
    - not_null_tables
 
9640
    - dep_tables.
 
9641
    - on_expr_dep_tables
 
9642
    The first two attributes are used to test whether an outer join can
 
9643
    be substituted for an inner join. The third attribute represents the
 
9644
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9645
    on table t1, then in any evaluated execution plan table access to
 
9646
    table t2 must precede access to table t2. This relation is used also
 
9647
    to check whether the query contains  invalid cross-references.
 
9648
    The forth attribute is an auxiliary one and is used to calculate
 
9649
    dep_tables.
 
9650
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9651
    execution plan, the dependencies required by the straight join
 
9652
    modifiers are reflected in this attribute as well.
 
9653
    The function also removes all braces that can be removed from the join
 
9654
    expression without changing its meaning.
 
9655
 
 
9656
  @note
 
9657
    An outer join can be replaced by an inner join if the where condition
 
9658
    or the on expression for an embedding nested join contains a conjunctive
 
9659
    predicate rejecting null values for some attribute of the inner tables.
 
9660
 
 
9661
    E.g. in the query:    
 
9662
    @code
 
9663
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9664
    @endcode
 
9665
    the predicate t2.b < 5 rejects nulls.
 
9666
    The query is converted first to:
 
9667
    @code
 
9668
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9669
    @endcode
 
9670
    then to the equivalent form:
 
9671
    @code
 
9672
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9673
    @endcode
 
9674
 
 
9675
 
 
9676
    Similarly the following query:
 
9677
    @code
 
9678
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9679
        WHERE t2.c < 5  
 
9680
    @endcode
 
9681
    is converted to:
 
9682
    @code
 
9683
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9684
 
 
9685
    @endcode
 
9686
 
 
9687
    One conversion might trigger another:
 
9688
    @code
 
9689
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9690
                       LEFT JOIN t3 ON t3.b=t2.b
 
9691
        WHERE t3 IS NOT NULL =>
 
9692
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9693
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9694
      SELECT * FROM t1, t2, t3
 
9695
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9696
  @endcode
 
9697
 
 
9698
    The function removes all unnecessary braces from the expression
 
9699
    produced by the conversions.
 
9700
    E.g.
 
9701
    @code
 
9702
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9703
    @endcode
 
9704
    finally is converted to: 
 
9705
    @code
 
9706
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9707
 
 
9708
    @endcode
 
9709
 
 
9710
 
 
9711
    It also will remove braces from the following queries:
 
9712
    @code
 
9713
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9714
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9715
    @endcode
 
9716
 
 
9717
    The benefit of this simplification procedure is that it might return 
 
9718
    a query for which the optimizer can evaluate execution plan with more
 
9719
    join orders. With a left join operation the optimizer does not
 
9720
    consider any plan where one of the inner tables is before some of outer
 
9721
    tables.
 
9722
 
 
9723
  IMPLEMENTATION
 
9724
    The function is implemented by a recursive procedure.  On the recursive
 
9725
    ascent all attributes are calculated, all outer joins that can be
 
9726
    converted are replaced and then all unnecessary braces are removed.
 
9727
    As join list contains join tables in the reverse order sequential
 
9728
    elimination of outer joins does not require extra recursive calls.
 
9729
 
 
9730
  SEMI-JOIN NOTES
 
9731
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9732
    an "ancestor" semi-join nest)
 
9733
 
 
9734
  EXAMPLES
 
9735
    Here is an example of a join query with invalid cross references:
 
9736
    @code
 
9737
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9738
    @endcode
 
9739
 
 
9740
  @param join        reference to the query info
 
9741
  @param join_list   list representation of the join to be converted
 
9742
  @param conds       conditions to add on expressions for converted joins
 
9743
  @param top         true <=> conds is the where condition
 
9744
 
 
9745
  @return
 
9746
    - The new condition, if success
 
9747
    - 0, otherwise
 
9748
*/
 
9749
 
 
9750
static COND *
 
9751
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
 
9752
               bool in_sj)
 
9753
{
 
9754
  TableList *table;
 
9755
  nested_join_st *nested_join;
 
9756
  TableList *prev_table= 0;
 
9757
  List_iterator<TableList> li(*join_list);
 
9758
 
 
9759
  /* 
 
9760
    Try to simplify join operations from join_list.
 
9761
    The most outer join operation is checked for conversion first. 
 
9762
  */
 
9763
  while ((table= li++))
 
9764
  {
 
9765
    table_map used_tables;
 
9766
    table_map not_null_tables= (table_map) 0;
 
9767
 
 
9768
    if ((nested_join= table->nested_join))
 
9769
    {
 
9770
      /* 
 
9771
         If the element of join_list is a nested join apply
 
9772
         the procedure to its nested join list first.
 
9773
      */
 
9774
      if (table->on_expr)
 
9775
      {
 
9776
        Item *expr= table->on_expr;
 
9777
        /* 
 
9778
           If an on expression E is attached to the table, 
 
9779
           check all null rejected predicates in this expression.
 
9780
           If such a predicate over an attribute belonging to
 
9781
           an inner table of an embedded outer join is found,
 
9782
           the outer join is converted to an inner join and
 
9783
           the corresponding on expression is added to E. 
 
9784
        */ 
 
9785
        expr= simplify_joins(join, &nested_join->join_list,
 
9786
                             expr, false, in_sj || table->sj_on_expr);
 
9787
 
 
9788
        if (!table->prep_on_expr || expr != table->on_expr)
 
9789
        {
 
9790
          assert(expr);
 
9791
 
 
9792
          table->on_expr= expr;
 
9793
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9794
        }
 
9795
      }
 
9796
      nested_join->used_tables= (table_map) 0;
 
9797
      nested_join->not_null_tables=(table_map) 0;
 
9798
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9799
                            in_sj || table->sj_on_expr);
 
9800
      used_tables= nested_join->used_tables;
 
9801
      not_null_tables= nested_join->not_null_tables;  
 
9802
    }
 
9803
    else
 
9804
    {
 
9805
      if (!table->prep_on_expr)
 
9806
        table->prep_on_expr= table->on_expr;
 
9807
      used_tables= table->table->map;
 
9808
      if (conds)
 
9809
        not_null_tables= conds->not_null_tables();
 
9810
    }
 
9811
      
 
9812
    if (table->embedding)
 
9813
    {
 
9814
      table->embedding->nested_join->used_tables|= used_tables;
 
9815
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9816
    }
 
9817
 
 
9818
    if (!table->outer_join || (used_tables & not_null_tables))
 
9819
    {
 
9820
      /* 
 
9821
        For some of the inner tables there are conjunctive predicates
 
9822
        that reject nulls => the outer join can be replaced by an inner join.
 
9823
      */
 
9824
      table->outer_join= 0;
 
9825
      if (table->on_expr)
 
9826
      {
 
9827
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9828
        if (conds)
 
9829
        {
 
9830
          conds= and_conds(conds, table->on_expr);
 
9831
          conds->top_level_item();
 
9832
          /* conds is always a new item as both cond and on_expr existed */
 
9833
          assert(!conds->fixed);
 
9834
          conds->fix_fields(join->thd, &conds);
 
9835
        }
 
9836
        else
 
9837
          conds= table->on_expr; 
 
9838
        table->prep_on_expr= table->on_expr= 0;
 
9839
      }
 
9840
    }
 
9841
    
 
9842
    if (!top)
 
9843
      continue;
 
9844
 
 
9845
    /* 
 
9846
      Only inner tables of non-convertible outer joins
 
9847
      remain with on_expr.
 
9848
    */ 
 
9849
    if (table->on_expr)
 
9850
    {
 
9851
      table->dep_tables|= table->on_expr->used_tables(); 
 
9852
      if (table->embedding)
 
9853
      {
 
9854
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9855
        /*
 
9856
           Embedding table depends on tables used
 
9857
           in embedded on expressions. 
 
9858
        */
 
9859
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9860
      }
 
9861
      else
 
9862
        table->dep_tables&= ~table->table->map;
 
9863
    }
 
9864
 
 
9865
    if (prev_table)
 
9866
    {
 
9867
      /* The order of tables is reverse: prev_table follows table */
 
9868
      if (prev_table->straight)
 
9869
        prev_table->dep_tables|= used_tables;
 
9870
      if (prev_table->on_expr)
 
9871
      {
 
9872
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9873
        table_map prev_used_tables= prev_table->nested_join ?
 
9874
                                    prev_table->nested_join->used_tables :
 
9875
                                    prev_table->table->map;
 
9876
        /* 
 
9877
          If on expression contains only references to inner tables
 
9878
          we still make the inner tables dependent on the outer tables.
 
9879
          It would be enough to set dependency only on one outer table
 
9880
          for them. Yet this is really a rare case.
 
9881
        */  
 
9882
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9883
          prev_table->dep_tables|= used_tables;
 
9884
      }
 
9885
    }
 
9886
    prev_table= table;
 
9887
  }
 
9888
    
 
9889
  /* 
 
9890
    Flatten nested joins that can be flattened.
 
9891
    no ON expression and not a semi-join => can be flattened.
 
9892
  */
 
9893
  li.rewind();
 
9894
  while ((table= li++))
 
9895
  {
 
9896
    nested_join= table->nested_join;
 
9897
    if (table->sj_on_expr && !in_sj)
 
9898
    {
 
9899
       /*
 
9900
         If this is a semi-join that is not contained within another semi-join, 
 
9901
         leave it intact (otherwise it is flattened)
 
9902
       */
 
9903
      join->select_lex->sj_nests.push_back(table);
 
9904
    }
 
9905
    else if (nested_join && !table->on_expr)
 
9906
    {
 
9907
      TableList *tbl;
 
9908
      List_iterator<TableList> it(nested_join->join_list);
 
9909
      while ((tbl= it++))
 
9910
      {
 
9911
        tbl->embedding= table->embedding;
 
9912
        tbl->join_list= table->join_list;
 
9913
      }      
 
9914
      li.replace(nested_join->join_list);
 
9915
    }
 
9916
  }
 
9917
  return(conds); 
 
9918
}
 
9919
 
 
9920
 
 
9921
/**
 
9922
  Assign each nested join structure a bit in nested_join_map.
 
9923
 
 
9924
    Assign each nested join structure (except "confluent" ones - those that
 
9925
    embed only one element) a bit in nested_join_map.
 
9926
 
 
9927
  @param join          Join being processed
 
9928
  @param join_list     List of tables
 
9929
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9930
                       call
 
9931
 
 
9932
  @note
 
9933
    This function is called after simplify_joins(), when there are no
 
9934
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9935
    we will not run out of bits in nested_join_map.
 
9936
 
 
9937
  @return
 
9938
    First unused bit in nested_join_map after the call.
 
9939
*/
 
9940
 
 
9941
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, 
 
9942
                                          uint32_t first_unused)
 
9943
{
 
9944
  List_iterator<TableList> li(*join_list);
 
9945
  TableList *table;
 
9946
  while ((table= li++))
 
9947
  {
 
9948
    nested_join_st *nested_join;
 
9949
    if ((nested_join= table->nested_join))
 
9950
    {
 
9951
      /*
 
9952
        It is guaranteed by simplify_joins() function that a nested join
 
9953
        that has only one child is either
 
9954
         - a single-table view (the child is the underlying table), or 
 
9955
         - a single-table semi-join nest
 
9956
 
 
9957
        We don't assign bits to such sj-nests because 
 
9958
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9959
            with anything)
 
9960
        2. we could run out bits in nested_join_map otherwise.
 
9961
      */
 
9962
      if (nested_join->join_list.elements != 1)
 
9963
      {
 
9964
        /* Don't assign bits to sj-nests */
 
9965
        if (table->on_expr)
 
9966
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9967
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9968
                                                    first_unused);
 
9969
      }
 
9970
    }
 
9971
  }
 
9972
  return(first_unused);
 
9973
}
 
9974
 
 
9975
 
 
9976
/**
 
9977
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
9978
 
 
9979
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
9980
    the passed join_list.
 
9981
 
 
9982
  @param join_list  List of nested joins to process. It may also contain base
 
9983
                    tables which will be ignored.
 
9984
*/
 
9985
 
 
9986
static void reset_nj_counters(List<TableList> *join_list)
 
9987
{
 
9988
  List_iterator<TableList> li(*join_list);
 
9989
  TableList *table;
 
9990
  while ((table= li++))
 
9991
  {
 
9992
    nested_join_st *nested_join;
 
9993
    if ((nested_join= table->nested_join))
 
9994
    {
 
9995
      nested_join->counter_= 0;
 
9996
      reset_nj_counters(&nested_join->join_list);
 
9997
    }
 
9998
  }
 
9999
  return;
 
10000
}
 
10001
 
2529
10002
 
2530
10003
/**
2531
10004
  Check interleaving with an inner tables of an outer join for
2532
10005
  extension table.
2533
10006
 
2534
 
    Check if table next_tab can be added to current partial join order, and
 
10007
    Check if table next_tab can be added to current partial join order, and 
2535
10008
    if yes, record that it has been added.
2536
10009
 
2537
10010
    The function assumes that both current partial join order and its
2538
10011
    extension with next_tab are valid wrt table dependencies.
2539
10012
 
2540
10013
  @verbatim
2541
 
     IMPLEMENTATION
 
10014
     IMPLEMENTATION 
2542
10015
       LIMITATIONS ON JOIN order_st
2543
10016
         The nested [outer] joins executioner algorithm imposes these limitations
2544
10017
         on join order:
2545
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10018
         1. "Outer tables first" -  any "outer" table must be before any 
2546
10019
             corresponding "inner" table.
2547
10020
         2. "No interleaving" - tables inside a nested join must form a continuous
2548
 
            sequence in join order (i.e. the sequence must not be interrupted by
 
10021
            sequence in join order (i.e. the sequence must not be interrupted by 
2549
10022
            tables that are outside of this nested join).
2550
10023
 
2551
10024
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2552
10025
         been already checked.
2553
10026
 
2554
10027
       WHY NEED NON-INTERLEAVING
2555
 
         Consider an example:
 
10028
         Consider an example: 
2556
10029
 
2557
10030
           select * from t0 join t1 left join (t2 join t3) on cond1
2558
10031
 
2576
10049
         The limitations on join order can be rephrased as follows: for valid
2577
10050
         join order one must be able to:
2578
10051
           1. write down the used tables in the join order on one line.
2579
 
           2. for each nested join, put one '(' and one ')' on the said line
 
10052
           2. for each nested join, put one '(' and one ')' on the said line        
2580
10053
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2581
10054
           4. get a query equivalent to the query we're trying to execute.
2582
10055
 
2583
10056
         Calls to check_interleaving_with_nj() are equivalent to writing the
2584
 
         above described line from left to right.
2585
 
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
 
10057
         above described line from left to right. 
 
10058
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2586
10059
         table B and appropriate brackets on condition that table A and
2587
10060
         appropriate brackets is the last what was written. Graphically the
2588
10061
         transition is as follows:
2595
10068
                                                      position.
2596
10069
 
2597
10070
         Notes about the position:
2598
 
           The caller guarantees that there is no more then one X-bracket by
2599
 
           checking "!(remaining_tables & s->dependent)" before calling this
 
10071
           The caller guarantees that there is no more then one X-bracket by 
 
10072
           checking "!(remaining_tables & s->dependent)" before calling this 
2600
10073
           function. X-bracket may have a pair in Y-bracket.
2601
10074
 
2602
10075
         When "writing" we store/update this auxilary info about the current
2609
10082
  @endverbatim
2610
10083
 
2611
10084
  @param join       Join being processed
 
10085
  @param last_tab   Last table in current partial join order (this function is
 
10086
                    not called for empty partial join orders)
2612
10087
  @param next_tab   Table we're going to extend the current partial join with
2613
10088
 
2614
10089
  @retval
2617
10092
  @retval
2618
10093
    true   Requested join order extension not allowed.
2619
10094
*/
2620
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
10095
 
 
10096
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2621
10097
{
2622
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2623
 
  Join *join= next_tab->join;
 
10098
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10099
  JOIN *join= last_tab->join;
2624
10100
 
2625
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10101
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2626
10102
  {
2627
 
    /*
 
10103
    /* 
2628
10104
      next_tab is outside of the "pair of brackets" we're currently in.
2629
10105
      Cannot add it.
2630
10106
    */
2631
10107
    return true;
2632
10108
  }
2633
 
 
 
10109
   
2634
10110
  /*
2635
10111
    Do update counters for "pairs of brackets" that we've left (marked as
2636
10112
    X,Y,Z in the above picture)
2637
10113
  */
2638
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
10114
  for (;next_emb; next_emb= next_emb->embedding)
2639
10115
  {
2640
 
    next_emb->getNestedJoin()->counter_++;
2641
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
10116
    next_emb->nested_join->counter_++;
 
10117
    if (next_emb->nested_join->counter_ == 1)
2642
10118
    {
2643
 
      /*
 
10119
      /* 
2644
10120
        next_emb is the first table inside a nested join we've "entered". In
2645
10121
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2646
10122
        X bracket might have Y pair bracket.
2647
10123
      */
2648
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
10124
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2649
10125
    }
2650
 
 
2651
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2652
 
        next_emb->getNestedJoin()->counter_)
 
10126
    
 
10127
    if (next_emb->nested_join->join_list.elements !=
 
10128
        next_emb->nested_join->counter_)
2653
10129
      break;
2654
10130
 
2655
10131
    /*
2656
10132
      We're currently at Y or Z-bracket as depicted in the above picture.
2657
10133
      Mark that we've left it and continue walking up the brackets hierarchy.
2658
10134
    */
2659
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
10135
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2660
10136
  }
2661
10137
  return false;
2662
10138
}
2663
10139
 
2664
 
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2665
 
{
2666
 
  Session *session= join->session;
 
10140
 
 
10141
/**
 
10142
  Nested joins perspective: Remove the last table from the join order.
 
10143
 
 
10144
    Remove the last table from the partial join order and update the nested
 
10145
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10146
    function for the first table in join order (for which 
 
10147
    check_interleaving_with_nj has not been called)
 
10148
 
 
10149
  @param last  join table to remove, it is assumed to be the last in current
 
10150
               partial join order.
 
10151
*/
 
10152
 
 
10153
static void restore_prev_nj_state(JOIN_TAB *last)
 
10154
{
 
10155
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
10156
  JOIN *join= last->join;
 
10157
  while (last_emb)
 
10158
  {
 
10159
    if (last_emb->on_expr)
 
10160
    {
 
10161
      if (!(--last_emb->nested_join->counter_))
 
10162
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10163
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10164
               last_emb->nested_join->counter_) 
 
10165
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10166
      else
 
10167
        break;
 
10168
    }
 
10169
    last_emb= last_emb->embedding;
 
10170
  }
 
10171
}
 
10172
 
 
10173
 
 
10174
 
 
10175
static 
 
10176
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10177
{
 
10178
  TableList *emb_sj_nest;
 
10179
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10180
  {
 
10181
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10182
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10183
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10184
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10185
  }
 
10186
}
 
10187
 
 
10188
 
 
10189
/*
 
10190
  we assume remaining_tables doesnt contain @tab.
 
10191
*/
 
10192
 
 
10193
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10194
                                  const JOIN_TAB *tab)
 
10195
{
 
10196
  TableList *emb_sj_nest;
 
10197
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10198
  {
 
10199
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10200
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10201
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10202
    {
 
10203
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10204
    }
 
10205
  }
 
10206
}
 
10207
 
 
10208
 
 
10209
static COND *
 
10210
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
 
10211
              Item::cond_result *cond_value)
 
10212
{
 
10213
  THD *thd= join->thd;
2667
10214
 
2668
10215
  if (!conds)
2669
10216
    *cond_value= Item::COND_TRUE;
2670
10217
  else
2671
10218
  {
2672
 
    /*
 
10219
    /* 
2673
10220
      Build all multiple equality predicates and eliminate equality
2674
10221
      predicates that can be inferred from these multiple equalities.
2675
10222
      For each reference of a field included into a multiple equality
2676
10223
      that occurs in a function set a pointer to the multiple equality
2677
10224
      predicate. Substitute a constant instead of this field if the
2678
10225
      multiple equality contains a constant.
2679
 
    */
2680
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10226
    */ 
 
10227
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2681
10228
                             &join->cond_equal);
2682
10229
 
2683
10230
    /* change field = field to field = const for each found field = const */
2684
 
    vector<COND_CMP> temp;
2685
 
    propagate_cond_constants(session, temp, conds, conds);
 
10231
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2686
10232
    /*
2687
10233
      Remove all instances of item == item
2688
10234
      Remove all and-levels where CONST item != CONST item
2689
10235
    */
2690
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10236
    conds= remove_eq_conds(thd, conds, cond_value) ;
2691
10237
  }
2692
10238
  return(conds);
2693
10239
}
2694
10240
 
 
10241
 
2695
10242
/**
2696
10243
  Remove const and eq items.
2697
10244
 
2702
10249
    - COND_TRUE   : always true ( 1 = 1 )
2703
10250
    - COND_FALSE  : always false        ( 1 = 2 )
2704
10251
*/
2705
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10252
 
 
10253
COND *
 
10254
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2706
10255
{
2707
10256
  if (cond->type() == Item::COND_ITEM)
2708
10257
  {
2709
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2710
 
 
 
10258
    bool and_level= ((Item_cond*) cond)->functype()
 
10259
      == Item_func::COND_AND_FUNC;
2711
10260
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2712
10261
    Item::cond_result tmp_cond_value;
2713
 
    bool should_fix_fields= false;
 
10262
    bool should_fix_fields=0;
2714
10263
 
2715
 
    *cond_value= Item::COND_UNDEF;
 
10264
    *cond_value=Item::COND_UNDEF;
2716
10265
    Item *item;
2717
 
    while ((item= li++))
 
10266
    while ((item=li++))
2718
10267
    {
2719
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2720
 
      if (! new_item)
2721
 
              li.remove();
 
10268
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10269
      if (!new_item)
 
10270
        li.remove();
2722
10271
      else if (item != new_item)
2723
10272
      {
2724
 
        li.replace(new_item);
2725
 
        should_fix_fields= true;
 
10273
        li.replace(new_item);
 
10274
        should_fix_fields=1;
2726
10275
      }
2727
10276
      if (*cond_value == Item::COND_UNDEF)
2728
 
              *cond_value= tmp_cond_value;
2729
 
 
2730
 
      switch (tmp_cond_value) 
2731
 
      {
2732
 
        case Item::COND_OK:                     /* Not true or false */
2733
 
          if (and_level || (*cond_value == Item::COND_FALSE))
2734
 
            *cond_value= tmp_cond_value;
2735
 
          break;
2736
 
        case Item::COND_FALSE:
2737
 
          if (and_level)
2738
 
          {
2739
 
            *cond_value= tmp_cond_value;
2740
 
            return (COND *) NULL;                       /* Always false */
2741
 
          }
2742
 
          break;
2743
 
        case Item::COND_TRUE:
2744
 
          if (! and_level)
2745
 
          {
2746
 
            *cond_value= tmp_cond_value;
2747
 
            return (COND *) NULL;                       /* Always true */
2748
 
          }
2749
 
          break;
2750
 
        case Item::COND_UNDEF:                  /* Impossible */
2751
 
          break;
 
10277
        *cond_value=tmp_cond_value;
 
10278
      switch (tmp_cond_value) {
 
10279
      case Item::COND_OK:                       // Not true or false
 
10280
        if (and_level || *cond_value == Item::COND_FALSE)
 
10281
          *cond_value=tmp_cond_value;
 
10282
        break;
 
10283
      case Item::COND_FALSE:
 
10284
        if (and_level)
 
10285
        {
 
10286
          *cond_value=tmp_cond_value;
 
10287
          return (COND*) 0;                     // Always false
 
10288
        }
 
10289
        break;
 
10290
      case Item::COND_TRUE:
 
10291
        if (!and_level)
 
10292
        {
 
10293
          *cond_value= tmp_cond_value;
 
10294
          return (COND*) 0;                     // Always true
 
10295
        }
 
10296
        break;
 
10297
      case Item::COND_UNDEF:                    // Impossible
 
10298
        break; /* purecov: deadcode */
2752
10299
      }
2753
10300
    }
2754
 
 
2755
10301
    if (should_fix_fields)
2756
10302
      cond->update_used_tables();
2757
10303
 
2758
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
2759
 
      return (COND*) NULL;
2760
 
 
 
10304
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10305
        *cond_value != Item::COND_OK)
 
10306
      return (COND*) 0;
2761
10307
    if (((Item_cond*) cond)->argument_list()->elements == 1)
2762
 
    {                                           
2763
 
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
10308
    {                                           // Remove list
2764
10309
      item= ((Item_cond*) cond)->argument_list()->head();
2765
10310
      ((Item_cond*) cond)->argument_list()->empty();
2766
10311
      return item;
2767
10312
    }
2768
10313
  }
2769
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10314
  else if (cond->type() == Item::FUNC_ITEM &&
 
10315
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2770
10316
  {
2771
10317
    /*
2772
10318
      Handles this special case for some ODBC applications:
2778
10324
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
2779
10325
    */
2780
10326
 
2781
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10327
    Item_func_isnull *func=(Item_func_isnull*) cond;
2782
10328
    Item **args= func->arguments();
2783
10329
    if (args[0]->type() == Item::FIELD_ITEM)
2784
10330
    {
2785
 
      Field *field= ((Item_field*) args[0])->field;
2786
 
      if (field->flags & AUTO_INCREMENT_FLAG 
2787
 
          && ! field->getTable()->maybe_null 
2788
 
          && session->options & OPTION_AUTO_IS_NULL
2789
 
          && (
2790
 
            session->first_successful_insert_id_in_prev_stmt > 0 
2791
 
            && session->substitute_null_with_insert_id
2792
 
            )
2793
 
          )
 
10331
      Field *field=((Item_field*) args[0])->field;
 
10332
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10333
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10334
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10335
           thd->substitute_null_with_insert_id))
2794
10336
      {
2795
 
        COND *new_cond;
2796
 
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
2797
 
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
2798
 
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
2799
 
        {
2800
 
          cond= new_cond;
 
10337
        COND *new_cond;
 
10338
        if ((new_cond= new Item_func_eq(args[0],
 
10339
                                        new Item_int("last_insert_id()",
 
10340
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10341
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10342
        {
 
10343
          cond=new_cond;
2801
10344
          /*
2802
10345
            Item_func_eq can't be fixed after creation so we do not check
2803
10346
            cond->fixed, also it do not need tables so we use 0 as second
2804
10347
            argument.
2805
10348
          */
2806
 
          cond->fix_fields(session, &cond);
2807
 
        }
 
10349
          cond->fix_fields(thd, &cond);
 
10350
        }
2808
10351
        /*
2809
10352
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
2810
10353
          clear for next row
2811
10354
        */
2812
 
        session->substitute_null_with_insert_id= false;
 
10355
        thd->substitute_null_with_insert_id= false;
2813
10356
      }
2814
 
#ifdef NOTDEFINED
2815
10357
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
2816
 
      else if (
2817
 
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
2818
 
          && (field->flags & NOT_NULL_FLAG) 
2819
 
          && ! field->table->maybe_null)
 
10358
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
 
10359
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10360
                (field->flags & NOT_NULL_FLAG) &&
 
10361
               !field->table->maybe_null)
2820
10362
      {
2821
 
        COND *new_cond;
2822
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
2823
 
        {
2824
 
          cond= new_cond;
 
10363
        COND *new_cond;
 
10364
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10365
        {
 
10366
          cond=new_cond;
2825
10367
          /*
2826
10368
            Item_func_eq can't be fixed after creation so we do not check
2827
10369
            cond->fixed, also it do not need tables so we use 0 as second
2828
10370
            argument.
2829
10371
          */
2830
 
          cond->fix_fields(session, &cond);
2831
 
        }
 
10372
          cond->fix_fields(thd, &cond);
 
10373
        }
2832
10374
      }
2833
 
#endif /* NOTDEFINED */
2834
10375
    }
2835
10376
    if (cond->const_item())
2836
10377
    {
2837
10378
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2838
 
      return (COND *) NULL;
 
10379
      return (COND*) 0;
2839
10380
    }
2840
10381
  }
2841
10382
  else if (cond->const_item() && !cond->is_expensive())
2842
10383
  /*
2843
10384
    TODO:
2844
10385
    Excluding all expensive functions is too restritive we should exclude only
2845
 
    materialized IN subquery predicates because they can't yet be evaluated
2846
 
    here (they need additional initialization that is done later on).
2847
 
 
2848
 
    The proper way to exclude the subqueries would be to walk the cond tree and
2849
 
    check for materialized subqueries there.
2850
 
 
 
10386
    materialized IN because it is created later than this phase, and cannot be
 
10387
    evaluated at this point.
 
10388
    The condition should be something as (need to fix member access):
 
10389
      !(cond->type() == Item::FUNC_ITEM &&
 
10390
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
10391
        ((Item_in_optimizer*)cond)->is_expensive()))
2851
10392
  */
2852
10393
  {
2853
10394
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2854
 
    return (COND *) NULL;
 
10395
    return (COND*) 0;
2855
10396
  }
2856
10397
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
2857
 
  {                                             
2858
 
    /* boolan compare function */
 
10398
  {                                             // boolan compare function
2859
10399
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2860
10400
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2861
10401
    if (left_item->eq(right_item,1))
2862
10402
    {
2863
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
2864
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10403
      if (!left_item->maybe_null ||
 
10404
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10405
        return (COND*) 0;                       // Compare of identical items
2865
10406
    }
2866
10407
  }
2867
 
  *cond_value= Item::COND_OK;
2868
 
  return cond;                                  /* Point at next and return into recursion */
 
10408
  *cond_value=Item::COND_OK;
 
10409
  return cond;                                  // Point at next and level
2869
10410
}
2870
10411
 
2871
 
/*
 
10412
/* 
2872
10413
  Check if equality can be used in removing components of GROUP BY/DISTINCT
2873
 
 
 
10414
  
2874
10415
  SYNOPSIS
2875
10416
    test_if_equality_guarantees_uniqueness()
2876
10417
      l          the left comparison argument (a field if any)
2877
10418
      r          the right comparison argument (a const of any)
2878
 
 
2879
 
  DESCRIPTION
2880
 
    Checks if an equality predicate can be used to take away
2881
 
    DISTINCT/GROUP BY because it is known to be true for exactly one
 
10419
  
 
10420
  DESCRIPTION    
 
10421
    Checks if an equality predicate can be used to take away 
 
10422
    DISTINCT/GROUP BY because it is known to be true for exactly one 
2882
10423
    distinct value (e.g. <expr> == <const>).
2883
 
    Arguments must be of the same type because e.g.
2884
 
    <string_field> = <int_const> may match more than 1 distinct value from
2885
 
    the column.
2886
 
    We must take into consideration and the optimization done for various
 
10424
    Arguments must be of the same type because e.g. 
 
10425
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10426
    the column. 
 
10427
    We must take into consideration and the optimization done for various 
2887
10428
    string constants when compared to dates etc (see Item_int_with_ref) as
2888
10429
    well as the collation of the arguments.
2889
 
 
2890
 
  RETURN VALUE
 
10430
  
 
10431
  RETURN VALUE  
2891
10432
    true    can be used
2892
10433
    false   cannot be used
2893
10434
*/
2894
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10435
static bool
 
10436
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
2895
10437
{
2896
10438
  return r->const_item() &&
2897
10439
    /* elements must be compared as dates */
2906
10448
/**
2907
10449
  Return true if the item is a const value in all the WHERE clause.
2908
10450
*/
2909
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10451
 
 
10452
static bool
 
10453
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
2910
10454
{
2911
10455
  if (cond->type() == Item::COND_ITEM)
2912
10456
  {
2919
10463
      bool res=const_expression_in_where(item, comp_item, const_item);
2920
10464
      if (res)                                  // Is a const value
2921
10465
      {
2922
 
        if (and_level)
2923
 
          return 1;
 
10466
        if (and_level)
 
10467
          return 1;
2924
10468
      }
2925
10469
      else if (!and_level)
2926
 
        return 0;
 
10470
        return 0;
2927
10471
    }
2928
10472
    return and_level ? 0 : 1;
2929
10473
  }
2931
10475
  {                                             // boolan compare function
2932
10476
    Item_func* func= (Item_func*) cond;
2933
10477
    if (func->functype() != Item_func::EQUAL_FUNC &&
2934
 
              func->functype() != Item_func::EQ_FUNC)
 
10478
        func->functype() != Item_func::EQ_FUNC)
2935
10479
      return 0;
2936
10480
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
2937
10481
    Item *right_item= ((Item_func*) cond)->arguments()[1];
2939
10483
    {
2940
10484
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2941
10485
      {
2942
 
        if (*const_item)
2943
 
          return right_item->eq(*const_item, 1);
2944
 
        *const_item=right_item;
2945
 
        return 1;
 
10486
        if (*const_item)
 
10487
          return right_item->eq(*const_item, 1);
 
10488
        *const_item=right_item;
 
10489
        return 1;
2946
10490
      }
2947
10491
    }
2948
10492
    else if (right_item->eq(comp_item,1))
2949
10493
    {
2950
10494
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2951
10495
      {
2952
 
        if (*const_item)
2953
 
          return left_item->eq(*const_item, 1);
2954
 
        *const_item=left_item;
2955
 
        return 1;
 
10496
        if (*const_item)
 
10497
          return left_item->eq(*const_item, 1);
 
10498
        *const_item=left_item;
 
10499
        return 1;
2956
10500
      }
2957
10501
    }
2958
10502
  }
2959
10503
  return 0;
2960
10504
}
2961
10505
 
 
10506
 
2962
10507
/**
2963
10508
  @details
2964
10509
  Rows produced by a join sweep may end up in a temporary table or be sent
2970
10515
  @return
2971
10516
    end_select function to use. This function can't fail.
2972
10517
*/
2973
 
Next_select_func setup_end_select_func(Join *join)
 
10518
 
 
10519
Next_select_func setup_end_select_func(JOIN *join)
2974
10520
{
2975
10521
  Table *table= join->tmp_table;
2976
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
10522
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
2977
10523
  Next_select_func end_select;
2978
10524
 
2979
10525
  /* Set up select_end */
2980
10526
  if (table)
2981
10527
  {
2982
 
    if (table->group && tmp_tbl->sum_func_count &&
 
10528
    if (table->group && tmp_tbl->sum_func_count && 
2983
10529
        !tmp_tbl->precomputed_group_by)
2984
10530
    {
2985
 
      if (table->getShare()->sizeKeys())
 
10531
      if (table->s->keys)
2986
10532
      {
2987
 
        end_select= end_update;
 
10533
        end_select=end_update;
2988
10534
      }
2989
10535
      else
2990
10536
      {
2991
 
        end_select= end_unique_update;
 
10537
        end_select=end_unique_update;
2992
10538
      }
2993
10539
    }
2994
10540
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
2995
10541
    {
2996
 
      end_select= end_write_group;
 
10542
      end_select=end_write_group;
2997
10543
    }
2998
10544
    else
2999
10545
    {
3000
 
      end_select= end_write;
 
10546
      end_select=end_write;
3001
10547
      if (tmp_tbl->precomputed_group_by)
3002
10548
      {
3003
10549
        /*
3004
10550
          A preceding call to create_tmp_table in the case when loose
3005
10551
          index scan is used guarantees that
3006
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
10552
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3007
10553
          by functions. It is OK here to use memcpy since we copy
3008
10554
          Item_sum pointers into an array of Item pointers.
3009
10555
        */
3025
10571
  return end_select;
3026
10572
}
3027
10573
 
 
10574
 
3028
10575
/**
3029
10576
  Make a join of all tables and write it on socket or to table.
3030
10577
 
3035
10582
  @retval
3036
10583
    -1  if error should be sent
3037
10584
*/
3038
 
int do_select(Join *join, List<Item> *fields, Table *table)
 
10585
 
 
10586
static int
 
10587
do_select(JOIN *join,List<Item> *fields,Table *table)
3039
10588
{
3040
10589
  int rc= 0;
3041
10590
  enum_nested_loop_state error= NESTED_LOOP_OK;
3042
 
  JoinTable *join_tab= NULL;
3043
 
 
 
10591
  JOIN_TAB *join_tab= NULL;
 
10592
  
3044
10593
  join->tmp_table= table;                       /* Save for easy recursion */
3045
10594
  join->fields= fields;
3046
10595
 
3047
10596
  if (table)
3048
10597
  {
3049
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3050
 
    table->emptyRecord();
 
10598
    table->file->extra(HA_EXTRA_WRITE_CACHE);
 
10599
    empty_record(table);
3051
10600
    if (table->group && join->tmp_table_param.sum_func_count &&
3052
 
        table->getShare()->sizeKeys() && !table->cursor->inited)
3053
 
      table->cursor->startIndexScan(0, 0);
 
10601
        table->s->keys && !table->file->inited)
 
10602
      table->file->ha_index_init(0, 0);
3054
10603
  }
3055
10604
  /* Set up select_end */
3056
10605
  Next_select_func end_select= setup_end_select_func(join);
3071
10620
    {
3072
10621
      error= (*end_select)(join, 0, 0);
3073
10622
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3074
 
              error= (*end_select)(join, 0, 1);
 
10623
        error= (*end_select)(join, 0, 1);
3075
10624
 
3076
10625
      /*
3077
10626
        If we don't go through evaluate_join_record(), do the counting
3079
10628
        so we don't touch it here.
3080
10629
      */
3081
10630
      join->examined_rows++;
3082
 
      join->session->row_count++;
 
10631
      join->thd->row_count++;
3083
10632
      assert(join->examined_rows <= 1);
3084
10633
    }
3085
10634
    else if (join->send_row_on_empty_set())
3109
10658
    if (!table)                                 // If sending data to client
3110
10659
    {
3111
10660
      /*
3112
 
        The following will unlock all cursors if the command wasn't an
3113
 
        update command
 
10661
        The following will unlock all cursors if the command wasn't an
 
10662
        update command
3114
10663
      */
3115
10664
      join->join_free();                        // Unlock all cursors
3116
10665
      if (join->result->send_eof())
3117
 
        rc= 1;                                  // Don't send error
 
10666
        rc= 1;                                  // Don't send error
3118
10667
    }
3119
10668
  }
3120
10669
  else
3122
10671
  if (table)
3123
10672
  {
3124
10673
    int tmp, new_errno= 0;
3125
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10674
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3126
10675
    {
3127
10676
      new_errno= tmp;
3128
10677
    }
3129
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10678
    if ((tmp=table->file->ha_index_or_rnd_end()))
3130
10679
    {
3131
10680
      new_errno= tmp;
3132
10681
    }
3133
10682
    if (new_errno)
3134
 
      table->print_error(new_errno,MYF(0));
 
10683
      table->file->print_error(new_errno,MYF(0));
3135
10684
  }
3136
 
  return(join->session->is_error() ? -1 : rc);
 
10685
  return(join->thd->is_error() ? -1 : rc);
3137
10686
}
3138
10687
 
3139
 
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
 
10688
 
 
10689
enum_nested_loop_state
 
10690
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3140
10691
{
3141
10692
  enum_nested_loop_state rc;
3142
10693
 
3147
10698
      rc= sub_select(join,join_tab,end_of_records);
3148
10699
    return rc;
3149
10700
  }
3150
 
  if (join->session->getKilled())               // If aborted by user
 
10701
  if (join->thd->killed)                // If aborted by user
3151
10702
  {
3152
 
    join->session->send_kill_message();
3153
 
    return NESTED_LOOP_KILLED;
 
10703
    join->thd->send_kill_message();
 
10704
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3154
10705
  }
3155
10706
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3156
10707
  {
3157
 
    if (! join_tab->cache.store_record_in_cache())
 
10708
    if (!store_record_in_cache(&join_tab->cache))
3158
10709
      return NESTED_LOOP_OK;                     // There is more room in cache
3159
10710
    return flush_cached_records(join,join_tab,false);
3160
10711
  }
3167
10718
/**
3168
10719
  Retrieve records ends with a given beginning from the result of a join.
3169
10720
 
3170
 
    For a given partial join record consisting of records from the tables
 
10721
    For a given partial join record consisting of records from the tables 
3171
10722
    preceding the table join_tab in the execution plan, the function
3172
10723
    retrieves all matching full records from the result set and
3173
 
    send them to the result set stream.
 
10724
    send them to the result set stream. 
3174
10725
 
3175
10726
  @note
3176
10727
    The function effectively implements the  final (n-k) nested loops
3210
10761
    first row with t3.a=t1.a has been encountered.
3211
10762
    Thus, the second predicate P2 is supplied with a guarded value that are
3212
10763
    stored in the field 'found' of the first inner table for the outer join
3213
 
    (table t2). When the first row with t3.a=t1.a for the  current row
 
10764
    (table t2). When the first row with t3.a=t1.a for the  current row 
3214
10765
    of table t1  appears, the value becomes true. For now on the predicate
3215
10766
    is evaluated immediately after the row of table t2 has been read.
3216
10767
    When the first row with t3.a=t1.a has been encountered all
3218
10769
    Only when all of them are true the row is sent to the output stream.
3219
10770
    If not, the function returns to the lowest nest level that has a false
3220
10771
    attached condition.
3221
 
    The predicates from on expressions are also pushed down. If in the
 
10772
    The predicates from on expressions are also pushed down. If in the 
3222
10773
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3223
10774
    then t1.a=t2.a would be pushed down to table t2, and without any
3224
10775
    guard.
3228
10779
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3229
10780
    are checked for the composed row almost in the same way as it had
3230
10781
    been done for the first row with a match. The only difference is
3231
 
    the predicates from on expressions are not checked.
 
10782
    the predicates from on expressions are not checked. 
3232
10783
 
3233
10784
  @par
3234
10785
  @b IMPLEMENTATION
3244
10795
    and a pointer to a guarding boolean variable.
3245
10796
    When the value of the guard variable is true the value of the object
3246
10797
    is the same as the value of the predicate, otherwise it's just returns
3247
 
    true.
3248
 
    To carry out a return to a nested loop level of join table t the pointer
 
10798
    true. 
 
10799
    To carry out a return to a nested loop level of join table t the pointer 
3249
10800
    to t is remembered in the field 'return_tab' of the join structure.
3250
10801
    Consider the following query:
3251
10802
    @code
3262
10813
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3263
10814
    becomes 'activated', as well the predicate t4.a=t2.a. But
3264
10815
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3265
 
    t4.a=t2.a becomes true.
 
10816
    t4.a=t2.a becomes true. 
3266
10817
    In order not to re-evaluate the predicates that were already evaluated
3267
10818
    as attached pushed down predicates, a pointer to the the first
3268
10819
    most inner unmatched table is maintained in join_tab->first_unmatched.
3269
10820
    Thus, when the first row from t5 with t5.a=t3.a is found
3270
 
    this pointer for t5 is changed from t4 to t2.
 
10821
    this pointer for t5 is changed from t4 to t2.             
3271
10822
 
3272
10823
    @par
3273
10824
    @b STRUCTURE @b NOTES
3278
10829
  @param join      pointer to the structure providing all context info for
3279
10830
                   the query
3280
10831
  @param join_tab  the first next table of the execution plan to be retrieved
3281
 
  @param end_records  true when we need to perform final steps of retrival
 
10832
  @param end_records  true when we need to perform final steps of retrival   
3282
10833
 
3283
10834
  @return
3284
10835
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3285
10836
*/
3286
 
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
 
10837
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10838
 
 
10839
enum_nested_loop_state
 
10840
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3287
10841
{
3288
10842
  join_tab->table->null_row=0;
3289
10843
  if (end_of_records)
3291
10845
 
3292
10846
  int error;
3293
10847
  enum_nested_loop_state rc;
3294
 
  ReadRecord *info= &join_tab->read_record;
 
10848
  READ_RECORD *info= &join_tab->read_record;
 
10849
 
 
10850
  if (join_tab->flush_weedout_table)
 
10851
  {
 
10852
    do_sj_reset(join_tab->flush_weedout_table);
 
10853
  }
3295
10854
 
3296
10855
  if (join->resume_nested_loop)
3297
10856
  {
3319
10878
      /* Set first_unmatched for the last inner table of this group */
3320
10879
      join_tab->last_inner->first_unmatched= join_tab;
3321
10880
    }
3322
 
    join->session->row_count= 0;
 
10881
    join->thd->row_count= 0;
3323
10882
 
3324
10883
    error= (*join_tab->read_first_record)(join_tab);
3325
10884
    rc= evaluate_join_record(join, join_tab, error);
3326
10885
  }
3327
 
 
3328
 
  /*
3329
 
    Note: psergey has added the 2nd part of the following condition; the
 
10886
  
 
10887
  /* 
 
10888
    Note: psergey has added the 2nd part of the following condition; the 
3330
10889
    change should probably be made in 5.1, too.
3331
10890
  */
3332
10891
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3344
10903
  return rc;
3345
10904
}
3346
10905
 
3347
 
int safe_index_read(JoinTable *tab)
 
10906
 
 
10907
 
 
10908
 
 
10909
/*
 
10910
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10911
 
 
10912
  SYNPOSIS
 
10913
    do_sj_dups_weedout()
 
10914
      
 
10915
  RETURN
 
10916
    -1  Error
 
10917
    1   The row combination is a duplicate (discard it)
 
10918
    0   The row combination is not a duplicate (continue)
 
10919
*/
 
10920
 
 
10921
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
10922
{
 
10923
  int error;
 
10924
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10925
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10926
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
10927
  unsigned char *nulls_ptr= ptr;
 
10928
  
 
10929
  /* Put the the rowids tuple into table->record[0]: */
 
10930
 
 
10931
  // 1. Store the length 
 
10932
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10933
  {
 
10934
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10935
    ptr++;
 
10936
  }
 
10937
  else
 
10938
  {
 
10939
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10940
    ptr += 2;
 
10941
  }
 
10942
 
 
10943
  // 2. Zero the null bytes 
 
10944
  if (sjtbl->null_bytes)
 
10945
  {
 
10946
    memset(ptr, 0, sjtbl->null_bytes);
 
10947
    ptr += sjtbl->null_bytes; 
 
10948
  }
 
10949
 
 
10950
  // 3. Put the rowids
 
10951
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
10952
  {
 
10953
    handler *h= tab->join_tab->table->file;
 
10954
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10955
    {
 
10956
      /* It's a NULL-complemented row */
 
10957
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10958
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10959
    }
 
10960
    else
 
10961
    {
 
10962
      /* Copy the rowid value */
 
10963
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10964
        h->position(tab->join_tab->table->record[0]);
 
10965
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10966
    }
 
10967
  }
 
10968
 
 
10969
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10970
  if (error)
 
10971
  {
 
10972
    /* create_myisam_from_heap will generate error if needed */
 
10973
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
10974
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
10975
                                &sjtbl->recinfo, error, 1))
 
10976
      return -1;
 
10977
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
10978
    return 1;
 
10979
  }
 
10980
  return 0;
 
10981
}
 
10982
 
 
10983
 
 
10984
/*
 
10985
  SemiJoinDuplicateElimination: Reset the temporary table
 
10986
*/
 
10987
 
 
10988
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
10989
{
 
10990
  if (sj_tbl->tmp_table)
 
10991
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
10992
  return 0;
 
10993
}
 
10994
 
 
10995
/*
 
10996
  Process one record of the nested loop join.
 
10997
 
 
10998
    This function will evaluate parts of WHERE/ON clauses that are
 
10999
    applicable to the partial record on hand and in case of success
 
11000
    submit this record to the next level of the nested loop.
 
11001
*/
 
11002
 
 
11003
static enum_nested_loop_state
 
11004
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11005
                     int error)
 
11006
{
 
11007
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11008
  ha_rows found_records=join->found_records;
 
11009
  COND *select_cond= join_tab->select_cond;
 
11010
 
 
11011
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
11012
    return NESTED_LOOP_ERROR;
 
11013
  if (error < 0)
 
11014
    return NESTED_LOOP_NO_MORE_ROWS;
 
11015
  if (join->thd->killed)                        // Aborted by user
 
11016
  {
 
11017
    join->thd->send_kill_message();
 
11018
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11019
  }
 
11020
  if (!select_cond || select_cond->val_int())
 
11021
  {
 
11022
    /*
 
11023
      There is no select condition or the attached pushed down
 
11024
      condition is true => a match is found.
 
11025
    */
 
11026
    bool found= 1;
 
11027
    while (join_tab->first_unmatched && found)
 
11028
    {
 
11029
      /*
 
11030
        The while condition is always false if join_tab is not
 
11031
        the last inner join table of an outer join operation.
 
11032
      */
 
11033
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11034
      /*
 
11035
        Mark that a match for current outer table is found.
 
11036
        This activates push down conditional predicates attached
 
11037
        to the all inner tables of the outer join.
 
11038
      */
 
11039
      first_unmatched->found= 1;
 
11040
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11041
      {
 
11042
        if (tab->table->reginfo.not_exists_optimize)
 
11043
          return NESTED_LOOP_NO_MORE_ROWS;
 
11044
        /* Check all predicates that has just been activated. */
 
11045
        /*
 
11046
          Actually all predicates non-guarded by first_unmatched->found
 
11047
          will be re-evaluated again. It could be fixed, but, probably,
 
11048
          it's not worth doing now.
 
11049
        */
 
11050
        if (tab->select_cond && !tab->select_cond->val_int())
 
11051
        {
 
11052
          /* The condition attached to table tab is false */
 
11053
          if (tab == join_tab)
 
11054
            found= 0;
 
11055
          else
 
11056
          {
 
11057
            /*
 
11058
              Set a return point if rejected predicate is attached
 
11059
              not to the last table of the current nest level.
 
11060
            */
 
11061
            join->return_tab= tab;
 
11062
            return NESTED_LOOP_OK;
 
11063
          }
 
11064
        }
 
11065
      }
 
11066
      /*
 
11067
        Check whether join_tab is not the last inner table
 
11068
        for another embedding outer join.
 
11069
      */
 
11070
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11071
          first_unmatched->last_inner != join_tab)
 
11072
        first_unmatched= 0;
 
11073
      join_tab->first_unmatched= first_unmatched;
 
11074
    }
 
11075
 
 
11076
    JOIN_TAB *return_tab= join->return_tab;
 
11077
    join_tab->found_match= true;
 
11078
    if (join_tab->check_weed_out_table)
 
11079
    {
 
11080
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
11081
      if (res == -1)
 
11082
        return NESTED_LOOP_ERROR;
 
11083
      if (res == 1)
 
11084
        return NESTED_LOOP_OK;
 
11085
    }
 
11086
    else if (join_tab->do_firstmatch)
 
11087
    {
 
11088
      /* 
 
11089
        We should return to the join_tab->do_firstmatch after we have 
 
11090
        enumerated all the suffixes for current prefix row combination
 
11091
      */
 
11092
      return_tab= join_tab->do_firstmatch;
 
11093
    }
 
11094
 
 
11095
    /*
 
11096
      It was not just a return to lower loop level when one
 
11097
      of the newly activated predicates is evaluated as false
 
11098
      (See above join->return_tab= tab).
 
11099
    */
 
11100
    join->examined_rows++;
 
11101
    join->thd->row_count++;
 
11102
 
 
11103
    if (found)
 
11104
    {
 
11105
      enum enum_nested_loop_state rc;
 
11106
      /* A match from join_tab is found for the current partial join. */
 
11107
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11108
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11109
        return rc;
 
11110
      if (return_tab < join->return_tab)
 
11111
        join->return_tab= return_tab;
 
11112
 
 
11113
      if (join->return_tab < join_tab)
 
11114
        return NESTED_LOOP_OK;
 
11115
      /*
 
11116
        Test if this was a SELECT DISTINCT query on a table that
 
11117
        was not in the field list;  In this case we can abort if
 
11118
        we found a row, as no new rows can be added to the result.
 
11119
      */
 
11120
      if (not_used_in_distinct && found_records != join->found_records)
 
11121
        return NESTED_LOOP_NO_MORE_ROWS;
 
11122
    }
 
11123
    else
 
11124
      join_tab->read_record.file->unlock_row();
 
11125
  }
 
11126
  else
 
11127
  {
 
11128
    /*
 
11129
      The condition pushed down to the table join_tab rejects all rows
 
11130
      with the beginning coinciding with the current partial join.
 
11131
    */
 
11132
    join->examined_rows++;
 
11133
    join->thd->row_count++;
 
11134
    join_tab->read_record.file->unlock_row();
 
11135
  }
 
11136
  return NESTED_LOOP_OK;
 
11137
}
 
11138
 
 
11139
 
 
11140
/**
 
11141
 
 
11142
  @details
 
11143
    Construct a NULL complimented partial join record and feed it to the next
 
11144
    level of the nested loop. This function is used in case we have
 
11145
    an OUTER join and no matching record was found.
 
11146
*/
 
11147
 
 
11148
static enum_nested_loop_state
 
11149
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11150
{
 
11151
  /*
 
11152
    The table join_tab is the first inner table of a outer join operation
 
11153
    and no matches has been found for the current outer row.
 
11154
  */
 
11155
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11156
  /* Cache variables for faster loop */
 
11157
  COND *select_cond;
 
11158
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11159
  {
 
11160
    /* Change the the values of guard predicate variables. */
 
11161
    join_tab->found= 1;
 
11162
    join_tab->not_null_compl= 0;
 
11163
    /* The outer row is complemented by nulls for each inner tables */
 
11164
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11165
    mark_as_null_row(join_tab->table);       // For group by without error
 
11166
    select_cond= join_tab->select_cond;
 
11167
    /* Check all attached conditions for inner table rows. */
 
11168
    if (select_cond && !select_cond->val_int())
 
11169
      return NESTED_LOOP_OK;
 
11170
  }
 
11171
  join_tab--;
 
11172
  /*
 
11173
    The row complemented by nulls might be the first row
 
11174
    of embedding outer joins.
 
11175
    If so, perform the same actions as in the code
 
11176
    for the first regular outer join row above.
 
11177
  */
 
11178
  for ( ; ; )
 
11179
  {
 
11180
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11181
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11182
        first_unmatched->last_inner != join_tab)
 
11183
      first_unmatched= 0;
 
11184
    join_tab->first_unmatched= first_unmatched;
 
11185
    if (!first_unmatched)
 
11186
      break;
 
11187
    first_unmatched->found= 1;
 
11188
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11189
    {
 
11190
      if (tab->select_cond && !tab->select_cond->val_int())
 
11191
      {
 
11192
        join->return_tab= tab;
 
11193
        return NESTED_LOOP_OK;
 
11194
      }
 
11195
    }
 
11196
  }
 
11197
  /*
 
11198
    The row complemented by nulls satisfies all conditions
 
11199
    attached to inner tables.
 
11200
    Send the row complemented by nulls to be joined with the
 
11201
    remaining tables.
 
11202
  */
 
11203
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11204
}
 
11205
 
 
11206
 
 
11207
static enum_nested_loop_state
 
11208
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11209
{
 
11210
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11211
  int error;
 
11212
  READ_RECORD *info;
 
11213
 
 
11214
  join_tab->table->null_row= 0;
 
11215
  if (!join_tab->cache.records)
 
11216
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11217
  if (skip_last)
 
11218
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11219
  if (join_tab->use_quick == 2)
 
11220
  {
 
11221
    if (join_tab->select->quick)
 
11222
    {                                   /* Used quick select last. reset it */
 
11223
      delete join_tab->select->quick;
 
11224
      join_tab->select->quick=0;
 
11225
    }
 
11226
  }
 
11227
 /* read through all records */
 
11228
  if ((error=join_init_read_record(join_tab)))
 
11229
  {
 
11230
    reset_cache_write(&join_tab->cache);
 
11231
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11232
  }
 
11233
 
 
11234
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11235
  {
 
11236
    tmp->status=tmp->table->status;
 
11237
    tmp->table->status=0;
 
11238
  }
 
11239
 
 
11240
  info= &join_tab->read_record;
 
11241
  do
 
11242
  {
 
11243
    if (join->thd->killed)
 
11244
    {
 
11245
      join->thd->send_kill_message();
 
11246
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11247
    }
 
11248
    SQL_SELECT *select=join_tab->select;
 
11249
    if (rc == NESTED_LOOP_OK &&
 
11250
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11251
    {
 
11252
      uint32_t i;
 
11253
      reset_cache_read(&join_tab->cache);
 
11254
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11255
      {
 
11256
        read_cached_record(join_tab);
 
11257
        if (!select || !select->skip_record())
 
11258
        {
 
11259
          int res= 0;
 
11260
          if (!join_tab->check_weed_out_table || 
 
11261
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
11262
          {
 
11263
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11264
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11265
            {
 
11266
              reset_cache_write(&join_tab->cache);
 
11267
              return rc;
 
11268
            }
 
11269
          }
 
11270
          if (res == -1)
 
11271
            return NESTED_LOOP_ERROR;
 
11272
        }
 
11273
      }
 
11274
    }
 
11275
  } while (!(error=info->read_record(info)));
 
11276
 
 
11277
  if (skip_last)
 
11278
    read_cached_record(join_tab);               // Restore current record
 
11279
  reset_cache_write(&join_tab->cache);
 
11280
  if (error > 0)                                // Fatal error
 
11281
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11282
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11283
    tmp2->table->status=tmp2->status;
 
11284
  return NESTED_LOOP_OK;
 
11285
}
 
11286
 
 
11287
int safe_index_read(JOIN_TAB *tab)
3348
11288
{
3349
11289
  int error;
3350
11290
  Table *table= tab->table;
3351
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11291
  if ((error=table->file->index_read_map(table->record[0],
3352
11292
                                         tab->ref.key_buff,
3353
11293
                                         make_prev_keypart_map(tab->ref.key_parts),
3354
11294
                                         HA_READ_KEY_EXACT)))
3356
11296
  return 0;
3357
11297
}
3358
11298
 
3359
 
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
11299
 
 
11300
static int
 
11301
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3360
11302
{
3361
11303
  int error;
3362
11304
  Table *table=tab->table;
3363
11305
  table->const_table=1;
3364
11306
  table->null_row=0;
3365
11307
  table->status=STATUS_NO_RECORD;
3366
 
 
3367
 
  if (tab->type == AM_SYSTEM)
 
11308
  
 
11309
  if (tab->type == JT_SYSTEM)
3368
11310
  {
3369
11311
    if ((error=join_read_system(tab)))
3370
11312
    {                                           // Info for DESCRIBE
3371
11313
      tab->info="const row not found";
3372
11314
      /* Mark for EXPLAIN that the row was not found */
3373
 
      pos->setFanout(0.0);
3374
 
      pos->clearRefDependMap();
3375
 
      if (! table->maybe_null || error > 0)
3376
 
        return(error);
 
11315
      pos->records_read=0.0;
 
11316
      pos->ref_depend_map= 0;
 
11317
      if (!table->maybe_null || error > 0)
 
11318
        return(error);
3377
11319
    }
3378
11320
  }
3379
11321
  else
3380
11322
  {
3381
 
    if (! table->key_read && 
3382
 
        table->covering_keys.test(tab->ref.key) && 
3383
 
        ! table->no_keyread &&
3384
 
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
11323
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11324
        !table->no_keyread &&
 
11325
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3385
11326
    {
3386
11327
      table->key_read=1;
3387
 
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
11328
      table->file->extra(HA_EXTRA_KEYREAD);
3388
11329
      tab->index= tab->ref.key;
3389
11330
    }
3390
11331
    error=join_read_const(tab);
3391
11332
    if (table->key_read)
3392
11333
    {
3393
11334
      table->key_read=0;
3394
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
11335
      table->file->extra(HA_EXTRA_NO_KEYREAD);
3395
11336
    }
3396
11337
    if (error)
3397
11338
    {
3398
11339
      tab->info="unique row not found";
3399
11340
      /* Mark for EXPLAIN that the row was not found */
3400
 
      pos->setFanout(0.0);
3401
 
      pos->clearRefDependMap();
 
11341
      pos->records_read=0.0;
 
11342
      pos->ref_depend_map= 0;
3402
11343
      if (!table->maybe_null || error > 0)
3403
 
        return(error);
 
11344
        return(error);
3404
11345
    }
3405
11346
  }
3406
11347
  if (*tab->on_expr_ref && !table->null_row)
3407
11348
  {
3408
11349
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3409
 
      table->mark_as_null_row();
 
11350
      mark_as_null_row(table);  
3410
11351
  }
3411
11352
  if (!table->null_row)
3412
11353
    table->maybe_null=0;
3413
11354
 
3414
11355
  /* Check appearance of new constant items in Item_equal objects */
3415
 
  Join *join= tab->join;
 
11356
  JOIN *join= tab->join;
3416
11357
  if (join->conds)
3417
11358
    update_const_equal_items(join->conds, tab);
3418
11359
  TableList *tbl;
3425
11366
      embedded= embedding;
3426
11367
      if (embedded->on_expr)
3427
11368
         update_const_equal_items(embedded->on_expr, tab);
3428
 
      embedding= embedded->getEmbedding();
 
11369
      embedding= embedded->embedding;
3429
11370
    }
3430
11371
    while (embedding &&
3431
 
           embedding->getNestedJoin()->join_list.head() == embedded);
 
11372
           embedding->nested_join->join_list.head() == embedded);
3432
11373
  }
3433
11374
 
3434
11375
  return(0);
3435
11376
}
3436
11377
 
3437
 
int join_read_system(JoinTable *tab)
 
11378
 
 
11379
static int
 
11380
join_read_system(JOIN_TAB *tab)
3438
11381
{
3439
11382
  Table *table= tab->table;
3440
11383
  int error;
3441
11384
  if (table->status & STATUS_GARBAGE)           // If first read
3442
11385
  {
3443
 
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3444
 
                                           table->getShare()->getPrimaryKey())))
 
11386
    if ((error=table->file->read_first_row(table->record[0],
 
11387
                                           table->s->primary_key)))
3445
11388
    {
3446
11389
      if (error != HA_ERR_END_OF_FILE)
3447
 
        return table->report_error(error);
3448
 
      tab->table->mark_as_null_row();
3449
 
      table->emptyRecord();                     // Make empty record
 
11390
        return table->report_error(error);
 
11391
      mark_as_null_row(tab->table);
 
11392
      empty_record(table);                      // Make empty record
3450
11393
      return -1;
3451
11394
    }
3452
 
    table->storeRecord();
 
11395
    update_virtual_fields_marked_for_write(table);
 
11396
    store_record(table,record[1]);
3453
11397
  }
3454
11398
  else if (!table->status)                      // Only happens with left join
3455
 
    table->restoreRecord();                     // restore old record
 
11399
    restore_record(table,record[1]);                    // restore old record
3456
11400
  table->null_row=0;
3457
11401
  return table->status ? -1 : 0;
3458
11402
}
3459
11403
 
 
11404
 
3460
11405
/**
3461
11406
  Read a (constant) table when there is at most one matching row.
3462
11407
 
3469
11414
  @retval
3470
11415
    1   Got an error (other than row not found) during read
3471
11416
*/
3472
 
int join_read_const(JoinTable *tab)
 
11417
 
 
11418
static int
 
11419
join_read_const(JOIN_TAB *tab)
3473
11420
{
3474
11421
  int error;
3475
11422
  Table *table= tab->table;
3476
11423
  if (table->status & STATUS_GARBAGE)           // If first read
3477
11424
  {
3478
11425
    table->status= 0;
3479
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3480
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11426
    if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
 
11427
      error=HA_ERR_KEY_NOT_FOUND;
3481
11428
    else
3482
11429
    {
3483
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
11430
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
3484
11431
                                            (unsigned char*) tab->ref.key_buff,
3485
11432
                                            make_prev_keypart_map(tab->ref.key_parts),
3486
11433
                                            HA_READ_KEY_EXACT);
3488
11435
    if (error)
3489
11436
    {
3490
11437
      table->status= STATUS_NOT_FOUND;
3491
 
      tab->table->mark_as_null_row();
3492
 
      table->emptyRecord();
 
11438
      mark_as_null_row(tab->table);
 
11439
      empty_record(table);
3493
11440
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3494
 
        return table->report_error(error);
 
11441
        return table->report_error(error);
3495
11442
      return -1;
3496
11443
    }
3497
 
    table->storeRecord();
 
11444
    update_virtual_fields_marked_for_write(table);
 
11445
    store_record(table,record[1]);
3498
11446
  }
3499
11447
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3500
11448
  {
3501
11449
    table->status=0;
3502
 
    table->restoreRecord();                     // restore old record
 
11450
    restore_record(table,record[1]);                    // restore old record
3503
11451
  }
3504
11452
  table->null_row=0;
3505
11453
  return table->status ? -1 : 0;
3506
11454
}
3507
11455
 
 
11456
 
3508
11457
/*
3509
11458
  eq_ref access method implementation: "read_first" function
3510
11459
 
3511
11460
  SYNOPSIS
3512
11461
    join_read_key()
3513
 
      tab  JoinTable of the accessed table
 
11462
      tab  JOIN_TAB of the accessed table
3514
11463
 
3515
11464
  DESCRIPTION
3516
11465
    This is "read_fist" function for the "ref" access method. The difference
3518
11467
 
3519
11468
  RETURN
3520
11469
    0  - Ok
3521
 
   -1  - Row not found
 
11470
   -1  - Row not found 
3522
11471
    1  - Error
3523
11472
*/
3524
 
int join_read_key(JoinTable *tab)
 
11473
 
 
11474
static int
 
11475
join_read_key(JOIN_TAB *tab)
3525
11476
{
3526
11477
  int error;
3527
11478
  Table *table= tab->table;
3528
11479
 
3529
 
  if (!table->cursor->inited)
 
11480
  if (!table->file->inited)
3530
11481
  {
3531
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
11482
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3532
11483
  }
3533
11484
 
3534
11485
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3540
11491
      table->status=STATUS_NOT_FOUND;
3541
11492
      return -1;
3542
11493
    }
3543
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
11494
    error=table->file->index_read_map(table->record[0],
3544
11495
                                      tab->ref.key_buff,
3545
11496
                                      make_prev_keypart_map(tab->ref.key_parts),
3546
11497
                                      HA_READ_KEY_EXACT);
3551
11502
  return table->status ? -1 : 0;
3552
11503
}
3553
11504
 
 
11505
 
3554
11506
/*
3555
11507
  ref access method implementation: "read_first" function
3556
11508
 
3557
11509
  SYNOPSIS
3558
11510
    join_read_always_key()
3559
 
      tab  JoinTable of the accessed table
 
11511
      tab  JOIN_TAB of the accessed table
3560
11512
 
3561
11513
  DESCRIPTION
3562
 
    This is "read_first" function for the "ref" access method.
3563
 
 
 
11514
    This is "read_fist" function for the "ref" access method.
 
11515
   
3564
11516
    The functon must leave the index initialized when it returns.
3565
11517
    ref_or_null access implementation depends on that.
3566
11518
 
3567
11519
  RETURN
3568
11520
    0  - Ok
3569
 
   -1  - Row not found
 
11521
   -1  - Row not found 
3570
11522
    1  - Error
3571
11523
*/
3572
 
int join_read_always_key(JoinTable *tab)
 
11524
 
 
11525
static int
 
11526
join_read_always_key(JOIN_TAB *tab)
3573
11527
{
3574
11528
  int error;
3575
11529
  Table *table= tab->table;
3576
11530
 
3577
11531
  /* Initialize the index first */
3578
 
  if (!table->cursor->inited)
3579
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3580
 
 
 
11532
  if (!table->file->inited)
 
11533
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11534
 
3581
11535
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3582
11536
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3583
11537
  {
3585
11539
        return -1;
3586
11540
  }
3587
11541
 
3588
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11542
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3589
11543
    return -1;
3590
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
11544
  if ((error=table->file->index_read_map(table->record[0],
3591
11545
                                         tab->ref.key_buff,
3592
11546
                                         make_prev_keypart_map(tab->ref.key_parts),
3593
11547
                                         HA_READ_KEY_EXACT)))
3594
11548
  {
3595
11549
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3596
11550
      return table->report_error(error);
3597
 
    return -1;
 
11551
    return -1; /* purecov: inspected */
3598
11552
  }
3599
 
 
 
11553
  update_virtual_fields_marked_for_write(table);
3600
11554
  return 0;
3601
11555
}
3602
11556
 
 
11557
 
3603
11558
/**
3604
 
  This function is used when optimizing away ORDER BY in
3605
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
11559
  This function is used when optimizing away order_st BY in 
 
11560
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
3606
11561
*/
3607
 
int join_read_last_key(JoinTable *tab)
 
11562
  
 
11563
static int
 
11564
join_read_last_key(JOIN_TAB *tab)
3608
11565
{
3609
11566
  int error;
3610
11567
  Table *table= tab->table;
3611
11568
 
3612
 
  if (!table->cursor->inited)
3613
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3614
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11569
  if (!table->file->inited)
 
11570
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11571
  if (cp_buffer_from_ref(tab->join->thd, &tab->ref))
3615
11572
    return -1;
3616
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
11573
  if ((error=table->file->index_read_last_map(table->record[0],
3617
11574
                                              tab->ref.key_buff,
3618
11575
                                              make_prev_keypart_map(tab->ref.key_parts))))
3619
11576
  {
3620
11577
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3621
11578
      return table->report_error(error);
3622
 
    return -1;
 
11579
    return -1; /* purecov: inspected */
3623
11580
  }
3624
11581
  return 0;
3625
11582
}
3626
11583
 
3627
 
int join_no_more_records(ReadRecord *)
 
11584
 
 
11585
        /* ARGSUSED */
 
11586
static int
 
11587
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3628
11588
{
3629
11589
  return -1;
3630
11590
}
3631
11591
 
3632
 
int join_read_next_same_diff(ReadRecord *info)
 
11592
static int
 
11593
join_read_next_same_diff(READ_RECORD *info)
3633
11594
{
3634
11595
  Table *table= info->table;
3635
 
  JoinTable *tab=table->reginfo.join_tab;
 
11596
  JOIN_TAB *tab=table->reginfo.join_tab;
3636
11597
  if (tab->insideout_match_tab->found_match)
3637
11598
  {
3638
 
    KeyInfo *key= tab->table->key_info + tab->index;
3639
 
    do
 
11599
    KEY *key= tab->table->key_info + tab->index;
 
11600
    do 
3640
11601
    {
3641
11602
      int error;
3642
11603
      /* Save index tuple from record to the buffer */
3643
11604
      key_copy(tab->insideout_buf, info->record, key, 0);
3644
11605
 
3645
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11606
      if ((error=table->file->index_next_same(table->record[0],
3646
11607
                                              tab->ref.key_buff,
3647
11608
                                              tab->ref.key_length)))
3648
11609
      {
3651
11612
        table->status= STATUS_GARBAGE;
3652
11613
        return -1;
3653
11614
      }
3654
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11615
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3655
11616
                      tab->insideout_buf, key->key_length));
3656
11617
    tab->insideout_match_tab->found_match= 0;
3657
11618
    return 0;
3660
11621
    return join_read_next_same(info);
3661
11622
}
3662
11623
 
3663
 
int join_read_next_same(ReadRecord *info)
 
11624
static int
 
11625
join_read_next_same(READ_RECORD *info)
3664
11626
{
3665
11627
  int error;
3666
11628
  Table *table= info->table;
3667
 
  JoinTable *tab=table->reginfo.join_tab;
 
11629
  JOIN_TAB *tab=table->reginfo.join_tab;
3668
11630
 
3669
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
11631
  if ((error=table->file->index_next_same(table->record[0],
3670
11632
                                          tab->ref.key_buff,
3671
11633
                                          tab->ref.key_length)))
3672
11634
  {
3675
11637
    table->status= STATUS_GARBAGE;
3676
11638
    return -1;
3677
11639
  }
3678
 
 
 
11640
  update_virtual_fields_marked_for_write(table);
3679
11641
  return 0;
3680
11642
}
3681
11643
 
3682
 
int join_read_prev_same(ReadRecord *info)
 
11644
 
 
11645
static int
 
11646
join_read_prev_same(READ_RECORD *info)
3683
11647
{
3684
11648
  int error;
3685
11649
  Table *table= info->table;
3686
 
  JoinTable *tab=table->reginfo.join_tab;
 
11650
  JOIN_TAB *tab=table->reginfo.join_tab;
3687
11651
 
3688
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
11652
  if ((error=table->file->index_prev(table->record[0])))
3689
11653
    return table->report_error(error);
3690
11654
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3691
11655
                      tab->ref.key_length))
3693
11657
    table->status=STATUS_NOT_FOUND;
3694
11658
    error= -1;
3695
11659
  }
 
11660
  update_virtual_fields_marked_for_write(table);
3696
11661
  return error;
3697
11662
}
3698
11663
 
3699
 
int join_init_quick_read_record(JoinTable *tab)
 
11664
 
 
11665
static int
 
11666
join_init_quick_read_record(JOIN_TAB *tab)
3700
11667
{
3701
11668
  if (test_if_quick_select(tab) == -1)
3702
11669
    return -1;                                  /* No possible records */
3703
11670
  return join_init_read_record(tab);
3704
11671
}
3705
11672
 
3706
 
int init_read_record_seq(JoinTable *tab)
 
11673
 
 
11674
int rr_sequential(READ_RECORD *info);
 
11675
int init_read_record_seq(JOIN_TAB *tab)
3707
11676
{
3708
 
  tab->read_record.init_reard_record_sequential();
3709
 
 
3710
 
  if (tab->read_record.cursor->startTableScan(1))
 
11677
  tab->read_record.read_record= rr_sequential;
 
11678
  if (tab->read_record.file->ha_rnd_init(1))
3711
11679
    return 1;
3712
11680
  return (*tab->read_record.read_record)(&tab->read_record);
3713
11681
}
3714
11682
 
3715
 
int test_if_quick_select(JoinTable *tab)
 
11683
static int
 
11684
test_if_quick_select(JOIN_TAB *tab)
3716
11685
{
3717
11686
  delete tab->select->quick;
3718
 
  tab->select->quick= 0;
3719
 
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3720
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11687
  tab->select->quick=0;
 
11688
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
11689
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11690
                                        false);
3721
11691
}
3722
11692
 
3723
 
int join_init_read_record(JoinTable *tab)
 
11693
 
 
11694
static int
 
11695
join_init_read_record(JOIN_TAB *tab)
3724
11696
{
3725
11697
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3726
11698
    return 1;
3727
 
 
3728
 
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
3729
 
 
 
11699
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
11700
                   tab->select,1,1);
3730
11701
  return (*tab->read_record.read_record)(&tab->read_record);
3731
11702
}
3732
11703
 
3733
 
int join_read_first(JoinTable *tab)
 
11704
 
 
11705
static int
 
11706
join_read_first(JOIN_TAB *tab)
3734
11707
{
3735
11708
  int error;
3736
11709
  Table *table=tab->table;
3737
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11710
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3738
11711
      !table->no_keyread)
3739
11712
  {
3740
 
    table->key_read= 1;
3741
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11713
    table->key_read=1;
 
11714
    table->file->extra(HA_EXTRA_KEYREAD);
3742
11715
  }
3743
 
  tab->table->status= 0;
 
11716
  tab->table->status=0;
3744
11717
  tab->read_record.table=table;
3745
 
  tab->read_record.cursor=table->cursor;
 
11718
  tab->read_record.file=table->file;
3746
11719
  tab->read_record.index=tab->index;
3747
 
  tab->read_record.record=table->getInsertRecord();
 
11720
  tab->read_record.record=table->record[0];
3748
11721
  if (tab->insideout_match_tab)
3749
11722
  {
3750
11723
    tab->read_record.do_insideout_scan= tab;
3757
11730
    tab->read_record.do_insideout_scan= 0;
3758
11731
  }
3759
11732
 
3760
 
  if (!table->cursor->inited)
3761
 
    table->cursor->startIndexScan(tab->index, tab->sorted);
3762
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
11733
  if (!table->file->inited)
 
11734
    table->file->ha_index_init(tab->index, tab->sorted);
 
11735
  if ((error=tab->table->file->index_first(tab->table->record[0])))
3763
11736
  {
3764
11737
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3765
11738
      table->report_error(error);
3766
11739
    return -1;
3767
11740
  }
3768
 
 
 
11741
  if (not error)
 
11742
    update_virtual_fields_marked_for_write(tab->table);
3769
11743
  return 0;
3770
11744
}
3771
11745
 
3772
 
int join_read_next_different(ReadRecord *info)
 
11746
 
 
11747
static int
 
11748
join_read_next_different(READ_RECORD *info)
3773
11749
{
3774
 
  JoinTable *tab= info->do_insideout_scan;
 
11750
  JOIN_TAB *tab= info->do_insideout_scan;
3775
11751
  if (tab->insideout_match_tab->found_match)
3776
11752
  {
3777
 
    KeyInfo *key= tab->table->key_info + tab->index;
3778
 
    do
 
11753
    KEY *key= tab->table->key_info + tab->index;
 
11754
    do 
3779
11755
    {
3780
11756
      int error;
3781
11757
      /* Save index tuple from record to the buffer */
3782
11758
      key_copy(tab->insideout_buf, info->record, key, 0);
3783
11759
 
3784
 
      if ((error=info->cursor->index_next(info->record)))
 
11760
      if ((error=info->file->index_next(info->record)))
3785
11761
        return info->table->report_error(error);
3786
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11762
      if (not error)
 
11763
        update_virtual_fields_marked_for_write(tab->table);
 
11764
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3787
11765
                      tab->insideout_buf, key->key_length));
3788
11766
    tab->insideout_match_tab->found_match= 0;
3789
11767
    return 0;
3792
11770
    return join_read_next(info);
3793
11771
}
3794
11772
 
3795
 
int join_read_next(ReadRecord *info)
 
11773
 
 
11774
static int
 
11775
join_read_next(READ_RECORD *info)
3796
11776
{
3797
11777
  int error;
3798
 
  if ((error=info->cursor->index_next(info->record)))
 
11778
  if ((error=info->file->index_next(info->record)))
3799
11779
    return info->table->report_error(error);
 
11780
  if (not error)
 
11781
    update_virtual_fields_marked_for_write(info->table);
3800
11782
  return 0;
3801
11783
}
3802
11784
 
3803
 
int join_read_last(JoinTable *tab)
 
11785
 
 
11786
static int
 
11787
join_read_last(JOIN_TAB *tab)
3804
11788
{
3805
11789
  Table *table=tab->table;
3806
11790
  int error;
3807
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11791
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3808
11792
      !table->no_keyread)
3809
11793
  {
3810
11794
    table->key_read=1;
3811
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11795
    table->file->extra(HA_EXTRA_KEYREAD);
3812
11796
  }
3813
11797
  tab->table->status=0;
3814
11798
  tab->read_record.read_record=join_read_prev;
3815
11799
  tab->read_record.table=table;
3816
 
  tab->read_record.cursor=table->cursor;
 
11800
  tab->read_record.file=table->file;
3817
11801
  tab->read_record.index=tab->index;
3818
 
  tab->read_record.record=table->getInsertRecord();
3819
 
  if (!table->cursor->inited)
3820
 
    table->cursor->startIndexScan(tab->index, 1);
3821
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
 
11802
  tab->read_record.record=table->record[0];
 
11803
  if (!table->file->inited)
 
11804
    table->file->ha_index_init(tab->index, 1);
 
11805
  if ((error= tab->table->file->index_last(tab->table->record[0])))
3822
11806
    return table->report_error(error);
3823
 
 
 
11807
  if (not error)
 
11808
    update_virtual_fields_marked_for_write(tab->table);
3824
11809
  return 0;
3825
11810
}
3826
11811
 
3827
 
int join_read_prev(ReadRecord *info)
 
11812
 
 
11813
static int
 
11814
join_read_prev(READ_RECORD *info)
3828
11815
{
3829
11816
  int error;
3830
 
  if ((error= info->cursor->index_prev(info->record)))
 
11817
  if ((error= info->file->index_prev(info->record)))
3831
11818
    return info->table->report_error(error);
3832
 
 
 
11819
  if (not error)
 
11820
    update_virtual_fields_marked_for_write(info->table);
3833
11821
  return 0;
3834
11822
}
3835
11823
 
3836
11824
/**
3837
11825
  Reading of key with key reference and one part that may be NULL.
3838
11826
*/
3839
 
int join_read_always_key_or_null(JoinTable *tab)
 
11827
 
 
11828
int
 
11829
join_read_always_key_or_null(JOIN_TAB *tab)
3840
11830
{
3841
11831
  int res;
3842
11832
 
3850
11840
  return safe_index_read(tab);
3851
11841
}
3852
11842
 
3853
 
int join_read_next_same_or_null(ReadRecord *info)
 
11843
 
 
11844
int
 
11845
join_read_next_same_or_null(READ_RECORD *info)
3854
11846
{
3855
11847
  int error;
3856
11848
  if ((error= join_read_next_same(info)) >= 0)
3857
11849
    return error;
3858
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11850
  JOIN_TAB *tab= info->table->reginfo.join_tab;
3859
11851
 
3860
11852
  /* Test if we have already done a read after null key */
3861
11853
  if (*tab->ref.null_ref_key)
3864
11856
  return safe_index_read(tab);                  // then read null keys
3865
11857
}
3866
11858
 
3867
 
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
 
11859
 
 
11860
/*****************************************************************************
 
11861
  DESCRIPTION
 
11862
    Functions that end one nested loop iteration. Different functions
 
11863
    are used to support GROUP BY clause and to redirect records
 
11864
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11865
    network client.
 
11866
 
 
11867
  RETURN VALUES
 
11868
    NESTED_LOOP_OK           - the record has been successfully handled
 
11869
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11870
                               was detected
 
11871
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11872
                               the record
 
11873
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11874
                               additionally, the nested loop produced the
 
11875
                               number of rows specified in the LIMIT clause
 
11876
                               for the query
 
11877
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11878
                               additionally, there is a cursor and the nested
 
11879
                               loop algorithm produced the number of rows
 
11880
                               that is specified for current cursor fetch
 
11881
                               operation.
 
11882
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11883
*****************************************************************************/
 
11884
 
 
11885
/* ARGSUSED */
 
11886
static enum_nested_loop_state
 
11887
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11888
         bool end_of_records)
 
11889
{
 
11890
  if (!end_of_records)
 
11891
  {
 
11892
    int error;
 
11893
    if (join->having && join->having->val_int() == 0)
 
11894
      return(NESTED_LOOP_OK);               // Didn't match having
 
11895
    error=0;
 
11896
    if (join->do_send_rows)
 
11897
      error=join->result->send_data(*join->fields);
 
11898
    if (error)
 
11899
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11900
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11901
        join->do_send_rows)
 
11902
    {
 
11903
      if (join->select_options & OPTION_FOUND_ROWS)
 
11904
      {
 
11905
        JOIN_TAB *jt=join->join_tab;
 
11906
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11907
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11908
            !(jt->select && jt->select->quick) &&
 
11909
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11910
            (jt->ref.key < 0))
 
11911
        {
 
11912
          /* Join over all rows in table;  Return number of found rows */
 
11913
          Table *table=jt->table;
 
11914
 
 
11915
          join->select_options ^= OPTION_FOUND_ROWS;
 
11916
          if (table->sort.record_pointers ||
 
11917
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11918
          {
 
11919
            /* Using filesort */
 
11920
            join->send_records= table->sort.found_records;
 
11921
          }
 
11922
          else
 
11923
          {
 
11924
            table->file->info(HA_STATUS_VARIABLE);
 
11925
            join->send_records= table->file->stats.records;
 
11926
          }
 
11927
        }
 
11928
        else 
 
11929
        {
 
11930
          join->do_send_rows= 0;
 
11931
          if (join->unit->fake_select_lex)
 
11932
            join->unit->fake_select_lex->select_limit= 0;
 
11933
          return(NESTED_LOOP_OK);
 
11934
        }
 
11935
      }
 
11936
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11937
    }
 
11938
    else if (join->send_records >= join->fetch_limit)
 
11939
    {
 
11940
      /*
 
11941
        There is a server side cursor and all rows for
 
11942
        this fetch request are sent.
 
11943
      */
 
11944
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11945
    }
 
11946
  }
 
11947
 
 
11948
  return(NESTED_LOOP_OK);
 
11949
}
 
11950
 
 
11951
 
 
11952
        /* ARGSUSED */
 
11953
enum_nested_loop_state
 
11954
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11955
               bool end_of_records)
3868
11956
{
3869
11957
  int idx= -1;
3870
11958
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
3872
11960
  if (!join->first_record || end_of_records ||
3873
11961
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3874
11962
  {
3875
 
    if (join->first_record ||
 
11963
    if (join->first_record || 
3876
11964
        (end_of_records && !join->group && !join->group_optimized_away))
3877
11965
    {
3878
11966
      if (idx < (int) join->send_group_parts)
3879
11967
      {
3880
 
        int error=0;
3881
 
        {
3882
 
          if (!join->first_record)
3883
 
          {
3884
 
                  List_iterator_fast<Item> it(*join->fields);
3885
 
                  Item *item;
3886
 
            /* No matching rows for group function */
3887
 
            join->clear();
 
11968
        int error=0;
 
11969
        {
 
11970
          if (!join->first_record)
 
11971
          {
 
11972
            List_iterator_fast<Item> it(*join->fields);
 
11973
            Item *item;
 
11974
            /* No matching rows for group function */
 
11975
            join->clear();
3888
11976
 
3889
11977
            while ((item= it++))
3890
11978
              item->no_rows_in_result();
3891
 
          }
3892
 
          if (join->having && join->having->val_int() == 0)
3893
 
            error= -1;                          // Didn't satisfy having
3894
 
          else
3895
 
          {
3896
 
            if (join->do_send_rows)
3897
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
3898
 
            join->send_records++;
3899
 
          }
3900
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
3901
 
          {
3902
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
3903
 
              error= 1;
3904
 
          }
3905
 
        }
3906
 
        if (error > 0)
3907
 
          return(NESTED_LOOP_ERROR);
3908
 
        if (end_of_records)
3909
 
          return(NESTED_LOOP_OK);
3910
 
        if (join->send_records >= join->unit->select_limit_cnt &&
3911
 
            join->do_send_rows)
3912
 
        {
3913
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
3914
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
3915
 
          join->do_send_rows=0;
3916
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
11979
          }
 
11980
          if (join->having && join->having->val_int() == 0)
 
11981
            error= -1;                          // Didn't satisfy having
 
11982
          else
 
11983
          {
 
11984
            if (join->do_send_rows)
 
11985
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
11986
            join->send_records++;
 
11987
          }
 
11988
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
11989
          {
 
11990
            if (join->rollup_send_data((uint) (idx+1)))
 
11991
              error= 1;
 
11992
          }
 
11993
        }
 
11994
        if (error > 0)
 
11995
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
11996
        if (end_of_records)
 
11997
          return(NESTED_LOOP_OK);
 
11998
        if (join->send_records >= join->unit->select_limit_cnt &&
 
11999
            join->do_send_rows)
 
12000
        {
 
12001
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
12002
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
12003
          join->do_send_rows=0;
 
12004
          join->unit->select_limit_cnt = HA_POS_ERROR;
3917
12005
        }
3918
12006
        else if (join->send_records >= join->fetch_limit)
3919
12007
        {
3932
12020
    else
3933
12021
    {
3934
12022
      if (end_of_records)
3935
 
        return(NESTED_LOOP_OK);
 
12023
        return(NESTED_LOOP_OK);
3936
12024
      join->first_record=1;
3937
12025
      test_if_item_cache_changed(join->group_fields);
3938
12026
    }
3944
12032
      */
3945
12033
      copy_fields(&join->tmp_table_param);
3946
12034
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3947
 
        return(NESTED_LOOP_ERROR);
 
12035
        return(NESTED_LOOP_ERROR);
3948
12036
      return(ok_code);
3949
12037
    }
3950
12038
  }
3953
12041
  return(NESTED_LOOP_OK);
3954
12042
}
3955
12043
 
3956
 
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
 
12044
 
 
12045
        /* ARGSUSED */
 
12046
enum_nested_loop_state
 
12047
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12048
          bool end_of_records)
 
12049
{
 
12050
  Table *table=join->tmp_table;
 
12051
 
 
12052
  if (join->thd->killed)                        // Aborted by user
 
12053
  {
 
12054
    join->thd->send_kill_message();
 
12055
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12056
  }
 
12057
  if (!end_of_records)
 
12058
  {
 
12059
    copy_fields(&join->tmp_table_param);
 
12060
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12061
#ifdef TO_BE_DELETED
 
12062
    if (!table->uniques)                        // If not unique handling
 
12063
    {
 
12064
      /* Copy null values from group to row */
 
12065
      order_st   *group;
 
12066
      for (group=table->group ; group ; group=group->next)
 
12067
      {
 
12068
        Item *item= *group->item;
 
12069
        if (item->maybe_null)
 
12070
        {
 
12071
          Field *field=item->get_tmp_table_field();
 
12072
          field->ptr[-1]= (unsigned char) (field->is_null() ? 1 : 0);
 
12073
        }
 
12074
      }
 
12075
    }
 
12076
#endif
 
12077
    if (!join->having || join->having->val_int())
 
12078
    {
 
12079
      int error;
 
12080
      join->found_records++;
 
12081
      if ((error=table->file->ha_write_row(table->record[0])))
 
12082
      {
 
12083
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12084
          goto end;
 
12085
        if (create_myisam_from_heap(join->thd, table,
 
12086
                                    join->tmp_table_param.start_recinfo,
 
12087
                                    &join->tmp_table_param.recinfo,
 
12088
                                    error, 1))
 
12089
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12090
        table->s->uniques=0;                    // To ensure rows are the same
 
12091
      }
 
12092
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12093
          join->do_send_rows)
 
12094
      {
 
12095
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12096
          return(NESTED_LOOP_QUERY_LIMIT);
 
12097
        join->do_send_rows=0;
 
12098
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12099
        return(NESTED_LOOP_OK);
 
12100
      }
 
12101
    }
 
12102
  }
 
12103
end:
 
12104
  return(NESTED_LOOP_OK);
 
12105
}
 
12106
 
 
12107
/* ARGSUSED */
 
12108
/** Group by searching after group record and updating it if possible. */
 
12109
 
 
12110
static enum_nested_loop_state
 
12111
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12112
           bool end_of_records)
 
12113
{
 
12114
  Table *table=join->tmp_table;
 
12115
  order_st   *group;
 
12116
  int     error;
 
12117
 
 
12118
  if (end_of_records)
 
12119
    return(NESTED_LOOP_OK);
 
12120
  if (join->thd->killed)                        // Aborted by user
 
12121
  {
 
12122
    join->thd->send_kill_message();
 
12123
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12124
  }
 
12125
 
 
12126
  join->found_records++;
 
12127
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12128
  /* Make a key of group index */
 
12129
  for (group=table->group ; group ; group=group->next)
 
12130
  {
 
12131
    Item *item= *group->item;
 
12132
    item->save_org_in_field(group->field);
 
12133
    /* Store in the used key if the field was 0 */
 
12134
    if (item->maybe_null)
 
12135
      group->buff[-1]= (char) group->field->is_null();
 
12136
  }
 
12137
  if (!table->file->index_read_map(table->record[1],
 
12138
                                   join->tmp_table_param.group_buff,
 
12139
                                   HA_WHOLE_KEY,
 
12140
                                   HA_READ_KEY_EXACT))
 
12141
  {                                             /* Update old record */
 
12142
    restore_record(table,record[1]);
 
12143
    update_tmptable_sum_func(join->sum_funcs,table);
 
12144
    if ((error=table->file->ha_update_row(table->record[1],
 
12145
                                          table->record[0])))
 
12146
    {
 
12147
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12148
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12149
    }
 
12150
    return(NESTED_LOOP_OK);
 
12151
  }
 
12152
 
 
12153
  /*
 
12154
    Copy null bits from group key to table
 
12155
    We can't copy all data as the key may have different format
 
12156
    as the row data (for example as with VARCHAR keys)
 
12157
  */
 
12158
  KEY_PART_INFO *key_part;
 
12159
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12160
       group ;
 
12161
       group=group->next,key_part++)
 
12162
  {
 
12163
    if (key_part->null_bit)
 
12164
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12165
  }
 
12166
  init_tmptable_sum_functions(join->sum_funcs);
 
12167
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12168
  if ((error=table->file->ha_write_row(table->record[0])))
 
12169
  {
 
12170
    if (create_myisam_from_heap(join->thd, table,
 
12171
                                join->tmp_table_param.start_recinfo,
 
12172
                                &join->tmp_table_param.recinfo,
 
12173
                                error, 0))
 
12174
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12175
    /* Change method to update rows */
 
12176
    table->file->ha_index_init(0, 0);
 
12177
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12178
  }
 
12179
  join->send_records++;
 
12180
  return(NESTED_LOOP_OK);
 
12181
}
 
12182
 
 
12183
 
 
12184
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12185
 
 
12186
static enum_nested_loop_state
 
12187
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12188
                  bool end_of_records)
 
12189
{
 
12190
  Table *table=join->tmp_table;
 
12191
  int     error;
 
12192
 
 
12193
  if (end_of_records)
 
12194
    return(NESTED_LOOP_OK);
 
12195
  if (join->thd->killed)                        // Aborted by user
 
12196
  {
 
12197
    join->thd->send_kill_message();
 
12198
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12199
  }
 
12200
 
 
12201
  init_tmptable_sum_functions(join->sum_funcs);
 
12202
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12203
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12204
 
 
12205
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12206
    join->send_records++;                       // New group
 
12207
  else
 
12208
  {
 
12209
    if ((int) table->file->get_dup_key(error) < 0)
 
12210
    {
 
12211
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12212
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12213
    }
 
12214
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12215
    {
 
12216
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12217
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12218
    }
 
12219
    restore_record(table,record[1]);
 
12220
    update_tmptable_sum_func(join->sum_funcs,table);
 
12221
    if ((error=table->file->ha_update_row(table->record[1],
 
12222
                                          table->record[0])))
 
12223
    {
 
12224
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12225
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12226
    }
 
12227
  }
 
12228
  return(NESTED_LOOP_OK);
 
12229
}
 
12230
 
 
12231
 
 
12232
        /* ARGSUSED */
 
12233
enum_nested_loop_state
 
12234
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12235
                bool end_of_records)
3957
12236
{
3958
12237
  Table *table=join->tmp_table;
3959
12238
  int     idx= -1;
3960
12239
 
3961
 
  if (join->session->getKilled())
 
12240
  if (join->thd->killed)
3962
12241
  {                                             // Aborted by user
3963
 
    join->session->send_kill_message();
3964
 
    return NESTED_LOOP_KILLED;
 
12242
    join->thd->send_kill_message();
 
12243
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
3965
12244
  }
3966
12245
  if (!join->first_record || end_of_records ||
3967
12246
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3971
12250
      int send_group_parts= join->send_group_parts;
3972
12251
      if (idx < send_group_parts)
3973
12252
      {
3974
 
        if (!join->first_record)
3975
 
        {
3976
 
          /* No matching rows for group function */
3977
 
          join->clear();
3978
 
        }
3979
 
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
3980
 
        if (!join->having || join->having->val_int())
3981
 
        {
3982
 
          int error= table->cursor->insertRecord(table->getInsertRecord());
3983
 
 
3984
 
          if (error)
3985
 
          {
3986
 
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3987
 
            return NESTED_LOOP_ERROR;
3988
 
          }
 
12253
        if (!join->first_record)
 
12254
        {
 
12255
          /* No matching rows for group function */
 
12256
          join->clear();
 
12257
        }
 
12258
        copy_sum_funcs(join->sum_funcs,
 
12259
                       join->sum_funcs_end[send_group_parts]);
 
12260
        if (!join->having || join->having->val_int())
 
12261
        {
 
12262
          int error= table->file->ha_write_row(table->record[0]);
 
12263
          if (error && create_myisam_from_heap(join->thd, table,
 
12264
                                               join->tmp_table_param.start_recinfo,
 
12265
                                                &join->tmp_table_param.recinfo,
 
12266
                                               error, 0))
 
12267
            return(NESTED_LOOP_ERROR);
3989
12268
        }
3990
12269
        if (join->rollup.state != ROLLUP::STATE_NONE)
3991
 
        {
3992
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3993
 
            return NESTED_LOOP_ERROR;
3994
 
        }
3995
 
        if (end_of_records)
3996
 
          return NESTED_LOOP_OK;
 
12270
        {
 
12271
          if (join->rollup_write_data((uint) (idx+1), table))
 
12272
            return(NESTED_LOOP_ERROR);
 
12273
        }
 
12274
        if (end_of_records)
 
12275
          return(NESTED_LOOP_OK);
3997
12276
      }
3998
12277
    }
3999
12278
    else
4000
12279
    {
4001
12280
      if (end_of_records)
4002
 
        return NESTED_LOOP_OK;
 
12281
        return(NESTED_LOOP_OK);
4003
12282
      join->first_record=1;
4004
12283
      test_if_item_cache_changed(join->group_fields);
4005
12284
    }
4008
12287
      copy_fields(&join->tmp_table_param);
4009
12288
      copy_funcs(join->tmp_table_param.items_to_copy);
4010
12289
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4011
 
        return NESTED_LOOP_ERROR;
4012
 
      return NESTED_LOOP_OK;
 
12290
        return(NESTED_LOOP_ERROR);
 
12291
      return(NESTED_LOOP_OK);
4013
12292
    }
4014
12293
  }
4015
12294
  if (update_sum_func(join->sum_funcs))
4016
 
    return NESTED_LOOP_ERROR;
4017
 
  return NESTED_LOOP_OK;
 
12295
    return(NESTED_LOOP_ERROR);
 
12296
  return(NESTED_LOOP_OK);
4018
12297
}
4019
12298
 
 
12299
 
4020
12300
/*****************************************************************************
4021
12301
  Remove calculation with tables that aren't yet read. Remove also tests
4022
12302
  against fields that are read through key where the table is not a
4023
12303
  outer join table.
4024
12304
  We can't remove tests that are made against columns which are stored
4025
12305
  in sorted order.
 
12306
*****************************************************************************/
 
12307
 
 
12308
/**
4026
12309
  @return
4027
 
    1 if right_item used is a removable reference key on left_item
4028
 
    0 otherwise.
4029
 
****************************************************************************/
4030
 
bool test_if_ref(Item_field *left_item,Item *right_item)
 
12310
    1 if right_item is used removable reference key on left_item
 
12311
*/
 
12312
 
 
12313
static bool test_if_ref(Item_field *left_item,Item *right_item)
4031
12314
{
4032
12315
  Field *field=left_item->field;
4033
12316
  // No need to change const test. We also have to keep tests on LEFT JOIN
4034
 
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
 
12317
  if (!field->table->const_table && !field->table->maybe_null)
4035
12318
  {
4036
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
12319
    Item *ref_item=part_of_refkey(field->table,field);
4037
12320
    if (ref_item && ref_item->eq(right_item,1))
4038
12321
    {
4039
12322
      right_item= right_item->real_item();
4040
12323
      if (right_item->type() == Item::FIELD_ITEM)
4041
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12324
        return (field->eq_def(((Item_field *) right_item)->field));
4042
12325
      /* remove equalities injected by IN->EXISTS transformation */
4043
12326
      else if (right_item->type() == Item::CACHE_ITEM)
4044
12327
        return ((Item_cache *)right_item)->eq_def (field);
4045
12328
      if (right_item->const_item() && !(right_item->is_null()))
4046
12329
      {
4047
 
        /*
4048
 
          We can remove binary fields and numerical fields except float,
4049
 
          as float comparison isn't 100 % secure
4050
 
          We have to keep normal strings to be able to check for end spaces
 
12330
        /*
 
12331
          We can remove binary fields and numerical fields except float,
 
12332
          as float comparison isn't 100 % secure
 
12333
          We have to keep normal strings to be able to check for end spaces
4051
12334
 
4052
 
                sergefp: the above seems to be too restrictive. Counterexample:
4053
 
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
4054
 
                  insert into t100 values ('a'),('a ');
4055
 
                  explain select * from t100 where v='a';
4056
 
                The EXPLAIN shows 'using Where'. Running the query returns both
4057
 
                rows, so it seems there are no problems with endspace in the most
4058
 
                frequent case?
4059
 
        */
4060
 
        if (field->binary() &&
4061
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4062
 
            field->decimals() == 0)
4063
 
        {
4064
 
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4065
 
        }
 
12335
          sergefp: the above seems to be too restrictive. Counterexample:
 
12336
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12337
            insert into t100 values ('a'),('a ');
 
12338
            explain select * from t100 where v='a';
 
12339
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12340
          rows, so it seems there are no problems with endspace in the most
 
12341
          frequent case?
 
12342
        */
 
12343
        if (field->binary() &&
 
12344
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12345
            field->decimals() == 0)
 
12346
        {
 
12347
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12348
        }
4066
12349
      }
4067
12350
    }
4068
12351
  }
4069
 
  return 0;
 
12352
  return 0;                                     // keep test
 
12353
}
 
12354
 
 
12355
/**
 
12356
   @brief Replaces an expression destructively inside the expression tree of
 
12357
   the WHERE clase.
 
12358
 
 
12359
   @note Because of current requirements for semijoin flattening, we do not
 
12360
   need to recurse here, hence this function will only examine the top-level
 
12361
   AND conditions. (see JOIN::prepare, comment above the line 
 
12362
   'if (do_materialize)'
 
12363
   
 
12364
   @param join The top-level query.
 
12365
   @param old_cond The expression to be replaced.
 
12366
   @param new_cond The expression to be substituted.
 
12367
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
12368
   the new expression.
 
12369
   @return <code>true</code> if there was an error, <code>false</code> if
 
12370
   successful.
 
12371
*/
 
12372
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
12373
                                       Item *new_cond, bool do_fix_fields)
 
12374
{
 
12375
  if (join->conds == old_cond) {
 
12376
    join->conds= new_cond;
 
12377
    if (do_fix_fields)
 
12378
      new_cond->fix_fields(join->thd, &join->conds);
 
12379
    return false;
 
12380
  }
 
12381
  
 
12382
  if (join->conds->type() == Item::COND_ITEM) {
 
12383
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12384
    Item *item;
 
12385
    while ((item= li++))
 
12386
      if (item == old_cond) 
 
12387
      {
 
12388
        li.replace(new_cond);
 
12389
        if (do_fix_fields)
 
12390
          new_cond->fix_fields(join->thd, li.ref());
 
12391
        return false;
 
12392
      }
 
12393
  }
 
12394
 
 
12395
  return true;
4070
12396
}
4071
12397
 
4072
12398
/*
4073
12399
  Extract a condition that can be checked after reading given table
4074
 
 
 
12400
  
4075
12401
  SYNOPSIS
4076
12402
    make_cond_for_table()
4077
12403
      cond         Condition to analyze
4078
12404
      tables       Tables for which "current field values" are available
4079
 
      used_table   Table that we're extracting the condition for (may
 
12405
      used_table   Table that we're extracting the condition for (may 
4080
12406
                   also include PSEUDO_TABLE_BITS
4081
12407
 
4082
12408
  DESCRIPTION
4086
12412
 
4087
12413
    The function assumes that
4088
12414
      - Constant parts of the condition has already been checked.
4089
 
      - Condition that could be checked for tables in 'tables' has already
 
12415
      - Condition that could be checked for tables in 'tables' has already 
4090
12416
        been checked.
4091
 
 
 
12417
        
4092
12418
    The function takes into account that some parts of the condition are
4093
12419
    guaranteed to be true by employed 'ref' access methods (the code that
4094
12420
    does this is located at the end, search down for "EQ_FUNC").
4095
12421
 
4096
12422
 
4097
 
  SEE ALSO
 
12423
  SEE ALSO 
4098
12424
    make_cond_for_info_schema uses similar algorithm
4099
12425
 
4100
12426
  RETURN
4101
12427
    Extracted condition
4102
12428
*/
4103
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12429
 
 
12430
static COND *
 
12431
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12432
                    bool exclude_expensive_cond)
4104
12433
{
4105
12434
  if (used_table && !(cond->used_tables() & used_table) &&
4106
 
    /*
4107
 
      Exclude constant conditions not checked at optimization time if
4108
 
      the table we are pushing conditions to is the first one.
4109
 
      As a result, such conditions are not considered as already checked
4110
 
      and will be checked at execution time, attached to the first table.
4111
 
    */
4112
 
    !((used_table & 1) && cond->is_expensive()))
 
12435
      /*
 
12436
        Exclude constant conditions not checked at optimization time if
 
12437
        the table we are pushing conditions to is the first one.
 
12438
        As a result, such conditions are not considered as already checked
 
12439
        and will be checked at execution time, attached to the first table.
 
12440
      */
 
12441
      !((used_table & 1) && cond->is_expensive()))
4113
12442
    return (COND*) 0;                           // Already checked
4114
12443
  if (cond->type() == Item::COND_ITEM)
4115
12444
  {
4118
12447
      /* Create new top level AND item */
4119
12448
      Item_cond_and *new_cond=new Item_cond_and;
4120
12449
      if (!new_cond)
4121
 
        return (COND*) 0;
 
12450
        return (COND*) 0;                       // OOM /* purecov: inspected */
4122
12451
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4123
12452
      Item *item;
4124
12453
      while ((item=li++))
4125
12454
      {
4126
 
        Item *fix= make_cond_for_table(item,tables,used_table,
4127
 
                                            exclude_expensive_cond);
4128
 
        if (fix)
4129
 
          new_cond->argument_list()->push_back(fix);
 
12455
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12456
                                      exclude_expensive_cond);
 
12457
        if (fix)
 
12458
          new_cond->argument_list()->push_back(fix);
4130
12459
      }
4131
 
      switch (new_cond->argument_list()->elements) 
4132
 
      {
4133
 
        case 0:
4134
 
          return (COND*) 0;                     // Always true
4135
 
        case 1:
4136
 
          return new_cond->argument_list()->head();
4137
 
        default:
4138
 
          /*
4139
 
            Item_cond_and do not need fix_fields for execution, its parameters
4140
 
            are fixed or do not need fix_fields, too
4141
 
          */
4142
 
          new_cond->quick_fix_field();
4143
 
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4144
 
          return new_cond;
 
12460
      switch (new_cond->argument_list()->elements) {
 
12461
      case 0:
 
12462
        return (COND*) 0;                       // Always true
 
12463
      case 1:
 
12464
        return new_cond->argument_list()->head();
 
12465
      default:
 
12466
        /*
 
12467
          Item_cond_and do not need fix_fields for execution, its parameters
 
12468
          are fixed or do not need fix_fields, too
 
12469
        */
 
12470
        new_cond->quick_fix_field();
 
12471
        new_cond->used_tables_cache=
 
12472
          ((Item_cond_and*) cond)->used_tables_cache &
 
12473
          tables;
 
12474
        return new_cond;
4145
12475
      }
4146
12476
    }
4147
12477
    else
4148
12478
    {                                           // Or list
4149
12479
      Item_cond_or *new_cond=new Item_cond_or;
4150
12480
      if (!new_cond)
4151
 
        return (COND*) 0;
 
12481
        return (COND*) 0;                       // OOM /* purecov: inspected */
4152
12482
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4153
12483
      Item *item;
4154
12484
      while ((item=li++))
4155
12485
      {
4156
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4157
 
        if (!fix)
4158
 
          return (COND*) 0;                     // Always true
4159
 
        new_cond->argument_list()->push_back(fix);
 
12486
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12487
        if (!fix)
 
12488
          return (COND*) 0;                     // Always true
 
12489
        new_cond->argument_list()->push_back(fix);
4160
12490
      }
4161
12491
      /*
4162
 
        Item_cond_and do not need fix_fields for execution, its parameters
4163
 
        are fixed or do not need fix_fields, too
 
12492
        Item_cond_and do not need fix_fields for execution, its parameters
 
12493
        are fixed or do not need fix_fields, too
4164
12494
      */
4165
12495
      new_cond->quick_fix_field();
4166
12496
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4185
12515
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4186
12516
    return cond;                                // Not boolean op
4187
12517
 
4188
 
  /*
 
12518
  /* 
4189
12519
    Remove equalities that are guaranteed to be true by use of 'ref' access
4190
12520
    method
4191
12521
  */
4193
12523
  {
4194
12524
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4195
12525
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4196
 
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
 
12526
    if (left_item->type() == Item::FIELD_ITEM &&
 
12527
        test_if_ref((Item_field*) left_item,right_item))
4197
12528
    {
4198
12529
      cond->marker=3;                   // Checked when read
4199
12530
      return (COND*) 0;
4200
12531
    }
4201
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12532
    if (right_item->type() == Item::FIELD_ITEM &&
 
12533
        test_if_ref((Item_field*) right_item,left_item))
4202
12534
    {
4203
12535
      cond->marker=3;                   // Checked when read
4204
12536
      return (COND*) 0;
4208
12540
  return cond;
4209
12541
}
4210
12542
 
4211
 
static Item *part_of_refkey(Table *table,Field *field)
 
12543
 
 
12544
static Item *
 
12545
part_of_refkey(Table *table,Field *field)
4212
12546
{
4213
12547
  if (!table->reginfo.join_tab)
4214
12548
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4216
12550
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4217
12551
  if (ref_parts)
4218
12552
  {
4219
 
    KeyPartInfo *key_part=
 
12553
    KEY_PART_INFO *key_part=
4220
12554
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4221
12555
    uint32_t part;
4222
12556
 
4227
12561
    }
4228
12562
 
4229
12563
    for (part=0 ; part < ref_parts ; part++,key_part++)
4230
 
    {
4231
12564
      if (field->eq(key_part->field) &&
4232
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4233
 
          //If field can be NULL, we should not remove this predicate, as
4234
 
          //it may lead to non-rejection of NULL values. 
4235
 
          !(field->real_maybe_null()))
4236
 
      {
 
12565
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4237
12566
        return table->reginfo.join_tab->ref.items[part];
4238
 
      }
4239
 
    }
4240
12567
  }
4241
12568
  return (Item*) 0;
4242
12569
}
4243
12570
 
 
12571
 
4244
12572
/**
4245
12573
  Test if one can use the key to resolve order_st BY.
4246
12574
 
4261
12589
  @retval
4262
12590
    -1   Reverse key can be used
4263
12591
*/
4264
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12592
 
 
12593
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
 
12594
                                uint32_t *used_key_parts)
4265
12595
{
4266
 
  KeyPartInfo *key_part= NULL;
4267
 
  KeyPartInfo *key_part_end= NULL;
4268
 
  key_part= table->key_info[idx].key_part;
4269
 
  key_part_end= key_part + table->key_info[idx].key_parts;
 
12596
  KEY_PART_INFO *key_part,*key_part_end;
 
12597
  key_part=table->key_info[idx].key_part;
 
12598
  key_part_end=key_part+table->key_info[idx].key_parts;
4270
12599
  key_part_map const_key_parts=table->const_key_parts[idx];
4271
 
  int reverse= 0;
 
12600
  int reverse=0;
4272
12601
  bool on_primary_key= false;
4273
12602
 
4274
12603
  for (; order ; order=order->next, const_key_parts>>=1)
4278
12607
 
4279
12608
    /*
4280
12609
      Skip key parts that are constants in the WHERE clause.
4281
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
12610
      These are already skipped in the order_st BY by const_expression_in_where()
4282
12611
    */
4283
12612
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4284
 
      key_part++;
 
12613
      key_part++; 
4285
12614
 
4286
12615
    if (key_part == key_part_end)
4287
12616
    {
4288
 
      /*
 
12617
      /* 
4289
12618
        We are at the end of the key. Check if the engine has the primary
4290
12619
        key as a suffix to the secondary keys. If it has continue to check
4291
12620
        the primary key as a suffix.
4292
12621
      */
4293
12622
      if (!on_primary_key &&
4294
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4295
 
          table->getShare()->hasPrimaryKey())
 
12623
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
12624
          table->s->primary_key != MAX_KEY)
4296
12625
      {
4297
12626
        on_primary_key= true;
4298
 
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4299
 
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4300
 
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
 
12627
        key_part= table->key_info[table->s->primary_key].key_part;
 
12628
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
 
12629
        const_key_parts=table->const_key_parts[table->s->primary_key];
4301
12630
 
4302
12631
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4303
 
          key_part++;
 
12632
          key_part++; 
4304
12633
        /*
4305
12634
         The primary and secondary key parts were all const (i.e. there's
4306
12635
         one row).  The sorting doesn't matter.
4324
12653
    key_part++;
4325
12654
  }
4326
12655
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4327
 
    (uint32_t) (key_part - table->key_info[idx].key_part);
4328
 
  if (reverse == -1 && !(table->index_flags(idx) &
 
12656
    (uint) (key_part - table->key_info[idx].key_part);
 
12657
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4329
12658
                         HA_READ_PREV))
4330
12659
    reverse= 0;                                 // Index can't be used
4331
12660
  return(reverse);
4332
12661
}
4333
12662
 
 
12663
 
4334
12664
/**
4335
12665
  Test if a second key is the subkey of the first one.
4336
12666
 
4346
12676
  @retval
4347
12677
    0   no sub key
4348
12678
*/
4349
 
inline bool is_subkey(KeyPartInfo *key_part,
4350
 
                      KeyPartInfo *ref_key_part,
4351
 
                      KeyPartInfo *ref_key_part_end)
 
12679
 
 
12680
inline bool 
 
12681
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12682
          KEY_PART_INFO *ref_key_part_end)
4352
12683
{
4353
12684
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4354
 
    if (! key_part->field->eq(ref_key_part->field))
 
12685
    if (!key_part->field->eq(ref_key_part->field))
4355
12686
      return 0;
4356
12687
  return 1;
4357
12688
}
4367
12698
    - MAX_KEY                   If we can't use other key
4368
12699
    - the number of found key   Otherwise
4369
12700
*/
4370
 
static uint32_t test_if_subkey(Order *order,
4371
 
                               Table *table,
4372
 
                               uint32_t ref,
4373
 
                               uint32_t ref_key_parts,
4374
 
                               const key_map *usable_keys)
 
12701
 
 
12702
static uint
 
12703
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
 
12704
               const key_map *usable_keys)
4375
12705
{
4376
12706
  uint32_t nr;
4377
12707
  uint32_t min_length= UINT32_MAX;
4378
12708
  uint32_t best= MAX_KEY;
4379
12709
  uint32_t not_used;
4380
 
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
4381
 
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
 
12710
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
 
12711
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4382
12712
 
4383
 
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
 
12713
  for (nr= 0 ; nr < table->s->keys ; nr++)
4384
12714
  {
4385
 
    if (usable_keys->test(nr) &&
 
12715
    if (usable_keys->is_set(nr) &&
4386
12716
        table->key_info[nr].key_length < min_length &&
4387
12717
        table->key_info[nr].key_parts >= ref_key_parts &&
4388
12718
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4396
12726
  return best;
4397
12727
}
4398
12728
 
 
12729
 
4399
12730
/**
4400
12731
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4401
12732
  already known to be distinct.
4414
12745
    of the table are referenced by a list : either the select list
4415
12746
    through find_field_in_item_list or GROUP BY list through
4416
12747
    find_field_in_order_list.
4417
 
    If the above holds and the key parts cannot contain NULLs then we
 
12748
    If the above holds and the key parts cannot contain NULLs then we 
4418
12749
    can safely remove the GROUP BY/DISTINCT,
4419
12750
    as no result set can be more distinct than an unique key.
4420
12751
 
4427
12758
  @retval
4428
12759
    0                    not found.
4429
12760
*/
4430
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12761
 
 
12762
static bool
 
12763
list_contains_unique_index(Table *table,
 
12764
                          bool (*find_func) (Field *, void *), void *data)
4431
12765
{
4432
 
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
 
12766
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
4433
12767
  {
4434
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
12768
    if (keynr == table->s->primary_key ||
4435
12769
         (table->key_info[keynr].flags & HA_NOSAME))
4436
12770
    {
4437
 
      KeyInfo *keyinfo= table->key_info + keynr;
4438
 
      KeyPartInfo *key_part= NULL;
4439
 
      KeyPartInfo *key_part_end= NULL;
 
12771
      KEY *keyinfo= table->key_info + keynr;
 
12772
      KEY_PART_INFO *key_part, *key_part_end;
4440
12773
 
4441
12774
      for (key_part=keyinfo->key_part,
4442
12775
           key_part_end=key_part+ keyinfo->key_parts;
4443
12776
           key_part < key_part_end;
4444
12777
           key_part++)
4445
12778
      {
4446
 
        if (key_part->field->maybe_null() ||
4447
 
            ! find_func(key_part->field, data))
 
12779
        if (key_part->field->maybe_null() || 
 
12780
            !find_func(key_part->field, data))
4448
12781
          break;
4449
12782
      }
4450
12783
      if (key_part == key_part_end)
4454
12787
  return 0;
4455
12788
}
4456
12789
 
 
12790
 
4457
12791
/**
4458
12792
  Helper function for list_contains_unique_index.
4459
12793
  Find a field reference in a list of order_st structures.
4467
12801
  @retval
4468
12802
    0                    not found.
4469
12803
*/
4470
 
bool find_field_in_order_list (Field *field, void *data)
 
12804
 
 
12805
static bool
 
12806
find_field_in_order_list (Field *field, void *data)
4471
12807
{
4472
 
  Order *group= (Order *) data;
 
12808
  order_st *group= (order_st *) data;
4473
12809
  bool part_found= 0;
4474
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12810
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4475
12811
  {
4476
12812
    Item *item= (*tmp_group->item)->real_item();
4477
12813
    if (item->type() == Item::FIELD_ITEM &&
4484
12820
  return part_found;
4485
12821
}
4486
12822
 
 
12823
 
4487
12824
/**
4488
12825
  Helper function for list_contains_unique_index.
4489
12826
  Find a field reference in a dynamic list of Items.
4497
12834
  @retval
4498
12835
    0                    not found.
4499
12836
*/
4500
 
bool find_field_in_item_list (Field *field, void *data)
 
12837
 
 
12838
static bool
 
12839
find_field_in_item_list (Field *field, void *data)
4501
12840
{
4502
12841
  List<Item> *fields= (List<Item> *) data;
4503
12842
  bool part_found= 0;
4516
12855
  return part_found;
4517
12856
}
4518
12857
 
 
12858
 
4519
12859
/**
4520
 
  Test if we can skip the ORDER BY by using an index.
 
12860
  Test if we can skip the order_st BY by using an index.
4521
12861
 
4522
12862
  SYNOPSIS
4523
12863
    test_if_skip_sort_order()
4527
12867
      no_changes
4528
12868
      map
4529
12869
 
4530
 
  If we can use an index, the JoinTable / tab->select struct
 
12870
  If we can use an index, the JOIN_TAB / tab->select struct
4531
12871
  is changed to use the index.
4532
12872
 
4533
12873
  The index must cover all fields in <order>, or it will not be considered.
4534
12874
 
4535
12875
  @todo
4536
 
    - sergeyp: Results of all index merge selects actually are ordered
 
12876
    - sergeyp: Results of all index merge selects actually are ordered 
4537
12877
    by clustered PK values.
4538
12878
 
4539
12879
  @retval
4541
12881
  @retval
4542
12882
    1    We can use an index.
4543
12883
*/
4544
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12884
 
 
12885
static bool
 
12886
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
 
12887
                        bool no_changes, const key_map *map)
4545
12888
{
4546
12889
  int32_t ref_key;
4547
12890
  uint32_t ref_key_parts;
4548
12891
  int order_direction;
4549
12892
  uint32_t used_key_parts;
4550
12893
  Table *table=tab->table;
4551
 
  optimizer::SqlSelect *select= tab->select;
 
12894
  SQL_SELECT *select=tab->select;
4552
12895
  key_map usable_keys;
4553
 
  optimizer::QuickSelectInterface *save_quick= NULL;
 
12896
  QUICK_SELECT_I *save_quick= 0;
4554
12897
 
4555
12898
  /*
4556
12899
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
4558
12901
  */
4559
12902
  usable_keys= *map;
4560
12903
 
4561
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12904
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4562
12905
  {
4563
12906
    Item *item= (*tmp_order->item)->real_item();
4564
12907
    if (item->type() != Item::FIELD_ITEM)
4565
12908
    {
4566
 
      usable_keys.reset();
 
12909
      usable_keys.clear_all();
4567
12910
      return(0);
4568
12911
    }
4569
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4570
 
    if (usable_keys.none())
 
12912
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12913
    if (usable_keys.is_clear_all())
4571
12914
      return(0);                                        // No usable keys
4572
12915
  }
4573
12916
 
4577
12920
  {
4578
12921
    ref_key=       tab->ref.key;
4579
12922
    ref_key_parts= tab->ref.key_parts;
4580
 
    if (tab->type == AM_REF_OR_NULL)
 
12923
    if (tab->type == JT_REF_OR_NULL)
4581
12924
      return(0);
4582
12925
  }
4583
 
  else if (select && select->quick)             // Range found by optimizer/range
 
12926
  else if (select && select->quick)             // Range found by opt_range
4584
12927
  {
4585
12928
    int quick_type= select->quick->get_type();
4586
12929
    save_quick= select->quick;
4587
 
    /*
4588
 
      assume results are not ordered when index merge is used
4589
 
      TODO: sergeyp: Results of all index merge selects actually are ordered
 
12930
    /* 
 
12931
      assume results are not ordered when index merge is used 
 
12932
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4590
12933
      by clustered PK values.
4591
12934
    */
4592
 
 
4593
 
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4594
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4595
 
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
 
12935
  
 
12936
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
12937
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
12938
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4596
12939
      return(0);
4597
12940
    ref_key=       select->quick->index;
4598
12941
    ref_key_parts= select->quick->used_key_parts;
4603
12946
    /*
4604
12947
      We come here when there is a REF key.
4605
12948
    */
4606
 
    if (! usable_keys.test(ref_key))
 
12949
    if (!usable_keys.is_set(ref_key))
4607
12950
    {
4608
12951
      /*
4609
 
        We come here when ref_key is not among usable_keys
 
12952
        We come here when ref_key is not among usable_keys
4610
12953
      */
4611
12954
      uint32_t new_ref_key;
4612
12955
      /*
4613
 
        If using index only read, only consider other possible index only
4614
 
        keys
 
12956
        If using index only read, only consider other possible index only
 
12957
        keys
4615
12958
      */
4616
 
      if (table->covering_keys.test(ref_key))
4617
 
        usable_keys&= table->covering_keys;
 
12959
      if (table->covering_keys.is_set(ref_key))
 
12960
        usable_keys.intersect(table->covering_keys);
4618
12961
      if (tab->pre_idx_push_select_cond)
4619
12962
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4620
12963
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4621
12964
                                       &usable_keys)) < MAX_KEY)
4622
12965
      {
4623
 
        /* Found key that can be used to retrieve data in sorted order */
4624
 
        if (tab->ref.key >= 0)
4625
 
        {
 
12966
        /* Found key that can be used to retrieve data in sorted order */
 
12967
        if (tab->ref.key >= 0)
 
12968
        {
4626
12969
          /*
4627
 
            We'll use ref access method on key new_ref_key. In general case
 
12970
            We'll use ref access method on key new_ref_key. In general case 
4628
12971
            the index search tuple for new_ref_key will be different (e.g.
4629
12972
            when one index is defined as (part1, part2, ...) and another as
4630
 
            (part1, part2(N), ...) and the WHERE clause contains
4631
 
            "part1 = const1 AND part2=const2".
 
12973
            (part1, part2(N), ...) and the WHERE clause contains 
 
12974
            "part1 = const1 AND part2=const2". 
4632
12975
            So we build tab->ref from scratch here.
4633
12976
          */
4634
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4635
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
12977
          KEYUSE *keyuse= tab->keyuse;
 
12978
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4636
12979
            keyuse++;
4637
12980
 
4638
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
12981
          if (create_ref_for_key(tab->join, tab, keyuse, 
4639
12982
                                 tab->join->const_table_map))
4640
12983
            return(0);
4641
 
        }
4642
 
        else
4643
 
        {
 
12984
        }
 
12985
        else
 
12986
        {
4644
12987
          /*
4645
 
            The range optimizer constructed QuickRange for ref_key, and
 
12988
            The range optimizer constructed QUICK_RANGE for ref_key, and
4646
12989
            we want to use instead new_ref_key as the index. We can't
4647
12990
            just change the index of the quick select, because this may
4648
12991
            result in an incosistent QUICK_SELECT object. Below we
4650
12993
            parameres are set correctly by the range optimizer.
4651
12994
           */
4652
12995
          key_map new_ref_key_map;
4653
 
          new_ref_key_map.reset();  // Force the creation of quick select
4654
 
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
 
12996
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
12997
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4655
12998
 
4656
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
12999
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4657
13000
                                        (tab->join->select_options &
4658
13001
                                         OPTION_FOUND_ROWS) ?
4659
13002
                                        HA_POS_ERROR :
4661
13004
                                        true) <=
4662
13005
              0)
4663
13006
            return(0);
4664
 
        }
 
13007
        }
4665
13008
        ref_key= new_ref_key;
4666
13009
      }
4667
13010
    }
4668
13011
    /* Check if we get the rows in requested sorted order by using the key */
4669
 
    if (usable_keys.test(ref_key) &&
 
13012
    if (usable_keys.is_set(ref_key) &&
4670
13013
        (order_direction= test_if_order_by_key(order,table,ref_key,
4671
13014
                                               &used_key_parts)))
4672
13015
      goto check_reverse_order;
4687
13030
    int best_key= -1;
4688
13031
    bool is_best_covering= false;
4689
13032
    double fanout= 1;
4690
 
    Join *join= tab->join;
 
13033
    JOIN *join= tab->join;
4691
13034
    uint32_t tablenr= tab - join->join_tab;
4692
 
    ha_rows table_records= table->cursor->stats.records;
 
13035
    ha_rows table_records= table->file->stats.records;
4693
13036
    bool group= join->group && order == join->group_list;
4694
 
    optimizer::Position cur_pos;
4695
13037
 
4696
13038
    /*
4697
13039
      If not used with LIMIT, only use keys if the whole query can be
4700
13042
    */
4701
13043
    if (select_limit >= table_records)
4702
13044
    {
4703
 
      /*
4704
 
        filesort() and join cache are usually faster than reading in
 
13045
      /* 
 
13046
        filesort() and join cache are usually faster than reading in 
4705
13047
        index order and not using join cache
4706
13048
        */
4707
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13049
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4708
13050
        return(0);
4709
 
      keys= *table->cursor->keys_to_use_for_scanning();
4710
 
      keys|= table->covering_keys;
 
13051
      keys= *table->file->keys_to_use_for_scanning();
 
13052
      keys.merge(table->covering_keys);
4711
13053
 
4712
13054
      /*
4713
 
        We are adding here also the index specified in FORCE INDEX clause,
4714
 
        if any.
 
13055
        We are adding here also the index specified in FORCE INDEX clause, 
 
13056
        if any.
4715
13057
        This is to allow users to use index in order_st BY.
4716
13058
      */
4717
 
      if (table->force_index)
4718
 
        keys|= (group ? table->keys_in_use_for_group_by :
4719
 
                                table->keys_in_use_for_order_by);
4720
 
      keys&= usable_keys;
 
13059
      if (table->force_index) 
 
13060
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13061
                           table->keys_in_use_for_order_by);
 
13062
      keys.intersect(usable_keys);
4721
13063
    }
4722
13064
    else
4723
13065
      keys= usable_keys;
4724
13066
 
4725
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4726
 
    read_time= cur_pos.getCost();
 
13067
    read_time= join->best_positions[tablenr].read_time;
4727
13068
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4728
 
    {
4729
 
      cur_pos= join->getPosFromOptimalPlan(i);
4730
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4731
 
    }
 
13069
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4732
13070
 
4733
 
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
 
13071
    for (nr=0; nr < table->s->keys ; nr++)
4734
13072
    {
4735
13073
      int direction;
4736
 
      if (keys.test(nr) &&
 
13074
      if (keys.is_set(nr) &&
4737
13075
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4738
13076
      {
4739
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4740
 
 
4741
 
        /*
4742
 
          Don't use an index scan with ORDER BY without limit.
 
13077
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
13078
        
 
13079
        /* 
 
13080
          Don't use an index scan with order_st BY without limit.
4743
13081
          For GROUP BY without limit always use index scan
4744
 
          if there is a suitable index.
 
13082
          if there is a suitable index. 
4745
13083
          Why we hold to this asymmetry hardly can be explained
4746
13084
          rationally. It's easy to demonstrate that using
4747
13085
          temporary table + filesort could be cheaper for grouping
4748
13086
          queries too.
4749
 
        */
 
13087
        */ 
4750
13088
        if (is_covering ||
4751
 
            select_limit != HA_POS_ERROR ||
 
13089
            select_limit != HA_POS_ERROR || 
4752
13090
            (ref_key < 0 && (group || table->force_index)))
4753
 
        {
 
13091
        { 
4754
13092
          double rec_per_key;
4755
13093
          double index_scan_time;
4756
 
          KeyInfo *keyinfo= tab->table->key_info+nr;
 
13094
          KEY *keyinfo= tab->table->key_info+nr;
4757
13095
          if (select_limit == HA_POS_ERROR)
4758
13096
            select_limit= table_records;
4759
13097
          if (group)
4760
13098
          {
4761
13099
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
4762
 
            set_if_bigger(rec_per_key, 1.0);
 
13100
            set_if_bigger(rec_per_key, 1);
4763
13101
            /*
4764
13102
              With a grouping query each group containing on average
4765
13103
              rec_per_key records produces only one row that will
4766
13104
              be included into the result set.
4767
 
            */
 
13105
            */  
4768
13106
            if (select_limit > table_records/rec_per_key)
4769
13107
                select_limit= table_records;
4770
13108
            else
4771
13109
              select_limit= (ha_rows) (select_limit*rec_per_key);
4772
13110
          }
4773
 
          /*
 
13111
          /* 
4774
13112
            If tab=tk is not the last joined table tn then to get first
4775
13113
            L records from the result set we can expect to retrieve
4776
13114
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
4779
13117
            So the estimate for L/fanout(tk,tn) will be too optimistic
4780
13118
            and as result we'll choose an index scan when using ref/range
4781
13119
            access + filesort will be cheaper.
4782
 
          */
 
13120
          */
4783
13121
          select_limit= (ha_rows) (select_limit < fanout ?
4784
13122
                                   1 : select_limit/fanout);
4785
13123
          /*
4786
13124
            We assume that each of the tested indexes is not correlated
4787
13125
            with ref_key. Thus, to select first N records we have to scan
4788
 
            N/selectivity(ref_key) index entries.
 
13126
            N/selectivity(ref_key) index entries. 
4789
13127
            selectivity(ref_key) = #scanned_records/#table_records =
4790
13128
            table->quick_condition_rows/table_records.
4791
13129
            In any case we can't select more than #table_records.
4792
 
            N/(table->quick_condition_rows/table_records) > table_records
 
13130
            N/(table->quick_condition_rows/table_records) > table_records 
4793
13131
            <=> N > table->quick_condition_rows.
4794
 
          */
 
13132
          */ 
4795
13133
          if (select_limit > table->quick_condition_rows)
4796
13134
            select_limit= table_records;
4797
13135
          else
4799
13137
                                     (double) table_records /
4800
13138
                                      table->quick_condition_rows);
4801
13139
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
4802
 
          set_if_bigger(rec_per_key, 1.0);
 
13140
          set_if_bigger(rec_per_key, 1);
4803
13141
          /*
4804
13142
            Here we take into account the fact that rows are
4805
13143
            accessed in sequences rec_per_key records in each.
4806
13144
            Rows in such a sequence are supposed to be ordered
4807
13145
            by rowid/primary key. When reading the data
4808
13146
            in a sequence we'll touch not more pages than the
4809
 
            table cursor contains.
 
13147
            table file contains.
4810
13148
            TODO. Use the formula for a disk sweep sequential access
4811
 
            to calculate the cost of accessing data rows for one
 
13149
            to calculate the cost of accessing data rows for one 
4812
13150
            index entry.
4813
 
          */
 
13151
          */
4814
13152
          index_scan_time= select_limit/rec_per_key *
4815
 
                           min(rec_per_key, table->cursor->scan_time());
 
13153
                           cmin(rec_per_key, table->file->scan_time());
4816
13154
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
4817
13155
              index_scan_time < read_time)
4818
13156
          {
4819
13157
            ha_rows quick_records= table_records;
4820
13158
            if (is_best_covering && !is_covering)
4821
13159
              continue;
4822
 
            if (table->quick_keys.test(nr))
 
13160
            if (table->quick_keys.is_set(nr))
4823
13161
              quick_records= table->quick_rows[nr];
4824
13162
            if (best_key < 0 ||
4825
 
                (select_limit <= min(quick_records,best_records) ?
 
13163
                (select_limit <= cmin(quick_records,best_records) ?
4826
13164
                 keyinfo->key_parts < best_key_parts :
4827
13165
                 quick_records < best_records))
4828
13166
            {
4830
13168
              best_key_parts= keyinfo->key_parts;
4831
13169
              best_records= quick_records;
4832
13170
              is_best_covering= is_covering;
4833
 
              best_key_direction= direction;
 
13171
              best_key_direction= direction; 
4834
13172
            }
4835
 
          }
4836
 
        }
 
13173
          }   
 
13174
        }      
4837
13175
      }
4838
13176
    }
4839
13177
    if (best_key >= 0)
4840
13178
    {
4841
13179
      bool quick_created= false;
4842
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13180
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
4843
13181
      {
4844
 
        key_map test_map;
4845
 
        test_map.reset();       // Force the creation of quick select
4846
 
        test_map.set(best_key); // only best_key.
4847
 
        quick_created=
4848
 
          select->test_quick_select(join->session, test_map, 0,
 
13182
        key_map map;
 
13183
        map.clear_all();       // Force the creation of quick select
 
13184
        map.set_bit(best_key); // only best_key.
 
13185
        quick_created=         
 
13186
          select->test_quick_select(join->thd, map, 0,
4849
13187
                                    join->select_options & OPTION_FOUND_ROWS ?
4850
13188
                                    HA_POS_ERROR :
4851
13189
                                    join->unit->select_limit_cnt,
4854
13192
      if (!no_changes)
4855
13193
      {
4856
13194
        if (!quick_created)
4857
 
        {
 
13195
        {
4858
13196
          tab->index= best_key;
4859
13197
          tab->read_first_record= best_key_direction > 0 ?
4860
13198
                                  join_read_first:join_read_last;
4861
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13199
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
4862
13200
          if (select && select->quick)
4863
13201
          {
4864
13202
            delete select->quick;
4865
13203
            select->quick= 0;
4866
13204
          }
4867
 
          if (table->covering_keys.test(best_key))
 
13205
          if (table->covering_keys.is_set(best_key))
4868
13206
          {
4869
13207
            table->key_read=1;
4870
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13208
            table->file->extra(HA_EXTRA_KEYREAD);
4871
13209
          }
4872
 
          table->cursor->ha_index_or_rnd_end();
 
13210
          table->file->ha_index_or_rnd_end();
4873
13211
          if (join->select_options & SELECT_DESCRIBE)
4874
13212
          {
4875
13213
            tab->ref.key= -1;
4876
13214
            tab->ref.key_parts= 0;
4877
 
            if (select_limit < table_records)
 
13215
            if (select_limit < table_records) 
4878
13216
              tab->limit= select_limit;
4879
13217
          }
4880
13218
        }
4881
 
        else if (tab->type != AM_ALL)
 
13219
        else if (tab->type != JT_ALL)
4882
13220
        {
4883
13221
          /*
4884
13222
            We're about to use a quick access to the table.
4886
13224
            method is actually used.
4887
13225
          */
4888
13226
          assert(tab->select->quick);
4889
 
          tab->type= AM_ALL;
 
13227
          tab->type=JT_ALL;
4890
13228
          tab->use_quick=1;
4891
13229
          tab->ref.key= -1;
4892
13230
          tab->ref.key_parts=0;         // Don't use ref key.
4893
13231
          tab->read_first_record= join_init_read_record;
 
13232
          /*
 
13233
            TODO: update the number of records in join->best_positions[tablenr]
 
13234
          */
4894
13235
        }
4895
13236
      }
4896
13237
      used_key_parts= best_key_parts;
4897
13238
      order_direction= best_key_direction;
4898
13239
    }
4899
13240
    else
4900
 
      return(0);
4901
 
  }
 
13241
      return(0); 
 
13242
  } 
4902
13243
 
4903
 
check_reverse_order:
4904
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
13244
check_reverse_order:                  
 
13245
  if (order_direction == -1)            // If order_st BY ... DESC
4905
13246
  {
4906
13247
    if (select && select->quick)
4907
13248
    {
4908
13249
      /*
4909
 
        Don't reverse the sort order, if it's already done.
 
13250
        Don't reverse the sort order, if it's already done.
4910
13251
        (In some cases test_if_order_by_key() can be called multiple times
4911
13252
      */
4912
 
      if (! select->quick->reverse_sorted())
 
13253
      if (!select->quick->reverse_sorted())
4913
13254
      {
4914
 
        optimizer::QuickSelectDescending *tmp= NULL;
 
13255
        QUICK_SELECT_DESC *tmp;
4915
13256
        bool error= false;
4916
13257
        int quick_type= select->quick->get_type();
4917
 
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4918
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
4919
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4920
 
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
4921
 
        {
4922
 
          tab->limit= 0;
4923
 
          select->quick= save_quick;
4924
 
          return 0; // Use filesort
4925
 
        }
4926
 
 
4927
 
        /* ORDER BY range_key DESC */
4928
 
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
4929
 
                                                  used_key_parts, 
4930
 
                                                  &error);
4931
 
        if (! tmp || error)
4932
 
        {
4933
 
          delete tmp;
4934
 
          select->quick= save_quick;
4935
 
          tab->limit= 0;
4936
 
          return 0; // Reverse sort not supported
4937
 
        }
4938
 
        select->quick=tmp;
 
13258
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
 
13259
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
13260
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
13261
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
13262
        {
 
13263
          tab->limit= 0;
 
13264
          select->quick= save_quick;
 
13265
          return(0);                   // Use filesort
 
13266
        }
 
13267
            
 
13268
        /* order_st BY range_key DESC */
 
13269
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13270
                                    used_key_parts, &error);
 
13271
        if (!tmp || error)
 
13272
        {
 
13273
          delete tmp;
 
13274
          select->quick= save_quick;
 
13275
          tab->limit= 0;
 
13276
          return(0);            // Reverse sort not supported
 
13277
        }
 
13278
        select->quick=tmp;
4939
13279
      }
4940
13280
    }
4941
 
    else if (tab->type != AM_NEXT &&
 
13281
    else if (tab->type != JT_NEXT && 
4942
13282
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
4943
13283
    {
4944
13284
      /*
4945
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
13285
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
4946
13286
 
4947
 
        Use a traversal function that starts by reading the last row
4948
 
        with key part (A) and then traverse the index backwards.
 
13287
        Use a traversal function that starts by reading the last row
 
13288
        with key part (A) and then traverse the index backwards.
4949
13289
      */
4950
13290
      tab->read_first_record= join_read_last_key;
4951
13291
      tab->read_record.read_record= join_read_prev_same;
4953
13293
  }
4954
13294
  else if (select && select->quick)
4955
13295
    select->quick->sorted= 1;
4956
 
  return 1;
 
13296
  return(1);
4957
13297
}
4958
13298
 
 
13299
 
4959
13300
/*
4960
13301
  If not selecting by given key, create an index how records should be read
4961
13302
 
4962
13303
  SYNOPSIS
4963
13304
   create_sort_index()
4964
 
     session            Thread Cursor
 
13305
     thd                Thread handler
4965
13306
     tab                Table to sort (in join structure)
4966
13307
     order              How table should be sorted
4967
13308
     filesort_limit     Max number of rows that needs to be sorted
4968
13309
     select_limit       Max number of rows in final output
4969
13310
                        Used to decide if we should use index or not
4970
13311
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
4971
 
                        Used to decide if we should use index or not
 
13312
                        Used to decide if we should use index or not     
4972
13313
 
4973
13314
 
4974
13315
  IMPLEMENTATION
4975
13316
   - If there is an index that can be used, 'tab' is modified to use
4976
13317
     this index.
4977
 
   - If no index, create with filesort() an index cursor that can be used to
 
13318
   - If no index, create with filesort() an index file that can be used to
4978
13319
     retrieve rows in order (should be done with 'read_record').
4979
13320
     The sorted data is stored in tab->table and will be freed when calling
4980
 
     tab->table->free_io_cache().
 
13321
     free_io_cache(tab->table).
4981
13322
 
4982
13323
  RETURN VALUES
4983
13324
    0           ok
4984
13325
    -1          Some fatal error
4985
13326
    1           No records
4986
13327
*/
4987
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13328
 
 
13329
static int
 
13330
create_sort_index(THD *thd, JOIN *join, order_st *order,
 
13331
                  ha_rows filesort_limit, ha_rows select_limit,
 
13332
                  bool is_order_by)
4988
13333
{
4989
13334
  uint32_t length= 0;
4990
13335
  ha_rows examined_rows;
4991
13336
  Table *table;
4992
 
  optimizer::SqlSelect *select= NULL;
4993
 
  JoinTable *tab;
 
13337
  SQL_SELECT *select;
 
13338
  JOIN_TAB *tab;
4994
13339
 
4995
13340
  if (join->tables == join->const_tables)
4996
13341
    return(0);                          // One row, no need to sort
5004
13349
    is going to be used as it is applied now only for one table queries
5005
13350
    with covering indexes.
5006
13351
  */
5007
 
  if ((order != join->group_list ||
 
13352
  if ((order != join->group_list || 
5008
13353
       !(join->select_options & SELECT_BIG_RESULT) ||
5009
 
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
5010
 
      test_if_skip_sort_order(tab,order,select_limit,0,
 
13354
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
 
13355
      test_if_skip_sort_order(tab,order,select_limit,0, 
5011
13356
                              is_order_by ?  &table->keys_in_use_for_order_by :
5012
13357
                              &table->keys_in_use_for_group_by))
5013
13358
    return(0);
5014
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
13359
  for (order_st *ord= join->order; ord; ord= ord->next)
5015
13360
    length++;
5016
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
5017
 
  {
5018
 
    return(-1);
5019
 
  }
 
13361
  if (!(join->sortorder= 
 
13362
        make_unireg_sortorder(order, &length, join->sortorder)))
 
13363
    goto err;                           /* purecov: inspected */
5020
13364
 
5021
 
  table->sort.io_cache= new internal::IO_CACHE;
 
13365
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
13366
                                             MYF(MY_WME | MY_ZEROFILL));
5022
13367
  table->status=0;                              // May be wrong if quick_select
5023
13368
 
5024
13369
  // If table has a range, move it to select
5028
13373
    {
5029
13374
      select->quick=tab->quick;
5030
13375
      tab->quick=0;
5031
 
      /*
 
13376
      /* 
5032
13377
        We can only use 'Only index' if quick key is same as ref_key
5033
13378
        and in index_merge 'Only index' cannot be used
5034
13379
      */
5035
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
13380
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
5036
13381
      {
5037
 
        table->key_read=0;
5038
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13382
        table->key_read=0;
 
13383
        table->file->extra(HA_EXTRA_NO_KEYREAD);
5039
13384
      }
5040
13385
    }
5041
13386
    else
5042
13387
    {
5043
13388
      /*
5044
 
        We have a ref on a const;  Change this to a range that filesort
5045
 
        can use.
5046
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
5047
 
        field, quick will contain an empty record set.
 
13389
        We have a ref on a const;  Change this to a range that filesort
 
13390
        can use.
 
13391
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13392
        field, quick will contain an empty record set.
5048
13393
      */
5049
 
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session, 
5050
 
                                                                 table, 
5051
 
                                                                 &tab->ref,
5052
 
                                                                 tab->found_records))))
5053
 
      {
5054
 
        return(-1);
5055
 
      }
 
13394
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
 
13395
                                                     tab->found_records))))
 
13396
        goto err;
5056
13397
    }
5057
13398
  }
5058
13399
 
5059
 
  if (table->getShare()->getType())
5060
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
13400
  /* Fill schema tables with data before filesort if it's necessary */
 
13401
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13402
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13403
    goto err;
5061
13404
 
5062
 
  FileSort filesort(*session);
5063
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5064
 
                                         select, filesort_limit, 0,
5065
 
                                         examined_rows);
 
13405
  if (table->s->tmp_table)
 
13406
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13407
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
13408
                                     select, filesort_limit, 0,
 
13409
                                     &examined_rows);
5066
13410
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5067
13411
  if (select)
5068
13412
  {
5072
13416
  tab->select_cond=0;
5073
13417
  tab->last_inner= 0;
5074
13418
  tab->first_unmatched= 0;
5075
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13419
  tab->type=JT_ALL;                             // Read with normal read_record
5076
13420
  tab->read_first_record= join_init_read_record;
5077
13421
  tab->join->examined_rows+=examined_rows;
5078
13422
  if (table->key_read)                          // Restore if we used indexes
5079
13423
  {
5080
13424
    table->key_read=0;
5081
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13425
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5082
13426
  }
5083
 
 
5084
13427
  return(table->sort.found_records == HA_POS_ERROR);
5085
 
}
5086
 
 
5087
 
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5088
 
{
5089
 
  Cursor *cursor=table->cursor;
 
13428
err:
 
13429
  return(-1);
 
13430
}
 
13431
 
 
13432
static bool copy_blobs(Field **ptr)
 
13433
{
 
13434
  for (; *ptr ; ptr++)
 
13435
  {
 
13436
    if ((*ptr)->flags & BLOB_FLAG)
 
13437
      if (((Field_blob *) (*ptr))->copy())
 
13438
        return 1;                               // Error
 
13439
  }
 
13440
  return 0;
 
13441
}
 
13442
 
 
13443
static void free_blobs(Field **ptr)
 
13444
{
 
13445
  for (; *ptr ; ptr++)
 
13446
  {
 
13447
    if ((*ptr)->flags & BLOB_FLAG)
 
13448
      ((Field_blob *) (*ptr))->free();
 
13449
  }
 
13450
}
 
13451
 
 
13452
 
 
13453
static int
 
13454
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
13455
{
 
13456
  int error;
 
13457
  ulong reclength,offset;
 
13458
  uint32_t field_count;
 
13459
  THD *thd= join->thd;
 
13460
 
 
13461
  entry->reginfo.lock_type=TL_WRITE;
 
13462
 
 
13463
  /* Calculate how many saved fields there is in list */
 
13464
  field_count=0;
 
13465
  List_iterator<Item> it(fields);
 
13466
  Item *item;
 
13467
  while ((item=it++))
 
13468
  {
 
13469
    if (item->get_tmp_table_field() && ! item->const_item())
 
13470
      field_count++;
 
13471
  }
 
13472
 
 
13473
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
13474
  {                    // only const items with no OPTION_FOUND_ROWS
 
13475
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13476
    return(0);
 
13477
  }
 
13478
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13479
  offset= (field_count ? 
 
13480
           entry->field[entry->s->fields - field_count]->
 
13481
           offset(entry->record[0]) : 0);
 
13482
  reclength=entry->s->reclength-offset;
 
13483
 
 
13484
  free_io_cache(entry);                         // Safety
 
13485
  entry->file->info(HA_STATUS_VARIABLE);
 
13486
  if (entry->s->db_type() == heap_hton ||
 
13487
      (!entry->s->blob_fields &&
 
13488
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13489
        thd->variables.sortbuff_size)))
 
13490
    error=remove_dup_with_hash_index(join->thd, entry,
 
13491
                                     field_count, first_field,
 
13492
                                     reclength, having);
 
13493
  else
 
13494
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
13495
                                  having);
 
13496
 
 
13497
  free_blobs(first_field);
 
13498
  return(error);
 
13499
}
 
13500
 
 
13501
 
 
13502
static int remove_dup_with_compare(THD *thd, Table *table, Field **first_field,
 
13503
                                   ulong offset, Item *having)
 
13504
{
 
13505
  handler *file=table->file;
5090
13506
  char *org_record,*new_record;
5091
13507
  unsigned char *record;
5092
13508
  int error;
5093
 
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5094
 
 
5095
 
  org_record=(char*) (record=table->getInsertRecord())+offset;
5096
 
  new_record=(char*) table->getUpdateRecord()+offset;
5097
 
 
5098
 
  cursor->startTableScan(1);
5099
 
  error=cursor->rnd_next(record);
 
13509
  ulong reclength= table->s->reclength-offset;
 
13510
 
 
13511
  org_record=(char*) (record=table->record[0])+offset;
 
13512
  new_record=(char*) table->record[1]+offset;
 
13513
 
 
13514
  file->ha_rnd_init(1);
 
13515
  error=file->rnd_next(record);
5100
13516
  for (;;)
5101
13517
  {
5102
 
    if (session->getKilled())
 
13518
    if (thd->killed)
5103
13519
    {
5104
 
      session->send_kill_message();
 
13520
      thd->send_kill_message();
5105
13521
      error=0;
5106
13522
      goto err;
5107
13523
    }
5108
13524
    if (error)
5109
13525
    {
5110
13526
      if (error == HA_ERR_RECORD_DELETED)
5111
 
        continue;
 
13527
        continue;
5112
13528
      if (error == HA_ERR_END_OF_FILE)
5113
 
        break;
 
13529
        break;
5114
13530
      goto err;
5115
13531
    }
5116
13532
    if (having && !having->val_int())
5117
13533
    {
5118
 
      if ((error=cursor->deleteRecord(record)))
5119
 
        goto err;
5120
 
      error=cursor->rnd_next(record);
 
13534
      if ((error=file->ha_delete_row(record)))
 
13535
        goto err;
 
13536
      error=file->rnd_next(record);
5121
13537
      continue;
5122
13538
    }
5123
13539
    if (copy_blobs(first_field))
5128
13544
    }
5129
13545
    memcpy(new_record,org_record,reclength);
5130
13546
 
5131
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13547
    /* Read through rest of file and mark duplicated rows deleted */
5132
13548
    bool found=0;
5133
13549
    for (;;)
5134
13550
    {
5135
 
      if ((error=cursor->rnd_next(record)))
 
13551
      if ((error=file->rnd_next(record)))
5136
13552
      {
5137
 
        if (error == HA_ERR_RECORD_DELETED)
5138
 
          continue;
5139
 
        if (error == HA_ERR_END_OF_FILE)
5140
 
          break;
5141
 
        goto err;
 
13553
        if (error == HA_ERR_RECORD_DELETED)
 
13554
          continue;
 
13555
        if (error == HA_ERR_END_OF_FILE)
 
13556
          break;
 
13557
        goto err;
5142
13558
      }
5143
13559
      if (table->compare_record(first_field) == 0)
5144
13560
      {
5145
 
        if ((error=cursor->deleteRecord(record)))
5146
 
          goto err;
 
13561
        if ((error=file->ha_delete_row(record)))
 
13562
          goto err;
5147
13563
      }
5148
13564
      else if (!found)
5149
13565
      {
5150
 
        found= 1;
5151
 
        cursor->position(record);       // Remember position
 
13566
        found=1;
 
13567
        file->position(record); // Remember position
5152
13568
      }
5153
13569
    }
5154
13570
    if (!found)
5155
 
      break;                                    // End of cursor
5156
 
    /* Move current position to the next row */
5157
 
    error= cursor->rnd_pos(record, cursor->ref);
 
13571
      break;                                    // End of file
 
13572
    /* Restart search on next row */
 
13573
    error=file->restart_rnd_next(record,file->ref);
5158
13574
  }
5159
13575
 
5160
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13576
  file->extra(HA_EXTRA_NO_CACHE);
5161
13577
  return(0);
5162
13578
err:
5163
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13579
  file->extra(HA_EXTRA_NO_CACHE);
5164
13580
  if (error)
5165
 
    table->print_error(error,MYF(0));
 
13581
    file->print_error(error,MYF(0));
5166
13582
  return(1);
5167
13583
}
5168
13584
 
 
13585
 
5169
13586
/**
5170
13587
  Generate a hash index for each row to quickly find duplicate rows.
5171
13588
 
5172
13589
  @note
5173
13590
    Note that this will not work on tables with blobs!
5174
13591
*/
5175
 
int remove_dup_with_hash_index(Session *session, 
5176
 
                               Table *table,
5177
 
                               uint32_t field_count,
5178
 
                               Field **first_field,
5179
 
                               uint32_t key_length,
5180
 
                               Item *having)
 
13592
 
 
13593
static int remove_dup_with_hash_index(THD *thd, Table *table,
 
13594
                                      uint32_t field_count,
 
13595
                                      Field **first_field,
 
13596
                                      ulong key_length,
 
13597
                                      Item *having)
5181
13598
{
5182
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
13599
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
5183
13600
  int error;
5184
 
  Cursor *cursor= table->cursor;
5185
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5186
 
  uint32_t *field_length;
 
13601
  handler *file= table->file;
 
13602
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
13603
  uint32_t *field_lengths,*field_length;
5187
13604
  HASH hash;
5188
 
  std::vector<unsigned char> key_buffer;
5189
 
  std::vector<uint32_t> field_lengths;
5190
13605
 
5191
 
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5192
 
  field_lengths.resize(field_count);
 
13606
  if (!my_multi_malloc(MYF(MY_WME),
 
13607
                       &key_buffer,
 
13608
                       (uint) ((key_length + extra_length) *
 
13609
                               (long) file->stats.records),
 
13610
                       &field_lengths,
 
13611
                       (uint) (field_count*sizeof(*field_lengths)),
 
13612
                       NULL))
 
13613
    return(1);
5193
13614
 
5194
13615
  {
5195
13616
    Field **ptr;
5196
 
    uint32_t total_length= 0;
5197
 
 
5198
 
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
 
13617
    ulong total_length= 0;
 
13618
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5199
13619
    {
5200
13620
      uint32_t length= (*ptr)->sort_length();
5201
13621
      (*field_length++)= length;
5206
13626
    extra_length= ALIGN_SIZE(key_length)-key_length;
5207
13627
  }
5208
13628
 
5209
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13629
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5210
13630
                key_length, (hash_get_key) 0, 0, 0))
5211
13631
  {
 
13632
    free((char*) key_buffer);
5212
13633
    return(1);
5213
13634
  }
5214
13635
 
5215
 
  cursor->startTableScan(1);
5216
 
  key_pos= &key_buffer[0];
 
13636
  file->ha_rnd_init(1);
 
13637
  key_pos=key_buffer;
5217
13638
  for (;;)
5218
13639
  {
5219
13640
    unsigned char *org_key_pos;
5220
 
    if (session->getKilled())
 
13641
    if (thd->killed)
5221
13642
    {
5222
 
      session->send_kill_message();
 
13643
      thd->send_kill_message();
5223
13644
      error=0;
5224
13645
      goto err;
5225
13646
    }
5226
 
    if ((error=cursor->rnd_next(record)))
 
13647
    if ((error=file->rnd_next(record)))
5227
13648
    {
5228
13649
      if (error == HA_ERR_RECORD_DELETED)
5229
 
        continue;
 
13650
        continue;
5230
13651
      if (error == HA_ERR_END_OF_FILE)
5231
 
        break;
 
13652
        break;
5232
13653
      goto err;
5233
13654
    }
5234
13655
    if (having && !having->val_int())
5235
13656
    {
5236
 
      if ((error=cursor->deleteRecord(record)))
5237
 
        goto err;
 
13657
      if ((error=file->ha_delete_row(record)))
 
13658
        goto err;
5238
13659
      continue;
5239
13660
    }
5240
13661
 
5241
13662
    /* copy fields to key buffer */
5242
13663
    org_key_pos= key_pos;
5243
 
    field_length= &field_lengths[0];
 
13664
    field_length=field_lengths;
5244
13665
    for (Field **ptr= first_field ; *ptr ; ptr++)
5245
13666
    {
5246
13667
      (*ptr)->sort_string(key_pos,*field_length);
5250
13671
    if (hash_search(&hash, org_key_pos, key_length))
5251
13672
    {
5252
13673
      /* Duplicated found ; Remove the row */
5253
 
      if ((error=cursor->deleteRecord(record)))
5254
 
        goto err;
 
13674
      if ((error=file->ha_delete_row(record)))
 
13675
        goto err;
5255
13676
    }
5256
13677
    else
5257
13678
      (void) my_hash_insert(&hash, org_key_pos);
5258
13679
    key_pos+=extra_length;
5259
13680
  }
 
13681
  free((char*) key_buffer);
5260
13682
  hash_free(&hash);
5261
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5262
 
  (void) cursor->endTableScan();
 
13683
  file->extra(HA_EXTRA_NO_CACHE);
 
13684
  (void) file->ha_rnd_end();
5263
13685
  return(0);
5264
13686
 
5265
13687
err:
 
13688
  free((char*) key_buffer);
5266
13689
  hash_free(&hash);
5267
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5268
 
  (void) cursor->endTableScan();
 
13690
  file->extra(HA_EXTRA_NO_CACHE);
 
13691
  (void) file->ha_rnd_end();
5269
13692
  if (error)
5270
 
    table->print_error(error,MYF(0));
 
13693
    file->print_error(error,MYF(0));
5271
13694
  return(1);
5272
13695
}
5273
13696
 
5274
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
13697
 
 
13698
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
 
13699
                                  SORT_FIELD *sortorder)
5275
13700
{
5276
13701
  uint32_t count;
5277
 
  SortField *sort,*pos;
 
13702
  SORT_FIELD *sort,*pos;
5278
13703
 
5279
13704
  count=0;
5280
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
13705
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5281
13706
    count++;
5282
13707
  if (!sortorder)
5283
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5284
 
                                       (max(count, *length) + 1));
 
13708
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
13709
                                       (cmax(count, *length) + 1));
5285
13710
  pos= sort= sortorder;
5286
13711
 
5287
13712
  if (!pos)
5307
13732
  return(sort);
5308
13733
}
5309
13734
 
 
13735
 
 
13736
/*****************************************************************************
 
13737
  Fill join cache with packed records
 
13738
  Records are stored in tab->cache.buffer and last record in
 
13739
  last record is stored with pointers to blobs to support very big
 
13740
  records
 
13741
******************************************************************************/
 
13742
 
 
13743
static int
 
13744
join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count)
 
13745
{
 
13746
  register unsigned int i;
 
13747
  unsigned int length, blobs;
 
13748
  size_t size;
 
13749
  CACHE_FIELD *copy,**blob_ptr;
 
13750
  JOIN_CACHE  *cache;
 
13751
  JOIN_TAB *join_tab;
 
13752
 
 
13753
  cache= &tables[table_count].cache;
 
13754
  cache->fields=blobs=0;
 
13755
 
 
13756
  join_tab=tables;
 
13757
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13758
  {
 
13759
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13760
      calc_used_field_length(thd, join_tab);
 
13761
    cache->fields+=join_tab->used_fields;
 
13762
    blobs+=join_tab->used_blobs;
 
13763
 
 
13764
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13765
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13766
    {
 
13767
      cache->fields++;
 
13768
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13769
    }
 
13770
  }
 
13771
  if (!(cache->field=(CACHE_FIELD*)
 
13772
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13773
 
 
13774
                  sizeof(CACHE_FIELD*))))
 
13775
  {
 
13776
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
13777
    cache->buff=0;                              /* purecov: inspected */
 
13778
    return(1);                          /* purecov: inspected */
 
13779
  }
 
13780
  copy=cache->field;
 
13781
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13782
    (cache->field+cache->fields+table_count*2);
 
13783
 
 
13784
  length=0;
 
13785
  for (i=0 ; i < table_count ; i++)
 
13786
  {
 
13787
    uint32_t null_fields=0, used_fields;
 
13788
    Field **f_ptr,*field;
 
13789
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13790
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13791
         used_fields ;
 
13792
         f_ptr++)
 
13793
    {
 
13794
      field= *f_ptr;
 
13795
      if (bitmap_is_set(read_set, field->field_index))
 
13796
      {
 
13797
        used_fields--;
 
13798
        length+=field->fill_cache_field(copy);
 
13799
        if (copy->blob_field)
 
13800
          (*blob_ptr++)=copy;
 
13801
        if (field->maybe_null())
 
13802
          null_fields++;
 
13803
        copy->get_rowid= NULL;
 
13804
        copy++;
 
13805
      }
 
13806
    }
 
13807
    /* Copy null bits from table */
 
13808
    if (null_fields && tables[i].table->getNullFields())
 
13809
    {                                           /* must copy null bits */
 
13810
      copy->str= tables[i].table->null_flags;
 
13811
      copy->length= tables[i].table->s->null_bytes;
 
13812
      copy->strip=0;
 
13813
      copy->blob_field=0;
 
13814
      copy->get_rowid= NULL;
 
13815
      length+=copy->length;
 
13816
      copy++;
 
13817
      cache->fields++;
 
13818
    }
 
13819
    /* If outer join table, copy null_row flag */
 
13820
    if (tables[i].table->maybe_null)
 
13821
    {
 
13822
      copy->str= (unsigned char*) &tables[i].table->null_row;
 
13823
      copy->length=sizeof(tables[i].table->null_row);
 
13824
      copy->strip=0;
 
13825
      copy->blob_field=0;
 
13826
      copy->get_rowid= NULL;
 
13827
      length+=copy->length;
 
13828
      copy++;
 
13829
      cache->fields++;
 
13830
    }
 
13831
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13832
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13833
    {
 
13834
      copy->str= tables[i].table->file->ref;
 
13835
      copy->length= tables[i].table->file->ref_length;
 
13836
      copy->strip=0;
 
13837
      copy->blob_field=0;
 
13838
      copy->get_rowid= NULL;
 
13839
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13840
      {
 
13841
        /* We will need to call h->position(): */
 
13842
        copy->get_rowid= tables[i].table;
 
13843
        /* And those after us won't have to: */
 
13844
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13845
      }
 
13846
      copy++;
 
13847
    }
 
13848
  }
 
13849
 
 
13850
  cache->length=length+blobs*sizeof(char*);
 
13851
  cache->blobs=blobs;
 
13852
  *blob_ptr=0;                                  /* End sequentel */
 
13853
  size=cmax(thd->variables.join_buff_size, (ulong)cache->length);
 
13854
  if (!(cache->buff=(unsigned char*) my_malloc(size,MYF(0))))
 
13855
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
13856
  cache->end=cache->buff+size;
 
13857
  reset_cache_write(cache);
 
13858
  return(0);
 
13859
}
 
13860
 
 
13861
 
 
13862
static ulong
 
13863
used_blob_length(CACHE_FIELD **ptr)
 
13864
{
 
13865
  uint32_t length,blob_length;
 
13866
  for (length=0 ; *ptr ; ptr++)
 
13867
  {
 
13868
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13869
    length+=blob_length;
 
13870
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13871
  }
 
13872
  return length;
 
13873
}
 
13874
 
 
13875
 
 
13876
static bool
 
13877
store_record_in_cache(JOIN_CACHE *cache)
 
13878
{
 
13879
  uint32_t length;
 
13880
  unsigned char *pos;
 
13881
  CACHE_FIELD *copy,*end_field;
 
13882
  bool last_record;
 
13883
 
 
13884
  pos=cache->pos;
 
13885
  end_field=cache->field+cache->fields;
 
13886
 
 
13887
  length=cache->length;
 
13888
  if (cache->blobs)
 
13889
    length+=used_blob_length(cache->blob_ptr);
 
13890
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13891
    cache->ptr_record=cache->records;
 
13892
  /*
 
13893
    There is room in cache. Put record there
 
13894
  */
 
13895
  cache->records++;
 
13896
  for (copy=cache->field ; copy < end_field; copy++)
 
13897
  {
 
13898
    if (copy->blob_field)
 
13899
    {
 
13900
      if (last_record)
 
13901
      {
 
13902
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
13903
                                    copy->blob_field->charset());
 
13904
        pos+=copy->length+sizeof(char*);
 
13905
      }
 
13906
      else
 
13907
      {
 
13908
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13909
                                    copy->blob_field->charset());
 
13910
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13911
        pos+=copy->length+copy->blob_length;
 
13912
      }
 
13913
    }
 
13914
    else
 
13915
    {
 
13916
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13917
      if (copy->get_rowid)
 
13918
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13919
 
 
13920
      if (copy->strip)
 
13921
      {
 
13922
        unsigned char *str,*end;
 
13923
        for (str=copy->str,end= str+copy->length;
 
13924
             end > str && end[-1] == ' ' ;
 
13925
             end--) ;
 
13926
        length=(uint) (end-str);
 
13927
        memcpy(pos+2, str, length);
 
13928
        int2store(pos, length);
 
13929
        pos+= length+2;
 
13930
      }
 
13931
      else
 
13932
      {
 
13933
        memcpy(pos,copy->str,copy->length);
 
13934
        pos+=copy->length;
 
13935
      }
 
13936
    }
 
13937
  }
 
13938
  cache->pos=pos;
 
13939
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
13940
}
 
13941
 
 
13942
 
 
13943
static void
 
13944
reset_cache_read(JOIN_CACHE *cache)
 
13945
{
 
13946
  cache->record_nr=0;
 
13947
  cache->pos=cache->buff;
 
13948
}
 
13949
 
 
13950
 
 
13951
static void reset_cache_write(JOIN_CACHE *cache)
 
13952
{
 
13953
  reset_cache_read(cache);
 
13954
  cache->records= 0;
 
13955
  cache->ptr_record= UINT32_MAX;
 
13956
}
 
13957
 
 
13958
 
 
13959
static void
 
13960
read_cached_record(JOIN_TAB *tab)
 
13961
{
 
13962
  unsigned char *pos;
 
13963
  uint32_t length;
 
13964
  bool last_record;
 
13965
  CACHE_FIELD *copy,*end_field;
 
13966
 
 
13967
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
13968
  pos=tab->cache.pos;
 
13969
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
13970
       copy < end_field;
 
13971
       copy++)
 
13972
  {
 
13973
    if (copy->blob_field)
 
13974
    {
 
13975
      if (last_record)
 
13976
      {
 
13977
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
13978
                                    copy->blob_field->charset());
 
13979
        pos+=copy->length+sizeof(char*);
 
13980
      }
 
13981
      else
 
13982
      {
 
13983
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
13984
        pos+=copy->length+copy->blob_field->get_length();
 
13985
      }
 
13986
    }
 
13987
    else
 
13988
    {
 
13989
      if (copy->strip)
 
13990
      {
 
13991
        length= uint2korr(pos);
 
13992
        memcpy(copy->str, pos+2, length);
 
13993
        memset(copy->str+length, ' ', copy->length-length);
 
13994
        pos+= 2 + length;
 
13995
      }
 
13996
      else
 
13997
      {
 
13998
        memcpy(copy->str,pos,copy->length);
 
13999
        pos+=copy->length;
 
14000
      }
 
14001
    }
 
14002
  }
 
14003
  tab->cache.pos=pos;
 
14004
  return;
 
14005
}
 
14006
 
 
14007
 
5310
14008
/*
5311
14009
  eq_ref: Create the lookup key and check if it is the same as saved key
5312
14010
 
5313
14011
  SYNOPSIS
5314
14012
    cmp_buffer_with_ref()
5315
14013
      tab  Join tab of the accessed table
5316
 
 
5317
 
  DESCRIPTION
5318
 
    Used by eq_ref access method: create the index lookup key and check if
 
14014
 
 
14015
  DESCRIPTION 
 
14016
    Used by eq_ref access method: create the index lookup key and check if 
5319
14017
    we've used this key at previous lookup (If yes, we don't need to repeat
5320
14018
    the lookup - the record has been already fetched)
5321
14019
 
5322
 
  RETURN
 
14020
  RETURN 
5323
14021
    true   No cached record for the key, or failed to create the key (due to
5324
14022
           out-of-domain error)
5325
 
    false  The created key is the same as the previous one (and the record
 
14023
    false  The created key is the same as the previous one (and the record 
5326
14024
           is already in table->record)
5327
14025
*/
5328
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14026
 
 
14027
static bool
 
14028
cmp_buffer_with_ref(JOIN_TAB *tab)
5329
14029
{
5330
14030
  bool no_prev_key;
5331
14031
  if (!tab->ref.disable_cache)
5336
14036
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5337
14037
    }
5338
14038
  }
5339
 
  else
 
14039
  else 
5340
14040
    no_prev_key= true;
5341
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
14041
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, &tab->ref)) ||
5342
14042
      no_prev_key)
5343
14043
    return 1;
5344
14044
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5345
14045
    != 0;
5346
14046
}
5347
14047
 
5348
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14048
 
 
14049
bool
 
14050
cp_buffer_from_ref(THD *thd, TABLE_REF *ref)
5349
14051
{
5350
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5351
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
14052
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
14053
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
5352
14054
  bool result= 0;
5353
14055
 
5354
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14056
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5355
14057
  {
5356
14058
    if ((*copy)->copy() & 1)
5357
14059
    {
5359
14061
      break;
5360
14062
    }
5361
14063
  }
5362
 
  session->count_cuted_fields= save_count_cuted_fields;
 
14064
  thd->count_cuted_fields= save_count_cuted_fields;
5363
14065
  return result;
5364
14066
}
5365
14067
 
 
14068
 
5366
14069
/*****************************************************************************
5367
14070
  Group and order functions
5368
14071
*****************************************************************************/
5369
14072
 
5370
14073
/**
5371
 
  Resolve an ORDER BY or GROUP BY column reference.
 
14074
  Resolve an order_st BY or GROUP BY column reference.
5372
14075
 
5373
14076
  Given a column reference (represented by 'order') from a GROUP BY or order_st
5374
14077
  BY clause, find the actual column it represents. If the column being
5375
14078
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5376
14079
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5377
 
  the ORDER BY clause, only the SELECT list is being searched.
 
14080
  the order_st BY clause, only the SELECT list is being searched.
5378
14081
 
5379
14082
  If 'order' is resolved to an Item, then order->item is set to the found
5380
14083
  Item. If there is no item for the found column (that is, it was resolved
5383
14086
 
5384
14087
  ref_pointer_array and all_fields are updated.
5385
14088
 
5386
 
  @param[in] session                 Pointer to current thread structure
 
14089
  @param[in] thd                     Pointer to current thread structure
5387
14090
  @param[in,out] ref_pointer_array  All select, group and order by fields
5388
14091
  @param[in] tables                 List of tables to search in (usually
5389
14092
    FROM clause)
5399
14102
  @retval
5400
14103
    true  if error occurred
5401
14104
*/
5402
 
static bool find_order_in_list(Session *session, 
5403
 
                               Item **ref_pointer_array, 
5404
 
                               TableList *tables,
5405
 
                               Order *order,
5406
 
                               List<Item> &fields,
5407
 
                               List<Item> &all_fields,
5408
 
                               bool is_group_field)
 
14105
 
 
14106
static bool
 
14107
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14108
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
 
14109
                   bool is_group_field)
5409
14110
{
5410
14111
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
5411
14112
  Item::Type order_item_type;
5420
14121
  */
5421
14122
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5422
14123
  {                                             /* Order by position */
5423
 
    uint32_t count= (uint32_t) order_item->val_int();
 
14124
    uint32_t count= (uint) order_item->val_int();
5424
14125
    if (!count || count > fields.elements)
5425
14126
    {
5426
14127
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5427
 
               order_item->full_name(), session->where);
 
14128
               order_item->full_name(), thd->where);
5428
14129
      return true;
5429
14130
    }
5430
14131
    order->item= ref_pointer_array + count - 1;
5434
14135
    return false;
5435
14136
  }
5436
14137
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5437
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
14138
  select_item= find_item_in_list(order_item, fields, &counter,
5438
14139
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5439
14140
  if (!select_item)
5440
14141
    return true; /* The item is not unique, or some other error occured. */
5450
14151
      for this name (in case if we would perform lookup in all tables).
5451
14152
    */
5452
14153
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5453
 
        order_item->fix_fields(session, order->item))
 
14154
        order_item->fix_fields(thd, order->item))
5454
14155
      return true;
5455
14156
 
5456
14157
    /* Lookup the current GROUP field in the FROM clause. */
5459
14160
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5460
14161
        order_item_type == Item::REF_ITEM)
5461
14162
    {
5462
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5463
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14163
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
14164
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14165
                                       false);
5464
14166
      if (!from_field)
5465
14167
        from_field= (Field*) not_found_field;
5466
14168
    }
5498
14200
        warning so the user knows that the field from the FROM clause
5499
14201
        overshadows the column reference from the SELECT list.
5500
14202
      */
5501
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
14203
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5502
14204
                          ER(ER_NON_UNIQ_ERROR),
5503
14205
                          ((Item_ident*) order_item)->field_name,
5504
 
                          session->where);
 
14206
                          current_thd->where);
5505
14207
    }
5506
14208
  }
5507
14209
 
5518
14220
    arguments for which fix_fields already was called.
5519
14221
  */
5520
14222
  if (!order_item->fixed &&
5521
 
      (order_item->fix_fields(session, order->item) ||
 
14223
      (order_item->fix_fields(thd, order->item) ||
5522
14224
       (order_item= *order->item)->check_cols(1) ||
5523
 
       session->is_fatal_error))
 
14225
       thd->is_fatal_error))
5524
14226
    return true; /* Wrong field. */
5525
14227
 
5526
14228
  uint32_t el= all_fields.elements;
5530
14232
  return false;
5531
14233
}
5532
14234
 
 
14235
 
5533
14236
/**
5534
14237
  Change order to point at item in select list.
5535
14238
 
5536
14239
  If item isn't a number and doesn't exits in the select list, add it the
5537
14240
  the field list.
5538
14241
*/
5539
 
int setup_order(Session *session,
5540
 
                Item **ref_pointer_array,
5541
 
                TableList *tables,
5542
 
                            List<Item> &fields,
5543
 
                List<Item> &all_fields,
5544
 
                Order *order)
 
14242
 
 
14243
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14244
                List<Item> &fields, List<Item> &all_fields, order_st *order)
5545
14245
{
5546
 
  session->where="order clause";
 
14246
  thd->where="order clause";
5547
14247
  for (; order; order=order->next)
5548
14248
  {
5549
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
14249
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5550
14250
                           all_fields, false))
5551
14251
      return 1;
5552
14252
  }
5553
14253
  return 0;
5554
14254
}
5555
14255
 
 
14256
 
5556
14257
/**
5557
14258
  Intitialize the GROUP BY list.
5558
14259
 
5559
 
  @param session                        Thread Cursor
 
14260
  @param thd                    Thread handler
5560
14261
  @param ref_pointer_array      We store references to all fields that was
5561
14262
                               not in 'fields' here.
5562
14263
  @param fields         All fields in the select part. Any item in
5578
14279
  @retval
5579
14280
    1  error (probably out of memory)
5580
14281
*/
5581
 
int setup_group(Session *session,
5582
 
                Item **ref_pointer_array,
5583
 
                TableList *tables,
5584
 
                      List<Item> &fields,
5585
 
                List<Item> &all_fields,
5586
 
                Order *order,
5587
 
                      bool *hidden_group_fields)
 
14282
 
 
14283
int
 
14284
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
 
14285
            List<Item> &fields, List<Item> &all_fields, order_st *order,
 
14286
            bool *hidden_group_fields)
5588
14287
{
5589
14288
  *hidden_group_fields=0;
5590
 
  Order *ord;
 
14289
  order_st *ord;
5591
14290
 
5592
14291
  if (!order)
5593
14292
    return 0;                           /* Everything is ok */
5594
14293
 
5595
14294
  uint32_t org_fields=all_fields.elements;
5596
14295
 
5597
 
  session->where="group statement";
 
14296
  thd->where="group statement";
5598
14297
  for (ord= order; ord; ord= ord->next)
5599
14298
  {
5600
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
14299
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5601
14300
                           all_fields, true))
5602
14301
      return 1;
5603
14302
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5628
14327
    Item_field *field;
5629
14328
    int cur_pos_in_select_list= 0;
5630
14329
    List_iterator<Item> li(fields);
5631
 
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
 
14330
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5632
14331
 
5633
14332
    field= naf_it++;
5634
14333
    while (field && (item=li++))
5677
14376
  Try to use the fields in the order given by 'order' to allow one to
5678
14377
  optimize away 'order by'.
5679
14378
*/
5680
 
Order *create_distinct_group(Session *session,
5681
 
                                Item **ref_pointer_array,
5682
 
                                Order *order_list,
5683
 
                                List<Item> &fields,
5684
 
                                List<Item> &,
5685
 
                                bool *all_order_by_fields_used)
 
14379
 
 
14380
static order_st *
 
14381
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
14382
                      order_st *order_list, List<Item> &fields,
 
14383
                      List<Item> &all_fields __attribute__((unused)),
 
14384
                      bool *all_order_by_fields_used)
5686
14385
{
5687
14386
  List_iterator<Item> li(fields);
5688
14387
  Item *item;
5689
 
  Order *order,*group,**prev;
 
14388
  order_st *order,*group,**prev;
5690
14389
 
5691
14390
  *all_order_by_fields_used= 1;
5692
14391
  while ((item=li++))
5697
14396
  {
5698
14397
    if (order->in_field_list)
5699
14398
    {
5700
 
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
 
14399
      order_st *ord=(order_st*) thd->memdup((char*) order,sizeof(order_st));
5701
14400
      if (!ord)
5702
 
        return 0;
 
14401
        return 0;
5703
14402
      *prev=ord;
5704
14403
      prev= &ord->next;
5705
14404
      (*ord->item)->marker=1;
5713
14412
  {
5714
14413
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5715
14414
    {
5716
 
      /*
5717
 
        Don't put duplicate columns from the SELECT list into the
 
14415
      /* 
 
14416
        Don't put duplicate columns from the SELECT list into the 
5718
14417
        GROUP BY list.
5719
14418
      */
5720
 
      Order *ord_iter;
 
14419
      order_st *ord_iter;
5721
14420
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5722
14421
        if ((*ord_iter->item)->eq(item, 1))
5723
14422
          goto next_item;
5724
 
 
5725
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
14423
      
 
14424
      order_st *ord=(order_st*) thd->calloc(sizeof(order_st));
5726
14425
      if (!ord)
5727
 
        return 0;
 
14426
        return 0;
5728
14427
 
5729
14428
      /*
5730
14429
        We have here only field_list (not all_field_list), so we can use
5743
14442
  return group;
5744
14443
}
5745
14444
 
 
14445
 
5746
14446
/**
5747
14447
  Update join with count of the different type of fields.
5748
14448
*/
5749
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14449
 
 
14450
void
 
14451
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
14452
                  List<Item> &fields, bool reset_with_sum_func)
5750
14453
{
5751
14454
  List_iterator<Item> li(fields);
5752
14455
  Item *field;
5763
14466
    {
5764
14467
      if (! field->const_item())
5765
14468
      {
5766
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14469
        Item_sum *sum_item=(Item_sum*) field->real_item();
5767
14470
        if (!sum_item->depended_from() ||
5768
14471
            sum_item->depended_from() == select_lex)
5769
14472
        {
5786
14489
    {
5787
14490
      param->func_count++;
5788
14491
      if (reset_with_sum_func)
5789
 
        field->with_sum_func=0;
5790
 
    }
5791
 
  }
5792
 
}
 
14492
        field->with_sum_func=0;
 
14493
    }
 
14494
  }
 
14495
}
 
14496
 
 
14497
 
 
14498
/**
 
14499
  Return 1 if second is a subpart of first argument.
 
14500
 
 
14501
  If first parts has different direction, change it to second part
 
14502
  (group is sorted like order)
 
14503
*/
 
14504
 
 
14505
static bool
 
14506
test_if_subpart(order_st *a,order_st *b)
 
14507
{
 
14508
  for (; a && b; a=a->next,b=b->next)
 
14509
  {
 
14510
    if ((*a->item)->eq(*b->item,1))
 
14511
      a->asc=b->asc;
 
14512
    else
 
14513
      return 0;
 
14514
  }
 
14515
  return test(!b);
 
14516
}
 
14517
 
 
14518
/**
 
14519
  Return table number if there is only one table in sort order
 
14520
  and group and order is compatible, else return 0.
 
14521
*/
 
14522
 
 
14523
static Table *
 
14524
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
14525
{
 
14526
  table_map map= (table_map) 0;
 
14527
 
 
14528
  if (!a)
 
14529
    a=b;                                        // Only one need to be given
 
14530
  else if (!b)
 
14531
    b=a;
 
14532
 
 
14533
  for (; a && b; a=a->next,b=b->next)
 
14534
  {
 
14535
    if (!(*a->item)->eq(*b->item,1))
 
14536
      return(0);
 
14537
    map|=a->item[0]->used_tables();
 
14538
  }
 
14539
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14540
    return(0);
 
14541
 
 
14542
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14543
  if (map != tables->table->map)
 
14544
    return(0);                          // More than one table
 
14545
  return(tables->table);
 
14546
}
 
14547
 
 
14548
 
 
14549
/**
 
14550
  calc how big buffer we need for comparing group entries.
 
14551
*/
 
14552
 
 
14553
static void
 
14554
calc_group_buffer(JOIN *join,order_st *group)
 
14555
{
 
14556
  uint32_t key_length=0, parts=0, null_parts=0;
 
14557
 
 
14558
  if (group)
 
14559
    join->group= 1;
 
14560
  for (; group ; group=group->next)
 
14561
  {
 
14562
    Item *group_item= *group->item;
 
14563
    Field *field= group_item->get_tmp_table_field();
 
14564
    if (field)
 
14565
    {
 
14566
      enum_field_types type;
 
14567
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14568
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14569
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14570
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14571
      else
 
14572
        key_length+= field->pack_length();
 
14573
    }
 
14574
    else
 
14575
    { 
 
14576
      switch (group_item->result_type()) {
 
14577
      case REAL_RESULT:
 
14578
        key_length+= sizeof(double);
 
14579
        break;
 
14580
      case INT_RESULT:
 
14581
        key_length+= sizeof(int64_t);
 
14582
        break;
 
14583
      case DECIMAL_RESULT:
 
14584
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
14585
                                                (group_item->decimals ? 1 : 0),
 
14586
                                                group_item->decimals);
 
14587
        break;
 
14588
      case STRING_RESULT:
 
14589
      {
 
14590
        enum enum_field_types type= group_item->field_type();
 
14591
        /*
 
14592
          As items represented as DATE/TIME fields in the group buffer
 
14593
          have STRING_RESULT result type, we increase the length 
 
14594
          by 8 as maximum pack length of such fields.
 
14595
        */
 
14596
        if (type == DRIZZLE_TYPE_TIME ||
 
14597
            type == DRIZZLE_TYPE_NEWDATE ||
 
14598
            type == DRIZZLE_TYPE_DATETIME ||
 
14599
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14600
        {
 
14601
          key_length+= 8;
 
14602
        }
 
14603
        else
 
14604
        {
 
14605
          /*
 
14606
            Group strings are taken as varstrings and require an length field.
 
14607
            A field is not yet created by create_tmp_field()
 
14608
            and the sizes should match up.
 
14609
          */
 
14610
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14611
        }
 
14612
        break;
 
14613
      }
 
14614
      default:
 
14615
        /* This case should never be choosen */
 
14616
        assert(0);
 
14617
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14618
      }
 
14619
    }
 
14620
    parts++;
 
14621
    if (group_item->maybe_null)
 
14622
      null_parts++;
 
14623
  }
 
14624
  join->tmp_table_param.group_length=key_length+null_parts;
 
14625
  join->tmp_table_param.group_parts=parts;
 
14626
  join->tmp_table_param.group_null_parts=null_parts;
 
14627
}
 
14628
 
 
14629
 
 
14630
/**
 
14631
  allocate group fields or take prepared (cached).
 
14632
 
 
14633
  @param main_join   join of current select
 
14634
  @param curr_join   current join (join of current select or temporary copy
 
14635
                     of it)
 
14636
 
 
14637
  @retval
 
14638
    0   ok
 
14639
  @retval
 
14640
    1   failed
 
14641
*/
 
14642
 
 
14643
static bool
 
14644
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14645
{
 
14646
  if (main_join->group_fields_cache.elements)
 
14647
  {
 
14648
    curr_join->group_fields= main_join->group_fields_cache;
 
14649
    curr_join->sort_and_group= 1;
 
14650
  }
 
14651
  else
 
14652
  {
 
14653
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14654
      return (1);
 
14655
    main_join->group_fields_cache= curr_join->group_fields;
 
14656
  }
 
14657
  return (0);
 
14658
}
 
14659
 
 
14660
 
 
14661
/**
 
14662
  Get a list of buffers for saveing last group.
 
14663
 
 
14664
  Groups are saved in reverse order for easyer check loop.
 
14665
*/
 
14666
 
 
14667
static bool
 
14668
alloc_group_fields(JOIN *join,order_st *group)
 
14669
{
 
14670
  if (group)
 
14671
  {
 
14672
    for (; group ; group=group->next)
 
14673
    {
 
14674
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
14675
      if (!tmp || join->group_fields.push_front(tmp))
 
14676
        return true;
 
14677
    }
 
14678
  }
 
14679
  join->sort_and_group=1;                       /* Mark for do_select */
 
14680
  return false;
 
14681
}
 
14682
 
5793
14683
 
5794
14684
/*
5795
14685
  Test if a single-row cache of items changed, and update the cache.
5797
14687
  @details Test if a list of items that typically represents a result
5798
14688
  row has changed. If the value of some item changed, update the cached
5799
14689
  value for this item.
5800
 
 
 
14690
  
5801
14691
  @param list list of <item, cached_value> pairs stored as Cached_item.
5802
14692
 
5803
14693
  @return -1 if no item changed
5804
14694
  @return index of the first item that changed
5805
14695
*/
 
14696
 
5806
14697
int test_if_item_cache_changed(List<Cached_item> &list)
5807
14698
{
5808
14699
  List_iterator<Cached_item> li(list);
5817
14708
  return(idx);
5818
14709
}
5819
14710
 
 
14711
 
5820
14712
/**
5821
14713
  Setup copy_fields to save fields at start of new group.
5822
14714
 
5826
14718
  Change old item_field to use a new field with points at saved fieldvalue
5827
14719
  This function is only called before use of send_fields.
5828
14720
 
5829
 
  @param session                   Session pointer
 
14721
  @param thd                   THD pointer
5830
14722
  @param param                 temporary table parameters
5831
14723
  @param ref_pointer_array     array of pointers to top elements of filed list
5832
14724
  @param res_selected_fields   new list of items of select item list
5845
14737
  @retval
5846
14738
    !=0   error
5847
14739
*/
5848
 
bool setup_copy_fields(Session *session,
5849
 
                       Tmp_Table_Param *param,
5850
 
                       Item **ref_pointer_array,
5851
 
                       List<Item> &res_selected_fields,
5852
 
                       List<Item> &res_all_fields,
5853
 
                       uint32_t elements,
5854
 
                       List<Item> &all_fields)
 
14740
 
 
14741
bool
 
14742
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
14743
                  Item **ref_pointer_array,
 
14744
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14745
                  uint32_t elements, List<Item> &all_fields)
5855
14746
{
5856
14747
  Item *pos;
5857
14748
  List_iterator_fast<Item> li(all_fields);
5858
 
  CopyField *copy= NULL;
 
14749
  Copy_field *copy= NULL;
5859
14750
  res_selected_fields.empty();
5860
14751
  res_all_fields.empty();
5861
14752
  List_iterator_fast<Item> itr(res_all_fields);
5862
14753
  List<Item> extra_funcs;
5863
14754
  uint32_t i, border= all_fields.elements - elements;
5864
14755
 
5865
 
  if (param->field_count &&
5866
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14756
  if (param->field_count && 
 
14757
      !(copy=param->copy_field= new Copy_field[param->field_count]))
5867
14758
    goto err2;
5868
14759
 
5869
14760
  param->copy_funcs.empty();
5875
14766
    if (real_pos->type() == Item::FIELD_ITEM)
5876
14767
    {
5877
14768
      Item_field *item;
5878
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
5879
 
        goto err;
 
14769
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
14770
        goto err;
5880
14771
      if (pos->type() == Item::REF_ITEM)
5881
14772
      {
5882
14773
        /* preserve the names of the ref when dereferncing */
5888
14779
      pos= item;
5889
14780
      if (item->field->flags & BLOB_FLAG)
5890
14781
      {
5891
 
        if (!(pos= new Item_copy_string(pos)))
5892
 
          goto err;
5893
 
            /*
5894
 
              Item_copy_string::copy for function can call
5895
 
              Item_copy_string::val_int for blob via Item_ref.
5896
 
              But if Item_copy_string::copy for blob isn't called before,
5897
 
              it's value will be wrong
5898
 
              so let's insert Item_copy_string for blobs in the beginning of
5899
 
              copy_funcs
5900
 
              (to see full test case look at having.test, BUG #4358)
5901
 
            */
5902
 
        if (param->copy_funcs.push_front(pos))
5903
 
          goto err;
 
14782
        if (!(pos= new Item_copy_string(pos)))
 
14783
          goto err;
 
14784
       /*
 
14785
         Item_copy_string::copy for function can call 
 
14786
         Item_copy_string::val_int for blob via Item_ref.
 
14787
         But if Item_copy_string::copy for blob isn't called before,
 
14788
         it's value will be wrong
 
14789
         so let's insert Item_copy_string for blobs in the beginning of 
 
14790
         copy_funcs
 
14791
         (to see full test case look at having.test, BUG #4358) 
 
14792
       */
 
14793
        if (param->copy_funcs.push_front(pos))
 
14794
          goto err;
5904
14795
      }
5905
14796
      else
5906
14797
      {
 
14798
        /* 
 
14799
           set up save buffer and change result_field to point at 
 
14800
           saved value
 
14801
        */
 
14802
        field= item->field;
 
14803
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
5907
14804
        /*
5908
 
          set up save buffer and change result_field to point at
5909
 
          saved value
 
14805
          We need to allocate one extra byte for null handling and
 
14806
          another extra byte to not get warnings from purify in
 
14807
          Field_varstring::val_int
5910
14808
        */
5911
 
        field= item->field;
5912
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
5913
 
              /*
5914
 
                We need to allocate one extra byte for null handling and
5915
 
                another extra byte to not get warnings from purify in
5916
 
                Field_varstring::val_int
5917
 
              */
5918
 
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
5919
 
          goto err;
 
14809
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
14810
          goto err;
5920
14811
        if (copy)
5921
14812
        {
5922
14813
          copy->set(tmp, item->result_field);
5923
14814
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5924
 
#ifdef HAVE_VALGRIND
 
14815
#ifdef HAVE_purify
5925
14816
          copy->to_ptr[copy->from_length]= 0;
5926
14817
#endif
5927
14818
          copy++;
5936
14827
    {                                           // Save for send fields
5937
14828
      pos= real_pos;
5938
14829
      /* TODO:
5939
 
        In most cases this result will be sent to the user.
5940
 
        This should be changed to use copy_int or copy_real depending
5941
 
        on how the value is to be used: In some cases this may be an
5942
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
14830
         In most cases this result will be sent to the user.
 
14831
         This should be changed to use copy_int or copy_real depending
 
14832
         on how the value is to be used: In some cases this may be an
 
14833
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5943
14834
      */
5944
14835
      if (!(pos=new Item_copy_string(pos)))
5945
 
        goto err;
 
14836
        goto err;
5946
14837
      if (i < border)                           // HAVING, order_st and GROUP BY
5947
14838
      {
5948
14839
        if (extra_funcs.push_back(pos))
5949
14840
          goto err;
5950
14841
      }
5951
14842
      else if (param->copy_funcs.push_back(pos))
5952
 
        goto err;
 
14843
        goto err;
5953
14844
    }
5954
14845
    res_all_fields.push_back(pos);
5955
14846
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
5961
14852
    itr++;
5962
14853
  itr.sublist(res_selected_fields, elements);
5963
14854
  /*
5964
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
14855
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
5965
14856
    reference used in these will resolve to a item that is already calculated
5966
14857
  */
5967
14858
  param->copy_funcs.concat(&extra_funcs);
5968
14859
 
5969
14860
  return(0);
5970
14861
 
5971
 
err:
 
14862
 err:
5972
14863
  if (copy)
5973
14864
    delete [] param->copy_field;                        // This is never 0
5974
14865
  param->copy_field=0;
5976
14867
  return(true);
5977
14868
}
5978
14869
 
 
14870
 
5979
14871
/**
5980
14872
  Make a copy of all simple SELECT'ed items.
5981
14873
 
5982
14874
  This is done at the start of a new group so that we can retrieve
5983
14875
  these later when the group changes.
5984
14876
*/
5985
 
void copy_fields(Tmp_Table_Param *param)
 
14877
 
 
14878
void
 
14879
copy_fields(TMP_TABLE_PARAM *param)
5986
14880
{
5987
 
  CopyField *ptr= param->copy_field;
5988
 
  CopyField *end= param->copy_field_end;
 
14881
  Copy_field *ptr=param->copy_field;
 
14882
  Copy_field *end=param->copy_field_end;
5989
14883
 
5990
14884
  for (; ptr != end; ptr++)
5991
14885
    (*ptr->do_copy)(ptr);
5996
14890
    item->copy();
5997
14891
}
5998
14892
 
 
14893
 
 
14894
/**
 
14895
  Make an array of pointers to sum_functions to speed up
 
14896
  sum_func calculation.
 
14897
 
 
14898
  @retval
 
14899
    0   ok
 
14900
  @retval
 
14901
    1   Error
 
14902
*/
 
14903
 
 
14904
bool JOIN::alloc_func_list()
 
14905
{
 
14906
  uint32_t func_count, group_parts;
 
14907
 
 
14908
  func_count= tmp_table_param.sum_func_count;
 
14909
  /*
 
14910
    If we are using rollup, we need a copy of the summary functions for
 
14911
    each level
 
14912
  */
 
14913
  if (rollup.state != ROLLUP::STATE_NONE)
 
14914
    func_count*= (send_group_parts+1);
 
14915
 
 
14916
  group_parts= send_group_parts;
 
14917
  /*
 
14918
    If distinct, reserve memory for possible
 
14919
    disctinct->group_by optimization
 
14920
  */
 
14921
  if (select_distinct)
 
14922
  {
 
14923
    group_parts+= fields_list.elements;
 
14924
    /*
 
14925
      If the order_st clause is specified then it's possible that
 
14926
      it also will be optimized, so reserve space for it too
 
14927
    */
 
14928
    if (order)
 
14929
    {
 
14930
      order_st *ord;
 
14931
      for (ord= order; ord; ord= ord->next)
 
14932
        group_parts++;
 
14933
    }
 
14934
  }
 
14935
 
 
14936
  /* This must use calloc() as rollup_make_fields depends on this */
 
14937
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
14938
                                      sizeof(Item_sum***) * (group_parts+1));
 
14939
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
14940
  return(sum_funcs == 0);
 
14941
}
 
14942
 
 
14943
 
 
14944
/**
 
14945
  Initialize 'sum_funcs' array with all Item_sum objects.
 
14946
 
 
14947
  @param field_list        All items
 
14948
  @param send_fields       Items in select list
 
14949
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
14950
  @param recompute         Set to true if sum_funcs must be recomputed
 
14951
 
 
14952
  @retval
 
14953
    0  ok
 
14954
  @retval
 
14955
    1  error
 
14956
*/
 
14957
 
 
14958
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
14959
                              bool before_group_by, bool recompute)
 
14960
{
 
14961
  List_iterator_fast<Item> it(field_list);
 
14962
  Item_sum **func;
 
14963
  Item *item;
 
14964
 
 
14965
  if (*sum_funcs && !recompute)
 
14966
    return(false); /* We have already initialized sum_funcs. */
 
14967
 
 
14968
  func= sum_funcs;
 
14969
  while ((item=it++))
 
14970
  {
 
14971
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
14972
        (!((Item_sum*) item)->depended_from() ||
 
14973
         ((Item_sum *)item)->depended_from() == select_lex))
 
14974
      *func++= (Item_sum*) item;
 
14975
  }
 
14976
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
14977
  {
 
14978
    rollup.state= ROLLUP::STATE_READY;
 
14979
    if (rollup_make_fields(field_list, send_fields, &func))
 
14980
      return(true);                     // Should never happen
 
14981
  }
 
14982
  else if (rollup.state == ROLLUP::STATE_NONE)
 
14983
  {
 
14984
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
14985
      sum_funcs_end[i]= func;
 
14986
  }
 
14987
  else if (rollup.state == ROLLUP::STATE_READY)
 
14988
    return(false);                         // Don't put end marker
 
14989
  *func=0;                                      // End marker
 
14990
  return(false);
 
14991
}
 
14992
 
 
14993
 
5999
14994
/**
6000
14995
  Change all funcs and sum_funcs to fields in tmp table, and create
6001
14996
  new list of all items.
6002
14997
 
6003
 
  @param session                   Session pointer
 
14998
  @param thd                   THD pointer
6004
14999
  @param ref_pointer_array     array of pointers to top elements of filed list
6005
15000
  @param res_selected_fields   new list of items of select item list
6006
15001
  @param res_all_fields        new list of all items
6012
15007
  @retval
6013
15008
    !=0   error
6014
15009
*/
6015
 
bool change_to_use_tmp_fields(Session *session,
6016
 
                              Item **ref_pointer_array,
6017
 
                                                List<Item> &res_selected_fields,
6018
 
                                                List<Item> &res_all_fields,
6019
 
                                                uint32_t elements,
6020
 
                              List<Item> &all_fields)
 
15010
 
 
15011
static bool
 
15012
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15013
                         List<Item> &res_selected_fields,
 
15014
                         List<Item> &res_all_fields,
 
15015
                         uint32_t elements, List<Item> &all_fields)
6021
15016
{
6022
15017
  List_iterator_fast<Item> it(all_fields);
6023
15018
  Item *item_field,*item;
6038
15033
    {
6039
15034
      if (item->type() == Item::FIELD_ITEM)
6040
15035
      {
6041
 
        item_field= item->get_tmp_table_item(session);
 
15036
        item_field= item->get_tmp_table_item(thd);
6042
15037
      }
6043
15038
      else if ((field= item->get_tmp_table_field()))
6044
15039
      {
6045
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6046
 
          item_field= ((Item_sum*) item)->result_item(field);
6047
 
        else
6048
 
          item_field= (Item*) new Item_field(field);
6049
 
        if (!item_field)
6050
 
          return(true);                    // Fatal error
 
15040
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15041
          item_field= ((Item_sum*) item)->result_item(field);
 
15042
        else
 
15043
          item_field= (Item*) new Item_field(field);
 
15044
        if (!item_field)
 
15045
          return(true);                    // Fatal error
6051
15046
 
6052
15047
        if (item->real_item()->type() != Item::FIELD_ITEM)
6053
15048
          field->orig_table= 0;
6054
 
        item_field->name= item->name;
 
15049
        item_field->name= item->name;
6055
15050
        if (item->type() == Item::REF_ITEM)
6056
15051
        {
6057
15052
          Item_field *ifield= (Item_field *) item_field;
6061
15056
        }
6062
15057
      }
6063
15058
      else
6064
 
        item_field= item;
 
15059
        item_field= item;
6065
15060
    }
6066
15061
    res_all_fields.push_back(item_field);
6067
15062
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6075
15070
  return(false);
6076
15071
}
6077
15072
 
 
15073
 
6078
15074
/**
6079
15075
  Change all sum_func refs to fields to point at fields in tmp table.
6080
15076
  Change all funcs to be fields in tmp table.
6081
15077
 
6082
 
  @param session                   Session pointer
 
15078
  @param thd                   THD pointer
6083
15079
  @param ref_pointer_array     array of pointers to top elements of filed list
6084
15080
  @param res_selected_fields   new list of items of select item list
6085
15081
  @param res_all_fields        new list of all items
6091
15087
  @retval
6092
15088
    1   error
6093
15089
*/
6094
 
bool change_refs_to_tmp_fields(Session *session,
6095
 
                               Item **ref_pointer_array,
6096
 
                               List<Item> &res_selected_fields,
6097
 
                               List<Item> &res_all_fields,
6098
 
                               uint32_t elements,
6099
 
                                                 List<Item> &all_fields)
 
15090
 
 
15091
static bool
 
15092
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15093
                          List<Item> &res_selected_fields,
 
15094
                          List<Item> &res_all_fields, uint32_t elements,
 
15095
                          List<Item> &all_fields)
6100
15096
{
6101
15097
  List_iterator_fast<Item> it(all_fields);
6102
15098
  Item *item, *new_item;
6106
15102
  uint32_t i, border= all_fields.elements - elements;
6107
15103
  for (i= 0; (item= it++); i++)
6108
15104
  {
6109
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
15105
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6110
15106
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6111
15107
      new_item;
6112
15108
  }
6116
15112
    itr++;
6117
15113
  itr.sublist(res_selected_fields, elements);
6118
15114
 
6119
 
  return session->is_fatal_error;
 
15115
  return thd->is_fatal_error;
6120
15116
}
6121
15117
 
 
15118
 
 
15119
 
6122
15120
/******************************************************************************
6123
15121
  Code for calculating functions
6124
15122
******************************************************************************/
6125
15123
 
 
15124
 
6126
15125
/**
6127
15126
  Call ::setup for all sum functions.
6128
15127
 
6129
 
  @param session           thread Cursor
 
15128
  @param thd           thread handler
6130
15129
  @param func_ptr      sum function list
6131
15130
 
6132
15131
  @retval
6134
15133
  @retval
6135
15134
    true   error
6136
15135
*/
6137
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15136
 
 
15137
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6138
15138
{
6139
15139
  Item_sum *func;
6140
15140
  while ((func= *(func_ptr++)))
6141
15141
  {
6142
 
    if (func->setup(session))
 
15142
    if (func->setup(thd))
6143
15143
      return(true);
6144
15144
  }
6145
15145
  return(false);
6146
15146
}
6147
15147
 
6148
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15148
 
 
15149
static void
 
15150
init_tmptable_sum_functions(Item_sum **func_ptr)
6149
15151
{
6150
15152
  Item_sum *func;
6151
15153
  while ((func= *(func_ptr++)))
6152
15154
    func->reset_field();
6153
15155
}
6154
15156
 
 
15157
 
6155
15158
/** Update record 0 in tmp_table from record 1. */
6156
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15159
 
 
15160
static void
 
15161
update_tmptable_sum_func(Item_sum **func_ptr,
 
15162
                         Table *tmp_table __attribute__((unused)))
6157
15163
{
6158
15164
  Item_sum *func;
6159
15165
  while ((func= *(func_ptr++)))
6160
15166
    func->update_field();
6161
15167
}
6162
15168
 
 
15169
 
6163
15170
/** Copy result of sum functions to record in tmp_table. */
6164
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15171
 
 
15172
static void
 
15173
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6165
15174
{
6166
15175
  for (; func_ptr != end_ptr ; func_ptr++)
6167
15176
    (void) (*func_ptr)->save_in_result_field(1);
6168
15177
  return;
6169
15178
}
6170
15179
 
6171
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15180
 
 
15181
static bool
 
15182
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6172
15183
{
6173
15184
  for (; func_ptr != end_ptr ;func_ptr++)
6174
15185
  {
6184
15195
  return 0;
6185
15196
}
6186
15197
 
6187
 
bool update_sum_func(Item_sum **func_ptr)
 
15198
 
 
15199
static bool
 
15200
update_sum_func(Item_sum **func_ptr)
6188
15201
{
6189
15202
  Item_sum *func;
6190
15203
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6194
15207
}
6195
15208
 
6196
15209
/** Copy result of functions to record in tmp_table. */
6197
 
void copy_funcs(Item **func_ptr)
 
15210
 
 
15211
void
 
15212
copy_funcs(Item **func_ptr)
6198
15213
{
6199
15214
  Item *func;
6200
15215
  for (; (func = *func_ptr) ; func_ptr++)
6201
15216
    func->save_in_result_field(1);
6202
15217
}
6203
15218
 
 
15219
 
 
15220
/**
 
15221
  Create a condition for a const reference and add this to the
 
15222
  currenct select for the table.
 
15223
*/
 
15224
 
 
15225
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
15226
{
 
15227
  if (!join_tab->ref.key_parts)
 
15228
    return(false);
 
15229
 
 
15230
  Item_cond_and *cond=new Item_cond_and();
 
15231
  Table *table=join_tab->table;
 
15232
  int error;
 
15233
  if (!cond)
 
15234
    return(true);
 
15235
 
 
15236
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
15237
  {
 
15238
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15239
                              fieldnr-1];
 
15240
    Item *value=join_tab->ref.items[i];
 
15241
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15242
  }
 
15243
  if (thd->is_fatal_error)
 
15244
    return(true);
 
15245
 
 
15246
  if (!cond->fixed)
 
15247
    cond->fix_fields(thd, (Item**)&cond);
 
15248
  if (join_tab->select)
 
15249
  {
 
15250
    error=(int) cond->add(join_tab->select->cond);
 
15251
    join_tab->select_cond=join_tab->select->cond=cond;
 
15252
  }
 
15253
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15254
                                          &error)))
 
15255
    join_tab->select_cond=cond;
 
15256
 
 
15257
  return(error ? true : false);
 
15258
}
 
15259
 
 
15260
 
6204
15261
/**
6205
15262
  Free joins of subselect of this select.
6206
15263
 
6207
 
  @param session      Session pointer
6208
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
15264
  @param thd      THD pointer
 
15265
  @param select   pointer to st_select_lex which subselects joins we will free
6209
15266
*/
6210
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
15267
 
 
15268
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
15269
                          SELECT_LEX *select)
6211
15270
{
6212
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
15271
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6213
15272
       unit;
6214
15273
       unit= unit->next_unit())
6215
15274
    unit->cleanup();
6231
15290
  @b EXAMPLES
6232
15291
    @code
6233
15292
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6234
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
15293
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6235
15294
  @endcode
6236
15295
 
6237
15296
  @b IMPLEMENTATION
6244
15303
    This substitution is needed GROUP BY queries with ROLLUP if
6245
15304
    SELECT list contains expressions over group by attributes.
6246
15305
 
6247
 
  @param session                  reference to the context
 
15306
  @param thd                  reference to the context
6248
15307
  @param expr                 expression to make replacement
6249
15308
  @param group_list           list of references to group by items
6250
15309
  @param changed        out:  returns 1 if item contains a replaced field item
6251
15310
 
6252
15311
  @todo
6253
15312
    - TODO: Some functions are not null-preserving. For those functions
6254
 
    updating of the maybe_null attribute is an overkill.
 
15313
    updating of the maybe_null attribute is an overkill. 
6255
15314
 
6256
15315
  @retval
6257
15316
    0   if ok
6258
15317
  @retval
6259
15318
    1   on error
6260
15319
*/
6261
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
15320
 
 
15321
static bool change_group_ref(THD *thd, Item_func *expr, order_st *group_list,
 
15322
                             bool *changed)
6262
15323
{
6263
15324
  if (expr->arg_count)
6264
15325
  {
6265
 
    Name_resolution_context *context= &session->lex->current_select->context;
 
15326
    Name_resolution_context *context= &thd->lex->current_select->context;
6266
15327
    Item **arg,**arg_end;
6267
15328
    bool arg_changed= false;
6268
15329
    for (arg= expr->arguments(),
6272
15333
      Item *item= *arg;
6273
15334
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6274
15335
      {
6275
 
        Order *group_tmp;
 
15336
        order_st *group_tmp;
6276
15337
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6277
15338
        {
6278
15339
          if (item->eq(*group_tmp->item,0))
6281
15342
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6282
15343
                                        item->name)))
6283
15344
              return 1;                                 // fatal_error is set
6284
 
            session->change_item_tree(arg, new_item);
 
15345
            thd->change_item_tree(arg, new_item);
6285
15346
            arg_changed= true;
6286
15347
          }
6287
15348
        }
6288
15349
      }
6289
15350
      else if (item->type() == Item::FUNC_ITEM)
6290
15351
      {
6291
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
15352
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6292
15353
          return 1;
6293
15354
      }
6294
15355
    }
6302
15363
}
6303
15364
 
6304
15365
 
6305
 
static void print_table_array(Session *session, String *str, TableList **table,
 
15366
/** Allocate memory needed for other rollup functions. */
 
15367
 
 
15368
bool JOIN::rollup_init()
 
15369
{
 
15370
  uint32_t i,j;
 
15371
  Item **ref_array;
 
15372
 
 
15373
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15374
  rollup.state= ROLLUP::STATE_INITED;
 
15375
 
 
15376
  /*
 
15377
    Create pointers to the different sum function groups
 
15378
    These are updated by rollup_make_fields()
 
15379
  */
 
15380
  tmp_table_param.group_parts= send_group_parts;
 
15381
 
 
15382
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
15383
                                                sizeof(Item**) +
 
15384
                                                sizeof(List<Item>) +
 
15385
                                                ref_pointer_array_size)
 
15386
                                                * send_group_parts )))
 
15387
    return 1;
 
15388
  
 
15389
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15390
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15391
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15392
 
 
15393
  /*
 
15394
    Prepare space for field list for the different levels
 
15395
    These will be filled up in rollup_make_fields()
 
15396
  */
 
15397
  for (i= 0 ; i < send_group_parts ; i++)
 
15398
  {
 
15399
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
15400
    List<Item> *rollup_fields= &rollup.fields[i];
 
15401
    rollup_fields->empty();
 
15402
    rollup.ref_pointer_arrays[i]= ref_array;
 
15403
    ref_array+= all_fields.elements;
 
15404
  }
 
15405
  for (i= 0 ; i < send_group_parts; i++)
 
15406
  {
 
15407
    for (j=0 ; j < fields_list.elements ; j++)
 
15408
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15409
  }
 
15410
  List_iterator<Item> it(all_fields);
 
15411
  Item *item;
 
15412
  while ((item= it++))
 
15413
  {
 
15414
    order_st *group_tmp;
 
15415
    bool found_in_group= 0;
 
15416
 
 
15417
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15418
    {
 
15419
      if (*group_tmp->item == item)
 
15420
      {
 
15421
        item->maybe_null= 1;
 
15422
        found_in_group= 1;
 
15423
        if (item->const_item())
 
15424
        {
 
15425
          /*
 
15426
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15427
            is wrapped up into an Item_func object yielding the same value
 
15428
            as the constant item. The objects of the wrapper class are never
 
15429
            considered as constant items and besides they inherit all
 
15430
            properties of the Item_result_field class.
 
15431
            This wrapping allows us to ensure writing constant items
 
15432
            into temporary tables whenever the result of the ROLLUP
 
15433
            operation has to be written into a temporary table, e.g. when
 
15434
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15435
            Usually when creating temporary tables for a intermidiate
 
15436
            result we do not include fields for constant expressions.
 
15437
          */           
 
15438
          Item* new_item= new Item_func_rollup_const(item);
 
15439
          if (!new_item)
 
15440
            return 1;
 
15441
          new_item->fix_fields(thd, (Item **) 0);
 
15442
          thd->change_item_tree(it.ref(), new_item);
 
15443
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
15444
          { 
 
15445
            if (*tmp->item == item)
 
15446
              thd->change_item_tree(tmp->item, new_item);
 
15447
          }
 
15448
        }
 
15449
      }
 
15450
    }
 
15451
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15452
    {
 
15453
      bool changed= false;
 
15454
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
15455
        return 1;
 
15456
      /*
 
15457
        We have to prevent creation of a field in a temporary table for
 
15458
        an expression that contains GROUP BY attributes.
 
15459
        Marking the expression item as 'with_sum_func' will ensure this.
 
15460
      */ 
 
15461
      if (changed)
 
15462
        item->with_sum_func= 1;
 
15463
    }
 
15464
  }
 
15465
  return 0;
 
15466
}
 
15467
  
 
15468
 
 
15469
/**
 
15470
  Fill up rollup structures with pointers to fields to use.
 
15471
 
 
15472
  Creates copies of item_sum items for each sum level.
 
15473
 
 
15474
  @param fields_arg             List of all fields (hidden and real ones)
 
15475
  @param sel_fields             Pointer to selected fields
 
15476
  @param func                   Store here a pointer to all fields
 
15477
 
 
15478
  @retval
 
15479
    0   if ok;
 
15480
    In this case func is pointing to next not used element.
 
15481
  @retval
 
15482
    1    on error
 
15483
*/
 
15484
 
 
15485
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15486
                              Item_sum ***func)
 
15487
{
 
15488
  List_iterator_fast<Item> it(fields_arg);
 
15489
  Item *first_field= sel_fields.head();
 
15490
  uint32_t level;
 
15491
 
 
15492
  /*
 
15493
    Create field lists for the different levels
 
15494
 
 
15495
    The idea here is to have a separate field list for each rollup level to
 
15496
    avoid all runtime checks of which columns should be NULL.
 
15497
 
 
15498
    The list is stored in reverse order to get sum function in such an order
 
15499
    in func that it makes it easy to reset them with init_sum_functions()
 
15500
 
 
15501
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15502
 
 
15503
    rollup.fields[0] will contain list where a,b,c is NULL
 
15504
    rollup.fields[1] will contain list where b,c is NULL
 
15505
    ...
 
15506
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15507
    ...
 
15508
    sum_funcs_end[0] points to all sum functions
 
15509
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15510
    ...
 
15511
  */
 
15512
 
 
15513
  for (level=0 ; level < send_group_parts ; level++)
 
15514
  {
 
15515
    uint32_t i;
 
15516
    uint32_t pos= send_group_parts - level -1;
 
15517
    bool real_fields= 0;
 
15518
    Item *item;
 
15519
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15520
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15521
    order_st *start_group;
 
15522
 
 
15523
    /* Point to first hidden field */
 
15524
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15525
 
 
15526
    /* Remember where the sum functions ends for the previous level */
 
15527
    sum_funcs_end[pos+1]= *func;
 
15528
 
 
15529
    /* Find the start of the group for this level */
 
15530
    for (i= 0, start_group= group_list ;
 
15531
         i++ < pos ;
 
15532
         start_group= start_group->next)
 
15533
      ;
 
15534
 
 
15535
    it.rewind();
 
15536
    while ((item= it++))
 
15537
    {
 
15538
      if (item == first_field)
 
15539
      {
 
15540
        real_fields= 1;                         // End of hidden fields
 
15541
        ref_array= ref_array_start;
 
15542
      }
 
15543
 
 
15544
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15545
          (!((Item_sum*) item)->depended_from() ||
 
15546
           ((Item_sum *)item)->depended_from() == select_lex))
 
15547
          
 
15548
      {
 
15549
        /*
 
15550
          This is a top level summary function that must be replaced with
 
15551
          a sum function that is reset for this level.
 
15552
 
 
15553
          NOTE: This code creates an object which is not that nice in a
 
15554
          sub select.  Fortunately it's not common to have rollup in
 
15555
          sub selects.
 
15556
        */
 
15557
        item= item->copy_or_same(thd);
 
15558
        ((Item_sum*) item)->make_unique();
 
15559
        *(*func)= (Item_sum*) item;
 
15560
        (*func)++;
 
15561
      }
 
15562
      else 
 
15563
      {
 
15564
        /* Check if this is something that is part of this group by */
 
15565
        order_st *group_tmp;
 
15566
        for (group_tmp= start_group, i= pos ;
 
15567
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15568
        {
 
15569
          if (*group_tmp->item == item)
 
15570
          {
 
15571
            /*
 
15572
              This is an element that is used by the GROUP BY and should be
 
15573
              set to NULL in this level
 
15574
            */
 
15575
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
15576
            if (!null_item)
 
15577
              return 1;
 
15578
            item->maybe_null= 1;                // Value will be null sometimes
 
15579
            null_item->result_field= item->get_tmp_table_field();
 
15580
            item= null_item;
 
15581
            break;
 
15582
          }
 
15583
        }
 
15584
      }
 
15585
      *ref_array= item;
 
15586
      if (real_fields)
 
15587
      {
 
15588
        (void) new_it++;                        // Point to next item
 
15589
        new_it.replace(item);                   // Replace previous
 
15590
        ref_array++;
 
15591
      }
 
15592
      else
 
15593
        ref_array--;
 
15594
    }
 
15595
  }
 
15596
  sum_funcs_end[0]= *func;                      // Point to last function
 
15597
  return 0;
 
15598
}
 
15599
 
 
15600
/**
 
15601
  Send all rollup levels higher than the current one to the client.
 
15602
 
 
15603
  @b SAMPLE
 
15604
    @code
 
15605
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15606
  @endcode
 
15607
 
 
15608
  @param idx            Level we are on:
 
15609
                        - 0 = Total sum level
 
15610
                        - 1 = First group changed  (a)
 
15611
                        - 2 = Second group changed (a,b)
 
15612
 
 
15613
  @retval
 
15614
    0   ok
 
15615
  @retval
 
15616
    1   If send_data_failed()
 
15617
*/
 
15618
 
 
15619
int JOIN::rollup_send_data(uint32_t idx)
 
15620
{
 
15621
  uint32_t i;
 
15622
  for (i= send_group_parts ; i-- > idx ; )
 
15623
  {
 
15624
    /* Get reference pointers to sum functions in place */
 
15625
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15626
           ref_pointer_array_size);
 
15627
    if ((!having || having->val_int()))
 
15628
    {
 
15629
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15630
          result->send_data(rollup.fields[i]))
 
15631
        return 1;
 
15632
      send_records++;
 
15633
    }
 
15634
  }
 
15635
  /* Restore ref_pointer_array */
 
15636
  set_items_ref_array(current_ref_pointer_array);
 
15637
  return 0;
 
15638
}
 
15639
 
 
15640
/**
 
15641
  Write all rollup levels higher than the current one to a temp table.
 
15642
 
 
15643
  @b SAMPLE
 
15644
    @code
 
15645
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15646
  @endcode
 
15647
 
 
15648
  @param idx                 Level we are on:
 
15649
                               - 0 = Total sum level
 
15650
                               - 1 = First group changed  (a)
 
15651
                               - 2 = Second group changed (a,b)
 
15652
  @param table               reference to temp table
 
15653
 
 
15654
  @retval
 
15655
    0   ok
 
15656
  @retval
 
15657
    1   if write_data_failed()
 
15658
*/
 
15659
 
 
15660
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
15661
{
 
15662
  uint32_t i;
 
15663
  for (i= send_group_parts ; i-- > idx ; )
 
15664
  {
 
15665
    /* Get reference pointers to sum functions in place */
 
15666
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15667
           ref_pointer_array_size);
 
15668
    if ((!having || having->val_int()))
 
15669
    {
 
15670
      int write_error;
 
15671
      Item *item;
 
15672
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15673
      while ((item= it++))
 
15674
      {
 
15675
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15676
          item->save_in_result_field(1);
 
15677
      }
 
15678
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15679
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15680
      {
 
15681
        if (create_myisam_from_heap(thd, table_arg, 
 
15682
                                    tmp_table_param.start_recinfo,
 
15683
                                    &tmp_table_param.recinfo,
 
15684
                                    write_error, 0))
 
15685
          return 1;                  
 
15686
      }
 
15687
    }
 
15688
  }
 
15689
  /* Restore ref_pointer_array */
 
15690
  set_items_ref_array(current_ref_pointer_array);
 
15691
  return 0;
 
15692
}
 
15693
 
 
15694
/**
 
15695
  clear results if there are not rows found for group
 
15696
  (end_send_group/end_write_group)
 
15697
*/
 
15698
 
 
15699
void JOIN::clear()
 
15700
{
 
15701
  clear_tables(this);
 
15702
  copy_fields(&tmp_table_param);
 
15703
 
 
15704
  if (sum_funcs)
 
15705
  {
 
15706
    Item_sum *func, **func_ptr= sum_funcs;
 
15707
    while ((func= *(func_ptr++)))
 
15708
      func->clear();
 
15709
  }
 
15710
}
 
15711
 
 
15712
/**
 
15713
  EXPLAIN handling.
 
15714
 
 
15715
  Send a description about what how the select will be done to stdout.
 
15716
*/
 
15717
 
 
15718
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
 
15719
                     bool distinct,const char *message)
 
15720
{
 
15721
  List<Item> field_list;
 
15722
  List<Item> item_list;
 
15723
  THD *thd=join->thd;
 
15724
  select_result *result=join->result;
 
15725
  Item *item_null= new Item_null();
 
15726
  const CHARSET_INFO * const cs= system_charset_info;
 
15727
  int quick_type;
 
15728
  /* Don't log this into the slow query log */
 
15729
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15730
  join->unit->offset_limit_cnt= 0;
 
15731
 
 
15732
  /* 
 
15733
    NOTE: the number/types of items pushed into item_list must be in sync with
 
15734
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
 
15735
  */
 
15736
  if (message)
 
15737
  {
 
15738
    item_list.push_back(new Item_int((int32_t)
 
15739
                                     join->select_lex->select_number));
 
15740
    item_list.push_back(new Item_string(join->select_lex->type,
 
15741
                                        strlen(join->select_lex->type), cs));
 
15742
    for (uint32_t i=0 ; i < 7; i++)
 
15743
      item_list.push_back(item_null);
 
15744
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15745
      item_list.push_back(item_null);
 
15746
  
 
15747
    item_list.push_back(new Item_string(message,strlen(message),cs));
 
15748
    if (result->send_data(item_list))
 
15749
      join->error= 1;
 
15750
  }
 
15751
  else if (join->select_lex == join->unit->fake_select_lex)
 
15752
  {
 
15753
    /* 
 
15754
      here we assume that the query will return at least two rows, so we
 
15755
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
 
15756
      and no filesort will be actually done, but executing all selects in
 
15757
      the UNION to provide precise EXPLAIN information will hardly be
 
15758
      appreciated :)
 
15759
    */
 
15760
    char table_name_buffer[NAME_LEN];
 
15761
    item_list.empty();
 
15762
    /* id */
 
15763
    item_list.push_back(new Item_null);
 
15764
    /* select_type */
 
15765
    item_list.push_back(new Item_string(join->select_lex->type,
 
15766
                                        strlen(join->select_lex->type),
 
15767
                                        cs));
 
15768
    /* table */
 
15769
    {
 
15770
      SELECT_LEX *sl= join->unit->first_select();
 
15771
      uint32_t len= 6, lastop= 0;
 
15772
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
 
15773
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
 
15774
      {
 
15775
        len+= lastop;
 
15776
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
 
15777
                         "%u,", sl->select_number);
 
15778
      }
 
15779
      if (sl || len + lastop >= NAME_LEN)
 
15780
      {
 
15781
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
 
15782
        len+= 4;
 
15783
      }
 
15784
      else
 
15785
      {
 
15786
        len+= lastop;
 
15787
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
 
15788
      }
 
15789
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15790
    }
 
15791
    /* type */
 
15792
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15793
                                          strlen(join_type_str[JT_ALL]),
 
15794
                                          cs));
 
15795
    /* possible_keys */
 
15796
    item_list.push_back(item_null);
 
15797
    /* key*/
 
15798
    item_list.push_back(item_null);
 
15799
    /* key_len */
 
15800
    item_list.push_back(item_null);
 
15801
    /* ref */
 
15802
    item_list.push_back(item_null);
 
15803
    /* in_rows */
 
15804
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15805
      item_list.push_back(item_null);
 
15806
    /* rows */
 
15807
    item_list.push_back(item_null);
 
15808
    /* extra */
 
15809
    if (join->unit->global_parameters->order_list.first)
 
15810
      item_list.push_back(new Item_string("Using filesort",
 
15811
                                          14, cs));
 
15812
    else
 
15813
      item_list.push_back(new Item_string("", 0, cs));
 
15814
 
 
15815
    if (result->send_data(item_list))
 
15816
      join->error= 1;
 
15817
  }
 
15818
  else
 
15819
  {
 
15820
    table_map used_tables=0;
 
15821
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15822
    {
 
15823
      JOIN_TAB *tab=join->join_tab+i;
 
15824
      Table *table=tab->table;
 
15825
      TableList *table_list= tab->table->pos_in_table_list;
 
15826
      char buff[512]; 
 
15827
      char buff1[512], buff2[512], buff3[512];
 
15828
      char keylen_str_buf[64];
 
15829
      String extra(buff, sizeof(buff),cs);
 
15830
      char table_name_buffer[NAME_LEN];
 
15831
      String tmp1(buff1,sizeof(buff1),cs);
 
15832
      String tmp2(buff2,sizeof(buff2),cs);
 
15833
      String tmp3(buff3,sizeof(buff3),cs);
 
15834
      extra.length(0);
 
15835
      tmp1.length(0);
 
15836
      tmp2.length(0);
 
15837
      tmp3.length(0);
 
15838
 
 
15839
      quick_type= -1;
 
15840
      item_list.empty();
 
15841
      /* id */
 
15842
      item_list.push_back(new Item_uint((uint32_t)
 
15843
                                       join->select_lex->select_number));
 
15844
      /* select_type */
 
15845
      item_list.push_back(new Item_string(join->select_lex->type,
 
15846
                                          strlen(join->select_lex->type),
 
15847
                                          cs));
 
15848
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
 
15849
      {
 
15850
        quick_type= tab->select->quick->get_type();
 
15851
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
 
15852
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
 
15853
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
 
15854
          tab->type = JT_INDEX_MERGE;
 
15855
        else
 
15856
          tab->type = JT_RANGE;
 
15857
      }
 
15858
      /* table */
 
15859
      if (table->derived_select_number)
 
15860
      {
 
15861
        /* Derived table name generation */
 
15862
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
 
15863
                          "<derived%u>",
 
15864
                          table->derived_select_number);
 
15865
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
 
15866
      }
 
15867
      else
 
15868
      {
 
15869
        TableList *real_table= table->pos_in_table_list; 
 
15870
        item_list.push_back(new Item_string(real_table->alias,
 
15871
                                            strlen(real_table->alias),
 
15872
                                            cs));
 
15873
      }
 
15874
      /* "type" column */
 
15875
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15876
                                          strlen(join_type_str[tab->type]),
 
15877
                                          cs));
 
15878
      /* Build "possible_keys" value and add it to item_list */
 
15879
      if (!tab->keys.is_clear_all())
 
15880
      {
 
15881
        uint32_t j;
 
15882
        for (j=0 ; j < table->s->keys ; j++)
 
15883
        {
 
15884
          if (tab->keys.is_set(j))
 
15885
          {
 
15886
            if (tmp1.length())
 
15887
              tmp1.append(',');
 
15888
            tmp1.append(table->key_info[j].name, 
 
15889
                        strlen(table->key_info[j].name),
 
15890
                        system_charset_info);
 
15891
          }
 
15892
        }
 
15893
      }
 
15894
      if (tmp1.length())
 
15895
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
 
15896
      else
 
15897
        item_list.push_back(item_null);
 
15898
 
 
15899
      /* Build "key", "key_len", and "ref" values and add them to item_list */
 
15900
      if (tab->ref.key_parts)
 
15901
      {
 
15902
        KEY *key_info=table->key_info+ tab->ref.key;
 
15903
        register uint32_t length;
 
15904
        item_list.push_back(new Item_string(key_info->name,
 
15905
                                            strlen(key_info->name),
 
15906
                                            system_charset_info));
 
15907
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
 
15908
                keylen_str_buf;
 
15909
        item_list.push_back(new Item_string(keylen_str_buf, length,
 
15910
                                            system_charset_info));
 
15911
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
15912
        {
 
15913
          if (tmp2.length())
 
15914
            tmp2.append(',');
 
15915
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
 
15916
                      system_charset_info);
 
15917
        }
 
15918
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15919
      }
 
15920
      else if (tab->type == JT_NEXT)
 
15921
      {
 
15922
        KEY *key_info=table->key_info+ tab->index;
 
15923
        register uint32_t length;
 
15924
        item_list.push_back(new Item_string(key_info->name,
 
15925
                                            strlen(key_info->name),cs));
 
15926
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
 
15927
                keylen_str_buf;
 
15928
        item_list.push_back(new Item_string(keylen_str_buf, 
 
15929
                                            length,
 
15930
                                            system_charset_info));
 
15931
        item_list.push_back(item_null);
 
15932
      }
 
15933
      else if (tab->select && tab->select->quick)
 
15934
      {
 
15935
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
 
15936
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15937
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
 
15938
        item_list.push_back(item_null);
 
15939
      }
 
15940
      else
 
15941
      {
 
15942
        if (table_list->schema_table && table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
15943
        {
 
15944
          const char *tmp_buff;
 
15945
          int f_idx;
 
15946
          if (table_list->has_db_lookup_value)
 
15947
          {
 
15948
            f_idx= table_list->schema_table->idx_field1;
 
15949
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15950
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15951
          }          
 
15952
          if (table_list->has_table_lookup_value)
 
15953
          {
 
15954
            if (table_list->has_db_lookup_value)
 
15955
              tmp2.append(',');
 
15956
            f_idx= table_list->schema_table->idx_field2;
 
15957
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
15958
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
15959
          }
 
15960
          if (tmp2.length())
 
15961
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
15962
          else
 
15963
            item_list.push_back(item_null);
 
15964
        }
 
15965
        else
 
15966
          item_list.push_back(item_null);
 
15967
        item_list.push_back(item_null);
 
15968
        item_list.push_back(item_null);
 
15969
      }
 
15970
      
 
15971
      /* Add "rows" field to item_list. */
 
15972
      if (table_list->schema_table)
 
15973
      {
 
15974
        /* in_rows */
 
15975
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15976
          item_list.push_back(item_null);
 
15977
        /* rows */
 
15978
        item_list.push_back(item_null);
 
15979
      }
 
15980
      else
 
15981
      {
 
15982
        double examined_rows;
 
15983
        if (tab->select && tab->select->quick)
 
15984
          examined_rows= rows2double(tab->select->quick->records);
 
15985
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
15986
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
15987
                                     tab->table->file->records());
 
15988
        else
 
15989
          examined_rows= join->best_positions[i].records_read; 
 
15990
 
 
15991
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
15992
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
15993
 
 
15994
        /* Add "filtered" field to item_list. */
 
15995
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15996
        {
 
15997
          float f= 0.0; 
 
15998
          if (examined_rows)
 
15999
            f= (float) (100.0 * join->best_positions[i].records_read /
 
16000
                        examined_rows);
 
16001
          item_list.push_back(new Item_float(f, 2));
 
16002
        }
 
16003
      }
 
16004
 
 
16005
      /* Build "Extra" field and add it to item_list. */
 
16006
      bool key_read=table->key_read;
 
16007
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
16008
          table->covering_keys.is_set(tab->index))
 
16009
        key_read=1;
 
16010
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
 
16011
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
 
16012
        key_read=1;
 
16013
        
 
16014
      if (tab->info)
 
16015
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
 
16016
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
 
16017
      {
 
16018
        if (tab->packed_info & TAB_INFO_USING_INDEX)
 
16019
          extra.append(STRING_WITH_LEN("; Using index"));
 
16020
        if (tab->packed_info & TAB_INFO_USING_WHERE)
 
16021
          extra.append(STRING_WITH_LEN("; Using where"));
 
16022
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
 
16023
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16024
        /* Skip initial "; "*/
 
16025
        const char *str= extra.ptr();
 
16026
        uint32_t len= extra.length();
 
16027
        if (len)
 
16028
        {
 
16029
          str += 2;
 
16030
          len -= 2;
 
16031
        }
 
16032
        item_list.push_back(new Item_string(str, len, cs));
 
16033
      }
 
16034
      else
 
16035
      {
 
16036
        uint32_t keyno= MAX_KEY;
 
16037
        if (tab->ref.key_parts)
 
16038
          keyno= tab->ref.key;
 
16039
        else if (tab->select && tab->select->quick)
 
16040
          keyno = tab->select->quick->index;
 
16041
 
 
16042
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16043
            table->file->pushed_idx_cond)
 
16044
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16045
 
 
16046
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
 
16047
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
 
16048
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
 
16049
        {
 
16050
          extra.append(STRING_WITH_LEN("; Using "));
 
16051
          tab->select->quick->add_info_string(&extra);
 
16052
        }
 
16053
          if (tab->select)
 
16054
        {
 
16055
          if (tab->use_quick == 2)
 
16056
          {
 
16057
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16058
            char buf[MAX_KEY / 4 + 1];
 
16059
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
16060
                                         "record (index map: 0x"));
 
16061
            extra.append(tab->keys.print(buf));
 
16062
            extra.append(')');
 
16063
          }
 
16064
          else if (tab->select->cond)
 
16065
          {
 
16066
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16067
 
 
16068
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
16069
            {
 
16070
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16071
                                           "condition"));
 
16072
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
16073
              {
 
16074
                extra.append(STRING_WITH_LEN(": "));
 
16075
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16076
              }
 
16077
            }
 
16078
            else
 
16079
              extra.append(STRING_WITH_LEN("; Using where"));
 
16080
          }
 
16081
        }
 
16082
        if (key_read)
 
16083
        {
 
16084
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
 
16085
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
 
16086
          else
 
16087
            extra.append(STRING_WITH_LEN("; Using index"));
 
16088
        }
 
16089
        if (table->reginfo.not_exists_optimize)
 
16090
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16091
          
 
16092
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16093
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16094
             HA_MRR_USE_DEFAULT_IMPL))
 
16095
        {
 
16096
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16097
        }
 
16098
 
 
16099
        if (table_list->schema_table &&
 
16100
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16101
        {
 
16102
          if (!table_list->table_open_method)
 
16103
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16104
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16105
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16106
          else
 
16107
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16108
          if (table_list->has_db_lookup_value &&
 
16109
              table_list->has_table_lookup_value)
 
16110
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16111
          else if (table_list->has_db_lookup_value ||
 
16112
                   table_list->has_table_lookup_value)
 
16113
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16114
          else
 
16115
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16116
        }
 
16117
        if (need_tmp_table)
 
16118
        {
 
16119
          need_tmp_table=0;
 
16120
          extra.append(STRING_WITH_LEN("; Using temporary"));
 
16121
        }
 
16122
        if (need_order)
 
16123
        {
 
16124
          need_order=0;
 
16125
          extra.append(STRING_WITH_LEN("; Using filesort"));
 
16126
        }
 
16127
        if (distinct & test_all_bits(used_tables,thd->used_tables))
 
16128
          extra.append(STRING_WITH_LEN("; Distinct"));
 
16129
 
 
16130
        if (tab->insideout_match_tab)
 
16131
        {
 
16132
          extra.append(STRING_WITH_LEN("; LooseScan"));
 
16133
        }
 
16134
 
 
16135
        if (tab->flush_weedout_table)
 
16136
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16137
        else if (tab->check_weed_out_table)
 
16138
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16139
        else if (tab->do_firstmatch)
 
16140
        {
 
16141
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16142
          Table *prev_table=tab->do_firstmatch->table;
 
16143
          if (prev_table->derived_select_number)
 
16144
          {
 
16145
            char namebuf[NAME_LEN];
 
16146
            /* Derived table name generation */
 
16147
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16148
                              "<derived%u>",
 
16149
                              prev_table->derived_select_number);
 
16150
            extra.append(namebuf, len);
 
16151
          }
 
16152
          else
 
16153
            extra.append(prev_table->pos_in_table_list->alias);
 
16154
          extra.append(STRING_WITH_LEN(")"));
 
16155
        }
 
16156
 
 
16157
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16158
        {
 
16159
          if (tab->ref.cond_guards[part])
 
16160
          {
 
16161
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
16162
            break;
 
16163
          }
 
16164
        }
 
16165
 
 
16166
        if (i > 0 && tab[-1].next_select == sub_select_cache)
 
16167
          extra.append(STRING_WITH_LEN("; Using join buffer"));
 
16168
 
 
16169
        /* Skip initial "; "*/
 
16170
        const char *str= extra.ptr();
 
16171
        uint32_t len= extra.length();
 
16172
        if (len)
 
16173
        {
 
16174
          str += 2;
 
16175
          len -= 2;
 
16176
        }
 
16177
        item_list.push_back(new Item_string(str, len, cs));
 
16178
      }
 
16179
      // For next iteration
 
16180
      used_tables|=table->map;
 
16181
      if (result->send_data(item_list))
 
16182
        join->error= 1;
 
16183
    }
 
16184
  }
 
16185
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
16186
       unit;
 
16187
       unit= unit->next_unit())
 
16188
  {
 
16189
    if (mysql_explain_union(thd, unit, result))
 
16190
      return;
 
16191
  }
 
16192
  return;
 
16193
}
 
16194
 
 
16195
 
 
16196
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
 
16197
{
 
16198
  bool res= 0;
 
16199
  SELECT_LEX *first= unit->first_select();
 
16200
 
 
16201
  for (SELECT_LEX *sl= first;
 
16202
       sl;
 
16203
       sl= sl->next_select())
 
16204
  {
 
16205
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
 
16206
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
 
16207
    sl->type= (((&thd->lex->select_lex)==sl)?
 
16208
               (sl->first_inner_unit() || sl->next_select() ? 
 
16209
                "PRIMARY" : "SIMPLE"):
 
16210
               ((sl == first)?
 
16211
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
 
16212
                 "DERIVED":
 
16213
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16214
                  "DEPENDENT SUBQUERY":
 
16215
                  (uncacheable?"UNCACHEABLE SUBQUERY":
 
16216
                   "SUBQUERY"))):
 
16217
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
 
16218
                 "DEPENDENT UNION":
 
16219
                 uncacheable?"UNCACHEABLE UNION":
 
16220
                 "UNION")));
 
16221
    sl->options|= SELECT_DESCRIBE;
 
16222
  }
 
16223
  if (unit->is_union())
 
16224
  {
 
16225
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
 
16226
    unit->fake_select_lex->type= "UNION RESULT";
 
16227
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
 
16228
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16229
      res= unit->exec();
 
16230
    res|= unit->cleanup();
 
16231
  }
 
16232
  else
 
16233
  {
 
16234
    thd->lex->current_select= first;
 
16235
    unit->set_limit(unit->global_parameters);
 
16236
    res= mysql_select(thd, &first->ref_pointer_array,
 
16237
                        (TableList*) first->table_list.first,
 
16238
                        first->with_wild, first->item_list,
 
16239
                        first->where,
 
16240
                        first->order_list.elements +
 
16241
                        first->group_list.elements,
 
16242
                        (order_st*) first->order_list.first,
 
16243
                        (order_st*) first->group_list.first,
 
16244
                        first->having,
 
16245
                        (order_st*) thd->lex->proc_list.first,
 
16246
                        first->options | thd->options | SELECT_DESCRIBE,
 
16247
                        result, unit, first);
 
16248
  }
 
16249
  return(res || thd->is_error());
 
16250
}
 
16251
 
 
16252
 
 
16253
static void print_table_array(THD *thd, String *str, TableList **table, 
6306
16254
                              TableList **end)
6307
16255
{
6308
 
  (*table)->print(session, str, QT_ORDINARY);
 
16256
  (*table)->print(thd, str, QT_ORDINARY);
6309
16257
 
6310
16258
  for (TableList **tbl= table + 1; tbl < end; tbl++)
6311
16259
  {
6317
16265
    }
6318
16266
    else if (curr->straight)
6319
16267
      str->append(STRING_WITH_LEN(" straight_join "));
 
16268
    else if (curr->sj_inner_tables)
 
16269
      str->append(STRING_WITH_LEN(" semi join "));
6320
16270
    else
6321
16271
      str->append(STRING_WITH_LEN(" join "));
6322
 
    curr->print(session, str, QT_ORDINARY);
 
16272
    curr->print(thd, str, QT_ORDINARY);
6323
16273
    if (curr->on_expr)
6324
16274
    {
6325
16275
      str->append(STRING_WITH_LEN(" on("));
6329
16279
  }
6330
16280
}
6331
16281
 
 
16282
 
6332
16283
/**
6333
16284
  Print joins from the FROM clause.
6334
 
  @param session     thread Cursor
 
16285
  @param thd     thread handler
6335
16286
  @param str     string where table should be printed
6336
16287
  @param tables  list of tables in join
6337
16288
  @query_type    type of the query is being generated
6338
16289
*/
6339
 
void print_join(Session *session, String *str,
6340
 
                List<TableList> *tables, enum_query_type)
 
16290
 
 
16291
static void print_join(THD *thd,
 
16292
                       String *str,
 
16293
                       List<TableList> *tables,
 
16294
                       enum_query_type query_type __attribute__((unused)))
6341
16295
{
6342
16296
  /* List is reversed => we should reverse it before using */
6343
16297
  List_iterator_fast<TableList> ti(*tables);
6344
 
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
 
16298
  TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
6345
16299
                                                tables->elements);
6346
16300
  if (table == 0)
6347
16301
    return;  // out of memory
6348
16302
 
6349
16303
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
6350
16304
    *t= ti++;
 
16305
  
 
16306
  /* 
 
16307
    If the first table is a semi-join nest, swap it with something that is
 
16308
    not a semi-join nest.
 
16309
  */
 
16310
  if ((*table)->sj_inner_tables)
 
16311
  {
 
16312
    TableList **end= table + tables->elements;
 
16313
    for (TableList **t2= table; t2!=end; t2++)
 
16314
    {
 
16315
      if (!(*t2)->sj_inner_tables)
 
16316
      {
 
16317
        TableList *tmp= *t2;
 
16318
        *t2= *table;
 
16319
        *table= tmp;
 
16320
        break;
 
16321
      }
 
16322
    }
 
16323
  }
6351
16324
  assert(tables->elements >= 1);
6352
 
  print_table_array(session, str, table, table + tables->elements);
6353
 
}
6354
 
 
6355
 
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6356
 
{
6357
 
  /* QQ: session may not be set for sub queries, but this should be fixed */
6358
 
  if(not session)
6359
 
    session= current_session;
6360
 
 
 
16325
  print_table_array(thd, str, table, table + tables->elements);
 
16326
}
 
16327
 
 
16328
 
 
16329
/**
 
16330
  @brief Print an index hint
 
16331
 
 
16332
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16333
 
 
16334
  @param      thd         the current thread
 
16335
  @param[out] str         appends the index hint here
 
16336
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16337
                          "FORCE INDEX"|"IGNORE INDEX")
 
16338
  @param      hint_length the length of the string in 'hint'
 
16339
  @param      indexes     a list of index names for the hint
 
16340
*/
 
16341
 
 
16342
void 
 
16343
Index_hint::print(THD *thd, String *str)
 
16344
{
 
16345
  switch (type)
 
16346
  {
 
16347
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16348
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16349
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16350
  }
 
16351
  str->append (STRING_WITH_LEN(" ("));
 
16352
  if (key_name.length)
 
16353
  {
 
16354
    if (thd && !my_strnncoll(system_charset_info,
 
16355
                             (const unsigned char *)key_name.str, key_name.length, 
 
16356
                             (const unsigned char *)primary_key_name, 
 
16357
                             strlen(primary_key_name)))
 
16358
      str->append(primary_key_name);
 
16359
    else
 
16360
      append_identifier(thd, str, key_name.str, key_name.length);
 
16361
  }
 
16362
  str->append(')');
 
16363
}
 
16364
 
 
16365
 
 
16366
/**
 
16367
  Print table as it should be in join list.
 
16368
 
 
16369
  @param str   string where table should be printed
 
16370
*/
 
16371
 
 
16372
void TableList::print(THD *thd, String *str, enum_query_type query_type)
 
16373
{
 
16374
  if (nested_join)
 
16375
  {
 
16376
    str->append('(');
 
16377
    print_join(thd, str, &nested_join->join_list, query_type);
 
16378
    str->append(')');
 
16379
  }
 
16380
  else
 
16381
  {
 
16382
    const char *cmp_name;                         // Name to compare with alias
 
16383
    if (derived)
 
16384
    {
 
16385
      // A derived table
 
16386
      str->append('(');
 
16387
      derived->print(str, query_type);
 
16388
      str->append(')');
 
16389
      cmp_name= "";                               // Force printing of alias
 
16390
    }
 
16391
    else
 
16392
    {
 
16393
      // A normal table
 
16394
      {
 
16395
        append_identifier(thd, str, db, db_length);
 
16396
        str->append('.');
 
16397
      }
 
16398
      if (schema_table)
 
16399
      {
 
16400
        append_identifier(thd, str, schema_table_name,
 
16401
                          strlen(schema_table_name));
 
16402
        cmp_name= schema_table_name;
 
16403
      }
 
16404
      else
 
16405
      {
 
16406
        append_identifier(thd, str, table_name, table_name_length);
 
16407
        cmp_name= table_name;
 
16408
      }
 
16409
    }
 
16410
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16411
    {
 
16412
      char t_alias_buff[MAX_ALIAS_NAME];
 
16413
      const char *t_alias= alias;
 
16414
 
 
16415
      str->append(' ');
 
16416
      if (lower_case_table_names== 1)
 
16417
      {
 
16418
        if (alias && alias[0])
 
16419
        {
 
16420
          my_stpcpy(t_alias_buff, alias);
 
16421
          my_casedn_str(files_charset_info, t_alias_buff);
 
16422
          t_alias= t_alias_buff;
 
16423
        }
 
16424
      }
 
16425
 
 
16426
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
16427
    }
 
16428
 
 
16429
    if (index_hints)
 
16430
    {
 
16431
      List_iterator<Index_hint> it(*index_hints);
 
16432
      Index_hint *hint;
 
16433
 
 
16434
      while ((hint= it++))
 
16435
      {
 
16436
        str->append (STRING_WITH_LEN(" "));
 
16437
        hint->print (thd, str);
 
16438
      }
 
16439
    }
 
16440
  }
 
16441
}
 
16442
 
 
16443
 
 
16444
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
16445
{
 
16446
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
16447
  if (!thd)
 
16448
    thd= current_thd;
6361
16449
 
6362
16450
  str->append(STRING_WITH_LEN("select "));
6363
16451
 
6364
16452
  /* First add options */
6365
16453
  if (options & SELECT_STRAIGHT_JOIN)
6366
16454
    str->append(STRING_WITH_LEN("straight_join "));
 
16455
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16456
      (this == &thd->lex->select_lex))
 
16457
    str->append(STRING_WITH_LEN("high_priority "));
6367
16458
  if (options & SELECT_DISTINCT)
6368
16459
    str->append(STRING_WITH_LEN("distinct "));
6369
16460
  if (options & SELECT_SMALL_RESULT)
6396
16487
  {
6397
16488
    str->append(STRING_WITH_LEN(" from "));
6398
16489
    /* go through join tree */
6399
 
    print_join(session, str, &top_join_list, query_type);
 
16490
    print_join(thd, str, &top_join_list, query_type);
6400
16491
  }
6401
16492
  else if (where)
6402
16493
  {
6403
16494
    /*
6404
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
16495
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
6405
16496
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
6406
16497
    */
6407
16498
    str->append(STRING_WITH_LEN(" from DUAL "));
6424
16515
  if (group_list.elements)
6425
16516
  {
6426
16517
    str->append(STRING_WITH_LEN(" group by "));
6427
 
    print_order(str, (Order *) group_list.first, query_type);
 
16518
    print_order(str, (order_st *) group_list.first, query_type);
6428
16519
    switch (olap)
6429
16520
    {
6430
16521
      case CUBE_TYPE:
6455
16546
  if (order_list.elements)
6456
16547
  {
6457
16548
    str->append(STRING_WITH_LEN(" order by "));
6458
 
    print_order(str, (Order *) order_list.first, query_type);
 
16549
    print_order(str, (order_st *) order_list.first, query_type);
6459
16550
  }
6460
16551
 
6461
16552
  // limit
6462
 
  print_limit(session, str, query_type);
 
16553
  print_limit(thd, str, query_type);
6463
16554
 
6464
16555
  // PROCEDURE unsupported here
6465
16556
}
6466
16557
 
 
16558
 
 
16559
/**
 
16560
  change select_result object of JOIN.
 
16561
 
 
16562
  @param res            new select_result object
 
16563
 
 
16564
  @retval
 
16565
    false   OK
 
16566
  @retval
 
16567
    true    error
 
16568
*/
 
16569
 
 
16570
bool JOIN::change_result(select_result *res)
 
16571
{
 
16572
  result= res;
 
16573
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
16574
                     result->prepare2())
 
16575
  {
 
16576
    return(true);
 
16577
  }
 
16578
  return(false);
 
16579
}
 
16580
 
6467
16581
/**
6468
16582
  @} (end of group Query_Optimizer)
6469
16583
*/
6470
 
 
6471
 
} /* namespace drizzled */