~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2008-08-16 15:41:14 UTC
  • mto: This revision was merged to the branch mainline in revision 346.
  • Revision ID: brian@tangent.org-20080816154114-eufmwf31p6ie1nd6
Cleaned up depend in Proto utils. Modified int to bool. Put TmpTable class
into play.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "drizzled/server_includes.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
 
#include "drizzled/table_map_iterator.h"
34
 
 
35
 
#include "drizzled/error.h"
36
 
#include "drizzled/gettext.h"
37
 
#include "drizzled/util/test.h"
38
 
#include "drizzled/name_resolution_context_state.h"
39
 
#include "drizzled/nested_join.h"
40
 
#include "drizzled/probes.h"
41
 
#include "drizzled/show.h"
42
 
#include "drizzled/plugin/info_schema_table.h"
43
 
#include "drizzled/item/cache.h"
44
 
#include "drizzled/item/cmpfunc.h"
45
 
#include "drizzled/item/copy_string.h"
46
 
#include "drizzled/item/uint.h"
47
 
#include "drizzled/cached_item.h"
48
 
#include "drizzled/sql_base.h"
49
 
#include "drizzled/field/blob.h"
50
 
#include "drizzled/check_stack_overrun.h"
51
 
#include "drizzled/lock.h"
52
 
#include "drizzled/item/outer_ref.h"
53
 
#include "drizzled/index_hint.h"
54
 
#include "drizzled/memory/multi_malloc.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
 
 
62
 
using namespace std;
63
 
using namespace drizzled;
64
 
 
65
 
static const string access_method_str[]=
66
 
{
67
 
  "UNKNOWN",
68
 
  "system",
69
 
  "const",
70
 
  "eq_ref",
71
 
  "ref",
72
 
  "MAYBE_REF",
73
 
  "ALL",
74
 
  "range",
75
 
  "index",
76
 
  "ref_or_null",
77
 
  "unique_subquery",
78
 
  "index_subquery",
79
 
  "index_merge"
 
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
 
 
33
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
 
34
                              "MAYBE_REF","ALL","range","index",
 
35
                              "ref_or_null","unique_subquery","index_subquery",
 
36
                              "index_merge"
80
37
};
81
38
 
82
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
83
 
static COND *build_equal_items(Session *session, COND *cond,
 
39
struct st_sargable_param;
 
40
 
 
41
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
 
42
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
 
43
                                 DYNAMIC_ARRAY *keyuse);
 
44
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
 
45
                                JOIN_TAB *join_tab,
 
46
                                uint tables, COND *conds,
 
47
                                COND_EQUAL *cond_equal,
 
48
                                table_map table_map, SELECT_LEX *select_lex,
 
49
                                st_sargable_param **sargables);
 
50
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
 
51
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
 
52
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
53
                               table_map used_tables);
 
54
static bool choose_plan(JOIN *join,table_map join_tables);
 
55
 
 
56
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
 
57
                             table_map remaining_tables, uint idx,
 
58
                             double record_count, double read_time);
 
59
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
60
static bool greedy_search(JOIN *join, table_map remaining_tables,
 
61
                             uint depth, uint prune_level);
 
62
static bool best_extension_by_limited_search(JOIN *join,
 
63
                                             table_map remaining_tables,
 
64
                                             uint idx, double record_count,
 
65
                                             double read_time, uint depth,
 
66
                                             uint prune_level);
 
67
static uint determine_search_depth(JOIN* join);
 
68
static int join_tab_cmp(const void* ptr1, const void* ptr2);
 
69
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
 
70
/*
 
71
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
72
  tested and approved.
 
73
*/
 
74
static bool find_best(JOIN *join,table_map rest_tables,uint index,
 
75
                      double record_count,double read_time);
 
76
static uint cache_record_length(JOIN *join,uint index);
 
77
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
 
78
static bool get_best_combination(JOIN *join);
 
79
static store_key *get_store_key(THD *thd,
 
80
                                KEYUSE *keyuse, table_map used_tables,
 
81
                                KEY_PART_INFO *key_part, uchar *key_buff,
 
82
                                uint maybe_null);
 
83
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
 
84
static void make_outerjoin_info(JOIN *join);
 
85
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
86
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
 
87
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
 
88
static void update_depend_map(JOIN *join);
 
89
static void update_depend_map(JOIN *join, ORDER *order);
 
90
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
 
91
                           bool change_list, bool *simple_order);
 
92
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
 
93
                            List<Item> &fields, bool send_row,
 
94
                            uint64_t select_options, const char *info,
 
95
                            Item *having);
 
96
static COND *build_equal_items(THD *thd, COND *cond,
84
97
                               COND_EQUAL *inherited,
85
 
                               List<TableList> *join_list,
 
98
                               List<TABLE_LIST> *join_list,
86
99
                               COND_EQUAL **cond_equal_ref);
87
 
 
88
 
static Item* part_of_refkey(Table *form,Field *field);
89
 
static bool cmp_buffer_with_ref(JoinTable *tab);
90
 
static void change_cond_ref_to_const(Session *session,
91
 
                                     vector<COND_CMP>& save_list,
92
 
                                     Item *and_father,
93
 
                                     Item *cond,
94
 
                                     Item *field,
95
 
                                     Item *value);
96
 
static bool copy_blobs(Field **ptr);
97
 
 
98
 
static bool eval_const_cond(COND *cond)
99
 
{
100
 
    return ((Item_func*) cond)->val_int() ? true : false;
101
 
}
 
100
static COND* substitute_for_best_equal_field(COND *cond,
 
101
                                             COND_EQUAL *cond_equal,
 
102
                                             void *table_join_idx);
 
103
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
 
104
                            COND *conds, bool top, bool in_sj);
 
105
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
 
106
static void restore_prev_nj_state(JOIN_TAB *last);
 
107
static void reset_nj_counters(List<TABLE_LIST> *join_list);
 
108
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
 
109
                                          uint first_unused);
 
110
 
 
111
static 
 
112
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
 
113
static void restore_prev_sj_state(const table_map remaining_tables, 
 
114
                                  const JOIN_TAB *tab);
 
115
 
 
116
static COND *optimize_cond(JOIN *join, COND *conds,
 
117
                           List<TABLE_LIST> *join_list,
 
118
                           Item::cond_result *cond_value);
 
119
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
 
120
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
 
121
 
 
122
static enum_nested_loop_state
 
123
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
124
                     int error);
 
125
static enum_nested_loop_state
 
126
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 
127
static enum_nested_loop_state
 
128
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 
129
static enum_nested_loop_state
 
130
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
131
static enum_nested_loop_state
 
132
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
133
static enum_nested_loop_state
 
134
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
135
static enum_nested_loop_state
 
136
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
 
137
 
 
138
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
 
139
static int join_read_system(JOIN_TAB *tab);
 
140
static int join_read_const(JOIN_TAB *tab);
 
141
static int join_read_key(JOIN_TAB *tab);
 
142
static int join_read_always_key(JOIN_TAB *tab);
 
143
static int join_read_last_key(JOIN_TAB *tab);
 
144
static int join_no_more_records(READ_RECORD *info);
 
145
static int join_read_next(READ_RECORD *info);
 
146
static int join_read_next_different(READ_RECORD *info);
 
147
static int join_init_quick_read_record(JOIN_TAB *tab);
 
148
static int test_if_quick_select(JOIN_TAB *tab);
 
149
static int join_init_read_record(JOIN_TAB *tab);
 
150
static int join_read_first(JOIN_TAB *tab);
 
151
static int join_read_next_same(READ_RECORD *info);
 
152
static int join_read_next_same_diff(READ_RECORD *info);
 
153
static int join_read_last(JOIN_TAB *tab);
 
154
static int join_read_prev_same(READ_RECORD *info);
 
155
static int join_read_prev(READ_RECORD *info);
 
156
int join_read_always_key_or_null(JOIN_TAB *tab);
 
157
int join_read_next_same_or_null(READ_RECORD *info);
 
158
static COND *make_cond_for_table(COND *cond,table_map table,
 
159
                                 table_map used_table,
 
160
                                 bool exclude_expensive_cond);
 
161
static Item* part_of_refkey(TABLE *form,Field *field);
 
162
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
 
163
                                    ha_rows select_limit, bool no_changes,
 
164
                                    const key_map *map);
 
165
static bool list_contains_unique_index(TABLE *table,
 
166
                          bool (*find_func) (Field *, void *), void *data);
 
167
static bool find_field_in_item_list (Field *field, void *data);
 
168
static bool find_field_in_order_list (Field *field, void *data);
 
169
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
170
                             ha_rows filesort_limit, ha_rows select_limit,
 
171
                             bool is_order_by);
 
172
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
 
173
                             Item *having);
 
174
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
 
175
                                   ulong offset,Item *having);
 
176
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
 
177
                                      uint field_count, Field **first_field,
 
178
 
 
179
                                      ulong key_length,Item *having);
 
180
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
 
181
static ulong used_blob_length(CACHE_FIELD **ptr);
 
182
static bool store_record_in_cache(JOIN_CACHE *cache);
 
183
static void reset_cache_read(JOIN_CACHE *cache);
 
184
static void reset_cache_write(JOIN_CACHE *cache);
 
185
static void read_cached_record(JOIN_TAB *tab);
 
186
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
187
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
 
188
                                    ORDER *order, List<Item> &fields,
 
189
                                    List<Item> &all_fields,
 
190
                                    bool *all_order_by_fields_used);
 
191
static bool test_if_subpart(ORDER *a,ORDER *b);
 
192
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
 
193
static void calc_group_buffer(JOIN *join,ORDER *group);
 
194
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
195
static bool alloc_group_fields(JOIN *join,ORDER *group);
 
196
// Create list for using with tempory table
 
197
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
198
                                     List<Item> &new_list1,
 
199
                                     List<Item> &new_list2,
 
200
                                     uint elements, List<Item> &items);
 
201
// Create list for using with tempory table
 
202
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
203
                                      List<Item> &new_list1,
 
204
                                      List<Item> &new_list2,
 
205
                                      uint elements, List<Item> &items);
 
206
static void init_tmptable_sum_functions(Item_sum **func);
 
207
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
 
208
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
 
209
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
 
210
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
 
211
static bool init_sum_functions(Item_sum **func, Item_sum **end);
 
212
static bool update_sum_func(Item_sum **func);
 
213
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
 
214
                            bool distinct, const char *message=NullS);
 
215
static Item *remove_additional_cond(Item* conds);
 
216
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
 
217
static bool test_if_ref(Item_field *left_item,Item *right_item);
 
218
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
219
                                       Item *new_cond, bool fix_fields);
102
220
 
103
221
/*
104
222
  This is used to mark equalities that were made from i-th IN-equality.
108
226
const char *subq_sj_cond_name=
109
227
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
110
228
 
111
 
static bool copy_blobs(Field **ptr)
 
229
static bool bitmap_covers(const table_map x, const table_map y)
112
230
{
113
 
  for (; *ptr ; ptr++)
114
 
  {
115
 
    if ((*ptr)->flags & BLOB_FLAG)
116
 
      if (((Field_blob *) (*ptr))->copy())
117
 
        return 1;                               // Error
118
 
  }
119
 
  return 0;
 
231
  return !test(y & ~x);
120
232
}
121
233
 
122
234
/**
123
235
  This handles SELECT with and without UNION.
124
236
*/
125
 
bool handle_select(Session *session, LEX *lex, select_result *result,
126
 
                   uint64_t setup_tables_done_option)
 
237
 
 
238
bool handle_select(THD *thd, LEX *lex, select_result *result,
 
239
                   ulong setup_tables_done_option)
127
240
{
128
241
  bool res;
129
 
  register Select_Lex *select_lex= &lex->select_lex;
130
 
  DRIZZLE_SELECT_START(session->query);
 
242
  register SELECT_LEX *select_lex = &lex->select_lex;
 
243
  DRIZZLE_SELECT_START();
131
244
 
132
 
  if (select_lex->master_unit()->is_union() ||
 
245
  if (select_lex->master_unit()->is_union() || 
133
246
      select_lex->master_unit()->fake_select_lex)
134
 
    res= drizzle_union(session, lex, result, &lex->unit,
135
 
                       setup_tables_done_option);
 
247
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
136
248
  else
137
249
  {
138
 
    Select_Lex_Unit *unit= &lex->unit;
 
250
    SELECT_LEX_UNIT *unit= &lex->unit;
139
251
    unit->set_limit(unit->global_parameters);
140
 
    session->session_marker= 0;
 
252
    thd->thd_marker= 0;
141
253
    /*
142
254
      'options' of mysql_select will be set in JOIN, as far as JOIN for
143
 
      every PS/SP execution new, we will not need reset this flag if
 
255
      every PS/SP execution new, we will not need reset this flag if 
144
256
      setup_tables_done_option changed for next rexecution
145
257
    */
146
 
    res= mysql_select(session, &select_lex->ref_pointer_array,
147
 
                      (TableList*) select_lex->table_list.first,
 
258
    res= mysql_select(thd, &select_lex->ref_pointer_array,
 
259
                      (TABLE_LIST*) select_lex->table_list.first,
148
260
                      select_lex->with_wild, select_lex->item_list,
149
261
                      select_lex->where,
150
262
                      select_lex->order_list.elements +
151
263
                      select_lex->group_list.elements,
152
 
                      (order_st*) select_lex->order_list.first,
153
 
                      (order_st*) select_lex->group_list.first,
 
264
                      (ORDER*) select_lex->order_list.first,
 
265
                      (ORDER*) select_lex->group_list.first,
154
266
                      select_lex->having,
155
 
                      select_lex->options | session->options |
 
267
                      (ORDER*) lex->proc_list.first,
 
268
                      select_lex->options | thd->options |
156
269
                      setup_tables_done_option,
157
270
                      result, unit, select_lex);
158
271
  }
159
 
  res|= session->is_error();
 
272
  res|= thd->is_error();
160
273
  if (unlikely(res))
161
274
    result->abort();
162
275
 
163
 
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
164
 
  return res;
 
276
  DRIZZLE_SELECT_END();
 
277
  return(res);
165
278
}
166
279
 
 
280
 
167
281
/*
168
282
  Fix fields referenced from inner selects.
169
283
 
170
284
  SYNOPSIS
171
285
    fix_inner_refs()
172
 
    session               Thread handle
 
286
    thd               Thread handle
173
287
    all_fields        List of all fields used in select
174
288
    select            Current select
175
289
    ref_pointer_array Array of references to Items used in current select
204
318
    true  an error occured
205
319
    false ok
206
320
*/
207
 
bool fix_inner_refs(Session *session, 
208
 
                    List<Item> &all_fields, 
209
 
                    Select_Lex *select, 
210
 
                    Item **ref_pointer_array)
 
321
 
 
322
bool
 
323
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
 
324
                 Item **ref_pointer_array)
211
325
{
212
326
  Item_outer_ref *ref;
213
327
  bool res= false;
267
381
    ref->outer_ref= new_ref;
268
382
    ref->ref= &ref->outer_ref;
269
383
 
270
 
    if (!ref->fixed && ref->fix_fields(session, 0))
 
384
    if (!ref->fixed && ref->fix_fields(thd, 0))
271
385
      return true;
272
 
    session->used_tables|= item->used_tables();
 
386
    thd->used_tables|= item->used_tables();
273
387
  }
274
388
  return res;
275
389
}
276
390
 
 
391
#define MAGIC_IN_WHERE_TOP_LEVEL 10
 
392
/**
 
393
  Function to setup clauses without sum functions.
 
394
*/
 
395
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
 
396
                               TABLE_LIST *tables,
 
397
                               TABLE_LIST *leaves,
 
398
                               List<Item> &fields,
 
399
                               List<Item> &all_fields,
 
400
                               COND **conds,
 
401
                               ORDER *order,
 
402
                               ORDER *group, bool *hidden_group_fields)
 
403
{
 
404
  int res;
 
405
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
 
406
 
 
407
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
408
  res= setup_conds(thd, tables, leaves, conds);
 
409
 
 
410
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
 
411
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
 
412
                          order);
 
413
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
 
414
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
 
415
                          group, hidden_group_fields);
 
416
  thd->lex->allow_sum_func= save_allow_sum_func;
 
417
  return(res);
 
418
}
 
419
 
277
420
/*****************************************************************************
278
421
  Check fields, find best join, do the select and output fields.
279
422
  mysql_select assumes that all tables are already opened
280
423
*****************************************************************************/
281
424
 
 
425
/**
 
426
  Prepare of whole select (including sub queries in future).
 
427
 
 
428
  @todo
 
429
    Add check of calculation of GROUP functions and fields:
 
430
    SELECT COUNT(*)+table.col1 from table1;
 
431
 
 
432
  @retval
 
433
    -1   on error
 
434
  @retval
 
435
    0   on success
 
436
*/
 
437
int
 
438
JOIN::prepare(Item ***rref_pointer_array,
 
439
              TABLE_LIST *tables_init,
 
440
              uint wild_num, COND *conds_init, uint og_num,
 
441
              ORDER *order_init, ORDER *group_init,
 
442
              Item *having_init,
 
443
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
 
444
              SELECT_LEX_UNIT *unit_arg)
 
445
{
 
446
  // to prevent double initialization on EXPLAIN
 
447
  if (optimized)
 
448
    return(0);
 
449
 
 
450
  conds= conds_init;
 
451
  order= order_init;
 
452
  group_list= group_init;
 
453
  having= having_init;
 
454
  proc_param= proc_param_init;
 
455
  tables_list= tables_init;
 
456
  select_lex= select_lex_arg;
 
457
  select_lex->join= this;
 
458
  join_list= &select_lex->top_join_list;
 
459
  union_part= unit_arg->is_union();
 
460
 
 
461
  thd->lex->current_select->is_item_list_lookup= 1;
 
462
  /*
 
463
    If we have already executed SELECT, then it have not sense to prevent
 
464
    its table from update (see unique_table())
 
465
  */
 
466
  if (thd->derived_tables_processing)
 
467
    select_lex->exclude_from_table_unique_test= true;
 
468
 
 
469
  /* Check that all tables, fields, conds and order are ok */
 
470
 
 
471
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
472
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
 
473
                                    tables_list, &select_lex->leaf_tables,
 
474
                                    false))
 
475
      return(-1);
 
476
 
 
477
  TABLE_LIST *table_ptr;
 
478
  for (table_ptr= select_lex->leaf_tables;
 
479
       table_ptr;
 
480
       table_ptr= table_ptr->next_leaf)
 
481
    tables++;
 
482
 
 
483
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
 
484
      select_lex->setup_ref_array(thd, og_num) ||
 
485
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
486
                   &all_fields, 1) ||
 
487
      setup_without_group(thd, (*rref_pointer_array), tables_list,
 
488
                          select_lex->leaf_tables, fields_list,
 
489
                          all_fields, &conds, order, group_list,
 
490
                          &hidden_group_fields))
 
491
    return(-1);                         /* purecov: inspected */
 
492
 
 
493
  ref_pointer_array= *rref_pointer_array;
 
494
  
 
495
  if (having)
 
496
  {
 
497
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
498
    thd->where="having clause";
 
499
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
500
    select_lex->having_fix_field= 1;
 
501
    bool having_fix_rc= (!having->fixed &&
 
502
                         (having->fix_fields(thd, &having) ||
 
503
                          having->check_cols(1)));
 
504
    select_lex->having_fix_field= 0;
 
505
    if (having_fix_rc || thd->is_error())
 
506
      return(-1);                               /* purecov: inspected */
 
507
    thd->lex->allow_sum_func= save_allow_sum_func;
 
508
  }
 
509
 
 
510
  {
 
511
    Item_subselect *subselect;
 
512
    Item_in_subselect *in_subs= NULL;
 
513
    /*
 
514
      Are we in a subquery predicate?
 
515
      TODO: the block below will be executed for every PS execution without need.
 
516
    */
 
517
    if ((subselect= select_lex->master_unit()->item))
 
518
    {
 
519
      bool do_semijoin= !test(thd->variables.optimizer_switch &
 
520
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
521
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
522
        in_subs= (Item_in_subselect*)subselect;
 
523
 
 
524
      /*
 
525
        Check if we're in subquery that is a candidate for flattening into a
 
526
        semi-join (which is done done in flatten_subqueries()). The
 
527
        requirements are:
 
528
          1. Subquery predicate is an IN/=ANY subq predicate
 
529
          2. Subquery is a single SELECT (not a UNION)
 
530
          3. Subquery does not have GROUP BY or ORDER BY
 
531
          4. Subquery does not use aggregate functions or HAVING
 
532
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
533
          6. No execution method was already chosen (by a prepared statement).
 
534
 
 
535
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
 
536
               doesn't have a JOIN (TODO: We should handle this at some
 
537
               point by switching to multi-table UPDATE/DELETE)
 
538
 
 
539
          (**). We're not in a confluent table-less subquery, like
 
540
                "SELECT 1". 
 
541
      */
 
542
      if (in_subs &&                                                    // 1
 
543
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
544
          !select_lex->group_list.elements && !order &&                 // 3
 
545
          !having && !select_lex->with_sum_func &&                      // 4
 
546
          thd->thd_marker &&                                            // 5
 
547
          select_lex->outer_select()->join &&                           // (*)
 
548
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
 
549
          do_semijoin &&
 
550
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
551
      {
 
552
        {
 
553
          if (!in_subs->left_expr->fixed &&
 
554
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
 
555
          {
 
556
            return(-1);
 
557
          }
 
558
          /*
 
559
            Check that the right part of the subselect contains no more than one
 
560
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
561
          */
 
562
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
563
             (select_lex->item_list.elements != 
 
564
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
565
          {
 
566
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
567
            return(-1);
 
568
          }
 
569
        }
 
570
 
 
571
        /* Register the subquery for further processing */
 
572
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
 
573
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
 
574
      }
 
575
      else
 
576
      {
 
577
        bool do_materialize= !test(thd->variables.optimizer_switch &
 
578
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
579
        /*
 
580
          Check if the subquery predicate can be executed via materialization.
 
581
          The required conditions are:
 
582
          1. Subquery predicate is an IN/=ANY subq predicate
 
583
          2. Subquery is a single SELECT (not a UNION)
 
584
          3. Subquery is not a table-less query. In this case there is no
 
585
             point in materializing.
 
586
          4. Subquery predicate is a top-level predicate
 
587
             (this implies it is not negated)
 
588
             TODO: this is a limitation that should be lifeted once we
 
589
             implement correct NULL semantics (WL#3830)
 
590
          5. Subquery is non-correlated
 
591
             TODO:
 
592
             This is an overly restrictive condition. It can be extended to:
 
593
             (Subquery is non-correlated ||
 
594
              Subquery is correlated to any query outer to IN predicate ||
 
595
              (Subquery is correlated to the immediate outer query &&
 
596
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
 
597
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
598
          6. No execution method was already chosen (by a prepared statement).
 
599
 
 
600
          (*) The subquery must be part of a SELECT statement. The current
 
601
               condition also excludes multi-table update statements.
 
602
 
 
603
          We have to determine whether we will perform subquery materialization
 
604
          before calling the IN=>EXISTS transformation, so that we know whether to
 
605
          perform the whole transformation or only that part of it which wraps
 
606
          Item_in_subselect in an Item_in_optimizer.
 
607
        */
 
608
        if (do_materialize && 
 
609
            in_subs  &&                                                   // 1
 
610
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
611
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
612
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
 
613
        {
 
614
          if (in_subs->is_top_level_item() &&                             // 4
 
615
              !in_subs->is_correlated &&                                  // 5
 
616
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
617
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
618
        }
 
619
 
 
620
        Item_subselect::trans_res trans_res;
 
621
        if ((trans_res= subselect->select_transformer(this)) !=
 
622
            Item_subselect::RES_OK)
 
623
        {
 
624
          return((trans_res == Item_subselect::RES_ERROR));
 
625
        }
 
626
      }
 
627
    }
 
628
  }
 
629
 
 
630
  if (order)
 
631
  {
 
632
    ORDER *ord;
 
633
    for (ord= order; ord; ord= ord->next)
 
634
    {
 
635
      Item *item= *ord->item;
 
636
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
637
        item->split_sum_func(thd, ref_pointer_array, all_fields);
 
638
    }
 
639
  }
 
640
 
 
641
  if (having && having->with_sum_func)
 
642
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
 
643
                            &having, true);
 
644
  if (select_lex->inner_sum_func_list)
 
645
  {
 
646
    Item_sum *end=select_lex->inner_sum_func_list;
 
647
    Item_sum *item_sum= end;  
 
648
    do
 
649
    { 
 
650
      item_sum= item_sum->next;
 
651
      item_sum->split_sum_func2(thd, ref_pointer_array,
 
652
                                all_fields, item_sum->ref_by, false);
 
653
    } while (item_sum != end);
 
654
  }
 
655
 
 
656
  if (select_lex->inner_refs_list.elements &&
 
657
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
 
658
    return(-1);
 
659
 
 
660
  /*
 
661
    Check if there are references to un-aggregated columns when computing 
 
662
    aggregate functions with implicit grouping (there is no GROUP BY).
 
663
 
 
664
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
665
  */
 
666
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
667
  {
 
668
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
669
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
670
    return(-1);
 
671
  }
 
672
  {
 
673
    /* Caclulate the number of groups */
 
674
    send_group_parts= 0;
 
675
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
676
      send_group_parts++;
 
677
  }
 
678
  
 
679
  if (error)
 
680
    goto err;                                   /* purecov: inspected */
 
681
 
 
682
  if (result && result->prepare(fields_list, unit_arg))
 
683
    goto err;                                   /* purecov: inspected */
 
684
 
 
685
  /* Init join struct */
 
686
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
687
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
688
  this->group= group_list != 0;
 
689
  unit= unit_arg;
 
690
 
 
691
#ifdef RESTRICTED_GROUP
 
692
  if (sum_func_count && !group_list && (func_count || field_count))
 
693
  {
 
694
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
695
    goto err;
 
696
  }
 
697
#endif
 
698
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
699
    goto err;
 
700
  if (alloc_func_list())
 
701
    goto err;
 
702
 
 
703
  return(0); // All OK
 
704
 
 
705
err:
 
706
  return(-1);                           /* purecov: inspected */
 
707
}
 
708
 
 
709
 
 
710
/*
 
711
  Remove the predicates pushed down into the subquery
 
712
 
 
713
  SYNOPSIS
 
714
    JOIN::remove_subq_pushed_predicates()
 
715
      where   IN  Must be NULL
 
716
              OUT The remaining WHERE condition, or NULL
 
717
 
 
718
  DESCRIPTION
 
719
    Given that this join will be executed using (unique|index)_subquery,
 
720
    without "checking NULL", remove the predicates that were pushed down
 
721
    into the subquery.
 
722
 
 
723
    If the subquery compares scalar values, we can remove the condition that
 
724
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
725
    engine)
 
726
 
 
727
    If the subquery compares row values, we need to keep the wrapped
 
728
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
729
    and non-NULL values, we'll do a full table scan and will rely on the
 
730
    equalities corresponding to non-NULL parts of left tuple to filter out
 
731
    non-matching records.
 
732
 
 
733
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
734
    fact that subquery engine will be using index lookup. This must be done only
 
735
    for cases where there are no conversion errors of significance, e.g. 257
 
736
    that is searched in a byte. But this requires homogenization of the return 
 
737
    codes of all Field*::store() methods.
 
738
*/
 
739
 
 
740
void JOIN::remove_subq_pushed_predicates(Item **where)
 
741
{
 
742
  if (conds->type() == Item::FUNC_ITEM &&
 
743
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
744
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
745
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
746
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
747
                   ((Item_func *)conds)->arguments()[0]))
 
748
  {
 
749
    *where= 0;
 
750
    return;
 
751
  }
 
752
}
 
753
 
 
754
 
282
755
/*
283
756
  Index lookup-based subquery: save some flags for EXPLAIN output
284
757
 
291
764
  DESCRIPTION
292
765
    For index lookup-based subquery (i.e. one executed with
293
766
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
294
 
    check its EXPLAIN output row should contain
295
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
 
767
    check its EXPLAIN output row should contain 
 
768
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
296
769
      "Using Where" (TAB_INFO_USING_WHERE)
297
770
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
298
771
    and set appropriate flags in join_tab->packed_info.
299
772
*/
300
 
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
 
773
 
 
774
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
301
775
{
302
776
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
303
 
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
 
777
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
304
778
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
305
779
  if (where)
306
780
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
307
 
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
 
781
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
308
782
  {
309
783
    if (join_tab->ref.cond_guards[i])
310
784
    {
314
788
  }
315
789
}
316
790
 
 
791
 
 
792
 
 
793
 
 
794
/*
 
795
  Check if the table's rowid is included in the temptable
 
796
 
 
797
  SYNOPSIS
 
798
    sj_table_is_included()
 
799
      join      The join
 
800
      join_tab  The table to be checked
 
801
 
 
802
  DESCRIPTION
 
803
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
804
    in the temptable. This is so if
 
805
 
 
806
    1. The table is not embedded within some semi-join nest
 
807
    2. The has been pulled out of a semi-join nest, or
 
808
 
 
809
    3. The table is functionally dependent on some previous table
 
810
 
 
811
    [4. This is also true for constant tables that can't be
 
812
        NULL-complemented but this function is not called for such tables]
 
813
 
 
814
  RETURN
 
815
    true  - Include table's rowid
 
816
    false - Don't
 
817
*/
 
818
 
 
819
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
820
{
 
821
  if (join_tab->emb_sj_nest)
 
822
    return false;
 
823
  
 
824
  /* Check if this table is functionally dependent on the tables that
 
825
     are within the same outer join nest
 
826
  */
 
827
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
 
828
  if (join_tab->type == JT_EQ_REF)
 
829
  {
 
830
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
831
    uint idx;
 
832
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
833
    {
 
834
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
835
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
836
        return true;
 
837
    }
 
838
    /* Ok, functionally dependent */
 
839
    return false;
 
840
  }
 
841
  /* Not functionally dependent => need to include*/
 
842
  return true;
 
843
}
 
844
 
 
845
 
 
846
/*
 
847
  Setup the strategies to eliminate semi-join duplicates.
 
848
  
 
849
  SYNOPSIS
 
850
    setup_semijoin_dups_elimination()
 
851
      join           Join to process
 
852
      options        Join options (needed to see if join buffering will be 
 
853
                     used or not)
 
854
      no_jbuf_after  Another bit of information re where join buffering will
 
855
                     be used.
 
856
 
 
857
  DESCRIPTION
 
858
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
859
    strategies:
 
860
 
 
861
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
862
                         of row combinations)
 
863
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
864
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
865
                  together and picking the 1st one)
 
866
    
 
867
    The join order has "duplicate-generating ranges", and every range is
 
868
    served by one strategy or a combination of FirstMatch with with some
 
869
    other strategy.
 
870
    
 
871
    "Duplicate-generating range" is defined as a range within the join order
 
872
    that contains all of the inner tables of a semi-join. All ranges must be
 
873
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
874
    are joined together, which is equivalent to converting
 
875
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
876
    to
 
877
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
878
    .
 
879
 
 
880
    Applicability conditions are as follows:
 
881
 
 
882
    DuplicateWeedout strategy
 
883
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
884
 
 
885
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
886
      +------+  +=========================+  +---+
 
887
        (1)                 (2)               (3)
 
888
 
 
889
       (1) - Prefix of OuterTables (those that participate in 
 
890
             IN-equality and/or are correlated with subquery) and outer 
 
891
             Noncorrelated Tables.
 
892
       (2) - The handled range. The range starts with the first sj-inner
 
893
             table, and covers all sj-inner and outer tables 
 
894
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
895
             may follow in any order.
 
896
       (3) - The suffix of outer Noncorrelated tables.
 
897
    
 
898
    FirstMatch strategy
 
899
    ~~~~~~~~~~~~~~~~~~~
 
900
 
 
901
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
902
      +------+  +==================+  +---+
 
903
        (1)             (2)          (3)
 
904
 
 
905
      (1) - Prefix of outer and non-correlated tables
 
906
      (2) - The handled range, which may contain only inner and
 
907
            non-correlated tables.
 
908
      (3) - The suffix of outer Noncorrelated tables.
 
909
 
 
910
    InsideOut strategy 
 
911
    ~~~~~~~~~~~~~~~~~~
 
912
 
 
913
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
914
     +--------+   +===========+ +=============+   +------+
 
915
        (1)           (2)          (3)              (4)
 
916
     
 
917
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
918
            all the non-trivially correlated outer tables. (non-trivially means
 
919
            that the correlation is not just through the IN-equality).
 
920
      
 
921
      (2) - Inner table for which the InsideOut scan is performed.
 
922
 
 
923
      (3) - The remainder of the duplicate-generating range. It is served by 
 
924
            application of FirstMatch strategy, with the exception that
 
925
            outer IN-correlated tables are considered to be non-correlated.
 
926
 
 
927
      (4) - THe suffix of outer and outer non-correlated tables.
 
928
 
 
929
    If several strategies are applicable, their relative priorities are:
 
930
      1. InsideOut
 
931
      2. FirstMatch 
 
932
      3. DuplicateWeedout
 
933
 
 
934
    This function walks over the join order and sets up the strategies by
 
935
    setting appropriate members in join_tab structures.
 
936
 
 
937
  RETURN
 
938
    false  OK 
 
939
    true   Out of memory error
 
940
*/
 
941
 
 
942
static
 
943
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
 
944
{
 
945
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
946
  struct {
 
947
    /* 
 
948
      0 - invalid (EOF marker), 
 
949
      1 - InsideOut, 
 
950
      2 - Temptable (maybe confluent),
 
951
      3 - Temptable with join buffering
 
952
    */
 
953
    uint strategy;
 
954
    uint start_idx; /* Left range bound */
 
955
    uint end_idx;   /* Right range bound */
 
956
    /* 
 
957
      For Temptable strategy: Bitmap of all outer and correlated tables from 
 
958
      all involved join nests.
 
959
    */
 
960
    table_map outer_tables;
 
961
  } dups_ranges [MAX_TABLES];
 
962
 
 
963
  TABLE_LIST *emb_insideout_nest= NULL;
 
964
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
965
                               tables) whose ranges we're in */
 
966
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
967
  table_map range_start_map= 0; /* table_map at current range start */
 
968
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
969
  int cur_range= 0;
 
970
  uint i;
 
971
 
 
972
  /*
 
973
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
974
  */
 
975
  for (i=join->const_tables ; i < join->tables ; i++)
 
976
  {
 
977
    JOIN_TAB *tab=join->join_tab+i;
 
978
    TABLE *table=tab->table;
 
979
    cur_map |= table->map;
 
980
 
 
981
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
982
    {
 
983
      if (!emb_sj_map)
 
984
      {
 
985
        dups_ranges[cur_range].start_idx= i;
 
986
        range_start_map= cur_map & ~table->map;
 
987
        /*
 
988
          Remember if this is a possible start of range that is covered by
 
989
          the InsideOut strategy (the reason that it is not covered could
 
990
          be that it overlaps with anther semi-join's range. we don't
 
991
          support InsideOut for joined ranges)
 
992
        */
 
993
        if (join->best_positions[i].use_insideout_scan)
 
994
          emb_insideout_nest= tab->emb_sj_nest;
 
995
      }
 
996
 
 
997
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
998
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
999
 
 
1000
      if (tab->emb_sj_nest != emb_insideout_nest)
 
1001
      {
 
1002
        /*
 
1003
          Two different semi-joins interleave. This cannot be handled by
 
1004
          InsideOut strategy.
 
1005
        */
 
1006
        emb_insideout_nest= NULL;
 
1007
      }
 
1008
    }
 
1009
 
 
1010
    if (emb_sj_map) /* We're in duplicate-generating range */
 
1011
    {
 
1012
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
1013
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
1014
          i <= no_jbuf_after && !dealing_with_jbuf)
 
1015
      {
 
1016
        /*
 
1017
          This table uses join buffering, which makes use of FirstMatch or 
 
1018
          InsideOut strategies impossible for the current and (we assume) 
 
1019
          preceding duplicate-producing ranges.
 
1020
          That is, for the join order:
 
1021
 
 
1022
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
1023
                  |     |     |     |          | \
 
1024
                  +-----+     +-----+          |  join buffering use
 
1025
                     r1          r2         we're here
 
1026
 
 
1027
          we'll have to remove r1 and r2 and use duplicate-elimination
 
1028
          strategy that spans all the tables, starting from the very 1st
 
1029
          one.
 
1030
        */
 
1031
        dealing_with_jbuf= true;
 
1032
        emb_insideout_nest= false;
 
1033
 
 
1034
        /* 
 
1035
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
1036
          do not matter: 
 
1037
        */
 
1038
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
1039
        {
 
1040
          dups_ranges[cur_range].outer_tables |= 
 
1041
            dups_ranges[prev_range].outer_tables;
 
1042
        }
 
1043
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
1044
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
1045
        cur_range=  0;
 
1046
      }
 
1047
 
 
1048
      /*
 
1049
        Check if we are at the end of duplicate-producing range. We are if
 
1050
 
 
1051
        1. It's an InsideOut range (which presumes all correlated tables are
 
1052
           in the prefix), and all inner tables are in the join order prefix,
 
1053
           or
 
1054
        2. It's a DuplicateElimination range (possibly covering several
 
1055
           SJ-nests), and all inner, outer, and correlated tables of all 
 
1056
           sj-nests are in the join order prefix.
 
1057
      */
 
1058
      bool end_of_range= false;
 
1059
      if (emb_insideout_nest && 
 
1060
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
1061
      {
 
1062
        /* Save that this range is handled with InsideOut: */
 
1063
        dups_ranges[cur_range].strategy= 1;
 
1064
        end_of_range= true;
 
1065
      }
 
1066
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
1067
      {
 
1068
        /*
 
1069
          This is a complete range to be handled with either DuplicateWeedout 
 
1070
          or FirstMatch
 
1071
        */
 
1072
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
1073
        /* 
 
1074
          This will hold tables from within the range that need to be put 
 
1075
          into the join buffer before we can use the FirstMatch on its tail.
 
1076
        */
 
1077
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
 
1078
                                             ~range_start_map;
 
1079
        end_of_range= true;
 
1080
      }
 
1081
 
 
1082
      if (end_of_range)
 
1083
      {
 
1084
        dups_ranges[cur_range].end_idx= i+1;
 
1085
        emb_sj_map= emb_outer_tables= 0;
 
1086
        emb_insideout_nest= NULL;
 
1087
        dealing_with_jbuf= false;
 
1088
        dups_ranges[++cur_range].strategy= 0;
 
1089
      }
 
1090
    }
 
1091
  }
 
1092
 
 
1093
  THD *thd= join->thd;
 
1094
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
1095
  /*
 
1096
    Second pass: setup the chosen strategies    
 
1097
  */
 
1098
  for (int j= 0; j < cur_range; j++)
 
1099
  {
 
1100
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
1101
    JOIN_TAB *jump_to;
 
1102
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
1103
    {
 
1104
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
1105
      jump_to= tab++;
 
1106
    }
 
1107
    else // DuplicateWeedout strategy
 
1108
    {
 
1109
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
1110
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
1111
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
1112
      uint jt_null_bits= 0;    // # null bits in tuple bytes
 
1113
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
1114
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
1115
      JOIN_TAB *last_outer_tab= tab - 1;
 
1116
      /*
 
1117
        Walk through the range and remember
 
1118
         - tables that need their rowids to be put into temptable
 
1119
         - the last outer table
 
1120
      */
 
1121
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1122
      {
 
1123
        if (sj_table_is_included(join, tab))
 
1124
        {
 
1125
          last_tab->join_tab= tab;
 
1126
          last_tab->rowid_offset= jt_rowid_offset;
 
1127
          jt_rowid_offset += tab->table->file->ref_length;
 
1128
          if (tab->table->maybe_null)
 
1129
          {
 
1130
            last_tab->null_byte= jt_null_bits / 8;
 
1131
            last_tab->null_bit= jt_null_bits++;
 
1132
          }
 
1133
          last_tab++;
 
1134
          tab->table->prepare_for_position();
 
1135
          tab->rowid_keep_flags= rowid_keep_flags;
 
1136
        }
 
1137
        cur_map |= tab->table->map;
 
1138
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
 
1139
                                               dups_ranges[j].outer_tables))
 
1140
          last_outer_tab= tab;
 
1141
      }
 
1142
 
 
1143
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
1144
      {
 
1145
        SJ_TMP_TABLE *sjtbl;
 
1146
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
1147
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
 
1148
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
 
1149
          return(true);
 
1150
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
1151
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
1152
        sjtbl->rowid_len= jt_rowid_offset;
 
1153
        sjtbl->null_bits= jt_null_bits;
 
1154
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
1155
 
 
1156
        *next_sjtbl_ptr= sjtbl;
 
1157
        next_sjtbl_ptr= &(sjtbl->next);
 
1158
        sjtbl->next= NULL;
 
1159
 
 
1160
        sjtbl->tmp_table= 
 
1161
          create_duplicate_weedout_tmp_table(thd, 
 
1162
                                             sjtbl->rowid_len + 
 
1163
                                             sjtbl->null_bytes,
 
1164
                                             sjtbl);
 
1165
 
 
1166
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
1167
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
1168
      }
 
1169
      tab= last_outer_tab + 1;
 
1170
      jump_to= last_outer_tab;
 
1171
    }
 
1172
 
 
1173
    /* Create the FirstMatch tail */
 
1174
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
1175
    {
 
1176
      if (tab->emb_sj_nest)
 
1177
        tab->do_firstmatch= jump_to; 
 
1178
      else
 
1179
        jump_to= tab;
 
1180
    }
 
1181
  }
 
1182
  return(false);
 
1183
}
 
1184
 
 
1185
 
 
1186
static void cleanup_sj_tmp_tables(JOIN *join)
 
1187
{
 
1188
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
 
1189
       sj_tbl= sj_tbl->next)
 
1190
  {
 
1191
    if (sj_tbl->tmp_table)
 
1192
    {
 
1193
      sj_tbl->tmp_table->free_tmp_table(join->thd);
 
1194
    }
 
1195
  }
 
1196
  join->sj_tmp_tables= NULL;
 
1197
}
 
1198
 
 
1199
uint make_join_orderinfo(JOIN *join);
 
1200
 
 
1201
/**
 
1202
  global select optimisation.
 
1203
 
 
1204
  @note
 
1205
    error code saved in field 'error'
 
1206
 
 
1207
  @retval
 
1208
    0   success
 
1209
  @retval
 
1210
    1   error
 
1211
*/
 
1212
 
 
1213
int
 
1214
JOIN::optimize()
 
1215
{
 
1216
  // to prevent double initialization on EXPLAIN
 
1217
  if (optimized)
 
1218
    return(0);
 
1219
  optimized= 1;
 
1220
 
 
1221
  thd_proc_info(thd, "optimizing");
 
1222
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
1223
              unit->select_limit_cnt);
 
1224
  /* select_limit is used to decide if we are likely to scan the whole table */
 
1225
  select_limit= unit->select_limit_cnt;
 
1226
  if (having || (select_options & OPTION_FOUND_ROWS))
 
1227
    select_limit= HA_POS_ERROR;
 
1228
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
1229
  // Ignore errors of execution if option IGNORE present
 
1230
  if (thd->lex->ignore)
 
1231
    thd->lex->current_select->no_error= 1;
 
1232
 
 
1233
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
 
1234
  /* Add HAVING to WHERE if possible */
 
1235
  if (having && !group_list && !sum_func_count)
 
1236
  {
 
1237
    if (!conds)
 
1238
    {
 
1239
      conds= having;
 
1240
      having= 0;
 
1241
    }
 
1242
    else if ((conds=new Item_cond_and(conds,having)))
 
1243
    {
 
1244
      /*
 
1245
        Item_cond_and can't be fixed after creation, so we do not check
 
1246
        conds->fixed
 
1247
      */
 
1248
      conds->fix_fields(thd, &conds);
 
1249
      conds->change_ref_to_fields(thd, tables_list);
 
1250
      conds->top_level_item();
 
1251
      having= 0;
 
1252
    }
 
1253
  }
 
1254
#endif
 
1255
  SELECT_LEX *sel= thd->lex->current_select;
 
1256
  if (sel->first_cond_optimization)
 
1257
  {
 
1258
    /*
 
1259
      The following code will allocate the new items in a permanent
 
1260
      MEMROOT for prepared statements and stored procedures.
 
1261
    */
 
1262
    sel->first_cond_optimization= 0;
 
1263
 
 
1264
    /* Convert all outer joins to inner joins if possible */
 
1265
    conds= simplify_joins(this, join_list, conds, true, false);
 
1266
    build_bitmap_for_nested_joins(join_list, 0);
 
1267
  }
 
1268
 
 
1269
  conds= optimize_cond(this, conds, join_list, &cond_value);   
 
1270
  if (thd->is_error())
 
1271
  {
 
1272
    error= 1;
 
1273
    return(1);
 
1274
  }
 
1275
 
 
1276
  {
 
1277
    having= optimize_cond(this, having, join_list, &having_value);
 
1278
    if (thd->is_error())
 
1279
    {
 
1280
      error= 1;
 
1281
      return(1);
 
1282
    }
 
1283
    if (select_lex->where)
 
1284
      select_lex->cond_value= cond_value;
 
1285
    if (select_lex->having)
 
1286
      select_lex->having_value= having_value;
 
1287
 
 
1288
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
 
1289
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
1290
    {                                           /* Impossible cond */
 
1291
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
1292
                           "Impossible HAVING" : "Impossible WHERE";
 
1293
      error= 0;
 
1294
      return(0);
 
1295
    }
 
1296
  }
 
1297
 
 
1298
  /* Optimize count(*), min() and max() */
 
1299
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
1300
  {
 
1301
    int res;
 
1302
    /*
 
1303
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
1304
      to the WHERE conditions,
 
1305
      or 1 if all items were resolved,
 
1306
      or 0, or an error number HA_ERR_...
 
1307
    */
 
1308
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
1309
    {
 
1310
      if (res == HA_ERR_KEY_NOT_FOUND)
 
1311
      {
 
1312
        zero_result_cause= "No matching min/max row";
 
1313
        error=0;
 
1314
        return(0);
 
1315
      }
 
1316
      if (res > 1)
 
1317
      {
 
1318
        error= res;
 
1319
        return(1);
 
1320
      }
 
1321
      if (res < 0)
 
1322
      {
 
1323
        zero_result_cause= "No matching min/max row";
 
1324
        error=0;
 
1325
        return(0);
 
1326
      }
 
1327
      zero_result_cause= "Select tables optimized away";
 
1328
      tables_list= 0;                           // All tables resolved
 
1329
      /*
 
1330
        Extract all table-independent conditions and replace the WHERE
 
1331
        clause with them. All other conditions were computed by opt_sum_query
 
1332
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
1333
        so there is no need to compute the whole WHERE clause again.
 
1334
        Notice that make_cond_for_table() will always succeed to remove all
 
1335
        computed conditions, because opt_sum_query() is applicable only to
 
1336
        conjunctions.
 
1337
        Preserve conditions for EXPLAIN.
 
1338
      */
 
1339
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
 
1340
      {
 
1341
        COND *table_independent_conds=
 
1342
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
1343
        conds= table_independent_conds;
 
1344
      }
 
1345
    }
 
1346
  }
 
1347
  if (!tables_list)
 
1348
  {
 
1349
    error= 0;
 
1350
    return(0);
 
1351
  }
 
1352
  error= -1;                                    // Error is sent to client
 
1353
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
1354
 
 
1355
  /* Calculate how to do the join */
 
1356
  thd_proc_info(thd, "statistics");
 
1357
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
1358
      thd->is_fatal_error)
 
1359
  {
 
1360
    return(1);
 
1361
  }
 
1362
 
 
1363
  /* Remove distinct if only const tables */
 
1364
  select_distinct= select_distinct && (const_tables != tables);
 
1365
  thd_proc_info(thd, "preparing");
 
1366
  if (result->initialize_tables(this))
 
1367
  {
 
1368
    return(1);                          // error == -1
 
1369
  }
 
1370
  if (const_table_map != found_const_table_map &&
 
1371
      !(select_options & SELECT_DESCRIBE) &&
 
1372
      (!conds ||
 
1373
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
1374
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
 
1375
  {
 
1376
    zero_result_cause= "no matching row in const table";
 
1377
    error= 0;
 
1378
    return(0);
 
1379
  }
 
1380
  if (!(thd->options & OPTION_BIG_SELECTS) &&
 
1381
      best_read > (double) thd->variables.max_join_size &&
 
1382
      !(select_options & SELECT_DESCRIBE))
 
1383
  {                                             /* purecov: inspected */
 
1384
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
1385
    error= -1;
 
1386
    return(1);
 
1387
  }
 
1388
  if (const_tables && !thd->locked_tables &&
 
1389
      !(select_options & SELECT_NO_UNLOCK))
 
1390
    mysql_unlock_some_tables(thd, table, const_tables);
 
1391
  if (!conds && outer_join)
 
1392
  {
 
1393
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
1394
    conds=new Item_int((int64_t) 1,1);  // Always true
 
1395
  }
 
1396
  select= make_select(*table, const_table_map,
 
1397
                      const_table_map, conds, 1, &error);
 
1398
  if (error)
 
1399
  {                                             /* purecov: inspected */
 
1400
    error= -1;                                  /* purecov: inspected */
 
1401
    return(1);
 
1402
  }
 
1403
  
 
1404
  reset_nj_counters(join_list);
 
1405
  make_outerjoin_info(this);
 
1406
 
 
1407
  /*
 
1408
    Among the equal fields belonging to the same multiple equality
 
1409
    choose the one that is to be retrieved first and substitute
 
1410
    all references to these in where condition for a reference for
 
1411
    the selected field.
 
1412
  */
 
1413
  if (conds)
 
1414
  {
 
1415
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
1416
    conds->update_used_tables();
 
1417
  }
 
1418
 
 
1419
  /*
 
1420
    Permorm the the optimization on fields evaluation mentioned above
 
1421
    for all on expressions.
 
1422
  */ 
 
1423
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
1424
  {
 
1425
    if (*tab->on_expr_ref)
 
1426
    {
 
1427
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
1428
                                                         tab->cond_equal,
 
1429
                                                         map2table);
 
1430
      (*tab->on_expr_ref)->update_used_tables();
 
1431
    }
 
1432
  }
 
1433
 
 
1434
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
 
1435
      (select_options & SELECT_DESCRIBE) &&
 
1436
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
 
1437
  {
 
1438
    conds=new Item_int((int64_t) 0,1);  // Always false
 
1439
  }
 
1440
  if (make_join_select(this, select, conds))
 
1441
  {
 
1442
    zero_result_cause=
 
1443
      "Impossible WHERE noticed after reading const tables";
 
1444
    return(0);                          // error == 0
 
1445
  }
 
1446
 
 
1447
  error= -1;                                    /* if goto err */
 
1448
 
 
1449
  /* Optimize distinct away if possible */
 
1450
  {
 
1451
    ORDER *org_order= order;
 
1452
    order=remove_const(this, order,conds,1, &simple_order);
 
1453
    if (thd->is_error())
 
1454
    {
 
1455
      error= 1;
 
1456
      return(1);
 
1457
    }
 
1458
 
 
1459
    /*
 
1460
      If we are using ORDER BY NULL or ORDER BY const_expression,
 
1461
      return result in any order (even if we are using a GROUP BY)
 
1462
    */
 
1463
    if (!order && org_order)
 
1464
      skip_sort_order= 1;
 
1465
  }
 
1466
  /*
 
1467
     Check if we can optimize away GROUP BY/DISTINCT.
 
1468
     We can do that if there are no aggregate functions, the
 
1469
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
1470
     (if present) contain direct references to all key parts of
 
1471
     an unique index (in whatever order) and if the key parts of the
 
1472
     unique index cannot contain NULLs.
 
1473
     Note that the unique keys for DISTINCT and GROUP BY should not
 
1474
     be the same (as long as they are unique).
 
1475
 
 
1476
     The FROM clause must contain a single non-constant table.
 
1477
  */
 
1478
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
1479
      !tmp_table_param.sum_func_count &&
 
1480
      (!join_tab[const_tables].select ||
 
1481
       !join_tab[const_tables].select->quick ||
 
1482
       join_tab[const_tables].select->quick->get_type() != 
 
1483
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
1484
  {
 
1485
    if (group_list &&
 
1486
       list_contains_unique_index(join_tab[const_tables].table,
 
1487
                                 find_field_in_order_list,
 
1488
                                 (void *) group_list))
 
1489
    {
 
1490
      /*
 
1491
        We have found that grouping can be removed since groups correspond to
 
1492
        only one row anyway, but we still have to guarantee correct result
 
1493
        order. The line below effectively rewrites the query from GROUP BY
 
1494
        <fields> to ORDER BY <fields>. There are two exceptions:
 
1495
        - if skip_sort_order is set (see above), then we can simply skip
 
1496
          GROUP BY;
 
1497
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
 
1498
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
1499
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
 
1500
          test_if_subpart() copies the ASC/DESC attributes from the original
 
1501
          ORDER BY fields.
 
1502
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
 
1503
          'order' as is.
 
1504
       */
 
1505
      if (!order || test_if_subpart(group_list, order))
 
1506
          order= skip_sort_order ? 0 : group_list;
 
1507
      /*
 
1508
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
 
1509
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
 
1510
      */
 
1511
      join_tab->table->keys_in_use_for_order_by=
 
1512
        join_tab->table->keys_in_use_for_group_by;
 
1513
      group_list= 0;
 
1514
      group= 0;
 
1515
    }
 
1516
    if (select_distinct &&
 
1517
       list_contains_unique_index(join_tab[const_tables].table,
 
1518
                                 find_field_in_item_list,
 
1519
                                 (void *) &fields_list))
 
1520
    {
 
1521
      select_distinct= 0;
 
1522
    }
 
1523
  }
 
1524
  if (group_list || tmp_table_param.sum_func_count)
 
1525
  {
 
1526
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
1527
      select_distinct=0;
 
1528
  }
 
1529
  else if (select_distinct && tables - const_tables == 1)
 
1530
  {
 
1531
    /*
 
1532
      We are only using one table. In this case we change DISTINCT to a
 
1533
      GROUP BY query if:
 
1534
      - The GROUP BY can be done through indexes (no sort) and the ORDER
 
1535
        BY only uses selected fields.
 
1536
        (In this case we can later optimize away GROUP BY and ORDER BY)
 
1537
      - We are scanning the whole table without LIMIT
 
1538
        This can happen if:
 
1539
        - We are using CALC_FOUND_ROWS
 
1540
        - We are using an ORDER BY that can't be optimized away.
 
1541
 
 
1542
      We don't want to use this optimization when we are using LIMIT
 
1543
      because in this case we can just create a temporary table that
 
1544
      holds LIMIT rows and stop when this table is full.
 
1545
    */
 
1546
    JOIN_TAB *tab= &join_tab[const_tables];
 
1547
    bool all_order_fields_used;
 
1548
    if (order)
 
1549
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
 
1550
        &tab->table->keys_in_use_for_order_by);
 
1551
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
 
1552
                                          order, fields_list, all_fields,
 
1553
                                          &all_order_fields_used)))
 
1554
    {
 
1555
      bool skip_group= (skip_sort_order &&
 
1556
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
 
1557
                                &tab->table->keys_in_use_for_group_by) != 0);
 
1558
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
1559
      if ((skip_group && all_order_fields_used) ||
 
1560
          select_limit == HA_POS_ERROR ||
 
1561
          (order && !skip_sort_order))
 
1562
      {
 
1563
        /*  Change DISTINCT to GROUP BY */
 
1564
        select_distinct= 0;
 
1565
        no_order= !order;
 
1566
        if (all_order_fields_used)
 
1567
        {
 
1568
          if (order && skip_sort_order)
 
1569
          {
 
1570
            /*
 
1571
              Force MySQL to read the table in sorted order to get result in
 
1572
              ORDER BY order.
 
1573
            */
 
1574
            tmp_table_param.quick_group=0;
 
1575
          }
 
1576
          order=0;
 
1577
        }
 
1578
        group=1;                                // For end_write_group
 
1579
      }
 
1580
      else
 
1581
        group_list= 0;
 
1582
    }
 
1583
    else if (thd->is_fatal_error)                       // End of memory
 
1584
      return(1);
 
1585
  }
 
1586
  simple_group= 0;
 
1587
  {
 
1588
    ORDER *old_group_list;
 
1589
    group_list= remove_const(this, (old_group_list= group_list), conds,
 
1590
                             rollup.state == ROLLUP::STATE_NONE,
 
1591
                             &simple_group);
 
1592
    if (thd->is_error())
 
1593
    {
 
1594
      error= 1;
 
1595
      return(1);
 
1596
    }
 
1597
    if (old_group_list && !group_list)
 
1598
      select_distinct= 0;
 
1599
  }
 
1600
  if (!group_list && group)
 
1601
  {
 
1602
    order=0;                                    // The output has only one row
 
1603
    simple_order=1;
 
1604
    select_distinct= 0;                       // No need in distinct for 1 row
 
1605
    group_optimized_away= 1;
 
1606
  }
 
1607
 
 
1608
  calc_group_buffer(this, group_list);
 
1609
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
1610
 
 
1611
  if (test_if_subpart(group_list, order) ||
 
1612
      (!group_list && tmp_table_param.sum_func_count))
 
1613
    order=0;
 
1614
 
 
1615
  // Can't use sort on head table if using row cache
 
1616
  if (full_join)
 
1617
  {
 
1618
    if (group_list)
 
1619
      simple_group=0;
 
1620
    if (order)
 
1621
      simple_order=0;
 
1622
  }
 
1623
 
 
1624
  /*
 
1625
    Check if we need to create a temporary table.
 
1626
    This has to be done if all tables are not already read (const tables)
 
1627
    and one of the following conditions holds:
 
1628
    - We are using DISTINCT (simple distinct's are already optimized away)
 
1629
    - We are using an ORDER BY or GROUP BY on fields not in the first table
 
1630
    - We are using different ORDER BY and GROUP BY orders
 
1631
    - The user wants us to buffer the result.
 
1632
  */
 
1633
  need_tmp= (const_tables != tables &&
 
1634
             ((select_distinct || !simple_order || !simple_group) ||
 
1635
              (group_list && order) ||
 
1636
              test(select_options & OPTION_BUFFER_RESULT)));
 
1637
 
 
1638
  uint no_jbuf_after= make_join_orderinfo(this);
 
1639
  uint64_t select_opts_for_readinfo= 
 
1640
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
1641
 
 
1642
  sj_tmp_tables= NULL;
 
1643
  if (!select_lex->sj_nests.is_empty())
 
1644
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
 
1645
                                    no_jbuf_after);
 
1646
 
 
1647
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
1648
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
1649
    return(1);
 
1650
 
 
1651
  /* Create all structures needed for materialized subquery execution. */
 
1652
  if (setup_subquery_materialization())
 
1653
    return(1);
 
1654
 
 
1655
  /*
 
1656
    is this simple IN subquery?
 
1657
  */
 
1658
  if (!group_list && !order &&
 
1659
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
1660
      tables == 1 && conds &&
 
1661
      !unit->is_union())
 
1662
  {
 
1663
    if (!having)
 
1664
    {
 
1665
      Item *where= conds;
 
1666
      if (join_tab[0].type == JT_EQ_REF &&
 
1667
          join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1668
      {
 
1669
        remove_subq_pushed_predicates(&where);
 
1670
        save_index_subquery_explain_info(join_tab, where);
 
1671
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
1672
        error= 0;
 
1673
        return(unit->item->
 
1674
                    change_engine(new
 
1675
                                  subselect_uniquesubquery_engine(thd,
 
1676
                                                                  join_tab,
 
1677
                                                                  unit->item,
 
1678
                                                                  where)));
 
1679
      }
 
1680
      else if (join_tab[0].type == JT_REF &&
 
1681
               join_tab[0].ref.items[0]->name == in_left_expr_name)
 
1682
      {
 
1683
        remove_subq_pushed_predicates(&where);
 
1684
        save_index_subquery_explain_info(join_tab, where);
 
1685
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
1686
        error= 0;
 
1687
        return(unit->item->
 
1688
                    change_engine(new
 
1689
                                  subselect_indexsubquery_engine(thd,
 
1690
                                                                 join_tab,
 
1691
                                                                 unit->item,
 
1692
                                                                 where,
 
1693
                                                                 NULL,
 
1694
                                                                 0)));
 
1695
      }
 
1696
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
 
1697
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
1698
               having->name == in_having_cond)
 
1699
    {
 
1700
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
1701
      error= 0;
 
1702
      conds= remove_additional_cond(conds);
 
1703
      save_index_subquery_explain_info(join_tab, conds);
 
1704
      return(unit->item->
 
1705
                  change_engine(new subselect_indexsubquery_engine(thd,
 
1706
                                                                   join_tab,
 
1707
                                                                   unit->item,
 
1708
                                                                   conds,
 
1709
                                                                   having,
 
1710
                                                                   1)));
 
1711
    }
 
1712
 
 
1713
  }
 
1714
  /*
 
1715
    Need to tell handlers that to play it safe, it should fetch all
 
1716
    columns of the primary key of the tables: this is because MySQL may
 
1717
    build row pointers for the rows, and for all columns of the primary key
 
1718
    the read set has not necessarily been set by the server code.
 
1719
  */
 
1720
  if (need_tmp || select_distinct || group_list || order)
 
1721
  {
 
1722
    for (uint i = const_tables; i < tables; i++)
 
1723
      join_tab[i].table->prepare_for_position();
 
1724
  }
 
1725
 
 
1726
  if (const_tables != tables)
 
1727
  {
 
1728
    /*
 
1729
      Because filesort always does a full table scan or a quick range scan
 
1730
      we must add the removed reference to the select for the table.
 
1731
      We only need to do this when we have a simple_order or simple_group
 
1732
      as in other cases the join is done before the sort.
 
1733
    */
 
1734
    if ((order || group_list) &&
 
1735
        (join_tab[const_tables].type != JT_ALL) &&
 
1736
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
1737
        ((order && simple_order) || (group_list && simple_group)))
 
1738
    {
 
1739
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
 
1740
        return(1);
 
1741
      }
 
1742
    }
 
1743
    
 
1744
    if (!(select_options & SELECT_BIG_RESULT) &&
 
1745
        ((group_list &&
 
1746
          (!simple_group ||
 
1747
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
1748
                                    unit->select_limit_cnt, 0, 
 
1749
                                    &join_tab[const_tables].table->
 
1750
                                    keys_in_use_for_group_by))) ||
 
1751
         select_distinct) &&
 
1752
        tmp_table_param.quick_group)
 
1753
    {
 
1754
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
1755
    }
 
1756
    if (order)
 
1757
    {
 
1758
      /*
 
1759
        Force using of tmp table if sorting by a SP or UDF function due to
 
1760
        their expensive and probably non-deterministic nature.
 
1761
      */
 
1762
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
1763
      {
 
1764
        Item *item= *tmp_order->item;
 
1765
        if (item->is_expensive())
 
1766
        {
 
1767
          /* Force tmp table without sort */
 
1768
          need_tmp=1; simple_order=simple_group=0;
 
1769
          break;
 
1770
        }
 
1771
      }
 
1772
    }
 
1773
  }
 
1774
 
 
1775
  tmp_having= having;
 
1776
  if (select_options & SELECT_DESCRIBE)
 
1777
  {
 
1778
    error= 0;
 
1779
    return(0);
 
1780
  }
 
1781
  having= 0;
 
1782
 
 
1783
  /*
 
1784
    The loose index scan access method guarantees that all grouping or
 
1785
    duplicate row elimination (for distinct) is already performed
 
1786
    during data retrieval, and that all MIN/MAX functions are already
 
1787
    computed for each group. Thus all MIN/MAX functions should be
 
1788
    treated as regular functions, and there is no need to perform
 
1789
    grouping in the main execution loop.
 
1790
    Notice that currently loose index scan is applicable only for
 
1791
    single table queries, thus it is sufficient to test only the first
 
1792
    join_tab element of the plan for its access method.
 
1793
  */
 
1794
  if (join_tab->is_using_loose_index_scan())
 
1795
    tmp_table_param.precomputed_group_by= true;
 
1796
 
 
1797
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1798
  if (need_tmp)
 
1799
  {
 
1800
    thd_proc_info(thd, "Creating tmp table");
 
1801
 
 
1802
    init_items_ref_array();
 
1803
 
 
1804
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1805
                                         fields_list.elements);
 
1806
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1807
                                                             (ORDER*) 0);
 
1808
    /*
 
1809
      Pushing LIMIT to the temporary table creation is not applicable
 
1810
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
 
1811
      there are aggregate functions, because in all these cases we need
 
1812
      all result rows.
 
1813
    */
 
1814
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1815
                             !tmp_group &&
 
1816
                             !thd->lex->current_select->with_sum_func) ?
 
1817
                            select_limit : HA_POS_ERROR;
 
1818
 
 
1819
    if (!(exec_tmp_table1=
 
1820
          create_tmp_table(thd, &tmp_table_param, all_fields,
 
1821
                           tmp_group,
 
1822
                           group_list ? 0 : select_distinct,
 
1823
                           group_list && simple_group,
 
1824
                           select_options,
 
1825
                           tmp_rows_limit,
 
1826
                           (char *) "")))
 
1827
                {
 
1828
      return(1);
 
1829
    }
 
1830
 
 
1831
    /*
 
1832
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1833
      - we are sorting the table and writing complete group rows to the
 
1834
        temp table.
 
1835
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1836
        on all columns.
 
1837
      
 
1838
      If having is not handled here, it will be checked before the row
 
1839
      is sent to the client.
 
1840
    */    
 
1841
    if (tmp_having && 
 
1842
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1843
      having= tmp_having;
 
1844
 
 
1845
    /* if group or order on first table, sort first */
 
1846
    if (group_list && simple_group)
 
1847
    {
 
1848
      thd_proc_info(thd, "Sorting for group");
 
1849
      if (create_sort_index(thd, this, group_list,
 
1850
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1851
          alloc_group_fields(this, group_list) ||
 
1852
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1853
          setup_sum_funcs(thd, sum_funcs))
 
1854
      {
 
1855
        return(1);
 
1856
      }
 
1857
      group_list=0;
 
1858
    }
 
1859
    else
 
1860
    {
 
1861
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1862
          setup_sum_funcs(thd, sum_funcs))
 
1863
      {
 
1864
        return(1);
 
1865
      }
 
1866
 
 
1867
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1868
      {
 
1869
        thd_proc_info(thd, "Sorting for order");
 
1870
        if (create_sort_index(thd, this, order,
 
1871
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1872
        {
 
1873
          return(1);
 
1874
        }
 
1875
        order=0;
 
1876
      }
 
1877
    }
 
1878
    
 
1879
    /*
 
1880
      Optimize distinct when used on some of the tables
 
1881
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1882
      In this case we can stop scanning t2 when we have found one t1.a
 
1883
    */
 
1884
 
 
1885
    if (exec_tmp_table1->distinct)
 
1886
    {
 
1887
      table_map used_tables= thd->used_tables;
 
1888
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1889
      do
 
1890
      {
 
1891
        if (used_tables & last_join_tab->table->map)
 
1892
          break;
 
1893
        last_join_tab->not_used_in_distinct=1;
 
1894
      } while (last_join_tab-- != join_tab);
 
1895
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1896
      if (order && skip_sort_order)
 
1897
      {
 
1898
        /* Should always succeed */
 
1899
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1900
                                    order, unit->select_limit_cnt, 0, 
 
1901
                                    &join_tab[const_tables].table->
 
1902
                                      keys_in_use_for_order_by))
 
1903
          order=0;
 
1904
      }
 
1905
    }
 
1906
 
 
1907
    /* 
 
1908
      If this join belongs to an uncacheable subquery save 
 
1909
      the original join 
 
1910
    */
 
1911
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1912
        init_save_join_tab())
 
1913
      return(-1);                         /* purecov: inspected */
 
1914
  }
 
1915
 
 
1916
  error= 0;
 
1917
  return(0);
 
1918
}
 
1919
 
 
1920
 
 
1921
/**
 
1922
  Restore values in temporary join.
 
1923
*/
 
1924
void JOIN::restore_tmp()
 
1925
{
 
1926
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1927
}
 
1928
 
 
1929
 
 
1930
int
 
1931
JOIN::reinit()
 
1932
{
 
1933
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1934
                                    select_lex->offset_limit->val_uint() :
 
1935
                                    0ULL);
 
1936
 
 
1937
  first_record= 0;
 
1938
 
 
1939
  if (exec_tmp_table1)
 
1940
  {
 
1941
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1942
    exec_tmp_table1->file->ha_delete_all_rows();
 
1943
    free_io_cache(exec_tmp_table1);
 
1944
    filesort_free_buffers(exec_tmp_table1,0);
 
1945
  }
 
1946
  if (exec_tmp_table2)
 
1947
  {
 
1948
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1949
    exec_tmp_table2->file->ha_delete_all_rows();
 
1950
    free_io_cache(exec_tmp_table2);
 
1951
    filesort_free_buffers(exec_tmp_table2,0);
 
1952
  }
 
1953
  if (items0)
 
1954
    set_items_ref_array(items0);
 
1955
 
 
1956
  if (join_tab_save)
 
1957
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1958
 
 
1959
  if (tmp_join)
 
1960
    restore_tmp();
 
1961
 
 
1962
  /* Reset of sum functions */
 
1963
  if (sum_funcs)
 
1964
  {
 
1965
    Item_sum *func, **func_ptr= sum_funcs;
 
1966
    while ((func= *(func_ptr++)))
 
1967
      func->clear();
 
1968
  }
 
1969
 
 
1970
  return(0);
 
1971
}
 
1972
 
 
1973
/**
 
1974
   @brief Save the original join layout
 
1975
      
 
1976
   @details Saves the original join layout so it can be reused in 
 
1977
   re-execution and for EXPLAIN.
 
1978
             
 
1979
   @return Operation status
 
1980
   @retval 0      success.
 
1981
   @retval 1      error occurred.
 
1982
*/
 
1983
 
 
1984
bool
 
1985
JOIN::init_save_join_tab()
 
1986
{
 
1987
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
 
1988
    return 1;                                  /* purecov: inspected */
 
1989
  error= 0;                                    // Ensure that tmp_join.error= 0
 
1990
  restore_tmp();
 
1991
  return 0;
 
1992
}
 
1993
 
 
1994
 
 
1995
bool
 
1996
JOIN::save_join_tab()
 
1997
{
 
1998
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
1999
  {
 
2000
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
 
2001
                                                sizeof(JOIN_TAB) * tables)))
 
2002
      return 1;
 
2003
  }
 
2004
  return 0;
 
2005
}
 
2006
 
 
2007
 
 
2008
/**
 
2009
  Exec select.
 
2010
 
 
2011
  @todo
 
2012
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
2013
    finally replace sorting with index scan if there is a LIMIT clause in
 
2014
    the query.  It's never shown in EXPLAIN!
 
2015
 
 
2016
  @todo
 
2017
    When can we have here thd->net.report_error not zero?
 
2018
*/
 
2019
void
 
2020
JOIN::exec()
 
2021
{
 
2022
  List<Item> *columns_list= &fields_list;
 
2023
  int      tmp_error;
 
2024
 
 
2025
  thd_proc_info(thd, "executing");
 
2026
  error= 0;
 
2027
  (void) result->prepare2(); // Currently, this cannot fail.
 
2028
 
 
2029
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
2030
  {                                           // Only test of functions
 
2031
    if (select_options & SELECT_DESCRIBE)
 
2032
      select_describe(this, false, false, false,
 
2033
                      (zero_result_cause?zero_result_cause:"No tables used"));
 
2034
    else
 
2035
    {
 
2036
      result->send_fields(*columns_list,
 
2037
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2038
      /*
 
2039
        We have to test for 'conds' here as the WHERE may not be constant
 
2040
        even if we don't have any tables for prepared statements or if
 
2041
        conds uses something like 'rand()'.
 
2042
      */
 
2043
      if (cond_value != Item::COND_FALSE &&
 
2044
          (!conds || conds->val_int()) &&
 
2045
          (!having || having->val_int()))
 
2046
      {
 
2047
        if (do_send_rows && result->send_data(fields_list))
 
2048
          error= 1;
 
2049
        else
 
2050
        {
 
2051
          error= (int) result->send_eof();
 
2052
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
 
2053
                         thd->sent_row_count);
 
2054
        }
 
2055
      }
 
2056
      else
 
2057
      {
 
2058
        error=(int) result->send_eof();
 
2059
        send_records= 0;
 
2060
      }
 
2061
    }
 
2062
    /* Single select (without union) always returns 0 or 1 row */
 
2063
    thd->limit_found_rows= send_records;
 
2064
    thd->examined_row_count= 0;
 
2065
    return;
 
2066
  }
 
2067
  /*
 
2068
    Don't reset the found rows count if there're no tables as
 
2069
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
2070
    It must be accumulated from all join iterations of all join parts.
 
2071
  */
 
2072
  if (tables)
 
2073
    thd->limit_found_rows= 0;
 
2074
 
 
2075
  if (zero_result_cause)
 
2076
  {
 
2077
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
2078
                            *columns_list,
 
2079
                            send_row_on_empty_set(),
 
2080
                            select_options,
 
2081
                            zero_result_cause,
 
2082
                            having);
 
2083
    return;
 
2084
  }
 
2085
 
 
2086
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
2087
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
2088
    return;
 
2089
 
 
2090
  if (select_options & SELECT_DESCRIBE)
 
2091
  {
 
2092
    /*
 
2093
      Check if we managed to optimize ORDER BY away and don't use temporary
 
2094
      table to resolve ORDER BY: in that case, we only may need to do
 
2095
      filesort for GROUP BY.
 
2096
    */
 
2097
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
2098
    {
 
2099
      /*
 
2100
        Reset 'order' to 'group_list' and reinit variables describing
 
2101
        'order'
 
2102
      */
 
2103
      order= group_list;
 
2104
      simple_order= simple_group;
 
2105
      skip_sort_order= 0;
 
2106
    }
 
2107
    if (order && 
 
2108
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
 
2109
        (const_tables == tables ||
 
2110
         ((simple_order || skip_sort_order) &&
 
2111
          test_if_skip_sort_order(&join_tab[const_tables], order,
 
2112
                                  select_limit, 0, 
 
2113
                                  &join_tab[const_tables].table->
 
2114
                                    keys_in_use_for_query))))
 
2115
      order=0;
 
2116
    having= tmp_having;
 
2117
    select_describe(this, need_tmp,
 
2118
                    order != 0 && !skip_sort_order,
 
2119
                    select_distinct,
 
2120
                    !tables ? "No tables used" : NullS);
 
2121
    return;
 
2122
  }
 
2123
 
 
2124
  JOIN *curr_join= this;
 
2125
  List<Item> *curr_all_fields= &all_fields;
 
2126
  List<Item> *curr_fields_list= &fields_list;
 
2127
  TABLE *curr_tmp_table= 0;
 
2128
  /*
 
2129
    Initialize examined rows here because the values from all join parts
 
2130
    must be accumulated in examined_row_count. Hence every join
 
2131
    iteration must count from zero.
 
2132
  */
 
2133
  curr_join->examined_rows= 0;
 
2134
 
 
2135
  /* Create a tmp table if distinct or if the sort is too complicated */
 
2136
  if (need_tmp)
 
2137
  {
 
2138
    if (tmp_join)
 
2139
    {
 
2140
      /*
 
2141
        We are in a non cacheable sub query. Get the saved join structure
 
2142
        after optimization.
 
2143
        (curr_join may have been modified during last exection and we need
 
2144
        to reset it)
 
2145
      */
 
2146
      curr_join= tmp_join;
 
2147
    }
 
2148
    curr_tmp_table= exec_tmp_table1;
 
2149
 
 
2150
    /* Copy data to the temporary table */
 
2151
    thd_proc_info(thd, "Copying to tmp table");
 
2152
    if (!curr_join->sort_and_group &&
 
2153
        curr_join->const_tables != curr_join->tables)
 
2154
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2155
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2156
    {
 
2157
      error= tmp_error;
 
2158
      return;
 
2159
    }
 
2160
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
2161
    
 
2162
    if (curr_join->having)
 
2163
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
2164
    
 
2165
    /* Change sum_fields reference to calculated fields in tmp_table */
 
2166
    curr_join->all_fields= *curr_all_fields;
 
2167
    if (!items1)
 
2168
    {
 
2169
      items1= items0 + all_fields.elements;
 
2170
      if (sort_and_group || curr_tmp_table->group)
 
2171
      {
 
2172
        if (change_to_use_tmp_fields(thd, items1,
 
2173
                                     tmp_fields_list1, tmp_all_fields1,
 
2174
                                     fields_list.elements, all_fields))
 
2175
          return;
 
2176
      }
 
2177
      else
 
2178
      {
 
2179
        if (change_refs_to_tmp_fields(thd, items1,
 
2180
                                      tmp_fields_list1, tmp_all_fields1,
 
2181
                                      fields_list.elements, all_fields))
 
2182
          return;
 
2183
      }
 
2184
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
2185
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
2186
      curr_join->items1= items1;
 
2187
    }
 
2188
    curr_all_fields= &tmp_all_fields1;
 
2189
    curr_fields_list= &tmp_fields_list1;
 
2190
    curr_join->set_items_ref_array(items1);
 
2191
    
 
2192
    if (sort_and_group || curr_tmp_table->group)
 
2193
    {
 
2194
      curr_join->tmp_table_param.field_count+= 
 
2195
        curr_join->tmp_table_param.sum_func_count+
 
2196
        curr_join->tmp_table_param.func_count;
 
2197
      curr_join->tmp_table_param.sum_func_count= 
 
2198
        curr_join->tmp_table_param.func_count= 0;
 
2199
    }
 
2200
    else
 
2201
    {
 
2202
      curr_join->tmp_table_param.field_count+= 
 
2203
        curr_join->tmp_table_param.func_count;
 
2204
      curr_join->tmp_table_param.func_count= 0;
 
2205
    }
 
2206
    
 
2207
    if (curr_tmp_table->group)
 
2208
    {                                           // Already grouped
 
2209
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
2210
        curr_join->order= curr_join->group_list;  /* order by group */
 
2211
      curr_join->group_list= 0;
 
2212
    }
 
2213
    
 
2214
    /*
 
2215
      If we have different sort & group then we must sort the data by group
 
2216
      and copy it to another tmp table
 
2217
      This code is also used if we are using distinct something
 
2218
      we haven't been able to store in the temporary table yet
 
2219
      like SEC_TO_TIME(SUM(...)).
 
2220
    */
 
2221
 
 
2222
    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))
 
2223
    {                                   /* Must copy to another table */
 
2224
      /* Free first data from old join */
 
2225
      curr_join->join_free();
 
2226
      if (make_simple_join(curr_join, curr_tmp_table))
 
2227
        return;
 
2228
      calc_group_buffer(curr_join, group_list);
 
2229
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
2230
                        curr_join->tmp_all_fields1,
 
2231
                        curr_join->select_distinct && !curr_join->group_list);
 
2232
      curr_join->tmp_table_param.hidden_field_count= 
 
2233
        (curr_join->tmp_all_fields1.elements-
 
2234
         curr_join->tmp_fields_list1.elements);
 
2235
      
 
2236
      
 
2237
      if (exec_tmp_table2)
 
2238
        curr_tmp_table= exec_tmp_table2;
 
2239
      else
 
2240
      {
 
2241
        /* group data to new table */
 
2242
 
 
2243
        /*
 
2244
          If the access method is loose index scan then all MIN/MAX
 
2245
          functions are precomputed, and should be treated as regular
 
2246
          functions. See extended comment in JOIN::exec.
 
2247
        */
 
2248
        if (curr_join->join_tab->is_using_loose_index_scan())
 
2249
          curr_join->tmp_table_param.precomputed_group_by= true;
 
2250
 
 
2251
        if (!(curr_tmp_table=
 
2252
              exec_tmp_table2= create_tmp_table(thd,
 
2253
                                                &curr_join->tmp_table_param,
 
2254
                                                *curr_all_fields,
 
2255
                                                (ORDER*) 0,
 
2256
                                                curr_join->select_distinct && 
 
2257
                                                !curr_join->group_list,
 
2258
                                                1, curr_join->select_options,
 
2259
                                                HA_POS_ERROR,
 
2260
                                                (char *) "")))
 
2261
          return;
 
2262
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
2263
      }
 
2264
      if (curr_join->group_list)
 
2265
      {
 
2266
        thd_proc_info(thd, "Creating sort index");
 
2267
        if (curr_join->join_tab == join_tab && save_join_tab())
 
2268
        {
 
2269
          return;
 
2270
        }
 
2271
        if (create_sort_index(thd, curr_join, curr_join->group_list,
 
2272
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
 
2273
            make_group_fields(this, curr_join))
 
2274
        {
 
2275
          return;
 
2276
        }
 
2277
        sortorder= curr_join->sortorder;
 
2278
      }
 
2279
      
 
2280
      thd_proc_info(thd, "Copying to group table");
 
2281
      tmp_error= -1;
 
2282
      if (curr_join != this)
 
2283
      {
 
2284
        if (sum_funcs2)
 
2285
        {
 
2286
          curr_join->sum_funcs= sum_funcs2;
 
2287
          curr_join->sum_funcs_end= sum_funcs_end2; 
 
2288
        }
 
2289
        else
 
2290
        {
 
2291
          curr_join->alloc_func_list();
 
2292
          sum_funcs2= curr_join->sum_funcs;
 
2293
          sum_funcs_end2= curr_join->sum_funcs_end;
 
2294
        }
 
2295
      }
 
2296
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2297
                                        1, true))
 
2298
        return;
 
2299
      curr_join->group_list= 0;
 
2300
      if (!curr_join->sort_and_group &&
 
2301
          curr_join->const_tables != curr_join->tables)
 
2302
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
2303
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2304
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
2305
      {
 
2306
        error= tmp_error;
 
2307
        return;
 
2308
      }
 
2309
      end_read_record(&curr_join->join_tab->read_record);
 
2310
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
2311
      curr_join->join_tab[0].table= 0;           // Table is freed
 
2312
      
 
2313
      // No sum funcs anymore
 
2314
      if (!items2)
 
2315
      {
 
2316
        items2= items1 + all_fields.elements;
 
2317
        if (change_to_use_tmp_fields(thd, items2,
 
2318
                                     tmp_fields_list2, tmp_all_fields2, 
 
2319
                                     fields_list.elements, tmp_all_fields1))
 
2320
          return;
 
2321
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
2322
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
2323
      }
 
2324
      curr_fields_list= &curr_join->tmp_fields_list2;
 
2325
      curr_all_fields= &curr_join->tmp_all_fields2;
 
2326
      curr_join->set_items_ref_array(items2);
 
2327
      curr_join->tmp_table_param.field_count+= 
 
2328
        curr_join->tmp_table_param.sum_func_count;
 
2329
      curr_join->tmp_table_param.sum_func_count= 0;
 
2330
    }
 
2331
    if (curr_tmp_table->distinct)
 
2332
      curr_join->select_distinct=0;             /* Each row is unique */
 
2333
    
 
2334
    curr_join->join_free();                     /* Free quick selects */
 
2335
    if (curr_join->select_distinct && ! curr_join->group_list)
 
2336
    {
 
2337
      thd_proc_info(thd, "Removing duplicates");
 
2338
      if (curr_join->tmp_having)
 
2339
        curr_join->tmp_having->update_used_tables();
 
2340
      if (remove_duplicates(curr_join, curr_tmp_table,
 
2341
                            *curr_fields_list, curr_join->tmp_having))
 
2342
        return;
 
2343
      curr_join->tmp_having=0;
 
2344
      curr_join->select_distinct=0;
 
2345
    }
 
2346
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
2347
    if (make_simple_join(curr_join, curr_tmp_table))
 
2348
      return;
 
2349
    calc_group_buffer(curr_join, curr_join->group_list);
 
2350
    count_field_types(select_lex, &curr_join->tmp_table_param, 
 
2351
                      *curr_all_fields, 0);
 
2352
    
 
2353
  }
 
2354
  
 
2355
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
2356
  {
 
2357
    if (make_group_fields(this, curr_join))
 
2358
    {
 
2359
      return;
 
2360
    }
 
2361
    if (!items3)
 
2362
    {
 
2363
      if (!items0)
 
2364
        init_items_ref_array();
 
2365
      items3= ref_pointer_array + (all_fields.elements*4);
 
2366
      setup_copy_fields(thd, &curr_join->tmp_table_param,
 
2367
                        items3, tmp_fields_list3, tmp_all_fields3,
 
2368
                        curr_fields_list->elements, *curr_all_fields);
 
2369
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
2370
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
2371
      tmp_table_param.save_copy_field_end=
 
2372
        curr_join->tmp_table_param.copy_field_end;
 
2373
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
2374
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
2375
    }
 
2376
    else
 
2377
    {
 
2378
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
2379
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
2380
      curr_join->tmp_table_param.copy_field_end=
 
2381
        tmp_table_param.save_copy_field_end;
 
2382
    }
 
2383
    curr_fields_list= &tmp_fields_list3;
 
2384
    curr_all_fields= &tmp_all_fields3;
 
2385
    curr_join->set_items_ref_array(items3);
 
2386
 
 
2387
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
2388
                                      1, true) || 
 
2389
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
 
2390
        thd->is_fatal_error)
 
2391
      return;
 
2392
  }
 
2393
  if (curr_join->group_list || curr_join->order)
 
2394
  {
 
2395
    thd_proc_info(thd, "Sorting result");
 
2396
    /* If we have already done the group, add HAVING to sorted table */
 
2397
    if (curr_join->tmp_having && ! curr_join->group_list && 
 
2398
        ! curr_join->sort_and_group)
 
2399
    {
 
2400
      // Some tables may have been const
 
2401
      curr_join->tmp_having->update_used_tables();
 
2402
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
2403
      table_map used_tables= (curr_join->const_table_map |
 
2404
                              curr_table->table->map);
 
2405
 
 
2406
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
 
2407
                                                 used_tables,
 
2408
                                                 used_tables, 0);
 
2409
      if (sort_table_cond)
 
2410
      {
 
2411
        if (!curr_table->select)
 
2412
          if (!(curr_table->select= new SQL_SELECT))
 
2413
            return;
 
2414
        if (!curr_table->select->cond)
 
2415
          curr_table->select->cond= sort_table_cond;
 
2416
        else                                    // This should never happen
 
2417
        {
 
2418
          if (!(curr_table->select->cond=
 
2419
                new Item_cond_and(curr_table->select->cond,
 
2420
                                  sort_table_cond)))
 
2421
            return;
 
2422
          /*
 
2423
            Item_cond_and do not need fix_fields for execution, its parameters
 
2424
            are fixed or do not need fix_fields, too
 
2425
          */
 
2426
          curr_table->select->cond->quick_fix_field();
 
2427
        }
 
2428
        curr_table->select_cond= curr_table->select->cond;
 
2429
        curr_table->select_cond->top_level_item();
 
2430
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
2431
                                                   ~ (table_map) 0,
 
2432
                                                   ~used_tables, 0);
 
2433
      }
 
2434
    }
 
2435
    {
 
2436
      if (group)
 
2437
        curr_join->select_limit= HA_POS_ERROR;
 
2438
      else
 
2439
      {
 
2440
        /*
 
2441
          We can abort sorting after thd->select_limit rows if we there is no
 
2442
          WHERE clause for any tables after the sorted one.
 
2443
        */
 
2444
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
2445
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
2446
        for (; curr_table < end_table ; curr_table++)
 
2447
        {
 
2448
          /*
 
2449
            table->keyuse is set in the case there was an original WHERE clause
 
2450
            on the table that was optimized away.
 
2451
          */
 
2452
          if (curr_table->select_cond ||
 
2453
              (curr_table->keyuse && !curr_table->first_inner))
 
2454
          {
 
2455
            /* We have to sort all rows */
 
2456
            curr_join->select_limit= HA_POS_ERROR;
 
2457
            break;
 
2458
          }
 
2459
        }
 
2460
      }
 
2461
      if (curr_join->join_tab == join_tab && save_join_tab())
 
2462
      {
 
2463
        return;
 
2464
      }
 
2465
      /*
 
2466
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
 
2467
        chose FILESORT to be faster than INDEX SCAN or there is no 
 
2468
        suitable index present.
 
2469
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
2470
        finally replace sorting with index scan if there is a LIMIT clause in
 
2471
        the query. XXX: it's never shown in EXPLAIN!
 
2472
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
2473
      */
 
2474
      if (create_sort_index(thd, curr_join,
 
2475
                            curr_join->group_list ? 
 
2476
                            curr_join->group_list : curr_join->order,
 
2477
                            curr_join->select_limit,
 
2478
                            (select_options & OPTION_FOUND_ROWS ?
 
2479
                             HA_POS_ERROR : unit->select_limit_cnt),
 
2480
                            curr_join->group_list ? true : false))
 
2481
        return;
 
2482
      sortorder= curr_join->sortorder;
 
2483
      if (curr_join->const_tables != curr_join->tables &&
 
2484
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
2485
      {
 
2486
        /*
 
2487
          If no IO cache exists for the first table then we are using an
 
2488
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
2489
          attribute on the INDEX SCAN.
 
2490
        */
 
2491
        skip_sort_order= 1;
 
2492
      }
 
2493
    }
 
2494
  }
 
2495
  /* XXX: When can we have here thd->is_error() not zero? */
 
2496
  if (thd->is_error())
 
2497
  {
 
2498
    error= thd->is_error();
 
2499
    return;
 
2500
  }
 
2501
  curr_join->having= curr_join->tmp_having;
 
2502
  curr_join->fields= curr_fields_list;
 
2503
 
 
2504
  {
 
2505
    thd_proc_info(thd, "Sending data");
 
2506
    result->send_fields(*curr_fields_list,
 
2507
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
2508
    error= do_select(curr_join, curr_fields_list, NULL);
 
2509
    thd->limit_found_rows= curr_join->send_records;
 
2510
  }
 
2511
 
 
2512
  /* Accumulate the counts from all join iterations of all join parts. */
 
2513
  thd->examined_row_count+= curr_join->examined_rows;
 
2514
 
 
2515
  /* 
 
2516
    With EXPLAIN EXTENDED we have to restore original ref_array
 
2517
    for a derived table which is always materialized.
 
2518
    Otherwise we would not be able to print the query  correctly.
 
2519
  */ 
 
2520
  if (items0 &&
 
2521
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
 
2522
      select_lex->linkage == DERIVED_TABLE_TYPE)      
 
2523
    set_items_ref_array(items0);
 
2524
 
 
2525
  return;
 
2526
}
 
2527
 
 
2528
 
 
2529
/**
 
2530
  Clean up join.
 
2531
 
 
2532
  @return
 
2533
    Return error that hold JOIN.
 
2534
*/
 
2535
 
 
2536
int
 
2537
JOIN::destroy()
 
2538
{
 
2539
  select_lex->join= 0;
 
2540
 
 
2541
  if (tmp_join)
 
2542
  {
 
2543
    if (join_tab != tmp_join->join_tab)
 
2544
    {
 
2545
      JOIN_TAB *tab, *end;
 
2546
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
2547
        tab->cleanup();
 
2548
    }
 
2549
    tmp_join->tmp_join= 0;
 
2550
    tmp_table_param.copy_field=0;
 
2551
    return(tmp_join->destroy());
 
2552
  }
 
2553
  cond_equal= 0;
 
2554
 
 
2555
  cleanup(1);
 
2556
  if (exec_tmp_table1)
 
2557
    exec_tmp_table1->free_tmp_table(thd);
 
2558
  if (exec_tmp_table2)
 
2559
    exec_tmp_table2->free_tmp_table(thd);
 
2560
  delete select;
 
2561
  delete_dynamic(&keyuse);
 
2562
  return(error);
 
2563
}
 
2564
 
 
2565
 
 
2566
 
317
2567
/**
318
2568
  An entry point to single-unit select (a select without UNION).
319
2569
 
320
 
  @param session                  thread Cursor
 
2570
  @param thd                  thread handler
321
2571
  @param rref_pointer_array   a reference to ref_pointer_array of
322
2572
                              the top-level select_lex for this query
323
2573
  @param tables               list of all tables used in this query.
324
2574
                              The tables have been pre-opened.
325
 
  @param wild_num             number of wildcards used in the top level
 
2575
  @param wild_num             number of wildcards used in the top level 
326
2576
                              select of this query.
327
2577
                              For example statement
328
2578
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
333
2583
                              for a, b and c in this list.
334
2584
  @param conds                top level item of an expression representing
335
2585
                              WHERE clause of the top level select
336
 
  @param og_num               total number of order_st BY and GROUP BY clauses
 
2586
  @param og_num               total number of ORDER BY and GROUP BY clauses
337
2587
                              arguments
338
 
  @param order                linked list of order_st BY agruments
 
2588
  @param order                linked list of ORDER BY agruments
339
2589
  @param group                linked list of GROUP BY arguments
340
2590
  @param having               top level item of HAVING expression
 
2591
  @param proc_param           list of PROCEDUREs
341
2592
  @param select_options       select options (BIG_RESULT, etc)
342
2593
  @param result               an instance of result set handling class.
343
2594
                              This object is responsible for send result
344
2595
                              set rows to the client or inserting them
345
2596
                              into a table.
346
 
  @param select_lex           the only Select_Lex of this query
 
2597
  @param select_lex           the only SELECT_LEX of this query
347
2598
  @param unit                 top-level UNIT of this query
348
2599
                              UNIT is an artificial object created by the
349
2600
                              parser for every SELECT clause.
356
2607
  @retval
357
2608
    true   an error
358
2609
*/
359
 
bool mysql_select(Session *session,
360
 
                  Item ***rref_pointer_array,
361
 
                        TableList *tables, 
362
 
                  uint32_t wild_num, 
363
 
                  List<Item> &fields,
364
 
                        COND *conds, 
365
 
                  uint32_t og_num,  
366
 
                  order_st *order, 
367
 
                  order_st *group,
368
 
                        Item *having, 
369
 
                  uint64_t select_options,
370
 
                        select_result *result, 
371
 
                  Select_Lex_Unit *unit,
372
 
                        Select_Lex *select_lex)
 
2610
 
 
2611
bool
 
2612
mysql_select(THD *thd, Item ***rref_pointer_array,
 
2613
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
 
2614
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
 
2615
             Item *having, ORDER *proc_param, uint64_t select_options,
 
2616
             select_result *result, SELECT_LEX_UNIT *unit,
 
2617
             SELECT_LEX *select_lex)
373
2618
{
374
2619
  bool err;
375
2620
  bool free_join= 1;
384
2629
      creation
385
2630
    */
386
2631
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
387
 
        (select_options & SELECT_DESCRIBE))
 
2632
        (select_options & SELECT_DESCRIBE))
388
2633
    {
389
2634
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
390
2635
      {
391
 
        //here is EXPLAIN of subselect or derived table
392
 
        if (join->change_result(result))
393
 
        {
394
 
          return(true);
395
 
        }
 
2636
        //here is EXPLAIN of subselect or derived table
 
2637
        if (join->change_result(result))
 
2638
        {
 
2639
          return(true);
 
2640
        }
396
2641
      }
397
2642
      else
398
2643
      {
399
2644
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
400
 
                               conds, og_num, order, group, having, select_lex, unit)))
401
 
        {
402
 
          goto err;
403
 
        }
 
2645
                               conds, og_num, order, group, having, proc_param,
 
2646
                               select_lex, unit)))
 
2647
        {
 
2648
          goto err;
 
2649
        }
404
2650
      }
405
2651
    }
406
2652
    free_join= 0;
408
2654
  }
409
2655
  else
410
2656
  {
411
 
    if (!(join= new JOIN(session, fields, select_options, result)))
412
 
      return(true);
413
 
    session->set_proc_info("init");
414
 
    session->used_tables=0;                         // Updated by setup_fields
 
2657
    if (!(join= new JOIN(thd, fields, select_options, result)))
 
2658
        return(true);
 
2659
    thd_proc_info(thd, "init");
 
2660
    thd->used_tables=0;                         // Updated by setup_fields
415
2661
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
416
 
                           conds, og_num, order, group, having,
 
2662
                           conds, og_num, order, group, having, proc_param,
417
2663
                           select_lex, unit)) == true)
418
2664
    {
419
2665
      goto err;
420
2666
    }
421
2667
  }
422
2668
 
 
2669
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
 
2670
  if (join->flatten_subqueries())
 
2671
  {
 
2672
    err= 1;
 
2673
    goto err;
 
2674
  }
 
2675
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
 
2676
 
423
2677
  if ((err= join->optimize()))
424
2678
  {
425
2679
    goto err;                                   // 1
426
2680
  }
427
2681
 
428
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2682
  if (thd->lex->describe & DESCRIBE_EXTENDED)
429
2683
  {
430
2684
    join->conds_history= join->conds;
431
2685
    join->having_history= (join->having?join->having:join->tmp_having);
432
2686
  }
433
2687
 
434
 
  if (session->is_error())
 
2688
  if (thd->is_error())
435
2689
    goto err;
436
2690
 
437
2691
  join->exec();
438
2692
 
439
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2693
  if (thd->lex->describe & DESCRIBE_EXTENDED)
440
2694
  {
441
2695
    select_lex->where= join->conds_history;
442
2696
    select_lex->having= join->having_history;
445
2699
err:
446
2700
  if (free_join)
447
2701
  {
448
 
    session->set_proc_info("end");
 
2702
    thd_proc_info(thd, "end");
449
2703
    err|= select_lex->cleanup();
450
 
    return(err || session->is_error());
 
2704
    return(err || thd->is_error());
451
2705
  }
452
2706
  return(join->error);
453
2707
}
454
2708
 
455
 
inline Item *and_items(Item* cond, Item *item)
 
2709
 
 
2710
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
 
2711
                          Item_in_subselect* const *el2)
 
2712
{
 
2713
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
 
2714
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
 
2715
}
 
2716
 
 
2717
 
 
2718
inline Item * and_items(Item* cond, Item *item)
456
2719
{
457
2720
  return (cond? (new Item_cond_and(cond, item)) : item);
458
2721
}
459
2722
 
460
 
static void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
461
 
{
462
 
  List_iterator<TableList> it(*tlist);
463
 
  TableList *table;
 
2723
 
 
2724
static TABLE_LIST *alloc_join_nest(THD *thd)
 
2725
{
 
2726
  TABLE_LIST *tbl;
 
2727
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
 
2728
                                       sizeof(NESTED_JOIN))))
 
2729
    return NULL;
 
2730
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
 
2731
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
 
2732
  return tbl;
 
2733
}
 
2734
 
 
2735
 
 
2736
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
 
2737
{
 
2738
  List_iterator<TABLE_LIST> it(*tlist);
 
2739
  TABLE_LIST *table;
464
2740
  while ((table= it++))
465
2741
  {
466
2742
    if (table->on_expr)
470
2746
  }
471
2747
}
472
2748
 
 
2749
 
 
2750
/*
 
2751
  Convert a subquery predicate into a TABLE_LIST semi-join nest
 
2752
 
 
2753
  SYNOPSIS
 
2754
    convert_subq_to_sj()
 
2755
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
 
2756
                    clause
 
2757
       subq_pred    Subquery predicate to be converted
 
2758
  
 
2759
  DESCRIPTION
 
2760
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
 
2761
    prerequisites are already checked, so the conversion is always successfull.
 
2762
 
 
2763
    Prepared Statements: the transformation is permanent:
 
2764
     - Changes in TABLE_LIST structures are naturally permanent
 
2765
     - Item tree changes are performed on statement MEM_ROOT:
 
2766
        = we activate statement MEM_ROOT 
 
2767
        = this function is called before the first fix_prepare_information
 
2768
          call.
 
2769
 
 
2770
    This is intended because the criteria for subquery-to-sj conversion remain
 
2771
    constant for the lifetime of the Prepared Statement.
 
2772
 
 
2773
  RETURN
 
2774
    false  OK
 
2775
    true   Out of memory error
 
2776
*/
 
2777
 
 
2778
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
 
2779
{
 
2780
  SELECT_LEX *parent_lex= parent_join->select_lex;
 
2781
  TABLE_LIST *emb_tbl_nest= NULL;
 
2782
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
 
2783
  THD *thd= parent_join->thd;
 
2784
 
 
2785
  /*
 
2786
    1. Find out where to put the predicate into.
 
2787
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
 
2788
  */
 
2789
  if ((void*)subq_pred->expr_join_nest != (void*)1)
 
2790
  {
 
2791
    if (subq_pred->expr_join_nest->nested_join)
 
2792
    {
 
2793
      /*
 
2794
        We're dealing with
 
2795
 
 
2796
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
 
2797
 
 
2798
        The sj-nest will be inserted into the brackets nest.
 
2799
      */
 
2800
      emb_tbl_nest=  subq_pred->expr_join_nest;
 
2801
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2802
    }
 
2803
    else if (!subq_pred->expr_join_nest->outer_join)
 
2804
    {
 
2805
      /*
 
2806
        We're dealing with
 
2807
 
 
2808
          ... INNER JOIN tblX ON (subquery AND whatever) ...
 
2809
 
 
2810
        The sj-nest will be tblX's "sibling", i.e. another child of its
 
2811
        parent. This is ok because tblX is joined as an inner join.
 
2812
      */
 
2813
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
 
2814
      if (emb_tbl_nest)
 
2815
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
 
2816
    }
 
2817
    else if (!subq_pred->expr_join_nest->nested_join)
 
2818
    {
 
2819
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
 
2820
      TABLE_LIST *wrap_nest;
 
2821
      /*
 
2822
        We're dealing with
 
2823
 
 
2824
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
 
2825
 
 
2826
        we'll need to convert it into:
 
2827
 
 
2828
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
 
2829
                        |                      |
 
2830
                        |<----- wrap_nest ---->|
 
2831
        
 
2832
        Q:  other subqueries may be pointing to this element. What to do?
 
2833
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
 
2834
            But we'll need to fix other pointers.
 
2835
        A2: Another way: have TABLE_LIST::next_ptr so the following
 
2836
            subqueries know the table has been nested.
 
2837
        A3: changes in the TABLE_LIST::outer_join will make everything work
 
2838
            automatically.
 
2839
      */
 
2840
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
 
2841
      {
 
2842
        return(true);
 
2843
      }
 
2844
      wrap_nest->embedding= outer_tbl->embedding;
 
2845
      wrap_nest->join_list= outer_tbl->join_list;
 
2846
      wrap_nest->alias= (char*) "(sj-wrap)";
 
2847
 
 
2848
      wrap_nest->nested_join->join_list.empty();
 
2849
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
 
2850
 
 
2851
      outer_tbl->embedding= wrap_nest;
 
2852
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
 
2853
 
 
2854
      /*
 
2855
        wrap_nest will take place of outer_tbl, so move the outer join flag
 
2856
        and on_expr
 
2857
      */
 
2858
      wrap_nest->outer_join= outer_tbl->outer_join;
 
2859
      outer_tbl->outer_join= 0;
 
2860
 
 
2861
      wrap_nest->on_expr= outer_tbl->on_expr;
 
2862
      outer_tbl->on_expr= NULL;
 
2863
 
 
2864
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
 
2865
      TABLE_LIST *tbl;
 
2866
      while ((tbl= li++))
 
2867
      {
 
2868
        if (tbl == outer_tbl)
 
2869
        {
 
2870
          li.replace(wrap_nest);
 
2871
          break;
 
2872
        }
 
2873
      }
 
2874
      /*
 
2875
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
 
2876
        semi-join nest into it
 
2877
      */
 
2878
      emb_join_list= &wrap_nest->nested_join->join_list;
 
2879
      emb_tbl_nest=  wrap_nest;
 
2880
    }
 
2881
  }
 
2882
 
 
2883
  TABLE_LIST *sj_nest;
 
2884
  NESTED_JOIN *nested_join;
 
2885
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
 
2886
  {
 
2887
    return(true);
 
2888
  }
 
2889
  nested_join= sj_nest->nested_join;
 
2890
 
 
2891
  sj_nest->join_list= emb_join_list;
 
2892
  sj_nest->embedding= emb_tbl_nest;
 
2893
  sj_nest->alias= (char*) "(sj-nest)";
 
2894
  /* Nests do not participate in those 'chains', so: */
 
2895
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
 
2896
  emb_join_list->push_back(sj_nest);
 
2897
 
 
2898
  /* 
 
2899
    nested_join->used_tables and nested_join->not_null_tables are
 
2900
    initialized in simplify_joins().
 
2901
  */
 
2902
  
 
2903
  /* 
 
2904
    2. Walk through subquery's top list and set 'embedding' to point to the
 
2905
       sj-nest.
 
2906
  */
 
2907
  st_select_lex *subq_lex= subq_pred->unit->first_select();
 
2908
  nested_join->join_list.empty();
 
2909
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
 
2910
  TABLE_LIST *tl, *last_leaf;
 
2911
  while ((tl= li++))
 
2912
  {
 
2913
    tl->embedding= sj_nest;
 
2914
    tl->join_list= &nested_join->join_list;
 
2915
    nested_join->join_list.push_back(tl);
 
2916
  }
 
2917
  
 
2918
  /*
 
2919
    Reconnect the next_leaf chain.
 
2920
    TODO: Do we have to put subquery's tables at the end of the chain?
 
2921
          Inserting them at the beginning would be a bit faster.
 
2922
    NOTE: We actually insert them at the front! That's because the order is
 
2923
          reversed in this list.
 
2924
  */
 
2925
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
 
2926
  tl->next_leaf= subq_lex->leaf_tables;
 
2927
  last_leaf= tl;
 
2928
 
 
2929
  /*
 
2930
    Same as above for next_local chain
 
2931
    (a theory: a next_local chain always starts with ::leaf_tables
 
2932
     because view's tables are inserted after the view)
 
2933
  */
 
2934
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
 
2935
  tl->next_local= subq_lex->leaf_tables;
 
2936
 
 
2937
  /* A theory: no need to re-connect the next_global chain */
 
2938
 
 
2939
  /* 3. Remove the original subquery predicate from the WHERE/ON */
 
2940
 
 
2941
  // The subqueries were replaced for Item_int(1) earlier
 
2942
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
 
2943
  /*TODO: also reset the 'with_subselect' there. */
 
2944
 
 
2945
  /* n. Adjust the parent_join->tables counter */
 
2946
  uint table_no= parent_join->tables;
 
2947
  /* n. Walk through child's tables and adjust table->map */
 
2948
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
 
2949
  {
 
2950
    tl->table->tablenr= table_no;
 
2951
    tl->table->map= ((table_map)1) << table_no;
 
2952
    SELECT_LEX *old_sl= tl->select_lex;
 
2953
    tl->select_lex= parent_join->select_lex; 
 
2954
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
 
2955
      emb->select_lex= parent_join->select_lex;
 
2956
  }
 
2957
  parent_join->tables += subq_lex->join->tables;
 
2958
 
 
2959
  /* 
 
2960
    Put the subquery's WHERE into semi-join's sj_on_expr
 
2961
    Add the subquery-induced equalities too.
 
2962
  */
 
2963
  SELECT_LEX *save_lex= thd->lex->current_select;
 
2964
  thd->lex->current_select=subq_lex;
 
2965
  if (!subq_pred->left_expr->fixed &&
 
2966
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
 
2967
    return(true);
 
2968
  thd->lex->current_select=save_lex;
 
2969
 
 
2970
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
 
2971
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
 
2972
                                        subq_pred->left_expr->used_tables();
 
2973
  sj_nest->sj_on_expr= subq_lex->where;
 
2974
 
 
2975
  /*
 
2976
    Create the IN-equalities and inject them into semi-join's ON expression.
 
2977
    Additionally, for InsideOut strategy
 
2978
     - Record the number of IN-equalities.
 
2979
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
 
2980
       see which of the expressions are bound and which are not (for those
 
2981
       we'll produce a distinct stream of (ie_i1,...ie_ik).
 
2982
 
 
2983
       (TODO: can we just create a list of pointers and hope the expressions
 
2984
       will not substitute themselves on fix_fields()? or we need to wrap
 
2985
       them into Item_direct_view_refs and store pointers to those. The
 
2986
       pointers to Item_direct_view_refs are guaranteed to be stable as 
 
2987
       Item_direct_view_refs doesn't substitute itself with anything in 
 
2988
       Item_direct_view_ref::fix_fields.
 
2989
  */
 
2990
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
 
2991
  sj_nest->nested_join->sj_outer_expr_list.empty();
 
2992
 
 
2993
  if (subq_pred->left_expr->cols() == 1)
 
2994
  {
 
2995
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
 
2996
 
 
2997
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
 
2998
                                    subq_lex->ref_pointer_array[0]);
 
2999
    item_eq->name= (char*)subq_sj_cond_name;
 
3000
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3001
  }
 
3002
  else
 
3003
  {
 
3004
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
 
3005
    {
 
3006
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
 
3007
                                                element_index(i));
 
3008
      Item *item_eq= 
 
3009
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
 
3010
                         subq_lex->ref_pointer_array[i]);
 
3011
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
 
3012
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
 
3013
    }
 
3014
  }
 
3015
  /* Fix the created equality and AND */
 
3016
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
 
3017
 
 
3018
  /*
 
3019
    Walk through sj nest's WHERE and ON expressions and call
 
3020
    item->fix_table_changes() for all items.
 
3021
  */
 
3022
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
 
3023
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
 
3024
 
 
3025
 
 
3026
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
 
3027
  subq_lex->master_unit()->exclude_level();
 
3028
 
 
3029
  /* Inject sj_on_expr into the parent's WHERE or ON */
 
3030
  if (emb_tbl_nest)
 
3031
  {
 
3032
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
 
3033
                                     sj_nest->sj_on_expr);
 
3034
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
 
3035
  }
 
3036
  else
 
3037
  {
 
3038
    /* Inject into the WHERE */
 
3039
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
 
3040
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
 
3041
    parent_join->select_lex->where= parent_join->conds;
 
3042
  }
 
3043
 
 
3044
  return(false);
 
3045
}
 
3046
 
 
3047
 
 
3048
/*
 
3049
  Convert candidate subquery predicates to semi-joins
 
3050
 
 
3051
  SYNOPSIS
 
3052
    JOIN::flatten_subqueries()
 
3053
 
 
3054
  DESCRIPTION
 
3055
    Convert candidate subquery predicates to semi-joins.
 
3056
 
 
3057
  RETURN 
 
3058
    false  OK
 
3059
    true   Error
 
3060
*/
 
3061
 
 
3062
bool JOIN::flatten_subqueries()
 
3063
{
 
3064
  Item_in_subselect **in_subq;
 
3065
  Item_in_subselect **in_subq_end;
 
3066
 
 
3067
  if (sj_subselects.elements() == 0)
 
3068
    return(false);
 
3069
 
 
3070
  /* 1. Fix children subqueries */
 
3071
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
 
3072
       in_subq != in_subq_end; in_subq++)
 
3073
  {
 
3074
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3075
    child_join->outer_tables = child_join->tables;
 
3076
    if (child_join->flatten_subqueries())
 
3077
      return(true);
 
3078
    (*in_subq)->sj_convert_priority= 
 
3079
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
3080
  }
 
3081
 
 
3082
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
 
3083
  /* 
 
3084
    2. Pick which subqueries to convert:
 
3085
      sort the subquery array
 
3086
      - prefer correlated subqueries over uncorrelated;
 
3087
      - prefer subqueries that have greater number of outer tables;
 
3088
  */
 
3089
  sj_subselects.sort(subq_sj_candidate_cmp);
 
3090
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
3091
  /* Replace all subqueries to be flattened with Item_int(1) */
 
3092
  for (in_subq= sj_subselects.front(); 
 
3093
       in_subq != in_subq_end && 
 
3094
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3095
       in_subq++)
 
3096
  {
 
3097
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
3098
      return(true);
 
3099
  }
 
3100
 
 
3101
  for (in_subq= sj_subselects.front(); 
 
3102
       in_subq != in_subq_end && 
 
3103
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
3104
       in_subq++)
 
3105
  {
 
3106
    if (convert_subq_to_sj(this, *in_subq))
 
3107
      return(true);
 
3108
  }
 
3109
 
 
3110
  /* 3. Finalize those we didn't convert */
 
3111
  for (; in_subq!= in_subq_end; in_subq++)
 
3112
  {
 
3113
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
3114
    Item_subselect::trans_res res;
 
3115
    (*in_subq)->changed= 0;
 
3116
    (*in_subq)->fixed= 0;
 
3117
    res= (*in_subq)->select_transformer(child_join);
 
3118
    if (res == Item_subselect::RES_ERROR)
 
3119
      return(true);
 
3120
 
 
3121
    (*in_subq)->changed= 1;
 
3122
    (*in_subq)->fixed= 1;
 
3123
 
 
3124
    Item *substitute= (*in_subq)->substitution;
 
3125
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
3126
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
3127
      return(true);
 
3128
 
 
3129
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
 
3130
    //  return(true);
 
3131
  }
 
3132
  sj_subselects.clear();
 
3133
  return(false);
 
3134
}
 
3135
 
 
3136
 
 
3137
/**
 
3138
  Setup for execution all subqueries of a query, for which the optimizer
 
3139
  chose hash semi-join.
 
3140
 
 
3141
  @details Iterate over all subqueries of the query, and if they are under an
 
3142
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
3143
  - try to initialize all data structures needed for the materialized execution
 
3144
    of the IN predicate,
 
3145
  - if this fails, then perform the IN=>EXISTS transformation which was
 
3146
    previously blocked during JOIN::prepare.
 
3147
 
 
3148
  This method is part of the "code generation" query processing phase.
 
3149
 
 
3150
  This phase must be called after substitute_for_best_equal_field() because
 
3151
  that function may replace items with other items from a multiple equality,
 
3152
  and we need to reference the correct items in the index access method of the
 
3153
  IN predicate.
 
3154
 
 
3155
  @return Operation status
 
3156
  @retval false     success.
 
3157
  @retval true      error occurred.
 
3158
*/
 
3159
 
 
3160
bool JOIN::setup_subquery_materialization()
 
3161
{
 
3162
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
 
3163
       un= un->next_unit())
 
3164
  {
 
3165
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
 
3166
    {
 
3167
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
3168
      if (subquery_predicate &&
 
3169
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
3170
      {
 
3171
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
3172
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
3173
            in_subs->setup_engine())
 
3174
          return true;
 
3175
      }
 
3176
    }
 
3177
  }
 
3178
  return false;
 
3179
}
 
3180
 
 
3181
 
 
3182
/*
 
3183
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3184
 
 
3185
  SYNOPSIS
 
3186
    find_eq_ref_candidate()
 
3187
      table             Table to be checked
 
3188
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
 
3189
                        count.
 
3190
 
 
3191
  DESCRIPTION
 
3192
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
 
3193
 
 
3194
  TODO
 
3195
    Check again if it is feasible to factor common parts with constant table
 
3196
    search
 
3197
 
 
3198
  RETURN
 
3199
    true  - There exists an eq_ref(outer-tables) candidate
 
3200
    false - Otherwise
 
3201
*/
 
3202
 
 
3203
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
 
3204
{
 
3205
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
 
3206
  uint key;
 
3207
 
 
3208
  if (keyuse)
 
3209
  {
 
3210
    while (1) /* For each key */
 
3211
    {
 
3212
      key= keyuse->key;
 
3213
      KEY *keyinfo= table->key_info + key;
 
3214
      key_part_map bound_parts= 0;
 
3215
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
 
3216
      {
 
3217
        do  /* For all equalities on all key parts */
 
3218
        {
 
3219
          /* Check if this is "t.keypart = expr(outer_tables) */
 
3220
          if (!(keyuse->used_tables & sj_inner_tables) &&
 
3221
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
 
3222
          {
 
3223
            bound_parts |= 1 << keyuse->keypart;
 
3224
          }
 
3225
          keyuse++;
 
3226
        } while (keyuse->key == key && keyuse->table == table);
 
3227
 
 
3228
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
 
3229
          return true;
 
3230
        if (keyuse->table != table)
 
3231
          return false;
 
3232
      }
 
3233
      else
 
3234
      {
 
3235
        do
 
3236
        {
 
3237
          keyuse++;
 
3238
          if (keyuse->table != table)
 
3239
            return false;
 
3240
        }
 
3241
        while (keyuse->key == key);
 
3242
      }
 
3243
    }
 
3244
  }
 
3245
  return false;
 
3246
}
 
3247
 
 
3248
 
 
3249
/*
 
3250
  Pull tables out of semi-join nests, if possible
 
3251
 
 
3252
  SYNOPSIS
 
3253
    pull_out_semijoin_tables()
 
3254
      join  The join where to do the semi-join flattening
 
3255
 
 
3256
  DESCRIPTION
 
3257
    Try to pull tables out of semi-join nests.
 
3258
     
 
3259
    PRECONDITIONS
 
3260
    When this function is called, the join may have several semi-join nests
 
3261
    (possibly within different semi-join nests), but it is guaranteed that
 
3262
    one semi-join nest does not contain another.
 
3263
   
 
3264
    ACTION
 
3265
    A table can be pulled out of the semi-join nest if
 
3266
     - It is a constant table
 
3267
     - It is accessed 
 
3268
 
 
3269
    POSTCONDITIONS
 
3270
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
3271
       tables)
 
3272
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
3273
     * Semi-join nests TABLE_LIST::sj_inner_tables
 
3274
 
 
3275
    This operation is (and should be) performed at each PS execution since
 
3276
    tables may become/cease to be constant across PS reexecutions.
 
3277
 
 
3278
  RETURN 
 
3279
    0 - OK
 
3280
    1 - Out of memory error
 
3281
*/
 
3282
 
 
3283
int pull_out_semijoin_tables(JOIN *join)
 
3284
{
 
3285
  TABLE_LIST *sj_nest;
 
3286
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
 
3287
   
 
3288
  /* Try pulling out of the each of the semi-joins */
 
3289
  while ((sj_nest= sj_list_it++))
 
3290
  {
 
3291
    /* Action #1: Mark the constant tables to be pulled out */
 
3292
    table_map pulled_tables= 0;
 
3293
     
 
3294
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
 
3295
    TABLE_LIST *tbl;
 
3296
    while ((tbl= child_li++))
 
3297
    {
 
3298
      if (tbl->table)
 
3299
      {
 
3300
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
3301
        if (tbl->table->map & join->const_table_map)
 
3302
        {
 
3303
          pulled_tables |= tbl->table->map;
 
3304
        }
 
3305
      }
 
3306
    }
 
3307
    
 
3308
    /*
 
3309
      Action #2: Find which tables we can pull out based on
 
3310
      update_ref_and_keys() data. Note that pulling one table out can allow
 
3311
      us to pull out some other tables too.
 
3312
    */
 
3313
    bool pulled_a_table;
 
3314
    do 
 
3315
    {
 
3316
      pulled_a_table= false;
 
3317
      child_li.rewind();
 
3318
      while ((tbl= child_li++))
 
3319
      {
 
3320
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
3321
        {
 
3322
          if (find_eq_ref_candidate(tbl->table, 
 
3323
                                    sj_nest->nested_join->used_tables & 
 
3324
                                    ~pulled_tables))
 
3325
          {
 
3326
            pulled_a_table= true;
 
3327
            pulled_tables |= tbl->table->map;
 
3328
          }
 
3329
        }
 
3330
      }
 
3331
    } while (pulled_a_table);
 
3332
 
 
3333
    child_li.rewind();
 
3334
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
3335
    {
 
3336
      (sj_nest)->sj_inner_tables= 0;
 
3337
      while ((tbl= child_li++))
 
3338
      {
 
3339
        if (tbl->table)
 
3340
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3341
      }
 
3342
    }
 
3343
    else
 
3344
    {
 
3345
      /* Record the bitmap of inner tables, mark the inner tables */
 
3346
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
 
3347
                             ~pulled_tables;
 
3348
      (sj_nest)->sj_inner_tables= inner_tables;
 
3349
      while ((tbl= child_li++))
 
3350
      {
 
3351
        if (tbl->table)
 
3352
        {
 
3353
          if (inner_tables & tbl->table->map)
 
3354
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
3355
          else
 
3356
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
3357
        }
 
3358
      }
 
3359
    }
 
3360
  }
 
3361
  return(0);
 
3362
}
 
3363
 
473
3364
/*****************************************************************************
474
 
  Create JoinTableS, make a guess about the table types,
 
3365
  Create JOIN_TABS, make a guess about the table types,
475
3366
  Approximate how many records will be used in each table
476
3367
*****************************************************************************/
477
 
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit)
 
3368
 
 
3369
 
 
3370
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 
3371
                                      TABLE *table,
 
3372
                                      const key_map *keys,ha_rows limit)
478
3373
{
479
3374
  int error;
480
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
 
3375
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
481
3376
    return(0);                           // Fatal error flag is set
482
3377
  if (select)
483
3378
  {
484
3379
    select->head=table;
485
3380
    table->reginfo.impossible_range=0;
486
 
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
 
3381
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
487
3382
                                          limit, 0, false)) == 1)
488
3383
      return(select->quick->records);
489
3384
    if (error == -1)
495
3390
  return(HA_POS_ERROR);                 /* This shouldn't happend */
496
3391
}
497
3392
 
 
3393
/*
 
3394
   This structure is used to collect info on potentially sargable
 
3395
   predicates in order to check whether they become sargable after
 
3396
   reading const tables.
 
3397
   We form a bitmap of indexes that can be used for sargable predicates.
 
3398
   Only such indexes are involved in range analysis.
 
3399
*/
 
3400
typedef struct st_sargable_param
 
3401
{
 
3402
  Field *field;              /* field against which to check sargability */
 
3403
  Item **arg_value;          /* values of potential keys for lookups     */
 
3404
  uint num_values;           /* number of values in the above array      */
 
3405
} SARGABLE_PARAM;  
 
3406
 
 
3407
/**
 
3408
  Calculate the best possible join and initialize the join structure.
 
3409
 
 
3410
  @retval
 
3411
    0   ok
 
3412
  @retval
 
3413
    1   Fatal error
 
3414
*/
 
3415
 
 
3416
static bool
 
3417
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
 
3418
                     DYNAMIC_ARRAY *keyuse_array)
 
3419
{
 
3420
  int error;
 
3421
  TABLE *table;
 
3422
  uint i,table_count,const_count,key;
 
3423
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
3424
  key_map const_ref, eq_part;
 
3425
  TABLE **table_vector;
 
3426
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
3427
  KEYUSE *keyuse,*start_keyuse;
 
3428
  table_map outer_join=0;
 
3429
  SARGABLE_PARAM *sargables= 0;
 
3430
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
3431
 
 
3432
  table_count=join->tables;
 
3433
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
 
3434
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
3435
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
 
3436
  if (!stat || !stat_ref || !table_vector)
 
3437
    return(1);                          // Eom /* purecov: inspected */
 
3438
 
 
3439
  join->best_ref=stat_vector;
 
3440
 
 
3441
  stat_end=stat+table_count;
 
3442
  found_const_table_map= all_table_map=0;
 
3443
  const_count=0;
 
3444
 
 
3445
  for (s= stat, i= 0;
 
3446
       tables;
 
3447
       s++, tables= tables->next_leaf, i++)
 
3448
  {
 
3449
    TABLE_LIST *embedding= tables->embedding;
 
3450
    stat_vector[i]=s;
 
3451
    s->keys.init();
 
3452
    s->const_keys.init();
 
3453
    s->checked_keys.init();
 
3454
    s->needed_reg.init();
 
3455
    table_vector[i]=s->table=table=tables->table;
 
3456
    table->pos_in_table_list= tables;
 
3457
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
3458
    if(error)
 
3459
    {
 
3460
        table->file->print_error(error, MYF(0));
 
3461
        return(1);
 
3462
    }
 
3463
    table->quick_keys.clear_all();
 
3464
    table->reginfo.join_tab=s;
 
3465
    table->reginfo.not_exists_optimize=0;
 
3466
    memset(table->const_key_parts, 0,
 
3467
           sizeof(key_part_map)*table->s->keys);
 
3468
    all_table_map|= table->map;
 
3469
    s->join=join;
 
3470
    s->info=0;                                  // For describe
 
3471
 
 
3472
    s->dependent= tables->dep_tables;
 
3473
    s->key_dependent= 0;
 
3474
    if (tables->schema_table)
 
3475
      table->file->stats.records= 2;
 
3476
    table->quick_condition_rows= table->file->stats.records;
 
3477
 
 
3478
    s->on_expr_ref= &tables->on_expr;
 
3479
    if (*s->on_expr_ref)
 
3480
    {
 
3481
      /* s is the only inner table of an outer join */
 
3482
      if (!table->file->stats.records && !embedding)
 
3483
      {                                         // Empty table
 
3484
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
3485
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3486
        continue;
 
3487
      }
 
3488
      outer_join|= table->map;
 
3489
      s->embedding_map= 0;
 
3490
      for (;embedding; embedding= embedding->embedding)
 
3491
        s->embedding_map|= embedding->nested_join->nj_map;
 
3492
      continue;
 
3493
    }
 
3494
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
3495
    {
 
3496
      /* s belongs to a nested join, maybe to several embedded joins */
 
3497
      s->embedding_map= 0;
 
3498
      do
 
3499
      {
 
3500
        NESTED_JOIN *nested_join= embedding->nested_join;
 
3501
        s->embedding_map|=nested_join->nj_map;
 
3502
        s->dependent|= embedding->dep_tables;
 
3503
        embedding= embedding->embedding;
 
3504
        outer_join|= nested_join->used_tables;
 
3505
      }
 
3506
      while (embedding);
 
3507
      continue;
 
3508
    }
 
3509
    if ((table->file->stats.records <= 1) &&
 
3510
        !s->dependent &&
 
3511
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
 
3512
    {
 
3513
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
3514
    }
 
3515
  }
 
3516
  stat_vector[i]=0;
 
3517
  join->outer_join=outer_join;
 
3518
 
 
3519
  if (join->outer_join)
 
3520
  {
 
3521
    /* 
 
3522
       Build transitive closure for relation 'to be dependent on'.
 
3523
       This will speed up the plan search for many cases with outer joins,
 
3524
       as well as allow us to catch illegal cross references/
 
3525
       Warshall's algorithm is used to build the transitive closure.
 
3526
       As we use bitmaps to represent the relation the complexity
 
3527
       of the algorithm is O((number of tables)^2). 
 
3528
    */
 
3529
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3530
    {
 
3531
      for (uint j= 0 ; j < table_count ; j++)
 
3532
      {
 
3533
        table= stat[j].table;
 
3534
        if (s->dependent & table->map)
 
3535
          s->dependent |= table->reginfo.join_tab->dependent;
 
3536
      }
 
3537
      if (s->dependent)
 
3538
        s->table->maybe_null= 1;
 
3539
    }
 
3540
    /* Catch illegal cross references for outer joins */
 
3541
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
3542
    {
 
3543
      if (s->dependent & s->table->map)
 
3544
      {
 
3545
        join->tables=0;                 // Don't use join->table
 
3546
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
3547
        return(1);
 
3548
      }
 
3549
      s->key_dependent= s->dependent;
 
3550
    }
 
3551
  }
 
3552
 
 
3553
  if (conds || outer_join)
 
3554
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
 
3555
                            conds, join->cond_equal,
 
3556
                            ~outer_join, join->select_lex, &sargables))
 
3557
      return(1);
 
3558
 
 
3559
  /* Read tables with 0 or 1 rows (system tables) */
 
3560
  join->const_table_map= 0;
 
3561
 
 
3562
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
3563
       p_pos < p_end ;
 
3564
       p_pos++)
 
3565
  {
 
3566
    int tmp;
 
3567
    s= p_pos->table;
 
3568
    s->type=JT_SYSTEM;
 
3569
    join->const_table_map|=s->table->map;
 
3570
    if ((tmp=join_read_const_table(s, p_pos)))
 
3571
    {
 
3572
      if (tmp > 0)
 
3573
        return(1);                      // Fatal error
 
3574
    }
 
3575
    else
 
3576
      found_const_table_map|= s->table->map;
 
3577
  }
 
3578
 
 
3579
  /* loop until no more const tables are found */
 
3580
  int ref_changed;
 
3581
  do
 
3582
  {
 
3583
  more_const_tables_found:
 
3584
    ref_changed = 0;
 
3585
    found_ref=0;
 
3586
 
 
3587
    /*
 
3588
      We only have to loop from stat_vector + const_count as
 
3589
      set_position() will move all const_tables first in stat_vector
 
3590
    */
 
3591
 
 
3592
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
3593
    {
 
3594
      table=s->table;
 
3595
 
 
3596
      /* 
 
3597
        If equi-join condition by a key is null rejecting and after a
 
3598
        substitution of a const table the key value happens to be null
 
3599
        then we can state that there are no matches for this equi-join.
 
3600
      */  
 
3601
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
3602
      {
 
3603
        /* 
 
3604
          When performing an outer join operation if there are no matching rows
 
3605
          for the single row of the outer table all the inner tables are to be
 
3606
          null complemented and thus considered as constant tables.
 
3607
          Here we apply this consideration to the case of outer join operations 
 
3608
          with a single inner table only because the case with nested tables
 
3609
          would require a more thorough analysis.
 
3610
          TODO. Apply single row substitution to null complemented inner tables
 
3611
          for nested outer join operations. 
 
3612
        */              
 
3613
        while (keyuse->table == table)
 
3614
        {
 
3615
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
3616
              keyuse->val->is_null() && keyuse->null_rejecting)
 
3617
          {
 
3618
            s->type= JT_CONST;
 
3619
            mark_as_null_row(table);
 
3620
            found_const_table_map|= table->map;
 
3621
            join->const_table_map|= table->map;
 
3622
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
3623
            goto more_const_tables_found;
 
3624
           }
 
3625
          keyuse++;
 
3626
        }
 
3627
      }
 
3628
 
 
3629
      if (s->dependent)                         // If dependent on some table
 
3630
      {
 
3631
        // All dep. must be constants
 
3632
        if (s->dependent & ~(found_const_table_map))
 
3633
          continue;
 
3634
        if (table->file->stats.records <= 1L &&
 
3635
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
3636
            !table->pos_in_table_list->embedding)
 
3637
        {                                       // system table
 
3638
          int tmp= 0;
 
3639
          s->type=JT_SYSTEM;
 
3640
          join->const_table_map|=table->map;
 
3641
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
3642
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
3643
          {
 
3644
            if (tmp > 0)
 
3645
              return(1);                        // Fatal error
 
3646
          }
 
3647
          else
 
3648
            found_const_table_map|= table->map;
 
3649
          continue;
 
3650
        }
 
3651
      }
 
3652
      /* check if table can be read by key or table only uses const refs */
 
3653
      if ((keyuse=s->keyuse))
 
3654
      {
 
3655
        s->type= JT_REF;
 
3656
        while (keyuse->table == table)
 
3657
        {
 
3658
          start_keyuse=keyuse;
 
3659
          key=keyuse->key;
 
3660
          s->keys.set_bit(key);               // QQ: remove this ?
 
3661
 
 
3662
          refs=0;
 
3663
          const_ref.clear_all();
 
3664
          eq_part.clear_all();
 
3665
          do
 
3666
          {
 
3667
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
3668
            {
 
3669
              if (!((~found_const_table_map) & keyuse->used_tables))
 
3670
                const_ref.set_bit(keyuse->keypart);
 
3671
              else
 
3672
                refs|=keyuse->used_tables;
 
3673
              eq_part.set_bit(keyuse->keypart);
 
3674
            }
 
3675
            keyuse++;
 
3676
          } while (keyuse->table == table && keyuse->key == key);
 
3677
 
 
3678
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
 
3679
              !table->pos_in_table_list->embedding)
 
3680
          {
 
3681
            if ((table->key_info[key].flags & (HA_NOSAME))
 
3682
                 == HA_NOSAME)
 
3683
            {
 
3684
              if (const_ref == eq_part)
 
3685
              {                                 // Found everything for ref.
 
3686
                int tmp;
 
3687
                ref_changed = 1;
 
3688
                s->type= JT_CONST;
 
3689
                join->const_table_map|=table->map;
 
3690
                set_position(join,const_count++,s,start_keyuse);
 
3691
                if (create_ref_for_key(join, s, start_keyuse,
 
3692
                                       found_const_table_map))
 
3693
                  return(1);
 
3694
                if ((tmp=join_read_const_table(s,
 
3695
                                               join->positions+const_count-1)))
 
3696
                {
 
3697
                  if (tmp > 0)
 
3698
                    return(1);                  // Fatal error
 
3699
                }
 
3700
                else
 
3701
                  found_const_table_map|= table->map;
 
3702
                break;
 
3703
              }
 
3704
              else
 
3705
                found_ref|= refs;      // Table is const if all refs are const
 
3706
            }
 
3707
            else if (const_ref == eq_part)
 
3708
              s->const_keys.set_bit(key);
 
3709
          }
 
3710
        }
 
3711
      }
 
3712
    }
 
3713
  } while (join->const_table_map & found_ref && ref_changed);
 
3714
 
 
3715
  /* 
 
3716
    Update info on indexes that can be used for search lookups as
 
3717
    reading const tables may has added new sargable predicates. 
 
3718
  */
 
3719
  if (const_count && sargables)
 
3720
  {
 
3721
    for( ; sargables->field ; sargables++)
 
3722
    {
 
3723
      Field *field= sargables->field;
 
3724
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
3725
      key_map possible_keys= field->key_start;
 
3726
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
3727
      bool is_const= 1;
 
3728
      for (uint j=0; j < sargables->num_values; j++)
 
3729
        is_const&= sargables->arg_value[j]->const_item();
 
3730
      if (is_const)
 
3731
        join_tab[0].const_keys.merge(possible_keys);
 
3732
    }
 
3733
  }
 
3734
 
 
3735
  if (pull_out_semijoin_tables(join))
 
3736
    return(true);
 
3737
 
 
3738
  /* Calc how many (possible) matched records in each table */
 
3739
 
 
3740
  for (s=stat ; s < stat_end ; s++)
 
3741
  {
 
3742
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
3743
    {
 
3744
      /* Only one matching row */
 
3745
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
3746
      continue;
 
3747
    }
 
3748
    /* Approximate found rows and time to read them */
 
3749
    s->found_records=s->records=s->table->file->stats.records;
 
3750
    s->read_time=(ha_rows) s->table->file->scan_time();
 
3751
 
 
3752
    /*
 
3753
      Set a max range of how many seeks we can expect when using keys
 
3754
      This is can't be to high as otherwise we are likely to use
 
3755
      table scan.
 
3756
    */
 
3757
    s->worst_seeks= min((double) s->found_records / 10,
 
3758
                        (double) s->read_time*3);
 
3759
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
3760
      s->worst_seeks=2.0;
 
3761
 
 
3762
    /*
 
3763
      Add to stat->const_keys those indexes for which all group fields or
 
3764
      all select distinct fields participate in one index.
 
3765
    */
 
3766
    add_group_and_distinct_keys(join, s);
 
3767
 
 
3768
    if (!s->const_keys.is_clear_all() &&
 
3769
        !s->table->pos_in_table_list->embedding)
 
3770
    {
 
3771
      ha_rows records;
 
3772
      SQL_SELECT *select;
 
3773
      select= make_select(s->table, found_const_table_map,
 
3774
                          found_const_table_map,
 
3775
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
 
3776
                          1, &error);
 
3777
      if (!select)
 
3778
        return(1);
 
3779
      records= get_quick_record_count(join->thd, select, s->table,
 
3780
                                      &s->const_keys, join->row_limit);
 
3781
      s->quick=select->quick;
 
3782
      s->needed_reg=select->needed_reg;
 
3783
      select->quick=0;
 
3784
      if (records == 0 && s->table->reginfo.impossible_range)
 
3785
      {
 
3786
        /*
 
3787
          Impossible WHERE or ON expression
 
3788
          In case of ON, we mark that the we match one empty NULL row.
 
3789
          In case of WHERE, don't set found_const_table_map to get the
 
3790
          caller to abort with a zero row result.
 
3791
        */
 
3792
        join->const_table_map|= s->table->map;
 
3793
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
3794
        s->type= JT_CONST;
 
3795
        if (*s->on_expr_ref)
 
3796
        {
 
3797
          /* Generate empty row */
 
3798
          s->info= "Impossible ON condition";
 
3799
          found_const_table_map|= s->table->map;
 
3800
          s->type= JT_CONST;
 
3801
          mark_as_null_row(s->table);           // All fields are NULL
 
3802
        }
 
3803
      }
 
3804
      if (records != HA_POS_ERROR)
 
3805
      {
 
3806
        s->found_records=records;
 
3807
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
3808
      }
 
3809
      delete select;
 
3810
    }
 
3811
  }
 
3812
 
 
3813
  join->join_tab=stat;
 
3814
  join->map2table=stat_ref;
 
3815
  join->table= join->all_tables=table_vector;
 
3816
  join->const_tables=const_count;
 
3817
  join->found_const_table_map=found_const_table_map;
 
3818
 
 
3819
  /* Find an optimal join order of the non-constant tables. */
 
3820
  if (join->const_tables != join->tables)
 
3821
  {
 
3822
    optimize_keyuse(join, keyuse_array);
 
3823
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
3824
      return(true);
 
3825
  }
 
3826
  else
 
3827
  {
 
3828
    memcpy(join->best_positions, join->positions,
 
3829
           sizeof(POSITION)*join->const_tables);
 
3830
    join->best_read=1.0;
 
3831
  }
 
3832
  /* Generate an execution plan from the found optimal join order. */
 
3833
  return(join->thd->killed || get_best_combination(join));
 
3834
}
 
3835
 
 
3836
 
498
3837
/*****************************************************************************
499
3838
  Check with keys are used and with tables references with tables
500
3839
  Updates in stat:
503
3842
          keyuse     Pointer to possible keys
504
3843
*****************************************************************************/
505
3844
 
 
3845
/// Used when finding key fields
 
3846
typedef struct key_field_t {
 
3847
  Field         *field;
 
3848
  Item          *val;                   ///< May be empty if diff constant
 
3849
  uint          level;
 
3850
  uint          optimize; // KEY_OPTIMIZE_*
 
3851
  bool          eq_func;
 
3852
  /**
 
3853
    If true, the condition this struct represents will not be satisfied
 
3854
    when val IS NULL.
 
3855
  */
 
3856
  bool          null_rejecting; 
 
3857
  bool          *cond_guard; /* See KEYUSE::cond_guard */
 
3858
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
 
3859
} KEY_FIELD;
 
3860
 
 
3861
/**
 
3862
  Merge new key definitions to old ones, remove those not used in both.
 
3863
 
 
3864
  This is called for OR between different levels.
 
3865
 
 
3866
  To be able to do 'ref_or_null' we merge a comparison of a column
 
3867
  and 'column IS NULL' to one test.  This is useful for sub select queries
 
3868
  that are internally transformed to something like:.
 
3869
 
 
3870
  @code
 
3871
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
 
3872
  @endcode
 
3873
 
 
3874
  KEY_FIELD::null_rejecting is processed as follows: @n
 
3875
  result has null_rejecting=true if it is set for both ORed references.
 
3876
  for example:
 
3877
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
3878
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
3879
 
 
3880
  @todo
 
3881
    The result of this is that we're missing some 'ref' accesses.
 
3882
    OptimizerTeam: Fix this
 
3883
*/
 
3884
 
 
3885
static KEY_FIELD *
 
3886
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
 
3887
                 uint and_level)
 
3888
{
 
3889
  if (start == new_fields)
 
3890
    return start;                               // Impossible or
 
3891
  if (new_fields == end)
 
3892
    return start;                               // No new fields, skip all
 
3893
 
 
3894
  KEY_FIELD *first_free=new_fields;
 
3895
 
 
3896
  /* Mark all found fields in old array */
 
3897
  for (; new_fields != end ; new_fields++)
 
3898
  {
 
3899
    for (KEY_FIELD *old=start ; old != first_free ; old++)
 
3900
    {
 
3901
      if (old->field == new_fields->field)
 
3902
      {
 
3903
        /*
 
3904
          NOTE: below const_item() call really works as "!used_tables()", i.e.
 
3905
          it can return false where it is feasible to make it return true.
 
3906
          
 
3907
          The cause is as follows: Some of the tables are already known to be
 
3908
          const tables (the detection code is in make_join_statistics(),
 
3909
          above the update_ref_and_keys() call), but we didn't propagate 
 
3910
          information about this: TABLE::const_table is not set to true, and
 
3911
          Item::update_used_tables() hasn't been called for each item.
 
3912
          The result of this is that we're missing some 'ref' accesses.
 
3913
          TODO: OptimizerTeam: Fix this
 
3914
        */
 
3915
        if (!new_fields->val->const_item())
 
3916
        {
 
3917
          /*
 
3918
            If the value matches, we can use the key reference.
 
3919
            If not, we keep it until we have examined all new values
 
3920
          */
 
3921
          if (old->val->eq(new_fields->val, old->field->binary()))
 
3922
          {
 
3923
            old->level= and_level;
 
3924
            old->optimize= ((old->optimize & new_fields->optimize &
 
3925
                             KEY_OPTIMIZE_EXISTS) |
 
3926
                            ((old->optimize | new_fields->optimize) &
 
3927
                             KEY_OPTIMIZE_REF_OR_NULL));
 
3928
            old->null_rejecting= (old->null_rejecting &&
 
3929
                                  new_fields->null_rejecting);
 
3930
          }
 
3931
        }
 
3932
        else if (old->eq_func && new_fields->eq_func &&
 
3933
                 old->val->eq_by_collation(new_fields->val, 
 
3934
                                           old->field->binary(),
 
3935
                                           old->field->charset()))
 
3936
 
 
3937
        {
 
3938
          old->level= and_level;
 
3939
          old->optimize= ((old->optimize & new_fields->optimize &
 
3940
                           KEY_OPTIMIZE_EXISTS) |
 
3941
                          ((old->optimize | new_fields->optimize) &
 
3942
                           KEY_OPTIMIZE_REF_OR_NULL));
 
3943
          old->null_rejecting= (old->null_rejecting &&
 
3944
                                new_fields->null_rejecting);
 
3945
        }
 
3946
        else if (old->eq_func && new_fields->eq_func &&
 
3947
                 ((old->val->const_item() && old->val->is_null()) || 
 
3948
                  new_fields->val->is_null()))
 
3949
        {
 
3950
          /* field = expression OR field IS NULL */
 
3951
          old->level= and_level;
 
3952
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
3953
          /*
 
3954
            Remember the NOT NULL value unless the value does not depend
 
3955
            on other tables.
 
3956
          */
 
3957
          if (!old->val->used_tables() && old->val->is_null())
 
3958
            old->val= new_fields->val;
 
3959
          /* The referred expression can be NULL: */ 
 
3960
          old->null_rejecting= 0;
 
3961
        }
 
3962
        else
 
3963
        {
 
3964
          /*
 
3965
            We are comparing two different const.  In this case we can't
 
3966
            use a key-lookup on this so it's better to remove the value
 
3967
            and let the range optimzier handle it
 
3968
          */
 
3969
          if (old == --first_free)              // If last item
 
3970
            break;
 
3971
          *old= *first_free;                    // Remove old value
 
3972
          old--;                                // Retry this value
 
3973
        }
 
3974
      }
 
3975
    }
 
3976
  }
 
3977
  /* Remove all not used items */
 
3978
  for (KEY_FIELD *old=start ; old != first_free ;)
 
3979
  {
 
3980
    if (old->level != and_level)
 
3981
    {                                           // Not used in all levels
 
3982
      if (old == --first_free)
 
3983
        break;
 
3984
      *old= *first_free;                        // Remove old value
 
3985
      continue;
 
3986
    }
 
3987
    old++;
 
3988
  }
 
3989
  return first_free;
 
3990
}
 
3991
 
 
3992
 
 
3993
/**
 
3994
  Add a possible key to array of possible keys if it's usable as a key
 
3995
 
 
3996
    @param key_fields      Pointer to add key, if usable
 
3997
    @param and_level       And level, to be stored in KEY_FIELD
 
3998
    @param cond            Condition predicate
 
3999
    @param field           Field used in comparision
 
4000
    @param eq_func         True if we used =, <=> or IS NULL
 
4001
    @param value           Value used for comparison with field
 
4002
    @param usable_tables   Tables which can be used for key optimization
 
4003
    @param sargables       IN/OUT Array of found sargable candidates
 
4004
 
 
4005
  @note
 
4006
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
4007
    table, we store this to be able to do not exists optimization later.
 
4008
 
 
4009
  @returns
 
4010
    *key_fields is incremented if we stored a key in the array
 
4011
*/
 
4012
 
 
4013
static void
 
4014
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
 
4015
              Field *field, bool eq_func, Item **value, uint num_values,
 
4016
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
4017
{
 
4018
  uint exists_optimize= 0;
 
4019
  if (!(field->flags & PART_KEY_FLAG))
 
4020
  {
 
4021
    // Don't remove column IS NULL on a LEFT JOIN table
 
4022
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4023
        !field->table->maybe_null || field->null_ptr)
 
4024
      return;                                   // Not a key. Skip it
 
4025
    exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4026
    assert(num_values == 1);
 
4027
  }
 
4028
  else
 
4029
  {
 
4030
    table_map used_tables=0;
 
4031
    bool optimizable=0;
 
4032
    for (uint i=0; i<num_values; i++)
 
4033
    {
 
4034
      used_tables|=(value[i])->used_tables();
 
4035
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
 
4036
        optimizable=1;
 
4037
    }
 
4038
    if (!optimizable)
 
4039
      return;
 
4040
    if (!(usable_tables & field->table->map))
 
4041
    {
 
4042
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
 
4043
          !field->table->maybe_null || field->null_ptr)
 
4044
        return;                                 // Can't use left join optimize
 
4045
      exists_optimize= KEY_OPTIMIZE_EXISTS;
 
4046
    }
 
4047
    else
 
4048
    {
 
4049
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
4050
      key_map possible_keys=field->key_start;
 
4051
      possible_keys.intersect(field->table->keys_in_use_for_query);
 
4052
      stat[0].keys.merge(possible_keys);             // Add possible keys
 
4053
 
 
4054
      /*
 
4055
        Save the following cases:
 
4056
        Field op constant
 
4057
        Field LIKE constant where constant doesn't start with a wildcard
 
4058
        Field = field2 where field2 is in a different table
 
4059
        Field op formula
 
4060
        Field IS NULL
 
4061
        Field IS NOT NULL
 
4062
         Field BETWEEN ...
 
4063
         Field IN ...
 
4064
      */
 
4065
      stat[0].key_dependent|=used_tables;
 
4066
 
 
4067
      bool is_const=1;
 
4068
      for (uint i=0; i<num_values; i++)
 
4069
      {
 
4070
        if (!(is_const&= value[i]->const_item()))
 
4071
          break;
 
4072
      }
 
4073
      if (is_const)
 
4074
        stat[0].const_keys.merge(possible_keys);
 
4075
      else if (!eq_func)
 
4076
      {
 
4077
        /* 
 
4078
          Save info to be able check whether this predicate can be 
 
4079
          considered as sargable for range analisis after reading const tables.
 
4080
          We do not save info about equalities as update_const_equal_items
 
4081
          will take care of updating info on keys from sargable equalities. 
 
4082
        */
 
4083
        (*sargables)--;
 
4084
        (*sargables)->field= field;
 
4085
        (*sargables)->arg_value= value;
 
4086
        (*sargables)->num_values= num_values;
 
4087
      }
 
4088
      /*
 
4089
        We can't always use indexes when comparing a string index to a
 
4090
        number. cmp_type() is checked to allow compare of dates to numbers.
 
4091
        eq_func is NEVER true when num_values > 1
 
4092
       */
 
4093
      if (!eq_func)
 
4094
      {
 
4095
        /* 
 
4096
          Additional optimization: if we're processing
 
4097
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
 
4098
          "t.key = c1".
 
4099
          TODO: This is a very limited fix. A more generic fix is possible. 
 
4100
          There are 2 options:
 
4101
          A) Make equality propagation code be able to handle BETWEEN
 
4102
             (including cases like t1.key BETWEEN t2.key AND t3.key)
 
4103
          B) Make range optimizer to infer additional "t.key = c" equalities
 
4104
             and use them in equality propagation process (see details in
 
4105
             OptimizerKBAndTodo)
 
4106
        */
 
4107
        if ((cond->functype() != Item_func::BETWEEN) ||
 
4108
            ((Item_func_between*) cond)->negated ||
 
4109
            !value[0]->eq(value[1], field->binary()))
 
4110
          return;
 
4111
        eq_func= true;
 
4112
      }
 
4113
 
 
4114
      if (field->result_type() == STRING_RESULT)
 
4115
      {
 
4116
        if ((*value)->result_type() != STRING_RESULT)
 
4117
        {
 
4118
          if (field->cmp_type() != (*value)->result_type())
 
4119
            return;
 
4120
        }
 
4121
        else
 
4122
        {
 
4123
          /*
 
4124
            We can't use indexes if the effective collation
 
4125
            of the operation differ from the field collation.
 
4126
          */
 
4127
          if (field->cmp_type() == STRING_RESULT &&
 
4128
              ((Field_str*)field)->charset() != cond->compare_collation())
 
4129
            return;
 
4130
        }
 
4131
      }
 
4132
    }
 
4133
  }
 
4134
  /*
 
4135
    For the moment eq_func is always true. This slot is reserved for future
 
4136
    extensions where we want to remembers other things than just eq comparisons
 
4137
  */
 
4138
  assert(eq_func);
 
4139
  /* Store possible eq field */
 
4140
  (*key_fields)->field=         field;
 
4141
  (*key_fields)->eq_func=       eq_func;
 
4142
  (*key_fields)->val=           *value;
 
4143
  (*key_fields)->level=         and_level;
 
4144
  (*key_fields)->optimize=      exists_optimize;
 
4145
  /*
 
4146
    If the condition has form "tbl.keypart = othertbl.field" and 
 
4147
    othertbl.field can be NULL, there will be no matches if othertbl.field 
 
4148
    has NULL value.
 
4149
    We use null_rejecting in add_not_null_conds() to add
 
4150
    'othertbl.field IS NOT NULL' to tab->select_cond.
 
4151
  */
 
4152
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
 
4153
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
 
4154
                                  ((*value)->type() == Item::FIELD_ITEM) &&
 
4155
                                  ((Item_field*)*value)->field->maybe_null());
 
4156
  (*key_fields)->cond_guard= NULL;
 
4157
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
 
4158
                              cond->name < subq_sj_cond_name + 64)? 
 
4159
                              cond->name - subq_sj_cond_name: UINT_MAX;
 
4160
  (*key_fields)++;
 
4161
}
 
4162
 
 
4163
/**
 
4164
  Add possible keys to array of possible keys originated from a simple
 
4165
  predicate.
 
4166
 
 
4167
    @param  key_fields     Pointer to add key, if usable
 
4168
    @param  and_level      And level, to be stored in KEY_FIELD
 
4169
    @param  cond           Condition predicate
 
4170
    @param  field          Field used in comparision
 
4171
    @param  eq_func        True if we used =, <=> or IS NULL
 
4172
    @param  value          Value used for comparison with field
 
4173
                           Is NULL for BETWEEN and IN    
 
4174
    @param  usable_tables  Tables which can be used for key optimization
 
4175
    @param  sargables      IN/OUT Array of found sargable candidates
 
4176
 
 
4177
  @note
 
4178
    If field items f1 and f2 belong to the same multiple equality and
 
4179
    a key is added for f1, the the same key is added for f2.
 
4180
 
 
4181
  @returns
 
4182
    *key_fields is incremented if we stored a key in the array
 
4183
*/
 
4184
 
 
4185
static void
 
4186
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
 
4187
                     Item_func *cond, Item_field *field_item,
 
4188
                     bool eq_func, Item **val,
 
4189
                     uint num_values, table_map usable_tables,
 
4190
                     SARGABLE_PARAM **sargables)
 
4191
{
 
4192
  Field *field= field_item->field;
 
4193
  add_key_field(key_fields, and_level, cond, field,
 
4194
                eq_func, val, num_values, usable_tables, sargables);
 
4195
  Item_equal *item_equal= field_item->item_equal;
 
4196
  if (item_equal)
 
4197
  { 
 
4198
    /*
 
4199
      Add to the set of possible key values every substitution of
 
4200
      the field for an equal field included into item_equal
 
4201
    */
 
4202
    Item_equal_iterator it(*item_equal);
 
4203
    Item_field *item;
 
4204
    while ((item= it++))
 
4205
    {
 
4206
      if (!field->eq(item->field))
 
4207
      {
 
4208
        add_key_field(key_fields, and_level, cond, item->field,
 
4209
                      eq_func, val, num_values, usable_tables,
 
4210
                      sargables);
 
4211
      }
 
4212
    }
 
4213
  }
 
4214
}
 
4215
 
 
4216
static void
 
4217
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
 
4218
               COND *cond, table_map usable_tables,
 
4219
               SARGABLE_PARAM **sargables)
 
4220
{
 
4221
  if (cond->type() == Item_func::COND_ITEM)
 
4222
  {
 
4223
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
 
4224
    KEY_FIELD *org_key_fields= *key_fields;
 
4225
 
 
4226
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
4227
    {
 
4228
      Item *item;
 
4229
      while ((item=li++))
 
4230
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4231
                       sargables);
 
4232
      for (; org_key_fields != *key_fields ; org_key_fields++)
 
4233
        org_key_fields->level= *and_level;
 
4234
    }
 
4235
    else
 
4236
    {
 
4237
      (*and_level)++;
 
4238
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
 
4239
                     sargables);
 
4240
      Item *item;
 
4241
      while ((item=li++))
 
4242
      {
 
4243
        KEY_FIELD *start_key_fields= *key_fields;
 
4244
        (*and_level)++;
 
4245
        add_key_fields(join, key_fields, and_level, item, usable_tables,
 
4246
                       sargables);
 
4247
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
4248
                                     *key_fields,++(*and_level));
 
4249
      }
 
4250
    }
 
4251
    return;
 
4252
  }
 
4253
 
 
4254
  /* 
 
4255
    Subquery optimization: Conditions that are pushed down into subqueries
 
4256
    are wrapped into Item_func_trig_cond. We process the wrapped condition
 
4257
    but need to set cond_guard for KEYUSE elements generated from it.
 
4258
  */
 
4259
  {
 
4260
    if (cond->type() == Item::FUNC_ITEM &&
 
4261
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
 
4262
    {
 
4263
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
 
4264
      if (!join->group_list && !join->order &&
 
4265
          join->unit->item && 
 
4266
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
 
4267
          !join->unit->is_union())
 
4268
      {
 
4269
        KEY_FIELD *save= *key_fields;
 
4270
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
 
4271
                       sargables);
 
4272
        // Indicate that this ref access candidate is for subquery lookup:
 
4273
        for (; save != *key_fields; save++)
 
4274
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
 
4275
      }
 
4276
      return;
 
4277
    }
 
4278
  }
 
4279
 
 
4280
  /* If item is of type 'field op field/constant' add it to key_fields */
 
4281
  if (cond->type() != Item::FUNC_ITEM)
 
4282
    return;
 
4283
  Item_func *cond_func= (Item_func*) cond;
 
4284
  switch (cond_func->select_optimize()) {
 
4285
  case Item_func::OPTIMIZE_NONE:
 
4286
    break;
 
4287
  case Item_func::OPTIMIZE_KEY:
 
4288
  {
 
4289
    Item **values;
 
4290
    // BETWEEN, IN, NE
 
4291
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
 
4292
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4293
    {
 
4294
      values= cond_func->arguments()+1;
 
4295
      if (cond_func->functype() == Item_func::NE_FUNC &&
 
4296
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4297
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4298
        values--;
 
4299
      assert(cond_func->functype() != Item_func::IN_FUNC ||
 
4300
                  cond_func->argument_count() != 2);
 
4301
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4302
                           (Item_field*) (cond_func->key_item()->real_item()),
 
4303
                           0, values, 
 
4304
                           cond_func->argument_count()-1,
 
4305
                           usable_tables, sargables);
 
4306
    }
 
4307
    if (cond_func->functype() == Item_func::BETWEEN)
 
4308
    {
 
4309
      values= cond_func->arguments();
 
4310
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
 
4311
      {
 
4312
        Item_field *field_item;
 
4313
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
 
4314
            &&
 
4315
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
 
4316
        {
 
4317
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
 
4318
          add_key_equal_fields(key_fields, *and_level, cond_func,
 
4319
                               field_item, 0, values, 1, usable_tables, 
 
4320
                               sargables);
 
4321
        }
 
4322
      }  
 
4323
    }
 
4324
    break;
 
4325
  }
 
4326
  case Item_func::OPTIMIZE_OP:
 
4327
  {
 
4328
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
 
4329
                     cond_func->functype() == Item_func::EQUAL_FUNC);
 
4330
 
 
4331
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4332
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
4333
    {
 
4334
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4335
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4336
                           equal_func,
 
4337
                           cond_func->arguments()+1, 1, usable_tables,
 
4338
                           sargables);
 
4339
    }
 
4340
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
 
4341
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
4342
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
4343
    {
 
4344
      add_key_equal_fields(key_fields, *and_level, cond_func, 
 
4345
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
 
4346
                           equal_func,
 
4347
                           cond_func->arguments(),1,usable_tables,
 
4348
                           sargables);
 
4349
    }
 
4350
    break;
 
4351
  }
 
4352
  case Item_func::OPTIMIZE_NULL:
 
4353
    /* column_name IS [NOT] NULL */
 
4354
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
 
4355
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
4356
    {
 
4357
      Item *tmp=new Item_null;
 
4358
      if (unlikely(!tmp))                       // Should never be true
 
4359
        return;
 
4360
      add_key_equal_fields(key_fields, *and_level, cond_func,
 
4361
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
 
4362
                    cond_func->functype() == Item_func::ISNULL_FUNC,
 
4363
                           &tmp, 1, usable_tables, sargables);
 
4364
    }
 
4365
    break;
 
4366
  case Item_func::OPTIMIZE_EQUAL:
 
4367
    Item_equal *item_equal= (Item_equal *) cond;
 
4368
    Item *const_item= item_equal->get_const();
 
4369
    Item_equal_iterator it(*item_equal);
 
4370
    Item_field *item;
 
4371
    if (const_item)
 
4372
    {
 
4373
      /*
 
4374
        For each field field1 from item_equal consider the equality 
 
4375
        field1=const_item as a condition allowing an index access of the table
 
4376
        with field1 by the keys value of field1.
 
4377
      */   
 
4378
      while ((item= it++))
 
4379
      {
 
4380
        add_key_field(key_fields, *and_level, cond_func, item->field,
 
4381
                      true, &const_item, 1, usable_tables, sargables);
 
4382
      }
 
4383
    }
 
4384
    else 
 
4385
    {
 
4386
      /*
 
4387
        Consider all pairs of different fields included into item_equal.
 
4388
        For each of them (field1, field1) consider the equality 
 
4389
        field1=field2 as a condition allowing an index access of the table
 
4390
        with field1 by the keys value of field2.
 
4391
      */   
 
4392
      Item_equal_iterator fi(*item_equal);
 
4393
      while ((item= fi++))
 
4394
      {
 
4395
        Field *field= item->field;
 
4396
        while ((item= it++))
 
4397
        {
 
4398
          if (!field->eq(item->field))
 
4399
          {
 
4400
            add_key_field(key_fields, *and_level, cond_func, field,
 
4401
                          true, (Item **) &item, 1, usable_tables,
 
4402
                          sargables);
 
4403
          }
 
4404
        }
 
4405
        it.rewind();
 
4406
      }
 
4407
    }
 
4408
    break;
 
4409
  }
 
4410
}
506
4411
 
507
4412
/**
508
4413
  Add all keys with uses 'field' for some keypart.
509
4414
 
510
4415
  If field->and_level != and_level then only mark key_part as const_part.
511
4416
*/
512
 
uint32_t max_part_bit(key_part_map bits)
 
4417
 
 
4418
static uint
 
4419
max_part_bit(key_part_map bits)
513
4420
{
514
 
  uint32_t found;
 
4421
  uint found;
515
4422
  for (found=0; bits & 1 ; found++,bits>>=1) ;
516
4423
  return found;
517
4424
}
518
4425
 
519
 
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
 
4426
static void
 
4427
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
4428
{
 
4429
  Field *field=key_field->field;
 
4430
  TABLE *form= field->table;
 
4431
  KEYUSE keyuse;
 
4432
 
 
4433
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
 
4434
  {
 
4435
    for (uint key= 0 ; key < form->sizeKeys() ; key++)
 
4436
    {
 
4437
      if (!(form->keys_in_use_for_query.is_set(key)))
 
4438
        continue;
 
4439
 
 
4440
      uint key_parts= (uint) form->key_info[key].key_parts;
 
4441
      for (uint part=0 ; part <  key_parts ; part++)
 
4442
      {
 
4443
        if (field->eq(form->key_info[key].key_part[part].field))
 
4444
        {
 
4445
          keyuse.table= field->table;
 
4446
          keyuse.val =  key_field->val;
 
4447
          keyuse.key =  key;
 
4448
          keyuse.keypart=part;
 
4449
          keyuse.keypart_map= (key_part_map) 1 << part;
 
4450
          keyuse.used_tables=key_field->val->used_tables();
 
4451
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
4452
          keyuse.null_rejecting= key_field->null_rejecting;
 
4453
          keyuse.cond_guard= key_field->cond_guard;
 
4454
          keyuse.sj_pred_no= key_field->sj_pred_no;
 
4455
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
 
4456
        }
 
4457
      }
 
4458
    }
 
4459
  }
 
4460
}
 
4461
 
 
4462
static int
 
4463
sort_keyuse(KEYUSE *a,KEYUSE *b)
520
4464
{
521
4465
  int res;
522
 
  if (a->getTable()->tablenr != b->getTable()->tablenr)
523
 
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
524
 
  if (a->getKey() != b->getKey())
525
 
    return static_cast<int>((a->getKey() - b->getKey()));
526
 
  if (a->getKeypart() != b->getKeypart())
527
 
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
 
4466
  if (a->table->tablenr != b->table->tablenr)
 
4467
    return (int) (a->table->tablenr - b->table->tablenr);
 
4468
  if (a->key != b->key)
 
4469
    return (int) (a->key - b->key);
 
4470
  if (a->keypart != b->keypart)
 
4471
    return (int) (a->keypart - b->keypart);
528
4472
  // Place const values before other ones
529
 
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
530
 
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
 
4473
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
 
4474
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
531
4475
    return res;
532
4476
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
533
 
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
534
 
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
 
4477
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
 
4478
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
 
4479
}
 
4480
 
 
4481
 
 
4482
/*
 
4483
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
 
4484
 
 
4485
    This function populates KEY_FIELD array with entries generated from the 
 
4486
    ON condition of the given nested join, and does the same for nested joins 
 
4487
    contained within this nested join.
 
4488
 
 
4489
  @param[in]      nested_join_table   Nested join pseudo-table to process
 
4490
  @param[in,out]  end                 End of the key field array
 
4491
  @param[in,out]  and_level           And-level
 
4492
  @param[in,out]  sargables           Array of found sargable candidates
 
4493
 
 
4494
 
 
4495
  @note
 
4496
    We can add accesses to the tables that are direct children of this nested 
 
4497
    join (1), and are not inner tables w.r.t their neighbours (2).
 
4498
    
 
4499
    Example for #1 (outer brackets pair denotes nested join this function is 
 
4500
    invoked for):
 
4501
    @code
 
4502
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
4503
    @endcode
 
4504
    Example for #2:
 
4505
    @code
 
4506
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
4507
    @endcode
 
4508
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
 
4509
    t1 only.
 
4510
    Example #3:
 
4511
    @code
 
4512
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
4513
    @endcode
 
4514
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
4515
*/
 
4516
 
 
4517
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
 
4518
                                  KEY_FIELD **end, uint *and_level,
 
4519
                                  SARGABLE_PARAM **sargables)
 
4520
{
 
4521
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
 
4522
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
 
4523
  bool have_another = false;
 
4524
  table_map tables= 0;
 
4525
  TABLE_LIST *table;
 
4526
  assert(nested_join_table->nested_join);
 
4527
 
 
4528
  while ((table= li++) || (have_another && (li=li2, have_another=false,
 
4529
                                            (table= li++))))
 
4530
  {
 
4531
    if (table->nested_join)
 
4532
    {
 
4533
      if (!table->on_expr)
 
4534
      {
 
4535
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
 
4536
        have_another= true;
 
4537
        li2= li;
 
4538
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
 
4539
      }
 
4540
      else
 
4541
        add_key_fields_for_nj(join, table, end, and_level, sargables);
 
4542
    }
 
4543
    else
 
4544
      if (!table->on_expr)
 
4545
        tables |= table->table->map;
 
4546
  }
 
4547
  if (nested_join_table->on_expr)
 
4548
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
 
4549
                   sargables);
535
4550
}
536
4551
 
537
4552
 
538
4553
/**
539
4554
  Update keyuse array with all possible keys we can use to fetch rows.
540
 
 
541
 
  @param       session
542
 
  @param[out]  keyuse         Put here ordered array of KeyUse structures
 
4555
  
 
4556
  @param       thd 
 
4557
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
543
4558
  @param       join_tab       Array in tablenr_order
544
4559
  @param       tables         Number of tables in join
545
4560
  @param       cond           WHERE condition (note that the function analyzes
548
4563
                              for which we can make ref access based the WHERE
549
4564
                              clause)
550
4565
  @param       select_lex     current SELECT
551
 
  @param[out]  sargables      std::vector of found sargable candidates
552
 
 
 
4566
  @param[out]  sargables      Array of found sargable candidates
 
4567
      
553
4568
   @retval
554
4569
     0  OK
555
4570
   @retval
556
4571
     1  Out of memory.
557
4572
*/
558
 
bool update_ref_and_keys(Session *session,
559
 
                         DYNAMIC_ARRAY *keyuse,
560
 
                         JoinTable *join_tab,
561
 
                         uint32_t tables,
562
 
                         COND *cond, 
563
 
                         COND_EQUAL *,
564
 
                         table_map normal_tables,
565
 
                         Select_Lex *select_lex,
566
 
                         vector<optimizer::SargableParam> &sargables)
 
4573
 
 
4574
static bool
 
4575
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
 
4576
                    uint tables, COND *cond,
 
4577
                    COND_EQUAL *cond_equal __attribute__((unused)),
 
4578
                    table_map normal_tables, SELECT_LEX *select_lex,
 
4579
                    SARGABLE_PARAM **sargables)
567
4580
{
568
4581
  uint  and_level,i,found_eq_constant;
569
 
  optimizer::KeyField *key_fields, *end, *field;
570
 
  uint32_t sz;
571
 
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
572
 
 
573
 
  /*
574
 
    All predicates that are used to fill arrays of KeyField
575
 
    and SargableParam classes have at most 2 arguments
576
 
    except BETWEEN predicates that have 3 arguments and
 
4582
  KEY_FIELD *key_fields, *end, *field;
 
4583
  uint sz;
 
4584
  uint m= max(select_lex->max_equal_elems,(uint32_t)1);
 
4585
  
 
4586
  /* 
 
4587
    We use the same piece of memory to store both  KEY_FIELD 
 
4588
    and SARGABLE_PARAM structure.
 
4589
    KEY_FIELD values are placed at the beginning this memory
 
4590
    while  SARGABLE_PARAM values are put at the end.
 
4591
    All predicates that are used to fill arrays of KEY_FIELD
 
4592
    and SARGABLE_PARAM structures have at most 2 arguments
 
4593
    except BETWEEN predicates that have 3 arguments and 
577
4594
    IN predicates.
578
 
    This any predicate if it's not BETWEEN/IN can be used
579
 
    directly to fill at most 2 array elements, either of KeyField 
580
 
    or SargableParam type. For a BETWEEN predicate 3 elements
 
4595
    This any predicate if it's not BETWEEN/IN can be used 
 
4596
    directly to fill at most 2 array elements, either of KEY_FIELD
 
4597
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
581
4598
    can be filled as this predicate is considered as
582
4599
    saragable with respect to each of its argument.
583
4600
    An IN predicate can require at most 1 element as currently
584
4601
    it is considered as sargable only for its first argument.
585
4602
    Multiple equality can add  elements that are filled after
586
4603
    substitution of field arguments by equal fields. There
587
 
    can be not more than select_lex->max_equal_elems such
 
4604
    can be not more than select_lex->max_equal_elems such 
588
4605
    substitutions.
589
 
  */
590
 
  sz= sizeof(optimizer::KeyField) *
591
 
      (((session->lex->current_select->cond_count+1) +
592
 
        session->lex->current_select->between_count)*m+1);
593
 
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
594
 
    return true;
 
4606
  */ 
 
4607
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
 
4608
      (((thd->lex->current_select->cond_count+1)*2 +
 
4609
        thd->lex->current_select->between_count)*m+1);
 
4610
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
 
4611
    return true; /* purecov: inspected */
595
4612
  and_level= 0;
596
4613
  field= end= key_fields;
 
4614
  *sargables= (SARGABLE_PARAM *) key_fields + 
 
4615
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
 
4616
  /* set a barrier for the array of SARGABLE_PARAM */
 
4617
  (*sargables)[0].field= 0; 
597
4618
 
598
 
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
 
4619
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
599
4620
    return true;
600
4621
  if (cond)
601
4622
  {
602
4623
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
603
4624
                   sargables);
604
 
    for (; field != end; field++)
 
4625
    for (; field != end ; field++)
605
4626
    {
606
 
      add_key_part(keyuse, field);
 
4627
      add_key_part(keyuse,field);
607
4628
      /* Mark that we can optimize LEFT JOIN */
608
 
      if (field->getValue()->type() == Item::NULL_ITEM &&
609
 
          ! field->getField()->real_maybe_null())
610
 
      {
611
 
        field->getField()->table->reginfo.not_exists_optimize= 1;
612
 
      }
 
4629
      if (field->val->type() == Item::NULL_ITEM &&
 
4630
          !field->field->real_maybe_null())
 
4631
        field->field->table->reginfo.not_exists_optimize=1;
613
4632
    }
614
4633
  }
615
 
  for (i= 0; i < tables; i++)
 
4634
  for (i=0 ; i < tables ; i++)
616
4635
  {
617
4636
    /*
618
4637
      Block the creation of keys for inner tables of outer joins.
622
4641
      In the future when we introduce conditional accesses
623
4642
      for inner tables in outer joins these keys will be taken
624
4643
      into account as well.
625
 
    */
 
4644
    */ 
626
4645
    if (*join_tab[i].on_expr_ref)
627
 
      add_key_fields(join_tab->join, &end, &and_level,
 
4646
      add_key_fields(join_tab->join, &end, &and_level, 
628
4647
                     *join_tab[i].on_expr_ref,
629
4648
                     join_tab[i].table->map, sargables);
630
4649
  }
631
4650
 
632
4651
  /* Process ON conditions for the nested joins */
633
4652
  {
634
 
    List_iterator<TableList> li(*join_tab->join->join_list);
635
 
    TableList *table;
 
4653
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
 
4654
    TABLE_LIST *table;
636
4655
    while ((table= li++))
637
4656
    {
638
4657
      if (table->nested_join)
639
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
 
4658
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
640
4659
                              sargables);
641
4660
    }
642
4661
  }
656
4675
  */
657
4676
  if (keyuse->elements)
658
4677
  {
659
 
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
 
4678
    KEYUSE key_end,*prev,*save_pos,*use;
660
4679
 
661
 
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
 
4680
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
662
4681
          (qsort_cmp) sort_keyuse);
663
4682
 
664
4683
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
665
 
    insert_dynamic(keyuse,(unsigned char*) &key_end);
 
4684
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
666
4685
 
667
 
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
 
4686
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
668
4687
    prev= &key_end;
669
 
    found_eq_constant= 0;
670
 
    for (i= 0; i < keyuse->elements-1; i++, use++)
 
4688
    found_eq_constant=0;
 
4689
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
671
4690
    {
672
 
      if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
673
 
        use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
674
 
      if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
 
4691
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 
4692
        use->table->const_key_parts[use->key]|= use->keypart_map;
675
4693
      {
676
 
        if (prev->getKeypart() + 1 < use->getKeypart() || 
677
 
            ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
678
 
          continue;                             /* remove */
 
4694
        if (use->key == prev->key && use->table == prev->table)
 
4695
        {
 
4696
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
4697
            continue;                           /* remove */
 
4698
        }
 
4699
        else if (use->keypart != 0)             // First found must be 0
 
4700
          continue;
679
4701
      }
680
 
      else if (use->getKeypart() != 0)          // First found must be 0
681
 
        continue;
682
4702
 
683
4703
#ifdef HAVE_purify
684
4704
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
686
4706
#endif
687
4707
        *save_pos= *use;
688
4708
      prev=use;
689
 
      found_eq_constant= ! use->getUsedTables();
 
4709
      found_eq_constant= !use->used_tables;
690
4710
      /* Save ptr to first use */
691
 
      if (! use->getTable()->reginfo.join_tab->keyuse)
692
 
        use->getTable()->reginfo.join_tab->keyuse= save_pos;
693
 
      use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
 
4711
      if (!use->table->reginfo.join_tab->keyuse)
 
4712
        use->table->reginfo.join_tab->keyuse=save_pos;
 
4713
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
694
4714
      save_pos++;
695
4715
    }
696
 
    i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
697
 
    set_dynamic(keyuse, (unsigned char*) &key_end, i);
698
 
    keyuse->elements= i;
 
4716
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
 
4717
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
 
4718
    keyuse->elements=i;
699
4719
  }
700
4720
  return false;
701
4721
}
703
4723
/**
704
4724
  Update some values in keyuse for faster choose_plan() loop.
705
4725
*/
706
 
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
4726
 
 
4727
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
707
4728
{
708
 
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
709
 
                                                  0, 
710
 
                                                  optimizer::KeyUse*);
 
4729
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
711
4730
 
712
4731
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
713
4732
  {
720
4739
      Constant tables are ignored.
721
4740
      To avoid bad matches, we don't make ref_table_rows less than 100.
722
4741
    */
723
 
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
724
 
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
 
4742
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
 
4743
    if (keyuse->used_tables &
 
4744
        (map= (keyuse->used_tables & ~join->const_table_map &
 
4745
               ~OUTER_REF_TABLE_BIT)))
725
4746
    {
726
 
      uint32_t tablenr;
 
4747
      uint tablenr;
727
4748
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
728
4749
      if (map == 1)                     // Only one table
729
4750
      {
730
 
        Table *tmp_table=join->all_tables[tablenr];
731
 
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
 
4751
        TABLE *tmp_table=join->all_tables[tablenr];
 
4752
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, (ha_rows)100);
732
4753
      }
733
4754
    }
734
4755
    /*
735
4756
      Outer reference (external field) is constant for single executing
736
4757
      of subquery
737
4758
    */
738
 
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
739
 
      keyuse->setTableRows(1);
 
4759
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
 
4760
      keyuse->ref_table_rows= 1;
740
4761
  }
741
4762
}
742
4763
 
758
4779
  @return
759
4780
    None
760
4781
*/
761
 
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab)
 
4782
 
 
4783
static void
 
4784
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
762
4785
{
763
4786
  List<Item_field> indexed_fields;
764
4787
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
765
 
  order_st      *cur_group;
 
4788
  ORDER      *cur_group;
766
4789
  Item_field *cur_item;
767
4790
  key_map possible_keys(0);
768
4791
 
770
4793
  { /* Collect all query fields referenced in the GROUP clause. */
771
4794
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
772
4795
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
773
 
                               (unsigned char*) &indexed_fields);
 
4796
                               (uchar*) &indexed_fields);
774
4797
  }
775
4798
  else if (join->select_distinct)
776
4799
  { /* Collect all query fields referenced in the SELECT clause. */
779
4802
    Item *item;
780
4803
    while ((item= select_items_it++))
781
4804
      item->walk(&Item::collect_item_field_processor, 0,
782
 
                 (unsigned char*) &indexed_fields);
 
4805
                 (uchar*) &indexed_fields);
783
4806
  }
784
4807
  else
785
4808
    return;
789
4812
 
790
4813
  /* Intersect the keys of all group fields. */
791
4814
  cur_item= indexed_fields_it++;
792
 
  possible_keys|= cur_item->field->part_of_key;
 
4815
  possible_keys.merge(cur_item->field->part_of_key);
793
4816
  while ((cur_item= indexed_fields_it++))
794
4817
  {
795
 
    possible_keys&= cur_item->field->part_of_key;
796
 
  }
797
 
 
798
 
  if (possible_keys.any())
799
 
    join_tab->const_keys|= possible_keys;
800
 
}
801
 
 
802
 
/**
803
 
  Compare two JoinTable objects based on the number of accessed records.
804
 
 
805
 
  @param ptr1 pointer to first JoinTable object
806
 
  @param ptr2 pointer to second JoinTable object
 
4818
    possible_keys.intersect(cur_item->field->part_of_key);
 
4819
  }
 
4820
 
 
4821
  if (!possible_keys.is_clear_all())
 
4822
    join_tab->const_keys.merge(possible_keys);
 
4823
}
 
4824
 
 
4825
 
 
4826
/*****************************************************************************
 
4827
  Go through all combinations of not marked tables and find the one
 
4828
  which uses least records
 
4829
*****************************************************************************/
 
4830
 
 
4831
/** Save const tables first as used tables. */
 
4832
 
 
4833
static void
 
4834
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
 
4835
{
 
4836
  join->positions[idx].table= table;
 
4837
  join->positions[idx].key=key;
 
4838
  join->positions[idx].records_read=1.0;        /* This is a const table */
 
4839
  join->positions[idx].ref_depend_map= 0;
 
4840
 
 
4841
  /* Move the const table as down as possible in best_ref */
 
4842
  JOIN_TAB **pos=join->best_ref+idx+1;
 
4843
  JOIN_TAB *next=join->best_ref[idx];
 
4844
  for (;next != table ; pos++)
 
4845
  {
 
4846
    JOIN_TAB *tmp=pos[0];
 
4847
    pos[0]=next;
 
4848
    next=tmp;
 
4849
  }
 
4850
  join->best_ref[idx]=table;
 
4851
}
 
4852
 
 
4853
 
 
4854
/*
 
4855
  Given a semi-join nest, find out which of the IN-equalities are bound
 
4856
 
 
4857
  SYNOPSIS
 
4858
    get_bound_sj_equalities()
 
4859
      sj_nest           Semi-join nest
 
4860
      remaining_tables  Tables that are not yet bound
 
4861
 
 
4862
  DESCRIPTION
 
4863
    Given a semi-join nest, find out which of the IN-equalities have their
 
4864
    left part expression bound (i.e. the said expression doesn't refer to
 
4865
    any of remaining_tables and can be evaluated).
 
4866
 
 
4867
  RETURN
 
4868
    Bitmap of bound IN-equalities.
 
4869
*/
 
4870
 
 
4871
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, 
 
4872
                                  table_map remaining_tables)
 
4873
{
 
4874
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
 
4875
  Item *item;
 
4876
  uint i= 0;
 
4877
  uint64_t res= 0;
 
4878
  while ((item= li++))
 
4879
  {
 
4880
    /*
 
4881
      Q: should this take into account equality propagation and how?
 
4882
      A: If e->outer_side is an Item_field, walk over the equality
 
4883
         class and see if there is an element that is bound?
 
4884
      (this is an optional feature)
 
4885
    */
 
4886
    if (!(item->used_tables() & remaining_tables))
 
4887
    {
 
4888
      res |= 1ULL < i;
 
4889
    }
 
4890
  }
 
4891
  return res;
 
4892
}
 
4893
 
 
4894
 
 
4895
/**
 
4896
  Find the best access path for an extension of a partial execution
 
4897
  plan and add this path to the plan.
 
4898
 
 
4899
  The function finds the best access path to table 's' from the passed
 
4900
  partial plan where an access path is the general term for any means to
 
4901
  access the data in 's'. An access path may use either an index or a scan,
 
4902
  whichever is cheaper. The input partial plan is passed via the array
 
4903
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
4904
  cost are stored in 'join->positions[idx]'.
 
4905
 
 
4906
  @param join             pointer to the structure providing all context info
 
4907
                          for the query
 
4908
  @param s                the table to be joined by the function
 
4909
  @param thd              thread for the connection that submitted the query
 
4910
  @param remaining_tables set of tables not included into the partial plan yet
 
4911
  @param idx              the length of the partial plan
 
4912
  @param record_count     estimate for the number of records returned by the
 
4913
                          partial plan
 
4914
  @param read_time        the cost of the partial plan
 
4915
 
 
4916
  @return
 
4917
    None
 
4918
*/
 
4919
 
 
4920
static void
 
4921
best_access_path(JOIN      *join,
 
4922
                 JOIN_TAB  *s,
 
4923
                 THD       *thd,
 
4924
                 table_map remaining_tables,
 
4925
                 uint      idx,
 
4926
                 double    record_count,
 
4927
                 double    read_time __attribute__((unused)))
 
4928
{
 
4929
  KEYUSE *best_key=         0;
 
4930
  uint best_max_key_part=   0;
 
4931
  bool found_constraint= 0;
 
4932
  double best=              DBL_MAX;
 
4933
  double best_time=         DBL_MAX;
 
4934
  double records=           DBL_MAX;
 
4935
  table_map best_ref_depends_map= 0;
 
4936
  double tmp;
 
4937
  ha_rows rec;
 
4938
  uint best_is_sj_inside_out=    0;
 
4939
 
 
4940
  if (s->keyuse)
 
4941
  {                                            /* Use key if possible */
 
4942
    TABLE *table= s->table;
 
4943
    KEYUSE *keyuse,*start_key=0;
 
4944
    double best_records= DBL_MAX;
 
4945
    uint max_key_part=0;
 
4946
    uint64_t bound_sj_equalities= 0;
 
4947
    bool try_sj_inside_out= false;
 
4948
    /*
 
4949
      Discover the bound equalites. We need to do this, if
 
4950
        1. The next table is an SJ-inner table, and
 
4951
        2. It is the first table from that semijoin, and
 
4952
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
4953
           all or none of their tables in join_table_map), except
 
4954
           s->emb_sj_nest (which we've just entered).
 
4955
        3. All correlation references from this sj-nest are bound
 
4956
    */
 
4957
    if (s->emb_sj_nest &&                                                 // (1)
 
4958
        s->emb_sj_nest->sj_in_exprs < 64 && 
 
4959
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
4960
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
4961
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
4962
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
4963
    {
 
4964
      /* This table is an InsideOut scan candidate */
 
4965
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
 
4966
                                                   remaining_tables);
 
4967
      try_sj_inside_out= true;
 
4968
    }
 
4969
 
 
4970
    /* Test how we can use keys */
 
4971
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
4972
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
4973
    {
 
4974
      key_part_map found_part= 0;
 
4975
      table_map found_ref= 0;
 
4976
      uint key= keyuse->key;
 
4977
      KEY *keyinfo= table->key_info+key;
 
4978
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
4979
      key_part_map const_part= 0;
 
4980
      /* The or-null keypart in ref-or-null access: */
 
4981
      key_part_map ref_or_null_part= 0;
 
4982
 
 
4983
      /* Calculate how many key segments of the current key we can use */
 
4984
      start_key= keyuse;
 
4985
      uint64_t handled_sj_equalities=0;
 
4986
      key_part_map sj_insideout_map= 0;
 
4987
 
 
4988
      do /* For each keypart */
 
4989
      {
 
4990
        uint keypart= keyuse->keypart;
 
4991
        table_map best_part_found_ref= 0;
 
4992
        double best_prev_record_reads= DBL_MAX;
 
4993
        
 
4994
        do /* For each way to access the keypart */
 
4995
        {
 
4996
 
 
4997
          /*
 
4998
            if 1. expression doesn't refer to forward tables
 
4999
               2. we won't get two ref-or-null's
 
5000
          */
 
5001
          if (!(remaining_tables & keyuse->used_tables) &&
 
5002
              !(ref_or_null_part && (keyuse->optimize &
 
5003
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
5004
          {
 
5005
            found_part|= keyuse->keypart_map;
 
5006
            if (!(keyuse->used_tables & ~join->const_table_map))
 
5007
              const_part|= keyuse->keypart_map;
 
5008
 
 
5009
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
5010
                                                      keyuse->used_tables));
 
5011
            if (tmp2 < best_prev_record_reads)
 
5012
            {
 
5013
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
5014
              best_prev_record_reads= tmp2;
 
5015
            }
 
5016
            if (rec > keyuse->ref_table_rows)
 
5017
              rec= keyuse->ref_table_rows;
 
5018
            /*
 
5019
              If there is one 'key_column IS NULL' expression, we can
 
5020
              use this ref_or_null optimisation of this field
 
5021
            */
 
5022
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
5023
              ref_or_null_part |= keyuse->keypart_map;
 
5024
          }
 
5025
 
 
5026
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
5027
          {
 
5028
            if (!(remaining_tables & keyuse->used_tables))
 
5029
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5030
            else
 
5031
            {
 
5032
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
 
5033
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
5034
            }
 
5035
          }
 
5036
 
 
5037
          keyuse++;
 
5038
        } while (keyuse->table == table && keyuse->key == key &&
 
5039
                 keyuse->keypart == keypart);
 
5040
        found_ref|= best_part_found_ref;
 
5041
      } while (keyuse->table == table && keyuse->key == key);
 
5042
 
 
5043
      /*
 
5044
        Assume that that each key matches a proportional part of table.
 
5045
      */
 
5046
      if (!found_part && !handled_sj_equalities)
 
5047
        continue;                               // Nothing usable found
 
5048
 
 
5049
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
5050
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
5051
 
 
5052
      bool sj_inside_out_scan= false;
 
5053
      {
 
5054
        found_constraint= 1;
 
5055
        /*
 
5056
          Check if InsideOut scan is applicable:
 
5057
          1. All IN-equalities are either "bound" or "handled"
 
5058
          2. Index keyparts are 
 
5059
             ...
 
5060
        */
 
5061
        if (try_sj_inside_out && 
 
5062
            table->covering_keys.is_set(key) &&
 
5063
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
5064
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
5065
        {
 
5066
          uint n_fixed_parts= max_part_bit(found_part);
 
5067
          if (n_fixed_parts != keyinfo->key_parts &&
 
5068
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
5069
               PREV_BITS(uint, keyinfo->key_parts))
 
5070
          {
 
5071
            /*
 
5072
              Not all parts are fixed. Produce bitmap of remaining bits and
 
5073
              check if all of them are covered.
 
5074
            */
 
5075
            sj_inside_out_scan= true;
 
5076
            if (!n_fixed_parts)
 
5077
            {
 
5078
              /*
 
5079
                It's a confluent ref scan.
 
5080
 
 
5081
                That is, all found KEYUSE elements refer to IN-equalities,
 
5082
                and there is really no ref access because there is no
 
5083
                  t.keypart0 = {bound expression}
 
5084
 
 
5085
                Calculate the cost of complete loose index scan.
 
5086
              */
 
5087
              records= (double)s->table->file->stats.records;
 
5088
 
 
5089
              /* The cost is entire index scan cost (divided by 2) */
 
5090
              best_time= s->table->file->index_only_read_time(key, records);
 
5091
 
 
5092
              /* Now figure how many different keys we will get */
 
5093
              ulong rpc;
 
5094
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5095
                records= records / rpc;
 
5096
              start_key= NULL;
 
5097
            }
 
5098
          }
 
5099
        }
 
5100
 
 
5101
        /*
 
5102
          Check if we found full key
 
5103
        */
 
5104
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
5105
            !ref_or_null_part)
 
5106
        {                                         /* use eq key */
 
5107
          max_key_part= (uint) ~0;
 
5108
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
5109
          {
 
5110
            tmp = prev_record_reads(join, idx, found_ref);
 
5111
            records=1.0;
 
5112
          }
 
5113
          else
 
5114
          {
 
5115
            if (!found_ref)
 
5116
            {                                     /* We found a const key */
 
5117
              /*
 
5118
                ReuseRangeEstimateForRef-1:
 
5119
                We get here if we've found a ref(const) (c_i are constants):
 
5120
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
5121
                
 
5122
                If range optimizer was able to construct a "range" 
 
5123
                access on this index, then its condition "quick_cond" was
 
5124
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
5125
                from the range optimizer.
 
5126
                
 
5127
                Proof of (*): By properties of range and ref optimizers 
 
5128
                quick_cond will be equal or tighther than ref_const_cond. 
 
5129
                ref_const_cond already covers "smallest" possible interval - 
 
5130
                a singlepoint interval over all keyparts. Therefore, 
 
5131
                quick_cond is equivalent to ref_const_cond (if it was an 
 
5132
                empty interval we wouldn't have got here).
 
5133
              */
 
5134
              if (table->quick_keys.is_set(key))
 
5135
                records= (double) table->quick_rows[key];
 
5136
              else
 
5137
              {
 
5138
                /* quick_range couldn't use key! */
 
5139
                records= (double) s->records/rec;
 
5140
              }
 
5141
            }
 
5142
            else
 
5143
            {
 
5144
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5145
              {                                   /* Prefer longer keys */
 
5146
                records=
 
5147
                  ((double) s->records / (double) rec *
 
5148
                   (1.0 +
 
5149
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
5150
                     (double) table->s->max_key_length)));
 
5151
                if (records < 2.0)
 
5152
                  records=2.0;               /* Can't be as good as a unique */
 
5153
              }
 
5154
              /*
 
5155
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
5156
                E(#rows) from range optimizer. Make another try:
 
5157
                
 
5158
                If range optimizer produced E(#rows) for a prefix of the ref
 
5159
                access we're considering, and that E(#rows) is lower then our
 
5160
                current estimate, make an adjustment. The criteria of when we
 
5161
                can make an adjustment is a special case of the criteria used
 
5162
                in ReuseRangeEstimateForRef-3.
 
5163
              */
 
5164
              if (table->quick_keys.is_set(key) &&
 
5165
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5166
                  table->quick_n_ranges[key] == 1 &&
 
5167
                  records > (double) table->quick_rows[key])
 
5168
              {
 
5169
                records= (double) table->quick_rows[key];
 
5170
              }
 
5171
            }
 
5172
            /* Limit the number of matched rows */
 
5173
            tmp= records;
 
5174
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5175
            if (table->covering_keys.is_set(key))
 
5176
            {
 
5177
              /* we can use only index tree */
 
5178
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5179
            }
 
5180
            else
 
5181
              tmp= record_count*min(tmp,s->worst_seeks);
 
5182
          }
 
5183
        }
 
5184
        else
 
5185
        {
 
5186
          /*
 
5187
            Use as much key-parts as possible and a uniq key is better
 
5188
            than a not unique key
 
5189
            Set tmp to (previous record count) * (records / combination)
 
5190
          */
 
5191
          if ((found_part & 1) &&
 
5192
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
5193
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
5194
          {
 
5195
            max_key_part= max_part_bit(found_part);
 
5196
            /*
 
5197
              ReuseRangeEstimateForRef-3:
 
5198
              We're now considering a ref[or_null] access via
 
5199
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
 
5200
              (same-as-above but with one cond replaced 
 
5201
               with "t.keypart_i IS NULL")]  (**)
 
5202
              
 
5203
              Try re-using E(#rows) from "range" optimizer:
 
5204
              We can do so if "range" optimizer used the same intervals as
 
5205
              in (**). The intervals used by range optimizer may be not 
 
5206
              available at this point (as "range" access might have choosen to
 
5207
              create quick select over another index), so we can't compare
 
5208
              them to (**). We'll make indirect judgements instead.
 
5209
              The sufficient conditions for re-use are:
 
5210
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
5211
                   this is not satisfied we have no way to know which ranges
 
5212
                   will be actually scanned by 'ref' until we execute the 
 
5213
                   join)
 
5214
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
5215
                   is apparently a necessary requirement)
 
5216
 
 
5217
              We also have a property that "range optimizer produces equal or 
 
5218
              tighter set of scan intervals than ref(const) optimizer". Each
 
5219
              of the intervals in (**) are "tightest possible" intervals when 
 
5220
              one limits itself to using keyparts 1..K (which we do in #2).              
 
5221
              From here it follows that range access used either one, or
 
5222
              both of the (I1) and (I2) intervals:
 
5223
              
 
5224
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
 
5225
               (same-as-above but with one cond replaced  
 
5226
                with "t.keypart_i IS NULL")               (I2)
 
5227
 
 
5228
              The remaining part is to exclude the situation where range
 
5229
              optimizer used one interval while we're considering
 
5230
              ref-or-null and looking for estimate for two intervals. This
 
5231
              is done by last limitation:
 
5232
 
 
5233
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
5234
            */
 
5235
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
 
5236
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
5237
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
 
5238
            {
 
5239
              tmp= records= (double) table->quick_rows[key];
 
5240
            }
 
5241
            else
 
5242
            {
 
5243
              /* Check if we have statistic about the distribution */
 
5244
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
5245
              {
 
5246
                /* 
 
5247
                  Fix for the case where the index statistics is too
 
5248
                  optimistic: If 
 
5249
                  (1) We're considering ref(const) and there is quick select
 
5250
                      on the same index, 
 
5251
                  (2) and that quick select uses more keyparts (i.e. it will
 
5252
                      scan equal/smaller interval then this ref(const))
 
5253
                  (3) and E(#rows) for quick select is higher then our
 
5254
                      estimate,
 
5255
                  Then 
 
5256
                    We'll use E(#rows) from quick select.
 
5257
 
 
5258
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
5259
                  cheaper in some cases ?
 
5260
                  TODO: figure this out and adjust the plan choice if needed.
 
5261
                */
 
5262
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
 
5263
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
5264
                    records < (double)table->quick_rows[key])         // (3)
 
5265
                  records= (double)table->quick_rows[key];
 
5266
 
 
5267
                tmp= records;
 
5268
              }
 
5269
              else
 
5270
              {
 
5271
                /*
 
5272
                  Assume that the first key part matches 1% of the file
 
5273
                  and that the whole key matches 10 (duplicates) or 1
 
5274
                  (unique) records.
 
5275
                  Assume also that more key matches proportionally more
 
5276
                  records
 
5277
                  This gives the formula:
 
5278
                  records = (x * (b-a) + a*c-b)/(c-1)
 
5279
 
 
5280
                  b = records matched by whole key
 
5281
                  a = records matched by first key part (1% of all records?)
 
5282
                  c = number of key parts in key
 
5283
                  x = used key parts (1 <= x <= c)
 
5284
                */
 
5285
                double rec_per_key;
 
5286
                if (!(rec_per_key=(double)
 
5287
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
5288
                  rec_per_key=(double) s->records/rec+1;
 
5289
 
 
5290
                if (!s->records)
 
5291
                  tmp = 0;
 
5292
                else if (rec_per_key/(double) s->records >= 0.01)
 
5293
                  tmp = rec_per_key;
 
5294
                else
 
5295
                {
 
5296
                  double a=s->records*0.01;
 
5297
                  if (keyinfo->key_parts > 1)
 
5298
                    tmp= (max_key_part * (rec_per_key - a) +
 
5299
                          a*keyinfo->key_parts - rec_per_key)/
 
5300
                         (keyinfo->key_parts-1);
 
5301
                  else
 
5302
                    tmp= a;
 
5303
                  set_if_bigger(tmp,1.0);
 
5304
                }
 
5305
                records = (ulong) tmp;
 
5306
              }
 
5307
 
 
5308
              if (ref_or_null_part)
 
5309
              {
 
5310
                /* We need to do two key searches to find key */
 
5311
                tmp *= 2.0;
 
5312
                records *= 2.0;
 
5313
              }
 
5314
 
 
5315
              /*
 
5316
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
5317
                E(#rows) from range optimizer. Make another try:
 
5318
                
 
5319
                If range optimizer produced E(#rows) for a prefix of the ref 
 
5320
                access we're considering, and that E(#rows) is lower then our
 
5321
                current estimate, make the adjustment.
 
5322
 
 
5323
                The decision whether we can re-use the estimate from the range
 
5324
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
5325
                applied to first table->quick_key_parts[key] key parts.
 
5326
              */
 
5327
              if (table->quick_keys.is_set(key) &&
 
5328
                  table->quick_key_parts[key] <= max_key_part &&
 
5329
                  const_part & (1 << table->quick_key_parts[key]) &&
 
5330
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
 
5331
                                                         const_part) &&
 
5332
                  records > (double) table->quick_rows[key])
 
5333
              {
 
5334
                tmp= records= (double) table->quick_rows[key];
 
5335
              }
 
5336
            }
 
5337
 
 
5338
            /* Limit the number of matched rows */
 
5339
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
 
5340
            if (table->covering_keys.is_set(key))
 
5341
            {
 
5342
              /* we can use only index tree */
 
5343
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
5344
            }
 
5345
            else
 
5346
              tmp= record_count * min(tmp,s->worst_seeks);
 
5347
          }
 
5348
          else
 
5349
            tmp= best_time;                    // Do nothing
 
5350
        }
 
5351
 
 
5352
        if (sj_inside_out_scan && !start_key)
 
5353
        {
 
5354
          tmp= tmp/2;
 
5355
          if (records)
 
5356
            records= records/2;
 
5357
        }
 
5358
 
 
5359
      }
 
5360
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
5361
      {
 
5362
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
5363
        best= tmp;
 
5364
        best_records= records;
 
5365
        best_key= start_key;
 
5366
        best_max_key_part= max_key_part;
 
5367
        best_ref_depends_map= found_ref;
 
5368
        best_is_sj_inside_out= sj_inside_out_scan;
 
5369
      }
 
5370
    }
 
5371
    records= best_records;
 
5372
  }
 
5373
 
 
5374
  /*
 
5375
    Don't test table scan if it can't be better.
 
5376
    Prefer key lookup if we would use the same key for scanning.
 
5377
 
 
5378
    Don't do a table scan on InnoDB tables, if we can read the used
 
5379
    parts of the row from any of the used index.
 
5380
    This is because table scans uses index and we would not win
 
5381
    anything by using a table scan.
 
5382
 
 
5383
    A word for word translation of the below if-statement in sergefp's
 
5384
    understanding: we check if we should use table scan if:
 
5385
    (1) The found 'ref' access produces more records than a table scan
 
5386
        (or index scan, or quick select), or 'ref' is more expensive than
 
5387
        any of them.
 
5388
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
5389
        'range' access using index IDX, and the best way to perform 'ref' 
 
5390
        access is to use the same index IDX, with the same or more key parts.
 
5391
        (note: it is not clear how this rule is/should be extended to 
 
5392
        index_merge quick selects)
 
5393
    (3) See above note about InnoDB.
 
5394
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
5395
             path, but there is no quick select)
 
5396
        If the condition in the above brackets holds, then the only possible
 
5397
        "table scan" access method is ALL/index (there is no quick select).
 
5398
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
5399
        choose it over ALL/index, there is no need to consider a full table
 
5400
        scan.
 
5401
  */
 
5402
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
5403
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
5404
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
5405
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
5406
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
 
5407
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
5408
  {                                             // Check full join
 
5409
    ha_rows rnd_records= s->found_records;
 
5410
    /*
 
5411
      If there is a filtering condition on the table (i.e. ref analyzer found
 
5412
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
5413
      preceding this table in the join order we're now considering), then 
 
5414
      assume that 25% of the rows will be filtered out by this condition.
 
5415
 
 
5416
      This heuristic is supposed to force tables used in exprZ to be before
 
5417
      this table in join order.
 
5418
    */
 
5419
    if (found_constraint)
 
5420
      rnd_records-= rnd_records/4;
 
5421
 
 
5422
    /*
 
5423
      If applicable, get a more accurate estimate. Don't use the two
 
5424
      heuristics at once.
 
5425
    */
 
5426
    if (s->table->quick_condition_rows != s->found_records)
 
5427
      rnd_records= s->table->quick_condition_rows;
 
5428
 
 
5429
    /*
 
5430
      Range optimizer never proposes a RANGE if it isn't better
 
5431
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
5432
      Here we estimate its cost.
 
5433
    */
 
5434
    if (s->quick)
 
5435
    {
 
5436
      /*
 
5437
        For each record we:
 
5438
        - read record range through 'quick'
 
5439
        - skip rows which does not satisfy WHERE constraints
 
5440
        TODO: 
 
5441
        We take into account possible use of join cache for ALL/index
 
5442
        access (see first else-branch below), but we don't take it into 
 
5443
        account here for range/index_merge access. Find out why this is so.
 
5444
      */
 
5445
      tmp= record_count *
 
5446
        (s->quick->read_time +
 
5447
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5448
    }
 
5449
    else
 
5450
    {
 
5451
      /* Estimate cost of reading table. */
 
5452
      tmp= s->table->file->scan_time();
 
5453
      if (s->table->map & join->outer_join)     // Can't use join cache
 
5454
      {
 
5455
        /*
 
5456
          For each record we have to:
 
5457
          - read the whole table record 
 
5458
          - skip rows which does not satisfy join condition
 
5459
        */
 
5460
        tmp= record_count *
 
5461
          (tmp +
 
5462
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
5463
      }
 
5464
      else
 
5465
      {
 
5466
        /* We read the table as many times as join buffer becomes full. */
 
5467
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
5468
                           record_count /
 
5469
                           (double) thd->variables.join_buff_size));
 
5470
        /* 
 
5471
            We don't make full cartesian product between rows in the scanned
 
5472
           table and existing records because we skip all rows from the
 
5473
           scanned table, which does not satisfy join condition when 
 
5474
           we read the table (see flush_cached_records for details). Here we
 
5475
           take into account cost to read and skip these records.
 
5476
        */
 
5477
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
5478
      }
 
5479
    }
 
5480
 
 
5481
    /*
 
5482
      We estimate the cost of evaluating WHERE clause for found records
 
5483
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
5484
      tmp give us total cost of using TABLE SCAN
 
5485
    */
 
5486
    if (best == DBL_MAX ||
 
5487
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
5488
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
5489
    {
 
5490
      /*
 
5491
        If the table has a range (s->quick is set) make_join_select()
 
5492
        will ensure that this will be used
 
5493
      */
 
5494
      best= tmp;
 
5495
      records= rows2double(rnd_records);
 
5496
      best_key= 0;
 
5497
      /* range/index_merge/ALL/index access method are "independent", so: */
 
5498
      best_ref_depends_map= 0;
 
5499
      best_is_sj_inside_out= false;
 
5500
    }
 
5501
  }
 
5502
 
 
5503
  /* Update the cost information for the current partial plan */
 
5504
  join->positions[idx].records_read= records;
 
5505
  join->positions[idx].read_time=    best;
 
5506
  join->positions[idx].key=          best_key;
 
5507
  join->positions[idx].table=        s;
 
5508
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
5509
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
5510
 
 
5511
  if (!best_key &&
 
5512
      idx == join->const_tables &&
 
5513
      s->table == join->sort_by_table &&
 
5514
      join->unit->select_limit_cnt >= records)
 
5515
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
 
5516
 
 
5517
  return;
 
5518
}
 
5519
 
 
5520
 
 
5521
/**
 
5522
  Selects and invokes a search strategy for an optimal query plan.
 
5523
 
 
5524
  The function checks user-configurable parameters that control the search
 
5525
  strategy for an optimal plan, selects the search method and then invokes
 
5526
  it. Each specific optimization procedure stores the final optimal plan in
 
5527
  the array 'join->best_positions', and the cost of the plan in
 
5528
  'join->best_read'.
 
5529
 
 
5530
  @param join         pointer to the structure providing all context info for
 
5531
                      the query
 
5532
  @param join_tables  set of the tables in the query
 
5533
 
 
5534
  @todo
 
5535
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
5536
    the greedy version. Will be removed when greedy_search is approved.
 
5537
 
 
5538
  @retval
 
5539
    false       ok
 
5540
  @retval
 
5541
    true        Fatal error
 
5542
*/
 
5543
 
 
5544
static bool
 
5545
choose_plan(JOIN *join, table_map join_tables)
 
5546
{
 
5547
  uint search_depth= join->thd->variables.optimizer_search_depth;
 
5548
  uint prune_level=  join->thd->variables.optimizer_prune_level;
 
5549
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
5550
 
 
5551
  join->cur_embedding_map= 0;
 
5552
  reset_nj_counters(join->join_list);
 
5553
  /*
 
5554
    if (SELECT_STRAIGHT_JOIN option is set)
 
5555
      reorder tables so dependent tables come after tables they depend 
 
5556
      on, otherwise keep tables in the order they were specified in the query 
 
5557
    else
 
5558
      Apply heuristic: pre-sort all access plans with respect to the number of
 
5559
      records accessed.
 
5560
  */
 
5561
  my_qsort(join->best_ref + join->const_tables,
 
5562
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
5563
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
5564
  join->cur_emb_sj_nests= 0;
 
5565
  if (straight_join)
 
5566
  {
 
5567
    optimize_straight_join(join, join_tables);
 
5568
  }
 
5569
  else
 
5570
  {
 
5571
    if (search_depth == MAX_TABLES+2)
 
5572
    { /*
 
5573
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
5574
        the greedy version. Will be removed when greedy_search is approved.
 
5575
      */
 
5576
      join->best_read= DBL_MAX;
 
5577
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
5578
        return(true);
 
5579
    } 
 
5580
    else
 
5581
    {
 
5582
      if (search_depth == 0)
 
5583
        /* Automatically determine a reasonable value for 'search_depth' */
 
5584
        search_depth= determine_search_depth(join);
 
5585
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
5586
        return(true);
 
5587
    }
 
5588
  }
 
5589
 
 
5590
  /* 
 
5591
    Store the cost of this query into a user variable
 
5592
    Don't update last_query_cost for statements that are not "flat joins" :
 
5593
    i.e. they have subqueries, unions or call stored procedures.
 
5594
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
5595
  */
 
5596
  if (join->thd->lex->is_single_level_stmt())
 
5597
    join->thd->status_var.last_query_cost= join->best_read;
 
5598
  return(false);
 
5599
}
 
5600
 
 
5601
 
 
5602
/**
 
5603
  Compare two JOIN_TAB objects based on the number of accessed records.
 
5604
 
 
5605
  @param ptr1 pointer to first JOIN_TAB object
 
5606
  @param ptr2 pointer to second JOIN_TAB object
807
5607
 
808
5608
  NOTES
809
5609
    The order relation implemented by join_tab_cmp() is not transitive,
815
5615
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
816
5616
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
817
5617
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
818
 
 
 
5618
     
819
5619
  @retval
820
5620
    1  if first is bigger
821
5621
  @retval
823
5623
  @retval
824
5624
    0  if equal
825
5625
*/
826
 
int join_tab_cmp(const void* ptr1, const void* ptr2)
 
5626
 
 
5627
static int
 
5628
join_tab_cmp(const void* ptr1, const void* ptr2)
827
5629
{
828
 
  JoinTable *jt1= *(JoinTable**) ptr1;
829
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5630
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5631
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
830
5632
 
831
5633
  if (jt1->dependent & jt2->table->map)
832
5634
    return 1;
833
5635
  if (jt2->dependent & jt1->table->map)
834
 
    return -1;
 
5636
    return -1;  
835
5637
  if (jt1->found_records > jt2->found_records)
836
5638
    return 1;
837
5639
  if (jt1->found_records < jt2->found_records)
838
 
    return -1;
 
5640
    return -1; 
839
5641
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
840
5642
}
841
5643
 
 
5644
 
842
5645
/**
843
5646
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
844
5647
*/
845
 
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
5648
 
 
5649
static int
 
5650
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
846
5651
{
847
 
  JoinTable *jt1= *(JoinTable**) ptr1;
848
 
  JoinTable *jt2= *(JoinTable**) ptr2;
 
5652
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
 
5653
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
849
5654
 
850
5655
  if (jt1->dependent & jt2->table->map)
851
5656
    return 1;
855
5660
}
856
5661
 
857
5662
/**
 
5663
  Heuristic procedure to automatically guess a reasonable degree of
 
5664
  exhaustiveness for the greedy search procedure.
 
5665
 
 
5666
  The procedure estimates the optimization time and selects a search depth
 
5667
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
5668
  find. If the number of tables in the query exceeds some constant, then
 
5669
  search_depth is set to this constant.
 
5670
 
 
5671
  @param join   pointer to the structure providing all context info for
 
5672
                the query
 
5673
 
 
5674
  @note
 
5675
    This is an extremely simplistic implementation that serves as a stub for a
 
5676
    more advanced analysis of the join. Ideally the search depth should be
 
5677
    determined by learning from previous query optimizations, because it will
 
5678
    depend on the CPU power (and other factors).
 
5679
 
 
5680
  @todo
 
5681
    this value should be determined dynamically, based on statistics:
 
5682
    uint max_tables_for_exhaustive_opt= 7;
 
5683
 
 
5684
  @todo
 
5685
    this value could be determined by some mapping of the form:
 
5686
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5687
 
 
5688
  @return
 
5689
    A positive integer that specifies the search depth (and thus the
 
5690
    exhaustiveness) of the depth-first search algorithm used by
 
5691
    'greedy_search'.
 
5692
*/
 
5693
 
 
5694
static uint
 
5695
determine_search_depth(JOIN *join)
 
5696
{
 
5697
  uint table_count=  join->tables - join->const_tables;
 
5698
  uint search_depth;
 
5699
  /* TODO: this value should be determined dynamically, based on statistics: */
 
5700
  uint max_tables_for_exhaustive_opt= 7;
 
5701
 
 
5702
  if (table_count <= max_tables_for_exhaustive_opt)
 
5703
    search_depth= table_count+1; // use exhaustive for small number of tables
 
5704
  else
 
5705
    /*
 
5706
      TODO: this value could be determined by some mapping of the form:
 
5707
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
5708
    */
 
5709
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
5710
 
 
5711
  return search_depth;
 
5712
}
 
5713
 
 
5714
 
 
5715
/**
 
5716
  Select the best ways to access the tables in a query without reordering them.
 
5717
 
 
5718
    Find the best access paths for each query table and compute their costs
 
5719
    according to their order in the array 'join->best_ref' (thus without
 
5720
    reordering the join tables). The function calls sequentially
 
5721
    'best_access_path' for each table in the query to select the best table
 
5722
    access method. The final optimal plan is stored in the array
 
5723
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
5724
 
 
5725
  @param join          pointer to the structure providing all context info for
 
5726
                       the query
 
5727
  @param join_tables   set of the tables in the query
 
5728
 
 
5729
  @note
 
5730
    This function can be applied to:
 
5731
    - queries with STRAIGHT_JOIN
 
5732
    - internally to compute the cost of an arbitrary QEP
 
5733
  @par
 
5734
    Thus 'optimize_straight_join' can be used at any stage of the query
 
5735
    optimization process to finalize a QEP as it is.
 
5736
*/
 
5737
 
 
5738
static void
 
5739
optimize_straight_join(JOIN *join, table_map join_tables)
 
5740
{
 
5741
  JOIN_TAB *s;
 
5742
  uint idx= join->const_tables;
 
5743
  double    record_count= 1.0;
 
5744
  double    read_time=    0.0;
 
5745
 
 
5746
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
5747
  {
 
5748
    /* Find the best access method from 's' to the current partial plan */
 
5749
    advance_sj_state(join_tables, s);
 
5750
    best_access_path(join, s, join->thd, join_tables, idx,
 
5751
                     record_count, read_time);
 
5752
    /* compute the cost of the new plan extended with 's' */
 
5753
    record_count*= join->positions[idx].records_read;
 
5754
    read_time+=    join->positions[idx].read_time;
 
5755
    join_tables&= ~(s->table->map);
 
5756
    ++idx;
 
5757
  }
 
5758
 
 
5759
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
5760
  if (join->sort_by_table &&
 
5761
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
5762
    read_time+= record_count;  // We have to make a temp table
 
5763
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
5764
  join->best_read= read_time;
 
5765
}
 
5766
 
 
5767
 
 
5768
/**
 
5769
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
5770
 
 
5771
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
5772
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
5773
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
5774
    selects the most promising table, and extends the current partial QEP with
 
5775
    that table.  Currenly the most 'promising' table is the one with least
 
5776
    expensive extension.\
 
5777
 
 
5778
    There are two extreme cases:
 
5779
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
5780
    best complete continuation of the partial QEP. This continuation can be
 
5781
    used directly as a result of the search.
 
5782
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
5783
    consideres the extension of the current QEP with each of the remaining
 
5784
    unoptimized tables.
 
5785
 
 
5786
    All other cases are in-between these two extremes. Thus the parameter
 
5787
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
5788
    value, the longer the optimizaton time and possibly the better the
 
5789
    resulting plan. The lower the value, the fewer alternative plans are
 
5790
    estimated, but the more likely to get a bad QEP.
 
5791
 
 
5792
    All intermediate and final results of the procedure are stored in 'join':
 
5793
    - join->positions     : modified for every partial QEP that is explored
 
5794
    - join->best_positions: modified for the current best complete QEP
 
5795
    - join->best_read     : modified for the current best complete QEP
 
5796
    - join->best_ref      : might be partially reordered
 
5797
 
 
5798
    The final optimal plan is stored in 'join->best_positions', and its
 
5799
    corresponding cost in 'join->best_read'.
 
5800
 
 
5801
  @note
 
5802
    The following pseudocode describes the algorithm of 'greedy_search':
 
5803
 
 
5804
    @code
 
5805
    procedure greedy_search
 
5806
    input: remaining_tables
 
5807
    output: pplan;
 
5808
    {
 
5809
      pplan = <>;
 
5810
      do {
 
5811
        (t, a) = best_extension(pplan, remaining_tables);
 
5812
        pplan = concat(pplan, (t, a));
 
5813
        remaining_tables = remaining_tables - t;
 
5814
      } while (remaining_tables != {})
 
5815
      return pplan;
 
5816
    }
 
5817
 
 
5818
  @endcode
 
5819
    where 'best_extension' is a placeholder for a procedure that selects the
 
5820
    most "promising" of all tables in 'remaining_tables'.
 
5821
    Currently this estimate is performed by calling
 
5822
    'best_extension_by_limited_search' to evaluate all extensions of the
 
5823
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
5824
    mainly depends on that of 'best_extension_by_limited_search'.
 
5825
 
 
5826
  @par
 
5827
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
5828
    worst-case complexity of this algorithm is <=
 
5829
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
5830
    complexity of greedy_search is O(N!).
 
5831
 
 
5832
  @par
 
5833
    In the future, 'greedy_search' might be extended to support other
 
5834
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
5835
 
 
5836
  @param join             pointer to the structure providing all context info
 
5837
                          for the query
 
5838
  @param remaining_tables set of tables not included into the partial plan yet
 
5839
  @param search_depth     controlls the exhaustiveness of the search
 
5840
  @param prune_level      the pruning heuristics that should be applied during
 
5841
                          search
 
5842
 
 
5843
  @retval
 
5844
    false       ok
 
5845
  @retval
 
5846
    true        Fatal error
 
5847
*/
 
5848
 
 
5849
static bool
 
5850
greedy_search(JOIN      *join,
 
5851
              table_map remaining_tables,
 
5852
              uint      search_depth,
 
5853
              uint      prune_level)
 
5854
{
 
5855
  double    record_count= 1.0;
 
5856
  double    read_time=    0.0;
 
5857
  uint      idx= join->const_tables; // index into 'join->best_ref'
 
5858
  uint      best_idx;
 
5859
  uint      size_remain;    // cardinality of remaining_tables
 
5860
  POSITION  best_pos;
 
5861
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
5862
 
 
5863
  /* number of tables that remain to be optimized */
 
5864
  size_remain= my_count_bits(remaining_tables);
 
5865
 
 
5866
  do {
 
5867
    /* Find the extension of the current QEP with the lowest cost */
 
5868
    join->best_read= DBL_MAX;
 
5869
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
5870
                                         read_time, search_depth, prune_level))
 
5871
      return(true);
 
5872
 
 
5873
    if (size_remain <= search_depth)
 
5874
    {
 
5875
      /*
 
5876
        'join->best_positions' contains a complete optimal extension of the
 
5877
        current partial QEP.
 
5878
      */
 
5879
      return(false);
 
5880
    }
 
5881
 
 
5882
    /* select the first table in the optimal extension as most promising */
 
5883
    best_pos= join->best_positions[idx];
 
5884
    best_table= best_pos.table;
 
5885
    /*
 
5886
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
5887
      'join->positions' for cost estimates, therefore we have to update its
 
5888
      value.
 
5889
    */
 
5890
    join->positions[idx]= best_pos;
 
5891
 
 
5892
    /* find the position of 'best_table' in 'join->best_ref' */
 
5893
    best_idx= idx;
 
5894
    JOIN_TAB *pos= join->best_ref[best_idx];
 
5895
    while (pos && best_table != pos)
 
5896
      pos= join->best_ref[++best_idx];
 
5897
    assert((pos != NULL)); // should always find 'best_table'
 
5898
    /* move 'best_table' at the first free position in the array of joins */
 
5899
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
 
5900
 
 
5901
    /* compute the cost of the new plan extended with 'best_table' */
 
5902
    record_count*= join->positions[idx].records_read;
 
5903
    read_time+=    join->positions[idx].read_time;
 
5904
 
 
5905
    remaining_tables&= ~(best_table->table->map);
 
5906
    --size_remain;
 
5907
    ++idx;
 
5908
  } while (true);
 
5909
}
 
5910
 
 
5911
 
 
5912
/**
 
5913
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
5914
  exhaustive search.
 
5915
 
 
5916
    The procedure searches for the optimal ordering of the query tables in set
 
5917
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
5918
    each table. The choice of a table order and an access path for each table
 
5919
    constitutes a query execution plan (QEP) that fully specifies how to
 
5920
    execute the query.
 
5921
   
 
5922
    The maximal size of the found plan is controlled by the parameter
 
5923
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
5924
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
5925
    of only some of the query tables. Such "partial" optimal plans are useful
 
5926
    only as input to query optimization procedures, and cannot be used directly
 
5927
    to execute a query.
 
5928
 
 
5929
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
5930
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
5931
    evaluates the cost of the partial plan extended by all access plans for
 
5932
    each of the relations in 'remaining_tables', expands the current partial
 
5933
    plan with the access plan that results in lowest cost of the expanded
 
5934
    partial plan, and removes the corresponding relation from
 
5935
    'remaining_tables'. The algorithm continues until it either constructs a
 
5936
    complete optimal plan, or constructs an optimal plartial plan with size =
 
5937
    search_depth.
 
5938
 
 
5939
    The final optimal plan is stored in 'join->best_positions'. The
 
5940
    corresponding cost of the optimal plan is in 'join->best_read'.
 
5941
 
 
5942
  @note
 
5943
    The procedure uses a recursive depth-first search where the depth of the
 
5944
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
5945
    parameter 'search_depth'.
 
5946
 
 
5947
  @note
 
5948
    The pseudocode below describes the algorithm of
 
5949
    'best_extension_by_limited_search'. The worst-case complexity of this
 
5950
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
5951
    the complexity of greedy_search is O(N!).
 
5952
 
 
5953
    @code
 
5954
    procedure best_extension_by_limited_search(
 
5955
      pplan in,             // in, partial plan of tables-joined-so-far
 
5956
      pplan_cost,           // in, cost of pplan
 
5957
      remaining_tables,     // in, set of tables not referenced in pplan
 
5958
      best_plan_so_far,     // in/out, best plan found so far
 
5959
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
5960
      search_depth)         // in, maximum size of the plans being considered
 
5961
    {
 
5962
      for each table T from remaining_tables
 
5963
      {
 
5964
        // Calculate the cost of using table T as above
 
5965
        cost = complex-series-of-calculations;
 
5966
 
 
5967
        // Add the cost to the cost so far.
 
5968
        pplan_cost+= cost;
 
5969
 
 
5970
        if (pplan_cost >= best_plan_so_far_cost)
 
5971
          // pplan_cost already too great, stop search
 
5972
          continue;
 
5973
 
 
5974
        pplan= expand pplan by best_access_method;
 
5975
        remaining_tables= remaining_tables - table T;
 
5976
        if (remaining_tables is not an empty set
 
5977
            and
 
5978
            search_depth > 1)
 
5979
        {
 
5980
          best_extension_by_limited_search(pplan, pplan_cost,
 
5981
                                           remaining_tables,
 
5982
                                           best_plan_so_far,
 
5983
                                           best_plan_so_far_cost,
 
5984
                                           search_depth - 1);
 
5985
        }
 
5986
        else
 
5987
        {
 
5988
          best_plan_so_far_cost= pplan_cost;
 
5989
          best_plan_so_far= pplan;
 
5990
        }
 
5991
      }
 
5992
    }
 
5993
    @endcode
 
5994
 
 
5995
  @note
 
5996
    When 'best_extension_by_limited_search' is called for the first time,
 
5997
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
5998
    The actual implementation provides a way to optionally use pruning
 
5999
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
6000
    space by skipping some partial plans.
 
6001
 
 
6002
  @note
 
6003
    The parameter 'search_depth' provides control over the recursion
 
6004
    depth, and thus the size of the resulting optimal plan.
 
6005
 
 
6006
  @param join             pointer to the structure providing all context info
 
6007
                          for the query
 
6008
  @param remaining_tables set of tables not included into the partial plan yet
 
6009
  @param idx              length of the partial QEP in 'join->positions';
 
6010
                          since a depth-first search is used, also corresponds
 
6011
                          to the current depth of the search tree;
 
6012
                          also an index in the array 'join->best_ref';
 
6013
  @param record_count     estimate for the number of records returned by the
 
6014
                          best partial plan
 
6015
  @param read_time        the cost of the best partial plan
 
6016
  @param search_depth     maximum depth of the recursion and thus size of the
 
6017
                          found optimal plan
 
6018
                          (0 < search_depth <= join->tables+1).
 
6019
  @param prune_level      pruning heuristics that should be applied during
 
6020
                          optimization
 
6021
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
6022
 
 
6023
  @retval
 
6024
    false       ok
 
6025
  @retval
 
6026
    true        Fatal error
 
6027
*/
 
6028
 
 
6029
static bool
 
6030
best_extension_by_limited_search(JOIN      *join,
 
6031
                                 table_map remaining_tables,
 
6032
                                 uint      idx,
 
6033
                                 double    record_count,
 
6034
                                 double    read_time,
 
6035
                                 uint      search_depth,
 
6036
                                 uint      prune_level)
 
6037
{
 
6038
  THD *thd= join->thd;
 
6039
  if (thd->killed)  // Abort
 
6040
    return(true);
 
6041
 
 
6042
  /* 
 
6043
     'join' is a partial plan with lower cost than the best plan so far,
 
6044
     so continue expanding it further with the tables in 'remaining_tables'.
 
6045
  */
 
6046
  JOIN_TAB *s;
 
6047
  double best_record_count= DBL_MAX;
 
6048
  double best_read_time=    DBL_MAX;
 
6049
 
 
6050
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
6051
  {
 
6052
    table_map real_table_bit= s->table->map;
 
6053
    if ((remaining_tables & real_table_bit) && 
 
6054
        !(remaining_tables & s->dependent) && 
 
6055
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6056
    {
 
6057
      double current_record_count, current_read_time;
 
6058
      advance_sj_state(remaining_tables, s);
 
6059
 
 
6060
      /*
 
6061
        psergey-insideout-todo: 
 
6062
          when best_access_path() detects it could do an InsideOut scan or 
 
6063
          some other scan, have it return an insideout scan and a flag that 
 
6064
          requests to "fork" this loop iteration. (Q: how does that behave 
 
6065
          when the depth is insufficient??)
 
6066
      */
 
6067
      /* Find the best access method from 's' to the current partial plan */
 
6068
      best_access_path(join, s, thd, remaining_tables, idx,
 
6069
                       record_count, read_time);
 
6070
      /* Compute the cost of extending the plan with 's' */
 
6071
      current_record_count= record_count * join->positions[idx].records_read;
 
6072
      current_read_time=    read_time + join->positions[idx].read_time;
 
6073
 
 
6074
      /* Expand only partial plans with lower cost than the best QEP so far */
 
6075
      if ((current_read_time +
 
6076
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
6077
      {
 
6078
        restore_prev_nj_state(s);
 
6079
        restore_prev_sj_state(remaining_tables, s);
 
6080
        continue;
 
6081
      }
 
6082
 
 
6083
      /*
 
6084
        Prune some less promising partial plans. This heuristic may miss
 
6085
        the optimal QEPs, thus it results in a non-exhaustive search.
 
6086
      */
 
6087
      if (prune_level == 1)
 
6088
      {
 
6089
        if (best_record_count > current_record_count ||
 
6090
            best_read_time > current_read_time ||
 
6091
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
6092
        {
 
6093
          if (best_record_count >= current_record_count &&
 
6094
              best_read_time >= current_read_time &&
 
6095
              /* TODO: What is the reasoning behind this condition? */
 
6096
              (!(s->key_dependent & remaining_tables) ||
 
6097
               join->positions[idx].records_read < 2.0))
 
6098
          {
 
6099
            best_record_count= current_record_count;
 
6100
            best_read_time=    current_read_time;
 
6101
          }
 
6102
        }
 
6103
        else
 
6104
        {
 
6105
          restore_prev_nj_state(s);
 
6106
          restore_prev_sj_state(remaining_tables, s);
 
6107
          continue;
 
6108
        }
 
6109
      }
 
6110
 
 
6111
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
6112
      { /* Recursively expand the current partial plan */
 
6113
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6114
        if (best_extension_by_limited_search(join,
 
6115
                                             remaining_tables & ~real_table_bit,
 
6116
                                             idx + 1,
 
6117
                                             current_record_count,
 
6118
                                             current_read_time,
 
6119
                                             search_depth - 1,
 
6120
                                             prune_level))
 
6121
          return(true);
 
6122
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6123
      }
 
6124
      else
 
6125
      { /*
 
6126
          'join' is either the best partial QEP with 'search_depth' relations,
 
6127
          or the best complete QEP so far, whichever is smaller.
 
6128
        */
 
6129
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
6130
        if (join->sort_by_table &&
 
6131
            join->sort_by_table !=
 
6132
            join->positions[join->const_tables].table->table)
 
6133
          /* We have to make a temp table */
 
6134
          current_read_time+= current_record_count;
 
6135
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
6136
        {
 
6137
          memcpy(join->best_positions, join->positions,
 
6138
                 sizeof(POSITION) * (idx + 1));
 
6139
          join->best_read= current_read_time - 0.001;
 
6140
        }
 
6141
      }
 
6142
      restore_prev_nj_state(s);
 
6143
      restore_prev_sj_state(remaining_tables, s);
 
6144
    }
 
6145
  }
 
6146
  return(false);
 
6147
}
 
6148
 
 
6149
 
 
6150
/**
 
6151
  @todo
 
6152
  - TODO: this function is here only temporarily until 'greedy_search' is
 
6153
  tested and accepted.
 
6154
 
 
6155
  RETURN VALUES
 
6156
    false       ok
 
6157
    true        Fatal error
 
6158
*/
 
6159
static bool
 
6160
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
 
6161
          double read_time)
 
6162
{
 
6163
  THD *thd= join->thd;
 
6164
  if (thd->killed)
 
6165
    return(true);
 
6166
  if (!rest_tables)
 
6167
  {
 
6168
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
6169
    if (join->sort_by_table &&
 
6170
        join->sort_by_table !=
 
6171
        join->positions[join->const_tables].table->table)
 
6172
      read_time+=record_count;                  // We have to make a temp table
 
6173
    if (read_time < join->best_read)
 
6174
    {
 
6175
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
6176
      join->best_read= read_time - 0.001;
 
6177
    }
 
6178
    return(false);
 
6179
  }
 
6180
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
6181
    return(false);                                      /* Found better before */
 
6182
 
 
6183
  JOIN_TAB *s;
 
6184
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
6185
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
6186
  {
 
6187
    table_map real_table_bit=s->table->map;
 
6188
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
6189
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
6190
    {
 
6191
      double records, best;
 
6192
      advance_sj_state(rest_tables, s);
 
6193
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
 
6194
                       read_time);
 
6195
      records= join->positions[idx].records_read;
 
6196
      best= join->positions[idx].read_time;
 
6197
      /*
 
6198
        Go to the next level only if there hasn't been a better key on
 
6199
        this level! This will cut down the search for a lot simple cases!
 
6200
      */
 
6201
      double current_record_count=record_count*records;
 
6202
      double current_read_time=read_time+best;
 
6203
      if (best_record_count > current_record_count ||
 
6204
          best_read_time > current_read_time ||
 
6205
          (idx == join->const_tables && s->table == join->sort_by_table))
 
6206
      {
 
6207
        if (best_record_count >= current_record_count &&
 
6208
            best_read_time >= current_read_time &&
 
6209
            (!(s->key_dependent & rest_tables) || records < 2.0))
 
6210
        {
 
6211
          best_record_count=current_record_count;
 
6212
          best_read_time=current_read_time;
 
6213
        }
 
6214
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6215
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
6216
                      current_record_count,current_read_time))
 
6217
          return(true);
 
6218
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
 
6219
      }
 
6220
      restore_prev_nj_state(s);
 
6221
      restore_prev_sj_state(rest_tables, s);
 
6222
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
6223
        break;                          // Don't test all combinations
 
6224
    }
 
6225
  }
 
6226
  return(false);
 
6227
}
 
6228
 
 
6229
 
 
6230
/**
858
6231
  Find how much space the prevous read not const tables takes in cache.
859
6232
*/
860
 
void calc_used_field_length(Session *, JoinTable *join_tab)
 
6233
 
 
6234
static void calc_used_field_length(THD *thd __attribute__((unused)),
 
6235
                                   JOIN_TAB *join_tab)
861
6236
{
862
 
  uint32_t null_fields,blobs,fields,rec_length;
 
6237
  uint null_fields,blobs,fields,rec_length;
863
6238
  Field **f_ptr,*field;
 
6239
  MY_BITMAP *read_set= join_tab->table->read_set;;
864
6240
 
865
6241
  null_fields= blobs= fields= rec_length=0;
866
6242
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
867
6243
  {
868
 
    if (field->isReadSet())
 
6244
    if (bitmap_is_set(read_set, field->field_index))
869
6245
    {
870
 
      uint32_t flags=field->flags;
 
6246
      uint flags=field->flags;
871
6247
      fields++;
872
6248
      rec_length+=field->pack_length();
873
6249
      if (flags & BLOB_FLAG)
874
 
        blobs++;
 
6250
        blobs++;
875
6251
      if (!(flags & NOT_NULL_FLAG))
876
 
        null_fields++;
 
6252
        null_fields++;
877
6253
    }
878
6254
  }
879
6255
  if (null_fields)
882
6258
    rec_length+=sizeof(bool);
883
6259
  if (blobs)
884
6260
  {
885
 
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
886
 
                                     (join_tab->table->getRecordLength()- rec_length));
887
 
    rec_length+= max((uint32_t)4,blob_length);
888
 
  }
889
 
  join_tab->used_fields= fields;
890
 
  join_tab->used_fieldlength= rec_length;
891
 
  join_tab->used_blobs= blobs;
892
 
}
893
 
 
894
 
StoredKey *get_store_key(Session *session,
895
 
                         optimizer::KeyUse *keyuse,
896
 
                         table_map used_tables,
897
 
                         KEY_PART_INFO *key_part,
898
 
                         unsigned char *key_buff,
899
 
                         uint32_t maybe_null)
900
 
{
901
 
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
902
 
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
903
 
  {
904
 
    return new store_key_const_item(session,
905
 
                                    key_part->field,
906
 
                                    key_buff + maybe_null,
907
 
                                    maybe_null ? key_buff : 0,
908
 
                                    key_part->length,
909
 
                                    key_use_val);
910
 
  }
911
 
  else if (key_use_val->type() == Item::FIELD_ITEM ||
912
 
           (key_use_val->type() == Item::REF_ITEM &&
913
 
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
914
 
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
915
 
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
916
 
  {
917
 
    return new store_key_field(session,
918
 
                               key_part->field,
919
 
                               key_buff + maybe_null,
920
 
                               maybe_null ? key_buff : 0,
921
 
                               key_part->length,
922
 
                               ((Item_field*) key_use_val->real_item())->field,
923
 
                               key_use_val->full_name());
924
 
  }
925
 
  return new store_key_item(session,
926
 
                            key_part->field,
927
 
                            key_buff + maybe_null,
928
 
                            maybe_null ? key_buff : 0,
929
 
                            key_part->length,
930
 
                            key_use_val);
931
 
}
 
6261
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
 
6262
                             (join_tab->table->getRecordLength()- rec_length));
 
6263
    rec_length+=(uint) max((uint)4,blob_length);
 
6264
  }
 
6265
  join_tab->used_fields=fields;
 
6266
  join_tab->used_fieldlength=rec_length;
 
6267
  join_tab->used_blobs=blobs;
 
6268
}
 
6269
 
 
6270
 
 
6271
static uint
 
6272
cache_record_length(JOIN *join,uint idx)
 
6273
{
 
6274
  uint length=0;
 
6275
  JOIN_TAB **pos,**end;
 
6276
  THD *thd=join->thd;
 
6277
 
 
6278
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
6279
       pos != end ;
 
6280
       pos++)
 
6281
  {
 
6282
    JOIN_TAB *join_tab= *pos;
 
6283
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
6284
      calc_used_field_length(thd, join_tab);
 
6285
    length+=join_tab->used_fieldlength;
 
6286
  }
 
6287
  return length;
 
6288
}
 
6289
 
 
6290
 
 
6291
/*
 
6292
  Get the number of different row combinations for subset of partial join
 
6293
 
 
6294
  SYNOPSIS
 
6295
    prev_record_reads()
 
6296
      join       The join structure
 
6297
      idx        Number of tables in the partial join order (i.e. the
 
6298
                 partial join order is in join->positions[0..idx-1])
 
6299
      found_ref  Bitmap of tables for which we need to find # of distinct
 
6300
                 row combinations.
 
6301
 
 
6302
  DESCRIPTION
 
6303
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
6304
    tables within that join order (specified in found_ref), find out how many
 
6305
    distinct row combinations of subset tables will be in the result of the
 
6306
    partial join order.
 
6307
     
 
6308
    This is used as follows: Suppose we have a table accessed with a ref-based
 
6309
    method. The ref access depends on current rows of tables in found_ref.
 
6310
    We want to count # of different ref accesses. We assume two ref accesses
 
6311
    will be different if at least one of access parameters is different.
 
6312
    Example: consider a query
 
6313
 
 
6314
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
6315
 
 
6316
    and a join order:
 
6317
      t1,  ref access on t1.key=c1
 
6318
      t2,  ref access on t2.key=c2       
 
6319
      t3,  ref access on t3.key=t1.field 
 
6320
    
 
6321
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
6322
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
6323
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
6324
            n_distinct_ref_scans = #records_read(t1)
 
6325
    
 
6326
    The reason for having this function (at least the latest version of it)
 
6327
    is that we need to account for buffering in join execution. 
 
6328
    
 
6329
    An edge-case example: if we have a non-first table in join accessed via
 
6330
    ref(const) or ref(param) where there is a small number of different
 
6331
    values of param, then the access will likely hit the disk cache and will
 
6332
    not require any disk seeks.
 
6333
    
 
6334
    The proper solution would be to assume an LRU disk cache of some size,
 
6335
    calculate probability of cache hits, etc. For now we just count
 
6336
    identical ref accesses as one.
 
6337
 
 
6338
  RETURN 
 
6339
    Expected number of row combinations
 
6340
*/
 
6341
 
 
6342
static double
 
6343
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
 
6344
{
 
6345
  double found=1.0;
 
6346
  POSITION *pos_end= join->positions - 1;
 
6347
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
6348
  {
 
6349
    if (pos->table->table->map & found_ref)
 
6350
    {
 
6351
      found_ref|= pos->ref_depend_map;
 
6352
      /* 
 
6353
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
 
6354
        with no matching row we will get position[t2].records_read==0. 
 
6355
        Actually the size of output is one null-complemented row, therefore 
 
6356
        we will use value of 1 whenever we get records_read==0.
 
6357
 
 
6358
        Note
 
6359
        - the above case can't occur if inner part of outer join has more 
 
6360
          than one table: table with no matches will not be marked as const.
 
6361
 
 
6362
        - Ideally we should add 1 to records_read for every possible null-
 
6363
          complemented row. We're not doing it because: 1. it will require
 
6364
          non-trivial code and add overhead. 2. The value of records_read
 
6365
          is an inprecise estimate and adding 1 (or, in the worst case,
 
6366
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
6367
      */
 
6368
      if (pos->records_read > DBL_EPSILON)
 
6369
        found*= pos->records_read;
 
6370
    }
 
6371
  }
 
6372
  return found;
 
6373
}
 
6374
 
932
6375
 
933
6376
/**
934
 
  This function is only called for const items on fields which are keys.
935
 
 
936
 
  @return
937
 
    returns 1 if there was some conversion made when the field was stored.
 
6377
  Set up join struct according to best position.
938
6378
*/
939
 
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
940
 
{
941
 
  bool error;
942
 
  Table *table= field->table;
943
 
  Session *session= table->in_use;
944
 
  ha_rows cuted_fields=session->cuted_fields;
945
 
 
946
 
  /*
947
 
    we should restore old value of count_cuted_fields because
948
 
    store_val_in_field can be called from mysql_insert
949
 
    with select_insert, which make count_cuted_fields= 1
950
 
   */
951
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
952
 
  session->count_cuted_fields= check_flag;
953
 
  error= item->save_in_field(field, 1);
954
 
  session->count_cuted_fields= old_count_cuted_fields;
955
 
  return error || cuted_fields != session->cuted_fields;
956
 
}
957
 
 
958
 
inline void add_cond_and_fix(Item **e1, Item *e2)
959
 
{
960
 
  if (*e1)
 
6379
 
 
6380
static bool
 
6381
get_best_combination(JOIN *join)
 
6382
{
 
6383
  uint i,tablenr;
 
6384
  table_map used_tables;
 
6385
  JOIN_TAB *join_tab,*j;
 
6386
  KEYUSE *keyuse;
 
6387
  uint table_count;
 
6388
  THD *thd=join->thd;
 
6389
 
 
6390
  table_count=join->tables;
 
6391
  if (!(join->join_tab=join_tab=
 
6392
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
 
6393
    return(true);
 
6394
 
 
6395
  join->full_join=0;
 
6396
 
 
6397
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
 
6398
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
961
6399
  {
962
 
    Item *res;
963
 
    if ((res= new Item_cond_and(*e1, e2)))
 
6400
    TABLE *form;
 
6401
    *j= *join->best_positions[tablenr].table;
 
6402
    form=join->table[tablenr]=j->table;
 
6403
    used_tables|= form->map;
 
6404
    form->reginfo.join_tab=j;
 
6405
    if (!*j->on_expr_ref)
 
6406
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
 
6407
    if (j->type == JT_CONST)
 
6408
      continue;                                 // Handled in make_join_stat..
 
6409
 
 
6410
    j->ref.key = -1;
 
6411
    j->ref.key_parts=0;
 
6412
 
 
6413
    if (j->type == JT_SYSTEM)
 
6414
      continue;
 
6415
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
964
6416
    {
965
 
      *e1= res;
966
 
      res->quick_fix_field();
 
6417
      j->type=JT_ALL;
 
6418
      if (tablenr != join->const_tables)
 
6419
        join->full_join=1;
967
6420
    }
 
6421
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
6422
      return(true);                        // Something went wrong
968
6423
  }
969
 
  else
970
 
    *e1= e2;
 
6424
 
 
6425
  for (i=0 ; i < table_count ; i++)
 
6426
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
6427
  update_depend_map(join);
 
6428
  return(0);
971
6429
}
972
6430
 
973
 
bool create_ref_for_key(JOIN *join, 
974
 
                        JoinTable *j, 
975
 
                        optimizer::KeyUse *org_keyuse,
976
 
                        table_map used_tables)
 
6431
 
 
6432
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
6433
                               table_map used_tables)
977
6434
{
978
 
  optimizer::KeyUse *keyuse= org_keyuse;
979
 
  Session  *session= join->session;
980
 
  uint32_t keyparts;
981
 
  uint32_t length;
982
 
  uint32_t key;
983
 
  Table *table= NULL;
984
 
  KEY *keyinfo= NULL;
 
6435
  KEYUSE *keyuse=org_keyuse;
 
6436
  THD  *thd= join->thd;
 
6437
  uint keyparts,length,key;
 
6438
  TABLE *table;
 
6439
  KEY *keyinfo;
985
6440
 
986
6441
  /*  Use best key from find_best */
987
 
  table= j->table;
988
 
  key= keyuse->getKey();
989
 
  keyinfo= table->key_info + key;
 
6442
  table=j->table;
 
6443
  key=keyuse->key;
 
6444
  keyinfo=table->key_info+key;
990
6445
 
991
6446
  {
992
 
    keyparts= length= 0;
993
 
    uint32_t found_part_ref_or_null= 0;
 
6447
    keyparts=length=0;
 
6448
    uint found_part_ref_or_null= 0;
994
6449
    /*
995
6450
      Calculate length for the used key
996
6451
      Stop if there is a missing key part or when we find second key_part
998
6453
    */
999
6454
    do
1000
6455
    {
1001
 
      if (! (~used_tables & keyuse->getUsedTables()))
 
6456
      if (!(~used_tables & keyuse->used_tables))
1002
6457
      {
1003
 
        if (keyparts == keyuse->getKeypart() &&
1004
 
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
1005
 
        {
1006
 
          keyparts++;
1007
 
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
1008
 
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
1009
 
        }
 
6458
        if (keyparts == keyuse->keypart &&
 
6459
            !(found_part_ref_or_null & keyuse->optimize))
 
6460
        {
 
6461
          keyparts++;
 
6462
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
6463
          found_part_ref_or_null|= keyuse->optimize;
 
6464
        }
1010
6465
      }
1011
6466
      keyuse++;
1012
 
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
6467
    } while (keyuse->table == table && keyuse->key == key);
1013
6468
  }
1014
6469
 
1015
6470
  /* set up fieldref */
1017
6472
  j->ref.key_parts=keyparts;
1018
6473
  j->ref.key_length=length;
1019
6474
  j->ref.key=(int) key;
1020
 
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1021
 
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1022
 
               (keyparts+1)))) ||
1023
 
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
1024
 
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
 
6475
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
 
6476
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
 
6477
                                                   (keyparts+1)))) ||
 
6478
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
 
6479
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
1025
6480
  {
1026
6481
    return(true);
1027
6482
  }
1031
6486
  j->ref.disable_cache= false;
1032
6487
  keyuse=org_keyuse;
1033
6488
 
1034
 
  StoredKey **ref_key= j->ref.key_copy;
1035
 
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
 
6489
  store_key **ref_key= j->ref.key_copy;
 
6490
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
1036
6491
  bool keyuse_uses_no_tables= true;
1037
6492
  {
1038
 
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
 
6493
    uint i;
 
6494
    for (i=0 ; i < keyparts ; keyuse++,i++)
1039
6495
    {
1040
 
      while (keyuse->getKeypart() != i ||
1041
 
             ((~used_tables) & keyuse->getUsedTables()))
1042
 
        keyuse++;       /* Skip other parts */
 
6496
      while (keyuse->keypart != i ||
 
6497
             ((~used_tables) & keyuse->used_tables))
 
6498
        keyuse++;                               /* Skip other parts */
1043
6499
 
1044
 
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1045
 
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1046
 
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1047
 
      if (keyuse->isNullRejected())
 
6500
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
 
6501
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
6502
      j->ref.cond_guards[i]= keyuse->cond_guard;
 
6503
      if (keyuse->null_rejecting) 
1048
6504
        j->ref.null_rejecting |= 1 << i;
1049
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1050
 
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1051
 
      {         // Compare against constant
1052
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
6505
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
 
6506
      if (!keyuse->used_tables &&
 
6507
          !(join->select_options & SELECT_DESCRIBE))
 
6508
      {                                 // Compare against constant
 
6509
        store_key_item tmp(thd, keyinfo->key_part[i].field,
1053
6510
                           key_buff + maybe_null,
1054
6511
                           maybe_null ?  key_buff : 0,
1055
 
                           keyinfo->key_part[i].length, keyuse->getVal());
1056
 
        if (session->is_fatal_error)
1057
 
          return(true);
1058
 
        tmp.copy();
 
6512
                           keyinfo->key_part[i].length, keyuse->val);
 
6513
        if (thd->is_fatal_error)
 
6514
          return(true);
 
6515
        tmp.copy();
1059
6516
      }
1060
6517
      else
1061
 
        *ref_key++= get_store_key(session,
1062
 
          keyuse,join->const_table_map,
1063
 
          &keyinfo->key_part[i],
1064
 
          key_buff, maybe_null);
 
6518
        *ref_key++= get_store_key(thd,
 
6519
                                  keyuse,join->const_table_map,
 
6520
                                  &keyinfo->key_part[i],
 
6521
                                  key_buff, maybe_null);
1065
6522
      /*
1066
 
        Remember if we are going to use REF_OR_NULL
1067
 
        But only if field _really_ can be null i.e. we force AM_REF
1068
 
        instead of AM_REF_OR_NULL in case if field can't be null
 
6523
        Remember if we are going to use REF_OR_NULL
 
6524
        But only if field _really_ can be null i.e. we force JT_REF
 
6525
        instead of JT_REF_OR_NULL in case if field can't be null
1069
6526
      */
1070
 
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1071
 
        null_ref_key= key_buff;
 
6527
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
6528
        null_ref_key= key_buff;
1072
6529
      key_buff+=keyinfo->key_part[i].store_length;
1073
6530
    }
1074
6531
  }
1075
 
  *ref_key= 0;       // end_marker
1076
 
  if (j->type == AM_CONST)
 
6532
  *ref_key=0;                           // end_marker
 
6533
  if (j->type == JT_CONST)
1077
6534
    j->table->const_table= 1;
1078
6535
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
1079
6536
           keyparts != keyinfo->key_parts || null_ref_key)
1080
6537
  {
1081
6538
    /* Must read with repeat */
1082
 
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
 
6539
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
1083
6540
    j->ref.null_ref_key= null_ref_key;
1084
6541
  }
1085
6542
  else if (keyuse_uses_no_tables)
1091
6548
      Here we should not mark the table as a 'const' as a field may
1092
6549
      have a 'normal' value or a NULL value.
1093
6550
    */
1094
 
    j->type= AM_CONST;
1095
 
  }
1096
 
  else
1097
 
    j->type= AM_EQ_REF;
1098
 
  return 0;
1099
 
}
 
6551
    j->type=JT_CONST;
 
6552
  }
 
6553
  else
 
6554
    j->type=JT_EQ_REF;
 
6555
  return(0);
 
6556
}
 
6557
 
 
6558
 
 
6559
 
 
6560
static store_key *
 
6561
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
 
6562
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
 
6563
{
 
6564
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
6565
  {
 
6566
    return new store_key_const_item(thd,
 
6567
                                    key_part->field,
 
6568
                                    key_buff + maybe_null,
 
6569
                                    maybe_null ? key_buff : 0,
 
6570
                                    key_part->length,
 
6571
                                    keyuse->val);
 
6572
  }
 
6573
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
6574
           (keyuse->val->type() == Item::REF_ITEM &&
 
6575
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
6576
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
6577
             Item_ref::DIRECT_REF && 
 
6578
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
6579
    return new store_key_field(thd,
 
6580
                               key_part->field,
 
6581
                               key_buff + maybe_null,
 
6582
                               maybe_null ? key_buff : 0,
 
6583
                               key_part->length,
 
6584
                               ((Item_field*) keyuse->val->real_item())->field,
 
6585
                               keyuse->val->full_name());
 
6586
  return new store_key_item(thd,
 
6587
                            key_part->field,
 
6588
                            key_buff + maybe_null,
 
6589
                            maybe_null ? key_buff : 0,
 
6590
                            key_part->length,
 
6591
                            keyuse->val);
 
6592
}
 
6593
 
 
6594
/**
 
6595
  This function is only called for const items on fields which are keys.
 
6596
 
 
6597
  @return
 
6598
    returns 1 if there was some conversion made when the field was stored.
 
6599
*/
 
6600
 
 
6601
bool
 
6602
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
6603
{
 
6604
  bool error;
 
6605
  TABLE *table= field->table;
 
6606
  THD *thd= table->in_use;
 
6607
  ha_rows cuted_fields=thd->cuted_fields;
 
6608
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
 
6609
                                                   table->write_set);
 
6610
 
 
6611
  /*
 
6612
    we should restore old value of count_cuted_fields because
 
6613
    store_val_in_field can be called from mysql_insert 
 
6614
    with select_insert, which make count_cuted_fields= 1
 
6615
   */
 
6616
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
 
6617
  thd->count_cuted_fields= check_flag;
 
6618
  error= item->save_in_field(field, 1);
 
6619
  thd->count_cuted_fields= old_count_cuted_fields;
 
6620
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
6621
  return error || cuted_fields != thd->cuted_fields;
 
6622
}
 
6623
 
 
6624
 
 
6625
static bool
 
6626
make_simple_join(JOIN *join,TABLE *tmp_table)
 
6627
{
 
6628
  TABLE **tableptr;
 
6629
  JOIN_TAB *join_tab;
 
6630
 
 
6631
  /*
 
6632
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
 
6633
    to this function through JOIN::exec (may happen for sub-queries).
 
6634
  */
 
6635
  if (!join->table_reexec)
 
6636
  {
 
6637
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
 
6638
      return(true);                        /* purecov: inspected */
 
6639
    if (join->tmp_join)
 
6640
      join->tmp_join->table_reexec= join->table_reexec;
 
6641
  }
 
6642
  if (!join->join_tab_reexec)
 
6643
  {
 
6644
    if (!(join->join_tab_reexec=
 
6645
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
 
6646
      return(true);                        /* purecov: inspected */
 
6647
    if (join->tmp_join)
 
6648
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
6649
  }
 
6650
  tableptr= join->table_reexec;
 
6651
  join_tab= join->join_tab_reexec;
 
6652
 
 
6653
  join->join_tab=join_tab;
 
6654
  join->table=tableptr; tableptr[0]=tmp_table;
 
6655
  join->tables=1;
 
6656
  join->const_tables=0;
 
6657
  join->const_table_map=0;
 
6658
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
6659
    join->tmp_table_param.func_count=0;
 
6660
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
6661
  join->first_record=join->sort_and_group=0;
 
6662
  join->send_records=(ha_rows) 0;
 
6663
  join->group=0;
 
6664
  join->row_limit=join->unit->select_limit_cnt;
 
6665
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
6666
 
 
6667
  join_tab->cache.buff=0;                       /* No caching */
 
6668
  join_tab->table=tmp_table;
 
6669
  join_tab->select=0;
 
6670
  join_tab->select_cond=0;
 
6671
  join_tab->quick=0;
 
6672
  join_tab->type= JT_ALL;                       /* Map through all records */
 
6673
  join_tab->keys.init();
 
6674
  join_tab->keys.set_all();                     /* test everything in quick */
 
6675
  join_tab->info=0;
 
6676
  join_tab->on_expr_ref=0;
 
6677
  join_tab->last_inner= 0;
 
6678
  join_tab->first_unmatched= 0;
 
6679
  join_tab->ref.key = -1;
 
6680
  join_tab->not_used_in_distinct=0;
 
6681
  join_tab->read_first_record= join_init_read_record;
 
6682
  join_tab->join=join;
 
6683
  join_tab->ref.key_parts= 0;
 
6684
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
6685
  join_tab->do_firstmatch= NULL;
 
6686
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
6687
  tmp_table->status=0;
 
6688
  tmp_table->null_row=0;
 
6689
  return(false);
 
6690
}
 
6691
 
 
6692
 
 
6693
inline void add_cond_and_fix(Item **e1, Item *e2)
 
6694
{
 
6695
  if (*e1)
 
6696
  {
 
6697
    Item *res;
 
6698
    if ((res= new Item_cond_and(*e1, e2)))
 
6699
    {
 
6700
      *e1= res;
 
6701
      res->quick_fix_field();
 
6702
    }
 
6703
  }
 
6704
  else
 
6705
    *e1= e2;
 
6706
}
 
6707
 
1100
6708
 
1101
6709
/**
1102
6710
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
1111
6719
    add "t1.field IS NOT NULL" to t1's table condition. @n
1112
6720
 
1113
6721
    Description of the optimization:
1114
 
 
 
6722
    
1115
6723
      We look through equalities choosen to perform ref/eq_ref access,
1116
6724
      pick equalities that have form "tbl.part_of_key = othertbl.field"
1117
6725
      (where othertbl is a non-const table and othertbl.field may be NULL)
1139
6747
      This optimization doesn't affect the choices that ref, range, or join
1140
6748
      optimizer make. This was intentional because this was added after 4.1
1141
6749
      was GA.
1142
 
 
 
6750
      
1143
6751
    Implementation overview
1144
6752
      1. update_ref_and_keys() accumulates info about null-rejecting
1145
 
         predicates in in KeyField::null_rejecting
1146
 
      1.1 add_key_part saves these to KeyUse.
1147
 
      2. create_ref_for_key copies them to table_reference_st.
 
6753
         predicates in in KEY_FIELD::null_rejecting
 
6754
      1.1 add_key_part saves these to KEYUSE.
 
6755
      2. create_ref_for_key copies them to TABLE_REF.
1148
6756
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1149
 
         appropiate JoinTable members.
 
6757
         appropiate JOIN_TAB members.
1150
6758
*/
1151
 
void add_not_null_conds(JOIN *join)
 
6759
 
 
6760
static void add_not_null_conds(JOIN *join)
1152
6761
{
1153
 
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
6762
  for (uint i=join->const_tables ; i < join->tables ; i++)
1154
6763
  {
1155
 
    JoinTable *tab=join->join_tab+i;
1156
 
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1157
 
         tab->type == AM_REF_OR_NULL) &&
 
6764
    JOIN_TAB *tab=join->join_tab+i;
 
6765
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
 
6766
         tab->type == JT_REF_OR_NULL) &&
1158
6767
        !tab->table->maybe_null)
1159
6768
    {
1160
 
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
6769
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
1161
6770
      {
1162
6771
        if (tab->ref.null_rejecting & (1 << keypart))
1163
6772
        {
1165
6774
          Item *notnull;
1166
6775
          assert(item->type() == Item::FIELD_ITEM);
1167
6776
          Item_field *not_null_item= (Item_field*)item;
1168
 
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
6777
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
1169
6778
          /*
1170
6779
            For UPDATE queries such as:
1171
6780
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1177
6786
            return;
1178
6787
          /*
1179
6788
            We need to do full fix_fields() call here in order to have correct
1180
 
            notnull->const_item(). This is needed e.g. by test_quick_select
1181
 
            when it is called from make_join_select after this function is
 
6789
            notnull->const_item(). This is needed e.g. by test_quick_select 
 
6790
            when it is called from make_join_select after this function is 
1182
6791
            called.
1183
6792
          */
1184
 
          if (notnull->fix_fields(join->session, &notnull))
 
6793
          if (notnull->fix_fields(join->thd, &notnull))
1185
6794
            return;
1186
6795
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1187
6796
        }
1206
6815
    -  pointer to the guarded predicate, if success
1207
6816
    -  0, otherwise
1208
6817
*/
1209
 
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
 
6818
 
 
6819
static COND*
 
6820
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
1210
6821
{
1211
6822
  COND *tmp;
1212
6823
  assert(cond != 0);
1222
6833
  return tmp;
1223
6834
}
1224
6835
 
1225
 
/*
 
6836
 
 
6837
/**
 
6838
  Fill in outer join related info for the execution plan structure.
 
6839
 
 
6840
    For each outer join operation left after simplification of the
 
6841
    original query the function set up the following pointers in the linear
 
6842
    structure join->join_tab representing the selected execution plan.
 
6843
    The first inner table t0 for the operation is set to refer to the last
 
6844
    inner table tk through the field t0->last_inner.
 
6845
    Any inner table ti for the operation are set to refer to the first
 
6846
    inner table ti->first_inner.
 
6847
    The first inner table t0 for the operation is set to refer to the
 
6848
    first inner table of the embedding outer join operation, if there is any,
 
6849
    through the field t0->first_upper.
 
6850
    The on expression for the outer join operation is attached to the
 
6851
    corresponding first inner table through the field t0->on_expr_ref.
 
6852
    Here ti are structures of the JOIN_TAB type.
 
6853
 
 
6854
  EXAMPLE. For the query: 
 
6855
  @code
 
6856
        SELECT * FROM t1
 
6857
                      LEFT JOIN
 
6858
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
6859
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
6860
          WHERE t1.c > 5,
 
6861
  @endcode
 
6862
 
 
6863
    given the execution plan with the table order t1,t2,t3,t4
 
6864
    is selected, the following references will be set;
 
6865
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
6866
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
6867
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
 
6868
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
6869
 
 
6870
  @param join   reference to the info fully describing the query
 
6871
 
 
6872
  @note
 
6873
    The function assumes that the simplification procedure has been
 
6874
    already applied to the join query (see simplify_joins).
 
6875
    This function can be called only after the execution plan
 
6876
    has been chosen.
 
6877
*/
 
6878
 
 
6879
static void
 
6880
make_outerjoin_info(JOIN *join)
 
6881
{
 
6882
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
6883
  {
 
6884
    JOIN_TAB *tab=join->join_tab+i;
 
6885
    TABLE *table=tab->table;
 
6886
    TABLE_LIST *tbl= table->pos_in_table_list;
 
6887
    TABLE_LIST *embedding= tbl->embedding;
 
6888
 
 
6889
    if (tbl->outer_join)
 
6890
    {
 
6891
      /* 
 
6892
        Table tab is the only one inner table for outer join.
 
6893
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
6894
        is in the query above.)
 
6895
      */
 
6896
      tab->last_inner= tab->first_inner= tab;
 
6897
      tab->on_expr_ref= &tbl->on_expr;
 
6898
      tab->cond_equal= tbl->cond_equal;
 
6899
      if (embedding)
 
6900
        tab->first_upper= embedding->nested_join->first_nested;
 
6901
    }    
 
6902
    for ( ; embedding ; embedding= embedding->embedding)
 
6903
    {
 
6904
      /* Ignore sj-nests: */
 
6905
      if (!embedding->on_expr)
 
6906
        continue;
 
6907
      NESTED_JOIN *nested_join= embedding->nested_join;
 
6908
      if (!nested_join->counter_)
 
6909
      {
 
6910
        /* 
 
6911
          Table tab is the first inner table for nested_join.
 
6912
          Save reference to it in the nested join structure.
 
6913
        */ 
 
6914
        nested_join->first_nested= tab;
 
6915
        tab->on_expr_ref= &embedding->on_expr;
 
6916
        tab->cond_equal= tbl->cond_equal;
 
6917
        if (embedding->embedding)
 
6918
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
6919
      }
 
6920
      if (!tab->first_inner)  
 
6921
        tab->first_inner= nested_join->first_nested;
 
6922
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
6923
        break;
 
6924
      /* Table tab is the last inner table for nested join. */
 
6925
      nested_join->first_nested->last_inner= tab;
 
6926
    }
 
6927
  }
 
6928
  return;
 
6929
}
 
6930
 
 
6931
 
 
6932
static bool
 
6933
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
6934
{
 
6935
  THD *thd= join->thd;
 
6936
  if (select)
 
6937
  {
 
6938
    add_not_null_conds(join);
 
6939
    table_map used_tables;
 
6940
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
6941
    {                        /* there may be a select without a cond. */    
 
6942
      if (join->tables > 1)
 
6943
        cond->update_used_tables();             // Tablenr may have changed
 
6944
      if (join->const_tables == join->tables &&
 
6945
          thd->lex->current_select->master_unit() ==
 
6946
          &thd->lex->unit)              // not upper level SELECT
 
6947
        join->const_table_map|=RAND_TABLE_BIT;
 
6948
      {                                         // Check const tables
 
6949
        COND *const_cond=
 
6950
          make_cond_for_table(cond,
 
6951
                              join->const_table_map,
 
6952
                              (table_map) 0, 1);
 
6953
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
6954
             tab < join->join_tab+join->tables ; tab++)
 
6955
        {
 
6956
          if (*tab->on_expr_ref)
 
6957
          {
 
6958
            JOIN_TAB *cond_tab= tab->first_inner;
 
6959
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
6960
                                           join->const_table_map,
 
6961
                                           (  table_map) 0, 0);
 
6962
            if (!tmp)
 
6963
              continue;
 
6964
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
6965
            if (!tmp)
 
6966
              return(1);
 
6967
            tmp->quick_fix_field();
 
6968
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
6969
                                    new Item_cond_and(cond_tab->select_cond,
 
6970
                                                      tmp);
 
6971
            if (!cond_tab->select_cond)
 
6972
              return(1);
 
6973
            cond_tab->select_cond->quick_fix_field();
 
6974
          }       
 
6975
        }
 
6976
        if (const_cond && !const_cond->val_int())
 
6977
        {
 
6978
          return(1);     // Impossible const condition
 
6979
        }
 
6980
      }
 
6981
    }
 
6982
    used_tables=((select->const_tables=join->const_table_map) |
 
6983
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
6984
    for (uint i=join->const_tables ; i < join->tables ; i++)
 
6985
    {
 
6986
      JOIN_TAB *tab=join->join_tab+i;
 
6987
      /*
 
6988
        first_inner is the X in queries like:
 
6989
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
6990
      */
 
6991
      JOIN_TAB *first_inner_tab= tab->first_inner; 
 
6992
      table_map current_map= tab->table->map;
 
6993
      bool use_quick_range=0;
 
6994
      COND *tmp;
 
6995
 
 
6996
      /*
 
6997
        Following force including random expression in last table condition.
 
6998
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
6999
      */
 
7000
      if (i == join->tables-1)
 
7001
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
7002
      used_tables|=current_map;
 
7003
 
 
7004
      if (tab->type == JT_REF && tab->quick &&
 
7005
          (uint) tab->ref.key == tab->quick->index &&
 
7006
          tab->ref.key_length < tab->quick->max_used_key_length)
 
7007
      {
 
7008
        /* Range uses longer key;  Use this instead of ref on key */
 
7009
        tab->type=JT_ALL;
 
7010
        use_quick_range=1;
 
7011
        tab->use_quick=1;
 
7012
        tab->ref.key= -1;
 
7013
        tab->ref.key_parts=0;           // Don't use ref key.
 
7014
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
7015
        /* 
 
7016
          We will use join cache here : prevent sorting of the first
 
7017
          table only and sort at the end.
 
7018
        */
 
7019
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
7020
          join->full_join= 1;
 
7021
      }
 
7022
 
 
7023
      tmp= NULL;
 
7024
      if (cond)
 
7025
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
7026
      if (cond && !tmp && tab->quick)
 
7027
      {                                         // Outer join
 
7028
        if (tab->type != JT_ALL)
 
7029
        {
 
7030
          /*
 
7031
            Don't use the quick method
 
7032
            We come here in the case where we have 'key=constant' and
 
7033
            the test is removed by make_cond_for_table()
 
7034
          */
 
7035
          delete tab->quick;
 
7036
          tab->quick= 0;
 
7037
        }
 
7038
        else
 
7039
        {
 
7040
          /*
 
7041
            Hack to handle the case where we only refer to a table
 
7042
            in the ON part of an OUTER JOIN. In this case we want the code
 
7043
            below to check if we should use 'quick' instead.
 
7044
          */
 
7045
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
7046
        }
 
7047
 
 
7048
      }
 
7049
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
7050
          tab->type == JT_EQ_REF)
 
7051
      {
 
7052
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
7053
                                       thd->memdup((uchar*) select,
 
7054
                                                   sizeof(*select)));
 
7055
        if (!sel)
 
7056
          return(1);                    // End of memory
 
7057
        /*
 
7058
          If tab is an inner table of an outer join operation,
 
7059
          add a match guard to the pushed down predicate.
 
7060
          The guard will turn the predicate on only after
 
7061
          the first match for outer tables is encountered.
 
7062
        */        
 
7063
        if (cond && tmp)
 
7064
        {
 
7065
          /*
 
7066
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
7067
            a cond, so neutralize the hack above.
 
7068
          */
 
7069
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
7070
            return(1);
 
7071
          tab->select_cond=sel->cond=tmp;
 
7072
          /* Push condition to storage engine if this is enabled
 
7073
             and the condition is not guarded */
 
7074
          tab->table->file->pushed_cond= NULL;
 
7075
          if (thd->variables.engine_condition_pushdown)
 
7076
          {
 
7077
            COND *push_cond= 
 
7078
              make_cond_for_table(tmp, current_map, current_map, 0);
 
7079
            if (push_cond)
 
7080
            {
 
7081
              /* Push condition to handler */
 
7082
              if (!tab->table->file->cond_push(push_cond))
 
7083
                tab->table->file->pushed_cond= push_cond;
 
7084
            }
 
7085
          }
 
7086
        }
 
7087
        else
 
7088
          tab->select_cond= sel->cond= NULL;
 
7089
 
 
7090
        sel->head=tab->table;
 
7091
        if (tab->quick)
 
7092
        {
 
7093
          /* Use quick key read if it's a constant and it's not used
 
7094
             with key reading */
 
7095
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
 
7096
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
 
7097
          {
 
7098
            sel->quick=tab->quick;              // Use value from get_quick_...
 
7099
            sel->quick_keys.clear_all();
 
7100
            sel->needed_reg.clear_all();
 
7101
          }
 
7102
          else
 
7103
          {
 
7104
            delete tab->quick;
 
7105
          }
 
7106
          tab->quick=0;
 
7107
        }
 
7108
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
 
7109
        if (i == join->const_tables && ref_key)
 
7110
        {
 
7111
          if (!tab->const_keys.is_clear_all() &&
 
7112
              tab->table->reginfo.impossible_range)
 
7113
            return(1);
 
7114
        }
 
7115
        else if (tab->type == JT_ALL && ! use_quick_range)
 
7116
        {
 
7117
          if (!tab->const_keys.is_clear_all() &&
 
7118
              tab->table->reginfo.impossible_range)
 
7119
            return(1);                          // Impossible range
 
7120
          /*
 
7121
            We plan to scan all rows.
 
7122
            Check again if we should use an index.
 
7123
            We could have used an column from a previous table in
 
7124
            the index if we are using limit and this is the first table
 
7125
          */
 
7126
 
 
7127
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
 
7128
              (!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)))
 
7129
          {
 
7130
            /* Join with outer join condition */
 
7131
            COND *orig_cond=sel->cond;
 
7132
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
7133
 
 
7134
            /*
 
7135
              We can't call sel->cond->fix_fields,
 
7136
              as it will break tab->on_expr if it's AND condition
 
7137
              (fix_fields currently removes extra AND/OR levels).
 
7138
              Yet attributes of the just built condition are not needed.
 
7139
              Thus we call sel->cond->quick_fix_field for safety.
 
7140
            */
 
7141
            if (sel->cond && !sel->cond->fixed)
 
7142
              sel->cond->quick_fix_field();
 
7143
 
 
7144
            if (sel->test_quick_select(thd, tab->keys,
 
7145
                                       used_tables & ~ current_map,
 
7146
                                       (join->select_options &
 
7147
                                        OPTION_FOUND_ROWS ?
 
7148
                                        HA_POS_ERROR :
 
7149
                                        join->unit->select_limit_cnt), 0,
 
7150
                                        false) < 0)
 
7151
            {
 
7152
              /*
 
7153
                Before reporting "Impossible WHERE" for the whole query
 
7154
                we have to check isn't it only "impossible ON" instead
 
7155
              */
 
7156
              sel->cond=orig_cond;
 
7157
              if (!*tab->on_expr_ref ||
 
7158
                  sel->test_quick_select(thd, tab->keys,
 
7159
                                         used_tables & ~ current_map,
 
7160
                                         (join->select_options &
 
7161
                                          OPTION_FOUND_ROWS ?
 
7162
                                          HA_POS_ERROR :
 
7163
                                          join->unit->select_limit_cnt),0,
 
7164
                                          false) < 0)
 
7165
                return(1);                      // Impossible WHERE
 
7166
            }
 
7167
            else
 
7168
              sel->cond=orig_cond;
 
7169
 
 
7170
            /* Fix for EXPLAIN */
 
7171
            if (sel->quick)
 
7172
              join->best_positions[i].records_read= (double)sel->quick->records;
 
7173
          }
 
7174
          else
 
7175
          {
 
7176
            sel->needed_reg=tab->needed_reg;
 
7177
            sel->quick_keys.clear_all();
 
7178
          }
 
7179
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
 
7180
              !sel->needed_reg.is_subset(tab->checked_keys))
 
7181
          {
 
7182
            tab->keys=sel->quick_keys;
 
7183
            tab->keys.merge(sel->needed_reg);
 
7184
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
 
7185
                             (select->quick_keys.is_clear_all() ||
 
7186
                              (select->quick &&
 
7187
                               (select->quick->records >= 100L)))) ?
 
7188
              2 : 1;
 
7189
            sel->read_tables= used_tables & ~current_map;
 
7190
          }
 
7191
          if (i != join->const_tables && tab->use_quick != 2)
 
7192
          {                                     /* Read with cache */
 
7193
            if (cond &&
 
7194
                (tmp=make_cond_for_table(cond,
 
7195
                                         join->const_table_map |
 
7196
                                         current_map,
 
7197
                                         current_map, 0)))
 
7198
            {
 
7199
              tab->cache.select=(SQL_SELECT*)
 
7200
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
 
7201
              tab->cache.select->cond=tmp;
 
7202
              tab->cache.select->read_tables=join->const_table_map;
 
7203
            }
 
7204
          }
 
7205
        }
 
7206
      }
 
7207
      
 
7208
      /* 
 
7209
        Push down conditions from all on expressions.
 
7210
        Each of these conditions are guarded by a variable
 
7211
        that turns if off just before null complemented row for
 
7212
        outer joins is formed. Thus, the condition from an
 
7213
        'on expression' are guaranteed not to be checked for
 
7214
        the null complemented row.
 
7215
      */ 
 
7216
 
 
7217
      /* First push down constant conditions from on expressions */
 
7218
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
7219
           join_tab < join->join_tab+join->tables ; join_tab++)
 
7220
      {
 
7221
        if (*join_tab->on_expr_ref)
 
7222
        {
 
7223
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
7224
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
7225
                                         join->const_table_map,
 
7226
                                         (table_map) 0, 0);
 
7227
          if (!tmp)
 
7228
            continue;
 
7229
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
7230
          if (!tmp)
 
7231
            return(1);
 
7232
          tmp->quick_fix_field();
 
7233
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
7234
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
7235
          if (!cond_tab->select_cond)
 
7236
            return(1);
 
7237
          cond_tab->select_cond->quick_fix_field();
 
7238
        }       
 
7239
      }
 
7240
 
 
7241
      /* Push down non-constant conditions from on expressions */
 
7242
      JOIN_TAB *last_tab= tab;
 
7243
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
7244
      {  
 
7245
        /* 
 
7246
          Table tab is the last inner table of an outer join.
 
7247
          An on expression is always attached to it.
 
7248
        */     
 
7249
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
7250
 
 
7251
        table_map used_tables2= (join->const_table_map |
 
7252
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
7253
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
7254
        {
 
7255
          current_map= tab->table->map;
 
7256
          used_tables2|= current_map;
 
7257
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
7258
                                              current_map, 0);
 
7259
          if (tmp_cond)
 
7260
          {
 
7261
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
7262
            /*
 
7263
              First add the guards for match variables of
 
7264
              all embedding outer join operations.
 
7265
            */
 
7266
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
7267
                                                     tmp_cond,
 
7268
                                                     first_inner_tab)))
 
7269
              return(1);
 
7270
            /* 
 
7271
              Now add the guard turning the predicate off for 
 
7272
              the null complemented row.
 
7273
            */ 
 
7274
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
7275
                                              &first_inner_tab->
 
7276
                                              not_null_compl);
 
7277
            if (tmp_cond)
 
7278
              tmp_cond->quick_fix_field();
 
7279
            /* Add the predicate to other pushed down predicates */
 
7280
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
7281
                                  new Item_cond_and(cond_tab->select_cond,
 
7282
                                                    tmp_cond);
 
7283
            if (!cond_tab->select_cond)
 
7284
              return(1);
 
7285
            cond_tab->select_cond->quick_fix_field();
 
7286
          }              
 
7287
        }
 
7288
        first_inner_tab= first_inner_tab->first_upper;       
 
7289
      }
 
7290
    }
 
7291
  }
 
7292
  return(0);
 
7293
}
 
7294
 
 
7295
 
 
7296
/* 
1226
7297
  Check if given expression uses only table fields covered by the given index
1227
7298
 
1228
7299
  SYNOPSIS
1235
7306
  DESCRIPTION
1236
7307
    Check if given expression only uses fields covered by index #keyno in the
1237
7308
    table tbl. The expression can use any fields in any other tables.
1238
 
 
1239
 
    The expression is guaranteed not to be AND or OR - those constructs are
 
7309
    
 
7310
    The expression is guaranteed not to be AND or OR - those constructs are 
1240
7311
    handled outside of this function.
1241
7312
 
1242
7313
  RETURN
1243
7314
    true   Yes
1244
7315
    false  No
1245
7316
*/
1246
 
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
 
7317
 
 
7318
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
 
7319
                            bool other_tbls_ok)
1247
7320
{
1248
7321
  if (item->const_item())
1249
7322
    return true;
1250
7323
 
1251
 
  /*
1252
 
    Don't push down the triggered conditions. Nested outer joins execution
 
7324
  /* 
 
7325
    Don't push down the triggered conditions. Nested outer joins execution 
1253
7326
    code may need to evaluate a condition several times (both triggered and
1254
7327
    untriggered), and there is no way to put thi
1255
7328
    TODO: Consider cloning the triggered condition and using the copies for:
1256
7329
      1. push the first copy down, to have most restrictive index condition
1257
7330
         possible
1258
 
      2. Put the second copy into tab->select_cond.
 
7331
      2. Put the second copy into tab->select_cond. 
1259
7332
  */
1260
 
  if (item->type() == Item::FUNC_ITEM &&
 
7333
  if (item->type() == Item::FUNC_ITEM && 
1261
7334
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
1262
7335
    return false;
1263
7336
 
1283
7356
    {
1284
7357
      /* This is a function, apply condition recursively to arguments */
1285
7358
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
1286
 
      Item *list_item;
1287
 
      while ((list_item=li++))
 
7359
      Item *item;
 
7360
      while ((item=li++))
1288
7361
      {
1289
7362
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
1290
7363
          return false;
1294
7367
  case Item::FIELD_ITEM:
1295
7368
    {
1296
7369
      Item_field *item_field= (Item_field*)item;
1297
 
      if (item_field->field->table != tbl)
 
7370
      if (item_field->field->table != tbl) 
1298
7371
        return true;
1299
 
      return item_field->field->part_of_key.test(keyno);
 
7372
      return item_field->field->part_of_key.is_set(keyno);
1300
7373
    }
1301
7374
  case Item::REF_ITEM:
1302
7375
    return uses_index_fields_only(item->real_item(), tbl, keyno,
1306
7379
  }
1307
7380
}
1308
7381
 
 
7382
 
1309
7383
#define ICP_COND_USES_INDEX_ONLY 10
1310
7384
 
1311
7385
/*
1320
7394
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
1321
7395
 
1322
7396
  DESCRIPTION
1323
 
    Get a part of the condition that can be checked when for the given table
 
7397
    Get a part of the condition that can be checked when for the given table 
1324
7398
    we have values only of fields covered by some index. The condition may
1325
 
    refer to other tables, it is assumed that we have values of all of their
 
7399
    refer to other tables, it is assumed that we have values of all of their 
1326
7400
    fields.
1327
7401
 
1328
7402
    Example:
1329
7403
      make_cond_for_index(
1330
7404
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
1331
 
          t2, keyno(t2.key1))
 
7405
          t2, keyno(t2.key1)) 
1332
7406
      will return
1333
7407
        "cond(t1.field) AND cond(t2.key2)"
1334
7408
 
1335
7409
  RETURN
1336
7410
    Index condition, or NULL if no condition could be inferred.
1337
7411
*/
1338
 
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
 
7412
 
 
7413
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
 
7414
                          bool other_tbls_ok)
1339
7415
{
1340
7416
  if (!cond)
1341
7417
    return NULL;
1342
7418
  if (cond->type() == Item::COND_ITEM)
1343
7419
  {
1344
 
    uint32_t n_marked= 0;
 
7420
    uint n_marked= 0;
1345
7421
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1346
7422
    {
1347
7423
      Item_cond_and *new_cond=new Item_cond_and;
1348
7424
      if (!new_cond)
1349
 
        return (COND*) 0;
 
7425
        return (COND*) 0;
1350
7426
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1351
7427
      Item *item;
1352
7428
      while ((item=li++))
1353
7429
      {
1354
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1355
 
        if (fix)
1356
 
          new_cond->argument_list()->push_back(fix);
 
7430
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7431
        if (fix)
 
7432
          new_cond->argument_list()->push_back(fix);
1357
7433
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1358
7434
      }
1359
7435
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1360
7436
        cond->marker= ICP_COND_USES_INDEX_ONLY;
1361
7437
      switch (new_cond->argument_list()->elements) {
1362
7438
      case 0:
1363
 
        return (COND*) 0;
 
7439
        return (COND*) 0;
1364
7440
      case 1:
1365
 
        return new_cond->argument_list()->head();
 
7441
        return new_cond->argument_list()->head();
1366
7442
      default:
1367
 
        new_cond->quick_fix_field();
1368
 
        return new_cond;
 
7443
        new_cond->quick_fix_field();
 
7444
        return new_cond;
1369
7445
      }
1370
7446
    }
1371
7447
    else /* It's OR */
1372
7448
    {
1373
7449
      Item_cond_or *new_cond=new Item_cond_or;
1374
7450
      if (!new_cond)
1375
 
        return (COND*) 0;
 
7451
        return (COND*) 0;
1376
7452
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1377
7453
      Item *item;
1378
7454
      while ((item=li++))
1379
7455
      {
1380
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1381
 
        if (!fix)
1382
 
          return (COND*) 0;
1383
 
        new_cond->argument_list()->push_back(fix);
 
7456
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
7457
        if (!fix)
 
7458
          return (COND*) 0;
 
7459
        new_cond->argument_list()->push_back(fix);
1384
7460
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1385
7461
      }
1386
7462
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1398
7474
}
1399
7475
 
1400
7476
 
1401
 
static Item *make_cond_remainder(Item *cond, bool exclude_index)
 
7477
Item *make_cond_remainder(Item *cond, bool exclude_index)
1402
7478
{
1403
7479
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
1404
7480
    return 0; /* Already checked */
1411
7487
      /* Create new top level AND item */
1412
7488
      Item_cond_and *new_cond=new Item_cond_and;
1413
7489
      if (!new_cond)
1414
 
        return (COND*) 0;
 
7490
        return (COND*) 0;
1415
7491
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1416
7492
      Item *item;
1417
7493
      while ((item=li++))
1418
7494
      {
1419
 
        Item *fix= make_cond_remainder(item, exclude_index);
1420
 
        if (fix)
 
7495
        Item *fix= make_cond_remainder(item, exclude_index);
 
7496
        if (fix)
1421
7497
        {
1422
 
          new_cond->argument_list()->push_back(fix);
 
7498
          new_cond->argument_list()->push_back(fix);
1423
7499
          tbl_map |= fix->used_tables();
1424
7500
        }
1425
7501
      }
1426
7502
      switch (new_cond->argument_list()->elements) {
1427
7503
      case 0:
1428
 
        return (COND*) 0;
 
7504
        return (COND*) 0;
1429
7505
      case 1:
1430
 
        return new_cond->argument_list()->head();
 
7506
        return new_cond->argument_list()->head();
1431
7507
      default:
1432
 
        new_cond->quick_fix_field();
 
7508
        new_cond->quick_fix_field();
1433
7509
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
1434
 
        return new_cond;
 
7510
        return new_cond;
1435
7511
      }
1436
7512
    }
1437
7513
    else /* It's OR */
1438
7514
    {
1439
7515
      Item_cond_or *new_cond=new Item_cond_or;
1440
7516
      if (!new_cond)
1441
 
        return (COND*) 0;
 
7517
        return (COND*) 0;
1442
7518
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1443
7519
      Item *item;
1444
7520
      while ((item=li++))
1445
7521
      {
1446
 
        Item *fix= make_cond_remainder(item, false);
1447
 
        if (!fix)
1448
 
          return (COND*) 0;
1449
 
        new_cond->argument_list()->push_back(fix);
 
7522
        Item *fix= make_cond_remainder(item, false);
 
7523
        if (!fix)
 
7524
          return (COND*) 0;
 
7525
        new_cond->argument_list()->push_back(fix);
1450
7526
        tbl_map |= fix->used_tables();
1451
7527
      }
1452
7528
      new_cond->quick_fix_field();
1458
7534
  return cond;
1459
7535
}
1460
7536
 
1461
 
/**
1462
 
  cleanup JoinTable.
1463
 
*/
1464
 
void JoinTable::cleanup()
 
7537
 
 
7538
/*
 
7539
  Try to extract and push the index condition
 
7540
 
 
7541
  SYNOPSIS
 
7542
    push_index_cond()
 
7543
      tab            A join tab that has tab->table->file and its condition
 
7544
                     in tab->select_cond
 
7545
      keyno          Index for which extract and push the condition
 
7546
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
7547
 
 
7548
  DESCRIPTION
 
7549
    Try to extract and push the index condition down to table handler
 
7550
*/
 
7551
 
 
7552
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
 
7553
{
 
7554
  Item *idx_cond;
 
7555
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
 
7556
      tab->join->thd->variables.engine_condition_pushdown)
 
7557
  {
 
7558
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
 
7559
                                  other_tbls_ok);
 
7560
 
 
7561
    if (idx_cond)
 
7562
    {
 
7563
      tab->pre_idx_push_select_cond= tab->select_cond;
 
7564
      Item *idx_remainder_cond= 
 
7565
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
7566
 
 
7567
      /*
 
7568
        Disable eq_ref's "lookup cache" if we've pushed down an index
 
7569
        condition. 
 
7570
        TODO: This check happens to work on current ICP implementations, but
 
7571
        there may exist a compliant implementation that will not work 
 
7572
        correctly with it. Sort this out when we stabilize the condition
 
7573
        pushdown APIs.
 
7574
      */
 
7575
      if (idx_remainder_cond != idx_cond)
 
7576
        tab->ref.disable_cache= true;
 
7577
 
 
7578
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
 
7579
 
 
7580
      if (row_cond)
 
7581
      {
 
7582
        if (!idx_remainder_cond)
 
7583
          tab->select_cond= row_cond;
 
7584
        else
 
7585
        {
 
7586
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
 
7587
          tab->select_cond->quick_fix_field();
 
7588
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
 
7589
            row_cond->used_tables() | idx_remainder_cond->used_tables();
 
7590
        }
 
7591
      }
 
7592
      else
 
7593
        tab->select_cond= idx_remainder_cond;
 
7594
      if (tab->select)
 
7595
      {
 
7596
        tab->select->cond= tab->select_cond;
 
7597
      }
 
7598
    }
 
7599
  }
 
7600
  return;
 
7601
}
 
7602
 
 
7603
 
 
7604
 
 
7605
    /*
 
7606
      Determine if the set is already ordered for ORDER BY, so it can 
 
7607
      disable join cache because it will change the ordering of the results.
 
7608
      Code handles sort table that is at any location (not only first after 
 
7609
      the const tables) despite the fact that it's currently prohibited.
 
7610
      We must disable join cache if the first non-const table alone is
 
7611
      ordered. If there is a temp table the ordering is done as a last
 
7612
      operation and doesn't prevent join cache usage.
 
7613
    */
 
7614
uint make_join_orderinfo(JOIN *join)
 
7615
{
 
7616
  uint i;
 
7617
  if (join->need_tmp)
 
7618
    return join->tables;
 
7619
 
 
7620
  for (i=join->const_tables ; i < join->tables ; i++)
 
7621
  {
 
7622
    JOIN_TAB *tab=join->join_tab+i;
 
7623
    TABLE *table=tab->table;
 
7624
    if ((table == join->sort_by_table && 
 
7625
         (!join->order || join->skip_sort_order)) ||
 
7626
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
 
7627
    {
 
7628
      break;
 
7629
    }
 
7630
  }
 
7631
  return i;
 
7632
}
 
7633
 
 
7634
 
 
7635
/*
 
7636
  Plan refinement stage: do various set ups for the executioner
 
7637
 
 
7638
  SYNOPSIS
 
7639
    make_join_readinfo()
 
7640
      join           Join being processed
 
7641
      options        Join's options (checking for SELECT_DESCRIBE, 
 
7642
                     SELECT_NO_JOIN_CACHE)
 
7643
      no_jbuf_after  Don't use join buffering after table with this number.
 
7644
 
 
7645
  DESCRIPTION
 
7646
    Plan refinement stage: do various set ups for the executioner
 
7647
      - set up use of join buffering
 
7648
      - push index conditions
 
7649
      - increment counters
 
7650
      - etc
 
7651
 
 
7652
  RETURN 
 
7653
    false - OK
 
7654
    true  - Out of memory
 
7655
*/
 
7656
 
 
7657
static bool
 
7658
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
 
7659
{
 
7660
  uint i;
 
7661
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
7662
  bool sorted= 1;
 
7663
 
 
7664
  for (i=join->const_tables ; i < join->tables ; i++)
 
7665
  {
 
7666
    JOIN_TAB *tab=join->join_tab+i;
 
7667
    TABLE *table=tab->table;
 
7668
    bool using_join_cache;
 
7669
    tab->read_record.table= table;
 
7670
    tab->read_record.file=table->file;
 
7671
    tab->next_select=sub_select;                /* normal select */
 
7672
    /* 
 
7673
      TODO: don't always instruct first table's ref/range access method to 
 
7674
      produce sorted output.
 
7675
    */
 
7676
    tab->sorted= sorted;
 
7677
    sorted= 0;                                  // only first must be sorted
 
7678
    if (tab->insideout_match_tab)
 
7679
    {
 
7680
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
 
7681
                                                         [tab->index].
 
7682
                                                         key_length)))
 
7683
        return true;
 
7684
    }
 
7685
    switch (tab->type) {
 
7686
    case JT_SYSTEM:                             // Only happens with left join
 
7687
      table->status=STATUS_NO_RECORD;
 
7688
      tab->read_first_record= join_read_system;
 
7689
      tab->read_record.read_record= join_no_more_records;
 
7690
      break;
 
7691
    case JT_CONST:                              // Only happens with left join
 
7692
      table->status=STATUS_NO_RECORD;
 
7693
      tab->read_first_record= join_read_const;
 
7694
      tab->read_record.read_record= join_no_more_records;
 
7695
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7696
          !table->no_keyread)
 
7697
      {
 
7698
        table->key_read=1;
 
7699
        table->file->extra(HA_EXTRA_KEYREAD);
 
7700
      }
 
7701
      break;
 
7702
    case JT_EQ_REF:
 
7703
      table->status=STATUS_NO_RECORD;
 
7704
      if (tab->select)
 
7705
      {
 
7706
        delete tab->select->quick;
 
7707
        tab->select->quick=0;
 
7708
      }
 
7709
      delete tab->quick;
 
7710
      tab->quick=0;
 
7711
      tab->read_first_record= join_read_key;
 
7712
      tab->read_record.read_record= join_no_more_records;
 
7713
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7714
          !table->no_keyread)
 
7715
      {
 
7716
        table->key_read=1;
 
7717
        table->file->extra(HA_EXTRA_KEYREAD);
 
7718
      }
 
7719
      else
 
7720
        push_index_cond(tab, tab->ref.key, true);
 
7721
      break;
 
7722
    case JT_REF_OR_NULL:
 
7723
    case JT_REF:
 
7724
      table->status=STATUS_NO_RECORD;
 
7725
      if (tab->select)
 
7726
      {
 
7727
        delete tab->select->quick;
 
7728
        tab->select->quick=0;
 
7729
      }
 
7730
      delete tab->quick;
 
7731
      tab->quick=0;
 
7732
      if (table->covering_keys.is_set(tab->ref.key) &&
 
7733
          !table->no_keyread)
 
7734
      {
 
7735
        table->key_read=1;
 
7736
        table->file->extra(HA_EXTRA_KEYREAD);
 
7737
      }
 
7738
      else
 
7739
        push_index_cond(tab, tab->ref.key, true);
 
7740
      if (tab->type == JT_REF)
 
7741
      {
 
7742
        tab->read_first_record= join_read_always_key;
 
7743
        tab->read_record.read_record= tab->insideout_match_tab? 
 
7744
           join_read_next_same_diff : join_read_next_same;
 
7745
      }
 
7746
      else
 
7747
      {
 
7748
        tab->read_first_record= join_read_always_key_or_null;
 
7749
        tab->read_record.read_record= join_read_next_same_or_null;
 
7750
      }
 
7751
      break;
 
7752
    case JT_ALL:
 
7753
      /*
 
7754
        If previous table use cache
 
7755
        If the incoming data set is already sorted don't use cache.
 
7756
      */
 
7757
      table->status=STATUS_NO_RECORD;
 
7758
      using_join_cache= false;
 
7759
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
7760
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
7761
          !tab->insideout_match_tab)
 
7762
      {
 
7763
        if ((options & SELECT_DESCRIBE) ||
 
7764
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
 
7765
                             i-join->const_tables))
 
7766
        {
 
7767
          using_join_cache= true;
 
7768
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
7769
        }
 
7770
      }
 
7771
      /* These init changes read_record */
 
7772
      if (tab->use_quick == 2)
 
7773
      {
 
7774
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
7775
        tab->read_first_record= join_init_quick_read_record;
 
7776
        if (statistics)
 
7777
          status_var_increment(join->thd->status_var.select_range_check_count);
 
7778
      }
 
7779
      else
 
7780
      {
 
7781
        tab->read_first_record= join_init_read_record;
 
7782
        if (i == join->const_tables)
 
7783
        {
 
7784
          if (tab->select && tab->select->quick)
 
7785
          {
 
7786
            if (statistics)
 
7787
              status_var_increment(join->thd->status_var.select_range_count);
 
7788
          }
 
7789
          else
 
7790
          {
 
7791
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7792
            if (statistics)
 
7793
              status_var_increment(join->thd->status_var.select_scan_count);
 
7794
          }
 
7795
        }
 
7796
        else
 
7797
        {
 
7798
          if (tab->select && tab->select->quick)
 
7799
          {
 
7800
            if (statistics)
 
7801
              status_var_increment(join->thd->status_var.select_full_range_join_count);
 
7802
          }
 
7803
          else
 
7804
          {
 
7805
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
7806
            if (statistics)
 
7807
              status_var_increment(join->thd->status_var.select_full_join_count);
 
7808
          }
 
7809
        }
 
7810
        if (!table->no_keyread)
 
7811
        {
 
7812
          if (tab->select && tab->select->quick &&
 
7813
              tab->select->quick->index != MAX_KEY && //not index_merge
 
7814
              table->covering_keys.is_set(tab->select->quick->index))
 
7815
          {
 
7816
            table->key_read=1;
 
7817
            table->file->extra(HA_EXTRA_KEYREAD);
 
7818
          }
 
7819
          else if (!table->covering_keys.is_clear_all() &&
 
7820
                   !(tab->select && tab->select->quick))
 
7821
          {                                     // Only read index tree
 
7822
            if (!tab->insideout_match_tab)
 
7823
            {
 
7824
              /*
 
7825
                See bug #26447: "Using the clustered index for a table scan
 
7826
                is always faster than using a secondary index".
 
7827
              */
 
7828
              if (table->s->primary_key != MAX_KEY &&
 
7829
                  table->file->primary_key_is_clustered())
 
7830
                tab->index= table->s->primary_key;
 
7831
              else
 
7832
                tab->index=find_shortest_key(table, & table->covering_keys);
 
7833
            }
 
7834
            tab->read_first_record= join_read_first;
 
7835
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
7836
          }
 
7837
        }
 
7838
        if (tab->select && tab->select->quick &&
 
7839
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
7840
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
7841
      }
 
7842
      break;
 
7843
    default:
 
7844
      break;                                    /* purecov: deadcode */
 
7845
    case JT_UNKNOWN:
 
7846
    case JT_MAYBE_REF:
 
7847
      abort();                                  /* purecov: deadcode */
 
7848
    }
 
7849
  }
 
7850
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
7851
  return(false);
 
7852
}
 
7853
 
 
7854
 
 
7855
/**
 
7856
  Give error if we some tables are done with a full join.
 
7857
 
 
7858
  This is used by multi_table_update and multi_table_delete when running
 
7859
  in safe mode.
 
7860
 
 
7861
  @param join           Join condition
 
7862
 
 
7863
  @retval
 
7864
    0   ok
 
7865
  @retval
 
7866
    1   Error (full join used)
 
7867
*/
 
7868
 
 
7869
bool error_if_full_join(JOIN *join)
 
7870
{
 
7871
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
 
7872
       tab < end;
 
7873
       tab++)
 
7874
  {
 
7875
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
7876
    {
 
7877
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
7878
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
7879
      return(1);
 
7880
    }
 
7881
  }
 
7882
  return(0);
 
7883
}
 
7884
 
 
7885
 
 
7886
/**
 
7887
  cleanup JOIN_TAB.
 
7888
*/
 
7889
 
 
7890
void JOIN_TAB::cleanup()
1465
7891
{
1466
7892
  delete select;
1467
7893
  select= 0;
1468
7894
  delete quick;
1469
7895
  quick= 0;
1470
 
  if (cache.buff)
1471
 
    free(cache.buff);
 
7896
  x_free(cache.buff);
1472
7897
  cache.buff= 0;
1473
7898
  limit= 0;
1474
7899
  if (table)
1476
7901
    if (table->key_read)
1477
7902
    {
1478
7903
      table->key_read= 0;
1479
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
7904
      table->file->extra(HA_EXTRA_NO_KEYREAD);
1480
7905
    }
1481
 
    table->cursor->ha_index_or_rnd_end();
 
7906
    table->file->ha_index_or_rnd_end();
1482
7907
    /*
1483
7908
      We need to reset this for next select
1484
7909
      (Tested in part_of_refkey)
1488
7913
  end_read_record(&read_record);
1489
7914
}
1490
7915
 
1491
 
bool only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
1492
 
{
1493
 
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1494
 
  {
1495
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1496
 
      return 0;
1497
 
  }
1498
 
  return 1;
1499
 
}
1500
 
 
1501
 
/**
1502
 
  Remove the following expressions from order_st BY and GROUP BY:
 
7916
 
 
7917
/**
 
7918
  Partially cleanup JOIN after it has executed: close index or rnd read
 
7919
  (table cursors), free quick selects.
 
7920
 
 
7921
    This function is called in the end of execution of a JOIN, before the used
 
7922
    tables are unlocked and closed.
 
7923
 
 
7924
    For a join that is resolved using a temporary table, the first sweep is
 
7925
    performed against actual tables and an intermediate result is inserted
 
7926
    into the temprorary table.
 
7927
    The last sweep is performed against the temporary table. Therefore,
 
7928
    the base tables and associated buffers used to fill the temporary table
 
7929
    are no longer needed, and this function is called to free them.
 
7930
 
 
7931
    For a join that is performed without a temporary table, this function
 
7932
    is called after all rows are sent, but before EOF packet is sent.
 
7933
 
 
7934
    For a simple SELECT with no subqueries this function performs a full
 
7935
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
7936
    tables.
 
7937
 
 
7938
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
7939
    do the full cleanup and need to do a partial cleanup only.
 
7940
    - If a JOIN is not the top level join, we must not unlock the tables
 
7941
    because the outer select may not have been evaluated yet, and we
 
7942
    can't unlock only selected tables of a query.
 
7943
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
7944
    should not free quick selects and join buffers because they will be
 
7945
    needed for the next execution of the correlated subquery.
 
7946
    - However, if this is a JOIN for a [sub]select, which is not
 
7947
    a correlated subquery itself, but has subqueries, we can free it
 
7948
    fully and also free JOINs of all its subqueries. The exception
 
7949
    is a subquery in SELECT list, e.g: @n
 
7950
    SELECT a, (select max(b) from t1) group by c @n
 
7951
    This subquery will not be evaluated at first sweep and its value will
 
7952
    not be inserted into the temporary table. Instead, it's evaluated
 
7953
    when selecting from the temporary table. Therefore, it can't be freed
 
7954
    here even though it's not correlated.
 
7955
 
 
7956
  @todo
 
7957
    Unlock tables even if the join isn't top level select in the tree
 
7958
*/
 
7959
 
 
7960
void JOIN::join_free()
 
7961
{
 
7962
  SELECT_LEX_UNIT *tmp_unit;
 
7963
  SELECT_LEX *sl;
 
7964
  /*
 
7965
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
7966
    free all tables.
 
7967
  */
 
7968
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
 
7969
  bool can_unlock= full;
 
7970
 
 
7971
  cleanup(full);
 
7972
 
 
7973
  for (tmp_unit= select_lex->first_inner_unit();
 
7974
       tmp_unit;
 
7975
       tmp_unit= tmp_unit->next_unit())
 
7976
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
7977
    {
 
7978
      Item_subselect *subselect= sl->master_unit()->item;
 
7979
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
7980
      /*
 
7981
        If this join is evaluated, we can fully clean it up and clean up all
 
7982
        its underlying joins even if they are correlated -- they will not be
 
7983
        used any more anyway.
 
7984
        If this join is not yet evaluated, we still must clean it up to
 
7985
        close its table cursors -- it may never get evaluated, as in case of
 
7986
        ... HAVING false OR a IN (SELECT ...))
 
7987
        but all table cursors must be closed before the unlock.
 
7988
      */
 
7989
      sl->cleanup_all_joins(full_local);
 
7990
      /* Can't unlock if at least one JOIN is still needed */
 
7991
      can_unlock= can_unlock && full_local;
 
7992
    }
 
7993
 
 
7994
  /*
 
7995
    We are not using tables anymore
 
7996
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
7997
  */
 
7998
  if (can_unlock && lock && thd->lock &&
 
7999
      !(select_options & SELECT_NO_UNLOCK) &&
 
8000
      !select_lex->subquery_in_having &&
 
8001
      (select_lex == (thd->lex->unit.fake_select_lex ?
 
8002
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
 
8003
  {
 
8004
    /*
 
8005
      TODO: unlock tables even if the join isn't top level select in the
 
8006
      tree.
 
8007
    */
 
8008
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
 
8009
    lock= 0;
 
8010
  }
 
8011
 
 
8012
  return;
 
8013
}
 
8014
 
 
8015
 
 
8016
/**
 
8017
  Free resources of given join.
 
8018
 
 
8019
  @param fill   true if we should free all resources, call with full==1
 
8020
                should be last, before it this function can be called with
 
8021
                full==0
 
8022
 
 
8023
  @note
 
8024
    With subquery this function definitely will be called several times,
 
8025
    but even for simple query it can be called several times.
 
8026
*/
 
8027
 
 
8028
void JOIN::cleanup(bool full)
 
8029
{
 
8030
  if (table)
 
8031
  {
 
8032
    JOIN_TAB *tab,*end;
 
8033
    /*
 
8034
      Only a sorted table may be cached.  This sorted table is always the
 
8035
      first non const table in join->table
 
8036
    */
 
8037
    if (tables > const_tables) // Test for not-const tables
 
8038
    {
 
8039
      free_io_cache(table[const_tables]);
 
8040
      filesort_free_buffers(table[const_tables],full);
 
8041
    }
 
8042
 
 
8043
    if (full)
 
8044
    {
 
8045
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8046
        tab->cleanup();
 
8047
      table= 0;
 
8048
    }
 
8049
    else
 
8050
    {
 
8051
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
8052
      {
 
8053
        if (tab->table)
 
8054
          tab->table->file->ha_index_or_rnd_end();
 
8055
      }
 
8056
    }
 
8057
    cleanup_sj_tmp_tables(this);//
 
8058
  }
 
8059
  /*
 
8060
    We are not using tables anymore
 
8061
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
8062
  */
 
8063
  if (full)
 
8064
  {
 
8065
    if (tmp_join)
 
8066
      tmp_table_param.copy_field= 0;
 
8067
    group_fields.delete_elements();
 
8068
    /*
 
8069
      We can't call delete_elements() on copy_funcs as this will cause
 
8070
      problems in free_elements() as some of the elements are then deleted.
 
8071
    */
 
8072
    tmp_table_param.copy_funcs.empty();
 
8073
    /*
 
8074
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
8075
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
8076
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
8077
      be removed in tmp_table_param.cleanup().
 
8078
    */
 
8079
    if (tmp_join &&
 
8080
        tmp_join != this &&
 
8081
        tmp_join->tmp_table_param.copy_field ==
 
8082
        tmp_table_param.copy_field)
 
8083
    {
 
8084
      tmp_join->tmp_table_param.copy_field=
 
8085
        tmp_join->tmp_table_param.save_copy_field= 0;
 
8086
    }
 
8087
    tmp_table_param.cleanup();
 
8088
  }
 
8089
  return;
 
8090
}
 
8091
 
 
8092
 
 
8093
/**
 
8094
  Remove the following expressions from ORDER BY and GROUP BY:
1503
8095
  Constant expressions @n
1504
8096
  Expression that only uses tables that are of type EQ_REF and the reference
1505
 
  is in the order_st list or if all refereed tables are of the above type.
 
8097
  is in the ORDER list or if all refereed tables are of the above type.
1506
8098
 
1507
8099
  In the following, the X field can be removed:
1508
8100
  @code
1509
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
1510
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
 
8101
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
 
8102
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
1511
8103
  @endcode
1512
8104
 
1513
8105
  These can't be optimized:
1514
8106
  @code
1515
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
1516
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
1517
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
 
8107
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
 
8108
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
 
8109
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1518
8110
  @endcode
1519
8111
*/
1520
 
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
 
8112
 
 
8113
static bool
 
8114
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
1521
8115
{
1522
8116
  if (tab->cached_eq_ref_table)                 // If cached
1523
8117
    return tab->eq_ref_table;
1524
8118
  tab->cached_eq_ref_table=1;
1525
8119
  /* We can skip const tables only if not an outer table */
1526
 
  if (tab->type == AM_CONST && !tab->first_inner)
1527
 
    return (tab->eq_ref_table=1);
1528
 
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
 
8120
  if (tab->type == JT_CONST && !tab->first_inner)
 
8121
    return (tab->eq_ref_table=1);               /* purecov: inspected */
 
8122
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
1529
8123
    return (tab->eq_ref_table=0);               // We must use this
1530
8124
  Item **ref_item=tab->ref.items;
1531
8125
  Item **end=ref_item+tab->ref.key_parts;
1532
 
  uint32_t found=0;
 
8126
  uint found=0;
1533
8127
  table_map map=tab->table->map;
1534
8128
 
1535
8129
  for (; ref_item != end ; ref_item++)
1536
8130
  {
1537
8131
    if (! (*ref_item)->const_item())
1538
8132
    {                                           // Not a const ref
1539
 
      order_st *order;
 
8133
      ORDER *order;
1540
8134
      for (order=start_order ; order ; order=order->next)
1541
8135
      {
1542
 
        if ((*ref_item)->eq(order->item[0],0))
1543
 
          break;
 
8136
        if ((*ref_item)->eq(order->item[0],0))
 
8137
          break;
1544
8138
      }
1545
8139
      if (order)
1546
8140
      {
1547
 
        found++;
1548
 
        assert(!(order->used & map));
1549
 
        order->used|=map;
1550
 
        continue;                               // Used in order_st BY
 
8141
        found++;
 
8142
        assert(!(order->used & map));
 
8143
        order->used|=map;
 
8144
        continue;                               // Used in ORDER BY
1551
8145
      }
1552
8146
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1553
 
        return (tab->eq_ref_table= 0);
 
8147
        return (tab->eq_ref_table=0);
1554
8148
    }
1555
8149
  }
1556
8150
  /* Check that there was no reference to table before sort order */
1562
8156
      continue;
1563
8157
    }
1564
8158
    if (start_order->depend_map & map)
1565
 
      return (tab->eq_ref_table= 0);
1566
 
  }
1567
 
  return tab->eq_ref_table= 1;
1568
 
}
 
8159
      return (tab->eq_ref_table=0);
 
8160
  }
 
8161
  return tab->eq_ref_table=1;
 
8162
}
 
8163
 
 
8164
 
 
8165
static bool
 
8166
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
 
8167
{
 
8168
  if (specialflag &  SPECIAL_SAFE_MODE)
 
8169
    return 0;                   // skip this optimize /* purecov: inspected */
 
8170
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
8171
  {
 
8172
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
8173
      return 0;
 
8174
  }
 
8175
  return 1;
 
8176
}
 
8177
 
 
8178
 
 
8179
/** Update the dependency map for the tables. */
 
8180
 
 
8181
static void update_depend_map(JOIN *join)
 
8182
{
 
8183
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
8184
 
 
8185
  for (; join_tab != end ; join_tab++)
 
8186
  {
 
8187
    TABLE_REF *ref= &join_tab->ref;
 
8188
    table_map depend_map=0;
 
8189
    Item **item=ref->items;
 
8190
    uint i;
 
8191
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
8192
      depend_map|=(*item)->used_tables();
 
8193
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
8194
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
8195
    for (JOIN_TAB **tab=join->map2table;
 
8196
         depend_map ;
 
8197
         tab++,depend_map>>=1 )
 
8198
    {
 
8199
      if (depend_map & 1)
 
8200
        ref->depend_map|=(*tab)->ref.depend_map;
 
8201
    }
 
8202
  }
 
8203
}
 
8204
 
 
8205
 
 
8206
/** Update the dependency map for the sort order. */
 
8207
 
 
8208
static void update_depend_map(JOIN *join, ORDER *order)
 
8209
{
 
8210
  for (; order ; order=order->next)
 
8211
  {
 
8212
    table_map depend_map;
 
8213
    order->item[0]->update_used_tables();
 
8214
    order->depend_map=depend_map=order->item[0]->used_tables();
 
8215
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
8216
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
8217
        && !order->item[0]->with_sum_func)
 
8218
    {
 
8219
      for (JOIN_TAB **tab=join->map2table;
 
8220
           depend_map ;
 
8221
           tab++, depend_map>>=1)
 
8222
      {
 
8223
        if (depend_map & 1)
 
8224
          order->depend_map|=(*tab)->ref.depend_map;
 
8225
      }
 
8226
    }
 
8227
  }
 
8228
}
 
8229
 
 
8230
 
 
8231
/**
 
8232
  Remove all constants and check if ORDER only contains simple
 
8233
  expressions.
 
8234
 
 
8235
  simple_order is set to 1 if sort_order only uses fields from head table
 
8236
  and the head table is not a LEFT JOIN table.
 
8237
 
 
8238
  @param join                   Join handler
 
8239
  @param first_order            List of SORT or GROUP order
 
8240
  @param cond                   WHERE statement
 
8241
  @param change_list            Set to 1 if we should remove things from list.
 
8242
                               If this is not set, then only simple_order is
 
8243
                               calculated.
 
8244
  @param simple_order           Set to 1 if we are only using simple expressions
 
8245
 
 
8246
  @return
 
8247
    Returns new sort order
 
8248
*/
 
8249
 
 
8250
static ORDER *
 
8251
remove_const(JOIN *join,ORDER *first_order, COND *cond,
 
8252
             bool change_list, bool *simple_order)
 
8253
{
 
8254
  if (join->tables == join->const_tables)
 
8255
    return change_list ? 0 : first_order;               // No need to sort
 
8256
 
 
8257
  ORDER *order,**prev_ptr;
 
8258
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
8259
  table_map not_const_tables= ~join->const_table_map;
 
8260
  table_map ref;
 
8261
 
 
8262
  prev_ptr= &first_order;
 
8263
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
8264
 
 
8265
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
8266
 
 
8267
  update_depend_map(join, first_order);
 
8268
  for (order=first_order; order ; order=order->next)
 
8269
  {
 
8270
    table_map order_tables=order->item[0]->used_tables();
 
8271
    if (order->item[0]->with_sum_func)
 
8272
      *simple_order=0;                          // Must do a temp table to sort
 
8273
    else if (!(order_tables & not_const_tables))
 
8274
    {
 
8275
      if (order->item[0]->with_subselect)
 
8276
        order->item[0]->val_str(&order->item[0]->str_value);
 
8277
      continue;                                 // skip const item
 
8278
    }
 
8279
    else
 
8280
    {
 
8281
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
8282
        *simple_order=0;
 
8283
      else
 
8284
      {
 
8285
        Item *comp_item=0;
 
8286
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
8287
        {
 
8288
          continue;
 
8289
        }
 
8290
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
8291
        {
 
8292
          if (!(order_tables & first_table) &&
 
8293
              only_eq_ref_tables(join,first_order, ref))
 
8294
          {
 
8295
            continue;
 
8296
          }
 
8297
          *simple_order=0;                      // Must do a temp table to sort
 
8298
        }
 
8299
      }
 
8300
    }
 
8301
    if (change_list)
 
8302
      *prev_ptr= order;                         // use this entry
 
8303
    prev_ptr= &order->next;
 
8304
  }
 
8305
  if (change_list)
 
8306
    *prev_ptr=0;
 
8307
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
8308
    *simple_order=1;
 
8309
  return(first_order);
 
8310
}
 
8311
 
 
8312
 
 
8313
static int
 
8314
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
 
8315
                 List<Item> &fields, bool send_row, uint64_t select_options,
 
8316
                 const char *info, Item *having)
 
8317
{
 
8318
  if (select_options & SELECT_DESCRIBE)
 
8319
  {
 
8320
    select_describe(join, false, false, false, info);
 
8321
    return(0);
 
8322
  }
 
8323
 
 
8324
  join->join_free();
 
8325
 
 
8326
  if (send_row)
 
8327
  {
 
8328
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
 
8329
      mark_as_null_row(table->table);           // All fields are NULL
 
8330
    if (having && having->val_int() == 0)
 
8331
      send_row=0;
 
8332
  }
 
8333
  if (!(result->send_fields(fields,
 
8334
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
8335
  {
 
8336
    if (send_row)
 
8337
    {
 
8338
      List_iterator_fast<Item> it(fields);
 
8339
      Item *item;
 
8340
      while ((item= it++))
 
8341
        item->no_rows_in_result();
 
8342
      result->send_data(fields);
 
8343
    }
 
8344
    result->send_eof();                         // Should be safe
 
8345
  }
 
8346
  /* Update results for FOUND_ROWS */
 
8347
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
 
8348
  return(0);
 
8349
}
 
8350
 
 
8351
/*
 
8352
  used only in JOIN::clear
 
8353
*/
 
8354
static void clear_tables(JOIN *join)
 
8355
{
 
8356
  /* 
 
8357
    must clear only the non-const tables, as const tables
 
8358
    are not re-calculated.
 
8359
  */
 
8360
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
8361
    mark_as_null_row(join->table[i]);           // All fields are NULL
 
8362
}
 
8363
 
 
8364
/*****************************************************************************
 
8365
  Make som simple condition optimization:
 
8366
  If there is a test 'field = const' change all refs to 'field' to 'const'
 
8367
  Remove all dummy tests 'item = item', 'const op const'.
 
8368
  Remove all 'item is NULL', when item can never be null!
 
8369
  item->marker should be 0 for all items on entry
 
8370
  Return in cond_value false if condition is impossible (1 = 2)
 
8371
*****************************************************************************/
 
8372
 
 
8373
class COND_CMP :public ilink {
 
8374
public:
 
8375
  static void *operator new(size_t size)
 
8376
  {
 
8377
    return (void*) sql_alloc((uint) size);
 
8378
  }
 
8379
  static void operator delete(void *ptr __attribute__((unused)),
 
8380
                              size_t size __attribute__((unused)))
 
8381
  { TRASH(ptr, size); }
 
8382
 
 
8383
  Item *and_level;
 
8384
  Item_func *cmp_func;
 
8385
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
 
8386
};
 
8387
 
 
8388
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
8389
template class I_List<COND_CMP>;
 
8390
template class I_List_iterator<COND_CMP>;
 
8391
#endif
 
8392
 
1569
8393
 
1570
8394
/**
1571
8395
  Find the multiple equality predicate containing a field.
1585
8409
    - Item_equal for the found multiple equality predicate if a success;
1586
8410
    - NULL otherwise.
1587
8411
*/
1588
 
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
 
8412
 
 
8413
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
 
8414
                            bool *inherited_fl)
1589
8415
{
1590
8416
  Item_equal *item= 0;
1591
8417
  bool in_upper_level= false;
1606
8432
  return item;
1607
8433
}
1608
8434
 
 
8435
  
1609
8436
/**
1610
8437
  Check whether an equality can be used to build multiple equalities.
1611
8438
 
1632
8459
    the check_equality will be called for the following equality
1633
8460
    predicates a=b, b=c, b=2 and f=e.
1634
8461
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
1635
 
      *cond_equal into (0,[Item_equal(a,b)]).
 
8462
      *cond_equal into (0,[Item_equal(a,b)]). 
1636
8463
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
1637
8464
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
1638
8465
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
1645
8472
    the Field::eq_def method) are placed to the same multiple equalities.
1646
8473
    Because of this some equality predicates are not eliminated and
1647
8474
    can be used in the constant propagation procedure.
1648
 
    We could weeken the equlity test as soon as at least one of the
1649
 
    equal fields is to be equal to a constant. It would require a
 
8475
    We could weeken the equlity test as soon as at least one of the 
 
8476
    equal fields is to be equal to a constant. It would require a 
1650
8477
    more complicated implementation: we would have to store, in
1651
8478
    general case, its own constant for each fields from the multiple
1652
8479
    equality. But at the same time it would allow us to get rid
1664
8491
    containing just field1 and field2 is added to the existing
1665
8492
    multiple equalities.
1666
8493
    If the function processes the predicate of the form field1=const,
1667
 
    it looks for a multiple equality containing field1. If found, the
 
8494
    it looks for a multiple equality containing field1. If found, the 
1668
8495
    function checks the constant of the multiple equality. If the value
1669
8496
    is unknown, it is setup to const. Otherwise the value is compared with
1670
8497
    const and the evaluation of the equality predicate is performed.
1687
8514
  @retval
1688
8515
    false   otherwise
1689
8516
*/
1690
 
static bool check_simple_equality(Item *left_item,
1691
 
                                  Item *right_item,
1692
 
                                  Item *item,
1693
 
                                  COND_EQUAL *cond_equal)
 
8517
 
 
8518
static bool check_simple_equality(Item *left_item, Item *right_item,
 
8519
                                  Item *item, COND_EQUAL *cond_equal)
1694
8520
{
 
8521
  if (left_item->type() == Item::REF_ITEM &&
 
8522
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
 
8523
  {
 
8524
    if (((Item_ref*)left_item)->depended_from)
 
8525
      return false;
 
8526
    left_item= left_item->real_item();
 
8527
  }
 
8528
  if (right_item->type() == Item::REF_ITEM &&
 
8529
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
 
8530
  {
 
8531
    if (((Item_ref*)right_item)->depended_from)
 
8532
      return false;
 
8533
    right_item= right_item->real_item();
 
8534
  }
1695
8535
  if (left_item->type() == Item::FIELD_ITEM &&
1696
8536
      right_item->type() == Item::FIELD_ITEM &&
1697
8537
      !((Item_field*)left_item)->depended_from &&
1709
8549
    bool left_copyfl, right_copyfl;
1710
8550
    Item_equal *left_item_equal=
1711
8551
               find_item_equal(cond_equal, left_field, &left_copyfl);
1712
 
    Item_equal *right_item_equal=
 
8552
    Item_equal *right_item_equal= 
1713
8553
               find_item_equal(cond_equal, right_field, &right_copyfl);
1714
8554
 
1715
8555
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
1716
8556
    if (left_field->eq(right_field)) /* f = f */
1717
 
      return (!(left_field->maybe_null() && !left_item_equal));
 
8557
      return (!(left_field->maybe_null() && !left_item_equal)); 
1718
8558
 
1719
8559
    if (left_item_equal && left_item_equal == right_item_equal)
1720
8560
    {
1721
 
      /*
 
8561
      /* 
1722
8562
        The equality predicate is inference of one of the existing
1723
8563
        multiple equalities, i.e the condition is already covered
1724
8564
        by upper level equalities
1725
8565
      */
1726
8566
       return true;
1727
8567
    }
1728
 
 
1729
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
 
8568
    
 
8569
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
1730
8570
                              item->name < subq_sj_cond_name + 64);
1731
8571
    /* Copy the found multiple equalities at the current level if needed */
1732
8572
    if (left_copyfl)
1747
8587
    }
1748
8588
 
1749
8589
    if (left_item_equal)
1750
 
    {
 
8590
    { 
1751
8591
      /* left item was found in the current or one of the upper levels */
1752
8592
      if (! right_item_equal)
1753
8593
        left_item_equal->add((Item_field *) right_item);
1762
8602
      }
1763
8603
    }
1764
8604
    else
1765
 
    {
 
8605
    { 
1766
8606
      /* left item was not found neither the current nor in upper levels  */
1767
8607
      if (right_item_equal)
1768
8608
      {
1770
8610
        if (copy_item_name)
1771
8611
          right_item_equal->name = item->name;
1772
8612
      }
1773
 
      else
 
8613
      else 
1774
8614
      {
1775
8615
        /* None of the fields was found in multiple equalities */
1776
8616
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
1818
8658
          eq_item->set_cmp_func();
1819
8659
          eq_item->quick_fix_field();
1820
8660
          item= eq_item;
1821
 
        }
 
8661
        }  
1822
8662
        if ((cs != ((Item_func *) item)->compare_collation()) ||
1823
8663
            !cs->coll->propagate(cs, 0, 0))
1824
8664
          return false;
1833
8673
      }
1834
8674
      if (item_equal)
1835
8675
      {
1836
 
        /*
 
8676
        /* 
1837
8677
          The flag cond_false will be set to 1 after this, if item_equal
1838
8678
          already contains a constant and its value is  not equal to
1839
8679
          the value of const_item.
1851
8691
  return false;
1852
8692
}
1853
8693
 
 
8694
 
1854
8695
/**
1855
8696
  Convert row equalities into a conjunction of regular equalities.
1856
8697
 
1863
8704
    simple equality nor a row equality the item for this predicate is added
1864
8705
    to eq_list.
1865
8706
 
1866
 
  @param session        thread handle
 
8707
  @param thd        thread handle
1867
8708
  @param left_row   left term of the row equality to be processed
1868
8709
  @param right_row  right term of the row equality to be processed
1869
8710
  @param cond_equal multiple equalities that must hold together with the
1876
8717
  @retval
1877
8718
    false   otherwise
1878
8719
*/
1879
 
static bool check_row_equality(Session *session,
1880
 
                               Item *left_row, 
1881
 
                               Item_row *right_row,
1882
 
                               COND_EQUAL *cond_equal,
1883
 
                               List<Item>* eq_list)
1884
 
{
1885
 
  uint32_t n= left_row->cols();
1886
 
  for (uint32_t i= 0 ; i < n; i++)
 
8720
 
 
8721
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
 
8722
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
8723
 
8724
  uint n= left_row->cols();
 
8725
  for (uint i= 0 ; i < n; i++)
1887
8726
  {
1888
8727
    bool is_converted;
1889
8728
    Item *left_item= left_row->element_index(i);
1891
8730
    if (left_item->type() == Item::ROW_ITEM &&
1892
8731
        right_item->type() == Item::ROW_ITEM)
1893
8732
    {
1894
 
      is_converted= check_row_equality(session,
 
8733
      is_converted= check_row_equality(thd, 
1895
8734
                                       (Item_row *) left_item,
1896
8735
                                       (Item_row *) right_item,
1897
8736
                                       cond_equal, eq_list);
1898
8737
      if (!is_converted)
1899
 
        session->lex->current_select->cond_count++;
 
8738
        thd->lex->current_select->cond_count++;      
1900
8739
    }
1901
8740
    else
1902
 
    {
 
8741
    { 
1903
8742
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1904
 
      session->lex->current_select->cond_count++;
1905
 
    }
1906
 
 
 
8743
      thd->lex->current_select->cond_count++;
 
8744
    }  
 
8745
 
1907
8746
    if (!is_converted)
1908
8747
    {
1909
8748
      Item_func_eq *eq_item;
1917
8756
  return true;
1918
8757
}
1919
8758
 
 
8759
 
1920
8760
/**
1921
8761
  Eliminate row equalities and form multiple equalities predicates.
1922
8762
 
1931
8771
    equalities which are treated in the same way as original equality
1932
8772
    predicates.
1933
8773
 
1934
 
  @param session        thread handle
 
8774
  @param thd        thread handle
1935
8775
  @param item       predicate to process
1936
8776
  @param cond_equal multiple equalities that must hold together with the
1937
8777
                    predicate
1946
8786
           or, if the equality is neither a simple one nor a row equality,
1947
8787
           or, if the procedure fails by a fatal error.
1948
8788
*/
1949
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
 
8789
 
 
8790
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
 
8791
                           List<Item> *eq_list)
1950
8792
{
1951
8793
  if (item->type() == Item::FUNC_ITEM &&
1952
8794
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
1957
8799
    if (left_item->type() == Item::ROW_ITEM &&
1958
8800
        right_item->type() == Item::ROW_ITEM)
1959
8801
    {
1960
 
      session->lex->current_select->cond_count--;
1961
 
      return check_row_equality(session,
 
8802
      thd->lex->current_select->cond_count--;
 
8803
      return check_row_equality(thd,
1962
8804
                                (Item_row *) left_item,
1963
8805
                                (Item_row *) right_item,
1964
8806
                                cond_equal, eq_list);
1965
8807
    }
1966
 
    else
 
8808
    else 
1967
8809
      return check_simple_equality(left_item, right_item, item, cond_equal);
1968
 
  }
 
8810
  } 
1969
8811
  return false;
1970
8812
}
1971
8813
 
 
8814
                          
1972
8815
/**
1973
8816
  Replace all equality predicates in a condition by multiple equality items.
1974
8817
 
1975
8818
    At each 'and' level the function detects items for equality predicates
1976
8819
    and replaced them by a set of multiple equality items of class Item_equal,
1977
 
    taking into account inherited equalities from upper levels.
 
8820
    taking into account inherited equalities from upper levels. 
1978
8821
    If an equality predicate is used not in a conjunction it's just
1979
8822
    replaced by a multiple equality predicate.
1980
8823
    For each 'and' level the function set a pointer to the inherited
1981
8824
    multiple equalities in the cond_equal field of the associated
1982
 
    object of the type Item_cond_and.
 
8825
    object of the type Item_cond_and.   
1983
8826
    The function also traverses the cond tree and and for each field reference
1984
8827
    sets a pointer to the multiple equality item containing the field, if there
1985
8828
    is any. If this multiple equality equates fields to a constant the
1986
 
    function replaces the field reference by the constant in the cases
 
8829
    function replaces the field reference by the constant in the cases 
1987
8830
    when the field is not of a string type or when the field reference is
1988
8831
    just an argument of a comparison predicate.
1989
 
    The function also determines the maximum number of members in
 
8832
    The function also determines the maximum number of members in 
1990
8833
    equality lists of each Item_cond_and object assigning it to
1991
 
    session->lex->current_select->max_equal_elems.
 
8834
    thd->lex->current_select->max_equal_elems.
1992
8835
 
1993
8836
  @note
1994
8837
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
2000
8843
    in a conjuction for a minimal set of multiple equality predicates.
2001
8844
    This set can be considered as a canonical representation of the
2002
8845
    sub-conjunction of the equality predicates.
2003
 
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
 
8846
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
2004
8847
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
2005
8848
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
2006
8849
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
2011
8854
    The function performs the substitution in a recursive descent by
2012
8855
    the condtion tree, passing to the next AND level a chain of multiple
2013
8856
    equality predicates which have been built at the upper levels.
2014
 
    The Item_equal items built at the level are attached to other
 
8857
    The Item_equal items built at the level are attached to other 
2015
8858
    non-equality conjucts as a sublist. The pointer to the inherited
2016
8859
    multiple equalities is saved in the and condition object (Item_cond_and).
2017
 
    This chain allows us for any field reference occurence easyly to find a
 
8860
    This chain allows us for any field reference occurence easyly to find a 
2018
8861
    multiple equality that must be held for this occurence.
2019
8862
    For each AND level we do the following:
2020
8863
    - scan it for all equality predicate (=) items
2021
8864
    - join them into disjoint Item_equal() groups
2022
 
    - process the included OR conditions recursively to do the same for
2023
 
      lower AND levels.
 
8865
    - process the included OR conditions recursively to do the same for 
 
8866
      lower AND levels. 
2024
8867
 
2025
8868
    We need to do things in this order as lower AND levels need to know about
2026
8869
    all possible Item_equal objects in upper levels.
2027
8870
 
2028
 
  @param session        thread handle
 
8871
  @param thd        thread handle
2029
8872
  @param cond       condition(expression) where to make replacement
2030
8873
  @param inherited  path to all inherited multiple equality items
2031
8874
 
2032
8875
  @return
2033
8876
    pointer to the transformed condition
2034
8877
*/
2035
 
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
 
8878
 
 
8879
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
 
8880
                                        COND_EQUAL *inherited)
2036
8881
{
2037
8882
  Item_equal *item_equal;
2038
8883
  COND_EQUAL cond_equal;
2044
8889
    bool and_level= ((Item_cond*) cond)->functype() ==
2045
8890
      Item_func::COND_AND_FUNC;
2046
8891
    List<Item> *args= ((Item_cond*) cond)->argument_list();
2047
 
 
 
8892
    
2048
8893
    List_iterator<Item> li(*args);
2049
8894
    Item *item;
2050
8895
 
2053
8898
      /*
2054
8899
         Retrieve all conjucts of this level detecting the equality
2055
8900
         that are subject to substitution by multiple equality items and
2056
 
         removing each such predicate from the conjunction after having
 
8901
         removing each such predicate from the conjunction after having 
2057
8902
         found/created a multiple equality whose inference the predicate is.
2058
 
     */
 
8903
     */      
2059
8904
      while ((item= li++))
2060
8905
      {
2061
8906
        /*
2063
8908
          structure here because it's restored before each
2064
8909
          re-execution of any prepared statement/stored procedure.
2065
8910
        */
2066
 
        if (check_equality(session, item, &cond_equal, &eq_list))
 
8911
        if (check_equality(thd, item, &cond_equal, &eq_list))
2067
8912
          li.remove();
2068
8913
      }
2069
8914
 
2072
8917
      {
2073
8918
        item_equal->fix_length_and_dec();
2074
8919
        item_equal->update_used_tables();
2075
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
2076
 
                      item_equal->members());
 
8920
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8921
                      item_equal->members());  
2077
8922
      }
2078
8923
 
2079
8924
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
2085
8930
    */
2086
8931
    li.rewind();
2087
8932
    while ((item= li++))
2088
 
    {
 
8933
    { 
2089
8934
      Item *new_item;
2090
 
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
 
8935
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
2091
8936
      {
2092
8937
        /* This replacement happens only for standalone equalities */
2093
8938
        /*
2115
8960
      (b=5) and (a=c) are standalone equalities.
2116
8961
      In general we can't leave alone standalone eqalities:
2117
8962
      for WHERE a=b AND c=d AND (b=c OR d=5)
2118
 
      b=c is replaced by =(a,b,c,d).
 
8963
      b=c is replaced by =(a,b,c,d).  
2119
8964
     */
2120
 
    if (check_equality(session, cond, &cond_equal, &eq_list))
 
8965
    if (check_equality(thd, cond, &cond_equal, &eq_list))
2121
8966
    {
2122
8967
      int n= cond_equal.current_level.elements + eq_list.elements;
2123
8968
      if (n == 0)
2128
8973
        {
2129
8974
          item_equal->fix_length_and_dec();
2130
8975
          item_equal->update_used_tables();
2131
 
        }
 
8976
        }
2132
8977
        else
2133
8978
          item_equal= (Item_equal *) eq_list.pop();
2134
 
        set_if_bigger(session->lex->current_select->max_equal_elems,
2135
 
                      item_equal->members());
 
8979
        set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8980
                      item_equal->members());  
2136
8981
        return item_equal;
2137
8982
      }
2138
8983
      else
2139
8984
      {
2140
 
        /*
 
8985
        /* 
2141
8986
          Here a new AND level must be created. It can happen only
2142
8987
          when a row equality is processed as a standalone predicate.
2143
 
        */
 
8988
        */
2144
8989
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
2145
8990
        and_cond->quick_fix_field();
2146
8991
        List<Item> *args= and_cond->argument_list();
2149
8994
        {
2150
8995
          item_equal->fix_length_and_dec();
2151
8996
          item_equal->update_used_tables();
2152
 
          set_if_bigger(session->lex->current_select->max_equal_elems,
2153
 
                        item_equal->members());
 
8997
          set_if_bigger(thd->lex->current_select->max_equal_elems,
 
8998
                        item_equal->members());  
2154
8999
        }
2155
9000
        and_cond->cond_equal= cond_equal;
2156
9001
        args->concat((List<Item> *)&cond_equal.current_level);
2157
 
 
 
9002
        
2158
9003
        return and_cond;
2159
9004
      }
2160
9005
    }
2161
 
    /*
 
9006
    /* 
2162
9007
      For each field reference in cond, not from equal item predicates,
2163
9008
      set a pointer to the multiple equality it belongs to (if there is any)
2164
9009
      as soon the field is not of a string type or the field reference is
2165
 
      an argument of a comparison predicate.
2166
 
    */
2167
 
    unsigned char *is_subst_valid= (unsigned char *) 1;
 
9010
      an argument of a comparison predicate. 
 
9011
    */ 
 
9012
    uchar *is_subst_valid= (uchar *) 1;
2168
9013
    cond= cond->compile(&Item::subst_argument_checker,
2169
 
                        &is_subst_valid,
 
9014
                        &is_subst_valid, 
2170
9015
                        &Item::equal_fields_propagator,
2171
 
                        (unsigned char *) inherited);
 
9016
                        (uchar *) inherited);
2172
9017
    cond->update_used_tables();
2173
9018
  }
2174
9019
  return cond;
2175
9020
}
2176
9021
 
 
9022
 
2177
9023
/**
2178
9024
  Build multiple equalities for a condition and all on expressions that
2179
9025
  inherit these multiple equalities.
2219
9065
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
2220
9066
        WHERE t1.a=t2.a
2221
9067
    @endcode
2222
 
    that is equivalent to:
 
9068
    that is equivalent to:   
2223
9069
    @code
2224
9070
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
2225
9071
        WHERE t1.a=t2.a
2226
9072
    @endcode
2227
9073
    Thus, applying equalities from the where condition we basically
2228
9074
    can get more freedom in performing join operations.
2229
 
    Althogh we don't use this property now, it probably makes sense to use
2230
 
    it in the future.
2231
 
  @param session                      Thread Cursor
 
9075
    Althogh we don't use this property now, it probably makes sense to use 
 
9076
    it in the future.    
 
9077
  @param thd                  Thread handler
2232
9078
  @param cond                condition to build the multiple equalities for
2233
9079
  @param inherited           path to all inherited multiple equality items
2234
9080
  @param join_list           list of join tables to which the condition
2239
9085
  @return
2240
9086
    pointer to the transformed condition containing multiple equalities
2241
9087
*/
2242
 
static COND *build_equal_items(Session *session, COND *cond,
 
9088
   
 
9089
static COND *build_equal_items(THD *thd, COND *cond,
2243
9090
                               COND_EQUAL *inherited,
2244
 
                               List<TableList> *join_list,
 
9091
                               List<TABLE_LIST> *join_list,
2245
9092
                               COND_EQUAL **cond_equal_ref)
2246
9093
{
2247
9094
  COND_EQUAL *cond_equal= 0;
2248
9095
 
2249
 
  if (cond)
 
9096
  if (cond) 
2250
9097
  {
2251
 
    cond= build_equal_items_for_cond(session, cond, inherited);
 
9098
    cond= build_equal_items_for_cond(thd, cond, inherited);
2252
9099
    cond->update_used_tables();
2253
9100
    if (cond->type() == Item::COND_ITEM &&
2254
9101
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2269
9116
 
2270
9117
  if (join_list)
2271
9118
  {
2272
 
    TableList *table;
2273
 
    List_iterator<TableList> li(*join_list);
 
9119
    TABLE_LIST *table;
 
9120
    List_iterator<TABLE_LIST> li(*join_list);
2274
9121
 
2275
9122
    while ((table= li++))
2276
9123
    {
2277
9124
      if (table->on_expr)
2278
9125
      {
2279
 
        List<TableList> *nested_join_list= table->nested_join ?
 
9126
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
2280
9127
          &table->nested_join->join_list : NULL;
2281
9128
        /*
2282
9129
          We can modify table->on_expr because its old value will
2283
9130
          be restored before re-execution of PS/SP.
2284
9131
        */
2285
 
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
 
9132
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
2286
9133
                                          nested_join_list,
2287
9134
                                          &table->cond_equal);
2288
9135
      }
2290
9137
  }
2291
9138
 
2292
9139
  return cond;
2293
 
}
 
9140
}    
 
9141
 
2294
9142
 
2295
9143
/**
2296
9144
  Compare field items by table order in the execution plan.
2297
9145
 
2298
9146
    field1 considered as better than field2 if the table containing
2299
 
    field1 is accessed earlier than the table containing field2.
 
9147
    field1 is accessed earlier than the table containing field2.   
2300
9148
    The function finds out what of two fields is better according
2301
9149
    this criteria.
2302
9150
 
2311
9159
  @retval
2312
9160
    0  otherwise
2313
9161
*/
 
9162
 
2314
9163
static int compare_fields_by_table_order(Item_field *field1,
2315
 
                                         Item_field *field2,
2316
 
                                         void *table_join_idx)
 
9164
                                  Item_field *field2,
 
9165
                                  void *table_join_idx)
2317
9166
{
2318
9167
  int cmp= 0;
2319
9168
  bool outer_ref= 0;
2320
9169
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2321
 
  {
 
9170
  {  
2322
9171
    outer_ref= 1;
2323
9172
    cmp= -1;
2324
9173
  }
2329
9178
  }
2330
9179
  if (outer_ref)
2331
9180
    return cmp;
2332
 
  JoinTable **idx= (JoinTable **) table_join_idx;
 
9181
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
2333
9182
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2334
9183
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2335
9184
}
2336
9185
 
 
9186
 
2337
9187
/**
2338
9188
  Generate minimal set of simple equalities equivalent to a multiple equality.
2339
9189
 
2373
9223
    a pointer to the simple generated equality, if success.
2374
9224
    - 0, otherwise.
2375
9225
*/
2376
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
 
9226
 
 
9227
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
 
9228
                                  Item_equal *item_equal)
2377
9229
{
2378
9230
  List<Item> eq_list;
2379
9231
  Item_func_eq *eq_item= 0;
2380
9232
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
2381
 
    return new Item_int((int64_t) 0,1);
 
9233
    return new Item_int((int64_t) 0,1); 
2382
9234
  Item *item_const= item_equal->get_const();
2383
9235
  Item_equal_iterator it(*item_equal);
2384
9236
  Item *head;
2395
9247
    Item_equal *upper= item_field->find_item_equal(upper_levels);
2396
9248
    Item_field *item= item_field;
2397
9249
    if (upper)
2398
 
    {
 
9250
    { 
2399
9251
      if (item_const && upper->get_const())
2400
9252
        item= 0;
2401
9253
      else
2439
9291
 
2440
9292
  cond->quick_fix_field();
2441
9293
  cond->update_used_tables();
2442
 
 
 
9294
   
2443
9295
  return cond;
2444
9296
}
2445
9297
 
 
9298
 
2446
9299
/**
2447
9300
  Substitute every field reference in a condition by the best equal field
2448
9301
  and eliminate all multiple equality predicates.
2451
9304
    multiple equality predicate it sorts the field references in it
2452
9305
    according to the order of tables specified by the table_join_idx
2453
9306
    parameter. Then it eliminates the multiple equality predicate it
2454
 
    replacing it by the conjunction of simple equality predicates
 
9307
    replacing it by the conjunction of simple equality predicates 
2455
9308
    equating every field from the multiple equality to the first
2456
9309
    field in it, or to the constant, if there is any.
2457
9310
    After this the function retrieves all other conjuncted
2470
9323
  @return
2471
9324
    The transformed condition
2472
9325
*/
2473
 
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
 
9326
 
 
9327
static COND* substitute_for_best_equal_field(COND *cond,
 
9328
                                             COND_EQUAL *cond_equal,
 
9329
                                             void *table_join_idx)
2474
9330
{
2475
9331
  Item_equal *item_equal;
2476
9332
 
2485
9341
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2486
9342
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2487
9343
 
2488
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
 
9344
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
2489
9345
      while ((item_equal= it++))
2490
9346
      {
2491
9347
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2492
9348
      }
2493
9349
    }
2494
 
 
 
9350
    
2495
9351
    List_iterator<Item> li(*cond_list);
2496
9352
    Item *item;
2497
9353
    while ((item= li++))
2524
9380
      cond= new Item_int((int32_t)cond->val_bool());
2525
9381
 
2526
9382
  }
2527
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9383
  else if (cond->type() == Item::FUNC_ITEM && 
2528
9384
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2529
9385
  {
2530
9386
    item_equal= (Item_equal *) cond;
2538
9394
  return cond;
2539
9395
}
2540
9396
 
 
9397
 
2541
9398
/**
2542
9399
  Check appearance of new constant items in multiple equalities
2543
9400
  of a condition after reading a constant table.
2550
9407
  @param cond       condition whose multiple equalities are to be checked
2551
9408
  @param table      constant table that has been read
2552
9409
*/
2553
 
static void update_const_equal_items(COND *cond, JoinTable *tab)
 
9410
 
 
9411
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
2554
9412
{
2555
9413
  if (!(cond->used_tables() & tab->table->map))
2556
9414
    return;
2557
9415
 
2558
9416
  if (cond->type() == Item::COND_ITEM)
2559
9417
  {
2560
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
 
9418
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
2561
9419
    List_iterator_fast<Item> li(*cond_list);
2562
9420
    Item *item;
2563
9421
    while ((item= li++))
2564
9422
      update_const_equal_items(item, tab);
2565
9423
  }
2566
 
  else if (cond->type() == Item::FUNC_ITEM &&
 
9424
  else if (cond->type() == Item::FUNC_ITEM && 
2567
9425
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2568
9426
  {
2569
9427
    Item_equal *item_equal= (Item_equal *) cond;
2577
9435
      while ((item_field= it++))
2578
9436
      {
2579
9437
        Field *field= item_field->field;
2580
 
        JoinTable *stat= field->table->reginfo.join_tab;
 
9438
        JOIN_TAB *stat= field->table->reginfo.join_tab;
2581
9439
        key_map possible_keys= field->key_start;
2582
 
        possible_keys&= field->table->keys_in_use_for_query;
2583
 
        stat[0].const_keys|= possible_keys;
 
9440
        possible_keys.intersect(field->table->keys_in_use_for_query);
 
9441
        stat[0].const_keys.merge(possible_keys);
2584
9442
 
2585
9443
        /*
2586
 
          For each field in the multiple equality (for which we know that it
2587
 
          is a constant) we have to find its corresponding key part, and set
 
9444
          For each field in the multiple equality (for which we know that it 
 
9445
          is a constant) we have to find its corresponding key part, and set 
2588
9446
          that key part in const_key_parts.
2589
 
        */
2590
 
        if (possible_keys.any())
 
9447
        */  
 
9448
        if (!possible_keys.is_clear_all())
2591
9449
        {
2592
 
          Table *field_tab= field->table;
2593
 
          optimizer::KeyUse *use;
2594
 
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2595
 
            if (possible_keys.test(use->getKey()) &&
2596
 
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
 
9450
          TABLE *tab= field->table;
 
9451
          KEYUSE *use;
 
9452
          for (use= stat->keyuse; use && use->table == tab; use++)
 
9453
            if (possible_keys.is_set(use->key) && 
 
9454
                tab->key_info[use->key].key_part[use->keypart].field ==
2597
9455
                field)
2598
 
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
9456
              tab->const_key_parts[use->key]|= use->keypart_map;
2599
9457
        }
2600
9458
      }
2601
9459
    }
2602
9460
  }
2603
9461
}
2604
9462
 
 
9463
 
2605
9464
/*
2606
9465
  change field = field to field = const for each found field = const in the
2607
9466
  and_level
2608
9467
*/
2609
 
static void change_cond_ref_to_const(Session *session,
2610
 
                                     vector<COND_CMP>& save_list,
2611
 
                                     Item *and_father,
2612
 
                                     Item *cond,
2613
 
                                     Item *field,
2614
 
                                     Item *value)
 
9468
 
 
9469
static void
 
9470
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
 
9471
                         Item *and_father, Item *cond,
 
9472
                         Item *field, Item *value)
2615
9473
{
2616
9474
  if (cond->type() == Item::COND_ITEM)
2617
9475
  {
2618
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9476
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9477
      Item_func::COND_AND_FUNC;
2619
9478
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2620
9479
    Item *item;
2621
9480
    while ((item=li++))
2622
 
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
 
9481
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
 
9482
                               field, value);
2623
9483
    return;
2624
9484
  }
2625
9485
  if (cond->eq_cmp_result() == Item::COND_OK)
2639
9499
  {
2640
9500
    Item *tmp=value->clone_item();
2641
9501
    tmp->collation.set(right_item->collation);
2642
 
 
 
9502
    
2643
9503
    if (tmp)
2644
9504
    {
2645
 
      session->change_item_tree(args + 1, tmp);
 
9505
      thd->change_item_tree(args + 1, tmp);
2646
9506
      func->update_used_tables();
2647
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2648
 
                and_father != cond && 
2649
 
          ! left_item->const_item())
 
9507
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9508
          && and_father != cond && !left_item->const_item())
2650
9509
      {
2651
 
        cond->marker=1;
2652
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9510
        cond->marker=1;
 
9511
        COND_CMP *tmp2;
 
9512
        if ((tmp2=new COND_CMP(and_father,func)))
 
9513
          save_list->push_back(tmp2);
2653
9514
      }
2654
9515
      func->set_cmp_func();
2655
9516
    }
2662
9523
  {
2663
9524
    Item *tmp= value->clone_item();
2664
9525
    tmp->collation.set(left_item->collation);
2665
 
 
 
9526
    
2666
9527
    if (tmp)
2667
9528
    {
2668
 
      session->change_item_tree(args, tmp);
 
9529
      thd->change_item_tree(args, tmp);
2669
9530
      value= tmp;
2670
9531
      func->update_used_tables();
2671
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2672
 
          and_father != cond && 
2673
 
          ! right_item->const_item())
 
9532
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
 
9533
          && and_father != cond && !right_item->const_item())
2674
9534
      {
2675
9535
        args[0]= args[1];                       // For easy check
2676
 
        session->change_item_tree(args + 1, value);
2677
 
        cond->marker=1;
2678
 
        save_list.push_back( COND_CMP(and_father, func) );
 
9536
        thd->change_item_tree(args + 1, value);
 
9537
        cond->marker=1;
 
9538
        COND_CMP *tmp2;
 
9539
        if ((tmp2=new COND_CMP(and_father,func)))
 
9540
          save_list->push_back(tmp2);
2679
9541
      }
2680
9542
      func->set_cmp_func();
2681
9543
    }
2690
9552
  @return
2691
9553
    new conditions
2692
9554
*/
2693
 
Item *remove_additional_cond(Item* conds)
 
9555
 
 
9556
static Item *remove_additional_cond(Item* conds)
2694
9557
{
2695
9558
  if (conds->name == in_additional_cond)
2696
9559
    return 0;
2713
9576
  return conds;
2714
9577
}
2715
9578
 
2716
 
static void propagate_cond_constants(Session *session, 
2717
 
                                     vector<COND_CMP>& save_list, 
2718
 
                                     COND *and_father, 
2719
 
                                     COND *cond)
 
9579
static void
 
9580
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
 
9581
                         COND *and_father, COND *cond)
2720
9582
{
2721
9583
  if (cond->type() == Item::COND_ITEM)
2722
9584
  {
2723
 
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
 
9585
    bool and_level= ((Item_cond*) cond)->functype() ==
 
9586
      Item_func::COND_AND_FUNC;
2724
9587
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2725
9588
    Item *item;
2726
 
    vector<COND_CMP> save;
 
9589
    I_List<COND_CMP> save;
2727
9590
    while ((item=li++))
2728
9591
    {
2729
 
      propagate_cond_constants(session, save, and_level ? cond : item, item);
 
9592
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
2730
9593
    }
2731
9594
    if (and_level)
2732
 
    {
2733
 
      // Handle other found items
2734
 
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
9595
    {                                           // Handle other found items
 
9596
      I_List_iterator<COND_CMP> cond_itr(save);
 
9597
      COND_CMP *cond_cmp;
 
9598
      while ((cond_cmp=cond_itr++))
2735
9599
      {
2736
 
        Item **args= iter->cmp_func->arguments();
 
9600
        Item **args= cond_cmp->cmp_func->arguments();
2737
9601
        if (!args[0]->const_item())
2738
 
        {
2739
 
          change_cond_ref_to_const( session, save, iter->and_level,
2740
 
                                    iter->and_level, args[0], args[1] );
2741
 
        }
 
9602
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
 
9603
                                   cond_cmp->and_level, args[0], args[1]);
2742
9604
      }
2743
9605
    }
2744
9606
  }
2745
9607
  else if (and_father != cond && !cond->marker)         // In a AND group
2746
9608
  {
2747
9609
    if (cond->type() == Item::FUNC_ITEM &&
2748
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2749
 
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
9610
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
9611
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2750
9612
    {
2751
9613
      Item_func_eq *func=(Item_func_eq*) cond;
2752
9614
      Item **args= func->arguments();
2755
9617
      if (!(left_const && right_const) &&
2756
9618
          args[0]->result_type() == args[1]->result_type())
2757
9619
      {
2758
 
        if (right_const)
2759
 
        {
2760
 
                resolve_const_item(session, &args[1], args[0]);
2761
 
          func->update_used_tables();
2762
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2763
 
                                        args[0], args[1]);
2764
 
        }
2765
 
        else if (left_const)
2766
 
        {
2767
 
                resolve_const_item(session, &args[0], args[1]);
2768
 
          func->update_used_tables();
2769
 
                change_cond_ref_to_const(session, save_list, and_father, and_father,
2770
 
                                        args[1], args[0]);
2771
 
        }
2772
 
      }
2773
 
    }
2774
 
  }
2775
 
}
 
9620
        if (right_const)
 
9621
        {
 
9622
          resolve_const_item(thd, &args[1], args[0]);
 
9623
          func->update_used_tables();
 
9624
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9625
                                   args[0], args[1]);
 
9626
        }
 
9627
        else if (left_const)
 
9628
        {
 
9629
          resolve_const_item(thd, &args[0], args[1]);
 
9630
          func->update_used_tables();
 
9631
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
 
9632
                                   args[1], args[0]);
 
9633
        }
 
9634
      }
 
9635
    }
 
9636
  }
 
9637
}
 
9638
 
 
9639
 
 
9640
/**
 
9641
  Simplify joins replacing outer joins by inner joins whenever it's
 
9642
  possible.
 
9643
 
 
9644
    The function, during a retrieval of join_list,  eliminates those
 
9645
    outer joins that can be converted into inner join, possibly nested.
 
9646
    It also moves the on expressions for the converted outer joins
 
9647
    and from inner joins to conds.
 
9648
    The function also calculates some attributes for nested joins:
 
9649
    - used_tables    
 
9650
    - not_null_tables
 
9651
    - dep_tables.
 
9652
    - on_expr_dep_tables
 
9653
    The first two attributes are used to test whether an outer join can
 
9654
    be substituted for an inner join. The third attribute represents the
 
9655
    relation 'to be dependent on' for tables. If table t2 is dependent
 
9656
    on table t1, then in any evaluated execution plan table access to
 
9657
    table t2 must precede access to table t2. This relation is used also
 
9658
    to check whether the query contains  invalid cross-references.
 
9659
    The forth attribute is an auxiliary one and is used to calculate
 
9660
    dep_tables.
 
9661
    As the attribute dep_tables qualifies possibles orders of tables in the
 
9662
    execution plan, the dependencies required by the straight join
 
9663
    modifiers are reflected in this attribute as well.
 
9664
    The function also removes all braces that can be removed from the join
 
9665
    expression without changing its meaning.
 
9666
 
 
9667
  @note
 
9668
    An outer join can be replaced by an inner join if the where condition
 
9669
    or the on expression for an embedding nested join contains a conjunctive
 
9670
    predicate rejecting null values for some attribute of the inner tables.
 
9671
 
 
9672
    E.g. in the query:    
 
9673
    @code
 
9674
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9675
    @endcode
 
9676
    the predicate t2.b < 5 rejects nulls.
 
9677
    The query is converted first to:
 
9678
    @code
 
9679
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
9680
    @endcode
 
9681
    then to the equivalent form:
 
9682
    @code
 
9683
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
9684
    @endcode
 
9685
 
 
9686
 
 
9687
    Similarly the following query:
 
9688
    @code
 
9689
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
9690
        WHERE t2.c < 5  
 
9691
    @endcode
 
9692
    is converted to:
 
9693
    @code
 
9694
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
 
9695
 
 
9696
    @endcode
 
9697
 
 
9698
    One conversion might trigger another:
 
9699
    @code
 
9700
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
9701
                       LEFT JOIN t3 ON t3.b=t2.b
 
9702
        WHERE t3 IS NOT NULL =>
 
9703
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
9704
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
 
9705
      SELECT * FROM t1, t2, t3
 
9706
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
9707
  @endcode
 
9708
 
 
9709
    The function removes all unnecessary braces from the expression
 
9710
    produced by the conversions.
 
9711
    E.g.
 
9712
    @code
 
9713
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9714
    @endcode
 
9715
    finally is converted to: 
 
9716
    @code
 
9717
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
9718
 
 
9719
    @endcode
 
9720
 
 
9721
 
 
9722
    It also will remove braces from the following queries:
 
9723
    @code
 
9724
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
9725
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
9726
    @endcode
 
9727
 
 
9728
    The benefit of this simplification procedure is that it might return 
 
9729
    a query for which the optimizer can evaluate execution plan with more
 
9730
    join orders. With a left join operation the optimizer does not
 
9731
    consider any plan where one of the inner tables is before some of outer
 
9732
    tables.
 
9733
 
 
9734
  IMPLEMENTATION
 
9735
    The function is implemented by a recursive procedure.  On the recursive
 
9736
    ascent all attributes are calculated, all outer joins that can be
 
9737
    converted are replaced and then all unnecessary braces are removed.
 
9738
    As join list contains join tables in the reverse order sequential
 
9739
    elimination of outer joins does not require extra recursive calls.
 
9740
 
 
9741
  SEMI-JOIN NOTES
 
9742
    Remove all semi-joins that have are within another semi-join (i.e. have
 
9743
    an "ancestor" semi-join nest)
 
9744
 
 
9745
  EXAMPLES
 
9746
    Here is an example of a join query with invalid cross references:
 
9747
    @code
 
9748
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
 
9749
    @endcode
 
9750
 
 
9751
  @param join        reference to the query info
 
9752
  @param join_list   list representation of the join to be converted
 
9753
  @param conds       conditions to add on expressions for converted joins
 
9754
  @param top         true <=> conds is the where condition
 
9755
 
 
9756
  @return
 
9757
    - The new condition, if success
 
9758
    - 0, otherwise
 
9759
*/
 
9760
 
 
9761
static COND *
 
9762
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
 
9763
               bool in_sj)
 
9764
{
 
9765
  TABLE_LIST *table;
 
9766
  NESTED_JOIN *nested_join;
 
9767
  TABLE_LIST *prev_table= 0;
 
9768
  List_iterator<TABLE_LIST> li(*join_list);
 
9769
 
 
9770
  /* 
 
9771
    Try to simplify join operations from join_list.
 
9772
    The most outer join operation is checked for conversion first. 
 
9773
  */
 
9774
  while ((table= li++))
 
9775
  {
 
9776
    table_map used_tables;
 
9777
    table_map not_null_tables= (table_map) 0;
 
9778
 
 
9779
    if ((nested_join= table->nested_join))
 
9780
    {
 
9781
      /* 
 
9782
         If the element of join_list is a nested join apply
 
9783
         the procedure to its nested join list first.
 
9784
      */
 
9785
      if (table->on_expr)
 
9786
      {
 
9787
        Item *expr= table->on_expr;
 
9788
        /* 
 
9789
           If an on expression E is attached to the table, 
 
9790
           check all null rejected predicates in this expression.
 
9791
           If such a predicate over an attribute belonging to
 
9792
           an inner table of an embedded outer join is found,
 
9793
           the outer join is converted to an inner join and
 
9794
           the corresponding on expression is added to E. 
 
9795
        */ 
 
9796
        expr= simplify_joins(join, &nested_join->join_list,
 
9797
                             expr, false, in_sj || table->sj_on_expr);
 
9798
 
 
9799
        if (!table->prep_on_expr || expr != table->on_expr)
 
9800
        {
 
9801
          assert(expr);
 
9802
 
 
9803
          table->on_expr= expr;
 
9804
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
 
9805
        }
 
9806
      }
 
9807
      nested_join->used_tables= (table_map) 0;
 
9808
      nested_join->not_null_tables=(table_map) 0;
 
9809
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
 
9810
                            in_sj || table->sj_on_expr);
 
9811
      used_tables= nested_join->used_tables;
 
9812
      not_null_tables= nested_join->not_null_tables;  
 
9813
    }
 
9814
    else
 
9815
    {
 
9816
      if (!table->prep_on_expr)
 
9817
        table->prep_on_expr= table->on_expr;
 
9818
      used_tables= table->table->map;
 
9819
      if (conds)
 
9820
        not_null_tables= conds->not_null_tables();
 
9821
    }
 
9822
      
 
9823
    if (table->embedding)
 
9824
    {
 
9825
      table->embedding->nested_join->used_tables|= used_tables;
 
9826
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
9827
    }
 
9828
 
 
9829
    if (!table->outer_join || (used_tables & not_null_tables))
 
9830
    {
 
9831
      /* 
 
9832
        For some of the inner tables there are conjunctive predicates
 
9833
        that reject nulls => the outer join can be replaced by an inner join.
 
9834
      */
 
9835
      table->outer_join= 0;
 
9836
      if (table->on_expr)
 
9837
      {
 
9838
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
9839
        if (conds)
 
9840
        {
 
9841
          conds= and_conds(conds, table->on_expr);
 
9842
          conds->top_level_item();
 
9843
          /* conds is always a new item as both cond and on_expr existed */
 
9844
          assert(!conds->fixed);
 
9845
          conds->fix_fields(join->thd, &conds);
 
9846
        }
 
9847
        else
 
9848
          conds= table->on_expr; 
 
9849
        table->prep_on_expr= table->on_expr= 0;
 
9850
      }
 
9851
    }
 
9852
    
 
9853
    if (!top)
 
9854
      continue;
 
9855
 
 
9856
    /* 
 
9857
      Only inner tables of non-convertible outer joins
 
9858
      remain with on_expr.
 
9859
    */ 
 
9860
    if (table->on_expr)
 
9861
    {
 
9862
      table->dep_tables|= table->on_expr->used_tables(); 
 
9863
      if (table->embedding)
 
9864
      {
 
9865
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
 
9866
        /*
 
9867
           Embedding table depends on tables used
 
9868
           in embedded on expressions. 
 
9869
        */
 
9870
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
9871
      }
 
9872
      else
 
9873
        table->dep_tables&= ~table->table->map;
 
9874
    }
 
9875
 
 
9876
    if (prev_table)
 
9877
    {
 
9878
      /* The order of tables is reverse: prev_table follows table */
 
9879
      if (prev_table->straight)
 
9880
        prev_table->dep_tables|= used_tables;
 
9881
      if (prev_table->on_expr)
 
9882
      {
 
9883
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
9884
        table_map prev_used_tables= prev_table->nested_join ?
 
9885
                                    prev_table->nested_join->used_tables :
 
9886
                                    prev_table->table->map;
 
9887
        /* 
 
9888
          If on expression contains only references to inner tables
 
9889
          we still make the inner tables dependent on the outer tables.
 
9890
          It would be enough to set dependency only on one outer table
 
9891
          for them. Yet this is really a rare case.
 
9892
        */  
 
9893
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
9894
          prev_table->dep_tables|= used_tables;
 
9895
      }
 
9896
    }
 
9897
    prev_table= table;
 
9898
  }
 
9899
    
 
9900
  /* 
 
9901
    Flatten nested joins that can be flattened.
 
9902
    no ON expression and not a semi-join => can be flattened.
 
9903
  */
 
9904
  li.rewind();
 
9905
  while ((table= li++))
 
9906
  {
 
9907
    nested_join= table->nested_join;
 
9908
    if (table->sj_on_expr && !in_sj)
 
9909
    {
 
9910
       /*
 
9911
         If this is a semi-join that is not contained within another semi-join, 
 
9912
         leave it intact (otherwise it is flattened)
 
9913
       */
 
9914
      join->select_lex->sj_nests.push_back(table);
 
9915
    }
 
9916
    else if (nested_join && !table->on_expr)
 
9917
    {
 
9918
      TABLE_LIST *tbl;
 
9919
      List_iterator<TABLE_LIST> it(nested_join->join_list);
 
9920
      while ((tbl= it++))
 
9921
      {
 
9922
        tbl->embedding= table->embedding;
 
9923
        tbl->join_list= table->join_list;
 
9924
      }      
 
9925
      li.replace(nested_join->join_list);
 
9926
    }
 
9927
  }
 
9928
  return(conds); 
 
9929
}
 
9930
 
 
9931
 
 
9932
/**
 
9933
  Assign each nested join structure a bit in nested_join_map.
 
9934
 
 
9935
    Assign each nested join structure (except "confluent" ones - those that
 
9936
    embed only one element) a bit in nested_join_map.
 
9937
 
 
9938
  @param join          Join being processed
 
9939
  @param join_list     List of tables
 
9940
  @param first_unused  Number of first unused bit in nested_join_map before the
 
9941
                       call
 
9942
 
 
9943
  @note
 
9944
    This function is called after simplify_joins(), when there are no
 
9945
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
9946
    we will not run out of bits in nested_join_map.
 
9947
 
 
9948
  @return
 
9949
    First unused bit in nested_join_map after the call.
 
9950
*/
 
9951
 
 
9952
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
 
9953
                                          uint first_unused)
 
9954
{
 
9955
  List_iterator<TABLE_LIST> li(*join_list);
 
9956
  TABLE_LIST *table;
 
9957
  while ((table= li++))
 
9958
  {
 
9959
    NESTED_JOIN *nested_join;
 
9960
    if ((nested_join= table->nested_join))
 
9961
    {
 
9962
      /*
 
9963
        It is guaranteed by simplify_joins() function that a nested join
 
9964
        that has only one child is either
 
9965
         - a single-table view (the child is the underlying table), or 
 
9966
         - a single-table semi-join nest
 
9967
 
 
9968
        We don't assign bits to such sj-nests because 
 
9969
        1. it is redundant (a "sequence" of one table cannot be interleaved 
 
9970
            with anything)
 
9971
        2. we could run out bits in nested_join_map otherwise.
 
9972
      */
 
9973
      if (nested_join->join_list.elements != 1)
 
9974
      {
 
9975
        /* Don't assign bits to sj-nests */
 
9976
        if (table->on_expr)
 
9977
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
9978
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
9979
                                                    first_unused);
 
9980
      }
 
9981
    }
 
9982
  }
 
9983
  return(first_unused);
 
9984
}
 
9985
 
 
9986
 
 
9987
/**
 
9988
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
 
9989
 
 
9990
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
 
9991
    the passed join_list.
 
9992
 
 
9993
  @param join_list  List of nested joins to process. It may also contain base
 
9994
                    tables which will be ignored.
 
9995
*/
 
9996
 
 
9997
static void reset_nj_counters(List<TABLE_LIST> *join_list)
 
9998
{
 
9999
  List_iterator<TABLE_LIST> li(*join_list);
 
10000
  TABLE_LIST *table;
 
10001
  while ((table= li++))
 
10002
  {
 
10003
    NESTED_JOIN *nested_join;
 
10004
    if ((nested_join= table->nested_join))
 
10005
    {
 
10006
      nested_join->counter_= 0;
 
10007
      reset_nj_counters(&nested_join->join_list);
 
10008
    }
 
10009
  }
 
10010
  return;
 
10011
}
 
10012
 
2776
10013
 
2777
10014
/**
2778
10015
  Check interleaving with an inner tables of an outer join for
2779
10016
  extension table.
2780
10017
 
2781
 
    Check if table next_tab can be added to current partial join order, and
 
10018
    Check if table next_tab can be added to current partial join order, and 
2782
10019
    if yes, record that it has been added.
2783
10020
 
2784
10021
    The function assumes that both current partial join order and its
2785
10022
    extension with next_tab are valid wrt table dependencies.
2786
10023
 
2787
10024
  @verbatim
2788
 
     IMPLEMENTATION
2789
 
       LIMITATIONS ON JOIN order_st
 
10025
     IMPLEMENTATION 
 
10026
       LIMITATIONS ON JOIN ORDER
2790
10027
         The nested [outer] joins executioner algorithm imposes these limitations
2791
10028
         on join order:
2792
 
         1. "Outer tables first" -  any "outer" table must be before any
 
10029
         1. "Outer tables first" -  any "outer" table must be before any 
2793
10030
             corresponding "inner" table.
2794
10031
         2. "No interleaving" - tables inside a nested join must form a continuous
2795
 
            sequence in join order (i.e. the sequence must not be interrupted by
 
10032
            sequence in join order (i.e. the sequence must not be interrupted by 
2796
10033
            tables that are outside of this nested join).
2797
10034
 
2798
10035
         #1 is checked elsewhere, this function checks #2 provided that #1 has
2799
10036
         been already checked.
2800
10037
 
2801
10038
       WHY NEED NON-INTERLEAVING
2802
 
         Consider an example:
 
10039
         Consider an example: 
2803
10040
 
2804
10041
           select * from t0 join t1 left join (t2 join t3) on cond1
2805
10042
 
2823
10060
         The limitations on join order can be rephrased as follows: for valid
2824
10061
         join order one must be able to:
2825
10062
           1. write down the used tables in the join order on one line.
2826
 
           2. for each nested join, put one '(' and one ')' on the said line
 
10063
           2. for each nested join, put one '(' and one ')' on the said line        
2827
10064
           3. write "LEFT JOIN" and "ON (...)" where appropriate
2828
10065
           4. get a query equivalent to the query we're trying to execute.
2829
10066
 
2830
10067
         Calls to check_interleaving_with_nj() are equivalent to writing the
2831
 
         above described line from left to right.
2832
 
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
 
10068
         above described line from left to right. 
 
10069
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
2833
10070
         table B and appropriate brackets on condition that table A and
2834
10071
         appropriate brackets is the last what was written. Graphically the
2835
10072
         transition is as follows:
2842
10079
                                                      position.
2843
10080
 
2844
10081
         Notes about the position:
2845
 
           The caller guarantees that there is no more then one X-bracket by
2846
 
           checking "!(remaining_tables & s->dependent)" before calling this
 
10082
           The caller guarantees that there is no more then one X-bracket by 
 
10083
           checking "!(remaining_tables & s->dependent)" before calling this 
2847
10084
           function. X-bracket may have a pair in Y-bracket.
2848
10085
 
2849
10086
         When "writing" we store/update this auxilary info about the current
2850
10087
         position:
2851
10088
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2852
10089
             joins) we've opened but didn't close.
2853
 
          2. {each nested_join_st structure not simplified away}->counter - number
 
10090
          2. {each NESTED_JOIN structure not simplified away}->counter - number
2854
10091
             of this nested join's children that have already been added to to
2855
10092
             the partial join order.
2856
10093
  @endverbatim
2866
10103
  @retval
2867
10104
    true   Requested join order extension not allowed.
2868
10105
*/
2869
 
bool check_interleaving_with_nj(JoinTable *last_tab, JoinTable *next_tab)
 
10106
 
 
10107
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
2870
10108
{
2871
 
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
10109
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
2872
10110
  JOIN *join= last_tab->join;
2873
10111
 
2874
 
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
 
10112
  if (join->cur_embedding_map & ~next_tab->embedding_map)
2875
10113
  {
2876
 
    /*
 
10114
    /* 
2877
10115
      next_tab is outside of the "pair of brackets" we're currently in.
2878
10116
      Cannot add it.
2879
10117
    */
2880
10118
    return true;
2881
10119
  }
2882
 
 
 
10120
   
2883
10121
  /*
2884
10122
    Do update counters for "pairs of brackets" that we've left (marked as
2885
10123
    X,Y,Z in the above picture)
2889
10127
    next_emb->nested_join->counter_++;
2890
10128
    if (next_emb->nested_join->counter_ == 1)
2891
10129
    {
2892
 
      /*
 
10130
      /* 
2893
10131
        next_emb is the first table inside a nested join we've "entered". In
2894
10132
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2895
10133
        X bracket might have Y pair bracket.
2896
10134
      */
2897
10135
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2898
10136
    }
2899
 
 
 
10137
    
2900
10138
    if (next_emb->nested_join->join_list.elements !=
2901
10139
        next_emb->nested_join->counter_)
2902
10140
      break;
2910
10148
  return false;
2911
10149
}
2912
10150
 
2913
 
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2914
 
{
2915
 
  Session *session= join->session;
 
10151
 
 
10152
/**
 
10153
  Nested joins perspective: Remove the last table from the join order.
 
10154
 
 
10155
    Remove the last table from the partial join order and update the nested
 
10156
    joins counters and join->cur_embedding_map. It is ok to call this 
 
10157
    function for the first table in join order (for which 
 
10158
    check_interleaving_with_nj has not been called)
 
10159
 
 
10160
  @param last  join table to remove, it is assumed to be the last in current
 
10161
               partial join order.
 
10162
*/
 
10163
 
 
10164
static void restore_prev_nj_state(JOIN_TAB *last)
 
10165
{
 
10166
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
 
10167
  JOIN *join= last->join;
 
10168
  while (last_emb)
 
10169
  {
 
10170
    if (last_emb->on_expr)
 
10171
    {
 
10172
      if (!(--last_emb->nested_join->counter_))
 
10173
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
10174
      else if (last_emb->nested_join->join_list.elements-1 ==
 
10175
               last_emb->nested_join->counter_) 
 
10176
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
10177
      else
 
10178
        break;
 
10179
    }
 
10180
    last_emb= last_emb->embedding;
 
10181
  }
 
10182
}
 
10183
 
 
10184
 
 
10185
 
 
10186
static 
 
10187
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
10188
{
 
10189
  TABLE_LIST *emb_sj_nest;
 
10190
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10191
  {
 
10192
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
 
10193
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
 
10194
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
 
10195
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10196
  }
 
10197
}
 
10198
 
 
10199
 
 
10200
/*
 
10201
  we assume remaining_tables doesnt contain @tab.
 
10202
*/
 
10203
 
 
10204
static void restore_prev_sj_state(const table_map remaining_tables, 
 
10205
                                  const JOIN_TAB *tab)
 
10206
{
 
10207
  TABLE_LIST *emb_sj_nest;
 
10208
  if ((emb_sj_nest= tab->emb_sj_nest))
 
10209
  {
 
10210
    /* If we're removing the last SJ-inner table, remove the sj-nest */
 
10211
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
 
10212
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
 
10213
    {
 
10214
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
 
10215
    }
 
10216
  }
 
10217
}
 
10218
 
 
10219
 
 
10220
static COND *
 
10221
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
 
10222
              Item::cond_result *cond_value)
 
10223
{
 
10224
  THD *thd= join->thd;
2916
10225
 
2917
10226
  if (!conds)
2918
10227
    *cond_value= Item::COND_TRUE;
2919
10228
  else
2920
10229
  {
2921
 
    /*
 
10230
    /* 
2922
10231
      Build all multiple equality predicates and eliminate equality
2923
10232
      predicates that can be inferred from these multiple equalities.
2924
10233
      For each reference of a field included into a multiple equality
2925
10234
      that occurs in a function set a pointer to the multiple equality
2926
10235
      predicate. Substitute a constant instead of this field if the
2927
10236
      multiple equality contains a constant.
2928
 
    */
2929
 
    conds= build_equal_items(join->session, conds, NULL, join_list,
 
10237
    */ 
 
10238
    conds= build_equal_items(join->thd, conds, NULL, join_list,
2930
10239
                             &join->cond_equal);
2931
10240
 
2932
10241
    /* change field = field to field = const for each found field = const */
2933
 
    vector<COND_CMP> temp;
2934
 
    propagate_cond_constants(session, temp, conds, conds);
 
10242
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
2935
10243
    /*
2936
10244
      Remove all instances of item == item
2937
10245
      Remove all and-levels where CONST item != CONST item
2938
10246
    */
2939
 
    conds= remove_eq_conds(session, conds, cond_value) ;
 
10247
    conds= remove_eq_conds(thd, conds, cond_value) ;
2940
10248
  }
2941
10249
  return(conds);
2942
10250
}
2943
10251
 
 
10252
 
2944
10253
/**
2945
10254
  Remove const and eq items.
2946
10255
 
2951
10260
    - COND_TRUE   : always true ( 1 = 1 )
2952
10261
    - COND_FALSE  : always false        ( 1 = 2 )
2953
10262
*/
2954
 
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
10263
 
 
10264
COND *
 
10265
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
2955
10266
{
2956
10267
  if (cond->type() == Item::COND_ITEM)
2957
10268
  {
2958
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2959
 
 
 
10269
    bool and_level= ((Item_cond*) cond)->functype()
 
10270
      == Item_func::COND_AND_FUNC;
2960
10271
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2961
10272
    Item::cond_result tmp_cond_value;
2962
 
    bool should_fix_fields= false;
 
10273
    bool should_fix_fields=0;
2963
10274
 
2964
 
    *cond_value= Item::COND_UNDEF;
 
10275
    *cond_value=Item::COND_UNDEF;
2965
10276
    Item *item;
2966
 
    while ((item= li++))
 
10277
    while ((item=li++))
2967
10278
    {
2968
 
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2969
 
      if (! new_item)
2970
 
              li.remove();
 
10279
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
 
10280
      if (!new_item)
 
10281
        li.remove();
2971
10282
      else if (item != new_item)
2972
10283
      {
2973
 
        li.replace(new_item);
2974
 
        should_fix_fields= true;
 
10284
        VOID(li.replace(new_item));
 
10285
        should_fix_fields=1;
2975
10286
      }
2976
10287
      if (*cond_value == Item::COND_UNDEF)
2977
 
              *cond_value= tmp_cond_value;
2978
 
 
2979
 
      switch (tmp_cond_value) 
2980
 
      {
2981
 
        case Item::COND_OK:                     /* Not true or false */
2982
 
          if (and_level || (*cond_value == Item::COND_FALSE))
2983
 
            *cond_value= tmp_cond_value;
2984
 
          break;
2985
 
        case Item::COND_FALSE:
2986
 
          if (and_level)
2987
 
          {
2988
 
            *cond_value= tmp_cond_value;
2989
 
            return (COND *) NULL;                       /* Always false */
2990
 
          }
2991
 
          break;
2992
 
        case Item::COND_TRUE:
2993
 
          if (! and_level)
2994
 
          {
2995
 
            *cond_value= tmp_cond_value;
2996
 
            return (COND *) NULL;                       /* Always true */
2997
 
          }
2998
 
          break;
2999
 
        case Item::COND_UNDEF:                  /* Impossible */
3000
 
          break;
 
10288
        *cond_value=tmp_cond_value;
 
10289
      switch (tmp_cond_value) {
 
10290
      case Item::COND_OK:                       // Not true or false
 
10291
        if (and_level || *cond_value == Item::COND_FALSE)
 
10292
          *cond_value=tmp_cond_value;
 
10293
        break;
 
10294
      case Item::COND_FALSE:
 
10295
        if (and_level)
 
10296
        {
 
10297
          *cond_value=tmp_cond_value;
 
10298
          return (COND*) 0;                     // Always false
 
10299
        }
 
10300
        break;
 
10301
      case Item::COND_TRUE:
 
10302
        if (!and_level)
 
10303
        {
 
10304
          *cond_value= tmp_cond_value;
 
10305
          return (COND*) 0;                     // Always true
 
10306
        }
 
10307
        break;
 
10308
      case Item::COND_UNDEF:                    // Impossible
 
10309
        break; /* purecov: deadcode */
3001
10310
      }
3002
10311
    }
3003
 
 
3004
10312
    if (should_fix_fields)
3005
10313
      cond->update_used_tables();
3006
10314
 
3007
 
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
3008
 
      return (COND*) NULL;
3009
 
 
 
10315
    if (!((Item_cond*) cond)->argument_list()->elements ||
 
10316
        *cond_value != Item::COND_OK)
 
10317
      return (COND*) 0;
3010
10318
    if (((Item_cond*) cond)->argument_list()->elements == 1)
3011
 
    {                                           
3012
 
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
 
10319
    {                                           // Remove list
3013
10320
      item= ((Item_cond*) cond)->argument_list()->head();
3014
10321
      ((Item_cond*) cond)->argument_list()->empty();
3015
10322
      return item;
3016
10323
    }
3017
10324
  }
3018
 
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
10325
  else if (cond->type() == Item::FUNC_ITEM &&
 
10326
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
3019
10327
  {
3020
10328
    /*
3021
10329
      Handles this special case for some ODBC applications:
3027
10335
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
3028
10336
    */
3029
10337
 
3030
 
    Item_func_isnull *func= (Item_func_isnull*) cond;
 
10338
    Item_func_isnull *func=(Item_func_isnull*) cond;
3031
10339
    Item **args= func->arguments();
3032
10340
    if (args[0]->type() == Item::FIELD_ITEM)
3033
10341
    {
3034
 
      Field *field= ((Item_field*) args[0])->field;
3035
 
      if (field->flags & AUTO_INCREMENT_FLAG 
3036
 
          && ! field->table->maybe_null 
3037
 
          && session->options & OPTION_AUTO_IS_NULL
3038
 
          && (
3039
 
            session->first_successful_insert_id_in_prev_stmt > 0 
3040
 
            && session->substitute_null_with_insert_id
3041
 
            )
3042
 
          )
 
10342
      Field *field=((Item_field*) args[0])->field;
 
10343
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
 
10344
          (thd->options & OPTION_AUTO_IS_NULL) &&
 
10345
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
 
10346
           thd->substitute_null_with_insert_id))
3043
10347
      {
3044
 
        COND *new_cond;
3045
 
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
3046
 
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
3047
 
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
3048
 
        {
3049
 
          cond= new_cond;
 
10348
        COND *new_cond;
 
10349
        if ((new_cond= new Item_func_eq(args[0],
 
10350
                                        new Item_int("last_insert_id()",
 
10351
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
 
10352
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
 
10353
        {
 
10354
          cond=new_cond;
3050
10355
          /*
3051
10356
            Item_func_eq can't be fixed after creation so we do not check
3052
10357
            cond->fixed, also it do not need tables so we use 0 as second
3053
10358
            argument.
3054
10359
          */
3055
 
          cond->fix_fields(session, &cond);
3056
 
        }
 
10360
          cond->fix_fields(thd, &cond);
 
10361
        }
3057
10362
        /*
3058
10363
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
3059
10364
          clear for next row
3060
10365
        */
3061
 
        session->substitute_null_with_insert_id= false;
 
10366
        thd->substitute_null_with_insert_id= false;
3062
10367
      }
3063
 
#ifdef NOTDEFINED
3064
10368
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
3065
 
      else if (
3066
 
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
3067
 
          && (field->flags & NOT_NULL_FLAG) 
3068
 
          && ! field->table->maybe_null)
 
10369
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
 
10370
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
 
10371
                (field->flags & NOT_NULL_FLAG) &&
 
10372
               !field->table->maybe_null)
3069
10373
      {
3070
 
        COND *new_cond;
3071
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
3072
 
        {
3073
 
          cond= new_cond;
 
10374
        COND *new_cond;
 
10375
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
10376
        {
 
10377
          cond=new_cond;
3074
10378
          /*
3075
10379
            Item_func_eq can't be fixed after creation so we do not check
3076
10380
            cond->fixed, also it do not need tables so we use 0 as second
3077
10381
            argument.
3078
10382
          */
3079
 
          cond->fix_fields(session, &cond);
3080
 
        }
 
10383
          cond->fix_fields(thd, &cond);
 
10384
        }
3081
10385
      }
3082
 
#endif /* NOTDEFINED */
3083
10386
    }
3084
10387
    if (cond->const_item())
3085
10388
    {
3086
10389
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3087
 
      return (COND *) NULL;
 
10390
      return (COND*) 0;
3088
10391
    }
3089
10392
  }
3090
10393
  else if (cond->const_item() && !cond->is_expensive())
3100
10403
  */
3101
10404
  {
3102
10405
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3103
 
    return (COND *) NULL;
 
10406
    return (COND*) 0;
3104
10407
  }
3105
10408
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
3106
 
  {                                             
3107
 
    /* boolan compare function */
 
10409
  {                                             // boolan compare function
3108
10410
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
3109
10411
    Item *right_item= ((Item_func*) cond)->arguments()[1];
3110
10412
    if (left_item->eq(right_item,1))
3111
10413
    {
3112
 
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
3113
 
              return (COND*) NULL;                      /* Comparison of identical items */
 
10414
      if (!left_item->maybe_null ||
 
10415
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
10416
        return (COND*) 0;                       // Compare of identical items
3114
10417
    }
3115
10418
  }
3116
 
  *cond_value= Item::COND_OK;
3117
 
  return cond;                                  /* Point at next and return into recursion */
 
10419
  *cond_value=Item::COND_OK;
 
10420
  return cond;                                  // Point at next and level
3118
10421
}
3119
10422
 
3120
 
/*
 
10423
/* 
3121
10424
  Check if equality can be used in removing components of GROUP BY/DISTINCT
3122
 
 
 
10425
  
3123
10426
  SYNOPSIS
3124
10427
    test_if_equality_guarantees_uniqueness()
3125
10428
      l          the left comparison argument (a field if any)
3126
10429
      r          the right comparison argument (a const of any)
3127
 
 
3128
 
  DESCRIPTION
3129
 
    Checks if an equality predicate can be used to take away
3130
 
    DISTINCT/GROUP BY because it is known to be true for exactly one
 
10430
  
 
10431
  DESCRIPTION    
 
10432
    Checks if an equality predicate can be used to take away 
 
10433
    DISTINCT/GROUP BY because it is known to be true for exactly one 
3131
10434
    distinct value (e.g. <expr> == <const>).
3132
 
    Arguments must be of the same type because e.g.
3133
 
    <string_field> = <int_const> may match more than 1 distinct value from
3134
 
    the column.
3135
 
    We must take into consideration and the optimization done for various
 
10435
    Arguments must be of the same type because e.g. 
 
10436
    <string_field> = <int_const> may match more than 1 distinct value from 
 
10437
    the column. 
 
10438
    We must take into consideration and the optimization done for various 
3136
10439
    string constants when compared to dates etc (see Item_int_with_ref) as
3137
10440
    well as the collation of the arguments.
3138
 
 
3139
 
  RETURN VALUE
 
10441
  
 
10442
  RETURN VALUE  
3140
10443
    true    can be used
3141
10444
    false   cannot be used
3142
10445
*/
3143
 
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
10446
static bool
 
10447
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
3144
10448
{
3145
10449
  return r->const_item() &&
3146
10450
    /* elements must be compared as dates */
3155
10459
/**
3156
10460
  Return true if the item is a const value in all the WHERE clause.
3157
10461
*/
3158
 
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
10462
 
 
10463
static bool
 
10464
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
3159
10465
{
3160
10466
  if (cond->type() == Item::COND_ITEM)
3161
10467
  {
3168
10474
      bool res=const_expression_in_where(item, comp_item, const_item);
3169
10475
      if (res)                                  // Is a const value
3170
10476
      {
3171
 
        if (and_level)
3172
 
          return 1;
 
10477
        if (and_level)
 
10478
          return 1;
3173
10479
      }
3174
10480
      else if (!and_level)
3175
 
        return 0;
 
10481
        return 0;
3176
10482
    }
3177
10483
    return and_level ? 0 : 1;
3178
10484
  }
3180
10486
  {                                             // boolan compare function
3181
10487
    Item_func* func= (Item_func*) cond;
3182
10488
    if (func->functype() != Item_func::EQUAL_FUNC &&
3183
 
              func->functype() != Item_func::EQ_FUNC)
 
10489
        func->functype() != Item_func::EQ_FUNC)
3184
10490
      return 0;
3185
10491
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
3186
10492
    Item *right_item= ((Item_func*) cond)->arguments()[1];
3188
10494
    {
3189
10495
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
3190
10496
      {
3191
 
        if (*const_item)
3192
 
          return right_item->eq(*const_item, 1);
3193
 
        *const_item=right_item;
3194
 
        return 1;
 
10497
        if (*const_item)
 
10498
          return right_item->eq(*const_item, 1);
 
10499
        *const_item=right_item;
 
10500
        return 1;
3195
10501
      }
3196
10502
    }
3197
10503
    else if (right_item->eq(comp_item,1))
3198
10504
    {
3199
10505
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
3200
10506
      {
3201
 
        if (*const_item)
3202
 
          return left_item->eq(*const_item, 1);
3203
 
        *const_item=left_item;
3204
 
        return 1;
 
10507
        if (*const_item)
 
10508
          return left_item->eq(*const_item, 1);
 
10509
        *const_item=left_item;
 
10510
        return 1;
3205
10511
      }
3206
10512
    }
3207
10513
  }
3208
10514
  return 0;
3209
10515
}
3210
10516
 
 
10517
 
3211
10518
/**
3212
10519
  @details
3213
10520
  Rows produced by a join sweep may end up in a temporary table or be sent
3219
10526
  @return
3220
10527
    end_select function to use. This function can't fail.
3221
10528
*/
 
10529
 
3222
10530
Next_select_func setup_end_select_func(JOIN *join)
3223
10531
{
3224
 
  Table *table= join->tmp_table;
3225
 
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
 
10532
  TABLE *table= join->tmp_table;
 
10533
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
3226
10534
  Next_select_func end_select;
3227
10535
 
3228
10536
  /* Set up select_end */
3229
10537
  if (table)
3230
10538
  {
3231
 
    if (table->group && tmp_tbl->sum_func_count &&
 
10539
    if (table->group && tmp_tbl->sum_func_count && 
3232
10540
        !tmp_tbl->precomputed_group_by)
3233
10541
    {
3234
10542
      if (table->s->keys)
3235
10543
      {
3236
 
        end_select= end_update;
 
10544
        end_select=end_update;
3237
10545
      }
3238
10546
      else
3239
10547
      {
3240
 
        end_select= end_unique_update;
 
10548
        end_select=end_unique_update;
3241
10549
      }
3242
10550
    }
3243
10551
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3252
10560
        /*
3253
10561
          A preceding call to create_tmp_table in the case when loose
3254
10562
          index scan is used guarantees that
3255
 
          Tmp_Table_Param::items_to_copy has enough space for the group
 
10563
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
3256
10564
          by functions. It is OK here to use memcpy since we copy
3257
10565
          Item_sum pointers into an array of Item pointers.
3258
10566
        */
3274
10582
  return end_select;
3275
10583
}
3276
10584
 
 
10585
 
3277
10586
/**
3278
10587
  Make a join of all tables and write it on socket or to table.
3279
10588
 
3284
10593
  @retval
3285
10594
    -1  if error should be sent
3286
10595
*/
3287
 
int do_select(JOIN *join, List<Item> *fields, Table *table)
 
10596
 
 
10597
static int
 
10598
do_select(JOIN *join,List<Item> *fields,TABLE *table)
3288
10599
{
3289
10600
  int rc= 0;
3290
10601
  enum_nested_loop_state error= NESTED_LOOP_OK;
3291
 
  JoinTable *join_tab= NULL;
3292
 
 
 
10602
  JOIN_TAB *join_tab= NULL;
 
10603
  
3293
10604
  join->tmp_table= table;                       /* Save for easy recursion */
3294
10605
  join->fields= fields;
3295
10606
 
3296
10607
  if (table)
3297
10608
  {
3298
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3299
 
    table->emptyRecord();
 
10609
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
 
10610
    empty_record(table);
3300
10611
    if (table->group && join->tmp_table_param.sum_func_count &&
3301
 
        table->s->keys && !table->cursor->inited)
3302
 
      table->cursor->ha_index_init(0, 0);
 
10612
        table->s->keys && !table->file->inited)
 
10613
      table->file->ha_index_init(0, 0);
3303
10614
  }
3304
10615
  /* Set up select_end */
3305
10616
  Next_select_func end_select= setup_end_select_func(join);
3320
10631
    {
3321
10632
      error= (*end_select)(join, 0, 0);
3322
10633
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3323
 
              error= (*end_select)(join, 0, 1);
 
10634
        error= (*end_select)(join, 0, 1);
3324
10635
 
3325
10636
      /*
3326
10637
        If we don't go through evaluate_join_record(), do the counting
3328
10639
        so we don't touch it here.
3329
10640
      */
3330
10641
      join->examined_rows++;
3331
 
      join->session->row_count++;
 
10642
      join->thd->row_count++;
3332
10643
      assert(join->examined_rows <= 1);
3333
10644
    }
3334
10645
    else if (join->send_row_on_empty_set())
3358
10669
    if (!table)                                 // If sending data to client
3359
10670
    {
3360
10671
      /*
3361
 
        The following will unlock all cursors if the command wasn't an
3362
 
        update command
 
10672
        The following will unlock all cursors if the command wasn't an
 
10673
        update command
3363
10674
      */
3364
10675
      join->join_free();                        // Unlock all cursors
3365
10676
      if (join->result->send_eof())
3366
 
        rc= 1;                                  // Don't send error
 
10677
        rc= 1;                                  // Don't send error
3367
10678
    }
3368
10679
  }
3369
10680
  else
3371
10682
  if (table)
3372
10683
  {
3373
10684
    int tmp, new_errno= 0;
3374
 
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
10685
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
3375
10686
    {
3376
10687
      new_errno= tmp;
3377
10688
    }
3378
 
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
 
10689
    if ((tmp=table->file->ha_index_or_rnd_end()))
3379
10690
    {
3380
10691
      new_errno= tmp;
3381
10692
    }
3382
10693
    if (new_errno)
3383
 
      table->print_error(new_errno,MYF(0));
 
10694
      table->file->print_error(new_errno,MYF(0));
3384
10695
  }
3385
 
  return(join->session->is_error() ? -1 : rc);
 
10696
  return(join->thd->is_error() ? -1 : rc);
3386
10697
}
3387
10698
 
3388
 
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records)
 
10699
 
 
10700
enum_nested_loop_state
 
10701
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3389
10702
{
3390
10703
  enum_nested_loop_state rc;
3391
10704
 
3396
10709
      rc= sub_select(join,join_tab,end_of_records);
3397
10710
    return rc;
3398
10711
  }
3399
 
  if (join->session->killed)            // If aborted by user
 
10712
  if (join->thd->killed)                // If aborted by user
3400
10713
  {
3401
 
    join->session->send_kill_message();
3402
 
    return NESTED_LOOP_KILLED;
 
10714
    join->thd->send_kill_message();
 
10715
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
3403
10716
  }
3404
10717
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3405
10718
  {
3406
 
    if (! store_record_in_cache(&join_tab->cache))
 
10719
    if (!store_record_in_cache(&join_tab->cache))
3407
10720
      return NESTED_LOOP_OK;                     // There is more room in cache
3408
10721
    return flush_cached_records(join,join_tab,false);
3409
10722
  }
3416
10729
/**
3417
10730
  Retrieve records ends with a given beginning from the result of a join.
3418
10731
 
3419
 
    For a given partial join record consisting of records from the tables
 
10732
    For a given partial join record consisting of records from the tables 
3420
10733
    preceding the table join_tab in the execution plan, the function
3421
10734
    retrieves all matching full records from the result set and
3422
 
    send them to the result set stream.
 
10735
    send them to the result set stream. 
3423
10736
 
3424
10737
  @note
3425
10738
    The function effectively implements the  final (n-k) nested loops
3459
10772
    first row with t3.a=t1.a has been encountered.
3460
10773
    Thus, the second predicate P2 is supplied with a guarded value that are
3461
10774
    stored in the field 'found' of the first inner table for the outer join
3462
 
    (table t2). When the first row with t3.a=t1.a for the  current row
 
10775
    (table t2). When the first row with t3.a=t1.a for the  current row 
3463
10776
    of table t1  appears, the value becomes true. For now on the predicate
3464
10777
    is evaluated immediately after the row of table t2 has been read.
3465
10778
    When the first row with t3.a=t1.a has been encountered all
3467
10780
    Only when all of them are true the row is sent to the output stream.
3468
10781
    If not, the function returns to the lowest nest level that has a false
3469
10782
    attached condition.
3470
 
    The predicates from on expressions are also pushed down. If in the
 
10783
    The predicates from on expressions are also pushed down. If in the 
3471
10784
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
3472
10785
    then t1.a=t2.a would be pushed down to table t2, and without any
3473
10786
    guard.
3477
10790
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
3478
10791
    are checked for the composed row almost in the same way as it had
3479
10792
    been done for the first row with a match. The only difference is
3480
 
    the predicates from on expressions are not checked.
 
10793
    the predicates from on expressions are not checked. 
3481
10794
 
3482
10795
  @par
3483
10796
  @b IMPLEMENTATION
3493
10806
    and a pointer to a guarding boolean variable.
3494
10807
    When the value of the guard variable is true the value of the object
3495
10808
    is the same as the value of the predicate, otherwise it's just returns
3496
 
    true.
3497
 
    To carry out a return to a nested loop level of join table t the pointer
 
10809
    true. 
 
10810
    To carry out a return to a nested loop level of join table t the pointer 
3498
10811
    to t is remembered in the field 'return_tab' of the join structure.
3499
10812
    Consider the following query:
3500
10813
    @code
3511
10824
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
3512
10825
    becomes 'activated', as well the predicate t4.a=t2.a. But
3513
10826
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
3514
 
    t4.a=t2.a becomes true.
 
10827
    t4.a=t2.a becomes true. 
3515
10828
    In order not to re-evaluate the predicates that were already evaluated
3516
10829
    as attached pushed down predicates, a pointer to the the first
3517
10830
    most inner unmatched table is maintained in join_tab->first_unmatched.
3518
10831
    Thus, when the first row from t5 with t5.a=t3.a is found
3519
 
    this pointer for t5 is changed from t4 to t2.
 
10832
    this pointer for t5 is changed from t4 to t2.             
3520
10833
 
3521
10834
    @par
3522
10835
    @b STRUCTURE @b NOTES
3527
10840
  @param join      pointer to the structure providing all context info for
3528
10841
                   the query
3529
10842
  @param join_tab  the first next table of the execution plan to be retrieved
3530
 
  @param end_records  true when we need to perform final steps of retrival
 
10843
  @param end_records  true when we need to perform final steps of retrival   
3531
10844
 
3532
10845
  @return
3533
10846
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3534
10847
*/
3535
 
enum_nested_loop_state sub_select(JOIN *join, JoinTable *join_tab, bool end_of_records)
 
10848
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
 
10849
 
 
10850
enum_nested_loop_state
 
10851
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
3536
10852
{
3537
10853
  join_tab->table->null_row=0;
3538
10854
  if (end_of_records)
3542
10858
  enum_nested_loop_state rc;
3543
10859
  READ_RECORD *info= &join_tab->read_record;
3544
10860
 
 
10861
  if (join_tab->flush_weedout_table)
 
10862
  {
 
10863
    do_sj_reset(join_tab->flush_weedout_table);
 
10864
  }
 
10865
 
3545
10866
  if (join->resume_nested_loop)
3546
10867
  {
3547
10868
    /* If not the last table, plunge down the nested loop */
3568
10889
      /* Set first_unmatched for the last inner table of this group */
3569
10890
      join_tab->last_inner->first_unmatched= join_tab;
3570
10891
    }
3571
 
    join->session->row_count= 0;
 
10892
    join->thd->row_count= 0;
3572
10893
 
3573
10894
    error= (*join_tab->read_first_record)(join_tab);
3574
10895
    rc= evaluate_join_record(join, join_tab, error);
3575
10896
  }
3576
 
 
3577
 
  /*
3578
 
    Note: psergey has added the 2nd part of the following condition; the
 
10897
  
 
10898
  /* 
 
10899
    Note: psergey has added the 2nd part of the following condition; the 
3579
10900
    change should probably be made in 5.1, too.
3580
10901
  */
3581
10902
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
3593
10914
  return rc;
3594
10915
}
3595
10916
 
3596
 
int safe_index_read(JoinTable *tab)
3597
 
{
3598
 
  int error;
3599
 
  Table *table= tab->table;
3600
 
  if ((error=table->cursor->index_read_map(table->record[0],
 
10917
 
 
10918
 
 
10919
 
 
10920
/*
 
10921
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
10922
 
 
10923
  SYNPOSIS
 
10924
    do_sj_dups_weedout()
 
10925
      
 
10926
  RETURN
 
10927
    -1  Error
 
10928
    1   The row combination is a duplicate (discard it)
 
10929
    0   The row combination is not a duplicate (continue)
 
10930
*/
 
10931
 
 
10932
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
 
10933
{
 
10934
  int error;
 
10935
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
10936
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
10937
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
 
10938
  uchar *nulls_ptr= ptr;
 
10939
  
 
10940
  /* Put the the rowids tuple into table->record[0]: */
 
10941
 
 
10942
  // 1. Store the length 
 
10943
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
10944
  {
 
10945
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
 
10946
    ptr++;
 
10947
  }
 
10948
  else
 
10949
  {
 
10950
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
10951
    ptr += 2;
 
10952
  }
 
10953
 
 
10954
  // 2. Zero the null bytes 
 
10955
  if (sjtbl->null_bytes)
 
10956
  {
 
10957
    memset(ptr, 0, sjtbl->null_bytes);
 
10958
    ptr += sjtbl->null_bytes; 
 
10959
  }
 
10960
 
 
10961
  // 3. Put the rowids
 
10962
  for (uint i=0; tab != tab_end; tab++, i++)
 
10963
  {
 
10964
    handler *h= tab->join_tab->table->file;
 
10965
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
10966
    {
 
10967
      /* It's a NULL-complemented row */
 
10968
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
10969
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
10970
    }
 
10971
    else
 
10972
    {
 
10973
      /* Copy the rowid value */
 
10974
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
10975
        h->position(tab->join_tab->table->record[0]);
 
10976
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
10977
    }
 
10978
  }
 
10979
 
 
10980
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
10981
  if (error)
 
10982
  {
 
10983
    /* create_myisam_from_heap will generate error if needed */
 
10984
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
10985
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
 
10986
                                &sjtbl->recinfo, error, 1))
 
10987
      return -1;
 
10988
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
10989
    return 1;
 
10990
  }
 
10991
  return 0;
 
10992
}
 
10993
 
 
10994
 
 
10995
/*
 
10996
  SemiJoinDuplicateElimination: Reset the temporary table
 
10997
*/
 
10998
 
 
10999
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
 
11000
{
 
11001
  if (sj_tbl->tmp_table)
 
11002
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
 
11003
  return 0;
 
11004
}
 
11005
 
 
11006
/*
 
11007
  Process one record of the nested loop join.
 
11008
 
 
11009
    This function will evaluate parts of WHERE/ON clauses that are
 
11010
    applicable to the partial record on hand and in case of success
 
11011
    submit this record to the next level of the nested loop.
 
11012
*/
 
11013
 
 
11014
static enum_nested_loop_state
 
11015
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
 
11016
                     int error)
 
11017
{
 
11018
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
 
11019
  ha_rows found_records=join->found_records;
 
11020
  COND *select_cond= join_tab->select_cond;
 
11021
 
 
11022
  if (error > 0 || (join->thd->is_error()))     // Fatal error
 
11023
    return NESTED_LOOP_ERROR;
 
11024
  if (error < 0)
 
11025
    return NESTED_LOOP_NO_MORE_ROWS;
 
11026
  if (join->thd->killed)                        // Aborted by user
 
11027
  {
 
11028
    join->thd->send_kill_message();
 
11029
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
11030
  }
 
11031
  if (!select_cond || select_cond->val_int())
 
11032
  {
 
11033
    /*
 
11034
      There is no select condition or the attached pushed down
 
11035
      condition is true => a match is found.
 
11036
    */
 
11037
    bool found= 1;
 
11038
    while (join_tab->first_unmatched && found)
 
11039
    {
 
11040
      /*
 
11041
        The while condition is always false if join_tab is not
 
11042
        the last inner join table of an outer join operation.
 
11043
      */
 
11044
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11045
      /*
 
11046
        Mark that a match for current outer table is found.
 
11047
        This activates push down conditional predicates attached
 
11048
        to the all inner tables of the outer join.
 
11049
      */
 
11050
      first_unmatched->found= 1;
 
11051
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11052
      {
 
11053
        if (tab->table->reginfo.not_exists_optimize)
 
11054
          return NESTED_LOOP_NO_MORE_ROWS;
 
11055
        /* Check all predicates that has just been activated. */
 
11056
        /*
 
11057
          Actually all predicates non-guarded by first_unmatched->found
 
11058
          will be re-evaluated again. It could be fixed, but, probably,
 
11059
          it's not worth doing now.
 
11060
        */
 
11061
        if (tab->select_cond && !tab->select_cond->val_int())
 
11062
        {
 
11063
          /* The condition attached to table tab is false */
 
11064
          if (tab == join_tab)
 
11065
            found= 0;
 
11066
          else
 
11067
          {
 
11068
            /*
 
11069
              Set a return point if rejected predicate is attached
 
11070
              not to the last table of the current nest level.
 
11071
            */
 
11072
            join->return_tab= tab;
 
11073
            return NESTED_LOOP_OK;
 
11074
          }
 
11075
        }
 
11076
      }
 
11077
      /*
 
11078
        Check whether join_tab is not the last inner table
 
11079
        for another embedding outer join.
 
11080
      */
 
11081
      if ((first_unmatched= first_unmatched->first_upper) &&
 
11082
          first_unmatched->last_inner != join_tab)
 
11083
        first_unmatched= 0;
 
11084
      join_tab->first_unmatched= first_unmatched;
 
11085
    }
 
11086
 
 
11087
    JOIN_TAB *return_tab= join->return_tab;
 
11088
    join_tab->found_match= true;
 
11089
    if (join_tab->check_weed_out_table)
 
11090
    {
 
11091
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
 
11092
      if (res == -1)
 
11093
        return NESTED_LOOP_ERROR;
 
11094
      if (res == 1)
 
11095
        return NESTED_LOOP_OK;
 
11096
    }
 
11097
    else if (join_tab->do_firstmatch)
 
11098
    {
 
11099
      /* 
 
11100
        We should return to the join_tab->do_firstmatch after we have 
 
11101
        enumerated all the suffixes for current prefix row combination
 
11102
      */
 
11103
      return_tab= join_tab->do_firstmatch;
 
11104
    }
 
11105
 
 
11106
    /*
 
11107
      It was not just a return to lower loop level when one
 
11108
      of the newly activated predicates is evaluated as false
 
11109
      (See above join->return_tab= tab).
 
11110
    */
 
11111
    join->examined_rows++;
 
11112
    join->thd->row_count++;
 
11113
 
 
11114
    if (found)
 
11115
    {
 
11116
      enum enum_nested_loop_state rc;
 
11117
      /* A match from join_tab is found for the current partial join. */
 
11118
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
11119
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11120
        return rc;
 
11121
      if (return_tab < join->return_tab)
 
11122
        join->return_tab= return_tab;
 
11123
 
 
11124
      if (join->return_tab < join_tab)
 
11125
        return NESTED_LOOP_OK;
 
11126
      /*
 
11127
        Test if this was a SELECT DISTINCT query on a table that
 
11128
        was not in the field list;  In this case we can abort if
 
11129
        we found a row, as no new rows can be added to the result.
 
11130
      */
 
11131
      if (not_used_in_distinct && found_records != join->found_records)
 
11132
        return NESTED_LOOP_NO_MORE_ROWS;
 
11133
    }
 
11134
    else
 
11135
      join_tab->read_record.file->unlock_row();
 
11136
  }
 
11137
  else
 
11138
  {
 
11139
    /*
 
11140
      The condition pushed down to the table join_tab rejects all rows
 
11141
      with the beginning coinciding with the current partial join.
 
11142
    */
 
11143
    join->examined_rows++;
 
11144
    join->thd->row_count++;
 
11145
    join_tab->read_record.file->unlock_row();
 
11146
  }
 
11147
  return NESTED_LOOP_OK;
 
11148
}
 
11149
 
 
11150
 
 
11151
/**
 
11152
 
 
11153
  @details
 
11154
    Construct a NULL complimented partial join record and feed it to the next
 
11155
    level of the nested loop. This function is used in case we have
 
11156
    an OUTER join and no matching record was found.
 
11157
*/
 
11158
 
 
11159
static enum_nested_loop_state
 
11160
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
11161
{
 
11162
  /*
 
11163
    The table join_tab is the first inner table of a outer join operation
 
11164
    and no matches has been found for the current outer row.
 
11165
  */
 
11166
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
11167
  /* Cache variables for faster loop */
 
11168
  COND *select_cond;
 
11169
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
11170
  {
 
11171
    /* Change the the values of guard predicate variables. */
 
11172
    join_tab->found= 1;
 
11173
    join_tab->not_null_compl= 0;
 
11174
    /* The outer row is complemented by nulls for each inner tables */
 
11175
    restore_record(join_tab->table,s->default_values);  // Make empty record
 
11176
    mark_as_null_row(join_tab->table);       // For group by without error
 
11177
    select_cond= join_tab->select_cond;
 
11178
    /* Check all attached conditions for inner table rows. */
 
11179
    if (select_cond && !select_cond->val_int())
 
11180
      return NESTED_LOOP_OK;
 
11181
  }
 
11182
  join_tab--;
 
11183
  /*
 
11184
    The row complemented by nulls might be the first row
 
11185
    of embedding outer joins.
 
11186
    If so, perform the same actions as in the code
 
11187
    for the first regular outer join row above.
 
11188
  */
 
11189
  for ( ; ; )
 
11190
  {
 
11191
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
11192
    if ((first_unmatched= first_unmatched->first_upper) &&
 
11193
        first_unmatched->last_inner != join_tab)
 
11194
      first_unmatched= 0;
 
11195
    join_tab->first_unmatched= first_unmatched;
 
11196
    if (!first_unmatched)
 
11197
      break;
 
11198
    first_unmatched->found= 1;
 
11199
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
11200
    {
 
11201
      if (tab->select_cond && !tab->select_cond->val_int())
 
11202
      {
 
11203
        join->return_tab= tab;
 
11204
        return NESTED_LOOP_OK;
 
11205
      }
 
11206
    }
 
11207
  }
 
11208
  /*
 
11209
    The row complemented by nulls satisfies all conditions
 
11210
    attached to inner tables.
 
11211
    Send the row complemented by nulls to be joined with the
 
11212
    remaining tables.
 
11213
  */
 
11214
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
11215
}
 
11216
 
 
11217
 
 
11218
static enum_nested_loop_state
 
11219
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
 
11220
{
 
11221
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
11222
  int error;
 
11223
  READ_RECORD *info;
 
11224
 
 
11225
  join_tab->table->null_row= 0;
 
11226
  if (!join_tab->cache.records)
 
11227
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
11228
  if (skip_last)
 
11229
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
11230
  if (join_tab->use_quick == 2)
 
11231
  {
 
11232
    if (join_tab->select->quick)
 
11233
    {                                   /* Used quick select last. reset it */
 
11234
      delete join_tab->select->quick;
 
11235
      join_tab->select->quick=0;
 
11236
    }
 
11237
  }
 
11238
 /* read through all records */
 
11239
  if ((error=join_init_read_record(join_tab)))
 
11240
  {
 
11241
    reset_cache_write(&join_tab->cache);
 
11242
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
11243
  }
 
11244
 
 
11245
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
11246
  {
 
11247
    tmp->status=tmp->table->status;
 
11248
    tmp->table->status=0;
 
11249
  }
 
11250
 
 
11251
  info= &join_tab->read_record;
 
11252
  do
 
11253
  {
 
11254
    if (join->thd->killed)
 
11255
    {
 
11256
      join->thd->send_kill_message();
 
11257
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
11258
    }
 
11259
    SQL_SELECT *select=join_tab->select;
 
11260
    if (rc == NESTED_LOOP_OK &&
 
11261
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
11262
    {
 
11263
      uint i;
 
11264
      reset_cache_read(&join_tab->cache);
 
11265
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
11266
      {
 
11267
        read_cached_record(join_tab);
 
11268
        if (!select || !select->skip_record())
 
11269
        {
 
11270
          int res= 0;
 
11271
          if (!join_tab->check_weed_out_table || 
 
11272
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
 
11273
          {
 
11274
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
11275
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
11276
            {
 
11277
              reset_cache_write(&join_tab->cache);
 
11278
              return rc;
 
11279
            }
 
11280
          }
 
11281
          if (res == -1)
 
11282
            return NESTED_LOOP_ERROR;
 
11283
        }
 
11284
      }
 
11285
    }
 
11286
  } while (!(error=info->read_record(info)));
 
11287
 
 
11288
  if (skip_last)
 
11289
    read_cached_record(join_tab);               // Restore current record
 
11290
  reset_cache_write(&join_tab->cache);
 
11291
  if (error > 0)                                // Fatal error
 
11292
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
11293
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
11294
    tmp2->table->status=tmp2->status;
 
11295
  return NESTED_LOOP_OK;
 
11296
}
 
11297
 
 
11298
 
 
11299
/*****************************************************************************
 
11300
  The different ways to read a record
 
11301
  Returns -1 if row was not found, 0 if row was found and 1 on errors
 
11302
*****************************************************************************/
 
11303
 
 
11304
/** Help function when we get some an error from the table handler. */
 
11305
 
 
11306
int report_error(TABLE *table, int error)
 
11307
{
 
11308
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
 
11309
  {
 
11310
    table->status= STATUS_GARBAGE;
 
11311
    return -1;                                  // key not found; ok
 
11312
  }
 
11313
  /*
 
11314
    Locking reads can legally return also these errors, do not
 
11315
    print them to the .err log
 
11316
  */
 
11317
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
 
11318
    sql_print_error("Got error %d when reading table '%s'",
 
11319
                    error, table->s->path.str);
 
11320
  table->file->print_error(error,MYF(0));
 
11321
  return 1;
 
11322
}
 
11323
 
 
11324
 
 
11325
int safe_index_read(JOIN_TAB *tab)
 
11326
{
 
11327
  int error;
 
11328
  TABLE *table= tab->table;
 
11329
  if ((error=table->file->index_read_map(table->record[0],
3601
11330
                                         tab->ref.key_buff,
3602
11331
                                         make_prev_keypart_map(tab->ref.key_parts),
3603
11332
                                         HA_READ_KEY_EXACT)))
3604
 
    return table->report_error(error);
 
11333
    return report_error(table, error);
3605
11334
  return 0;
3606
11335
}
3607
11336
 
3608
 
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
11337
 
 
11338
static int
 
11339
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3609
11340
{
3610
11341
  int error;
3611
 
  Table *table=tab->table;
 
11342
  TABLE *table=tab->table;
3612
11343
  table->const_table=1;
3613
11344
  table->null_row=0;
3614
11345
  table->status=STATUS_NO_RECORD;
3615
 
 
3616
 
  if (tab->type == AM_SYSTEM)
 
11346
  
 
11347
  if (tab->type == JT_SYSTEM)
3617
11348
  {
3618
11349
    if ((error=join_read_system(tab)))
3619
11350
    {                                           // Info for DESCRIBE
3620
11351
      tab->info="const row not found";
3621
11352
      /* Mark for EXPLAIN that the row was not found */
3622
 
      pos->setFanout(0.0);
3623
 
      pos->clearRefDependMap();
3624
 
      if (! table->maybe_null || error > 0)
3625
 
        return(error);
 
11353
      pos->records_read=0.0;
 
11354
      pos->ref_depend_map= 0;
 
11355
      if (!table->maybe_null || error > 0)
 
11356
        return(error);
3626
11357
    }
3627
11358
  }
3628
11359
  else
3629
11360
  {
3630
 
    if (! table->key_read && 
3631
 
        table->covering_keys.test(tab->ref.key) && 
3632
 
        ! table->no_keyread &&
3633
 
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
11361
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
 
11362
        !table->no_keyread &&
 
11363
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3634
11364
    {
3635
11365
      table->key_read=1;
3636
 
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
11366
      table->file->extra(HA_EXTRA_KEYREAD);
3637
11367
      tab->index= tab->ref.key;
3638
11368
    }
3639
11369
    error=join_read_const(tab);
3640
11370
    if (table->key_read)
3641
11371
    {
3642
11372
      table->key_read=0;
3643
 
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
11373
      table->file->extra(HA_EXTRA_NO_KEYREAD);
3644
11374
    }
3645
11375
    if (error)
3646
11376
    {
3647
11377
      tab->info="unique row not found";
3648
11378
      /* Mark for EXPLAIN that the row was not found */
3649
 
      pos->setFanout(0.0);
3650
 
      pos->clearRefDependMap();
 
11379
      pos->records_read=0.0;
 
11380
      pos->ref_depend_map= 0;
3651
11381
      if (!table->maybe_null || error > 0)
3652
 
        return(error);
 
11382
        return(error);
3653
11383
    }
3654
11384
  }
3655
11385
  if (*tab->on_expr_ref && !table->null_row)
3656
11386
  {
3657
11387
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3658
 
      table->mark_as_null_row();
 
11388
      mark_as_null_row(table);  
3659
11389
  }
3660
11390
  if (!table->null_row)
3661
11391
    table->maybe_null=0;
3664
11394
  JOIN *join= tab->join;
3665
11395
  if (join->conds)
3666
11396
    update_const_equal_items(join->conds, tab);
3667
 
  TableList *tbl;
 
11397
  TABLE_LIST *tbl;
3668
11398
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
3669
11399
  {
3670
 
    TableList *embedded;
3671
 
    TableList *embedding= tbl;
 
11400
    TABLE_LIST *embedded;
 
11401
    TABLE_LIST *embedding= tbl;
3672
11402
    do
3673
11403
    {
3674
11404
      embedded= embedding;
3683
11413
  return(0);
3684
11414
}
3685
11415
 
3686
 
int join_read_system(JoinTable *tab)
 
11416
 
 
11417
static int
 
11418
join_read_system(JOIN_TAB *tab)
3687
11419
{
3688
 
  Table *table= tab->table;
 
11420
  TABLE *table= tab->table;
3689
11421
  int error;
3690
11422
  if (table->status & STATUS_GARBAGE)           // If first read
3691
11423
  {
3692
 
    if ((error=table->cursor->read_first_row(table->record[0],
 
11424
    if ((error=table->file->read_first_row(table->record[0],
3693
11425
                                           table->s->primary_key)))
3694
11426
    {
3695
11427
      if (error != HA_ERR_END_OF_FILE)
3696
 
        return table->report_error(error);
3697
 
      tab->table->mark_as_null_row();
3698
 
      table->emptyRecord();                     // Make empty record
 
11428
        return report_error(table, error);
 
11429
      mark_as_null_row(tab->table);
 
11430
      empty_record(table);                      // Make empty record
3699
11431
      return -1;
3700
11432
    }
3701
 
    table->storeRecord();
 
11433
    store_record(table,record[1]);
3702
11434
  }
3703
11435
  else if (!table->status)                      // Only happens with left join
3704
 
    table->restoreRecord();                     // restore old record
 
11436
    restore_record(table,record[1]);                    // restore old record
3705
11437
  table->null_row=0;
3706
11438
  return table->status ? -1 : 0;
3707
11439
}
3708
11440
 
 
11441
 
3709
11442
/**
3710
11443
  Read a (constant) table when there is at most one matching row.
3711
11444
 
3718
11451
  @retval
3719
11452
    1   Got an error (other than row not found) during read
3720
11453
*/
3721
 
int join_read_const(JoinTable *tab)
 
11454
 
 
11455
static int
 
11456
join_read_const(JOIN_TAB *tab)
3722
11457
{
3723
11458
  int error;
3724
 
  Table *table= tab->table;
 
11459
  TABLE *table= tab->table;
3725
11460
  if (table->status & STATUS_GARBAGE)           // If first read
3726
11461
  {
3727
11462
    table->status= 0;
3728
 
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3729
 
      error= HA_ERR_KEY_NOT_FOUND;
 
11463
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
11464
      error=HA_ERR_KEY_NOT_FOUND;
3730
11465
    else
3731
11466
    {
3732
 
      error=table->cursor->index_read_idx_map(table->record[0],tab->ref.key,
3733
 
                                            (unsigned char*) tab->ref.key_buff,
 
11467
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
 
11468
                                            (uchar*) tab->ref.key_buff,
3734
11469
                                            make_prev_keypart_map(tab->ref.key_parts),
3735
11470
                                            HA_READ_KEY_EXACT);
3736
11471
    }
3737
11472
    if (error)
3738
11473
    {
3739
11474
      table->status= STATUS_NOT_FOUND;
3740
 
      tab->table->mark_as_null_row();
3741
 
      table->emptyRecord();
 
11475
      mark_as_null_row(tab->table);
 
11476
      empty_record(table);
3742
11477
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3743
 
        return table->report_error(error);
 
11478
        return report_error(table, error);
3744
11479
      return -1;
3745
11480
    }
3746
 
    table->storeRecord();
 
11481
    store_record(table,record[1]);
3747
11482
  }
3748
11483
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
3749
11484
  {
3750
11485
    table->status=0;
3751
 
    table->restoreRecord();                     // restore old record
 
11486
    restore_record(table,record[1]);                    // restore old record
3752
11487
  }
3753
11488
  table->null_row=0;
3754
11489
  return table->status ? -1 : 0;
3755
11490
}
3756
11491
 
 
11492
 
3757
11493
/*
3758
11494
  eq_ref access method implementation: "read_first" function
3759
11495
 
3760
11496
  SYNOPSIS
3761
11497
    join_read_key()
3762
 
      tab  JoinTable of the accessed table
 
11498
      tab  JOIN_TAB of the accessed table
3763
11499
 
3764
11500
  DESCRIPTION
3765
11501
    This is "read_fist" function for the "ref" access method. The difference
3767
11503
 
3768
11504
  RETURN
3769
11505
    0  - Ok
3770
 
   -1  - Row not found
 
11506
   -1  - Row not found 
3771
11507
    1  - Error
3772
11508
*/
3773
 
int join_read_key(JoinTable *tab)
 
11509
 
 
11510
static int
 
11511
join_read_key(JOIN_TAB *tab)
3774
11512
{
3775
11513
  int error;
3776
 
  Table *table= tab->table;
 
11514
  TABLE *table= tab->table;
3777
11515
 
3778
 
  if (!table->cursor->inited)
 
11516
  if (!table->file->inited)
3779
11517
  {
3780
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
11518
    table->file->ha_index_init(tab->ref.key, tab->sorted);
3781
11519
  }
3782
11520
 
3783
11521
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3789
11527
      table->status=STATUS_NOT_FOUND;
3790
11528
      return -1;
3791
11529
    }
3792
 
    error=table->cursor->index_read_map(table->record[0],
 
11530
    error=table->file->index_read_map(table->record[0],
3793
11531
                                      tab->ref.key_buff,
3794
11532
                                      make_prev_keypart_map(tab->ref.key_parts),
3795
11533
                                      HA_READ_KEY_EXACT);
3796
11534
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3797
 
      return table->report_error(error);
 
11535
      return report_error(table, error);
3798
11536
  }
3799
11537
  table->null_row=0;
3800
11538
  return table->status ? -1 : 0;
3801
11539
}
3802
11540
 
 
11541
 
3803
11542
/*
3804
11543
  ref access method implementation: "read_first" function
3805
11544
 
3806
11545
  SYNOPSIS
3807
11546
    join_read_always_key()
3808
 
      tab  JoinTable of the accessed table
 
11547
      tab  JOIN_TAB of the accessed table
3809
11548
 
3810
11549
  DESCRIPTION
3811
 
    This is "read_first" function for the "ref" access method.
3812
 
 
 
11550
    This is "read_fist" function for the "ref" access method.
 
11551
   
3813
11552
    The functon must leave the index initialized when it returns.
3814
11553
    ref_or_null access implementation depends on that.
3815
11554
 
3816
11555
  RETURN
3817
11556
    0  - Ok
3818
 
   -1  - Row not found
 
11557
   -1  - Row not found 
3819
11558
    1  - Error
3820
11559
*/
3821
 
int join_read_always_key(JoinTable *tab)
 
11560
 
 
11561
static int
 
11562
join_read_always_key(JOIN_TAB *tab)
3822
11563
{
3823
11564
  int error;
3824
 
  Table *table= tab->table;
 
11565
  TABLE *table= tab->table;
3825
11566
 
3826
11567
  /* Initialize the index first */
3827
 
  if (!table->cursor->inited)
3828
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
3829
 
 
 
11568
  if (!table->file->inited)
 
11569
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11570
 
3830
11571
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3831
 
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
 
11572
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
3832
11573
  {
3833
11574
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3834
11575
        return -1;
3835
11576
  }
3836
11577
 
3837
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11578
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3838
11579
    return -1;
3839
 
  if ((error=table->cursor->index_read_map(table->record[0],
 
11580
  if ((error=table->file->index_read_map(table->record[0],
3840
11581
                                         tab->ref.key_buff,
3841
11582
                                         make_prev_keypart_map(tab->ref.key_parts),
3842
11583
                                         HA_READ_KEY_EXACT)))
3843
11584
  {
3844
11585
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3845
 
      return table->report_error(error);
3846
 
    return -1;
 
11586
      return report_error(table, error);
 
11587
    return -1; /* purecov: inspected */
3847
11588
  }
3848
 
 
3849
11589
  return 0;
3850
11590
}
3851
11591
 
 
11592
 
3852
11593
/**
3853
 
  This function is used when optimizing away order_st BY in
3854
 
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
 
11594
  This function is used when optimizing away ORDER BY in 
 
11595
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
3855
11596
*/
3856
 
int join_read_last_key(JoinTable *tab)
 
11597
  
 
11598
static int
 
11599
join_read_last_key(JOIN_TAB *tab)
3857
11600
{
3858
11601
  int error;
3859
 
  Table *table= tab->table;
 
11602
  TABLE *table= tab->table;
3860
11603
 
3861
 
  if (!table->cursor->inited)
3862
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
3863
 
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
11604
  if (!table->file->inited)
 
11605
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
11606
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
3864
11607
    return -1;
3865
 
  if ((error=table->cursor->index_read_last_map(table->record[0],
 
11608
  if ((error=table->file->index_read_last_map(table->record[0],
3866
11609
                                              tab->ref.key_buff,
3867
11610
                                              make_prev_keypart_map(tab->ref.key_parts))))
3868
11611
  {
3869
11612
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3870
 
      return table->report_error(error);
3871
 
    return -1;
 
11613
      return report_error(table, error);
 
11614
    return -1; /* purecov: inspected */
3872
11615
  }
3873
11616
  return 0;
3874
11617
}
3875
11618
 
3876
 
int join_no_more_records(READ_RECORD *)
 
11619
 
 
11620
        /* ARGSUSED */
 
11621
static int
 
11622
join_no_more_records(READ_RECORD *info __attribute__((unused)))
3877
11623
{
3878
11624
  return -1;
3879
11625
}
3880
11626
 
3881
 
int join_read_next_same_diff(READ_RECORD *info)
 
11627
static int
 
11628
join_read_next_same_diff(READ_RECORD *info)
3882
11629
{
3883
 
  Table *table= info->table;
3884
 
  JoinTable *tab=table->reginfo.join_tab;
 
11630
  TABLE *table= info->table;
 
11631
  JOIN_TAB *tab=table->reginfo.join_tab;
3885
11632
  if (tab->insideout_match_tab->found_match)
3886
11633
  {
3887
11634
    KEY *key= tab->table->key_info + tab->index;
3888
 
    do
 
11635
    do 
3889
11636
    {
3890
11637
      int error;
3891
11638
      /* Save index tuple from record to the buffer */
3892
11639
      key_copy(tab->insideout_buf, info->record, key, 0);
3893
11640
 
3894
 
      if ((error=table->cursor->index_next_same(table->record[0],
 
11641
      if ((error=table->file->index_next_same(table->record[0],
3895
11642
                                              tab->ref.key_buff,
3896
11643
                                              tab->ref.key_length)))
3897
11644
      {
3898
11645
        if (error != HA_ERR_END_OF_FILE)
3899
 
          return table->report_error(error);
 
11646
          return report_error(table, error);
3900
11647
        table->status= STATUS_GARBAGE;
3901
11648
        return -1;
3902
11649
      }
3903
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11650
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
3904
11651
                      tab->insideout_buf, key->key_length));
3905
11652
    tab->insideout_match_tab->found_match= 0;
3906
11653
    return 0;
3909
11656
    return join_read_next_same(info);
3910
11657
}
3911
11658
 
3912
 
int join_read_next_same(READ_RECORD *info)
 
11659
static int
 
11660
join_read_next_same(READ_RECORD *info)
3913
11661
{
3914
11662
  int error;
3915
 
  Table *table= info->table;
3916
 
  JoinTable *tab=table->reginfo.join_tab;
 
11663
  TABLE *table= info->table;
 
11664
  JOIN_TAB *tab=table->reginfo.join_tab;
3917
11665
 
3918
 
  if ((error=table->cursor->index_next_same(table->record[0],
 
11666
  if ((error=table->file->index_next_same(table->record[0],
3919
11667
                                          tab->ref.key_buff,
3920
11668
                                          tab->ref.key_length)))
3921
11669
  {
3922
11670
    if (error != HA_ERR_END_OF_FILE)
3923
 
      return table->report_error(error);
 
11671
      return report_error(table, error);
3924
11672
    table->status= STATUS_GARBAGE;
3925
11673
    return -1;
3926
11674
  }
3927
 
 
3928
11675
  return 0;
3929
11676
}
3930
11677
 
3931
 
int join_read_prev_same(READ_RECORD *info)
 
11678
 
 
11679
static int
 
11680
join_read_prev_same(READ_RECORD *info)
3932
11681
{
3933
11682
  int error;
3934
 
  Table *table= info->table;
3935
 
  JoinTable *tab=table->reginfo.join_tab;
 
11683
  TABLE *table= info->table;
 
11684
  JOIN_TAB *tab=table->reginfo.join_tab;
3936
11685
 
3937
 
  if ((error=table->cursor->index_prev(table->record[0])))
3938
 
    return table->report_error(error);
 
11686
  if ((error=table->file->index_prev(table->record[0])))
 
11687
    return report_error(table, error);
3939
11688
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3940
11689
                      tab->ref.key_length))
3941
11690
  {
3945
11694
  return error;
3946
11695
}
3947
11696
 
3948
 
int join_init_quick_read_record(JoinTable *tab)
 
11697
 
 
11698
static int
 
11699
join_init_quick_read_record(JOIN_TAB *tab)
3949
11700
{
3950
11701
  if (test_if_quick_select(tab) == -1)
3951
11702
    return -1;                                  /* No possible records */
3952
11703
  return join_init_read_record(tab);
3953
11704
}
3954
11705
 
 
11706
 
3955
11707
int rr_sequential(READ_RECORD *info);
3956
 
int init_read_record_seq(JoinTable *tab)
 
11708
int init_read_record_seq(JOIN_TAB *tab)
3957
11709
{
3958
11710
  tab->read_record.read_record= rr_sequential;
3959
 
  if (tab->read_record.cursor->ha_rnd_init(1))
 
11711
  if (tab->read_record.file->ha_rnd_init(1))
3960
11712
    return 1;
3961
11713
  return (*tab->read_record.read_record)(&tab->read_record);
3962
11714
}
3963
11715
 
3964
 
int test_if_quick_select(JoinTable *tab)
 
11716
static int
 
11717
test_if_quick_select(JOIN_TAB *tab)
3965
11718
{
3966
11719
  delete tab->select->quick;
3967
 
  tab->select->quick= 0;
3968
 
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3969
 
                                        (table_map) 0, HA_POS_ERROR, 0, false);
 
11720
  tab->select->quick=0;
 
11721
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
 
11722
                                        (table_map) 0, HA_POS_ERROR, 0,
 
11723
                                        false);
3970
11724
}
3971
11725
 
3972
 
int join_init_read_record(JoinTable *tab)
 
11726
 
 
11727
static int
 
11728
join_init_read_record(JOIN_TAB *tab)
3973
11729
{
3974
11730
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3975
11731
    return 1;
3976
 
  init_read_record(&tab->read_record, tab->join->session, tab->table,
 
11732
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
3977
11733
                   tab->select,1,1);
3978
11734
  return (*tab->read_record.read_record)(&tab->read_record);
3979
11735
}
3980
11736
 
3981
 
int join_read_first(JoinTable *tab)
 
11737
 
 
11738
static int
 
11739
join_read_first(JOIN_TAB *tab)
3982
11740
{
3983
11741
  int error;
3984
 
  Table *table=tab->table;
3985
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11742
  TABLE *table=tab->table;
 
11743
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
3986
11744
      !table->no_keyread)
3987
11745
  {
3988
11746
    table->key_read=1;
3989
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11747
    table->file->extra(HA_EXTRA_KEYREAD);
3990
11748
  }
3991
11749
  tab->table->status=0;
3992
11750
  tab->read_record.table=table;
3993
 
  tab->read_record.cursor=table->cursor;
 
11751
  tab->read_record.file=table->file;
3994
11752
  tab->read_record.index=tab->index;
3995
11753
  tab->read_record.record=table->record[0];
3996
11754
  if (tab->insideout_match_tab)
4005
11763
    tab->read_record.do_insideout_scan= 0;
4006
11764
  }
4007
11765
 
4008
 
  if (!table->cursor->inited)
4009
 
    table->cursor->ha_index_init(tab->index, tab->sorted);
4010
 
  if ((error=tab->table->cursor->index_first(tab->table->record[0])))
 
11766
  if (!table->file->inited)
 
11767
    table->file->ha_index_init(tab->index, tab->sorted);
 
11768
  if ((error=tab->table->file->index_first(tab->table->record[0])))
4011
11769
  {
4012
11770
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
4013
 
      table->report_error(error);
 
11771
      report_error(table, error);
4014
11772
    return -1;
4015
11773
  }
4016
 
 
4017
11774
  return 0;
4018
11775
}
4019
11776
 
4020
 
int join_read_next_different(READ_RECORD *info)
 
11777
 
 
11778
static int
 
11779
join_read_next_different(READ_RECORD *info)
4021
11780
{
4022
 
  JoinTable *tab= info->do_insideout_scan;
 
11781
  JOIN_TAB *tab= info->do_insideout_scan;
4023
11782
  if (tab->insideout_match_tab->found_match)
4024
11783
  {
4025
11784
    KEY *key= tab->table->key_info + tab->index;
4026
 
    do
 
11785
    do 
4027
11786
    {
4028
11787
      int error;
4029
11788
      /* Save index tuple from record to the buffer */
4030
11789
      key_copy(tab->insideout_buf, info->record, key, 0);
4031
11790
 
4032
 
      if ((error=info->cursor->index_next(info->record)))
4033
 
        return info->table->report_error(error);
4034
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
 
11791
      if ((error=info->file->index_next(info->record)))
 
11792
        return report_error(info->table, error);
 
11793
      
 
11794
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
4035
11795
                      tab->insideout_buf, key->key_length));
4036
11796
    tab->insideout_match_tab->found_match= 0;
4037
11797
    return 0;
4040
11800
    return join_read_next(info);
4041
11801
}
4042
11802
 
4043
 
int join_read_next(READ_RECORD *info)
 
11803
 
 
11804
static int
 
11805
join_read_next(READ_RECORD *info)
4044
11806
{
4045
11807
  int error;
4046
 
  if ((error=info->cursor->index_next(info->record)))
4047
 
    return info->table->report_error(error);
 
11808
  if ((error=info->file->index_next(info->record)))
 
11809
    return report_error(info->table, error);
4048
11810
  return 0;
4049
11811
}
4050
11812
 
4051
 
int join_read_last(JoinTable *tab)
 
11813
 
 
11814
static int
 
11815
join_read_last(JOIN_TAB *tab)
4052
11816
{
4053
 
  Table *table=tab->table;
 
11817
  TABLE *table=tab->table;
4054
11818
  int error;
4055
 
  if (!table->key_read && table->covering_keys.test(tab->index) &&
 
11819
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
4056
11820
      !table->no_keyread)
4057
11821
  {
4058
11822
    table->key_read=1;
4059
 
    table->cursor->extra(HA_EXTRA_KEYREAD);
 
11823
    table->file->extra(HA_EXTRA_KEYREAD);
4060
11824
  }
4061
11825
  tab->table->status=0;
4062
11826
  tab->read_record.read_record=join_read_prev;
4063
11827
  tab->read_record.table=table;
4064
 
  tab->read_record.cursor=table->cursor;
 
11828
  tab->read_record.file=table->file;
4065
11829
  tab->read_record.index=tab->index;
4066
11830
  tab->read_record.record=table->record[0];
4067
 
  if (!table->cursor->inited)
4068
 
    table->cursor->ha_index_init(tab->index, 1);
4069
 
  if ((error= tab->table->cursor->index_last(tab->table->record[0])))
4070
 
    return table->report_error(error);
4071
 
 
 
11831
  if (!table->file->inited)
 
11832
    table->file->ha_index_init(tab->index, 1);
 
11833
  if ((error= tab->table->file->index_last(tab->table->record[0])))
 
11834
    return report_error(table, error);
4072
11835
  return 0;
4073
11836
}
4074
11837
 
4075
 
int join_read_prev(READ_RECORD *info)
 
11838
 
 
11839
static int
 
11840
join_read_prev(READ_RECORD *info)
4076
11841
{
4077
11842
  int error;
4078
 
  if ((error= info->cursor->index_prev(info->record)))
4079
 
    return info->table->report_error(error);
4080
 
 
 
11843
  if ((error= info->file->index_prev(info->record)))
 
11844
    return report_error(info->table, error);
4081
11845
  return 0;
4082
11846
}
4083
11847
 
4084
11848
/**
4085
11849
  Reading of key with key reference and one part that may be NULL.
4086
11850
*/
4087
 
int join_read_always_key_or_null(JoinTable *tab)
 
11851
 
 
11852
int
 
11853
join_read_always_key_or_null(JOIN_TAB *tab)
4088
11854
{
4089
11855
  int res;
4090
11856
 
4098
11864
  return safe_index_read(tab);
4099
11865
}
4100
11866
 
4101
 
int join_read_next_same_or_null(READ_RECORD *info)
 
11867
 
 
11868
int
 
11869
join_read_next_same_or_null(READ_RECORD *info)
4102
11870
{
4103
11871
  int error;
4104
11872
  if ((error= join_read_next_same(info)) >= 0)
4105
11873
    return error;
4106
 
  JoinTable *tab= info->table->reginfo.join_tab;
 
11874
  JOIN_TAB *tab= info->table->reginfo.join_tab;
4107
11875
 
4108
11876
  /* Test if we have already done a read after null key */
4109
11877
  if (*tab->ref.null_ref_key)
4112
11880
  return safe_index_read(tab);                  // then read null keys
4113
11881
}
4114
11882
 
4115
 
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *, bool end_of_records)
 
11883
 
 
11884
/*****************************************************************************
 
11885
  DESCRIPTION
 
11886
    Functions that end one nested loop iteration. Different functions
 
11887
    are used to support GROUP BY clause and to redirect records
 
11888
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
11889
    network client.
 
11890
 
 
11891
  RETURN VALUES
 
11892
    NESTED_LOOP_OK           - the record has been successfully handled
 
11893
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
11894
                               was detected
 
11895
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
11896
                               the record
 
11897
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
11898
                               additionally, the nested loop produced the
 
11899
                               number of rows specified in the LIMIT clause
 
11900
                               for the query
 
11901
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
11902
                               additionally, there is a cursor and the nested
 
11903
                               loop algorithm produced the number of rows
 
11904
                               that is specified for current cursor fetch
 
11905
                               operation.
 
11906
   All return values except NESTED_LOOP_OK abort the nested loop.
 
11907
*****************************************************************************/
 
11908
 
 
11909
/* ARGSUSED */
 
11910
static enum_nested_loop_state
 
11911
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11912
         bool end_of_records)
 
11913
{
 
11914
  if (!end_of_records)
 
11915
  {
 
11916
    int error;
 
11917
    if (join->having && join->having->val_int() == 0)
 
11918
      return(NESTED_LOOP_OK);               // Didn't match having
 
11919
    error=0;
 
11920
    if (join->do_send_rows)
 
11921
      error=join->result->send_data(*join->fields);
 
11922
    if (error)
 
11923
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
 
11924
    if (++join->send_records >= join->unit->select_limit_cnt &&
 
11925
        join->do_send_rows)
 
11926
    {
 
11927
      if (join->select_options & OPTION_FOUND_ROWS)
 
11928
      {
 
11929
        JOIN_TAB *jt=join->join_tab;
 
11930
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
11931
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
11932
            !(jt->select && jt->select->quick) &&
 
11933
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
11934
            (jt->ref.key < 0))
 
11935
        {
 
11936
          /* Join over all rows in table;  Return number of found rows */
 
11937
          TABLE *table=jt->table;
 
11938
 
 
11939
          join->select_options ^= OPTION_FOUND_ROWS;
 
11940
          if (table->sort.record_pointers ||
 
11941
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
11942
          {
 
11943
            /* Using filesort */
 
11944
            join->send_records= table->sort.found_records;
 
11945
          }
 
11946
          else
 
11947
          {
 
11948
            table->file->info(HA_STATUS_VARIABLE);
 
11949
            join->send_records= table->file->stats.records;
 
11950
          }
 
11951
        }
 
11952
        else 
 
11953
        {
 
11954
          join->do_send_rows= 0;
 
11955
          if (join->unit->fake_select_lex)
 
11956
            join->unit->fake_select_lex->select_limit= 0;
 
11957
          return(NESTED_LOOP_OK);
 
11958
        }
 
11959
      }
 
11960
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
11961
    }
 
11962
    else if (join->send_records >= join->fetch_limit)
 
11963
    {
 
11964
      /*
 
11965
        There is a server side cursor and all rows for
 
11966
        this fetch request are sent.
 
11967
      */
 
11968
      return(NESTED_LOOP_CURSOR_LIMIT);
 
11969
    }
 
11970
  }
 
11971
 
 
11972
  return(NESTED_LOOP_OK);
 
11973
}
 
11974
 
 
11975
 
 
11976
        /* ARGSUSED */
 
11977
enum_nested_loop_state
 
11978
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
11979
               bool end_of_records)
4116
11980
{
4117
11981
  int idx= -1;
4118
11982
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
4120
11984
  if (!join->first_record || end_of_records ||
4121
11985
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4122
11986
  {
4123
 
    if (join->first_record ||
 
11987
    if (join->first_record || 
4124
11988
        (end_of_records && !join->group && !join->group_optimized_away))
4125
11989
    {
4126
11990
      if (idx < (int) join->send_group_parts)
4127
11991
      {
4128
 
        int error=0;
4129
 
        {
4130
 
          if (!join->first_record)
4131
 
          {
4132
 
                  List_iterator_fast<Item> it(*join->fields);
4133
 
                  Item *item;
4134
 
            /* No matching rows for group function */
4135
 
            join->clear();
 
11992
        int error=0;
 
11993
        {
 
11994
          if (!join->first_record)
 
11995
          {
 
11996
            List_iterator_fast<Item> it(*join->fields);
 
11997
            Item *item;
 
11998
            /* No matching rows for group function */
 
11999
            join->clear();
4136
12000
 
4137
12001
            while ((item= it++))
4138
12002
              item->no_rows_in_result();
4139
 
          }
4140
 
          if (join->having && join->having->val_int() == 0)
4141
 
            error= -1;                          // Didn't satisfy having
4142
 
          else
4143
 
          {
4144
 
            if (join->do_send_rows)
4145
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
4146
 
            join->send_records++;
4147
 
          }
4148
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
4149
 
          {
4150
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
4151
 
              error= 1;
4152
 
          }
4153
 
        }
4154
 
        if (error > 0)
4155
 
          return(NESTED_LOOP_ERROR);
4156
 
        if (end_of_records)
4157
 
          return(NESTED_LOOP_OK);
4158
 
        if (join->send_records >= join->unit->select_limit_cnt &&
4159
 
            join->do_send_rows)
4160
 
        {
4161
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
4162
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
4163
 
          join->do_send_rows=0;
4164
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
12003
          }
 
12004
          if (join->having && join->having->val_int() == 0)
 
12005
            error= -1;                          // Didn't satisfy having
 
12006
          else
 
12007
          {
 
12008
            if (join->do_send_rows)
 
12009
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
12010
            join->send_records++;
 
12011
          }
 
12012
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
12013
          {
 
12014
            if (join->rollup_send_data((uint) (idx+1)))
 
12015
              error= 1;
 
12016
          }
 
12017
        }
 
12018
        if (error > 0)
 
12019
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
12020
        if (end_of_records)
 
12021
          return(NESTED_LOOP_OK);
 
12022
        if (join->send_records >= join->unit->select_limit_cnt &&
 
12023
            join->do_send_rows)
 
12024
        {
 
12025
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
12026
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
12027
          join->do_send_rows=0;
 
12028
          join->unit->select_limit_cnt = HA_POS_ERROR;
4165
12029
        }
4166
12030
        else if (join->send_records >= join->fetch_limit)
4167
12031
        {
4180
12044
    else
4181
12045
    {
4182
12046
      if (end_of_records)
4183
 
        return(NESTED_LOOP_OK);
 
12047
        return(NESTED_LOOP_OK);
4184
12048
      join->first_record=1;
4185
 
      test_if_item_cache_changed(join->group_fields);
 
12049
      VOID(test_if_item_cache_changed(join->group_fields));
4186
12050
    }
4187
12051
    if (idx < (int) join->send_group_parts)
4188
12052
    {
4192
12056
      */
4193
12057
      copy_fields(&join->tmp_table_param);
4194
12058
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4195
 
        return(NESTED_LOOP_ERROR);
 
12059
        return(NESTED_LOOP_ERROR);
4196
12060
      return(ok_code);
4197
12061
    }
4198
12062
  }
4201
12065
  return(NESTED_LOOP_OK);
4202
12066
}
4203
12067
 
4204
 
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *, bool end_of_records)
4205
 
{
4206
 
  Table *table=join->tmp_table;
 
12068
 
 
12069
        /* ARGSUSED */
 
12070
enum_nested_loop_state
 
12071
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12072
          bool end_of_records)
 
12073
{
 
12074
  TABLE *table=join->tmp_table;
 
12075
 
 
12076
  if (join->thd->killed)                        // Aborted by user
 
12077
  {
 
12078
    join->thd->send_kill_message();
 
12079
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12080
  }
 
12081
  if (!end_of_records)
 
12082
  {
 
12083
    copy_fields(&join->tmp_table_param);
 
12084
    copy_funcs(join->tmp_table_param.items_to_copy);
 
12085
#ifdef TO_BE_DELETED
 
12086
    if (!table->uniques)                        // If not unique handling
 
12087
    {
 
12088
      /* Copy null values from group to row */
 
12089
      ORDER   *group;
 
12090
      for (group=table->group ; group ; group=group->next)
 
12091
      {
 
12092
        Item *item= *group->item;
 
12093
        if (item->maybe_null)
 
12094
        {
 
12095
          Field *field=item->get_tmp_table_field();
 
12096
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
 
12097
        }
 
12098
      }
 
12099
    }
 
12100
#endif
 
12101
    if (!join->having || join->having->val_int())
 
12102
    {
 
12103
      int error;
 
12104
      join->found_records++;
 
12105
      if ((error=table->file->ha_write_row(table->record[0])))
 
12106
      {
 
12107
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
12108
          goto end;
 
12109
        if (create_myisam_from_heap(join->thd, table,
 
12110
                                    join->tmp_table_param.start_recinfo,
 
12111
                                    &join->tmp_table_param.recinfo,
 
12112
                                    error, 1))
 
12113
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
12114
        table->s->uniques=0;                    // To ensure rows are the same
 
12115
      }
 
12116
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
 
12117
          join->do_send_rows)
 
12118
      {
 
12119
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
12120
          return(NESTED_LOOP_QUERY_LIMIT);
 
12121
        join->do_send_rows=0;
 
12122
        join->unit->select_limit_cnt = HA_POS_ERROR;
 
12123
        return(NESTED_LOOP_OK);
 
12124
      }
 
12125
    }
 
12126
  }
 
12127
end:
 
12128
  return(NESTED_LOOP_OK);
 
12129
}
 
12130
 
 
12131
/* ARGSUSED */
 
12132
/** Group by searching after group record and updating it if possible. */
 
12133
 
 
12134
static enum_nested_loop_state
 
12135
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12136
           bool end_of_records)
 
12137
{
 
12138
  TABLE *table=join->tmp_table;
 
12139
  ORDER   *group;
 
12140
  int     error;
 
12141
 
 
12142
  if (end_of_records)
 
12143
    return(NESTED_LOOP_OK);
 
12144
  if (join->thd->killed)                        // Aborted by user
 
12145
  {
 
12146
    join->thd->send_kill_message();
 
12147
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12148
  }
 
12149
 
 
12150
  join->found_records++;
 
12151
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12152
  /* Make a key of group index */
 
12153
  for (group=table->group ; group ; group=group->next)
 
12154
  {
 
12155
    Item *item= *group->item;
 
12156
    item->save_org_in_field(group->field);
 
12157
    /* Store in the used key if the field was 0 */
 
12158
    if (item->maybe_null)
 
12159
      group->buff[-1]= (char) group->field->is_null();
 
12160
  }
 
12161
  if (!table->file->index_read_map(table->record[1],
 
12162
                                   join->tmp_table_param.group_buff,
 
12163
                                   HA_WHOLE_KEY,
 
12164
                                   HA_READ_KEY_EXACT))
 
12165
  {                                             /* Update old record */
 
12166
    restore_record(table,record[1]);
 
12167
    update_tmptable_sum_func(join->sum_funcs,table);
 
12168
    if ((error=table->file->ha_update_row(table->record[1],
 
12169
                                          table->record[0])))
 
12170
    {
 
12171
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12172
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12173
    }
 
12174
    return(NESTED_LOOP_OK);
 
12175
  }
 
12176
 
 
12177
  /*
 
12178
    Copy null bits from group key to table
 
12179
    We can't copy all data as the key may have different format
 
12180
    as the row data (for example as with VARCHAR keys)
 
12181
  */
 
12182
  KEY_PART_INFO *key_part;
 
12183
  for (group=table->group,key_part=table->key_info[0].key_part;
 
12184
       group ;
 
12185
       group=group->next,key_part++)
 
12186
  {
 
12187
    if (key_part->null_bit)
 
12188
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
12189
  }
 
12190
  init_tmptable_sum_functions(join->sum_funcs);
 
12191
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12192
  if ((error=table->file->ha_write_row(table->record[0])))
 
12193
  {
 
12194
    if (create_myisam_from_heap(join->thd, table,
 
12195
                                join->tmp_table_param.start_recinfo,
 
12196
                                &join->tmp_table_param.recinfo,
 
12197
                                error, 0))
 
12198
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
12199
    /* Change method to update rows */
 
12200
    table->file->ha_index_init(0, 0);
 
12201
    join->join_tab[join->tables-1].next_select=end_unique_update;
 
12202
  }
 
12203
  join->send_records++;
 
12204
  return(NESTED_LOOP_OK);
 
12205
}
 
12206
 
 
12207
 
 
12208
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
12209
 
 
12210
static enum_nested_loop_state
 
12211
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12212
                  bool end_of_records)
 
12213
{
 
12214
  TABLE *table=join->tmp_table;
 
12215
  int     error;
 
12216
 
 
12217
  if (end_of_records)
 
12218
    return(NESTED_LOOP_OK);
 
12219
  if (join->thd->killed)                        // Aborted by user
 
12220
  {
 
12221
    join->thd->send_kill_message();
 
12222
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
12223
  }
 
12224
 
 
12225
  init_tmptable_sum_functions(join->sum_funcs);
 
12226
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
12227
  copy_funcs(join->tmp_table_param.items_to_copy);
 
12228
 
 
12229
  if (!(error=table->file->ha_write_row(table->record[0])))
 
12230
    join->send_records++;                       // New group
 
12231
  else
 
12232
  {
 
12233
    if ((int) table->file->get_dup_key(error) < 0)
 
12234
    {
 
12235
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12236
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12237
    }
 
12238
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
12239
    {
 
12240
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12241
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12242
    }
 
12243
    restore_record(table,record[1]);
 
12244
    update_tmptable_sum_func(join->sum_funcs,table);
 
12245
    if ((error=table->file->ha_update_row(table->record[1],
 
12246
                                          table->record[0])))
 
12247
    {
 
12248
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
12249
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
12250
    }
 
12251
  }
 
12252
  return(NESTED_LOOP_OK);
 
12253
}
 
12254
 
 
12255
 
 
12256
        /* ARGSUSED */
 
12257
enum_nested_loop_state
 
12258
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
 
12259
                bool end_of_records)
 
12260
{
 
12261
  TABLE *table=join->tmp_table;
4207
12262
  int     idx= -1;
4208
12263
 
4209
 
  if (join->session->killed)
 
12264
  if (join->thd->killed)
4210
12265
  {                                             // Aborted by user
4211
 
    join->session->send_kill_message();
4212
 
    return NESTED_LOOP_KILLED;
 
12266
    join->thd->send_kill_message();
 
12267
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
4213
12268
  }
4214
12269
  if (!join->first_record || end_of_records ||
4215
12270
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4219
12274
      int send_group_parts= join->send_group_parts;
4220
12275
      if (idx < send_group_parts)
4221
12276
      {
4222
 
        if (!join->first_record)
4223
 
        {
4224
 
          /* No matching rows for group function */
4225
 
          join->clear();
4226
 
        }
4227
 
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
4228
 
        if (!join->having || join->having->val_int())
4229
 
        {
4230
 
          int error= table->cursor->ha_write_row(table->record[0]);
4231
 
          if (error && create_myisam_from_heap(join->session, table,
4232
 
                                              join->tmp_table_param.start_recinfo,
 
12277
        if (!join->first_record)
 
12278
        {
 
12279
          /* No matching rows for group function */
 
12280
          join->clear();
 
12281
        }
 
12282
        copy_sum_funcs(join->sum_funcs,
 
12283
                       join->sum_funcs_end[send_group_parts]);
 
12284
        if (!join->having || join->having->val_int())
 
12285
        {
 
12286
          int error= table->file->ha_write_row(table->record[0]);
 
12287
          if (error && create_myisam_from_heap(join->thd, table,
 
12288
                                               join->tmp_table_param.start_recinfo,
4233
12289
                                                &join->tmp_table_param.recinfo,
4234
 
                                              error, 0))
4235
 
          return NESTED_LOOP_ERROR;
 
12290
                                               error, 0))
 
12291
            return(NESTED_LOOP_ERROR);
4236
12292
        }
4237
12293
        if (join->rollup.state != ROLLUP::STATE_NONE)
4238
 
        {
4239
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
4240
 
            return NESTED_LOOP_ERROR;
4241
 
        }
4242
 
        if (end_of_records)
4243
 
          return NESTED_LOOP_OK;
 
12294
        {
 
12295
          if (join->rollup_write_data((uint) (idx+1), table))
 
12296
            return(NESTED_LOOP_ERROR);
 
12297
        }
 
12298
        if (end_of_records)
 
12299
          return(NESTED_LOOP_OK);
4244
12300
      }
4245
12301
    }
4246
12302
    else
4247
12303
    {
4248
12304
      if (end_of_records)
4249
 
        return NESTED_LOOP_OK;
 
12305
        return(NESTED_LOOP_OK);
4250
12306
      join->first_record=1;
4251
 
      test_if_item_cache_changed(join->group_fields);
 
12307
      VOID(test_if_item_cache_changed(join->group_fields));
4252
12308
    }
4253
12309
    if (idx < (int) join->send_group_parts)
4254
12310
    {
4255
12311
      copy_fields(&join->tmp_table_param);
4256
12312
      copy_funcs(join->tmp_table_param.items_to_copy);
4257
12313
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4258
 
        return NESTED_LOOP_ERROR;
4259
 
      return NESTED_LOOP_OK;
 
12314
        return(NESTED_LOOP_ERROR);
 
12315
      return(NESTED_LOOP_OK);
4260
12316
    }
4261
12317
  }
4262
12318
  if (update_sum_func(join->sum_funcs))
4263
 
    return NESTED_LOOP_ERROR;
4264
 
  return NESTED_LOOP_OK;
 
12319
    return(NESTED_LOOP_ERROR);
 
12320
  return(NESTED_LOOP_OK);
4265
12321
}
4266
12322
 
 
12323
 
4267
12324
/*****************************************************************************
4268
12325
  Remove calculation with tables that aren't yet read. Remove also tests
4269
12326
  against fields that are read through key where the table is not a
4276
12333
  @return
4277
12334
    1 if right_item is used removable reference key on left_item
4278
12335
*/
4279
 
bool test_if_ref(Item_field *left_item,Item *right_item)
 
12336
 
 
12337
static bool test_if_ref(Item_field *left_item,Item *right_item)
4280
12338
{
4281
12339
  Field *field=left_item->field;
4282
12340
  // No need to change const test. We also have to keep tests on LEFT JOIN
4287
12345
    {
4288
12346
      right_item= right_item->real_item();
4289
12347
      if (right_item->type() == Item::FIELD_ITEM)
4290
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
12348
        return (field->eq_def(((Item_field *) right_item)->field));
4291
12349
      /* remove equalities injected by IN->EXISTS transformation */
4292
12350
      else if (right_item->type() == Item::CACHE_ITEM)
4293
12351
        return ((Item_cache *)right_item)->eq_def (field);
4294
12352
      if (right_item->const_item() && !(right_item->is_null()))
4295
12353
      {
4296
 
        /*
4297
 
          We can remove binary fields and numerical fields except float,
4298
 
          as float comparison isn't 100 % secure
4299
 
          We have to keep normal strings to be able to check for end spaces
 
12354
        /*
 
12355
          We can remove binary fields and numerical fields except float,
 
12356
          as float comparison isn't 100 % secure
 
12357
          We have to keep normal strings to be able to check for end spaces
4300
12358
 
4301
 
                sergefp: the above seems to be too restrictive. Counterexample:
4302
 
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
4303
 
                  insert into t100 values ('a'),('a ');
4304
 
                  explain select * from t100 where v='a';
4305
 
                The EXPLAIN shows 'using Where'. Running the query returns both
4306
 
                rows, so it seems there are no problems with endspace in the most
4307
 
                frequent case?
4308
 
        */
4309
 
        if (field->binary() &&
4310
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4311
 
            field->decimals() == 0)
4312
 
        {
4313
 
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
4314
 
        }
 
12359
          sergefp: the above seems to be too restrictive. Counterexample:
 
12360
            create table t100 (v varchar(10), key(v)) default charset=latin1;
 
12361
            insert into t100 values ('a'),('a ');
 
12362
            explain select * from t100 where v='a';
 
12363
          The EXPLAIN shows 'using Where'. Running the query returns both
 
12364
          rows, so it seems there are no problems with endspace in the most
 
12365
          frequent case?
 
12366
        */
 
12367
        if (field->binary() &&
 
12368
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
12369
            field->decimals() == 0)
 
12370
        {
 
12371
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
12372
        }
4315
12373
      }
4316
12374
    }
4317
12375
  }
4318
 
  return 0;
 
12376
  return 0;                                     // keep test
 
12377
}
 
12378
 
 
12379
/**
 
12380
   @brief Replaces an expression destructively inside the expression tree of
 
12381
   the WHERE clase.
 
12382
 
 
12383
   @note Because of current requirements for semijoin flattening, we do not
 
12384
   need to recurse here, hence this function will only examine the top-level
 
12385
   AND conditions. (see JOIN::prepare, comment above the line 
 
12386
   'if (do_materialize)'
 
12387
   
 
12388
   @param join The top-level query.
 
12389
   @param old_cond The expression to be replaced.
 
12390
   @param new_cond The expression to be substituted.
 
12391
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
 
12392
   the new expression.
 
12393
   @return <code>true</code> if there was an error, <code>false</code> if
 
12394
   successful.
 
12395
*/
 
12396
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
 
12397
                                       Item *new_cond, bool do_fix_fields)
 
12398
{
 
12399
  if (join->conds == old_cond) {
 
12400
    join->conds= new_cond;
 
12401
    if (do_fix_fields)
 
12402
      new_cond->fix_fields(join->thd, &join->conds);
 
12403
    return false;
 
12404
  }
 
12405
  
 
12406
  if (join->conds->type() == Item::COND_ITEM) {
 
12407
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
12408
    Item *item;
 
12409
    while ((item= li++))
 
12410
      if (item == old_cond) 
 
12411
      {
 
12412
        li.replace(new_cond);
 
12413
        if (do_fix_fields)
 
12414
          new_cond->fix_fields(join->thd, li.ref());
 
12415
        return false;
 
12416
      }
 
12417
  }
 
12418
 
 
12419
  return true;
4319
12420
}
4320
12421
 
4321
12422
/*
4322
12423
  Extract a condition that can be checked after reading given table
4323
 
 
 
12424
  
4324
12425
  SYNOPSIS
4325
12426
    make_cond_for_table()
4326
12427
      cond         Condition to analyze
4327
12428
      tables       Tables for which "current field values" are available
4328
 
      used_table   Table that we're extracting the condition for (may
 
12429
      used_table   Table that we're extracting the condition for (may 
4329
12430
                   also include PSEUDO_TABLE_BITS
4330
12431
 
4331
12432
  DESCRIPTION
4335
12436
 
4336
12437
    The function assumes that
4337
12438
      - Constant parts of the condition has already been checked.
4338
 
      - Condition that could be checked for tables in 'tables' has already
 
12439
      - Condition that could be checked for tables in 'tables' has already 
4339
12440
        been checked.
4340
 
 
 
12441
        
4341
12442
    The function takes into account that some parts of the condition are
4342
12443
    guaranteed to be true by employed 'ref' access methods (the code that
4343
12444
    does this is located at the end, search down for "EQ_FUNC").
4344
12445
 
4345
12446
 
4346
 
  SEE ALSO
 
12447
  SEE ALSO 
4347
12448
    make_cond_for_info_schema uses similar algorithm
4348
12449
 
4349
12450
  RETURN
4350
12451
    Extracted condition
4351
12452
*/
4352
 
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
 
12453
 
 
12454
static COND *
 
12455
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
 
12456
                    bool exclude_expensive_cond)
4353
12457
{
4354
12458
  if (used_table && !(cond->used_tables() & used_table) &&
4355
 
    /*
4356
 
      Exclude constant conditions not checked at optimization time if
4357
 
      the table we are pushing conditions to is the first one.
4358
 
      As a result, such conditions are not considered as already checked
4359
 
      and will be checked at execution time, attached to the first table.
4360
 
    */
4361
 
    !((used_table & 1) && cond->is_expensive()))
 
12459
      /*
 
12460
        Exclude constant conditions not checked at optimization time if
 
12461
        the table we are pushing conditions to is the first one.
 
12462
        As a result, such conditions are not considered as already checked
 
12463
        and will be checked at execution time, attached to the first table.
 
12464
      */
 
12465
      !((used_table & 1) && cond->is_expensive()))
4362
12466
    return (COND*) 0;                           // Already checked
4363
12467
  if (cond->type() == Item::COND_ITEM)
4364
12468
  {
4367
12471
      /* Create new top level AND item */
4368
12472
      Item_cond_and *new_cond=new Item_cond_and;
4369
12473
      if (!new_cond)
4370
 
        return (COND*) 0;
 
12474
        return (COND*) 0;                       // OOM /* purecov: inspected */
4371
12475
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4372
12476
      Item *item;
4373
12477
      while ((item=li++))
4374
12478
      {
4375
 
        Item *fix= make_cond_for_table(item,tables,used_table,
4376
 
                                            exclude_expensive_cond);
4377
 
        if (fix)
4378
 
          new_cond->argument_list()->push_back(fix);
 
12479
        Item *fix=make_cond_for_table(item,tables,used_table,
 
12480
                                      exclude_expensive_cond);
 
12481
        if (fix)
 
12482
          new_cond->argument_list()->push_back(fix);
4379
12483
      }
4380
 
      switch (new_cond->argument_list()->elements) 
4381
 
      {
4382
 
        case 0:
4383
 
          return (COND*) 0;                     // Always true
4384
 
        case 1:
4385
 
          return new_cond->argument_list()->head();
4386
 
        default:
4387
 
          /*
4388
 
            Item_cond_and do not need fix_fields for execution, its parameters
4389
 
            are fixed or do not need fix_fields, too
4390
 
          */
4391
 
          new_cond->quick_fix_field();
4392
 
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4393
 
          return new_cond;
 
12484
      switch (new_cond->argument_list()->elements) {
 
12485
      case 0:
 
12486
        return (COND*) 0;                       // Always true
 
12487
      case 1:
 
12488
        return new_cond->argument_list()->head();
 
12489
      default:
 
12490
        /*
 
12491
          Item_cond_and do not need fix_fields for execution, its parameters
 
12492
          are fixed or do not need fix_fields, too
 
12493
        */
 
12494
        new_cond->quick_fix_field();
 
12495
        new_cond->used_tables_cache=
 
12496
          ((Item_cond_and*) cond)->used_tables_cache &
 
12497
          tables;
 
12498
        return new_cond;
4394
12499
      }
4395
12500
    }
4396
12501
    else
4397
12502
    {                                           // Or list
4398
12503
      Item_cond_or *new_cond=new Item_cond_or;
4399
12504
      if (!new_cond)
4400
 
        return (COND*) 0;
 
12505
        return (COND*) 0;                       // OOM /* purecov: inspected */
4401
12506
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4402
12507
      Item *item;
4403
12508
      while ((item=li++))
4404
12509
      {
4405
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4406
 
        if (!fix)
4407
 
          return (COND*) 0;                     // Always true
4408
 
        new_cond->argument_list()->push_back(fix);
 
12510
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
12511
        if (!fix)
 
12512
          return (COND*) 0;                     // Always true
 
12513
        new_cond->argument_list()->push_back(fix);
4409
12514
      }
4410
12515
      /*
4411
 
        Item_cond_and do not need fix_fields for execution, its parameters
4412
 
        are fixed or do not need fix_fields, too
 
12516
        Item_cond_and do not need fix_fields for execution, its parameters
 
12517
        are fixed or do not need fix_fields, too
4413
12518
      */
4414
12519
      new_cond->quick_fix_field();
4415
12520
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4434
12539
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4435
12540
    return cond;                                // Not boolean op
4436
12541
 
4437
 
  /*
 
12542
  /* 
4438
12543
    Remove equalities that are guaranteed to be true by use of 'ref' access
4439
12544
    method
4440
12545
  */
4442
12547
  {
4443
12548
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4444
12549
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4445
 
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
 
12550
    if (left_item->type() == Item::FIELD_ITEM &&
 
12551
        test_if_ref((Item_field*) left_item,right_item))
4446
12552
    {
4447
12553
      cond->marker=3;                   // Checked when read
4448
12554
      return (COND*) 0;
4449
12555
    }
4450
 
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
 
12556
    if (right_item->type() == Item::FIELD_ITEM &&
 
12557
        test_if_ref((Item_field*) right_item,left_item))
4451
12558
    {
4452
12559
      cond->marker=3;                   // Checked when read
4453
12560
      return (COND*) 0;
4457
12564
  return cond;
4458
12565
}
4459
12566
 
4460
 
static Item *part_of_refkey(Table *table,Field *field)
 
12567
 
 
12568
static Item *
 
12569
part_of_refkey(TABLE *table,Field *field)
4461
12570
{
4462
12571
  if (!table->reginfo.join_tab)
4463
12572
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4464
12573
 
4465
 
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
 
12574
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
4466
12575
  if (ref_parts)
4467
12576
  {
4468
12577
    KEY_PART_INFO *key_part=
4469
12578
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4470
 
    uint32_t part;
 
12579
    uint part;
4471
12580
 
4472
12581
    for (part=0 ; part < ref_parts ; part++)
4473
12582
    {
4483
12592
  return (Item*) 0;
4484
12593
}
4485
12594
 
 
12595
 
4486
12596
/**
4487
 
  Test if one can use the key to resolve order_st BY.
 
12597
  Test if one can use the key to resolve ORDER BY.
4488
12598
 
4489
12599
  @param order                 Sort order
4490
12600
  @param table                 Table to sort
4503
12613
  @retval
4504
12614
    -1   Reverse key can be used
4505
12615
*/
4506
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
12616
 
 
12617
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
 
12618
                                uint *used_key_parts)
4507
12619
{
4508
 
  KEY_PART_INFO *key_part= NULL;
4509
 
  KEY_PART_INFO *key_part_end= NULL;
4510
 
  key_part= table->key_info[idx].key_part;
4511
 
  key_part_end= key_part + table->key_info[idx].key_parts;
 
12620
  KEY_PART_INFO *key_part,*key_part_end;
 
12621
  key_part=table->key_info[idx].key_part;
 
12622
  key_part_end=key_part+table->key_info[idx].key_parts;
4512
12623
  key_part_map const_key_parts=table->const_key_parts[idx];
4513
 
  int reverse= 0;
 
12624
  int reverse=0;
4514
12625
  bool on_primary_key= false;
4515
12626
 
4516
12627
  for (; order ; order=order->next, const_key_parts>>=1)
4520
12631
 
4521
12632
    /*
4522
12633
      Skip key parts that are constants in the WHERE clause.
4523
 
      These are already skipped in the order_st BY by const_expression_in_where()
 
12634
      These are already skipped in the ORDER BY by const_expression_in_where()
4524
12635
    */
4525
12636
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4526
 
      key_part++;
 
12637
      key_part++; 
4527
12638
 
4528
12639
    if (key_part == key_part_end)
4529
12640
    {
4530
 
      /*
 
12641
      /* 
4531
12642
        We are at the end of the key. Check if the engine has the primary
4532
12643
        key as a suffix to the secondary keys. If it has continue to check
4533
12644
        the primary key as a suffix.
4534
12645
      */
4535
12646
      if (!on_primary_key &&
4536
 
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
 
12647
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
4537
12648
          table->s->primary_key != MAX_KEY)
4538
12649
      {
4539
12650
        on_primary_key= true;
4542
12653
        const_key_parts=table->const_key_parts[table->s->primary_key];
4543
12654
 
4544
12655
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4545
 
          key_part++;
 
12656
          key_part++; 
4546
12657
        /*
4547
12658
         The primary and secondary key parts were all const (i.e. there's
4548
12659
         one row).  The sorting doesn't matter.
4566
12677
    key_part++;
4567
12678
  }
4568
12679
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4569
 
    (uint32_t) (key_part - table->key_info[idx].key_part);
4570
 
  if (reverse == -1 && !(table->cursor->index_flags(idx, *used_key_parts-1, 1) &
 
12680
    (uint) (key_part - table->key_info[idx].key_part);
 
12681
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
4571
12682
                         HA_READ_PREV))
4572
12683
    reverse= 0;                                 // Index can't be used
4573
12684
  return(reverse);
4574
12685
}
4575
12686
 
 
12687
 
 
12688
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
 
12689
{
 
12690
  uint min_length= (uint) ~0;
 
12691
  uint best= MAX_KEY;
 
12692
  if (!usable_keys->is_clear_all())
 
12693
  {
 
12694
    for (uint nr=0; nr < table->s->keys ; nr++)
 
12695
    {
 
12696
      if (usable_keys->is_set(nr))
 
12697
      {
 
12698
        if (table->key_info[nr].key_length < min_length)
 
12699
        {
 
12700
          min_length=table->key_info[nr].key_length;
 
12701
          best=nr;
 
12702
        }
 
12703
      }
 
12704
    }
 
12705
  }
 
12706
  return best;
 
12707
}
 
12708
 
4576
12709
/**
4577
12710
  Test if a second key is the subkey of the first one.
4578
12711
 
4588
12721
  @retval
4589
12722
    0   no sub key
4590
12723
*/
4591
 
inline bool is_subkey(KEY_PART_INFO *key_part,
4592
 
                      KEY_PART_INFO *ref_key_part,
4593
 
                      KEY_PART_INFO *ref_key_part_end)
 
12724
 
 
12725
inline bool 
 
12726
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
 
12727
          KEY_PART_INFO *ref_key_part_end)
4594
12728
{
4595
12729
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4596
 
    if (! key_part->field->eq(ref_key_part->field))
 
12730
    if (!key_part->field->eq(ref_key_part->field))
4597
12731
      return 0;
4598
12732
  return 1;
4599
12733
}
4609
12743
    - MAX_KEY                   If we can't use other key
4610
12744
    - the number of found key   Otherwise
4611
12745
*/
4612
 
static uint32_t test_if_subkey(order_st *order,
4613
 
                               Table *table,
4614
 
                               uint32_t ref,
4615
 
                               uint32_t ref_key_parts,
4616
 
                               const key_map *usable_keys)
 
12746
 
 
12747
static uint
 
12748
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
 
12749
               const key_map *usable_keys)
4617
12750
{
4618
 
  uint32_t nr;
4619
 
  uint32_t min_length= UINT32_MAX;
4620
 
  uint32_t best= MAX_KEY;
4621
 
  uint32_t not_used;
 
12751
  uint nr;
 
12752
  uint min_length= (uint) ~0;
 
12753
  uint best= MAX_KEY;
 
12754
  uint not_used;
4622
12755
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
4623
12756
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
4624
12757
 
4625
12758
  for (nr= 0 ; nr < table->s->keys ; nr++)
4626
12759
  {
4627
 
    if (usable_keys->test(nr) &&
 
12760
    if (usable_keys->is_set(nr) &&
4628
12761
        table->key_info[nr].key_length < min_length &&
4629
12762
        table->key_info[nr].key_parts >= ref_key_parts &&
4630
12763
        is_subkey(table->key_info[nr].key_part, ref_key_part,
4638
12771
  return best;
4639
12772
}
4640
12773
 
 
12774
 
4641
12775
/**
4642
12776
  Check if GROUP BY/DISTINCT can be optimized away because the set is
4643
12777
  already known to be distinct.
4656
12790
    of the table are referenced by a list : either the select list
4657
12791
    through find_field_in_item_list or GROUP BY list through
4658
12792
    find_field_in_order_list.
4659
 
    If the above holds and the key parts cannot contain NULLs then we
 
12793
    If the above holds and the key parts cannot contain NULLs then we 
4660
12794
    can safely remove the GROUP BY/DISTINCT,
4661
12795
    as no result set can be more distinct than an unique key.
4662
12796
 
4669
12803
  @retval
4670
12804
    0                    not found.
4671
12805
*/
4672
 
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
 
12806
 
 
12807
static bool
 
12808
list_contains_unique_index(TABLE *table,
 
12809
                          bool (*find_func) (Field *, void *), void *data)
4673
12810
{
4674
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
12811
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
4675
12812
  {
4676
12813
    if (keynr == table->s->primary_key ||
4677
12814
         (table->key_info[keynr].flags & HA_NOSAME))
4678
12815
    {
4679
12816
      KEY *keyinfo= table->key_info + keynr;
4680
 
      KEY_PART_INFO *key_part= NULL;
4681
 
      KEY_PART_INFO *key_part_end= NULL;
 
12817
      KEY_PART_INFO *key_part, *key_part_end;
4682
12818
 
4683
12819
      for (key_part=keyinfo->key_part,
4684
12820
           key_part_end=key_part+ keyinfo->key_parts;
4685
12821
           key_part < key_part_end;
4686
12822
           key_part++)
4687
12823
      {
4688
 
        if (key_part->field->maybe_null() ||
4689
 
            ! find_func(key_part->field, data))
 
12824
        if (key_part->field->maybe_null() || 
 
12825
            !find_func(key_part->field, data))
4690
12826
          break;
4691
12827
      }
4692
12828
      if (key_part == key_part_end)
4696
12832
  return 0;
4697
12833
}
4698
12834
 
 
12835
 
4699
12836
/**
4700
12837
  Helper function for list_contains_unique_index.
4701
 
  Find a field reference in a list of order_st structures.
 
12838
  Find a field reference in a list of ORDER structures.
4702
12839
  Finds a direct reference of the Field in the list.
4703
12840
 
4704
12841
  @param field                The field to search for.
4705
 
  @param data                 order_st *.The list to search in
 
12842
  @param data                 ORDER *.The list to search in
4706
12843
 
4707
12844
  @retval
4708
12845
    1                    found
4709
12846
  @retval
4710
12847
    0                    not found.
4711
12848
*/
4712
 
bool find_field_in_order_list (Field *field, void *data)
 
12849
 
 
12850
static bool
 
12851
find_field_in_order_list (Field *field, void *data)
4713
12852
{
4714
 
  order_st *group= (order_st *) data;
 
12853
  ORDER *group= (ORDER *) data;
4715
12854
  bool part_found= 0;
4716
 
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
12855
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4717
12856
  {
4718
12857
    Item *item= (*tmp_group->item)->real_item();
4719
12858
    if (item->type() == Item::FIELD_ITEM &&
4726
12865
  return part_found;
4727
12866
}
4728
12867
 
 
12868
 
4729
12869
/**
4730
12870
  Helper function for list_contains_unique_index.
4731
12871
  Find a field reference in a dynamic list of Items.
4739
12879
  @retval
4740
12880
    0                    not found.
4741
12881
*/
4742
 
bool find_field_in_item_list (Field *field, void *data)
 
12882
 
 
12883
static bool
 
12884
find_field_in_item_list (Field *field, void *data)
4743
12885
{
4744
12886
  List<Item> *fields= (List<Item> *) data;
4745
12887
  bool part_found= 0;
4758
12900
  return part_found;
4759
12901
}
4760
12902
 
 
12903
 
4761
12904
/**
4762
 
  Test if we can skip the order_st BY by using an index.
 
12905
  Test if we can skip the ORDER BY by using an index.
4763
12906
 
4764
12907
  SYNOPSIS
4765
12908
    test_if_skip_sort_order()
4769
12912
      no_changes
4770
12913
      map
4771
12914
 
4772
 
  If we can use an index, the JoinTable / tab->select struct
 
12915
  If we can use an index, the JOIN_TAB / tab->select struct
4773
12916
  is changed to use the index.
4774
12917
 
4775
12918
  The index must cover all fields in <order>, or it will not be considered.
4776
12919
 
4777
12920
  @todo
4778
 
    - sergeyp: Results of all index merge selects actually are ordered
 
12921
    - sergeyp: Results of all index merge selects actually are ordered 
4779
12922
    by clustered PK values.
4780
12923
 
4781
12924
  @retval
4783
12926
  @retval
4784
12927
    1    We can use an index.
4785
12928
*/
4786
 
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
12929
 
 
12930
static bool
 
12931
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
 
12932
                        bool no_changes, const key_map *map)
4787
12933
{
4788
 
  int32_t ref_key;
4789
 
  uint32_t ref_key_parts;
 
12934
  int ref_key;
 
12935
  uint ref_key_parts;
4790
12936
  int order_direction;
4791
 
  uint32_t used_key_parts;
4792
 
  Table *table=tab->table;
 
12937
  uint used_key_parts;
 
12938
  TABLE *table=tab->table;
4793
12939
  SQL_SELECT *select=tab->select;
4794
12940
  key_map usable_keys;
4795
12941
  QUICK_SELECT_I *save_quick= 0;
4796
12942
 
4797
12943
  /*
4798
 
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
 
12944
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
4799
12945
    been taken into account.
4800
12946
  */
4801
12947
  usable_keys= *map;
4802
12948
 
4803
 
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
12949
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4804
12950
  {
4805
12951
    Item *item= (*tmp_order->item)->real_item();
4806
12952
    if (item->type() != Item::FIELD_ITEM)
4807
12953
    {
4808
 
      usable_keys.reset();
 
12954
      usable_keys.clear_all();
4809
12955
      return(0);
4810
12956
    }
4811
 
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4812
 
    if (usable_keys.none())
 
12957
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
 
12958
    if (usable_keys.is_clear_all())
4813
12959
      return(0);                                        // No usable keys
4814
12960
  }
4815
12961
 
4819
12965
  {
4820
12966
    ref_key=       tab->ref.key;
4821
12967
    ref_key_parts= tab->ref.key_parts;
4822
 
    if (tab->type == AM_REF_OR_NULL)
 
12968
    if (tab->type == JT_REF_OR_NULL)
4823
12969
      return(0);
4824
12970
  }
4825
12971
  else if (select && select->quick)             // Range found by opt_range
4826
12972
  {
4827
12973
    int quick_type= select->quick->get_type();
4828
12974
    save_quick= select->quick;
4829
 
    /*
4830
 
      assume results are not ordered when index merge is used
4831
 
      TODO: sergeyp: Results of all index merge selects actually are ordered
 
12975
    /* 
 
12976
      assume results are not ordered when index merge is used 
 
12977
      TODO: sergeyp: Results of all index merge selects actually are ordered 
4832
12978
      by clustered PK values.
4833
12979
    */
4834
 
 
4835
 
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
4836
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
12980
  
 
12981
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
 
12982
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
4837
12983
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
4838
12984
      return(0);
4839
12985
    ref_key=       select->quick->index;
4845
12991
    /*
4846
12992
      We come here when there is a REF key.
4847
12993
    */
4848
 
    if (! usable_keys.test(ref_key))
 
12994
    if (!usable_keys.is_set(ref_key))
4849
12995
    {
4850
12996
      /*
4851
 
        We come here when ref_key is not among usable_keys
 
12997
        We come here when ref_key is not among usable_keys
4852
12998
      */
4853
 
      uint32_t new_ref_key;
 
12999
      uint new_ref_key;
4854
13000
      /*
4855
 
        If using index only read, only consider other possible index only
4856
 
        keys
 
13001
        If using index only read, only consider other possible index only
 
13002
        keys
4857
13003
      */
4858
 
      if (table->covering_keys.test(ref_key))
4859
 
        usable_keys&= table->covering_keys;
 
13004
      if (table->covering_keys.is_set(ref_key))
 
13005
        usable_keys.intersect(table->covering_keys);
4860
13006
      if (tab->pre_idx_push_select_cond)
4861
13007
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4862
13008
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4863
13009
                                       &usable_keys)) < MAX_KEY)
4864
13010
      {
4865
 
        /* Found key that can be used to retrieve data in sorted order */
4866
 
        if (tab->ref.key >= 0)
4867
 
        {
 
13011
        /* Found key that can be used to retrieve data in sorted order */
 
13012
        if (tab->ref.key >= 0)
 
13013
        {
4868
13014
          /*
4869
 
            We'll use ref access method on key new_ref_key. In general case
 
13015
            We'll use ref access method on key new_ref_key. In general case 
4870
13016
            the index search tuple for new_ref_key will be different (e.g.
4871
13017
            when one index is defined as (part1, part2, ...) and another as
4872
 
            (part1, part2(N), ...) and the WHERE clause contains
4873
 
            "part1 = const1 AND part2=const2".
 
13018
            (part1, part2(N), ...) and the WHERE clause contains 
 
13019
            "part1 = const1 AND part2=const2". 
4874
13020
            So we build tab->ref from scratch here.
4875
13021
          */
4876
 
          optimizer::KeyUse *keyuse= tab->keyuse;
4877
 
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
 
13022
          KEYUSE *keyuse= tab->keyuse;
 
13023
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
4878
13024
            keyuse++;
4879
13025
 
4880
 
          if (create_ref_for_key(tab->join, tab, keyuse,
 
13026
          if (create_ref_for_key(tab->join, tab, keyuse, 
4881
13027
                                 tab->join->const_table_map))
4882
13028
            return(0);
4883
 
        }
4884
 
        else
4885
 
        {
 
13029
        }
 
13030
        else
 
13031
        {
4886
13032
          /*
4887
13033
            The range optimizer constructed QUICK_RANGE for ref_key, and
4888
13034
            we want to use instead new_ref_key as the index. We can't
4892
13038
            parameres are set correctly by the range optimizer.
4893
13039
           */
4894
13040
          key_map new_ref_key_map;
4895
 
          new_ref_key_map.reset();  // Force the creation of quick select
4896
 
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
 
13041
          new_ref_key_map.clear_all();  // Force the creation of quick select
 
13042
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
4897
13043
 
4898
 
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
 
13044
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
4899
13045
                                        (tab->join->select_options &
4900
13046
                                         OPTION_FOUND_ROWS) ?
4901
13047
                                        HA_POS_ERROR :
4903
13049
                                        true) <=
4904
13050
              0)
4905
13051
            return(0);
4906
 
        }
 
13052
        }
4907
13053
        ref_key= new_ref_key;
4908
13054
      }
4909
13055
    }
4910
13056
    /* Check if we get the rows in requested sorted order by using the key */
4911
 
    if (usable_keys.test(ref_key) &&
 
13057
    if (usable_keys.is_set(ref_key) &&
4912
13058
        (order_direction= test_if_order_by_key(order,table,ref_key,
4913
13059
                                               &used_key_parts)))
4914
13060
      goto check_reverse_order;
4918
13064
      Check whether there is an index compatible with the given order
4919
13065
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
4920
13066
      or a table scan.
4921
 
      It may be the case if order_st/GROUP BY is used with LIMIT.
 
13067
      It may be the case if ORDER/GROUP BY is used with LIMIT.
4922
13068
    */
4923
 
    uint32_t nr;
 
13069
    uint nr;
4924
13070
    key_map keys;
4925
 
    uint32_t best_key_parts= 0;
 
13071
    uint best_key_parts= 0;
4926
13072
    int best_key_direction= 0;
4927
13073
    ha_rows best_records= 0;
4928
13074
    double read_time;
4930
13076
    bool is_best_covering= false;
4931
13077
    double fanout= 1;
4932
13078
    JOIN *join= tab->join;
4933
 
    uint32_t tablenr= tab - join->join_tab;
4934
 
    ha_rows table_records= table->cursor->stats.records;
 
13079
    uint tablenr= tab - join->join_tab;
 
13080
    ha_rows table_records= table->file->stats.records;
4935
13081
    bool group= join->group && order == join->group_list;
4936
 
    optimizer::Position cur_pos;
4937
13082
 
4938
13083
    /*
4939
13084
      If not used with LIMIT, only use keys if the whole query can be
4942
13087
    */
4943
13088
    if (select_limit >= table_records)
4944
13089
    {
4945
 
      /*
4946
 
        filesort() and join cache are usually faster than reading in
 
13090
      /* 
 
13091
        filesort() and join cache are usually faster than reading in 
4947
13092
        index order and not using join cache
4948
13093
        */
4949
 
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
 
13094
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
4950
13095
        return(0);
4951
 
      keys= *table->cursor->keys_to_use_for_scanning();
4952
 
      keys|= table->covering_keys;
 
13096
      keys= *table->file->keys_to_use_for_scanning();
 
13097
      keys.merge(table->covering_keys);
4953
13098
 
4954
13099
      /*
4955
 
        We are adding here also the index specified in FORCE INDEX clause,
4956
 
        if any.
4957
 
        This is to allow users to use index in order_st BY.
 
13100
        We are adding here also the index specified in FORCE INDEX clause, 
 
13101
        if any.
 
13102
        This is to allow users to use index in ORDER BY.
4958
13103
      */
4959
 
      if (table->force_index)
4960
 
        keys|= (group ? table->keys_in_use_for_group_by :
4961
 
                                table->keys_in_use_for_order_by);
4962
 
      keys&= usable_keys;
 
13104
      if (table->force_index) 
 
13105
        keys.merge(group ? table->keys_in_use_for_group_by :
 
13106
                           table->keys_in_use_for_order_by);
 
13107
      keys.intersect(usable_keys);
4963
13108
    }
4964
13109
    else
4965
13110
      keys= usable_keys;
4966
13111
 
4967
 
    cur_pos= join->getPosFromOptimalPlan(tablenr);
4968
 
    read_time= cur_pos.getCost();
4969
 
    for (uint32_t i= tablenr+1; i < join->tables; i++)
4970
 
    {
4971
 
      cur_pos= join->getPosFromOptimalPlan(i);
4972
 
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4973
 
    }
 
13112
    read_time= join->best_positions[tablenr].read_time;
 
13113
    for (uint i= tablenr+1; i < join->tables; i++)
 
13114
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
4974
13115
 
4975
13116
    for (nr=0; nr < table->s->keys ; nr++)
4976
13117
    {
4977
13118
      int direction;
4978
 
      if (keys.test(nr) &&
 
13119
      if (keys.is_set(nr) &&
4979
13120
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4980
13121
      {
4981
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->s->primary_key && table->cursor->primary_key_is_clustered());
4982
 
 
4983
 
        /*
4984
 
          Don't use an index scan with order_st BY without limit.
 
13122
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
 
13123
        
 
13124
        /* 
 
13125
          Don't use an index scan with ORDER BY without limit.
4985
13126
          For GROUP BY without limit always use index scan
4986
 
          if there is a suitable index.
 
13127
          if there is a suitable index. 
4987
13128
          Why we hold to this asymmetry hardly can be explained
4988
13129
          rationally. It's easy to demonstrate that using
4989
13130
          temporary table + filesort could be cheaper for grouping
4990
13131
          queries too.
4991
 
        */
 
13132
        */ 
4992
13133
        if (is_covering ||
4993
 
            select_limit != HA_POS_ERROR ||
 
13134
            select_limit != HA_POS_ERROR || 
4994
13135
            (ref_key < 0 && (group || table->force_index)))
4995
 
        {
 
13136
        { 
4996
13137
          double rec_per_key;
4997
13138
          double index_scan_time;
4998
13139
          KEY *keyinfo= tab->table->key_info+nr;
5001
13142
          if (group)
5002
13143
          {
5003
13144
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
5004
 
            set_if_bigger(rec_per_key, 1.0);
 
13145
            set_if_bigger(rec_per_key, 1);
5005
13146
            /*
5006
13147
              With a grouping query each group containing on average
5007
13148
              rec_per_key records produces only one row that will
5008
13149
              be included into the result set.
5009
 
            */
 
13150
            */  
5010
13151
            if (select_limit > table_records/rec_per_key)
5011
13152
                select_limit= table_records;
5012
13153
            else
5013
13154
              select_limit= (ha_rows) (select_limit*rec_per_key);
5014
13155
          }
5015
 
          /*
 
13156
          /* 
5016
13157
            If tab=tk is not the last joined table tn then to get first
5017
13158
            L records from the result set we can expect to retrieve
5018
13159
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
5021
13162
            So the estimate for L/fanout(tk,tn) will be too optimistic
5022
13163
            and as result we'll choose an index scan when using ref/range
5023
13164
            access + filesort will be cheaper.
5024
 
          */
 
13165
          */
5025
13166
          select_limit= (ha_rows) (select_limit < fanout ?
5026
13167
                                   1 : select_limit/fanout);
5027
13168
          /*
5028
13169
            We assume that each of the tested indexes is not correlated
5029
13170
            with ref_key. Thus, to select first N records we have to scan
5030
 
            N/selectivity(ref_key) index entries.
 
13171
            N/selectivity(ref_key) index entries. 
5031
13172
            selectivity(ref_key) = #scanned_records/#table_records =
5032
13173
            table->quick_condition_rows/table_records.
5033
13174
            In any case we can't select more than #table_records.
5034
 
            N/(table->quick_condition_rows/table_records) > table_records
 
13175
            N/(table->quick_condition_rows/table_records) > table_records 
5035
13176
            <=> N > table->quick_condition_rows.
5036
 
          */
 
13177
          */ 
5037
13178
          if (select_limit > table->quick_condition_rows)
5038
13179
            select_limit= table_records;
5039
13180
          else
5041
13182
                                     (double) table_records /
5042
13183
                                      table->quick_condition_rows);
5043
13184
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
5044
 
          set_if_bigger(rec_per_key, 1.0);
 
13185
          set_if_bigger(rec_per_key, 1);
5045
13186
          /*
5046
13187
            Here we take into account the fact that rows are
5047
13188
            accessed in sequences rec_per_key records in each.
5048
13189
            Rows in such a sequence are supposed to be ordered
5049
13190
            by rowid/primary key. When reading the data
5050
13191
            in a sequence we'll touch not more pages than the
5051
 
            table cursor contains.
 
13192
            table file contains.
5052
13193
            TODO. Use the formula for a disk sweep sequential access
5053
 
            to calculate the cost of accessing data rows for one
 
13194
            to calculate the cost of accessing data rows for one 
5054
13195
            index entry.
5055
 
          */
 
13196
          */
5056
13197
          index_scan_time= select_limit/rec_per_key *
5057
 
                           min(rec_per_key, table->cursor->scan_time());
 
13198
                           min(rec_per_key, table->file->scan_time());
5058
13199
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
5059
13200
              index_scan_time < read_time)
5060
13201
          {
5061
13202
            ha_rows quick_records= table_records;
5062
13203
            if (is_best_covering && !is_covering)
5063
13204
              continue;
5064
 
            if (table->quick_keys.test(nr))
 
13205
            if (table->quick_keys.is_set(nr))
5065
13206
              quick_records= table->quick_rows[nr];
5066
13207
            if (best_key < 0 ||
5067
13208
                (select_limit <= min(quick_records,best_records) ?
5072
13213
              best_key_parts= keyinfo->key_parts;
5073
13214
              best_records= quick_records;
5074
13215
              is_best_covering= is_covering;
5075
 
              best_key_direction= direction;
 
13216
              best_key_direction= direction; 
5076
13217
            }
5077
 
          }
5078
 
        }
 
13218
          }   
 
13219
        }      
5079
13220
      }
5080
13221
    }
5081
13222
    if (best_key >= 0)
5082
13223
    {
5083
13224
      bool quick_created= false;
5084
 
      if (table->quick_keys.test(best_key) && best_key != ref_key)
 
13225
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
5085
13226
      {
5086
 
        key_map test_map;
5087
 
        test_map.reset();       // Force the creation of quick select
5088
 
        test_map.set(best_key); // only best_key.
5089
 
        quick_created=
5090
 
          select->test_quick_select(join->session, test_map, 0,
 
13227
        key_map map;
 
13228
        map.clear_all();       // Force the creation of quick select
 
13229
        map.set_bit(best_key); // only best_key.
 
13230
        quick_created=         
 
13231
          select->test_quick_select(join->thd, map, 0,
5091
13232
                                    join->select_options & OPTION_FOUND_ROWS ?
5092
13233
                                    HA_POS_ERROR :
5093
13234
                                    join->unit->select_limit_cnt,
5096
13237
      if (!no_changes)
5097
13238
      {
5098
13239
        if (!quick_created)
5099
 
        {
 
13240
        {
5100
13241
          tab->index= best_key;
5101
13242
          tab->read_first_record= best_key_direction > 0 ?
5102
13243
                                  join_read_first:join_read_last;
5103
 
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
 
13244
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
5104
13245
          if (select && select->quick)
5105
13246
          {
5106
13247
            delete select->quick;
5107
13248
            select->quick= 0;
5108
13249
          }
5109
 
          if (table->covering_keys.test(best_key))
 
13250
          if (table->covering_keys.is_set(best_key))
5110
13251
          {
5111
13252
            table->key_read=1;
5112
 
            table->cursor->extra(HA_EXTRA_KEYREAD);
 
13253
            table->file->extra(HA_EXTRA_KEYREAD);
5113
13254
          }
5114
 
          table->cursor->ha_index_or_rnd_end();
 
13255
          table->file->ha_index_or_rnd_end();
5115
13256
          if (join->select_options & SELECT_DESCRIBE)
5116
13257
          {
5117
13258
            tab->ref.key= -1;
5118
13259
            tab->ref.key_parts= 0;
5119
 
            if (select_limit < table_records)
 
13260
            if (select_limit < table_records) 
5120
13261
              tab->limit= select_limit;
5121
13262
          }
5122
13263
        }
5123
 
        else if (tab->type != AM_ALL)
 
13264
        else if (tab->type != JT_ALL)
5124
13265
        {
5125
13266
          /*
5126
13267
            We're about to use a quick access to the table.
5128
13269
            method is actually used.
5129
13270
          */
5130
13271
          assert(tab->select->quick);
5131
 
          tab->type= AM_ALL;
 
13272
          tab->type=JT_ALL;
5132
13273
          tab->use_quick=1;
5133
13274
          tab->ref.key= -1;
5134
13275
          tab->ref.key_parts=0;         // Don't use ref key.
5135
13276
          tab->read_first_record= join_init_read_record;
 
13277
          /*
 
13278
            TODO: update the number of records in join->best_positions[tablenr]
 
13279
          */
5136
13280
        }
5137
13281
      }
5138
13282
      used_key_parts= best_key_parts;
5139
13283
      order_direction= best_key_direction;
5140
13284
    }
5141
13285
    else
5142
 
      return(0);
5143
 
  }
 
13286
      return(0); 
 
13287
  } 
5144
13288
 
5145
 
check_reverse_order:
5146
 
  if (order_direction == -1)            // If order_st BY ... DESC
 
13289
check_reverse_order:                  
 
13290
  if (order_direction == -1)            // If ORDER BY ... DESC
5147
13291
  {
5148
13292
    if (select && select->quick)
5149
13293
    {
5150
13294
      /*
5151
 
        Don't reverse the sort order, if it's already done.
 
13295
        Don't reverse the sort order, if it's already done.
5152
13296
        (In some cases test_if_order_by_key() can be called multiple times
5153
13297
      */
5154
13298
      if (!select->quick->reverse_sorted())
5165
13309
          select->quick= save_quick;
5166
13310
          return(0);                   // Use filesort
5167
13311
        }
5168
 
 
5169
 
        /* order_st BY range_key DESC */
5170
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
5171
 
                                          used_key_parts, &error);
5172
 
        if (!tmp || error)
5173
 
        {
5174
 
          delete tmp;
5175
 
                select->quick= save_quick;
5176
 
                tab->limit= 0;
5177
 
          return(0);            // Reverse sort not supported
5178
 
        }
5179
 
        select->quick=tmp;
 
13312
            
 
13313
        /* ORDER BY range_key DESC */
 
13314
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
13315
                                    used_key_parts, &error);
 
13316
        if (!tmp || error)
 
13317
        {
 
13318
          delete tmp;
 
13319
          select->quick= save_quick;
 
13320
          tab->limit= 0;
 
13321
          return(0);            // Reverse sort not supported
 
13322
        }
 
13323
        select->quick=tmp;
5180
13324
      }
5181
13325
    }
5182
 
    else if (tab->type != AM_NEXT &&
 
13326
    else if (tab->type != JT_NEXT && 
5183
13327
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
5184
13328
    {
5185
13329
      /*
5186
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
13330
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
5187
13331
 
5188
 
        Use a traversal function that starts by reading the last row
5189
 
        with key part (A) and then traverse the index backwards.
 
13332
        Use a traversal function that starts by reading the last row
 
13333
        with key part (A) and then traverse the index backwards.
5190
13334
      */
5191
13335
      tab->read_first_record= join_read_last_key;
5192
13336
      tab->read_record.read_record= join_read_prev_same;
5197
13341
  return(1);
5198
13342
}
5199
13343
 
 
13344
 
5200
13345
/*
5201
13346
  If not selecting by given key, create an index how records should be read
5202
13347
 
5203
13348
  SYNOPSIS
5204
13349
   create_sort_index()
5205
 
     session            Thread Cursor
 
13350
     thd                Thread handler
5206
13351
     tab                Table to sort (in join structure)
5207
13352
     order              How table should be sorted
5208
13353
     filesort_limit     Max number of rows that needs to be sorted
5209
13354
     select_limit       Max number of rows in final output
5210
13355
                        Used to decide if we should use index or not
5211
 
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
5212
 
                        Used to decide if we should use index or not
 
13356
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
 
13357
                        Used to decide if we should use index or not     
5213
13358
 
5214
13359
 
5215
13360
  IMPLEMENTATION
5216
13361
   - If there is an index that can be used, 'tab' is modified to use
5217
13362
     this index.
5218
 
   - If no index, create with filesort() an index cursor that can be used to
 
13363
   - If no index, create with filesort() an index file that can be used to
5219
13364
     retrieve rows in order (should be done with 'read_record').
5220
13365
     The sorted data is stored in tab->table and will be freed when calling
5221
 
     tab->table->free_io_cache().
 
13366
     free_io_cache(tab->table).
5222
13367
 
5223
13368
  RETURN VALUES
5224
13369
    0           ok
5225
13370
    -1          Some fatal error
5226
13371
    1           No records
5227
13372
*/
5228
 
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
13373
 
 
13374
static int
 
13375
create_sort_index(THD *thd, JOIN *join, ORDER *order,
 
13376
                  ha_rows filesort_limit, ha_rows select_limit,
 
13377
                  bool is_order_by)
5229
13378
{
5230
 
  uint32_t length= 0;
 
13379
  uint length= 0;
5231
13380
  ha_rows examined_rows;
5232
 
  Table *table;
 
13381
  TABLE *table;
5233
13382
  SQL_SELECT *select;
5234
 
  JoinTable *tab;
 
13383
  JOIN_TAB *tab;
5235
13384
 
5236
13385
  if (join->tables == join->const_tables)
5237
13386
    return(0);                          // One row, no need to sort
5245
13394
    is going to be used as it is applied now only for one table queries
5246
13395
    with covering indexes.
5247
13396
  */
5248
 
  if ((order != join->group_list ||
 
13397
  if ((order != join->group_list || 
5249
13398
       !(join->select_options & SELECT_BIG_RESULT) ||
5250
13399
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
5251
 
      test_if_skip_sort_order(tab,order,select_limit,0,
 
13400
      test_if_skip_sort_order(tab,order,select_limit,0, 
5252
13401
                              is_order_by ?  &table->keys_in_use_for_order_by :
5253
13402
                              &table->keys_in_use_for_group_by))
5254
13403
    return(0);
5255
 
  for (order_st *ord= join->order; ord; ord= ord->next)
 
13404
  for (ORDER *ord= join->order; ord; ord= ord->next)
5256
13405
    length++;
5257
 
  if (!(join->sortorder=
 
13406
  if (!(join->sortorder= 
5258
13407
        make_unireg_sortorder(order, &length, join->sortorder)))
5259
 
    goto err;
 
13408
    goto err;                           /* purecov: inspected */
5260
13409
 
5261
 
  table->sort.io_cache= new IO_CACHE;
5262
 
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
 
13410
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
 
13411
                                             MYF(MY_WME | MY_ZEROFILL));
5263
13412
  table->status=0;                              // May be wrong if quick_select
5264
13413
 
5265
13414
  // If table has a range, move it to select
5269
13418
    {
5270
13419
      select->quick=tab->quick;
5271
13420
      tab->quick=0;
5272
 
      /*
 
13421
      /* 
5273
13422
        We can only use 'Only index' if quick key is same as ref_key
5274
13423
        and in index_merge 'Only index' cannot be used
5275
13424
      */
5276
 
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
 
13425
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
5277
13426
      {
5278
 
        table->key_read=0;
5279
 
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13427
        table->key_read=0;
 
13428
        table->file->extra(HA_EXTRA_NO_KEYREAD);
5280
13429
      }
5281
13430
    }
5282
13431
    else
5283
13432
    {
5284
13433
      /*
5285
 
        We have a ref on a const;  Change this to a range that filesort
5286
 
        can use.
5287
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
5288
 
        field, quick will contain an empty record set.
 
13434
        We have a ref on a const;  Change this to a range that filesort
 
13435
        can use.
 
13436
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
13437
        field, quick will contain an empty record set.
5289
13438
      */
5290
 
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
 
13439
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
5291
13440
                                                     tab->found_records))))
5292
 
        goto err;
 
13441
        goto err;
5293
13442
    }
5294
13443
  }
5295
13444
 
 
13445
  /* Fill schema tables with data before filesort if it's necessary */
 
13446
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
 
13447
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
 
13448
    goto err;
 
13449
 
5296
13450
  if (table->s->tmp_table)
5297
 
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
5298
 
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
13451
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
 
13452
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
5299
13453
                                     select, filesort_limit, 0,
5300
13454
                                     &examined_rows);
5301
13455
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5307
13461
  tab->select_cond=0;
5308
13462
  tab->last_inner= 0;
5309
13463
  tab->first_unmatched= 0;
5310
 
  tab->type= AM_ALL;                            // Read with normal read_record
 
13464
  tab->type=JT_ALL;                             // Read with normal read_record
5311
13465
  tab->read_first_record= join_init_read_record;
5312
13466
  tab->join->examined_rows+=examined_rows;
5313
13467
  if (table->key_read)                          // Restore if we used indexes
5314
13468
  {
5315
13469
    table->key_read=0;
5316
 
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
13470
    table->file->extra(HA_EXTRA_NO_KEYREAD);
5317
13471
  }
5318
13472
  return(table->sort.found_records == HA_POS_ERROR);
5319
13473
err:
5320
13474
  return(-1);
5321
13475
}
5322
13476
 
5323
 
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5324
 
{
5325
 
  Cursor *cursor=table->cursor;
 
13477
/*****************************************************************************
 
13478
  Remove duplicates from tmp table
 
13479
  This should be recoded to add a unique index to the table and remove
 
13480
  duplicates
 
13481
  Table is a locked single thread table
 
13482
  fields is the number of fields to check (from the end)
 
13483
*****************************************************************************/
 
13484
 
 
13485
static bool compare_record(TABLE *table, Field **ptr)
 
13486
{
 
13487
  for (; *ptr ; ptr++)
 
13488
  {
 
13489
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
 
13490
      return 1;
 
13491
  }
 
13492
  return 0;
 
13493
}
 
13494
 
 
13495
static bool copy_blobs(Field **ptr)
 
13496
{
 
13497
  for (; *ptr ; ptr++)
 
13498
  {
 
13499
    if ((*ptr)->flags & BLOB_FLAG)
 
13500
      if (((Field_blob *) (*ptr))->copy())
 
13501
        return 1;                               // Error
 
13502
  }
 
13503
  return 0;
 
13504
}
 
13505
 
 
13506
static void free_blobs(Field **ptr)
 
13507
{
 
13508
  for (; *ptr ; ptr++)
 
13509
  {
 
13510
    if ((*ptr)->flags & BLOB_FLAG)
 
13511
      ((Field_blob *) (*ptr))->free();
 
13512
  }
 
13513
}
 
13514
 
 
13515
 
 
13516
static int
 
13517
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
 
13518
{
 
13519
  int error;
 
13520
  ulong reclength,offset;
 
13521
  uint field_count;
 
13522
  THD *thd= join->thd;
 
13523
 
 
13524
  entry->reginfo.lock_type=TL_WRITE;
 
13525
 
 
13526
  /* Calculate how many saved fields there is in list */
 
13527
  field_count=0;
 
13528
  List_iterator<Item> it(fields);
 
13529
  Item *item;
 
13530
  while ((item=it++))
 
13531
  {
 
13532
    if (item->get_tmp_table_field() && ! item->const_item())
 
13533
      field_count++;
 
13534
  }
 
13535
 
 
13536
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
 
13537
  {                    // only const items with no OPTION_FOUND_ROWS
 
13538
    join->unit->select_limit_cnt= 1;            // Only send first row
 
13539
    return(0);
 
13540
  }
 
13541
  Field **first_field=entry->field+entry->s->fields - field_count;
 
13542
  offset= (field_count ? 
 
13543
           entry->field[entry->s->fields - field_count]->
 
13544
           offset(entry->record[0]) : 0);
 
13545
  reclength=entry->s->reclength-offset;
 
13546
 
 
13547
  free_io_cache(entry);                         // Safety
 
13548
  entry->file->info(HA_STATUS_VARIABLE);
 
13549
  if (entry->s->db_type() == heap_hton ||
 
13550
      (!entry->s->blob_fields &&
 
13551
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
13552
        thd->variables.sortbuff_size)))
 
13553
    error=remove_dup_with_hash_index(join->thd, entry,
 
13554
                                     field_count, first_field,
 
13555
                                     reclength, having);
 
13556
  else
 
13557
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
 
13558
                                  having);
 
13559
 
 
13560
  free_blobs(first_field);
 
13561
  return(error);
 
13562
}
 
13563
 
 
13564
 
 
13565
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
 
13566
                                   ulong offset, Item *having)
 
13567
{
 
13568
  handler *file=table->file;
5326
13569
  char *org_record,*new_record;
5327
 
  unsigned char *record;
 
13570
  uchar *record;
5328
13571
  int error;
5329
 
  uint32_t reclength= table->s->reclength-offset;
 
13572
  ulong reclength= table->s->reclength-offset;
5330
13573
 
5331
13574
  org_record=(char*) (record=table->record[0])+offset;
5332
13575
  new_record=(char*) table->record[1]+offset;
5333
13576
 
5334
 
  cursor->ha_rnd_init(1);
5335
 
  error=cursor->rnd_next(record);
 
13577
  file->ha_rnd_init(1);
 
13578
  error=file->rnd_next(record);
5336
13579
  for (;;)
5337
13580
  {
5338
 
    if (session->killed)
 
13581
    if (thd->killed)
5339
13582
    {
5340
 
      session->send_kill_message();
 
13583
      thd->send_kill_message();
5341
13584
      error=0;
5342
13585
      goto err;
5343
13586
    }
5344
13587
    if (error)
5345
13588
    {
5346
13589
      if (error == HA_ERR_RECORD_DELETED)
5347
 
        continue;
 
13590
        continue;
5348
13591
      if (error == HA_ERR_END_OF_FILE)
5349
 
        break;
 
13592
        break;
5350
13593
      goto err;
5351
13594
    }
5352
13595
    if (having && !having->val_int())
5353
13596
    {
5354
 
      if ((error=cursor->ha_delete_row(record)))
5355
 
        goto err;
5356
 
      error=cursor->rnd_next(record);
 
13597
      if ((error=file->ha_delete_row(record)))
 
13598
        goto err;
 
13599
      error=file->rnd_next(record);
5357
13600
      continue;
5358
13601
    }
5359
13602
    if (copy_blobs(first_field))
5364
13607
    }
5365
13608
    memcpy(new_record,org_record,reclength);
5366
13609
 
5367
 
    /* Read through rest of cursor and mark duplicated rows deleted */
 
13610
    /* Read through rest of file and mark duplicated rows deleted */
5368
13611
    bool found=0;
5369
13612
    for (;;)
5370
13613
    {
5371
 
      if ((error=cursor->rnd_next(record)))
 
13614
      if ((error=file->rnd_next(record)))
5372
13615
      {
5373
 
        if (error == HA_ERR_RECORD_DELETED)
5374
 
          continue;
5375
 
        if (error == HA_ERR_END_OF_FILE)
5376
 
          break;
5377
 
        goto err;
 
13616
        if (error == HA_ERR_RECORD_DELETED)
 
13617
          continue;
 
13618
        if (error == HA_ERR_END_OF_FILE)
 
13619
          break;
 
13620
        goto err;
5378
13621
      }
5379
 
      if (table->compare_record(first_field) == 0)
 
13622
      if (compare_record(table, first_field) == 0)
5380
13623
      {
5381
 
        if ((error=cursor->ha_delete_row(record)))
5382
 
          goto err;
 
13624
        if ((error=file->ha_delete_row(record)))
 
13625
          goto err;
5383
13626
      }
5384
13627
      else if (!found)
5385
13628
      {
5386
 
        found= 1;
5387
 
        cursor->position(record);       // Remember position
 
13629
        found=1;
 
13630
        file->position(record); // Remember position
5388
13631
      }
5389
13632
    }
5390
13633
    if (!found)
5391
 
      break;                                    // End of cursor
 
13634
      break;                                    // End of file
5392
13635
    /* Restart search on next row */
5393
 
    error=cursor->restart_rnd_next(record,cursor->ref);
 
13636
    error=file->restart_rnd_next(record,file->ref);
5394
13637
  }
5395
13638
 
5396
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13639
  file->extra(HA_EXTRA_NO_CACHE);
5397
13640
  return(0);
5398
13641
err:
5399
 
  cursor->extra(HA_EXTRA_NO_CACHE);
 
13642
  file->extra(HA_EXTRA_NO_CACHE);
5400
13643
  if (error)
5401
 
    table->print_error(error,MYF(0));
 
13644
    file->print_error(error,MYF(0));
5402
13645
  return(1);
5403
13646
}
5404
13647
 
 
13648
 
5405
13649
/**
5406
13650
  Generate a hash index for each row to quickly find duplicate rows.
5407
13651
 
5408
13652
  @note
5409
13653
    Note that this will not work on tables with blobs!
5410
13654
*/
5411
 
int remove_dup_with_hash_index(Session *session, 
5412
 
                               Table *table,
5413
 
                               uint32_t field_count,
5414
 
                               Field **first_field,
5415
 
                               uint32_t key_length,
5416
 
                               Item *having)
 
13655
 
 
13656
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
 
13657
                                      uint field_count,
 
13658
                                      Field **first_field,
 
13659
                                      ulong key_length,
 
13660
                                      Item *having)
5417
13661
{
5418
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
13662
  uchar *key_buffer, *key_pos, *record=table->record[0];
5419
13663
  int error;
5420
 
  Cursor *cursor= table->cursor;
5421
 
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5422
 
  uint32_t *field_lengths,*field_length;
 
13664
  handler *file= table->file;
 
13665
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
 
13666
  uint *field_lengths,*field_length;
5423
13667
  HASH hash;
5424
13668
 
5425
 
  if (! memory::multi_malloc(false,
 
13669
  if (!my_multi_malloc(MYF(MY_WME),
5426
13670
                       &key_buffer,
5427
 
                       (uint32_t) ((key_length + extra_length) *
5428
 
                               (long) cursor->stats.records),
 
13671
                       (uint) ((key_length + extra_length) *
 
13672
                               (long) file->stats.records),
5429
13673
                       &field_lengths,
5430
 
                       (uint32_t) (field_count*sizeof(*field_lengths)),
5431
 
                       NULL))
 
13674
                       (uint) (field_count*sizeof(*field_lengths)),
 
13675
                       NullS))
5432
13676
    return(1);
5433
13677
 
5434
13678
  {
5435
13679
    Field **ptr;
5436
 
    uint32_t total_length= 0;
 
13680
    ulong total_length= 0;
5437
13681
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5438
13682
    {
5439
 
      uint32_t length= (*ptr)->sort_length();
 
13683
      uint length= (*ptr)->sort_length();
5440
13684
      (*field_length++)= length;
5441
13685
      total_length+= length;
5442
13686
    }
5445
13689
    extra_length= ALIGN_SIZE(key_length)-key_length;
5446
13690
  }
5447
13691
 
5448
 
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
 
13692
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
5449
13693
                key_length, (hash_get_key) 0, 0, 0))
5450
13694
  {
5451
 
    free((char*) key_buffer);
 
13695
    my_free((char*) key_buffer,MYF(0));
5452
13696
    return(1);
5453
13697
  }
5454
13698
 
5455
 
  cursor->ha_rnd_init(1);
 
13699
  file->ha_rnd_init(1);
5456
13700
  key_pos=key_buffer;
5457
13701
  for (;;)
5458
13702
  {
5459
 
    unsigned char *org_key_pos;
5460
 
    if (session->killed)
 
13703
    uchar *org_key_pos;
 
13704
    if (thd->killed)
5461
13705
    {
5462
 
      session->send_kill_message();
 
13706
      thd->send_kill_message();
5463
13707
      error=0;
5464
13708
      goto err;
5465
13709
    }
5466
 
    if ((error=cursor->rnd_next(record)))
 
13710
    if ((error=file->rnd_next(record)))
5467
13711
    {
5468
13712
      if (error == HA_ERR_RECORD_DELETED)
5469
 
        continue;
 
13713
        continue;
5470
13714
      if (error == HA_ERR_END_OF_FILE)
5471
 
        break;
 
13715
        break;
5472
13716
      goto err;
5473
13717
    }
5474
13718
    if (having && !having->val_int())
5475
13719
    {
5476
 
      if ((error=cursor->ha_delete_row(record)))
5477
 
        goto err;
 
13720
      if ((error=file->ha_delete_row(record)))
 
13721
        goto err;
5478
13722
      continue;
5479
13723
    }
5480
13724
 
5490
13734
    if (hash_search(&hash, org_key_pos, key_length))
5491
13735
    {
5492
13736
      /* Duplicated found ; Remove the row */
5493
 
      if ((error=cursor->ha_delete_row(record)))
5494
 
        goto err;
 
13737
      if ((error=file->ha_delete_row(record)))
 
13738
        goto err;
5495
13739
    }
5496
13740
    else
5497
13741
      (void) my_hash_insert(&hash, org_key_pos);
5498
13742
    key_pos+=extra_length;
5499
13743
  }
5500
 
  free((char*) key_buffer);
 
13744
  my_free((char*) key_buffer,MYF(0));
5501
13745
  hash_free(&hash);
5502
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5503
 
  (void) cursor->ha_rnd_end();
 
13746
  file->extra(HA_EXTRA_NO_CACHE);
 
13747
  (void) file->ha_rnd_end();
5504
13748
  return(0);
5505
13749
 
5506
13750
err:
5507
 
  free((char*) key_buffer);
 
13751
  my_free((char*) key_buffer,MYF(0));
5508
13752
  hash_free(&hash);
5509
 
  cursor->extra(HA_EXTRA_NO_CACHE);
5510
 
  (void) cursor->ha_rnd_end();
 
13753
  file->extra(HA_EXTRA_NO_CACHE);
 
13754
  (void) file->ha_rnd_end();
5511
13755
  if (error)
5512
 
    table->print_error(error,MYF(0));
 
13756
    file->print_error(error,MYF(0));
5513
13757
  return(1);
5514
13758
}
5515
13759
 
5516
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
 
13760
 
 
13761
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
 
13762
                                  SORT_FIELD *sortorder)
5517
13763
{
5518
 
  uint32_t count;
 
13764
  uint count;
5519
13765
  SORT_FIELD *sort,*pos;
5520
13766
 
5521
13767
  count=0;
5522
 
  for (order_st *tmp = order; tmp; tmp=tmp->next)
 
13768
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
5523
13769
    count++;
5524
13770
  if (!sortorder)
5525
13771
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
5549
13795
  return(sort);
5550
13796
}
5551
13797
 
 
13798
 
 
13799
/*****************************************************************************
 
13800
  Fill join cache with packed records
 
13801
  Records are stored in tab->cache.buffer and last record in
 
13802
  last record is stored with pointers to blobs to support very big
 
13803
  records
 
13804
******************************************************************************/
 
13805
 
 
13806
static int
 
13807
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
 
13808
{
 
13809
  register unsigned int i;
 
13810
  unsigned int length, blobs;
 
13811
  size_t size;
 
13812
  CACHE_FIELD *copy,**blob_ptr;
 
13813
  JOIN_CACHE  *cache;
 
13814
  JOIN_TAB *join_tab;
 
13815
 
 
13816
  cache= &tables[table_count].cache;
 
13817
  cache->fields=blobs=0;
 
13818
 
 
13819
  join_tab=tables;
 
13820
  for (i=0 ; i < table_count ; i++,join_tab++)
 
13821
  {
 
13822
    if (!join_tab->used_fieldlength)            /* Not calced yet */
 
13823
      calc_used_field_length(thd, join_tab);
 
13824
    cache->fields+=join_tab->used_fields;
 
13825
    blobs+=join_tab->used_blobs;
 
13826
 
 
13827
    /* SemiJoinDuplicateElimination: reserve space for rowid */
 
13828
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13829
    {
 
13830
      cache->fields++;
 
13831
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
 
13832
    }
 
13833
  }
 
13834
  if (!(cache->field=(CACHE_FIELD*)
 
13835
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
13836
 
 
13837
                  sizeof(CACHE_FIELD*))))
 
13838
  {
 
13839
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
 
13840
    cache->buff=0;                              /* purecov: inspected */
 
13841
    return(1);                          /* purecov: inspected */
 
13842
  }
 
13843
  copy=cache->field;
 
13844
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
13845
    (cache->field+cache->fields+table_count*2);
 
13846
 
 
13847
  length=0;
 
13848
  for (i=0 ; i < table_count ; i++)
 
13849
  {
 
13850
    uint null_fields=0, used_fields;
 
13851
    Field **f_ptr,*field;
 
13852
    MY_BITMAP *read_set= tables[i].table->read_set;
 
13853
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
 
13854
         used_fields ;
 
13855
         f_ptr++)
 
13856
    {
 
13857
      field= *f_ptr;
 
13858
      if (bitmap_is_set(read_set, field->field_index))
 
13859
      {
 
13860
        used_fields--;
 
13861
        length+=field->fill_cache_field(copy);
 
13862
        if (copy->blob_field)
 
13863
          (*blob_ptr++)=copy;
 
13864
        if (field->maybe_null())
 
13865
          null_fields++;
 
13866
        copy->get_rowid= NULL;
 
13867
        copy++;
 
13868
      }
 
13869
    }
 
13870
    /* Copy null bits from table */
 
13871
    if (null_fields && tables[i].table->getNullFields())
 
13872
    {                                           /* must copy null bits */
 
13873
      copy->str= tables[i].table->null_flags;
 
13874
      copy->length= tables[i].table->s->null_bytes;
 
13875
      copy->strip=0;
 
13876
      copy->blob_field=0;
 
13877
      copy->get_rowid= NULL;
 
13878
      length+=copy->length;
 
13879
      copy++;
 
13880
      cache->fields++;
 
13881
    }
 
13882
    /* If outer join table, copy null_row flag */
 
13883
    if (tables[i].table->maybe_null)
 
13884
    {
 
13885
      copy->str= (uchar*) &tables[i].table->null_row;
 
13886
      copy->length=sizeof(tables[i].table->null_row);
 
13887
      copy->strip=0;
 
13888
      copy->blob_field=0;
 
13889
      copy->get_rowid= NULL;
 
13890
      length+=copy->length;
 
13891
      copy++;
 
13892
      cache->fields++;
 
13893
    }
 
13894
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
 
13895
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
 
13896
    {
 
13897
      copy->str= tables[i].table->file->ref;
 
13898
      copy->length= tables[i].table->file->ref_length;
 
13899
      copy->strip=0;
 
13900
      copy->blob_field=0;
 
13901
      copy->get_rowid= NULL;
 
13902
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
13903
      {
 
13904
        /* We will need to call h->position(): */
 
13905
        copy->get_rowid= tables[i].table;
 
13906
        /* And those after us won't have to: */
 
13907
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
 
13908
      }
 
13909
      copy++;
 
13910
    }
 
13911
  }
 
13912
 
 
13913
  cache->length=length+blobs*sizeof(char*);
 
13914
  cache->blobs=blobs;
 
13915
  *blob_ptr=0;                                  /* End sequentel */
 
13916
  size=max(thd->variables.join_buff_size, (ulong)cache->length);
 
13917
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
 
13918
    return(1);                          /* Don't use cache */ /* purecov: inspected */
 
13919
  cache->end=cache->buff+size;
 
13920
  reset_cache_write(cache);
 
13921
  return(0);
 
13922
}
 
13923
 
 
13924
 
 
13925
static ulong
 
13926
used_blob_length(CACHE_FIELD **ptr)
 
13927
{
 
13928
  uint length,blob_length;
 
13929
  for (length=0 ; *ptr ; ptr++)
 
13930
  {
 
13931
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
 
13932
    length+=blob_length;
 
13933
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
 
13934
  }
 
13935
  return length;
 
13936
}
 
13937
 
 
13938
 
 
13939
static bool
 
13940
store_record_in_cache(JOIN_CACHE *cache)
 
13941
{
 
13942
  uint length;
 
13943
  uchar *pos;
 
13944
  CACHE_FIELD *copy,*end_field;
 
13945
  bool last_record;
 
13946
 
 
13947
  pos=cache->pos;
 
13948
  end_field=cache->field+cache->fields;
 
13949
 
 
13950
  length=cache->length;
 
13951
  if (cache->blobs)
 
13952
    length+=used_blob_length(cache->blob_ptr);
 
13953
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
13954
    cache->ptr_record=cache->records;
 
13955
  /*
 
13956
    There is room in cache. Put record there
 
13957
  */
 
13958
  cache->records++;
 
13959
  for (copy=cache->field ; copy < end_field; copy++)
 
13960
  {
 
13961
    if (copy->blob_field)
 
13962
    {
 
13963
      if (last_record)
 
13964
      {
 
13965
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
 
13966
                                    copy->blob_field->charset());
 
13967
        pos+=copy->length+sizeof(char*);
 
13968
      }
 
13969
      else
 
13970
      {
 
13971
        copy->blob_field->get_image(pos, copy->length, // blob length
 
13972
                                    copy->blob_field->charset());
 
13973
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
13974
        pos+=copy->length+copy->blob_length;
 
13975
      }
 
13976
    }
 
13977
    else
 
13978
    {
 
13979
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
 
13980
      if (copy->get_rowid)
 
13981
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
 
13982
 
 
13983
      if (copy->strip)
 
13984
      {
 
13985
        uchar *str,*end;
 
13986
        for (str=copy->str,end= str+copy->length;
 
13987
             end > str && end[-1] == ' ' ;
 
13988
             end--) ;
 
13989
        length=(uint) (end-str);
 
13990
        memcpy(pos+2, str, length);
 
13991
        int2store(pos, length);
 
13992
        pos+= length+2;
 
13993
      }
 
13994
      else
 
13995
      {
 
13996
        memcpy(pos,copy->str,copy->length);
 
13997
        pos+=copy->length;
 
13998
      }
 
13999
    }
 
14000
  }
 
14001
  cache->pos=pos;
 
14002
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
14003
}
 
14004
 
 
14005
 
 
14006
static void
 
14007
reset_cache_read(JOIN_CACHE *cache)
 
14008
{
 
14009
  cache->record_nr=0;
 
14010
  cache->pos=cache->buff;
 
14011
}
 
14012
 
 
14013
 
 
14014
static void reset_cache_write(JOIN_CACHE *cache)
 
14015
{
 
14016
  reset_cache_read(cache);
 
14017
  cache->records= 0;
 
14018
  cache->ptr_record= (uint) ~0;
 
14019
}
 
14020
 
 
14021
 
 
14022
static void
 
14023
read_cached_record(JOIN_TAB *tab)
 
14024
{
 
14025
  uchar *pos;
 
14026
  uint length;
 
14027
  bool last_record;
 
14028
  CACHE_FIELD *copy,*end_field;
 
14029
 
 
14030
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
 
14031
  pos=tab->cache.pos;
 
14032
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
14033
       copy < end_field;
 
14034
       copy++)
 
14035
  {
 
14036
    if (copy->blob_field)
 
14037
    {
 
14038
      if (last_record)
 
14039
      {
 
14040
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
14041
                                    copy->blob_field->charset());
 
14042
        pos+=copy->length+sizeof(char*);
 
14043
      }
 
14044
      else
 
14045
      {
 
14046
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
14047
        pos+=copy->length+copy->blob_field->get_length();
 
14048
      }
 
14049
    }
 
14050
    else
 
14051
    {
 
14052
      if (copy->strip)
 
14053
      {
 
14054
        length= uint2korr(pos);
 
14055
        memcpy(copy->str, pos+2, length);
 
14056
        memset(copy->str+length, ' ', copy->length-length);
 
14057
        pos+= 2 + length;
 
14058
      }
 
14059
      else
 
14060
      {
 
14061
        memcpy(copy->str,pos,copy->length);
 
14062
        pos+=copy->length;
 
14063
      }
 
14064
    }
 
14065
  }
 
14066
  tab->cache.pos=pos;
 
14067
  return;
 
14068
}
 
14069
 
 
14070
 
5552
14071
/*
5553
14072
  eq_ref: Create the lookup key and check if it is the same as saved key
5554
14073
 
5555
14074
  SYNOPSIS
5556
14075
    cmp_buffer_with_ref()
5557
14076
      tab  Join tab of the accessed table
5558
 
 
5559
 
  DESCRIPTION
5560
 
    Used by eq_ref access method: create the index lookup key and check if
 
14077
 
 
14078
  DESCRIPTION 
 
14079
    Used by eq_ref access method: create the index lookup key and check if 
5561
14080
    we've used this key at previous lookup (If yes, we don't need to repeat
5562
14081
    the lookup - the record has been already fetched)
5563
14082
 
5564
 
  RETURN
 
14083
  RETURN 
5565
14084
    true   No cached record for the key, or failed to create the key (due to
5566
14085
           out-of-domain error)
5567
 
    false  The created key is the same as the previous one (and the record
 
14086
    false  The created key is the same as the previous one (and the record 
5568
14087
           is already in table->record)
5569
14088
*/
5570
 
static bool cmp_buffer_with_ref(JoinTable *tab)
 
14089
 
 
14090
static bool
 
14091
cmp_buffer_with_ref(JOIN_TAB *tab)
5571
14092
{
5572
14093
  bool no_prev_key;
5573
14094
  if (!tab->ref.disable_cache)
5578
14099
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5579
14100
    }
5580
14101
  }
5581
 
  else
 
14102
  else 
5582
14103
    no_prev_key= true;
5583
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
14104
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
 
14105
                                            &tab->ref)) ||
5584
14106
      no_prev_key)
5585
14107
    return 1;
5586
14108
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
5587
14109
    != 0;
5588
14110
}
5589
14111
 
5590
 
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
 
14112
 
 
14113
bool
 
14114
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
5591
14115
{
5592
 
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5593
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
14116
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
 
14117
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
14118
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
5594
14119
  bool result= 0;
5595
14120
 
5596
 
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
 
14121
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
5597
14122
  {
5598
14123
    if ((*copy)->copy() & 1)
5599
14124
    {
5601
14126
      break;
5602
14127
    }
5603
14128
  }
5604
 
  session->count_cuted_fields= save_count_cuted_fields;
 
14129
  thd->count_cuted_fields= save_count_cuted_fields;
 
14130
  dbug_tmp_restore_column_map(table->write_set, old_map);
5605
14131
  return result;
5606
14132
}
5607
14133
 
 
14134
 
5608
14135
/*****************************************************************************
5609
14136
  Group and order functions
5610
14137
*****************************************************************************/
5611
14138
 
5612
14139
/**
5613
 
  Resolve an order_st BY or GROUP BY column reference.
 
14140
  Resolve an ORDER BY or GROUP BY column reference.
5614
14141
 
5615
 
  Given a column reference (represented by 'order') from a GROUP BY or order_st
 
14142
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
5616
14143
  BY clause, find the actual column it represents. If the column being
5617
14144
  resolved is from the GROUP BY clause, the procedure searches the SELECT
5618
14145
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
5619
 
  the order_st BY clause, only the SELECT list is being searched.
 
14146
  the ORDER BY clause, only the SELECT list is being searched.
5620
14147
 
5621
14148
  If 'order' is resolved to an Item, then order->item is set to the found
5622
14149
  Item. If there is no item for the found column (that is, it was resolved
5625
14152
 
5626
14153
  ref_pointer_array and all_fields are updated.
5627
14154
 
5628
 
  @param[in] session                 Pointer to current thread structure
 
14155
  @param[in] thd                     Pointer to current thread structure
5629
14156
  @param[in,out] ref_pointer_array  All select, group and order by fields
5630
14157
  @param[in] tables                 List of tables to search in (usually
5631
14158
    FROM clause)
5634
14161
    SELECT list)
5635
14162
  @param[in,out] all_fields         All select, group and order by fields
5636
14163
  @param[in] is_group_field         True if order is a GROUP field, false if
5637
 
    order_st by field
 
14164
    ORDER by field
5638
14165
 
5639
14166
  @retval
5640
14167
    false if OK
5641
14168
  @retval
5642
14169
    true  if error occurred
5643
14170
*/
5644
 
static bool find_order_in_list(Session *session, 
5645
 
                               Item **ref_pointer_array, 
5646
 
                               TableList *tables,
5647
 
                               order_st *order,
5648
 
                               List<Item> &fields,
5649
 
                               List<Item> &all_fields,
5650
 
                               bool is_group_field)
 
14171
 
 
14172
static bool
 
14173
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
14174
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
 
14175
                   bool is_group_field)
5651
14176
{
5652
 
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
 
14177
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
5653
14178
  Item::Type order_item_type;
5654
14179
  Item **select_item; /* The corresponding item from the SELECT clause. */
5655
14180
  Field *from_field;  /* The corresponding field from the FROM clause. */
5656
 
  uint32_t counter;
 
14181
  uint counter;
5657
14182
  enum_resolution_type resolution;
5658
14183
 
5659
14184
  /*
5662
14187
  */
5663
14188
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
5664
14189
  {                                             /* Order by position */
5665
 
    uint32_t count= (uint32_t) order_item->val_int();
 
14190
    uint count= (uint) order_item->val_int();
5666
14191
    if (!count || count > fields.elements)
5667
14192
    {
5668
14193
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5669
 
               order_item->full_name(), session->where);
 
14194
               order_item->full_name(), thd->where);
5670
14195
      return true;
5671
14196
    }
5672
14197
    order->item= ref_pointer_array + count - 1;
5675
14200
    order->counter_used= 1;
5676
14201
    return false;
5677
14202
  }
5678
 
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
 
14203
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
5679
14204
  select_item= find_item_in_list(order_item, fields, &counter,
5680
14205
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5681
14206
  if (!select_item)
5692
14217
      for this name (in case if we would perform lookup in all tables).
5693
14218
    */
5694
14219
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
5695
 
        order_item->fix_fields(session, order->item))
 
14220
        order_item->fix_fields(thd, order->item))
5696
14221
      return true;
5697
14222
 
5698
14223
    /* Lookup the current GROUP field in the FROM clause. */
5701
14226
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5702
14227
        order_item_type == Item::REF_ITEM)
5703
14228
    {
5704
 
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5705
 
                                       NULL, &view_ref, IGNORE_ERRORS, false);
 
14229
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
 
14230
                                       NULL, &view_ref, IGNORE_ERRORS, true,
 
14231
                                       false);
5706
14232
      if (!from_field)
5707
14233
        from_field= (Field*) not_found_field;
5708
14234
    }
5740
14266
        warning so the user knows that the field from the FROM clause
5741
14267
        overshadows the column reference from the SELECT list.
5742
14268
      */
5743
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
14269
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5744
14270
                          ER(ER_NON_UNIQ_ERROR),
5745
14271
                          ((Item_ident*) order_item)->field_name,
5746
 
                          current_session->where);
 
14272
                          current_thd->where);
5747
14273
    }
5748
14274
  }
5749
14275
 
5760
14286
    arguments for which fix_fields already was called.
5761
14287
  */
5762
14288
  if (!order_item->fixed &&
5763
 
      (order_item->fix_fields(session, order->item) ||
 
14289
      (order_item->fix_fields(thd, order->item) ||
5764
14290
       (order_item= *order->item)->check_cols(1) ||
5765
 
       session->is_fatal_error))
 
14291
       thd->is_fatal_error))
5766
14292
    return true; /* Wrong field. */
5767
14293
 
5768
 
  uint32_t el= all_fields.elements;
 
14294
  uint el= all_fields.elements;
5769
14295
  all_fields.push_front(order_item); /* Add new field to field list. */
5770
14296
  ref_pointer_array[el]= order_item;
5771
14297
  order->item= ref_pointer_array + el;
5772
14298
  return false;
5773
14299
}
5774
14300
 
 
14301
 
5775
14302
/**
5776
14303
  Change order to point at item in select list.
5777
14304
 
5778
14305
  If item isn't a number and doesn't exits in the select list, add it the
5779
14306
  the field list.
5780
14307
*/
5781
 
int setup_order(Session *session,
5782
 
                Item **ref_pointer_array,
5783
 
                TableList *tables,
5784
 
                            List<Item> &fields,
5785
 
                List<Item> &all_fields,
5786
 
                order_st *order)
 
14308
 
 
14309
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
14310
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
5787
14311
{
5788
 
  session->where="order clause";
 
14312
  thd->where="order clause";
5789
14313
  for (; order; order=order->next)
5790
14314
  {
5791
 
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
 
14315
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
5792
14316
                           all_fields, false))
5793
14317
      return 1;
5794
14318
  }
5795
14319
  return 0;
5796
14320
}
5797
14321
 
 
14322
 
5798
14323
/**
5799
14324
  Intitialize the GROUP BY list.
5800
14325
 
5801
 
  @param session                        Thread Cursor
 
14326
  @param thd                    Thread handler
5802
14327
  @param ref_pointer_array      We store references to all fields that was
5803
14328
                               not in 'fields' here.
5804
14329
  @param fields         All fields in the select part. Any item in
5820
14345
  @retval
5821
14346
    1  error (probably out of memory)
5822
14347
*/
5823
 
int setup_group(Session *session,
5824
 
                Item **ref_pointer_array,
5825
 
                TableList *tables,
5826
 
                      List<Item> &fields,
5827
 
                List<Item> &all_fields,
5828
 
                order_st *order,
5829
 
                      bool *hidden_group_fields)
 
14348
 
 
14349
int
 
14350
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
 
14351
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
 
14352
            bool *hidden_group_fields)
5830
14353
{
5831
14354
  *hidden_group_fields=0;
5832
 
  order_st *ord;
 
14355
  ORDER *ord;
5833
14356
 
5834
14357
  if (!order)
5835
14358
    return 0;                           /* Everything is ok */
5836
14359
 
5837
 
  uint32_t org_fields=all_fields.elements;
 
14360
  uint org_fields=all_fields.elements;
5838
14361
 
5839
 
  session->where="group statement";
 
14362
  thd->where="group statement";
5840
14363
  for (ord= order; ord; ord= ord->next)
5841
14364
  {
5842
 
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
 
14365
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
5843
14366
                           all_fields, true))
5844
14367
      return 1;
5845
14368
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5870
14393
    Item_field *field;
5871
14394
    int cur_pos_in_select_list= 0;
5872
14395
    List_iterator<Item> li(fields);
5873
 
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
 
14396
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
5874
14397
 
5875
14398
    field= naf_it++;
5876
14399
    while (field && (item=li++))
5919
14442
  Try to use the fields in the order given by 'order' to allow one to
5920
14443
  optimize away 'order by'.
5921
14444
*/
5922
 
order_st *create_distinct_group(Session *session,
5923
 
                                Item **ref_pointer_array,
5924
 
                                order_st *order_list,
5925
 
                                List<Item> &fields,
5926
 
                                List<Item> &,
5927
 
                                bool *all_order_by_fields_used)
 
14445
 
 
14446
static ORDER *
 
14447
create_distinct_group(THD *thd, Item **ref_pointer_array,
 
14448
                      ORDER *order_list, List<Item> &fields,
 
14449
                      List<Item> &all_fields __attribute__((unused)),
 
14450
                      bool *all_order_by_fields_used)
5928
14451
{
5929
14452
  List_iterator<Item> li(fields);
5930
14453
  Item *item;
5931
 
  order_st *order,*group,**prev;
 
14454
  ORDER *order,*group,**prev;
5932
14455
 
5933
14456
  *all_order_by_fields_used= 1;
5934
14457
  while ((item=li++))
5939
14462
  {
5940
14463
    if (order->in_field_list)
5941
14464
    {
5942
 
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
 
14465
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
5943
14466
      if (!ord)
5944
 
        return 0;
 
14467
        return 0;
5945
14468
      *prev=ord;
5946
14469
      prev= &ord->next;
5947
14470
      (*ord->item)->marker=1;
5955
14478
  {
5956
14479
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5957
14480
    {
5958
 
      /*
5959
 
        Don't put duplicate columns from the SELECT list into the
 
14481
      /* 
 
14482
        Don't put duplicate columns from the SELECT list into the 
5960
14483
        GROUP BY list.
5961
14484
      */
5962
 
      order_st *ord_iter;
 
14485
      ORDER *ord_iter;
5963
14486
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5964
14487
        if ((*ord_iter->item)->eq(item, 1))
5965
14488
          goto next_item;
5966
 
 
5967
 
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
 
14489
      
 
14490
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
5968
14491
      if (!ord)
5969
 
        return 0;
 
14492
        return 0;
5970
14493
 
5971
14494
      /*
5972
14495
        We have here only field_list (not all_field_list), so we can use
5985
14508
  return group;
5986
14509
}
5987
14510
 
 
14511
 
5988
14512
/**
5989
14513
  Update join with count of the different type of fields.
5990
14514
*/
5991
 
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
 
14515
 
 
14516
void
 
14517
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
 
14518
                  List<Item> &fields, bool reset_with_sum_func)
5992
14519
{
5993
14520
  List_iterator<Item> li(fields);
5994
14521
  Item *field;
6005
14532
    {
6006
14533
      if (! field->const_item())
6007
14534
      {
6008
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
14535
        Item_sum *sum_item=(Item_sum*) field->real_item();
6009
14536
        if (!sum_item->depended_from() ||
6010
14537
            sum_item->depended_from() == select_lex)
6011
14538
        {
6013
14540
            param->quick_group=0;                       // UDF SUM function
6014
14541
          param->sum_func_count++;
6015
14542
 
6016
 
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
 
14543
          for (uint i=0 ; i < sum_item->arg_count ; i++)
6017
14544
          {
6018
14545
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
6019
14546
              param->field_count++;
6028
14555
    {
6029
14556
      param->func_count++;
6030
14557
      if (reset_with_sum_func)
6031
 
        field->with_sum_func=0;
6032
 
    }
6033
 
  }
6034
 
}
 
14558
        field->with_sum_func=0;
 
14559
    }
 
14560
  }
 
14561
}
 
14562
 
 
14563
 
 
14564
/**
 
14565
  Return 1 if second is a subpart of first argument.
 
14566
 
 
14567
  If first parts has different direction, change it to second part
 
14568
  (group is sorted like order)
 
14569
*/
 
14570
 
 
14571
static bool
 
14572
test_if_subpart(ORDER *a,ORDER *b)
 
14573
{
 
14574
  for (; a && b; a=a->next,b=b->next)
 
14575
  {
 
14576
    if ((*a->item)->eq(*b->item,1))
 
14577
      a->asc=b->asc;
 
14578
    else
 
14579
      return 0;
 
14580
  }
 
14581
  return test(!b);
 
14582
}
 
14583
 
 
14584
/**
 
14585
  Return table number if there is only one table in sort order
 
14586
  and group and order is compatible, else return 0.
 
14587
*/
 
14588
 
 
14589
static TABLE *
 
14590
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
 
14591
{
 
14592
  table_map map= (table_map) 0;
 
14593
 
 
14594
  if (!a)
 
14595
    a=b;                                        // Only one need to be given
 
14596
  else if (!b)
 
14597
    b=a;
 
14598
 
 
14599
  for (; a && b; a=a->next,b=b->next)
 
14600
  {
 
14601
    if (!(*a->item)->eq(*b->item,1))
 
14602
      return(0);
 
14603
    map|=a->item[0]->used_tables();
 
14604
  }
 
14605
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
14606
    return(0);
 
14607
 
 
14608
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
14609
  if (map != tables->table->map)
 
14610
    return(0);                          // More than one table
 
14611
  return(tables->table);
 
14612
}
 
14613
 
 
14614
 
 
14615
/**
 
14616
  calc how big buffer we need for comparing group entries.
 
14617
*/
 
14618
 
 
14619
static void
 
14620
calc_group_buffer(JOIN *join,ORDER *group)
 
14621
{
 
14622
  uint key_length=0, parts=0, null_parts=0;
 
14623
 
 
14624
  if (group)
 
14625
    join->group= 1;
 
14626
  for (; group ; group=group->next)
 
14627
  {
 
14628
    Item *group_item= *group->item;
 
14629
    Field *field= group_item->get_tmp_table_field();
 
14630
    if (field)
 
14631
    {
 
14632
      enum_field_types type;
 
14633
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
14634
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
 
14635
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
14636
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
14637
      else
 
14638
        key_length+= field->pack_length();
 
14639
    }
 
14640
    else
 
14641
    { 
 
14642
      switch (group_item->result_type()) {
 
14643
      case REAL_RESULT:
 
14644
        key_length+= sizeof(double);
 
14645
        break;
 
14646
      case INT_RESULT:
 
14647
        key_length+= sizeof(int64_t);
 
14648
        break;
 
14649
      case DECIMAL_RESULT:
 
14650
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
 
14651
                                                (group_item->decimals ? 1 : 0),
 
14652
                                                group_item->decimals);
 
14653
        break;
 
14654
      case STRING_RESULT:
 
14655
      {
 
14656
        enum enum_field_types type= group_item->field_type();
 
14657
        /*
 
14658
          As items represented as DATE/TIME fields in the group buffer
 
14659
          have STRING_RESULT result type, we increase the length 
 
14660
          by 8 as maximum pack length of such fields.
 
14661
        */
 
14662
        if (type == DRIZZLE_TYPE_TIME ||
 
14663
            type == DRIZZLE_TYPE_NEWDATE ||
 
14664
            type == DRIZZLE_TYPE_DATETIME ||
 
14665
            type == DRIZZLE_TYPE_TIMESTAMP)
 
14666
        {
 
14667
          key_length+= 8;
 
14668
        }
 
14669
        else
 
14670
        {
 
14671
          /*
 
14672
            Group strings are taken as varstrings and require an length field.
 
14673
            A field is not yet created by create_tmp_field()
 
14674
            and the sizes should match up.
 
14675
          */
 
14676
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
14677
        }
 
14678
        break;
 
14679
      }
 
14680
      default:
 
14681
        /* This case should never be choosen */
 
14682
        assert(0);
 
14683
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
14684
      }
 
14685
    }
 
14686
    parts++;
 
14687
    if (group_item->maybe_null)
 
14688
      null_parts++;
 
14689
  }
 
14690
  join->tmp_table_param.group_length=key_length+null_parts;
 
14691
  join->tmp_table_param.group_parts=parts;
 
14692
  join->tmp_table_param.group_null_parts=null_parts;
 
14693
}
 
14694
 
 
14695
 
 
14696
/**
 
14697
  allocate group fields or take prepared (cached).
 
14698
 
 
14699
  @param main_join   join of current select
 
14700
  @param curr_join   current join (join of current select or temporary copy
 
14701
                     of it)
 
14702
 
 
14703
  @retval
 
14704
    0   ok
 
14705
  @retval
 
14706
    1   failed
 
14707
*/
 
14708
 
 
14709
static bool
 
14710
make_group_fields(JOIN *main_join, JOIN *curr_join)
 
14711
{
 
14712
  if (main_join->group_fields_cache.elements)
 
14713
  {
 
14714
    curr_join->group_fields= main_join->group_fields_cache;
 
14715
    curr_join->sort_and_group= 1;
 
14716
  }
 
14717
  else
 
14718
  {
 
14719
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
14720
      return (1);
 
14721
    main_join->group_fields_cache= curr_join->group_fields;
 
14722
  }
 
14723
  return (0);
 
14724
}
 
14725
 
 
14726
 
 
14727
/**
 
14728
  Get a list of buffers for saveing last group.
 
14729
 
 
14730
  Groups are saved in reverse order for easyer check loop.
 
14731
*/
 
14732
 
 
14733
static bool
 
14734
alloc_group_fields(JOIN *join,ORDER *group)
 
14735
{
 
14736
  if (group)
 
14737
  {
 
14738
    for (; group ; group=group->next)
 
14739
    {
 
14740
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
 
14741
      if (!tmp || join->group_fields.push_front(tmp))
 
14742
        return true;
 
14743
    }
 
14744
  }
 
14745
  join->sort_and_group=1;                       /* Mark for do_select */
 
14746
  return false;
 
14747
}
 
14748
 
6035
14749
 
6036
14750
/*
6037
14751
  Test if a single-row cache of items changed, and update the cache.
6039
14753
  @details Test if a list of items that typically represents a result
6040
14754
  row has changed. If the value of some item changed, update the cached
6041
14755
  value for this item.
6042
 
 
 
14756
  
6043
14757
  @param list list of <item, cached_value> pairs stored as Cached_item.
6044
14758
 
6045
14759
  @return -1 if no item changed
6046
14760
  @return index of the first item that changed
6047
14761
*/
 
14762
 
6048
14763
int test_if_item_cache_changed(List<Cached_item> &list)
6049
14764
{
6050
14765
  List_iterator<Cached_item> li(list);
6059
14774
  return(idx);
6060
14775
}
6061
14776
 
 
14777
 
6062
14778
/**
6063
14779
  Setup copy_fields to save fields at start of new group.
6064
14780
 
6068
14784
  Change old item_field to use a new field with points at saved fieldvalue
6069
14785
  This function is only called before use of send_fields.
6070
14786
 
6071
 
  @param session                   Session pointer
 
14787
  @param thd                   THD pointer
6072
14788
  @param param                 temporary table parameters
6073
14789
  @param ref_pointer_array     array of pointers to top elements of filed list
6074
14790
  @param res_selected_fields   new list of items of select item list
6087
14803
  @retval
6088
14804
    !=0   error
6089
14805
*/
6090
 
bool setup_copy_fields(Session *session,
6091
 
                       Tmp_Table_Param *param,
6092
 
                       Item **ref_pointer_array,
6093
 
                       List<Item> &res_selected_fields,
6094
 
                       List<Item> &res_all_fields,
6095
 
                       uint32_t elements,
6096
 
                       List<Item> &all_fields)
 
14806
 
 
14807
bool
 
14808
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
 
14809
                  Item **ref_pointer_array,
 
14810
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
 
14811
                  uint elements, List<Item> &all_fields)
6097
14812
{
6098
14813
  Item *pos;
6099
14814
  List_iterator_fast<Item> li(all_fields);
6100
 
  CopyField *copy= NULL;
 
14815
  Copy_field *copy= NULL;
6101
14816
  res_selected_fields.empty();
6102
14817
  res_all_fields.empty();
6103
14818
  List_iterator_fast<Item> itr(res_all_fields);
6104
14819
  List<Item> extra_funcs;
6105
 
  uint32_t i, border= all_fields.elements - elements;
 
14820
  uint i, border= all_fields.elements - elements;
6106
14821
 
6107
 
  if (param->field_count &&
6108
 
      !(copy=param->copy_field= new CopyField[param->field_count]))
 
14822
  if (param->field_count && 
 
14823
      !(copy=param->copy_field= new Copy_field[param->field_count]))
6109
14824
    goto err2;
6110
14825
 
6111
14826
  param->copy_funcs.empty();
6112
14827
  for (i= 0; (pos= li++); i++)
6113
14828
  {
6114
14829
    Field *field;
6115
 
    unsigned char *tmp;
 
14830
    uchar *tmp;
6116
14831
    Item *real_pos= pos->real_item();
6117
14832
    if (real_pos->type() == Item::FIELD_ITEM)
6118
14833
    {
6119
14834
      Item_field *item;
6120
 
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
6121
 
        goto err;
 
14835
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
 
14836
        goto err;
6122
14837
      if (pos->type() == Item::REF_ITEM)
6123
14838
      {
6124
14839
        /* preserve the names of the ref when dereferncing */
6130
14845
      pos= item;
6131
14846
      if (item->field->flags & BLOB_FLAG)
6132
14847
      {
6133
 
        if (!(pos= new Item_copy_string(pos)))
6134
 
          goto err;
6135
 
            /*
6136
 
              Item_copy_string::copy for function can call
6137
 
              Item_copy_string::val_int for blob via Item_ref.
6138
 
              But if Item_copy_string::copy for blob isn't called before,
6139
 
              it's value will be wrong
6140
 
              so let's insert Item_copy_string for blobs in the beginning of
6141
 
              copy_funcs
6142
 
              (to see full test case look at having.test, BUG #4358)
6143
 
            */
6144
 
        if (param->copy_funcs.push_front(pos))
6145
 
          goto err;
 
14848
        if (!(pos= new Item_copy_string(pos)))
 
14849
          goto err;
 
14850
       /*
 
14851
         Item_copy_string::copy for function can call 
 
14852
         Item_copy_string::val_int for blob via Item_ref.
 
14853
         But if Item_copy_string::copy for blob isn't called before,
 
14854
         it's value will be wrong
 
14855
         so let's insert Item_copy_string for blobs in the beginning of 
 
14856
         copy_funcs
 
14857
         (to see full test case look at having.test, BUG #4358) 
 
14858
       */
 
14859
        if (param->copy_funcs.push_front(pos))
 
14860
          goto err;
6146
14861
      }
6147
14862
      else
6148
14863
      {
 
14864
        /* 
 
14865
           set up save buffer and change result_field to point at 
 
14866
           saved value
 
14867
        */
 
14868
        field= item->field;
 
14869
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
6149
14870
        /*
6150
 
          set up save buffer and change result_field to point at
6151
 
          saved value
 
14871
          We need to allocate one extra byte for null handling and
 
14872
          another extra byte to not get warnings from purify in
 
14873
          Field_varstring::val_int
6152
14874
        */
6153
 
        field= item->field;
6154
 
        item->result_field=field->new_field(session->mem_root,field->table, 1);
6155
 
              /*
6156
 
                We need to allocate one extra byte for null handling and
6157
 
                another extra byte to not get warnings from purify in
6158
 
                Field_varstring::val_int
6159
 
              */
6160
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
6161
 
          goto err;
 
14875
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
 
14876
          goto err;
6162
14877
        if (copy)
6163
14878
        {
6164
14879
          copy->set(tmp, item->result_field);
6178
14893
    {                                           // Save for send fields
6179
14894
      pos= real_pos;
6180
14895
      /* TODO:
6181
 
        In most cases this result will be sent to the user.
6182
 
        This should be changed to use copy_int or copy_real depending
6183
 
        on how the value is to be used: In some cases this may be an
6184
 
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
14896
         In most cases this result will be sent to the user.
 
14897
         This should be changed to use copy_int or copy_real depending
 
14898
         on how the value is to be used: In some cases this may be an
 
14899
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
6185
14900
      */
6186
14901
      if (!(pos=new Item_copy_string(pos)))
6187
 
        goto err;
6188
 
      if (i < border)                           // HAVING, order_st and GROUP BY
 
14902
        goto err;
 
14903
      if (i < border)                           // HAVING, ORDER and GROUP BY
6189
14904
      {
6190
14905
        if (extra_funcs.push_back(pos))
6191
14906
          goto err;
6192
14907
      }
6193
14908
      else if (param->copy_funcs.push_back(pos))
6194
 
        goto err;
 
14909
        goto err;
6195
14910
    }
6196
14911
    res_all_fields.push_back(pos);
6197
14912
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6203
14918
    itr++;
6204
14919
  itr.sublist(res_selected_fields, elements);
6205
14920
  /*
6206
 
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
 
14921
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
6207
14922
    reference used in these will resolve to a item that is already calculated
6208
14923
  */
6209
14924
  param->copy_funcs.concat(&extra_funcs);
6210
14925
 
6211
14926
  return(0);
6212
14927
 
6213
 
err:
 
14928
 err:
6214
14929
  if (copy)
6215
14930
    delete [] param->copy_field;                        // This is never 0
6216
14931
  param->copy_field=0;
6218
14933
  return(true);
6219
14934
}
6220
14935
 
 
14936
 
6221
14937
/**
6222
14938
  Make a copy of all simple SELECT'ed items.
6223
14939
 
6224
14940
  This is done at the start of a new group so that we can retrieve
6225
14941
  these later when the group changes.
6226
14942
*/
6227
 
void copy_fields(Tmp_Table_Param *param)
 
14943
 
 
14944
void
 
14945
copy_fields(TMP_TABLE_PARAM *param)
6228
14946
{
6229
 
  CopyField *ptr= param->copy_field;
6230
 
  CopyField *end= param->copy_field_end;
 
14947
  Copy_field *ptr=param->copy_field;
 
14948
  Copy_field *end=param->copy_field_end;
6231
14949
 
6232
14950
  for (; ptr != end; ptr++)
6233
14951
    (*ptr->do_copy)(ptr);
6238
14956
    item->copy();
6239
14957
}
6240
14958
 
 
14959
 
 
14960
/**
 
14961
  Make an array of pointers to sum_functions to speed up
 
14962
  sum_func calculation.
 
14963
 
 
14964
  @retval
 
14965
    0   ok
 
14966
  @retval
 
14967
    1   Error
 
14968
*/
 
14969
 
 
14970
bool JOIN::alloc_func_list()
 
14971
{
 
14972
  uint func_count, group_parts;
 
14973
 
 
14974
  func_count= tmp_table_param.sum_func_count;
 
14975
  /*
 
14976
    If we are using rollup, we need a copy of the summary functions for
 
14977
    each level
 
14978
  */
 
14979
  if (rollup.state != ROLLUP::STATE_NONE)
 
14980
    func_count*= (send_group_parts+1);
 
14981
 
 
14982
  group_parts= send_group_parts;
 
14983
  /*
 
14984
    If distinct, reserve memory for possible
 
14985
    disctinct->group_by optimization
 
14986
  */
 
14987
  if (select_distinct)
 
14988
  {
 
14989
    group_parts+= fields_list.elements;
 
14990
    /*
 
14991
      If the ORDER clause is specified then it's possible that
 
14992
      it also will be optimized, so reserve space for it too
 
14993
    */
 
14994
    if (order)
 
14995
    {
 
14996
      ORDER *ord;
 
14997
      for (ord= order; ord; ord= ord->next)
 
14998
        group_parts++;
 
14999
    }
 
15000
  }
 
15001
 
 
15002
  /* This must use calloc() as rollup_make_fields depends on this */
 
15003
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
 
15004
                                      sizeof(Item_sum***) * (group_parts+1));
 
15005
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
15006
  return(sum_funcs == 0);
 
15007
}
 
15008
 
 
15009
 
 
15010
/**
 
15011
  Initialize 'sum_funcs' array with all Item_sum objects.
 
15012
 
 
15013
  @param field_list        All items
 
15014
  @param send_fields       Items in select list
 
15015
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
15016
  @param recompute         Set to true if sum_funcs must be recomputed
 
15017
 
 
15018
  @retval
 
15019
    0  ok
 
15020
  @retval
 
15021
    1  error
 
15022
*/
 
15023
 
 
15024
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
 
15025
                              bool before_group_by, bool recompute)
 
15026
{
 
15027
  List_iterator_fast<Item> it(field_list);
 
15028
  Item_sum **func;
 
15029
  Item *item;
 
15030
 
 
15031
  if (*sum_funcs && !recompute)
 
15032
    return(false); /* We have already initialized sum_funcs. */
 
15033
 
 
15034
  func= sum_funcs;
 
15035
  while ((item=it++))
 
15036
  {
 
15037
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15038
        (!((Item_sum*) item)->depended_from() ||
 
15039
         ((Item_sum *)item)->depended_from() == select_lex))
 
15040
      *func++= (Item_sum*) item;
 
15041
  }
 
15042
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
15043
  {
 
15044
    rollup.state= ROLLUP::STATE_READY;
 
15045
    if (rollup_make_fields(field_list, send_fields, &func))
 
15046
      return(true);                     // Should never happen
 
15047
  }
 
15048
  else if (rollup.state == ROLLUP::STATE_NONE)
 
15049
  {
 
15050
    for (uint i=0 ; i <= send_group_parts ;i++)
 
15051
      sum_funcs_end[i]= func;
 
15052
  }
 
15053
  else if (rollup.state == ROLLUP::STATE_READY)
 
15054
    return(false);                         // Don't put end marker
 
15055
  *func=0;                                      // End marker
 
15056
  return(false);
 
15057
}
 
15058
 
 
15059
 
6241
15060
/**
6242
15061
  Change all funcs and sum_funcs to fields in tmp table, and create
6243
15062
  new list of all items.
6244
15063
 
6245
 
  @param session                   Session pointer
 
15064
  @param thd                   THD pointer
6246
15065
  @param ref_pointer_array     array of pointers to top elements of filed list
6247
15066
  @param res_selected_fields   new list of items of select item list
6248
15067
  @param res_all_fields        new list of all items
6254
15073
  @retval
6255
15074
    !=0   error
6256
15075
*/
6257
 
bool change_to_use_tmp_fields(Session *session,
6258
 
                              Item **ref_pointer_array,
6259
 
                                                List<Item> &res_selected_fields,
6260
 
                                                List<Item> &res_all_fields,
6261
 
                                                uint32_t elements,
6262
 
                              List<Item> &all_fields)
 
15076
 
 
15077
static bool
 
15078
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15079
                         List<Item> &res_selected_fields,
 
15080
                         List<Item> &res_all_fields,
 
15081
                         uint elements, List<Item> &all_fields)
6263
15082
{
6264
15083
  List_iterator_fast<Item> it(all_fields);
6265
15084
  Item *item_field,*item;
6267
15086
  res_selected_fields.empty();
6268
15087
  res_all_fields.empty();
6269
15088
 
6270
 
  uint32_t i, border= all_fields.elements - elements;
 
15089
  uint i, border= all_fields.elements - elements;
6271
15090
  for (i= 0; (item= it++); i++)
6272
15091
  {
6273
15092
    Field *field;
6280
15099
    {
6281
15100
      if (item->type() == Item::FIELD_ITEM)
6282
15101
      {
6283
 
        item_field= item->get_tmp_table_item(session);
 
15102
        item_field= item->get_tmp_table_item(thd);
6284
15103
      }
6285
15104
      else if ((field= item->get_tmp_table_field()))
6286
15105
      {
6287
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
6288
 
          item_field= ((Item_sum*) item)->result_item(field);
6289
 
        else
6290
 
          item_field= (Item*) new Item_field(field);
6291
 
        if (!item_field)
6292
 
          return(true);                    // Fatal error
 
15106
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
15107
          item_field= ((Item_sum*) item)->result_item(field);
 
15108
        else
 
15109
          item_field= (Item*) new Item_field(field);
 
15110
        if (!item_field)
 
15111
          return(true);                    // Fatal error
6293
15112
 
6294
15113
        if (item->real_item()->type() != Item::FIELD_ITEM)
6295
15114
          field->orig_table= 0;
6296
 
        item_field->name= item->name;
 
15115
        item_field->name= item->name;
6297
15116
        if (item->type() == Item::REF_ITEM)
6298
15117
        {
6299
15118
          Item_field *ifield= (Item_field *) item_field;
6303
15122
        }
6304
15123
      }
6305
15124
      else
6306
 
        item_field= item;
 
15125
        item_field= item;
6307
15126
    }
6308
15127
    res_all_fields.push_back(item_field);
6309
15128
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6317
15136
  return(false);
6318
15137
}
6319
15138
 
 
15139
 
6320
15140
/**
6321
15141
  Change all sum_func refs to fields to point at fields in tmp table.
6322
15142
  Change all funcs to be fields in tmp table.
6323
15143
 
6324
 
  @param session                   Session pointer
 
15144
  @param thd                   THD pointer
6325
15145
  @param ref_pointer_array     array of pointers to top elements of filed list
6326
15146
  @param res_selected_fields   new list of items of select item list
6327
15147
  @param res_all_fields        new list of all items
6333
15153
  @retval
6334
15154
    1   error
6335
15155
*/
6336
 
bool change_refs_to_tmp_fields(Session *session,
6337
 
                               Item **ref_pointer_array,
6338
 
                               List<Item> &res_selected_fields,
6339
 
                               List<Item> &res_all_fields,
6340
 
                               uint32_t elements,
6341
 
                                                 List<Item> &all_fields)
 
15156
 
 
15157
static bool
 
15158
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
 
15159
                          List<Item> &res_selected_fields,
 
15160
                          List<Item> &res_all_fields, uint elements,
 
15161
                          List<Item> &all_fields)
6342
15162
{
6343
15163
  List_iterator_fast<Item> it(all_fields);
6344
15164
  Item *item, *new_item;
6345
15165
  res_selected_fields.empty();
6346
15166
  res_all_fields.empty();
6347
15167
 
6348
 
  uint32_t i, border= all_fields.elements - elements;
 
15168
  uint i, border= all_fields.elements - elements;
6349
15169
  for (i= 0; (item= it++); i++)
6350
15170
  {
6351
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
 
15171
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
6352
15172
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
6353
15173
      new_item;
6354
15174
  }
6358
15178
    itr++;
6359
15179
  itr.sublist(res_selected_fields, elements);
6360
15180
 
6361
 
  return session->is_fatal_error;
 
15181
  return thd->is_fatal_error;
6362
15182
}
6363
15183
 
 
15184
 
 
15185
 
6364
15186
/******************************************************************************
6365
15187
  Code for calculating functions
6366
15188
******************************************************************************/
6367
15189
 
 
15190
 
6368
15191
/**
6369
15192
  Call ::setup for all sum functions.
6370
15193
 
6371
 
  @param session           thread Cursor
 
15194
  @param thd           thread handler
6372
15195
  @param func_ptr      sum function list
6373
15196
 
6374
15197
  @retval
6376
15199
  @retval
6377
15200
    true   error
6378
15201
*/
6379
 
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
15202
 
 
15203
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
6380
15204
{
6381
15205
  Item_sum *func;
6382
15206
  while ((func= *(func_ptr++)))
6383
15207
  {
6384
 
    if (func->setup(session))
 
15208
    if (func->setup(thd))
6385
15209
      return(true);
6386
15210
  }
6387
15211
  return(false);
6388
15212
}
6389
15213
 
6390
 
void init_tmptable_sum_functions(Item_sum **func_ptr)
 
15214
 
 
15215
static void
 
15216
init_tmptable_sum_functions(Item_sum **func_ptr)
6391
15217
{
6392
15218
  Item_sum *func;
6393
15219
  while ((func= *(func_ptr++)))
6394
15220
    func->reset_field();
6395
15221
}
6396
15222
 
 
15223
 
6397
15224
/** Update record 0 in tmp_table from record 1. */
6398
 
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
15225
 
 
15226
static void
 
15227
update_tmptable_sum_func(Item_sum **func_ptr,
 
15228
                         TABLE *tmp_table __attribute__((unused)))
6399
15229
{
6400
15230
  Item_sum *func;
6401
15231
  while ((func= *(func_ptr++)))
6402
15232
    func->update_field();
6403
15233
}
6404
15234
 
 
15235
 
6405
15236
/** Copy result of sum functions to record in tmp_table. */
6406
 
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
15237
 
 
15238
static void
 
15239
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6407
15240
{
6408
15241
  for (; func_ptr != end_ptr ; func_ptr++)
6409
15242
    (void) (*func_ptr)->save_in_result_field(1);
6410
15243
  return;
6411
15244
}
6412
15245
 
6413
 
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
15246
 
 
15247
static bool
 
15248
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6414
15249
{
6415
15250
  for (; func_ptr != end_ptr ;func_ptr++)
6416
15251
  {
6426
15261
  return 0;
6427
15262
}
6428
15263
 
6429
 
bool update_sum_func(Item_sum **func_ptr)
 
15264
 
 
15265
static bool
 
15266
update_sum_func(Item_sum **func_ptr)
6430
15267
{
6431
15268
  Item_sum *func;
6432
15269
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6436
15273
}
6437
15274
 
6438
15275
/** Copy result of functions to record in tmp_table. */
6439
 
void copy_funcs(Item **func_ptr)
 
15276
 
 
15277
void
 
15278
copy_funcs(Item **func_ptr)
6440
15279
{
6441
15280
  Item *func;
6442
15281
  for (; (func = *func_ptr) ; func_ptr++)
6443
15282
    func->save_in_result_field(1);
6444
15283
}
6445
15284
 
 
15285
 
 
15286
/**
 
15287
  Create a condition for a const reference and add this to the
 
15288
  currenct select for the table.
 
15289
*/
 
15290
 
 
15291
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
 
15292
{
 
15293
  if (!join_tab->ref.key_parts)
 
15294
    return(false);
 
15295
 
 
15296
  Item_cond_and *cond=new Item_cond_and();
 
15297
  TABLE *table=join_tab->table;
 
15298
  int error;
 
15299
  if (!cond)
 
15300
    return(true);
 
15301
 
 
15302
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
 
15303
  {
 
15304
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
15305
                              fieldnr-1];
 
15306
    Item *value=join_tab->ref.items[i];
 
15307
    cond->add(new Item_func_equal(new Item_field(field), value));
 
15308
  }
 
15309
  if (thd->is_fatal_error)
 
15310
    return(true);
 
15311
 
 
15312
  if (!cond->fixed)
 
15313
    cond->fix_fields(thd, (Item**)&cond);
 
15314
  if (join_tab->select)
 
15315
  {
 
15316
    error=(int) cond->add(join_tab->select->cond);
 
15317
    join_tab->select_cond=join_tab->select->cond=cond;
 
15318
  }
 
15319
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
15320
                                          &error)))
 
15321
    join_tab->select_cond=cond;
 
15322
 
 
15323
  return(error ? true : false);
 
15324
}
 
15325
 
 
15326
 
6446
15327
/**
6447
15328
  Free joins of subselect of this select.
6448
15329
 
6449
 
  @param session      Session pointer
6450
 
  @param select   pointer to Select_Lex which subselects joins we will free
 
15330
  @param thd      THD pointer
 
15331
  @param select   pointer to st_select_lex which subselects joins we will free
6451
15332
*/
6452
 
void free_underlaid_joins(Session *, Select_Lex *select)
 
15333
 
 
15334
void free_underlaid_joins(THD *thd __attribute__((unused)),
 
15335
                          SELECT_LEX *select)
6453
15336
{
6454
 
  for (Select_Lex_Unit *unit= select->first_inner_unit();
 
15337
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
6455
15338
       unit;
6456
15339
       unit= unit->next_unit())
6457
15340
    unit->cleanup();
6473
15356
  @b EXAMPLES
6474
15357
    @code
6475
15358
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
6476
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
 
15359
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
6477
15360
  @endcode
6478
15361
 
6479
15362
  @b IMPLEMENTATION
6486
15369
    This substitution is needed GROUP BY queries with ROLLUP if
6487
15370
    SELECT list contains expressions over group by attributes.
6488
15371
 
6489
 
  @param session                  reference to the context
 
15372
  @param thd                  reference to the context
6490
15373
  @param expr                 expression to make replacement
6491
15374
  @param group_list           list of references to group by items
6492
15375
  @param changed        out:  returns 1 if item contains a replaced field item
6493
15376
 
6494
15377
  @todo
6495
15378
    - TODO: Some functions are not null-preserving. For those functions
6496
 
    updating of the maybe_null attribute is an overkill.
 
15379
    updating of the maybe_null attribute is an overkill. 
6497
15380
 
6498
15381
  @retval
6499
15382
    0   if ok
6500
15383
  @retval
6501
15384
    1   on error
6502
15385
*/
6503
 
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
 
15386
 
 
15387
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
 
15388
                             bool *changed)
6504
15389
{
6505
15390
  if (expr->arg_count)
6506
15391
  {
6507
 
    Name_resolution_context *context= &session->lex->current_select->context;
 
15392
    Name_resolution_context *context= &thd->lex->current_select->context;
6508
15393
    Item **arg,**arg_end;
6509
15394
    bool arg_changed= false;
6510
15395
    for (arg= expr->arguments(),
6514
15399
      Item *item= *arg;
6515
15400
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6516
15401
      {
6517
 
        order_st *group_tmp;
 
15402
        ORDER *group_tmp;
6518
15403
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6519
15404
        {
6520
15405
          if (item->eq(*group_tmp->item,0))
6523
15408
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
6524
15409
                                        item->name)))
6525
15410
              return 1;                                 // fatal_error is set
6526
 
            session->change_item_tree(arg, new_item);
 
15411
            thd->change_item_tree(arg, new_item);
6527
15412
            arg_changed= true;
6528
15413
          }
6529
15414
        }
6530
15415
      }
6531
15416
      else if (item->type() == Item::FUNC_ITEM)
6532
15417
      {
6533
 
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
 
15418
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
6534
15419
          return 1;
6535
15420
      }
6536
15421
    }
6543
15428
  return 0;
6544
15429
}
6545
15430
 
 
15431
 
 
15432
/** Allocate memory needed for other rollup functions. */
 
15433
 
 
15434
bool JOIN::rollup_init()
 
15435
{
 
15436
  uint i,j;
 
15437
  Item **ref_array;
 
15438
 
 
15439
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
 
15440
  rollup.state= ROLLUP::STATE_INITED;
 
15441
 
 
15442
  /*
 
15443
    Create pointers to the different sum function groups
 
15444
    These are updated by rollup_make_fields()
 
15445
  */
 
15446
  tmp_table_param.group_parts= send_group_parts;
 
15447
 
 
15448
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
 
15449
                                                sizeof(Item**) +
 
15450
                                                sizeof(List<Item>) +
 
15451
                                                ref_pointer_array_size)
 
15452
                                                * send_group_parts )))
 
15453
    return 1;
 
15454
  
 
15455
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
15456
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
15457
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
15458
 
 
15459
  /*
 
15460
    Prepare space for field list for the different levels
 
15461
    These will be filled up in rollup_make_fields()
 
15462
  */
 
15463
  for (i= 0 ; i < send_group_parts ; i++)
 
15464
  {
 
15465
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
 
15466
    List<Item> *rollup_fields= &rollup.fields[i];
 
15467
    rollup_fields->empty();
 
15468
    rollup.ref_pointer_arrays[i]= ref_array;
 
15469
    ref_array+= all_fields.elements;
 
15470
  }
 
15471
  for (i= 0 ; i < send_group_parts; i++)
 
15472
  {
 
15473
    for (j=0 ; j < fields_list.elements ; j++)
 
15474
      rollup.fields[i].push_back(rollup.null_items[i]);
 
15475
  }
 
15476
  List_iterator<Item> it(all_fields);
 
15477
  Item *item;
 
15478
  while ((item= it++))
 
15479
  {
 
15480
    ORDER *group_tmp;
 
15481
    bool found_in_group= 0;
 
15482
 
 
15483
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
15484
    {
 
15485
      if (*group_tmp->item == item)
 
15486
      {
 
15487
        item->maybe_null= 1;
 
15488
        found_in_group= 1;
 
15489
        if (item->const_item())
 
15490
        {
 
15491
          /*
 
15492
            For ROLLUP queries each constant item referenced in GROUP BY list
 
15493
            is wrapped up into an Item_func object yielding the same value
 
15494
            as the constant item. The objects of the wrapper class are never
 
15495
            considered as constant items and besides they inherit all
 
15496
            properties of the Item_result_field class.
 
15497
            This wrapping allows us to ensure writing constant items
 
15498
            into temporary tables whenever the result of the ROLLUP
 
15499
            operation has to be written into a temporary table, e.g. when
 
15500
            ROLLUP is used together with DISTINCT in the SELECT list.
 
15501
            Usually when creating temporary tables for a intermidiate
 
15502
            result we do not include fields for constant expressions.
 
15503
          */           
 
15504
          Item* new_item= new Item_func_rollup_const(item);
 
15505
          if (!new_item)
 
15506
            return 1;
 
15507
          new_item->fix_fields(thd, (Item **) 0);
 
15508
          thd->change_item_tree(it.ref(), new_item);
 
15509
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
 
15510
          { 
 
15511
            if (*tmp->item == item)
 
15512
              thd->change_item_tree(tmp->item, new_item);
 
15513
          }
 
15514
        }
 
15515
      }
 
15516
    }
 
15517
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
15518
    {
 
15519
      bool changed= false;
 
15520
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
 
15521
        return 1;
 
15522
      /*
 
15523
        We have to prevent creation of a field in a temporary table for
 
15524
        an expression that contains GROUP BY attributes.
 
15525
        Marking the expression item as 'with_sum_func' will ensure this.
 
15526
      */ 
 
15527
      if (changed)
 
15528
        item->with_sum_func= 1;
 
15529
    }
 
15530
  }
 
15531
  return 0;
 
15532
}
 
15533
  
 
15534
 
 
15535
/**
 
15536
  Fill up rollup structures with pointers to fields to use.
 
15537
 
 
15538
  Creates copies of item_sum items for each sum level.
 
15539
 
 
15540
  @param fields_arg             List of all fields (hidden and real ones)
 
15541
  @param sel_fields             Pointer to selected fields
 
15542
  @param func                   Store here a pointer to all fields
 
15543
 
 
15544
  @retval
 
15545
    0   if ok;
 
15546
    In this case func is pointing to next not used element.
 
15547
  @retval
 
15548
    1    on error
 
15549
*/
 
15550
 
 
15551
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
 
15552
                              Item_sum ***func)
 
15553
{
 
15554
  List_iterator_fast<Item> it(fields_arg);
 
15555
  Item *first_field= sel_fields.head();
 
15556
  uint level;
 
15557
 
 
15558
  /*
 
15559
    Create field lists for the different levels
 
15560
 
 
15561
    The idea here is to have a separate field list for each rollup level to
 
15562
    avoid all runtime checks of which columns should be NULL.
 
15563
 
 
15564
    The list is stored in reverse order to get sum function in such an order
 
15565
    in func that it makes it easy to reset them with init_sum_functions()
 
15566
 
 
15567
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15568
 
 
15569
    rollup.fields[0] will contain list where a,b,c is NULL
 
15570
    rollup.fields[1] will contain list where b,c is NULL
 
15571
    ...
 
15572
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
15573
    ...
 
15574
    sum_funcs_end[0] points to all sum functions
 
15575
    sum_funcs_end[1] points to all sum functions, except grand totals
 
15576
    ...
 
15577
  */
 
15578
 
 
15579
  for (level=0 ; level < send_group_parts ; level++)
 
15580
  {
 
15581
    uint i;
 
15582
    uint pos= send_group_parts - level -1;
 
15583
    bool real_fields= 0;
 
15584
    Item *item;
 
15585
    List_iterator<Item> new_it(rollup.fields[pos]);
 
15586
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
15587
    ORDER *start_group;
 
15588
 
 
15589
    /* Point to first hidden field */
 
15590
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
15591
 
 
15592
    /* Remember where the sum functions ends for the previous level */
 
15593
    sum_funcs_end[pos+1]= *func;
 
15594
 
 
15595
    /* Find the start of the group for this level */
 
15596
    for (i= 0, start_group= group_list ;
 
15597
         i++ < pos ;
 
15598
         start_group= start_group->next)
 
15599
      ;
 
15600
 
 
15601
    it.rewind();
 
15602
    while ((item= it++))
 
15603
    {
 
15604
      if (item == first_field)
 
15605
      {
 
15606
        real_fields= 1;                         // End of hidden fields
 
15607
        ref_array= ref_array_start;
 
15608
      }
 
15609
 
 
15610
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
15611
          (!((Item_sum*) item)->depended_from() ||
 
15612
           ((Item_sum *)item)->depended_from() == select_lex))
 
15613
          
 
15614
      {
 
15615
        /*
 
15616
          This is a top level summary function that must be replaced with
 
15617
          a sum function that is reset for this level.
 
15618
 
 
15619
          NOTE: This code creates an object which is not that nice in a
 
15620
          sub select.  Fortunately it's not common to have rollup in
 
15621
          sub selects.
 
15622
        */
 
15623
        item= item->copy_or_same(thd);
 
15624
        ((Item_sum*) item)->make_unique();
 
15625
        *(*func)= (Item_sum*) item;
 
15626
        (*func)++;
 
15627
      }
 
15628
      else 
 
15629
      {
 
15630
        /* Check if this is something that is part of this group by */
 
15631
        ORDER *group_tmp;
 
15632
        for (group_tmp= start_group, i= pos ;
 
15633
             group_tmp ; group_tmp= group_tmp->next, i++)
 
15634
        {
 
15635
          if (*group_tmp->item == item)
 
15636
          {
 
15637
            /*
 
15638
              This is an element that is used by the GROUP BY and should be
 
15639
              set to NULL in this level
 
15640
            */
 
15641
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
 
15642
            if (!null_item)
 
15643
              return 1;
 
15644
            item->maybe_null= 1;                // Value will be null sometimes
 
15645
            null_item->result_field= item->get_tmp_table_field();
 
15646
            item= null_item;
 
15647
            break;
 
15648
          }
 
15649
        }
 
15650
      }
 
15651
      *ref_array= item;
 
15652
      if (real_fields)
 
15653
      {
 
15654
        (void) new_it++;                        // Point to next item
 
15655
        new_it.replace(item);                   // Replace previous
 
15656
        ref_array++;
 
15657
      }
 
15658
      else
 
15659
        ref_array--;
 
15660
    }
 
15661
  }
 
15662
  sum_funcs_end[0]= *func;                      // Point to last function
 
15663
  return 0;
 
15664
}
 
15665
 
 
15666
/**
 
15667
  Send all rollup levels higher than the current one to the client.
 
15668
 
 
15669
  @b SAMPLE
 
15670
    @code
 
15671
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
15672
  @endcode
 
15673
 
 
15674
  @param idx            Level we are on:
 
15675
                        - 0 = Total sum level
 
15676
                        - 1 = First group changed  (a)
 
15677
                        - 2 = Second group changed (a,b)
 
15678
 
 
15679
  @retval
 
15680
    0   ok
 
15681
  @retval
 
15682
    1   If send_data_failed()
 
15683
*/
 
15684
 
 
15685
int JOIN::rollup_send_data(uint idx)
 
15686
{
 
15687
  uint i;
 
15688
  for (i= send_group_parts ; i-- > idx ; )
 
15689
  {
 
15690
    /* Get reference pointers to sum functions in place */
 
15691
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15692
           ref_pointer_array_size);
 
15693
    if ((!having || having->val_int()))
 
15694
    {
 
15695
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
15696
          result->send_data(rollup.fields[i]))
 
15697
        return 1;
 
15698
      send_records++;
 
15699
    }
 
15700
  }
 
15701
  /* Restore ref_pointer_array */
 
15702
  set_items_ref_array(current_ref_pointer_array);
 
15703
  return 0;
 
15704
}
 
15705
 
 
15706
/**
 
15707
  Write all rollup levels higher than the current one to a temp table.
 
15708
 
 
15709
  @b SAMPLE
 
15710
    @code
 
15711
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
15712
  @endcode
 
15713
 
 
15714
  @param idx                 Level we are on:
 
15715
                               - 0 = Total sum level
 
15716
                               - 1 = First group changed  (a)
 
15717
                               - 2 = Second group changed (a,b)
 
15718
  @param table               reference to temp table
 
15719
 
 
15720
  @retval
 
15721
    0   ok
 
15722
  @retval
 
15723
    1   if write_data_failed()
 
15724
*/
 
15725
 
 
15726
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
 
15727
{
 
15728
  uint i;
 
15729
  for (i= send_group_parts ; i-- > idx ; )
 
15730
  {
 
15731
    /* Get reference pointers to sum functions in place */
 
15732
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
15733
           ref_pointer_array_size);
 
15734
    if ((!having || having->val_int()))
 
15735
    {
 
15736
      int write_error;
 
15737
      Item *item;
 
15738
      List_iterator_fast<Item> it(rollup.fields[i]);
 
15739
      while ((item= it++))
 
15740
      {
 
15741
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
15742
          item->save_in_result_field(1);
 
15743
      }
 
15744
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
15745
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
15746
      {
 
15747
        if (create_myisam_from_heap(thd, table_arg, 
 
15748
                                    tmp_table_param.start_recinfo,
 
15749
                                    &tmp_table_param.recinfo,
 
15750
                                    write_error, 0))
 
15751
          return 1;                  
 
15752
      }
 
15753
    }
 
15754
  }
 
15755
  /* Restore ref_pointer_array */
 
15756
  set_items_ref_array(current_ref_pointer_array);
 
15757
  return 0;
 
15758
}
 
15759
 
 
15760
/**
 
15761
  clear results if there are not rows found for group
 
15762
  (end_send_group/end_write_group)
 
15763
*/
 
15764
 
 
15765
void JOIN::clear()
 
15766
{
 
15767
  clear_tables(this);
 
15768
  copy_fields(&tmp_table_param);
 
15769
 
 
15770
  if (sum_funcs)
 
15771
  {
 
15772
    Item_sum *func, **func_ptr= sum_funcs;
 
15773
    while ((func= *(func_ptr++)))
 
15774
      func->clear();
 
15775
  }
 
15776
}
 
15777
 
6546
15778
/**
6547
15779
  EXPLAIN handling.
6548
15780
 
6549
15781
  Send a description about what how the select will be done to stdout.
6550
15782
*/
 
15783
 
6551
15784
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
6552
15785
                     bool distinct,const char *message)
6553
15786
{
6554
15787
  List<Item> field_list;
6555
15788
  List<Item> item_list;
6556
 
  Session *session=join->session;
 
15789
  THD *thd=join->thd;
6557
15790
  select_result *result=join->result;
6558
15791
  Item *item_null= new Item_null();
6559
15792
  const CHARSET_INFO * const cs= system_charset_info;
6560
15793
  int quick_type;
6561
15794
  /* Don't log this into the slow query log */
6562
 
  session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
15795
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
6563
15796
  join->unit->offset_limit_cnt= 0;
6564
15797
 
6565
 
  /*
 
15798
  /* 
6566
15799
    NOTE: the number/types of items pushed into item_list must be in sync with
6567
 
    EXPLAIN column types as they're "defined" in Session::send_explain_fields()
 
15800
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
6568
15801
  */
6569
15802
  if (message)
6570
15803
  {
6572
15805
                                     join->select_lex->select_number));
6573
15806
    item_list.push_back(new Item_string(join->select_lex->type,
6574
15807
                                        strlen(join->select_lex->type), cs));
6575
 
    for (uint32_t i=0 ; i < 7; i++)
6576
 
      item_list.push_back(item_null);
6577
 
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
6578
 
      item_list.push_back(item_null);
6579
 
 
 
15808
    for (uint i=0 ; i < 7; i++)
 
15809
      item_list.push_back(item_null);
 
15810
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
15811
      item_list.push_back(item_null);
 
15812
  
6580
15813
    item_list.push_back(new Item_string(message,strlen(message),cs));
6581
15814
    if (result->send_data(item_list))
6582
15815
      join->error= 1;
6583
15816
  }
6584
15817
  else if (join->select_lex == join->unit->fake_select_lex)
6585
15818
  {
6586
 
    /*
 
15819
    /* 
6587
15820
      here we assume that the query will return at least two rows, so we
6588
15821
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
6589
15822
      and no filesort will be actually done, but executing all selects in
6600
15833
                                        cs));
6601
15834
    /* table */
6602
15835
    {
6603
 
      Select_Lex *sl= join->unit->first_select();
6604
 
      uint32_t len= 6, lastop= 0;
 
15836
      SELECT_LEX *sl= join->unit->first_select();
 
15837
      uint len= 6, lastop= 0;
6605
15838
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
6606
15839
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
6607
15840
      {
6622
15855
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
6623
15856
    }
6624
15857
    /* type */
6625
 
    item_list.push_back(new Item_string(access_method_str[AM_ALL].c_str(),
6626
 
                                        access_method_str[AM_ALL].length(),
6627
 
                                        cs));
 
15858
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
 
15859
                                          strlen(join_type_str[JT_ALL]),
 
15860
                                          cs));
6628
15861
    /* possible_keys */
6629
15862
    item_list.push_back(item_null);
6630
15863
    /* key*/
6634
15867
    /* ref */
6635
15868
    item_list.push_back(item_null);
6636
15869
    /* in_rows */
6637
 
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
 
15870
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
6638
15871
      item_list.push_back(item_null);
6639
15872
    /* rows */
6640
15873
    item_list.push_back(item_null);
6651
15884
  else
6652
15885
  {
6653
15886
    table_map used_tables=0;
6654
 
    for (uint32_t i=0 ; i < join->tables ; i++)
 
15887
    for (uint i=0 ; i < join->tables ; i++)
6655
15888
    {
6656
 
      JoinTable *tab=join->join_tab+i;
6657
 
      Table *table=tab->table;
6658
 
      char buff[512];
 
15889
      JOIN_TAB *tab=join->join_tab+i;
 
15890
      TABLE *table=tab->table;
 
15891
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
 
15892
      char buff[512]; 
6659
15893
      char buff1[512], buff2[512], buff3[512];
6660
15894
      char keylen_str_buf[64];
6661
15895
      String extra(buff, sizeof(buff),cs);
6677
15911
      item_list.push_back(new Item_string(join->select_lex->type,
6678
15912
                                          strlen(join->select_lex->type),
6679
15913
                                          cs));
6680
 
      if (tab->type == AM_ALL && tab->select && tab->select->quick)
 
15914
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
6681
15915
      {
6682
15916
        quick_type= tab->select->quick->get_type();
6683
15917
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
6684
15918
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
6685
15919
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
6686
 
          tab->type = AM_INDEX_MERGE;
 
15920
          tab->type = JT_INDEX_MERGE;
6687
15921
        else
6688
 
          tab->type = AM_RANGE;
 
15922
          tab->type = JT_RANGE;
6689
15923
      }
6690
15924
      /* table */
6691
15925
      if (table->derived_select_number)
6698
15932
      }
6699
15933
      else
6700
15934
      {
6701
 
        TableList *real_table= table->pos_in_table_list;
 
15935
        TABLE_LIST *real_table= table->pos_in_table_list; 
6702
15936
        item_list.push_back(new Item_string(real_table->alias,
6703
15937
                                            strlen(real_table->alias),
6704
15938
                                            cs));
6705
15939
      }
6706
15940
      /* "type" column */
6707
 
      item_list.push_back(new Item_string(access_method_str[tab->type].c_str(),
6708
 
                                          access_method_str[tab->type].length(),
 
15941
      item_list.push_back(new Item_string(join_type_str[tab->type],
 
15942
                                          strlen(join_type_str[tab->type]),
6709
15943
                                          cs));
6710
15944
      /* Build "possible_keys" value and add it to item_list */
6711
 
      if (tab->keys.any())
 
15945
      if (!tab->keys.is_clear_all())
6712
15946
      {
6713
 
        uint32_t j;
 
15947
        uint j;
6714
15948
        for (j=0 ; j < table->s->keys ; j++)
6715
15949
        {
6716
 
          if (tab->keys.test(j))
 
15950
          if (tab->keys.is_set(j))
6717
15951
          {
6718
15952
            if (tmp1.length())
6719
15953
              tmp1.append(',');
6720
 
            tmp1.append(table->key_info[j].name,
 
15954
            tmp1.append(table->key_info[j].name, 
6721
15955
                        strlen(table->key_info[j].name),
6722
15956
                        system_charset_info);
6723
15957
          }
6732
15966
      if (tab->ref.key_parts)
6733
15967
      {
6734
15968
        KEY *key_info=table->key_info+ tab->ref.key;
6735
 
        register uint32_t length;
 
15969
        register uint length;
6736
15970
        item_list.push_back(new Item_string(key_info->name,
6737
15971
                                            strlen(key_info->name),
6738
15972
                                            system_charset_info));
6739
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
 
15973
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
6740
15974
                keylen_str_buf;
6741
15975
        item_list.push_back(new Item_string(keylen_str_buf, length,
6742
15976
                                            system_charset_info));
6743
 
        for (StoredKey **ref=tab->ref.key_copy ; *ref ; ref++)
 
15977
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
6744
15978
        {
6745
15979
          if (tmp2.length())
6746
15980
            tmp2.append(',');
6749
15983
        }
6750
15984
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
6751
15985
      }
6752
 
      else if (tab->type == AM_NEXT)
 
15986
      else if (tab->type == JT_NEXT)
6753
15987
      {
6754
15988
        KEY *key_info=table->key_info+ tab->index;
6755
 
        register uint32_t length;
 
15989
        register uint length;
6756
15990
        item_list.push_back(new Item_string(key_info->name,
6757
15991
                                            strlen(key_info->name),cs));
6758
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
 
15992
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
6759
15993
                keylen_str_buf;
6760
 
        item_list.push_back(new Item_string(keylen_str_buf,
 
15994
        item_list.push_back(new Item_string(keylen_str_buf, 
6761
15995
                                            length,
6762
15996
                                            system_charset_info));
6763
15997
        item_list.push_back(item_null);
6771
16005
      }
6772
16006
      else
6773
16007
      {
 
16008
        if (table_list->schema_table &&
 
16009
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16010
        {
 
16011
          const char *tmp_buff;
 
16012
          int f_idx;
 
16013
          if (table_list->has_db_lookup_value)
 
16014
          {
 
16015
            f_idx= table_list->schema_table->idx_field1;
 
16016
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
16017
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
16018
          }          
 
16019
          if (table_list->has_table_lookup_value)
 
16020
          {
 
16021
            if (table_list->has_db_lookup_value)
 
16022
              tmp2.append(',');
 
16023
            f_idx= table_list->schema_table->idx_field2;
 
16024
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
 
16025
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
 
16026
          }
 
16027
          if (tmp2.length())
 
16028
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
16029
          else
 
16030
            item_list.push_back(item_null);
 
16031
        }
 
16032
        else
 
16033
          item_list.push_back(item_null);
 
16034
        item_list.push_back(item_null);
 
16035
        item_list.push_back(item_null);
 
16036
      }
 
16037
      
 
16038
      /* Add "rows" field to item_list. */
 
16039
      if (table_list->schema_table)
 
16040
      {
 
16041
        /* in_rows */
 
16042
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
 
16043
          item_list.push_back(item_null);
 
16044
        /* rows */
6774
16045
        item_list.push_back(item_null);
6775
 
        item_list.push_back(item_null);
6776
 
        item_list.push_back(item_null);
6777
16046
      }
6778
 
 
6779
 
      /* Add "rows" field to item_list. */
6780
 
      double examined_rows;
6781
 
      if (tab->select && tab->select->quick)
6782
 
        examined_rows= rows2double(tab->select->quick->records);
6783
 
      else if (tab->type == AM_NEXT || tab->type == AM_ALL)
6784
 
        examined_rows= rows2double(tab->limit ? tab->limit :
6785
 
            tab->table->cursor->records());
6786
16047
      else
6787
16048
      {
6788
 
        optimizer::Position cur_pos= join->getPosFromOptimalPlan(i);
6789
 
        examined_rows= cur_pos.getFanout();
6790
 
      }
6791
 
 
6792
 
      item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
6793
 
            MY_INT64_NUM_DECIMAL_DIGITS));
6794
 
 
6795
 
      /* Add "filtered" field to item_list. */
6796
 
      if (join->session->lex->describe & DESCRIBE_EXTENDED)
6797
 
      {
6798
 
        float f= 0.0;
6799
 
        if (examined_rows)
 
16049
        double examined_rows;
 
16050
        if (tab->select && tab->select->quick)
 
16051
          examined_rows= rows2double(tab->select->quick->records);
 
16052
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
 
16053
          examined_rows= rows2double(tab->limit ? tab->limit : 
 
16054
                                     tab->table->file->records());
 
16055
        else
 
16056
          examined_rows= join->best_positions[i].records_read; 
 
16057
 
 
16058
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
 
16059
                                         MY_INT64_NUM_DECIMAL_DIGITS));
 
16060
 
 
16061
        /* Add "filtered" field to item_list. */
 
16062
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
6800
16063
        {
6801
 
          optimizer::Position cur_pos= join->getPosFromOptimalPlan(i);
6802
 
          f= (float) (100.0 * cur_pos.getFanout() /
6803
 
              examined_rows);
 
16064
          float f= 0.0; 
 
16065
          if (examined_rows)
 
16066
            f= (float) (100.0 * join->best_positions[i].records_read /
 
16067
                        examined_rows);
 
16068
          item_list.push_back(new Item_float(f, 2));
6804
16069
        }
6805
 
        item_list.push_back(new Item_float(f, 2));
6806
16070
      }
6807
16071
 
6808
16072
      /* Build "Extra" field and add it to item_list. */
6809
16073
      bool key_read=table->key_read;
6810
 
      if ((tab->type == AM_NEXT || tab->type == AM_CONST) &&
6811
 
          table->covering_keys.test(tab->index))
 
16074
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
 
16075
          table->covering_keys.is_set(tab->index))
6812
16076
        key_read=1;
6813
16077
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
6814
16078
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
6815
16079
        key_read=1;
6816
 
 
 
16080
        
6817
16081
      if (tab->info)
6818
16082
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
6819
16083
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
6836
16100
      }
6837
16101
      else
6838
16102
      {
6839
 
        uint32_t keyno= MAX_KEY;
 
16103
        uint keyno= MAX_KEY;
6840
16104
        if (tab->ref.key_parts)
6841
16105
          keyno= tab->ref.key;
6842
16106
        else if (tab->select && tab->select->quick)
6843
16107
          keyno = tab->select->quick->index;
6844
16108
 
6845
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
 
16109
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
 
16110
            table->file->pushed_idx_cond)
 
16111
          extra.append(STRING_WITH_LEN("; Using index condition"));
 
16112
 
 
16113
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
6846
16114
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
6847
16115
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
6848
16116
        {
6853
16121
        {
6854
16122
          if (tab->use_quick == 2)
6855
16123
          {
6856
 
            /*
6857
 
             * To print out the bitset in tab->keys, we go through
6858
 
             * it 32 bits at a time. We need to do this to ensure
6859
 
             * that the to_ulong() method will not throw an
6860
 
             * out_of_range exception at runtime which would happen
6861
 
             * if the bitset we were working with was larger than 64
6862
 
             * bits on a 64-bit platform (for example).
6863
 
             */
6864
 
            stringstream s, w;
6865
 
            string str;
6866
 
            w << tab->keys;
6867
 
            w >> str;
6868
 
            for (uint32_t pos= 0; pos < tab->keys.size(); pos+= 32)
6869
 
            {
6870
 
              bitset<32> tmp(str, pos, 32);
6871
 
              if (tmp.any())
6872
 
                s << uppercase << hex << tmp.to_ulong();
6873
 
            }
 
16124
            /* 4 bits per 1 hex digit + terminating '\0' */
 
16125
            char buf[MAX_KEY / 4 + 1];
6874
16126
            extra.append(STRING_WITH_LEN("; Range checked for each "
6875
16127
                                         "record (index map: 0x"));
6876
 
            extra.append(s.str().c_str());
 
16128
            extra.append(tab->keys.print(buf));
6877
16129
            extra.append(')');
6878
16130
          }
6879
16131
          else if (tab->select->cond)
6880
16132
          {
6881
 
            extra.append(STRING_WITH_LEN("; Using where"));
 
16133
            const COND *pushed_cond= tab->table->file->pushed_cond;
 
16134
 
 
16135
            if (thd->variables.engine_condition_pushdown && pushed_cond)
 
16136
            {
 
16137
              extra.append(STRING_WITH_LEN("; Using where with pushed "
 
16138
                                           "condition"));
 
16139
              if (thd->lex->describe & DESCRIBE_EXTENDED)
 
16140
              {
 
16141
                extra.append(STRING_WITH_LEN(": "));
 
16142
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
 
16143
              }
 
16144
            }
 
16145
            else
 
16146
              extra.append(STRING_WITH_LEN("; Using where"));
6882
16147
          }
6883
16148
        }
6884
16149
        if (key_read)
6890
16155
        }
6891
16156
        if (table->reginfo.not_exists_optimize)
6892
16157
          extra.append(STRING_WITH_LEN("; Not exists"));
 
16158
          
 
16159
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
 
16160
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
 
16161
             HA_MRR_USE_DEFAULT_IMPL))
 
16162
        {
 
16163
          extra.append(STRING_WITH_LEN("; Using MRR"));
 
16164
        }
6893
16165
 
 
16166
        if (table_list->schema_table &&
 
16167
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
 
16168
        {
 
16169
          if (!table_list->table_open_method)
 
16170
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
 
16171
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
 
16172
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
 
16173
          else
 
16174
            extra.append(STRING_WITH_LEN("; Open_full_table"));
 
16175
          if (table_list->has_db_lookup_value &&
 
16176
              table_list->has_table_lookup_value)
 
16177
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
 
16178
          else if (table_list->has_db_lookup_value ||
 
16179
                   table_list->has_table_lookup_value)
 
16180
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
 
16181
          else
 
16182
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
 
16183
        }
6894
16184
        if (need_tmp_table)
6895
16185
        {
6896
16186
          need_tmp_table=0;
6901
16191
          need_order=0;
6902
16192
          extra.append(STRING_WITH_LEN("; Using filesort"));
6903
16193
        }
6904
 
        if (distinct & test_all_bits(used_tables,session->used_tables))
 
16194
        if (distinct & test_all_bits(used_tables,thd->used_tables))
6905
16195
          extra.append(STRING_WITH_LEN("; Distinct"));
6906
16196
 
6907
16197
        if (tab->insideout_match_tab)
6909
16199
          extra.append(STRING_WITH_LEN("; LooseScan"));
6910
16200
        }
6911
16201
 
6912
 
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
 
16202
        if (tab->flush_weedout_table)
 
16203
          extra.append(STRING_WITH_LEN("; Start temporary"));
 
16204
        else if (tab->check_weed_out_table)
 
16205
          extra.append(STRING_WITH_LEN("; End temporary"));
 
16206
        else if (tab->do_firstmatch)
 
16207
        {
 
16208
          extra.append(STRING_WITH_LEN("; FirstMatch("));
 
16209
          TABLE *prev_table=tab->do_firstmatch->table;
 
16210
          if (prev_table->derived_select_number)
 
16211
          {
 
16212
            char namebuf[NAME_LEN];
 
16213
            /* Derived table name generation */
 
16214
            int len= snprintf(namebuf, sizeof(namebuf)-1,
 
16215
                              "<derived%u>",
 
16216
                              prev_table->derived_select_number);
 
16217
            extra.append(namebuf, len);
 
16218
          }
 
16219
          else
 
16220
            extra.append(prev_table->pos_in_table_list->alias);
 
16221
          extra.append(STRING_WITH_LEN(")"));
 
16222
        }
 
16223
 
 
16224
        for (uint part= 0; part < tab->ref.key_parts; part++)
6913
16225
        {
6914
16226
          if (tab->ref.cond_guards[part])
6915
16227
          {
6937
16249
        join->error= 1;
6938
16250
    }
6939
16251
  }
6940
 
  for (Select_Lex_Unit *unit= join->select_lex->first_inner_unit();
 
16252
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
6941
16253
       unit;
6942
16254
       unit= unit->next_unit())
6943
16255
  {
6944
 
    if (mysql_explain_union(session, unit, result))
 
16256
    if (mysql_explain_union(thd, unit, result))
6945
16257
      return;
6946
16258
  }
6947
16259
  return;
6948
16260
}
6949
16261
 
6950
 
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit, select_result *result)
 
16262
 
 
16263
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
6951
16264
{
6952
 
  bool res= false;
6953
 
  Select_Lex *first= unit->first_select();
 
16265
  bool res= 0;
 
16266
  SELECT_LEX *first= unit->first_select();
6954
16267
 
6955
 
  for (Select_Lex *sl= first;
 
16268
  for (SELECT_LEX *sl= first;
6956
16269
       sl;
6957
16270
       sl= sl->next_select())
6958
16271
  {
6959
16272
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
6960
16273
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
6961
 
    sl->type= (((&session->lex->select_lex)==sl)?
6962
 
               (sl->first_inner_unit() || sl->next_select() ?
 
16274
    sl->type= (((&thd->lex->select_lex)==sl)?
 
16275
               (sl->first_inner_unit() || sl->next_select() ? 
6963
16276
                "PRIMARY" : "SIMPLE"):
6964
16277
               ((sl == first)?
6965
16278
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
6979
16292
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
6980
16293
    unit->fake_select_lex->type= "UNION RESULT";
6981
16294
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
6982
 
    if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
 
16295
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
6983
16296
      res= unit->exec();
6984
16297
    res|= unit->cleanup();
6985
16298
  }
6986
16299
  else
6987
16300
  {
6988
 
    session->lex->current_select= first;
 
16301
    thd->lex->current_select= first;
6989
16302
    unit->set_limit(unit->global_parameters);
6990
 
    res= mysql_select(session, &first->ref_pointer_array,
6991
 
                        (TableList*) first->table_list.first,
 
16303
    res= mysql_select(thd, &first->ref_pointer_array,
 
16304
                        (TABLE_LIST*) first->table_list.first,
6992
16305
                        first->with_wild, first->item_list,
6993
16306
                        first->where,
6994
16307
                        first->order_list.elements +
6995
16308
                        first->group_list.elements,
6996
 
                        (order_st*) first->order_list.first,
6997
 
                        (order_st*) first->group_list.first,
 
16309
                        (ORDER*) first->order_list.first,
 
16310
                        (ORDER*) first->group_list.first,
6998
16311
                        first->having,
6999
 
                        first->options | session->options | SELECT_DESCRIBE,
 
16312
                        (ORDER*) thd->lex->proc_list.first,
 
16313
                        first->options | thd->options | SELECT_DESCRIBE,
7000
16314
                        result, unit, first);
7001
16315
  }
7002
 
  return(res || session->is_error());
 
16316
  return(res || thd->is_error());
7003
16317
}
7004
16318
 
7005
 
static void print_table_array(Session *session, String *str, TableList **table,
7006
 
                              TableList **end)
 
16319
 
 
16320
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
 
16321
                              TABLE_LIST **end)
7007
16322
{
7008
 
  (*table)->print(session, str, QT_ORDINARY);
 
16323
  (*table)->print(thd, str, QT_ORDINARY);
7009
16324
 
7010
 
  for (TableList **tbl= table + 1; tbl < end; tbl++)
 
16325
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
7011
16326
  {
7012
 
    TableList *curr= *tbl;
 
16327
    TABLE_LIST *curr= *tbl;
7013
16328
    if (curr->outer_join)
7014
16329
    {
7015
16330
      /* MySQL converts right to left joins */
7017
16332
    }
7018
16333
    else if (curr->straight)
7019
16334
      str->append(STRING_WITH_LEN(" straight_join "));
 
16335
    else if (curr->sj_inner_tables)
 
16336
      str->append(STRING_WITH_LEN(" semi join "));
7020
16337
    else
7021
16338
      str->append(STRING_WITH_LEN(" join "));
7022
 
    curr->print(session, str, QT_ORDINARY);
 
16339
    curr->print(thd, str, QT_ORDINARY);
7023
16340
    if (curr->on_expr)
7024
16341
    {
7025
16342
      str->append(STRING_WITH_LEN(" on("));
7029
16346
  }
7030
16347
}
7031
16348
 
 
16349
 
7032
16350
/**
7033
16351
  Print joins from the FROM clause.
7034
 
  @param session     thread Cursor
 
16352
  @param thd     thread handler
7035
16353
  @param str     string where table should be printed
7036
16354
  @param tables  list of tables in join
7037
16355
  @query_type    type of the query is being generated
7038
16356
*/
7039
 
void print_join(Session *session, String *str,
7040
 
                List<TableList> *tables, enum_query_type)
 
16357
 
 
16358
static void print_join(THD *thd,
 
16359
                       String *str,
 
16360
                       List<TABLE_LIST> *tables,
 
16361
                       enum_query_type query_type __attribute__((unused)))
7041
16362
{
7042
16363
  /* List is reversed => we should reverse it before using */
7043
 
  List_iterator_fast<TableList> ti(*tables);
7044
 
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
 
16364
  List_iterator_fast<TABLE_LIST> ti(*tables);
 
16365
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
7045
16366
                                                tables->elements);
7046
16367
  if (table == 0)
7047
16368
    return;  // out of memory
7048
16369
 
7049
 
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
 
16370
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
7050
16371
    *t= ti++;
 
16372
  
 
16373
  /* 
 
16374
    If the first table is a semi-join nest, swap it with something that is
 
16375
    not a semi-join nest.
 
16376
  */
 
16377
  if ((*table)->sj_inner_tables)
 
16378
  {
 
16379
    TABLE_LIST **end= table + tables->elements;
 
16380
    for (TABLE_LIST **t2= table; t2!=end; t2++)
 
16381
    {
 
16382
      if (!(*t2)->sj_inner_tables)
 
16383
      {
 
16384
        TABLE_LIST *tmp= *t2;
 
16385
        *t2= *table;
 
16386
        *table= tmp;
 
16387
        break;
 
16388
      }
 
16389
    }
 
16390
  }
7051
16391
  assert(tables->elements >= 1);
7052
 
  print_table_array(session, str, table, table + tables->elements);
7053
 
}
7054
 
 
7055
 
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
7056
 
{
7057
 
  /* QQ: session may not be set for sub queries, but this should be fixed */
7058
 
  if (!session)
7059
 
    session= current_session;
 
16392
  print_table_array(thd, str, table, table + tables->elements);
 
16393
}
 
16394
 
 
16395
 
 
16396
/**
 
16397
  @brief Print an index hint
 
16398
 
 
16399
  @details Prints out the USE|FORCE|IGNORE index hint.
 
16400
 
 
16401
  @param      thd         the current thread
 
16402
  @param[out] str         appends the index hint here
 
16403
  @param      hint        what the hint is (as string : "USE INDEX"|
 
16404
                          "FORCE INDEX"|"IGNORE INDEX")
 
16405
  @param      hint_length the length of the string in 'hint'
 
16406
  @param      indexes     a list of index names for the hint
 
16407
*/
 
16408
 
 
16409
void 
 
16410
Index_hint::print(THD *thd, String *str)
 
16411
{
 
16412
  switch (type)
 
16413
  {
 
16414
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
 
16415
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
 
16416
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
 
16417
  }
 
16418
  str->append (STRING_WITH_LEN(" ("));
 
16419
  if (key_name.length)
 
16420
  {
 
16421
    if (thd && !my_strnncoll(system_charset_info,
 
16422
                             (const uchar *)key_name.str, key_name.length, 
 
16423
                             (const uchar *)primary_key_name, 
 
16424
                             strlen(primary_key_name)))
 
16425
      str->append(primary_key_name);
 
16426
    else
 
16427
      append_identifier(thd, str, key_name.str, key_name.length);
 
16428
  }
 
16429
  str->append(')');
 
16430
}
 
16431
 
 
16432
 
 
16433
/**
 
16434
  Print table as it should be in join list.
 
16435
 
 
16436
  @param str   string where table should be printed
 
16437
*/
 
16438
 
 
16439
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
 
16440
{
 
16441
  if (nested_join)
 
16442
  {
 
16443
    str->append('(');
 
16444
    print_join(thd, str, &nested_join->join_list, query_type);
 
16445
    str->append(')');
 
16446
  }
 
16447
  else
 
16448
  {
 
16449
    const char *cmp_name;                         // Name to compare with alias
 
16450
    if (derived)
 
16451
    {
 
16452
      // A derived table
 
16453
      str->append('(');
 
16454
      derived->print(str, query_type);
 
16455
      str->append(')');
 
16456
      cmp_name= "";                               // Force printing of alias
 
16457
    }
 
16458
    else
 
16459
    {
 
16460
      // A normal table
 
16461
      {
 
16462
        append_identifier(thd, str, db, db_length);
 
16463
        str->append('.');
 
16464
      }
 
16465
      if (schema_table)
 
16466
      {
 
16467
        append_identifier(thd, str, schema_table_name,
 
16468
                          strlen(schema_table_name));
 
16469
        cmp_name= schema_table_name;
 
16470
      }
 
16471
      else
 
16472
      {
 
16473
        append_identifier(thd, str, table_name, table_name_length);
 
16474
        cmp_name= table_name;
 
16475
      }
 
16476
    }
 
16477
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
 
16478
    {
 
16479
      char t_alias_buff[MAX_ALIAS_NAME];
 
16480
      const char *t_alias= alias;
 
16481
 
 
16482
      str->append(' ');
 
16483
      if (lower_case_table_names== 1)
 
16484
      {
 
16485
        if (alias && alias[0])
 
16486
        {
 
16487
          stpcpy(t_alias_buff, alias);
 
16488
          my_casedn_str(files_charset_info, t_alias_buff);
 
16489
          t_alias= t_alias_buff;
 
16490
        }
 
16491
      }
 
16492
 
 
16493
      append_identifier(thd, str, t_alias, strlen(t_alias));
 
16494
    }
 
16495
 
 
16496
    if (index_hints)
 
16497
    {
 
16498
      List_iterator<Index_hint> it(*index_hints);
 
16499
      Index_hint *hint;
 
16500
 
 
16501
      while ((hint= it++))
 
16502
      {
 
16503
        str->append (STRING_WITH_LEN(" "));
 
16504
        hint->print (thd, str);
 
16505
      }
 
16506
    }
 
16507
  }
 
16508
}
 
16509
 
 
16510
 
 
16511
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
 
16512
{
 
16513
  /* QQ: thd may not be set for sub queries, but this should be fixed */
 
16514
  if (!thd)
 
16515
    thd= current_thd;
7060
16516
 
7061
16517
  str->append(STRING_WITH_LEN("select "));
7062
16518
 
7063
16519
  /* First add options */
7064
16520
  if (options & SELECT_STRAIGHT_JOIN)
7065
16521
    str->append(STRING_WITH_LEN("straight_join "));
 
16522
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
 
16523
      (this == &thd->lex->select_lex))
 
16524
    str->append(STRING_WITH_LEN("high_priority "));
7066
16525
  if (options & SELECT_DISTINCT)
7067
16526
    str->append(STRING_WITH_LEN("distinct "));
7068
16527
  if (options & SELECT_SMALL_RESULT)
7095
16554
  {
7096
16555
    str->append(STRING_WITH_LEN(" from "));
7097
16556
    /* go through join tree */
7098
 
    print_join(session, str, &top_join_list, query_type);
 
16557
    print_join(thd, str, &top_join_list, query_type);
7099
16558
  }
7100
16559
  else if (where)
7101
16560
  {
7102
16561
    /*
7103
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
 
16562
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
7104
16563
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
7105
16564
    */
7106
16565
    str->append(STRING_WITH_LEN(" from DUAL "));
7123
16582
  if (group_list.elements)
7124
16583
  {
7125
16584
    str->append(STRING_WITH_LEN(" group by "));
7126
 
    print_order(str, (order_st *) group_list.first, query_type);
 
16585
    print_order(str, (ORDER *) group_list.first, query_type);
7127
16586
    switch (olap)
7128
16587
    {
7129
16588
      case CUBE_TYPE:
7154
16613
  if (order_list.elements)
7155
16614
  {
7156
16615
    str->append(STRING_WITH_LEN(" order by "));
7157
 
    print_order(str, (order_st *) order_list.first, query_type);
 
16616
    print_order(str, (ORDER *) order_list.first, query_type);
7158
16617
  }
7159
16618
 
7160
16619
  // limit
7161
 
  print_limit(session, str, query_type);
 
16620
  print_limit(thd, str, query_type);
7162
16621
 
7163
16622
  // PROCEDURE unsupported here
7164
16623
}
7165
16624
 
 
16625
 
 
16626
/**
 
16627
  change select_result object of JOIN.
 
16628
 
 
16629
  @param res            new select_result object
 
16630
 
 
16631
  @retval
 
16632
    false   OK
 
16633
  @retval
 
16634
    true    error
 
16635
*/
 
16636
 
 
16637
bool JOIN::change_result(select_result *res)
 
16638
{
 
16639
  result= res;
 
16640
  if (result->prepare(fields_list, select_lex->master_unit()) ||
 
16641
                     result->prepare2())
 
16642
  {
 
16643
    return(true);
 
16644
  }
 
16645
  return(false);
 
16646
}
 
16647
 
7166
16648
/**
7167
16649
  @} (end of group Query_Optimizer)
7168
16650
*/